34 #include "DGtal/base/Common.h"
35 #include "ConfigTest.h"
36 #include "DGtalCatch.h"
37 #include "DGtal/helpers/StdDefs.h"
38 #include "DGtal/kernel/PointVector.h"
39 #include "DGtal/graph/CUndirectedSimpleGraph.h"
40 #include "DGtal/graph/BreadthFirstVisitor.h"
41 #include "DGtal/shapes/SurfaceMesh.h"
42 #include "DGtal/shapes/SurfaceMeshHelper.h"
43 #include "DGtal/io/readers/SurfaceMeshReader.h"
44 #include "DGtal/io/writers/SurfaceMeshWriter.h"
48 using namespace DGtal;
63 std::vector< RealPoint > positions;
64 std::vector< Vertices > faces;
65 positions.push_back(
RealPoint( 0, 0, 0 ) );
66 positions.push_back(
RealPoint( 1, 0, 0 ) );
67 positions.push_back(
RealPoint( 0, 1, 0 ) );
68 positions.push_back(
RealPoint( 1, 1, 0 ) );
69 positions.push_back(
RealPoint( 0, 0, 1 ) );
70 positions.push_back(
RealPoint( 1, 0, 1 ) );
71 positions.push_back(
RealPoint( 0, 1, 1 ) );
72 positions.push_back(
RealPoint( 1, 1, 1 ) );
73 positions.push_back(
RealPoint( 1, 0, 2 ) );
74 positions.push_back(
RealPoint( 0, 0, 2 ) );
75 faces.push_back( { 1, 0, 2, 3 } );
76 faces.push_back( { 0, 1, 5, 4 } );
77 faces.push_back( { 1, 3, 7, 5 } );
78 faces.push_back( { 3, 2, 6, 7 } );
79 faces.push_back( { 2, 0, 4, 6 } );
80 faces.push_back( { 4, 5, 8, 9 } );
81 return PolygonMesh( positions.cbegin(), positions.cend(),
82 faces.cbegin(), faces.cend() );
93 std::vector< RealPoint > positions;
94 std::vector< Vertices > faces;
95 positions.push_back(
RealPoint( 0, 0, 1 ) );
96 positions.push_back(
RealPoint( 0, -1, 0 ) );
97 positions.push_back(
RealPoint( 1, 0, 0 ) );
98 positions.push_back(
RealPoint( 0, 1, 0 ) );
99 positions.push_back(
RealPoint( 0, 0, 0 ) );
100 faces.push_back( { 0, 4, 1 } );
101 faces.push_back( { 0, 4, 2 } );
102 faces.push_back( { 0, 4, 3 } );
103 return PolygonMesh( positions.cbegin(), positions.cend(),
104 faces.cbegin(), faces.cend() );
114 std::vector< RealPoint > positions;
115 std::vector< Vertices > faces;
116 positions.push_back(
RealPoint( 0, 0, 0 ) );
117 positions.push_back(
RealPoint( 1, 0, 0 ) );
118 positions.push_back(
RealPoint( 0, 1, 0 ) );
119 positions.push_back(
RealPoint( 0, 0, 1 ) );
120 faces.push_back( { 0, 1, 2 } );
121 faces.push_back( { 1, 0, 3 } );
122 faces.push_back( { 0, 2, 3 } );
123 faces.push_back( { 3, 2, 1 } );
124 return PolygonMesh( positions.cbegin(), positions.cend(),
125 faces.cbegin(), faces.cend() );
128 SCENARIO(
"SurfaceMesh< RealPoint3 > concept check tests",
"[surfmesh][concepts]" )
136 SCENARIO(
"SurfaceMesh< RealPoint3 > build tests",
"[surfmesh][build]" )
144 GIVEN(
"A box with an open side" ) {
145 PolygonMesh polymesh =
makeBox();
146 THEN(
"The mesh has 10 vertices, v0 has 3 neighbors, v1 has 3 neighbors, etc" ) {
147 REQUIRE( polymesh.size() == 10 );
148 REQUIRE( polymesh.degree( 0 ) == 3 );
149 REQUIRE( polymesh.degree( 1 ) == 3 );
150 REQUIRE( polymesh.degree( 2 ) == 3 );
151 REQUIRE( polymesh.degree( 3 ) == 3 );
152 REQUIRE( polymesh.degree( 4 ) == 4 );
153 REQUIRE( polymesh.degree( 5 ) == 4 );
154 REQUIRE( polymesh.degree( 6 ) == 3 );
155 REQUIRE( polymesh.degree( 7 ) == 3 );
156 REQUIRE( polymesh.degree( 8 ) == 2 );
157 REQUIRE( polymesh.degree( 9 ) == 2 );
159 THEN(
"Euler number is 1 as is the Euler number of a disk." )
161 REQUIRE( polymesh.nbVertices() == 10 );
162 REQUIRE( polymesh.nbEdges() == 15 );
163 REQUIRE( polymesh.nbFaces() == 6 );
164 REQUIRE( polymesh.Euler() == 1 );
166 THEN(
"Checking distances." )
168 REQUIRE( polymesh.distance(0,0) == Approx(0.0) );
169 REQUIRE( polymesh.distance(0,7) == Approx(std::sqrt(3)));
171 THEN(
"Breadth-first visiting the mesh from vertex 0, visit {0}, then {1,2,4}, then {3,5,6,9}, then {7,8}." )
174 std::vector<unsigned long> vertices;
175 std::vector<unsigned long> distances;
178 vertices.push_back( visitor.
current().first );
179 distances.push_back( visitor.
current().second );
182 REQUIRE( vertices.size() == 10 );
183 REQUIRE( distances.size() == 10 );
184 int expected_vertices[] = { 0, 1, 2, 4, 3, 5, 6, 9, 7, 8 };
185 int expected_distance[] = { 0, 1, 1, 1, 2, 2, 2, 2, 3, 3 };
186 auto itb = vertices.begin();
187 std::sort( itb+1, itb+4 );
188 std::sort( itb+4, itb+8 );
189 std::sort( itb+8, itb+10 );
191 = std::equal( vertices.begin(), vertices.end(), expected_vertices );
194 = std::equal( distances.begin(), distances.end(), expected_distance );
197 THEN(
"The mesh has 6 boundary edges and 9 manifold inner consistent edges, the boundary is a 1d manifold" ) {
198 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
199 auto mani_inner = polymesh.computeManifoldInnerEdges();
200 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
201 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
202 auto non_mani = polymesh.computeNonManifoldEdges();
204 REQUIRE( mani_bdry.size() == 6 );
205 REQUIRE( mani_inner.size() == 9 );
206 REQUIRE( mani_inner_c.size() == 9 );
207 REQUIRE( mani_inner_u.size() == 0 );
208 REQUIRE( non_mani.size() == 0 );
210 THEN(
"The face along (1,3) is a quadrangle (1,3,7,5)" ) {
211 Edge e13 = polymesh.makeEdge( 1, 3 );
212 auto lfs = polymesh.edgeLeftFaces( e13 );
213 Vertices T = polymesh.incidentVertices( lfs[ 0 ] );
215 std::sort( T.begin(), T.end() );
221 THEN(
"The face along (3,1) is a quadrangle (3,1,0,2)" ) {
222 Edge e13 = polymesh.makeEdge( 1, 3 );
223 auto rfs = polymesh.edgeRightFaces( e13 );
224 Vertices T = polymesh.incidentVertices( rfs[ 0 ] );
226 std::sort( T.begin(), T.end() );
232 THEN(
"The lower part of the mesh has the barycenter (0.5, 0.5, 0.5) " ) {
233 auto positions = polymesh.positions();
235 for (
Vertex v = 0; v < 8; ++v )
242 THEN(
"We can iterate over the vertices" ) {
243 auto positions = polymesh.positions();
244 RealPoint exp_positions[] = { { 0,0,0 }, { 1,0,0 }, { 0,1,0 }, { 1,1,0 },
245 { 0,0,1 }, { 1,0,1 }, { 0,1,1 }, { 1,1,1 },
246 { 1,0,2 }, { 0,0,2 } };
247 for (
auto it = polymesh.begin(), itE = polymesh.end(); it != itE; ++it ) {
248 REQUIRE( positions[ *it ] == exp_positions[ *it ] );
255 SCENARIO(
"SurfaceMesh< RealPoint3 > mesh helper tests",
"[surfmesh][helper]" )
260 typedef PolygonMeshHelper::NormalsType NormalsType;
261 GIVEN(
"A sphere of radius 10" ) {
262 auto polymesh = PolygonMeshHelper::makeSphere( 3.0, RealPoint::zero,
263 10, 10, NormalsType::NO_NORMALS );
264 THEN(
"The mesh has Euler characteristic 2" ) {
265 REQUIRE( polymesh.Euler() == 2 );
267 THEN(
"It is a consistent manifold without boundary" ) {
268 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
269 auto mani_inner = polymesh.computeManifoldInnerEdges();
270 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
271 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
272 auto non_mani = polymesh.computeNonManifoldEdges();
274 REQUIRE( mani_bdry.size() == 0 );
275 REQUIRE( mani_inner.size() == mani_inner_c.size() );
276 REQUIRE( mani_inner_u.size() == 0 );
277 REQUIRE( non_mani.size() == 0 );
280 GIVEN(
"A torus with radii 3 and 1" ) {
281 auto polymesh = PolygonMeshHelper::makeTorus( 3.0, 1.0, RealPoint::zero,
282 10, 10, 0, NormalsType::NO_NORMALS );
283 THEN(
"The mesh has Euler characteristic 0" ) {
284 REQUIRE( polymesh.Euler() == 0 );
286 THEN(
"It is a consistent manifold without boundary" ) {
287 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
288 auto mani_inner = polymesh.computeManifoldInnerEdges();
289 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
290 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
291 auto non_mani = polymesh.computeNonManifoldEdges();
293 REQUIRE( mani_bdry.size() == 0 );
294 REQUIRE( mani_inner.size() == mani_inner_c.size() );
295 REQUIRE( mani_inner_u.size() == 0 );
296 REQUIRE( non_mani.size() == 0 );
299 GIVEN(
"A lantern with radii 3" ) {
300 auto polymesh = PolygonMeshHelper::makeLantern( 3.0, 3.0, RealPoint::zero,
301 10, 10, NormalsType::NO_NORMALS );
302 THEN(
"The mesh has Euler characteristic 0" ) {
303 REQUIRE( polymesh.Euler() == 0 );
305 THEN(
"It is a consistent manifold with boundary" ) {
306 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
307 auto mani_inner = polymesh.computeManifoldInnerEdges();
308 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
309 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
310 auto non_mani = polymesh.computeNonManifoldEdges();
312 REQUIRE( mani_bdry.size() == 20 );
313 REQUIRE( mani_inner.size() == mani_inner_c.size() );
314 REQUIRE( mani_inner_u.size() == 0 );
315 REQUIRE( non_mani.size() == 0 );
320 SCENARIO(
"SurfaceMesh< RealPoint3 > reader/writer tests",
"[surfmesh][io]" )
328 typedef PolygonMeshHelper::NormalsType NormalsType;
329 auto polymesh = PolygonMeshHelper::makeSphere( 3.0, RealPoint::zero,
330 10, 10, NormalsType::VERTEX_NORMALS );
331 WHEN(
"Writing the mesh as an OBJ file and reading into another mesh" ) {
332 PolygonMesh readmesh;
333 std::ostringstream output;
334 bool okw = PolygonMeshWriter::writeOBJ( output, polymesh );
335 std::string file = output.str();
336 std::istringstream input( file );
337 bool okr = PolygonMeshReader::readOBJ ( input, readmesh );
338 THEN(
"The read mesh is the same as the original one" ) {
344 REQUIRE( polymesh.Euler() == readmesh.Euler() );
345 REQUIRE( polymesh.nbVertices() == readmesh.nbVertices() );
346 REQUIRE( polymesh.nbEdges() == readmesh.nbEdges() );
347 REQUIRE( polymesh.nbFaces() == readmesh.nbFaces() );
348 REQUIRE( polymesh.neighborVertices( 0 ).size()
349 == readmesh.neighborVertices( 0 ).size() );
350 REQUIRE( polymesh.neighborVertices( 20 ).size()
351 == readmesh.neighborVertices( 20 ).size() );
352 REQUIRE( polymesh.vertexNormals().size() == readmesh.vertexNormals().size() );
357 SCENARIO(
"SurfaceMesh< RealPoint3 > boundary tests",
"[surfmesh][boundary]" )
361 WHEN(
"Checking the topology of the mesh boundary" ) {
362 auto chains = polymesh2.computeManifoldBoundaryChains();
363 THEN(
"The box as a manifold boundary" ) {
365 REQUIRE( polymesh2.isBoundariesManifold() ==
true);
366 REQUIRE( polymesh2.isBoundariesManifold(
false) ==
true);
368 REQUIRE( chains[0].size() == 6);
370 THEN(
"The extra mesh does not have a manifold boundary" ) {
371 REQUIRE( polymesh.isBoundariesManifold() ==
false);
376 SCENARIO(
"SurfaceMesh< RealPoint3 > flippable tests",
"[surfmesh][flip]" )
383 typedef PolygonMeshHelper::NormalsType NormalsType;
386 auto meshLantern = PolygonMeshHelper::makeLantern( 3.0, 3.0, RealPoint::zero,
387 10, 10, NormalsType::NO_NORMALS );
388 auto meshTorus = PolygonMeshHelper::makeTorus( 3.0, 1.0, RealPoint::zero,
389 10, 10, 0, NormalsType::NO_NORMALS );
390 WHEN(
"Checking if one can flip box edges" ) {
391 auto nb_flippable = 0;
392 for (
Edge e = 0; e < meshBox.nbEdges(); e++ )
393 if ( meshBox.isFlippable( e ) ) nb_flippable++;
394 THEN(
"No box edges are flippable (they border quads)" ) {
398 WHEN(
"Checking if one can flip tetrahedron edges" ) {
399 auto nb_flippable = 0;
400 for (
Edge e = 0; e < meshTetra.nbEdges(); e++ )
401 if ( meshTetra.isFlippable( e ) ) nb_flippable++;
402 THEN(
"No tetrahedron edges are flippable (the neihgborhood is not simply connected)" ) {
406 WHEN(
"Checking if one can flip torus edges" ) {
407 Edge nb_flippable = 0;
408 for (
Edge e = 0; e < meshTorus.nbEdges(); e++ )
409 if ( meshTorus.isFlippable( e ) ) nb_flippable++;
410 THEN(
"All torus edges are flippable (it is a closed triangulated surface)" ) {
411 REQUIRE( nb_flippable == meshTorus.nbEdges() );
414 WHEN(
"Checking if one can flip lantern edges" ) {
415 auto bdry_edges = meshLantern.computeManifoldBoundaryEdges();
416 auto inner_edges = meshLantern.computeManifoldInnerEdges();
417 Edge nb_flippable = 0;
418 Edge nb_bdry_flippable = 0;
419 Edge nb_inner_flippable = 0;
420 for (
Edge e = 0; e < meshLantern.nbEdges(); e++ )
421 if ( meshLantern.isFlippable( e ) ) nb_flippable++;
422 for (
Edge e : bdry_edges )
423 if ( meshLantern.isFlippable( e ) ) nb_bdry_flippable++;
424 for (
Edge e : inner_edges )
425 if ( meshLantern.isFlippable( e ) ) nb_inner_flippable++;
426 THEN(
"Innner lantern edges are flippable while boundary edges are not flippable" ) {
427 REQUIRE( nb_flippable == inner_edges.size() );
428 REQUIRE( nb_bdry_flippable == 0 );
429 REQUIRE( nb_flippable == nb_inner_flippable );
430 REQUIRE( nb_flippable == ( meshLantern.nbEdges() - bdry_edges.size() ) );
435 SCENARIO(
"SurfaceMesh< RealPoint3 > flip tests",
"[surfmesh][flip]" )
442 typedef PolygonMeshHelper::NormalsType NormalsType;
443 auto meshLantern = PolygonMeshHelper::makeLantern( 3.0, 3.0, RealPoint::zero,
444 10, 10, NormalsType::NO_NORMALS );
445 auto bdry_edges = meshLantern.computeManifoldBoundaryEdges();
446 auto euler = meshLantern.Euler();
448 for (
auto i = 0; i < 100; i++ )
450 Edge e = rand() % meshLantern.nbEdges();
451 if ( meshLantern.isFlippable( e ) )
453 meshLantern.flip( e,
false );
457 WHEN(
"Flipping 100 random edges" ) {
458 THEN(
"More than 50 edges were flipped" ) {
461 THEN(
"Euler number is not changed" ) {
462 auto post_euler = meshLantern.Euler();
463 REQUIRE( euler == post_euler );
465 THEN(
"Boundary is unchanged" ) {
466 auto post_bdry_edges = meshLantern.computeManifoldBoundaryEdges();
467 REQUIRE( bdry_edges.size() == post_bdry_edges.size() );
472 SCENARIO(
"SurfaceMesh< RealPoint3 > restore lantern with flips tests",
"[surfmesh][flip]" )
480 typedef PolygonMeshHelper::NormalsType NormalsType;
481 auto meshLantern = PolygonMeshHelper::makeLantern( 3.0, 3.0, RealPoint::zero,
482 10, 10, NormalsType::NO_NORMALS );
484 std::ofstream output(
"lantern.obj" );
485 PolygonMeshWriter::writeOBJ( output, meshLantern );
489 const auto& X = meshLantern.positions();
490 for (
Edge e = 0; e < meshLantern.nbEdges(); e++ )
492 if ( meshLantern.isFlippable( e ) )
494 auto ij = meshLantern.edgeVertices ( e );
495 auto kl = meshLantern.otherDiagonal( e );
496 double l2_ij = ( X[ ij.first ] - X[ ij.second ] ).squaredNorm();
497 double l2_kl = ( X[ kl.first ] - X[ kl.second ] ).squaredNorm();
500 meshLantern.flip( e,
false );
506 std::ofstream output(
"flipped-lantern.obj" );
507 PolygonMeshWriter::writeOBJ( output, meshLantern );
510 WHEN(
"Flipping all long edges" ) {
511 THEN(
"80 edges were flipped" ) {
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
Space::RealVector RealVector
DGtal is the top-level namespace which contains all DGtal functions and types.
Aim: An helper class for building classical meshes.
Aim: An helper class for reading mesh files (Wavefront OBJ at this point) and creating a SurfaceMesh.
Aim: An helper class for writing mesh file formats (Waverfront OBJ at this point) and creating a Surf...
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Aim: Represents the concept of local graph: each vertex has neighboring vertices, but we do not neces...
GIVEN("A cubical complex with random 3-cells")
HalfEdgeDataStructure::Edge Edge
REQUIRE(domain.isInside(aPoint))
SCENARIO("SurfaceMesh< RealPoint3 > concept check tests", "[surfmesh][concepts]")
SurfaceMesh< PointVector< 3, double >, PointVector< 3, double > > makeBox()
SurfaceMesh< PointVector< 3, double >, PointVector< 3, double > > makeTetrahedron()
SurfaceMesh< PointVector< 3, double >, PointVector< 3, double > > makeNonManifoldBoundary()
PointVector< 3, double > RealPoint