DGtal  1.2.0
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 
38 using namespace std;
39 using namespace DGtal;
40 using namespace DGtal::Z2i;
41 
42 
43 
45 // Functions for testing class Mesh.
47 
51 bool testMesh()
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 
95  bool okMeshConstruct = (p0==p0f0) && (p1==p1f0) && (p2==p2f0) &&
96  (p3==p0f1) && (p4==p1f1) && (p5==p2f1) ;
97 
98  trace.endBlock();
99  bool okMeshIterators = true;
100  trace.beginBlock ( "Testing Mesh iterator ..." );
101  unsigned int nb=0;
102  // just testing nb iterations on const iterator
104  it !=aMesh.vertexEnd();
105  it++){
106  nb++;
107  }
108  okMeshIterators = okMeshIterators && (nb == aMesh.nbVertex());
109  if (nb == aMesh.nbVertex())
110  trace.info() << "vertex iteration test ok"<<std::endl;
111 
112  // testing to change vertex on iterator
114  it !=aMesh.vertexEnd();
115  it++){
116  (*it)[0]+=10; (*it)[1]+=5;
117  }
118  // just testing nb iterations on const iterator
119  nb=0;
121  it !=aMesh.faceEnd();
122  it++){
123  nb++;
124  }
125  okMeshIterators = okMeshIterators && (nb == aMesh.nbFaces());
126  if (nb == aMesh.nbFaces())
127  trace.info() << "face iteration test ok"<<std::endl;
128 
129  nb=0;
130  // just testing nb iterations on const iterator
132  it !=aMesh.faceEnd();
133  it++){
134  nb++;
135  }
136  okMeshIterators = okMeshIterators && ((nb == aMesh.nbFaces()) && ((aMesh.getVertex(5))[0]==13)) && aMesh.getFaceBarycenter(0)==Mesh<Point>::RealPoint(31.0/3.0,6.0);
137  if ((nb == aMesh.nbFaces()) && (aMesh.getVertex(5))[0]==13 && aMesh.getFaceBarycenter(0)==Mesh<Point>::RealPoint(31.0/3.0,6.0))
138  trace.info() << "getVertex and getFaceCenter tests ok"<<std::endl;
139 
140  // testing changing color of individual face:
142  bool okMeshColor = (aMesh.getFaceColor(0)==DGtal::Color::White)
143  && (aMesh.getFaceColor(1)==DGtal::Color::Red) ;
144 
145  trace.endBlock();
146 
147  trace.beginBlock ( "Testing Mesh Bouding box and scale change ..." );
148  aMesh.changeScale(2);
149  std::pair<Point, Point> bb = aMesh.getBoundingBox();
150  bool boundingBoxOK = (bb.first == Point(20,10)) && (bb.second == Point(26,18));
151  trace.info() << "bouding box=" << bb.first << " " << bb.second << "(should be (20,10) (26,18)" <<std::endl;
152  trace.endBlock();
153 
154  trace.beginBlock ( "Testing mesh subdivision ..." );
155  Mesh<RealPoint> aMeshR;
156  RealPoint pr0 (0,0);
157  RealPoint pr1 (1,0);
158  RealPoint pr2 (1,1);
159  aMeshR.addVertex(pr0); aMeshR.addVertex(pr1); aMeshR.addVertex(pr2);
160  aMeshR.addTriangularFace(0,1,2);
161  trace.info() << "nb vertices before subdivision: " << aMeshR.nbVertex() << std::endl;
162  trace.info() << "nb faces before subdivision: " << aMeshR.nbFaces() << std::endl;
163  aMeshR.subDivideTriangularFaces(0.5);
164  trace.info() << "nb vertices after subdivision: " << aMeshR.nbVertex() << " (should be 4)"<<std::endl;
165  trace.info() << "nb faces after subdivision: " << aMeshR.nbFaces() << " (should be 3)" <<std::endl;
166  trace.info() << "New point: " << aMeshR.getVertex(aMeshR.nbVertex()-1) << "(should be: "<< RealPoint(2.0/3.0, 1.0/3.0) << ") "<< std::endl;
167  bool okSubDivide = aMeshR.nbVertex()==4 && aMeshR.nbFaces()==3 &&
168  aMeshR.getVertex(aMeshR.nbVertex()-1) == RealPoint(2.0/3.0, 1.0/3.0);
169  trace.info() << (okSubDivide ? "[subdivise OK]":"[subdivise fail]" ) << std::endl;
170  trace.endBlock();
171 
172  trace.beginBlock ( "Testing mesh quad transform ..." );
173  Mesh<RealPoint> aMeshQ;
174  RealPoint pq0 (0,0);
175  RealPoint pq1 (1,0);
176  RealPoint pq2 (1,1);
177  RealPoint pq3 (0,1);
178  aMeshQ.addVertex(pq0); aMeshQ.addVertex(pq1); aMeshQ.addVertex(pq2);
179  aMeshQ.addVertex(pq3);
180  aMeshQ.addQuadFace(0,1,2,3);
181  aMeshQ.quadToTriangularFaces();
182 
183  trace.info() << "nb faces after quad to triangle transform: " << aMeshQ.nbFaces() ;
184  bool okQuadToTrans = aMeshQ.nbFaces() == 2;
185  trace.info() << "(should be 2) "<< (okQuadToTrans? "[ok]": "[error]") << std::endl;
186  trace.endBlock();
187 
188 
189  trace.beginBlock ( "Testing Mesh copy operator ..." );
190  Mesh<Point> aMesh2 = aMesh;
191  Mesh<Point> aMesh3 (aMesh2);
192  bool okMeshCopy = aMesh.nbFaces() == aMesh2.nbFaces() && aMesh.nbVertex() == aMesh2.nbVertex() &&
193  aMesh.nbFaces() == aMesh3.nbFaces() && aMesh.nbVertex() == aMesh3.nbVertex() &&
194  aMesh.getVertex(0) == aMesh2.getVertex(0) && aMesh.getVertex(0) == aMesh3.getVertex(0);
195  trace.info() << (okMeshCopy ? "[copy ok]":"[copy fail]" ) << std::endl;
196  trace.endBlock();
197 
198  trace.beginBlock ( "Testing face removing ..." );
199  Mesh<Point> aMesh4 = aMesh;
200  std::vector<unsigned int> f = {1};
201  aMesh4.removeFaces(f);
202  bool okRemoveFace = (aMesh4.nbFaces() == aMesh.nbFaces()-1) && (aMesh4.nbVertex() == aMesh.nbVertex()-3);
203  trace.info() << (okRemoveFace ? "[face remove ok]":"[face remove fail]" ) << std::endl;
204 
205  ok = ok & okMeshConstruct && okMeshIterators && okMeshColor && okMeshCopy && boundingBoxOK &&
206  okSubDivide && okQuadToTrans && okRemoveFace;
207  trace.endBlock();
208  return ok;
209 
210 }
211 
212 
213 
218 {
219 
220  trace.beginBlock ( "Testing Mesh generation ..." );
221  bool ok = true;
222 
223  trace.beginBlock ( "Testing Tube generation ..." );
225  std::vector<Z3i::RealPoint> aSkeleton;
226  aSkeleton.push_back(Z3i::RealPoint(0.0, 0.0, 0.0));
227  aSkeleton.push_back(Z3i::RealPoint(10.0, 0.0, 0.0));
228  aSkeleton.push_back(Z3i::RealPoint(20.0, 0.0, 0.0));
229  aSkeleton.push_back(Z3i::RealPoint(30.0, 0.0, 0.0));
230  aSkeleton.push_back(Z3i::RealPoint(35.0, 5.0, 0.0));
231  aSkeleton.push_back(Z3i::RealPoint(40.0, 10.0, 0.0));
232  aSkeleton.push_back(Z3i::RealPoint(40.0, 20.0, 0.0));
233  aSkeleton.push_back(Z3i::RealPoint(40.0, 30.0, 0.0));
234  aSkeleton.push_back(Z3i::RealPoint(40.0, 35.0, 5.0));
235  aSkeleton.push_back(Z3i::RealPoint(40.0, 40.0, 10.0));
236  aSkeleton.push_back(Z3i::RealPoint(40.0, 40.0, 20.0));
238 
240  Mesh<Z3i::RealPoint> aMesh(true);
241  Mesh<Z3i::RealPoint>::createTubularMesh(aMesh, aSkeleton, 0.5, 0.2, DGtal::Color::Blue);
243 
244  trace.endBlock();
245  trace.info() << "Nb faces: "<< aMesh.nbFaces() << " (sould be 320)" << std::endl;
246  trace.info() << "Nb vertices: "<< aMesh.nbVertex() << " (sould be 352)" << std::endl;
247  bool okMeshTube1 = aMesh.nbFaces() == 320 && aMesh.nbVertex() == 352;
248 
249  trace.beginBlock ( "Testing Tube generation (bis with variable raidii ..." );
250  Mesh<Z3i::RealPoint> aMeshBis(true);
251  std::vector<double> vectRadii;
252  vectRadii.push_back(0.5);
253  vectRadii.push_back(1.5);
254  vectRadii.push_back(2.5);
255  Mesh<Z3i::RealPoint>::createTubularMesh(aMeshBis, aSkeleton, vectRadii, 0.2, DGtal::Color::Green);
256 
257  trace.endBlock();
258  trace.info() << "Nb faces: "<< aMeshBis.nbFaces() << " (sould be 320)" << std::endl;
259  trace.info() << "Nb vertices: "<< aMeshBis.nbVertex() << " (sould be 352)" << std::endl;
260 
261  std::ofstream ofbis ("tubeVariableRadiiGeneratedFromTestMesh.off");
262  DGtal::MeshWriter<Z3i::RealPoint>::export2OFF(ofbis, aMeshBis, true);
263  ofbis.close();
264  bool okMeshTube1bis = aMeshBis.nbFaces() == 320 && aMeshBis.nbVertex() == 352;
265 
266 
267  trace.beginBlock("Testing Mesh from Height sequence");
269  std::vector<double> heightSequence;
270  heightSequence.push_back(0.1);
271  heightSequence.push_back(0.2);
272  heightSequence.push_back(0.15);
273 
274  heightSequence.push_back(1.1);
275  heightSequence.push_back(2.2);
276  heightSequence.push_back(1.15);
277 
278  heightSequence.push_back(0.1);
279  heightSequence.push_back(0.2);
280  heightSequence.push_back(0.15);
282 
284  Mesh<Z3i::RealPoint>::createMeshFromHeightSequence(aMesh, heightSequence, 3, 10, 10, 3, DGtal::Color::Yellow);
286 
287  trace.info() << "Nb faces: "<< aMesh.nbFaces() << " (sould be 324)" << std::endl;
288  trace.info() << "Nb vertices: "<< aMesh.nbVertex() << " (sould be 361)" << std::endl;
289  bool okMeshTube1AndHF = aMesh.nbFaces() == 324 && aMesh.nbVertex() == 361;
290 
292  std::ofstream of ("tubeAndHeighFieldGeneratedFromTestMesh.off");
294  of.close();
296 
297  ok = ok & okMeshTube1 & okMeshTube1bis & okMeshTube1AndHF;
298  trace.endBlock();
299  return ok;
300 }
301 
302 
303 
308 {
309  unsigned int nbok = 0;
310  unsigned int nb = 0;
311 
312  trace.beginBlock("Testing visual tubular mesh generation (shell mesh):");
313  // Generate the center line:
314  std::vector<Z3i::RealPoint> centerline;
315  unsigned int nbPoints = 0;
316  double z = 0.0;
317  double radiusSpirale = 13.0;
318  double radiusTube = 15.0;
319  double alphaMax = 32.0;
320  double reduc = 0.05;
321  for (double alpha = 0; alpha< alphaMax; alpha += 0.1, z += 0.5-reduc)
322  {
323  centerline.push_back(Z3i::RealPoint(radiusSpirale*cos(alpha), radiusSpirale*sin(alpha), z ));
324  nbPoints++;
325  radiusSpirale -=reduc;
326  radiusSpirale = std::max(radiusSpirale, 0.0);
327  }
328  // Generate radius:
329  std::vector<double> vectRadius;
330  for(unsigned int i=0; i<nbPoints; i++)
331  {
332  vectRadius.push_back(radiusTube);
333  radiusTube -=reduc;
334  radiusTube = std::max(radiusTube, 0.0);
335  }
336 
337  DGtal::Mesh<Z3i::RealPoint> theMeshShell(true);
338  DGtal::Mesh<Z3i::RealPoint>::createTubularMesh(theMeshShell, centerline, vectRadius, 0.1);
339 
340  trace.info() << "Mesh generated with " << theMeshShell.nbFaces()
341  << " faces (should be " << (centerline.size()-1)*63 << " )" << std::endl;
342  nb++;
343  nbok += theMeshShell.nbFaces() == (centerline.size()-1)*63;
344  theMeshShell >> "spiraleGeneratedFromTestMesh.off";
345  trace.info() << " [done]" << std::endl;
346  trace.endBlock();
347 
348 
349 
350  trace.beginBlock("Testing visual tubular mesh generation (tube mesh):");
351  std::vector<Z3i::RealPoint> centerLine2;
352  centerLine2.push_back(Z3i::RealPoint(0.0,0.0,0.0));
353  centerLine2.push_back(Z3i::RealPoint(3.3,0.0,0.0));
354  centerLine2.push_back(Z3i::RealPoint(6.6,0.0,0.0));
355  centerLine2.push_back(Z3i::RealPoint(10.0,0.0,0.0));
356  centerLine2.push_back(Z3i::RealPoint(13.3,0.0,0.0));
357  centerLine2.push_back(Z3i::RealPoint(16.6,0.0,0.0));
358  centerLine2.push_back(Z3i::RealPoint(20.0,0.0,0.0));
359  centerLine2.push_back(Z3i::RealPoint(60.0,0.0,0.0));
360  centerLine2.push_back(Z3i::RealPoint(63.3,0.0,0.0));
361  centerLine2.push_back(Z3i::RealPoint(66.6,0.0,0.0));
362  centerLine2.push_back(Z3i::RealPoint(70.0,0.0,0.0));
363  centerLine2.push_back(Z3i::RealPoint(71.7,0.1,0.0));
364  centerLine2.push_back(Z3i::RealPoint(73.4,0.6,0.0));
365  centerLine2.push_back(Z3i::RealPoint(75.0,1.3,0.0));
366  centerLine2.push_back(Z3i::RealPoint(76.4,2.3,0.0));
367  centerLine2.push_back(Z3i::RealPoint(77.6,3.5,0.0));
368  centerLine2.push_back(Z3i::RealPoint(78.6,5.0,0.0));
369  centerLine2.push_back(Z3i::RealPoint(79.3,6.5,0.0));
370  centerLine2.push_back(Z3i::RealPoint(79.8,8.2,0.0));
371  centerLine2.push_back(Z3i::RealPoint(80.0,10.0,0.0));
372  centerLine2.push_back(Z3i::RealPoint(80.0,13.8,0.0));
373  centerLine2.push_back(Z3i::RealPoint(80.0,86.1,0.0));
374  centerLine2.push_back(Z3i::RealPoint(80.0,90.0,0.0));
375  centerLine2.push_back(Z3i::RealPoint(80.1,91.7,-0.1));
376  centerLine2.push_back(Z3i::RealPoint(80.6,93.4,0.1));
377  centerLine2.push_back(Z3i::RealPoint(81.3,95.0,0.1));
378  centerLine2.push_back(Z3i::RealPoint(82.3,96.4,-0.1));
379  centerLine2.push_back(Z3i::RealPoint(83.5,97.6,-0.1));
380 
381  DGtal::Mesh<Z3i::RealPoint> theMeshTube(true);
383  5.0, 0.2, DGtal::Color::Blue);
384 
385  trace.info() << "Mesh generated with " << theMeshTube.nbFaces() << " faces (should be "
386  << (centerLine2.size()-1)*32 << " )" << std::endl;
387  nb++;
388  nbok += theMeshTube.nbFaces() == (centerLine2.size()-1)*32;
389 
390  theMeshTube >> "tubeGeneratedFromTestMesh.off";
391  trace.endBlock();
392 
393  return nb == nbok;
394 
395 }
396 
398 // Standard services - public :
399 
400 int main( int argc, char** argv )
401 {
402  trace.beginBlock ( "Testing class Mesh" );
403  trace.info() << "Args:";
404  for ( int i = 0; i < argc; ++i )
405  trace.info() << " " << argv[ i ];
406  trace.info() << endl;
407 
408  bool res = testMesh() && testMeshGeneration() && testVisualTubularMesh();
409  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
410  trace.endBlock();
411  return res ? 0 : 1;
412 }
413 // //
static const Color Yellow
Definition: Color.h:398
static const Color Green
Definition: Color.h:393
static const Color Red
Definition: Color.h:392
static const Color White
Definition: Color.h:391
static const Color Blue
Definition: Color.h:395
Aim: This class is defined to represent a surface mesh through a set of vertices and faces....
Definition: Mesh.h:92
Size nbFaces() const
unsigned int quadToTriangularFaces()
const Color & getFaceColor(unsigned int i) const
std::vector< unsigned int > MeshFace
Definition: Mesh.h:116
void addQuadFace(unsigned int indexVertex1, unsigned int indexVertex2, unsigned int indexVertex3, unsigned int indexVertex4, const DGtal::Color &aColor=DGtal::Color::White)
Size nbVertex() const
void changeScale(const typename TPoint::Component aScale)
std::pair< TPoint, TPoint > getBoundingBox() const
const MeshFace & getFace(unsigned int i) const
RealPoint getFaceBarycenter(unsigned int i) const
FaceStorage::const_iterator faceEnd() const
Definition: Mesh.h:409
const TPoint & getVertex(unsigned int i) const
void setFaceColor(unsigned int i, const DGtal::Color &aColor)
ConstIterator vertexEnd() const
Definition: Mesh.h:364
void addTriangularFace(unsigned int indexVertex1, unsigned int indexVertex2, unsigned int indexVertex3, const DGtal::Color &aColor=DGtal::Color::White)
double subDivideTriangularFaces(const double minArea)
void removeFaces(const std::vector< unsigned int > &facesIndex)
FaceStorage::const_iterator faceBegin() const
Definition: Mesh.h:397
ConstIterator vertexBegin() const
Definition: Mesh.h:354
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.
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
static bool export2OFF(std::ostream &out, const Mesh< TPoint > &aMesh, bool exportColor=true)
int max(int a, int b)
Z2i::RealPoint RealPoint
MyPointD Point
Definition: testClone2.cpp:383
bool testMeshGeneration()
Definition: testMesh.cpp:217
int main(int argc, char **argv)
Definition: testMesh.cpp:400
bool testMesh()
Definition: testMesh.cpp:51
bool testVisualTubularMesh()
Definition: testMesh.cpp:307