1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
5 \\ / A nd | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. 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
33 \*---------------------------------------------------------------------------*/
37 #include "triSurface.H"
40 #include "triFaceList.H"
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 int mapVertex(::List<int>& collapse_map, int a, int mx)
65 int main(int argc, char *argv[])
67 argList::noParallel();
68 argList::validArgs.clear();
69 argList::validArgs.append("Foam surface file");
70 argList::validArgs.append("reduction factor");
71 argList::validArgs.append("Foam output file");
72 argList args(argc, argv);
74 fileName inFileName(args.additionalArgs()[0]);
76 scalar reduction(readScalar(IStringStream(args.additionalArgs()[1])()));
78 if (reduction <= 0 || reduction > 1)
80 FatalErrorIn(args.executable())
81 << "Reduction factor " << reduction
82 << " should be within 0..1" << endl
83 << "(it is the reduction in number of vertices)"
87 fileName outFileName(args.additionalArgs()[2]);
89 Info<< "Input surface :" << inFileName << endl
90 << "Reduction factor:" << reduction << endl
91 << "Output surface :" << outFileName << endl << endl;
93 const triSurface surf(inFileName);
95 Info<< "Surface:" << endl;
96 surf.writeStats(Info);
100 ::List< ::Vector> vert; // global list of vertices
101 ::List< ::tridata> tri; // global list of triangles
104 // Convert triSurface to progmesh format. Note: can use global point
105 // numbering since surface read in from file.
106 const pointField& pts = surf.points();
110 const point& pt = pts[ptI];
112 vert.Add( ::Vector(pt.x(), pt.y(), pt.z()));
117 const labelledTri& f = surf[faceI];
126 ::List<int> collapse_map; // to which neighbor each vertex collapses
127 ::List<int> permutation;
129 ::ProgressiveMesh(vert,tri,collapse_map,permutation);
131 // rearrange the vertex list
132 ::List< ::Vector> temp_list;
133 for(int i=0;i<vert.num;i++)
135 temp_list.Add(vert[i]);
137 for(int i=0;i<vert.num;i++)
139 vert[permutation[i]]=temp_list[i];
142 // update the changes in the entries in the triangle list
143 for(int i=0;i<tri.num;i++)
147 tri[i].v[j] = permutation[tri[i].v[j]];
151 // Only get triangles with non-collapsed edges.
152 int render_num = int(reduction * surf.nPoints());
154 Info<< "Reducing to " << render_num << " vertices" << endl;
157 // Storage for new surface.
158 Foam::List<labelledTri> newTris(surf.size());
162 for (int i=0; i<tri.num; i++)
164 int p0 = mapVertex(collapse_map, tri[i].v[0], render_num);
165 int p1 = mapVertex(collapse_map, tri[i].v[1], render_num);
166 int p2 = mapVertex(collapse_map, tri[i].v[2], render_num);
168 // note: serious optimization opportunity here,
169 // by sorting the triangles the following "continue"
170 // could have been made into a "break" statement.
171 if (p0 == p1 || p1 == p2 || p2 == p0)
176 newTris[newI++] = labelledTri(p0, p1, p2, 0);
178 newTris.setSize(newI);
180 // Convert vert into pointField.
181 pointField newPoints(vert.num);
183 for(int i=0; i<vert.num; i++)
185 const ::Vector & v = vert[i];
187 newPoints[i] = point(v.x, v.y, v.z);
190 triSurface surf2(newTris, newPoints);
199 Info<< "Coarsened surface:" << endl;
200 surf2.writeStats(Info);
203 Info<< "Writing to file " << outFileName << endl << endl;
205 surf2.write(outFileName);
207 Info << "End\n" << endl;
213 // ************************************************************************* //