DGtal 1.4.0
Loading...
Searching...
No Matches
testMesh.cpp
Go to the documentation of this file.
1
31#include <iostream>
32#include "DGtal/base/Common.h"
33#include "DGtal/helpers/StdDefs.h"
34#include "DGtal/io/writers/MeshWriter.h"
35#include "DGtal/shapes/Mesh.h"
37
38using namespace std;
39using namespace DGtal;
40using namespace DGtal::Z2i;
41
42
43
45// Functions for testing class Mesh.
47
52{
53
54 trace.beginBlock ( "Testing Mesh ..." );
55 bool ok = true;
56 trace.beginBlock ( "Testing Mesh contruction ..." );
57 Mesh<Point> aMesh;
58 Point p0=Point(0,0);
59 Point p1=Point(0,1);
60 Point p2=Point(1,2);
61 Point p3=Point(3,2);
62 Point p4=Point(3,3);
63 Point p5=Point(3,4);
64
65 aMesh.addVertex(p0);
66 aMesh.addVertex(p1);
67 aMesh.addVertex(p2);
68 aMesh.addVertex(p3);
69 aMesh.addVertex(p4);
70 aMesh.addVertex(p5);
71
72 aMesh.addTriangularFace(0,1,2);
73 aMesh.addTriangularFace(3,4,5);
74
75 Mesh<Point>::MeshFace tface0 = aMesh.getFace(0);
76 Mesh<Point>::MeshFace tface1 = aMesh.getFace(1);
77 Point p0f0 = aMesh.getVertex(tface0.at(0));
78 Point p1f0 = aMesh.getVertex(tface0.at(1));
79 Point p2f0 = aMesh.getVertex(tface0.at(2));
80
81 Point p0f1 = aMesh.getVertex(tface1.at(0));
82 Point p1f1 = aMesh.getVertex(tface1.at(1));
83 Point p2f1 = aMesh.getVertex(tface1.at(2));
84 trace.info() << "Set of points" << endl;
85 trace.info() << p0 << p1 << p2 << endl;
86 trace.info() << p3 << p4 << p5 << endl;
87
88 trace.info() << "Face1 points " << endl;
89 trace.info() << p0f0 << p1f0 << p2f0<< endl;
90
91 trace.info() << "Face2 points " << endl;
92 trace.info() << p0f1 << p1f1 << p2f1<< endl;
93
94 //Checking inversion
97
98
99 bool okMeshConstruct = (p0==p0f0) && (p1==p1f0) && (p2==p2f0) &&
100 (p3==p0f1) && (p4==p1f1) && (p5==p2f1) ;
101
102 trace.endBlock();
103 bool okMeshIterators = true;
104 trace.beginBlock ( "Testing Mesh iterator ..." );
105 unsigned int nb=0;
106 // just testing nb iterations on const iterator
107 for( Mesh<Point>::VertexStorage::const_iterator it = aMesh.vertexBegin();
108 it !=aMesh.vertexEnd();
109 it++){
110 nb++;
111 }
112 okMeshIterators = okMeshIterators && (nb == aMesh.nbVertex());
113 if (nb == aMesh.nbVertex())
114 trace.info() << "vertex iteration test ok"<<std::endl;
115
116 // testing to change vertex on iterator
117 for( Mesh<Point>::VertexStorage::iterator it = aMesh.vertexBegin();
118 it !=aMesh.vertexEnd();
119 it++){
120 (*it)[0]+=10; (*it)[1]+=5;
121 }
122 // just testing nb iterations on const iterator
123 nb=0;
124 for( Mesh<Point>::FaceStorage::const_iterator it = aMesh.faceBegin();
125 it !=aMesh.faceEnd();
126 it++){
127 nb++;
128 }
129 okMeshIterators = okMeshIterators && (nb == aMesh.nbFaces());
130 if (nb == aMesh.nbFaces())
131 trace.info() << "face iteration test ok"<<std::endl;
132
133 nb=0;
134 // just testing nb iterations on const iterator
135 for( Mesh<Point>::FaceStorage::iterator it = aMesh.faceBegin();
136 it !=aMesh.faceEnd();
137 it++){
138 nb++;
139 }
140 okMeshIterators = okMeshIterators && ((nb == aMesh.nbFaces()) && ((aMesh.getVertex(5))[0]==13)) && aMesh.getFaceBarycenter(0)==Mesh<Point>::RealPoint(31.0/3.0,6.0);
141 if ((nb == aMesh.nbFaces()) && (aMesh.getVertex(5))[0]==13 && aMesh.getFaceBarycenter(0)==Mesh<Point>::RealPoint(31.0/3.0,6.0))
142 trace.info() << "getVertex and getFaceCenter tests ok"<<std::endl;
143
144 // testing changing color of individual face:
146 bool okMeshColor = (aMesh.getFaceColor(0)==DGtal::Color::White)
147 && (aMesh.getFaceColor(1)==DGtal::Color::Red) ;
148
149 trace.endBlock();
150
151 trace.beginBlock ( "Testing Mesh Bouding box and scale change ..." );
152 aMesh.rescale(2);
153 std::pair<Point, Point> bb = aMesh.getBoundingBox();
154 bool boundingBoxOK = (bb.first == Point(20,10)) && (bb.second == Point(26,18));
155 trace.info() << "bouding box=" << bb.first << " " << bb.second << "(should be (20,10) (26,18)" <<std::endl;
156 trace.endBlock();
157
158 trace.beginBlock ( "Testing mesh subdivision ..." );
159 Mesh<RealPoint> aMeshR;
160 RealPoint pr0 (0,0);
161 RealPoint pr1 (1,0);
162 RealPoint pr2 (1,1);
163 aMeshR.addVertex(pr0); aMeshR.addVertex(pr1); aMeshR.addVertex(pr2);
164 aMeshR.addTriangularFace(0,1,2);
165 trace.info() << "nb vertices before subdivision: " << aMeshR.nbVertex() << std::endl;
166 trace.info() << "nb faces before subdivision: " << aMeshR.nbFaces() << std::endl;
167 aMeshR.subDivideTriangularFaces(0.5);
168 trace.info() << "nb vertices after subdivision: " << aMeshR.nbVertex() << " (should be 4)"<<std::endl;
169 trace.info() << "nb faces after subdivision: " << aMeshR.nbFaces() << " (should be 3)" <<std::endl;
170 trace.info() << "New point: " << aMeshR.getVertex(aMeshR.nbVertex()-1) << "(should be: "<< RealPoint(2.0/3.0, 1.0/3.0) << ") "<< std::endl;
171 bool okSubDivide = aMeshR.nbVertex()==4 && aMeshR.nbFaces()==3 &&
172 aMeshR.getVertex(aMeshR.nbVertex()-1) == RealPoint(2.0/3.0, 1.0/3.0);
173 trace.info() << (okSubDivide ? "[subdivise OK]":"[subdivise fail]" ) << std::endl;
174 trace.endBlock();
175
176 trace.beginBlock ( "Testing mesh quad transform ..." );
177 Mesh<RealPoint> aMeshQ;
178 RealPoint pq0 (0,0);
179 RealPoint pq1 (1,0);
180 RealPoint pq2 (1,1);
181 RealPoint pq3 (0,1);
182 aMeshQ.addVertex(pq0); aMeshQ.addVertex(pq1); aMeshQ.addVertex(pq2);
183 aMeshQ.addVertex(pq3);
184 aMeshQ.addQuadFace(0,1,2,3);
185 aMeshQ.quadToTriangularFaces();
186
187 trace.info() << "nb faces after quad to triangle transform: " << aMeshQ.nbFaces() ;
188 bool okQuadToTrans = aMeshQ.nbFaces() == 2;
189 trace.info() << "(should be 2) "<< (okQuadToTrans? "[ok]": "[error]") << std::endl;
190 trace.endBlock();
191
192
193 trace.beginBlock ( "Testing Mesh copy operator ..." );
194 Mesh<Point> aMesh2 = aMesh;
195 Mesh<Point> aMesh3 (aMesh2);
196 bool okMeshCopy = aMesh.nbFaces() == aMesh2.nbFaces() && aMesh.nbVertex() == aMesh2.nbVertex() &&
197 aMesh.nbFaces() == aMesh3.nbFaces() && aMesh.nbVertex() == aMesh3.nbVertex() &&
198 aMesh.getVertex(0) == aMesh2.getVertex(0) && aMesh.getVertex(0) == aMesh3.getVertex(0);
199 trace.info() << (okMeshCopy ? "[copy ok]":"[copy fail]" ) << std::endl;
200 trace.endBlock();
201
202 trace.beginBlock ( "Testing face removing ..." );
203 Mesh<Point> aMesh4 = aMesh;
204 std::vector<typename Mesh<Point>::Index> f = {1};
205 aMesh4.removeFaces(f);
206 bool okRemoveFace = (aMesh4.nbFaces() == aMesh.nbFaces()-1) && (aMesh4.nbVertex() == aMesh.nbVertex()-3);
207 trace.info() << (okRemoveFace ? "[face remove ok]":"[face remove fail]" ) << std::endl;
208 trace.endBlock();
209
210 trace.beginBlock ( "Testing mesh cleaning ..." );
211 Mesh<RealPoint> aMeshClean;
212 RealPoint pc0 (0,0);
213 RealPoint pc1 (1,0);
214 RealPoint pc2 (1,1);
215 RealPoint pc3 (0,1);
216 RealPoint pc4 (1,-1);
217 aMeshClean.addVertex(pc3);aMeshClean.addVertex(pc0); aMeshClean.addVertex(pc1); aMeshClean.addVertex(pc2);
218 aMeshClean.addVertex(pc4);
219
220 aMeshClean.addTriangularFace(1,2,3);
221 aMeshClean.addTriangularFace(4,1,2);
222 aMeshClean.removeIsolatedVertices();
223
224 trace.info() << "nb vertex after clean: " << aMeshClean.nbVertex() ;
225 bool okClean = aMeshClean.nbVertex() == 4;
226 trace.info() << "get firt vertex index of second face : " << aMeshClean.nbVertex() ;
227 bool okClean2 = aMeshClean.getFace(1)[0] == 3;
228 trace.info() << "(should be 3) "<< (okClean2? "[ok]": "[error]") << std::endl;
229 trace.endBlock();
230
231
232 ok = ok & okMeshConstruct && okMeshIterators && okMeshColor && okMeshCopy && boundingBoxOK &&
233 okSubDivide && okQuadToTrans && okRemoveFace && okClean && okClean2;
234
235
236 return ok;
237
238}
239
240
241
246{
247
248 trace.beginBlock ( "Testing Mesh generation ..." );
249 bool ok = true;
250
251 trace.beginBlock ( "Testing Tube generation ..." );
253 std::vector<Z3i::RealPoint> aSkeleton;
254 aSkeleton.push_back(Z3i::RealPoint(0.0, 0.0, 0.0));
255 aSkeleton.push_back(Z3i::RealPoint(10.0, 0.0, 0.0));
256 aSkeleton.push_back(Z3i::RealPoint(20.0, 0.0, 0.0));
257 aSkeleton.push_back(Z3i::RealPoint(30.0, 0.0, 0.0));
258 aSkeleton.push_back(Z3i::RealPoint(35.0, 5.0, 0.0));
259 aSkeleton.push_back(Z3i::RealPoint(40.0, 10.0, 0.0));
260 aSkeleton.push_back(Z3i::RealPoint(40.0, 20.0, 0.0));
261 aSkeleton.push_back(Z3i::RealPoint(40.0, 30.0, 0.0));
262 aSkeleton.push_back(Z3i::RealPoint(40.0, 35.0, 5.0));
263 aSkeleton.push_back(Z3i::RealPoint(40.0, 40.0, 10.0));
264 aSkeleton.push_back(Z3i::RealPoint(40.0, 40.0, 20.0));
266
268 Mesh<Z3i::RealPoint> aMesh(true);
271
272 trace.endBlock();
273 trace.info() << "Nb faces: "<< aMesh.nbFaces() << " (sould be 320)" << std::endl;
274 trace.info() << "Nb vertices: "<< aMesh.nbVertex() << " (sould be 352)" << std::endl;
275 bool okMeshTube1 = aMesh.nbFaces() == 320 && aMesh.nbVertex() == 352;
276
277 trace.beginBlock ( "Testing Tube generation (bis with variable raidii ..." );
278 Mesh<Z3i::RealPoint> aMeshBis(true);
279 std::vector<double> vectRadii;
280 vectRadii.push_back(0.5);
281 vectRadii.push_back(1.5);
282 vectRadii.push_back(2.5);
283 Mesh<Z3i::RealPoint>::createTubularMesh(aMeshBis, aSkeleton, vectRadii, 0.2, DGtal::Color::Green);
284
285 trace.endBlock();
286 trace.info() << "Nb faces: "<< aMeshBis.nbFaces() << " (sould be 320)" << std::endl;
287 trace.info() << "Nb vertices: "<< aMeshBis.nbVertex() << " (sould be 352)" << std::endl;
288
289 std::ofstream ofbis ("tubeVariableRadiiGeneratedFromTestMesh.off");
291 ofbis.close();
292 bool okMeshTube1bis = aMeshBis.nbFaces() == 320 && aMeshBis.nbVertex() == 352;
293
294
295 trace.beginBlock("Testing Mesh from Height sequence");
297 std::vector<double> heightSequence;
298 heightSequence.push_back(0.1);
299 heightSequence.push_back(0.2);
300 heightSequence.push_back(0.15);
301
302 heightSequence.push_back(1.1);
303 heightSequence.push_back(2.2);
304 heightSequence.push_back(1.15);
305
306 heightSequence.push_back(0.1);
307 heightSequence.push_back(0.2);
308 heightSequence.push_back(0.15);
310
314
315 trace.info() << "Nb faces: "<< aMesh.nbFaces() << " (sould be 324)" << std::endl;
316 trace.info() << "Nb vertices: "<< aMesh.nbVertex() << " (sould be 361)" << std::endl;
317 bool okMeshTube1AndHF = aMesh.nbFaces() == 324 && aMesh.nbVertex() == 361;
318
320 std::ofstream of ("tubeAndHeighFieldGeneratedFromTestMesh.off");
322 of.close();
324
325 ok = ok & okMeshTube1 & okMeshTube1bis & okMeshTube1AndHF;
326 trace.endBlock();
327 return ok;
328}
329
330
331
336{
337 unsigned int nbok = 0;
338 unsigned int nb = 0;
339
340 trace.beginBlock("Testing visual tubular mesh generation (shell mesh):");
341 // Generate the center line:
342 std::vector<Z3i::RealPoint> centerline;
343 unsigned int nbPoints = 0;
344 double z = 0.0;
345 double radiusSpirale = 13.0;
346 double radiusTube = 15.0;
347 double alphaMax = 32.0;
348 double reduc = 0.05;
349 for (double alpha = 0; alpha< alphaMax; alpha += 0.1, z += 0.5-reduc)
350 {
351 centerline.push_back(Z3i::RealPoint(radiusSpirale*cos(alpha), radiusSpirale*sin(alpha), z ));
352 nbPoints++;
353 radiusSpirale -=reduc;
354 radiusSpirale = std::max(radiusSpirale, 0.0);
355 }
356 // Generate radius:
357 std::vector<double> vectRadius;
358 for(unsigned int i=0; i<nbPoints; i++)
359 {
360 vectRadius.push_back(radiusTube);
361 radiusTube -=reduc;
362 radiusTube = std::max(radiusTube, 0.0);
363 }
364
365 DGtal::Mesh<Z3i::RealPoint> theMeshShell(true);
366 DGtal::Mesh<Z3i::RealPoint>::createTubularMesh(theMeshShell, centerline, vectRadius, 0.1);
367
368 trace.info() << "Mesh generated with " << theMeshShell.nbFaces()
369 << " faces (should be " << (centerline.size()-1)*63 << " )" << std::endl;
370 nb++;
371 nbok += theMeshShell.nbFaces() == (centerline.size()-1)*63;
372 theMeshShell >> "spiraleGeneratedFromTestMesh.off";
373 trace.info() << " [done]" << std::endl;
374 trace.endBlock();
375
376
377
378 trace.beginBlock("Testing visual tubular mesh generation (tube mesh):");
379 std::vector<Z3i::RealPoint> centerLine2;
380 centerLine2.push_back(Z3i::RealPoint(0.0,0.0,0.0));
381 centerLine2.push_back(Z3i::RealPoint(3.3,0.0,0.0));
382 centerLine2.push_back(Z3i::RealPoint(6.6,0.0,0.0));
383 centerLine2.push_back(Z3i::RealPoint(10.0,0.0,0.0));
384 centerLine2.push_back(Z3i::RealPoint(13.3,0.0,0.0));
385 centerLine2.push_back(Z3i::RealPoint(16.6,0.0,0.0));
386 centerLine2.push_back(Z3i::RealPoint(20.0,0.0,0.0));
387 centerLine2.push_back(Z3i::RealPoint(60.0,0.0,0.0));
388 centerLine2.push_back(Z3i::RealPoint(63.3,0.0,0.0));
389 centerLine2.push_back(Z3i::RealPoint(66.6,0.0,0.0));
390 centerLine2.push_back(Z3i::RealPoint(70.0,0.0,0.0));
391 centerLine2.push_back(Z3i::RealPoint(71.7,0.1,0.0));
392 centerLine2.push_back(Z3i::RealPoint(73.4,0.6,0.0));
393 centerLine2.push_back(Z3i::RealPoint(75.0,1.3,0.0));
394 centerLine2.push_back(Z3i::RealPoint(76.4,2.3,0.0));
395 centerLine2.push_back(Z3i::RealPoint(77.6,3.5,0.0));
396 centerLine2.push_back(Z3i::RealPoint(78.6,5.0,0.0));
397 centerLine2.push_back(Z3i::RealPoint(79.3,6.5,0.0));
398 centerLine2.push_back(Z3i::RealPoint(79.8,8.2,0.0));
399 centerLine2.push_back(Z3i::RealPoint(80.0,10.0,0.0));
400 centerLine2.push_back(Z3i::RealPoint(80.0,13.8,0.0));
401 centerLine2.push_back(Z3i::RealPoint(80.0,86.1,0.0));
402 centerLine2.push_back(Z3i::RealPoint(80.0,90.0,0.0));
403 centerLine2.push_back(Z3i::RealPoint(80.1,91.7,-0.1));
404 centerLine2.push_back(Z3i::RealPoint(80.6,93.4,0.1));
405 centerLine2.push_back(Z3i::RealPoint(81.3,95.0,0.1));
406 centerLine2.push_back(Z3i::RealPoint(82.3,96.4,-0.1));
407 centerLine2.push_back(Z3i::RealPoint(83.5,97.6,-0.1));
408
409 DGtal::Mesh<Z3i::RealPoint> theMeshTube(true);
411 5.0, 0.2, DGtal::Color::Blue);
412
413 trace.info() << "Mesh generated with " << theMeshTube.nbFaces() << " faces (should be "
414 << (centerLine2.size()-1)*32 << " )" << std::endl;
415 nb++;
416 nbok += theMeshTube.nbFaces() == (centerLine2.size()-1)*32;
417
418 theMeshTube >> "tubeGeneratedFromTestMesh.off";
419 trace.endBlock();
420
421 return nb == nbok;
422
423}
424
426// Standard services - public :
427
428int main( int argc, char** argv )
429{
430 trace.beginBlock ( "Testing class Mesh" );
431 trace.info() << "Args:";
432 for ( int i = 0; i < argc; ++i )
433 trace.info() << " " << argv[ i ];
434 trace.info() << endl;
435
437 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
438 trace.endBlock();
439 return res ? 0 : 1;
440}
441// //
static const Color Yellow
Definition Color.h:422
static const Color Green
Definition Color.h:417
static const Color Red
Definition Color.h:416
static const Color White
Definition Color.h:415
static const Color Blue
Definition Color.h:419
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition Mesh.h:92
const Color & getFaceColor(Index i) const
DGtal::PointVector< TPoint::dimension, double > RealPoint
Definition Mesh.h:109
std::pair< TPoint, TPoint > getBoundingBox() const
void addQuadFace(Index indexVertex1, Index indexVertex2, Index indexVertex3, Index indexVertex4, const DGtal::Color &aColor=DGtal::Color::White)
void rescale(const typename TPoint::Component aScale)
Size nbFaces() const
RealPoint getFaceBarycenter(Index i) const
unsigned int quadToTriangularFaces()
Size nbVertex() const
const MeshFace & getFace(Index i) const
void setFaceColor(Index i, const DGtal::Color &aColor)
FaceStorage::const_iterator faceEnd() const
Definition Mesh.h:414
void invertVertexFaceOrder()
static void createMeshFromHeightSequence(Mesh< TPoint > &aMesh, const std::vector< TValue > &anValueSequence, const unsigned int lengthSequence, double stepX, double stepY, double stepZ, const DGtal::Color &aMeshColor=DGtal::Color::White)
const TPoint & getVertex(Index i) const
ConstIterator vertexEnd() const
Definition Mesh.h:369
std::vector< Index > MeshFace
Definition Mesh.h:126
void removeFaces(const std::vector< Index > &facesIndex)
double subDivideTriangularFaces(const double minArea)
void addTriangularFace(Index indexVertex1, Index indexVertex2, Index indexVertex3, const DGtal::Color &aColor=DGtal::Color::White)
FaceStorage::const_iterator faceBegin() const
Definition Mesh.h:402
ConstIterator vertexBegin() const
Definition Mesh.h:359
void removeIsolatedVertices()
static void createTubularMesh(Mesh< TPoint > &aMesh, const std::vector< TPoint > &aSkeleton, const double aRadius, const double angleStep=0.2, const DGtal::Color &aMeshColor=DGtal::Color::White)
void addVertex(const TPoint &vertex)
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Z2i this namespace gathers the standard of types for 2D imagery.
Space::RealPoint RealPoint
Definition StdDefs.h:97
Space::Point Point
Definition StdDefs.h:95
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition Common.h:153
STL namespace.
static bool export2OFF(std::ostream &out, const Mesh< TPoint > &aMesh, bool exportColor=true)
int main()
Definition testBits.cpp:56
bool testMeshGeneration()
Definition testMesh.cpp:245
bool testMesh()
Definition testMesh.cpp:51
bool testVisualTubularMesh()
Definition testMesh.cpp:335