1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 Surface coarsening using 'bunnylod':
27 Polygon Reduction Demo
28 By Stan Melax (c) 1998
29 mailto:melax@cs.ualberta.ca
30 http://www.cs.ualberta.ca/~melax
32 \*---------------------------------------------------------------------------*/
36 #include "triSurface.H"
39 #include "triFaceList.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 int mapVertex(::List<int>& collapse_map, int a, int mx)
64 int main(int argc, char *argv[])
66 argList::noParallel();
67 argList::validArgs.append("surfaceFile");
68 argList::validArgs.append("reductionFactor");
69 argList::validArgs.append("output surfaceFile");
70 argList args(argc, argv);
72 const fileName inFileName = args[1];
73 const scalar reduction = args.argRead<scalar>(2);
74 const fileName outFileName = args[3];
76 if (reduction <= 0 || reduction > 1)
78 FatalErrorIn(args.executable())
79 << "Reduction factor " << reduction
80 << " should be within 0..1" << endl
81 << "(it is the reduction in number of vertices)"
85 Info<< "Input surface :" << inFileName << endl
86 << "Reduction factor:" << reduction << endl
87 << "Output surface :" << outFileName << endl << endl;
89 const triSurface surf(inFileName);
91 Info<< "Surface:" << endl;
92 surf.writeStats(Info);
96 ::List< ::Vector> vert; // global list of vertices
97 ::List< ::tridata> tri; // global list of triangles
100 // Convert triSurface to progmesh format. Note: can use global point
101 // numbering since surface read in from file.
102 const pointField& pts = surf.points();
106 const point& pt = pts[ptI];
108 vert.Add( ::Vector(pt.x(), pt.y(), pt.z()));
113 const labelledTri& f = surf[faceI];
122 ::List<int> collapse_map; // to which neighbor each vertex collapses
123 ::List<int> permutation;
125 ::ProgressiveMesh(vert,tri,collapse_map,permutation);
127 // rearrange the vertex list
128 ::List< ::Vector> temp_list;
129 for (int i=0;i<vert.num;i++)
131 temp_list.Add(vert[i]);
133 for (int i=0;i<vert.num;i++)
135 vert[permutation[i]]=temp_list[i];
138 // update the changes in the entries in the triangle list
139 for (int i=0;i<tri.num;i++)
141 for (int j=0;j<3;j++)
143 tri[i].v[j] = permutation[tri[i].v[j]];
147 // Only get triangles with non-collapsed edges.
148 int render_num = int(reduction * surf.nPoints());
150 Info<< "Reducing to " << render_num << " vertices" << endl;
153 // Storage for new surface.
154 Foam::List<labelledTri> newTris(surf.size());
158 for (int i=0; i<tri.num; i++)
160 int p0 = mapVertex(collapse_map, tri[i].v[0], render_num);
161 int p1 = mapVertex(collapse_map, tri[i].v[1], render_num);
162 int p2 = mapVertex(collapse_map, tri[i].v[2], render_num);
164 // note: serious optimization opportunity here,
165 // by sorting the triangles the following "continue"
166 // could have been made into a "break" statement.
167 if (p0 == p1 || p1 == p2 || p2 == p0)
172 newTris[newI++] = labelledTri(p0, p1, p2, 0);
174 newTris.setSize(newI);
176 // Convert vert into pointField.
177 pointField newPoints(vert.num);
179 for (int i=0; i<vert.num; i++)
181 const ::Vector & v = vert[i];
183 newPoints[i] = point(v.x, v.y, v.z);
186 triSurface surf2(newTris, newPoints);
195 Info<< "Coarsened surface:" << endl;
196 surf2.writeStats(Info);
199 Info<< "Writing to file " << outFileName << endl << endl;
201 surf2.write(outFileName);
203 Info<< "End\n" << endl;
209 // ************************************************************************* //