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"
62 typedef PolygonMesh::Vertices
Vertices;
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() );
92 typedef PolygonMesh::Vertices
Vertices;
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() );
107SCENARIO(
"SurfaceMesh< RealPoint3 > concept check tests",
"[surfmesh][concepts]" )
115SCENARIO(
"SurfaceMesh< RealPoint3 > build tests",
"[surfmesh][build]" )
120 typedef PolygonMesh::Vertices
Vertices;
121 typedef PolygonMesh::Face
Face;
122 typedef PolygonMesh::Edge
Edge;
123 typedef PolygonMesh::Vertex
Vertex;
124 GIVEN(
"A box with an open side" ) {
125 PolygonMesh polymesh =
makeBox();
126 THEN(
"The mesh has 10 vertices, v0 has 3 neighbors, v1 has 3 neighbors, etc" ) {
127 REQUIRE( polymesh.size() == 10 );
128 REQUIRE( polymesh.degree( 0 ) == 3 );
129 REQUIRE( polymesh.degree( 1 ) == 3 );
130 REQUIRE( polymesh.degree( 2 ) == 3 );
131 REQUIRE( polymesh.degree( 3 ) == 3 );
132 REQUIRE( polymesh.degree( 4 ) == 4 );
133 REQUIRE( polymesh.degree( 5 ) == 4 );
134 REQUIRE( polymesh.degree( 6 ) == 3 );
135 REQUIRE( polymesh.degree( 7 ) == 3 );
136 REQUIRE( polymesh.degree( 8 ) == 2 );
137 REQUIRE( polymesh.degree( 9 ) == 2 );
139 THEN(
"Euler number is 1 as is the Euler number of a disk." )
141 REQUIRE( polymesh.nbVertices() == 10 );
142 REQUIRE( polymesh.nbEdges() == 15 );
143 REQUIRE( polymesh.nbFaces() == 6 );
144 REQUIRE( polymesh.Euler() == 1 );
146 THEN(
"Checking distances." )
148 REQUIRE( polymesh.distance(0,0) == Approx(0.0) );
149 REQUIRE( polymesh.distance(0,7) == Approx(std::sqrt(3)));
151 THEN(
"Breadth-first visiting the mesh from vertex 0, visit {0}, then {1,2,4}, then {3,5,6,9}, then {7,8}." )
154 std::vector<unsigned long> vertices;
155 std::vector<unsigned long> distances;
158 vertices.push_back( visitor.
current().first );
159 distances.push_back( visitor.
current().second );
162 REQUIRE( vertices.size() == 10 );
163 REQUIRE( distances.size() == 10 );
164 int expected_vertices[] = { 0, 1, 2, 4, 3, 5, 6, 9, 7, 8 };
165 int expected_distance[] = { 0, 1, 1, 1, 2, 2, 2, 2, 3, 3 };
166 auto itb = vertices.begin();
167 std::sort( itb+1, itb+4 );
168 std::sort( itb+4, itb+8 );
169 std::sort( itb+8, itb+10 );
171 = std::equal( vertices.begin(), vertices.end(), expected_vertices );
174 = std::equal( distances.begin(), distances.end(), expected_distance );
177 THEN(
"The mesh has 6 boundary edges and 9 manifold inner consistent edges, the boundary is a 1d manifold" ) {
178 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
179 auto mani_inner = polymesh.computeManifoldInnerEdges();
180 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
181 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
182 auto non_mani = polymesh.computeNonManifoldEdges();
184 REQUIRE( mani_bdry.size() == 6 );
185 REQUIRE( mani_inner.size() == 9 );
186 REQUIRE( mani_inner_c.size() == 9 );
187 REQUIRE( mani_inner_u.size() == 0 );
188 REQUIRE( non_mani.size() == 0 );
190 THEN(
"The face along (1,3) is a quadrangle (1,3,7,5)" ) {
191 Edge e13 = polymesh.makeEdge( 1, 3 );
192 auto lfs = polymesh.edgeLeftFaces( e13 );
193 Vertices T = polymesh.incidentVertices( lfs[ 0 ] );
195 std::sort( T.begin(), T.end() );
201 THEN(
"The face along (3,1) is a quadrangle (3,1,0,2)" ) {
202 Edge e13 = polymesh.makeEdge( 1, 3 );
203 auto rfs = polymesh.edgeRightFaces( e13 );
204 Vertices T = polymesh.incidentVertices( rfs[ 0 ] );
206 std::sort( T.begin(), T.end() );
212 THEN(
"The lower part of the mesh has the barycenter (0.5, 0.5, 0.5) " ) {
213 auto positions = polymesh.positions();
215 for (
Vertex v = 0; v < 8; ++v )
222 THEN(
"We can iterate over the vertices" ) {
223 auto positions = polymesh.positions();
224 RealPoint exp_positions[] = { { 0,0,0 }, { 1,0,0 }, { 0,1,0 }, { 1,1,0 },
225 { 0,0,1 }, { 1,0,1 }, { 0,1,1 }, { 1,1,1 },
226 { 1,0,2 }, { 0,0,2 } };
227 for (
auto it = polymesh.begin(), itE = polymesh.end(); it != itE; ++it ) {
228 REQUIRE( positions[ *it ] == exp_positions[ *it ] );
235SCENARIO(
"SurfaceMesh< RealPoint3 > mesh helper tests",
"[surfmesh][helper]" )
241 typedef PolygonMeshHelper::NormalsType NormalsType;
242 GIVEN(
"A sphere of radius 10" ) {
244 10, 10, NormalsType::NO_NORMALS );
245 THEN(
"The mesh has Euler characteristic 2" ) {
246 REQUIRE( polymesh.Euler() == 2 );
248 THEN(
"It is a consistent manifold without boundary" ) {
249 auto mani_bdry = polymesh.computeManifoldBoundaryEdges();
250 auto mani_inner = polymesh.computeManifoldInnerEdges();
251 auto mani_inner_c = polymesh.computeManifoldInnerConsistentEdges();
252 auto mani_inner_u = polymesh.computeManifoldInnerUnconsistentEdges();
253 auto non_mani = polymesh.computeNonManifoldEdges();
255 REQUIRE( mani_bdry.size() == 0 );
256 REQUIRE( mani_inner.size() == mani_inner_c.size() );
257 REQUIRE( mani_inner_u.size() == 0 );
258 REQUIRE( non_mani.size() == 0 );
261 GIVEN(
"A torus with radii 3 and 1" ) {
262 auto polymesh = PolygonMeshHelper::makeTorus( 3.0, 1.0,
RealPoint::zero,
263 10, 10, 0, NormalsType::NO_NORMALS );
264 THEN(
"The mesh has Euler characteristic 0" ) {
265 REQUIRE( polymesh.Euler() == 0 );
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 lantern with radii 3" ) {
281 auto polymesh = PolygonMeshHelper::makeLantern( 3.0, 3.0,
RealPoint::zero,
282 10, 10, NormalsType::NO_NORMALS );
283 THEN(
"The mesh has Euler characteristic 0" ) {
284 REQUIRE( polymesh.Euler() == 0 );
286 THEN(
"It is a consistent manifold with 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() == 20 );
294 REQUIRE( mani_inner.size() == mani_inner_c.size() );
295 REQUIRE( mani_inner_u.size() == 0 );
296 REQUIRE( non_mani.size() == 0 );
301SCENARIO(
"SurfaceMesh< RealPoint3 > reader/writer tests",
"[surfmesh][io]" )
309 typedef PolygonMeshHelper::NormalsType NormalsType;
311 10, 10, NormalsType::VERTEX_NORMALS );
312 WHEN(
"Writing the mesh as an OBJ file and reading into another mesh" ) {
313 PolygonMesh readmesh;
314 std::ostringstream output;
315 bool okw = PolygonMeshWriter::writeOBJ( output, polymesh );
316 std::string file = output.str();
317 std::istringstream input( file );
318 bool okr = PolygonMeshReader::readOBJ ( input, readmesh );
319 THEN(
"The read mesh is the same as the original one" ) {
325 REQUIRE( polymesh.Euler() == readmesh.Euler() );
326 REQUIRE( polymesh.nbVertices() == readmesh.nbVertices() );
327 REQUIRE( polymesh.nbEdges() == readmesh.nbEdges() );
328 REQUIRE( polymesh.nbFaces() == readmesh.nbFaces() );
329 REQUIRE( polymesh.neighborVertices( 0 ).size()
330 == readmesh.neighborVertices( 0 ).size() );
331 REQUIRE( polymesh.neighborVertices( 20 ).size()
332 == readmesh.neighborVertices( 20 ).size() );
333 REQUIRE( polymesh.vertexNormals().size() == readmesh.vertexNormals().size() );
338SCENARIO(
"SurfaceMesh< RealPoint3 > boundary tests",
"[surfmesh][boundary]" )
345 WHEN(
"Checking the topolopgy of the mesh boundary" ) {
346 auto chains = polymesh2.computeManifoldBoundaryChains();
347 THEN(
"The box as a manifold boundary" ) {
349 REQUIRE( polymesh2.isBoundariesManifold() ==
true);
350 REQUIRE( polymesh2.isBoundariesManifold(
false) ==
true);
352 REQUIRE( chains[0].size() == 6);
354 THEN(
"The extra mesh does not have a manifold boundary" ) {
355 REQUIRE( polymesh.isBoundariesManifold() ==
false);
Aim: This class is useful to perform a breadth-first exploration of a graph given a starting point or...
const Node & current() const
Aim: Implements basic operations that will be used in Point and Vector classes.
static Self zero
Static const for zero PointVector.
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))
SurfaceMesh< PointVector< 3, double >, PointVector< 3, double > > makeBox()
SurfaceMesh< PointVector< 3, double >, PointVector< 3, double > > makeNonManifoldBoundary()
SCENARIO("UnorderedSetByBlock< PointVector< 2, int > unit tests with 32 bits blocks", "[unorderedsetbyblock][2d]")