OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

surfaceCoarsen.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Description
00026     Surface coarsening using 'bunnylod':
00027 
00028     Polygon Reduction Demo
00029     By Stan Melax (c) 1998
00030     mailto:melax@cs.ualberta.ca
00031     http://www.cs.ualberta.ca/~melax
00032 
00033 
00034 \*---------------------------------------------------------------------------*/
00035 
00036 #include "argList.H"
00037 #include "fileName.H"
00038 #include "triSurface.H"
00039 #include "OFstream.H"
00040 #include "triFace.H"
00041 #include "triFaceList.H"
00042 
00043 // From bunnylod
00044 #include "progmesh.h"
00045 
00046 using namespace Foam;
00047 
00048 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00049 
00050 int mapVertex(::List<int>& collapse_map, int a, int mx)
00051 {
00052     if (mx <= 0)
00053     {
00054         return 0;
00055     }
00056     while (a >= mx)
00057     {  
00058     a = collapse_map[a];
00059     }
00060     return a;
00061 }
00062 
00063 
00064 // Main program:
00065 
00066 int main(int argc, char *argv[])
00067 {
00068     argList::noParallel();
00069     argList::validArgs.clear();
00070     argList::validArgs.append("Foam surface file");
00071     argList::validArgs.append("reduction factor");
00072     argList::validArgs.append("Foam output file");
00073     argList args(argc, argv);
00074 
00075     fileName inFileName(args.additionalArgs()[0]);
00076 
00077     scalar reduction(readScalar(IStringStream(args.additionalArgs()[1])()));
00078 
00079     if (reduction <= 0 || reduction > 1)
00080     {
00081         FatalErrorIn(args.executable())
00082             << "Reduction factor " << reduction
00083             << " should be within 0..1" << endl
00084             << "(it is the reduction in number of vertices)"
00085             << exit(FatalError);
00086     }
00087 
00088     fileName outFileName(args.additionalArgs()[2]);
00089 
00090     Info<< "Input surface   :" << inFileName << endl
00091         << "Reduction factor:" << reduction << endl
00092         << "Output surface  :" << outFileName << endl << endl;
00093 
00094     const triSurface surf(inFileName);
00095 
00096     Info<< "Surface:" << endl;
00097     surf.writeStats(Info);
00098     Info<< endl;
00099 
00100 
00101     ::List< ::Vector> vert;     // global list of vertices
00102     ::List< ::tridata> tri;     // global list of triangles
00103 
00104 
00105     // Convert triSurface to progmesh format. Note: can use global point
00106     // numbering since surface read in from file.
00107     const pointField& pts = surf.points();
00108 
00109     forAll(pts, ptI)
00110     {
00111         const point& pt = pts[ptI];
00112 
00113         vert.Add( ::Vector(pt.x(), pt.y(), pt.z()));
00114     }
00115 
00116     forAll(surf, faceI)
00117     {
00118         const labelledTri& f = surf[faceI];
00119 
00120         tridata td;
00121         td.v[0]=f[0];
00122         td.v[1]=f[1];
00123         td.v[2]=f[2];
00124         tri.Add(td);
00125     }        
00126 
00127     ::List<int> collapse_map;   // to which neighbor each vertex collapses
00128     ::List<int> permutation;
00129 
00130     ::ProgressiveMesh(vert,tri,collapse_map,permutation);
00131 
00132     // rearrange the vertex list 
00133     ::List< ::Vector> temp_list;
00134     for(int i=0;i<vert.num;i++)
00135     {
00136         temp_list.Add(vert[i]);
00137     }
00138     for(int i=0;i<vert.num;i++)
00139     {
00140         vert[permutation[i]]=temp_list[i];
00141     }
00142 
00143     // update the changes in the entries in the triangle list
00144     for(int i=0;i<tri.num;i++)
00145     {
00146         for(int j=0;j<3;j++)
00147         {
00148             tri[i].v[j] = permutation[tri[i].v[j]];
00149         }
00150     }
00151 
00152     // Only get triangles with non-collapsed edges.
00153     int render_num = int(reduction * surf.nPoints());
00154 
00155     Info<< "Reducing to " << render_num << " vertices" << endl;
00156 
00157 
00158     // Storage for new surface.
00159     Foam::List<labelledTri> newTris(surf.size());
00160 
00161     label newI = 0;
00162 
00163     for (int i=0; i<tri.num; i++)
00164     {
00165         int p0 = mapVertex(collapse_map, tri[i].v[0], render_num);
00166         int p1 = mapVertex(collapse_map, tri[i].v[1], render_num);
00167         int p2 = mapVertex(collapse_map, tri[i].v[2], render_num);
00168 
00169         // note:  serious optimization opportunity here,
00170         //  by sorting the triangles the following "continue" 
00171         //  could have been made into a "break" statement.
00172         if (p0 == p1 || p1 == p2 || p2 == p0)
00173         {
00174             continue;
00175         }
00176 
00177         newTris[newI++] = labelledTri(p0, p1, p2, 0);
00178     }
00179     newTris.setSize(newI);
00180 
00181     // Convert vert into pointField.
00182     pointField newPoints(vert.num);
00183 
00184     for(int i=0; i<vert.num; i++)
00185     {
00186         const ::Vector & v = vert[i];
00187 
00188         newPoints[i] = point(v.x, v.y, v.z);
00189     }
00190 
00191     triSurface surf2(newTris, newPoints);
00192 
00193     triSurface outSurf
00194     (
00195         surf2.localFaces(),
00196         surf2.patches(),
00197         surf2.localPoints()
00198     );
00199 
00200     Info<< "Coarsened surface:" << endl;
00201     surf2.writeStats(Info);
00202     Info<< endl;
00203 
00204     Info<< "Writing to file " << outFileName << endl << endl;
00205 
00206     surf2.write(outFileName);
00207 
00208     Info << "End\n" << endl;
00209 
00210     return 0;
00211 }
00212 
00213 
00214 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd