surfaceCoarsen.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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
19  for more details.
20 
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/>.
23 
24 Application
25  surfaceCoarsen
26 
27 Description
28  Surface coarsening using 'bunnylod':
29 
30  Polygon Reduction Demo
31  By Stan Melax (c) 1998
32  mailto:melax@cs.ualberta.ca
33  http://www.cs.ualberta.ca/~melax
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #include "argList.H"
38 #include "fileName.H"
39 #include "triSurface.H"
40 #include "OFstream.H"
41 #include "triFace.H"
42 #include "triFaceList.H"
43 
44 // From bunnylod
45 #include "progmesh.h"
46 
47 using namespace Foam;
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 int mapVertex(::List<int>& collapse_map, int a, int mx)
52 {
53  if (mx <= 0)
54  {
55  return 0;
56  }
57  while (a >= mx)
58  {
59  a = collapse_map[a];
60  }
61  return a;
62 }
63 
64 
65 
66 int main(int argc, char *argv[])
67 {
69  argList::validArgs.append("surfaceFile");
70  argList::validArgs.append("reductionFactor");
71  argList::validArgs.append("output surfaceFile");
72  argList args(argc, argv);
73 
74  const fileName inFileName = args[1];
75  const scalar reduction = args.argRead<scalar>(2);
76  const fileName outFileName = args[3];
77 
78  if (reduction <= 0 || reduction > 1)
79  {
81  << "Reduction factor " << reduction
82  << " should be within 0..1" << endl
83  << "(it is the reduction in number of vertices)"
84  << exit(FatalError);
85  }
86 
87  Info<< "Input surface :" << inFileName << endl
88  << "Reduction factor:" << reduction << endl
89  << "Output surface :" << outFileName << endl << endl;
90 
91  const triSurface surf(inFileName);
92 
93  Info<< "Surface:" << endl;
94  surf.writeStats(Info);
95  Info<< endl;
96 
97 
98  ::List< ::Vector> vert; // global list of vertices
99  ::List< ::tridata> tri; // global list of triangles
100 
101 
102  // Convert triSurface to progmesh format. Note: can use global point
103  // numbering since surface read in from file.
104  const pointField& pts = surf.points();
105 
106  forAll(pts, ptI)
107  {
108  const point& pt = pts[ptI];
109 
110  vert.Add( ::Vector(pt.x(), pt.y(), pt.z()));
111  }
112 
113  forAll(surf, facei)
114  {
115  const labelledTri& f = surf[facei];
116 
117  tridata td;
118  td.v[0]=f[0];
119  td.v[1]=f[1];
120  td.v[2]=f[2];
121  tri.Add(td);
122  }
123 
124  ::List<int> collapse_map; // to which neighbor each vertex collapses
125  ::List<int> permutation;
126 
127  ::ProgressiveMesh(vert,tri,collapse_map,permutation);
128 
129  // rearrange the vertex list
130  ::List< ::Vector> temp_list;
131  for (int i=0;i<vert.num;i++)
132  {
133  temp_list.Add(vert[i]);
134  }
135  for (int i=0;i<vert.num;i++)
136  {
137  vert[permutation[i]]=temp_list[i];
138  }
139 
140  // update the changes in the entries in the triangle list
141  for (int i=0;i<tri.num;i++)
142  {
143  for (int j=0;j<3;j++)
144  {
145  tri[i].v[j] = permutation[tri[i].v[j]];
146  }
147  }
148 
149  // Only get triangles with non-collapsed edges.
150  int render_num = int(reduction * surf.nPoints());
151 
152  Info<< "Reducing to " << render_num << " vertices" << endl;
153 
154 
155  // Storage for new surface.
156  Foam::List<labelledTri> newTris(surf.size());
157 
158  label newI = 0;
159 
160  for (int i=0; i<tri.num; i++)
161  {
162  int p0 = mapVertex(collapse_map, tri[i].v[0], render_num);
163  int p1 = mapVertex(collapse_map, tri[i].v[1], render_num);
164  int p2 = mapVertex(collapse_map, tri[i].v[2], render_num);
165 
166  // note: serious optimization opportunity here,
167  // by sorting the triangles the following "continue"
168  // could have been made into a "break" statement.
169  if (p0 == p1 || p1 == p2 || p2 == p0)
170  {
171  continue;
172  }
173 
174  newTris[newI++] = labelledTri(p0, p1, p2, 0);
175  }
176  newTris.setSize(newI);
177 
178  // Convert vert into pointField.
179  pointField newPoints(vert.num);
180 
181  for (int i=0; i<vert.num; i++)
182  {
183  const ::Vector & v = vert[i];
184 
185  newPoints[i] = point(v.x, v.y, v.z);
186  }
187 
188  triSurface surf2(newTris, newPoints);
189 
190  triSurface outSurf
191  (
192  surf2.localFaces(),
193  surf2.patches(),
194  surf2.localPoints()
195  );
196 
197  Info<< "Coarsened surface:" << endl;
198  surf2.writeStats(Info);
199  Info<< endl;
200 
201  Info<< "Writing to file " << outFileName << endl << endl;
202 
203  surf2.write(outFileName);
204 
205  Info<< "End\n" << endl;
206 
207  return 0;
208 }
209 
210 
211 // ************************************************************************* //
const Cmpt & z() const
Definition: VectorI.H:87
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const Cmpt & x() const
Definition: VectorI.H:75
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
static void noParallel()
Remove the parallel options.
Definition: argList.C:146
static SLList< string > validArgs
A list of valid (mandatory) arguments.
Definition: argList.H:154
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:102
const Cmpt & y() const
Definition: VectorI.H:81
Triangle with additional region number.
Definition: labelledTri.H:57
T argRead(const label index) const
Read a value from the argument at index.
Definition: argListI.H:177
vector point
Point is a vector.
Definition: point.H:41
messageStream Info
virtual Ostream & write(const token &)=0
Write next token to stream.
Triangulated surface description with patch information.
Definition: triSurface.H:65
Foam::argList args(argc, argv)
Namespace for OpenFOAM.