RINGMesh  Version 5.0.0
A programming library for geological model meshes
test-aabb.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #include <ringmesh/ringmesh_tests_config.h>
37 
38 #include <vector>
39 
41 #include <ringmesh/basic/matrix.h>
42 #include <ringmesh/mesh/aabb.h>
44 #include <ringmesh/mesh/mesh.h>
46 
51 using namespace RINGMesh;
52 
53 template < index_t DIMENSION >
54 vecn< DIMENSION > create_vertex( double i, double j );
55 
56 template <>
57 vecn< 2 > create_vertex( double i, double j )
58 {
59  return vec2( i, j );
60 }
61 
62 template <>
63 vecn< 3 > create_vertex( double i, double j )
64 {
65  return vec3( i, j, 0 );
66 }
67 
68 template < index_t DIMENSION >
69 void add_vertices( LineMeshBuilder< DIMENSION >* builder, index_t size )
70 {
71  builder->create_vertices( size );
72  for( index_t i : range( size ) )
73  {
74  builder->set_vertex( i, create_vertex< DIMENSION >( i, i + 1 ) );
75  }
76 }
77 
78 template < index_t DIMENSION >
79 void add_vertices( SurfaceMeshBuilder< DIMENSION >* builder, index_t size )
80 {
81  builder->create_vertices( size * size );
82  index_t id = 0;
83  for( index_t i : range( size ) )
84  {
85  for( index_t j : range( size ) )
86  {
87  builder->set_vertex( id++, create_vertex< DIMENSION >( i, j ) );
88  }
89  }
90 }
91 
92 template < index_t DIMENSION >
93 void add_vertices( VolumeMeshBuilder< DIMENSION >* builder, index_t size )
94 {
95  builder->create_vertices( size * size * size );
96  index_t id = 0;
97  for( index_t i : range( size ) )
98  {
99  for( index_t j : range( size ) )
100  {
101  for( index_t k : range( size ) )
102  {
103  builder->set_vertex( id++, vecn< DIMENSION >( i, j, k ) );
104  }
105  }
106  }
107 }
108 
109 template < index_t DIMENSION >
110 void add_edges( LineMeshBuilder< DIMENSION >* builder, index_t size )
111 {
112  builder->create_edges( size - 1 );
113  for( index_t i : range( size - 1 ) )
114  {
115  builder->set_edge_vertex( EdgeLocalVertex( i, 0 ), i );
116  builder->set_edge_vertex( EdgeLocalVertex( i, 1 ), i + 1 );
117  }
118 }
119 
120 template < index_t DIMENSION >
121 void add_triangles( SurfaceMeshBuilder< DIMENSION >* builder, index_t size )
122 {
123  builder->create_triangles( ( size - 1 ) * ( size - 1 ) * 2 );
124  index_t id = 0;
125  for( index_t i : range( size - 1 ) )
126  {
127  for( index_t j : range( size - 1 ) )
128  {
129  builder->set_polygon_vertex(
130  PolygonLocalEdge( id, 0 ), i * size + j );
131  builder->set_polygon_vertex(
132  PolygonLocalEdge( id, 1 ), i * size + j + 1 );
133  builder->set_polygon_vertex(
134  PolygonLocalEdge( id, 2 ), ( i + 1 ) * size + j );
135  id++;
136  builder->set_polygon_vertex(
137  PolygonLocalEdge( id, 0 ), i * size + j + 1 );
138  builder->set_polygon_vertex(
139  PolygonLocalEdge( id, 1 ), ( i + 1 ) * size + j + 1 );
140  builder->set_polygon_vertex(
141  PolygonLocalEdge( id, 2 ), ( i + 1 ) * size + j );
142  id++;
143  }
144  }
145 }
146 
147 template < index_t DIMENSION >
148 void add_hexs( VolumeMeshBuilder< DIMENSION >* builder, index_t size )
149 {
150  builder->create_cells(
151  ( size - 1 ) * ( size - 1 ) * ( size - 1 ), CellType::HEXAHEDRON );
152  index_t id = 0;
153  for( index_t i : range( size - 1 ) )
154  {
155  for( index_t j : range( size - 1 ) )
156  {
157  for( index_t k : range( size - 1 ) )
158  {
159  index_t corner = i + j * size + k * size * size;
160  builder->set_cell_vertex( ElementLocalVertex( id, 0 ), corner );
161  builder->set_cell_vertex(
162  ElementLocalVertex( id, 4 ), corner + size * size );
163  builder->set_cell_vertex(
164  ElementLocalVertex( id, 6 ), corner + size * size + 1 );
165  builder->set_cell_vertex(
166  ElementLocalVertex( id, 2 ), corner + 1 );
167  builder->set_cell_vertex(
168  ElementLocalVertex( id, 1 ), corner + size );
169  builder->set_cell_vertex(
170  ElementLocalVertex( id, 5 ), corner + size * size + size );
171  builder->set_cell_vertex( ElementLocalVertex( id, 7 ),
172  corner + size * size + size + 1 );
173  builder->set_cell_vertex(
174  ElementLocalVertex( id, 3 ), corner + size + 1 );
175  id++;
176  }
177  }
178  }
179  builder->connect_cells();
180 }
181 
182 template < index_t DIMENSION >
183 void check_tree( const SurfaceAABBTree< DIMENSION >& tree, index_t size )
184 {
185  double offset = 0.2;
186  index_t id = 0;
187  for( index_t i : range( size - 1 ) )
188  {
189  for( index_t j : range( size - 1 ) )
190  {
191  vecn< DIMENSION > query1 =
192  create_vertex< DIMENSION >( i + offset, j + offset );
193  index_t triangle1 = NO_ID;
194  vecn< DIMENSION > nearest_point1;
195  std::tie( triangle1, nearest_point1, std::ignore ) =
196  tree.closest_triangle( query1 );
197  if( triangle1 != id++ )
198  {
199  throw RINGMeshException(
200  "TEST", "Not the correct triangle found" );
201  }
202  if( nearest_point1 != query1 )
203  {
204  throw RINGMeshException(
205  "TEST", "Not the correct nearest point found" );
206  }
207 
208  vecn< DIMENSION > query2 =
209  create_vertex< DIMENSION >( i + 1 - offset, j + 1 - offset );
210  index_t triangle2 = NO_ID;
211  vecn< DIMENSION > nearest_point2;
212  std::tie( triangle2, nearest_point2, std::ignore ) =
213  tree.closest_triangle( query2 );
214  if( triangle2 != id++ )
215  {
216  throw RINGMeshException(
217  "TEST", "Not the correct triangle found" );
218  }
219  if( nearest_point2 != query2 )
220  {
221  throw RINGMeshException(
222  "TEST", "Not the correct nearest point found" );
223  }
224  }
225  }
226 
227  vecn< DIMENSION > query;
228  index_t triangle = NO_ID;
229  vecn< DIMENSION > nearest_point;
230  std::tie( triangle, nearest_point, std::ignore ) =
231  tree.closest_triangle( query );
232  if( triangle != 0 )
233  {
234  throw RINGMeshException( "TEST", "Not the correct triangle found" );
235  }
236  if( nearest_point != vecn< DIMENSION >() )
237  {
238  throw RINGMeshException(
239  "TEST", "Not the correct nearest point found" );
240  }
241 }
242 
243 template < index_t DIMENSION >
245  const GeogramVolumeMesh< DIMENSION >& mesh_hex,
246  index_t hex )
247 {
248  std::vector< index_t > vertices_in_hex( 8 );
249  for( index_t v : range( 8 ) )
250  {
251  vertices_in_hex[v] =
252  mesh_hex.cell_vertex( ElementLocalVertex( hex, v ) );
253  }
254  builder.set_cell_vertex(
255  ElementLocalVertex( 5 * hex, 0 ), vertices_in_hex[0] );
256  builder.set_cell_vertex(
257  ElementLocalVertex( 5 * hex, 1 ), vertices_in_hex[4] );
258  builder.set_cell_vertex(
259  ElementLocalVertex( 5 * hex, 2 ), vertices_in_hex[5] );
260  builder.set_cell_vertex(
261  ElementLocalVertex( 5 * hex, 3 ), vertices_in_hex[6] );
262 
263  builder.set_cell_vertex(
264  ElementLocalVertex( 5 * hex + 1, 0 ), vertices_in_hex[0] );
265  builder.set_cell_vertex(
266  ElementLocalVertex( 5 * hex + 1, 1 ), vertices_in_hex[2] );
267  builder.set_cell_vertex(
268  ElementLocalVertex( 5 * hex + 1, 2 ), vertices_in_hex[3] );
269  builder.set_cell_vertex(
270  ElementLocalVertex( 5 * hex + 1, 3 ), vertices_in_hex[6] );
271 
272  builder.set_cell_vertex(
273  ElementLocalVertex( 5 * hex + 2, 0 ), vertices_in_hex[7] );
274  builder.set_cell_vertex(
275  ElementLocalVertex( 5 * hex + 2, 1 ), vertices_in_hex[6] );
276  builder.set_cell_vertex(
277  ElementLocalVertex( 5 * hex + 2, 2 ), vertices_in_hex[3] );
278  builder.set_cell_vertex(
279  ElementLocalVertex( 5 * hex + 2, 3 ), vertices_in_hex[5] );
280 
281  builder.set_cell_vertex(
282  ElementLocalVertex( 5 * hex + 3, 0 ), vertices_in_hex[1] );
283  builder.set_cell_vertex(
284  ElementLocalVertex( 5 * hex + 3, 1 ), vertices_in_hex[0] );
285  builder.set_cell_vertex(
286  ElementLocalVertex( 5 * hex + 3, 2 ), vertices_in_hex[5] );
287  builder.set_cell_vertex(
288  ElementLocalVertex( 5 * hex + 3, 3 ), vertices_in_hex[3] );
289 
290  builder.set_cell_vertex(
291  ElementLocalVertex( 5 * hex + 4, 0 ), vertices_in_hex[0] );
292  builder.set_cell_vertex(
293  ElementLocalVertex( 5 * hex + 4, 1 ), vertices_in_hex[5] );
294  builder.set_cell_vertex(
295  ElementLocalVertex( 5 * hex + 4, 2 ), vertices_in_hex[6] );
296  builder.set_cell_vertex(
297  ElementLocalVertex( 5 * hex + 4, 3 ), vertices_in_hex[3] );
298 }
299 
300 template < index_t DIMENSION >
303  index_t size )
304 {
305  std::unique_ptr< VolumeMeshBuilder< DIMENSION > > builder =
307  builder->create_cells( hex_mesh.nb_cells() * 5, CellType::TETRAHEDRON );
308  add_vertices( builder.get(), size );
309  for( index_t hex : range( hex_mesh.nb_cells() ) )
310  {
311  create_5_tets_from_hex( *builder, hex_mesh, hex );
312  }
313 }
314 
315 template < index_t DIMENSION >
317 {
318  Logger::out( "TEST", "Test Surface AABB ", DIMENSION, "D" );
319  GeogramSurfaceMesh< DIMENSION > geogram_mesh;
320  std::unique_ptr< SurfaceMeshBuilder< DIMENSION > > builder =
322 
323  index_t size = 10;
324  add_vertices( builder.get(), size );
325  add_triangles( builder.get(), size );
326 
327  SurfaceAABBTree< DIMENSION > tree( geogram_mesh );
328  check_tree( tree, size );
329 }
330 
331 template < index_t DIMENSION >
333 {
334  for( index_t c : range( mesh.nb_cells() ) )
335  {
336  vecn< DIMENSION > barycenter = mesh.cell_barycenter( c );
337  const VolumeAABBTree< DIMENSION >& aabb3D = mesh.cell_aabb();
338  index_t containing_cell = aabb3D.containing_cell( barycenter );
339  if( containing_cell != c )
340  {
341  throw RINGMeshException( "TEST", "Not the correct cell found" );
342  }
343  }
344 }
345 
346 template < index_t DIMENSION >
348 {
349  Logger::out( "TEST", "Test Volume AABB ", DIMENSION, "D" );
350  GeogramVolumeMesh< DIMENSION > geogram_mesh_hex;
351  std::unique_ptr< VolumeMeshBuilder< DIMENSION > > builder =
353 
354  index_t size = 10;
355  add_vertices( builder.get(), size );
356  add_hexs( builder.get(), size );
357 
358  GeogramVolumeMesh< DIMENSION > geogram_mesh_tet;
359  decompose_in_tet( geogram_mesh_hex, geogram_mesh_tet, size );
360  test_locate_cell_on_3D_mesh( geogram_mesh_tet );
361 }
362 
363 template < index_t DIMENSION >
365 {
366  for( index_t e : range( mesh.nb_edges() ) )
367  {
368  vecn< DIMENSION > barycenter = mesh.edge_barycenter( e );
369  const LineAABBTree< DIMENSION >& aabb1D = mesh.edge_aabb();
370  index_t closest_edge = NO_ID;
371  std::tie( closest_edge, std::ignore, std::ignore ) =
372  aabb1D.closest_edge( barycenter );
373  if( closest_edge != e )
374  {
375  throw RINGMeshException( "TEST", "Not the correct edge found" );
376  }
377  }
378 }
379 
380 template < index_t DIMENSION >
382 {
383  Logger::out( "TEST", "Test Line AABB ", DIMENSION, "D" );
384  GeogramLineMesh< DIMENSION > geogram_mesh;
385  std::unique_ptr< LineMeshBuilder< DIMENSION > > builder =
387 
388  index_t size = 10;
389  add_vertices( builder.get(), size );
390  add_edges( builder.get(), size );
391  test_locate_edge_on_1D_mesh( geogram_mesh );
392 }
393 
394 int main()
395 {
396  using namespace RINGMesh;
397 
398  try
399  {
401  Logger::out( "TEST", "Test AABB" );
402  test_LineAABB< 2 >();
403  test_LineAABB< 3 >();
404  test_SurfaceAABB< 2 >();
405  test_SurfaceAABB< 3 >();
406  test_VolumeAABB< 3 >();
407  }
408  catch( const RINGMeshException& e )
409  {
410  Logger::err( e.category(), e.what() );
411  return 1;
412  }
413  catch( const std::exception& e )
414  {
415  Logger::err( "Exception", e.what() );
416  return 1;
417  }
418  Logger::out( "TEST", "SUCCESS" );
419  return 0;
420 }
GEO::vecng< DIMENSION, double > vecn
Definition: types.h:74
static std::unique_ptr< LineMeshBuilder > create_builder(LineMesh< DIMENSION > &mesh)
void add_vertices(LineMeshBuilder< DIMENSION > *builder, index_t size)
Definition: test-aabb.cpp:69
void add_triangles(SurfaceMeshBuilder< DIMENSION > *builder, index_t size)
Definition: test-aabb.cpp:121
std::tuple< index_t, vecn< DIMENSION >, double > closest_edge(const vecn< DIMENSION > &query) const
Gets the closest edge to a given point.
Definition: aabb.cpp:343
const VolumeAABBTree< DIMENSION > & cell_aabb() const
Creates an AABB tree for a Mesh cells.
Definition: mesh.h:1135
index_t nb_edges() const override
Gets the number of all the edges in the whole Mesh.
Definition: geogram_mesh.h:141
vecn< 3 > vec3
Definition: types.h:76
void test_VolumeAABB()
Definition: test-aabb.cpp:347
vecn< DIMENSION > cell_barycenter(index_t cell_id) const
Definition: mesh.h:1027
index_t cell_vertex(const ElementLocalVertex &cell_local_vertex) const override
Gets a vertex index by cell and local vertex index.
Definition: geogram_mesh.h:203
void add_edges(LineMeshBuilder< DIMENSION > *builder, index_t size)
Definition: test-aabb.cpp:110
void set_cell_vertex(const ElementLocalVertex &cell_local_vertex, index_t vertex_id)
Sets a vertex of a cell by local vertex index.
Definition: mesh_builder.h:941
static std::unique_ptr< VolumeMeshBuilder< DIMENSION > > create_builder(VolumeMesh< DIMENSION > &mesh)
vecn< 2 > vec2
Definition: types.h:78
index_t create_triangles(index_t nb_triangles)
Creates a contiguous chunk of triangles.
Definition: mesh_builder.h:554
void test_SurfaceAABB()
Definition: test-aabb.cpp:316
static void err(const std::string &feature, const Args &... args)
Definition: logger.h:68
void create_5_tets_from_hex(VolumeMeshBuilder< DIMENSION > &builder, const GeogramVolumeMesh< DIMENSION > &mesh_hex, index_t hex)
Definition: test-aabb.cpp:244
static void out(const std::string &feature, const Args &... args)
Definition: logger.h:61
index_t create_vertices(index_t nb)
Creates a contiguous chunk of vertices.
Definition: mesh_builder.h:148
vecn< DIMENSION > edge_barycenter(index_t edge_id) const
Definition: mesh.h:274
void set_polygon_vertex(const PolygonLocalEdge &polygon_local_edge, index_t vertex_id)
Sets a vertex of a polygon by local vertex index.
Definition: mesh_builder.h:580
void add_hexs(VolumeMeshBuilder< DIMENSION > *builder, index_t size)
Definition: test-aabb.cpp:148
void set_vertex(index_t v_id, const vecn< DIMENSION > &vertex)
Sets a point.
Definition: mesh_builder.h:117
void test_locate_cell_on_3D_mesh(const GeogramVolumeMesh< DIMENSION > &mesh)
Definition: test-aabb.cpp:332
void check_tree(const SurfaceAABBTree< DIMENSION > &tree, index_t size)
Definition: test-aabb.cpp:183
const LineAABBTree< DIMENSION > & edge_aabb() const
Creates an AABB tree for a Mesh edges.
Definition: mesh.h:306
index_t create_cells(index_t nb_cells, CellType type)
Creates a contiguous chunk of cells of the same type.
Definition: mesh_builder.h:914
void decompose_in_tet(const GeogramVolumeMesh< DIMENSION > &hex_mesh, GeogramVolumeMesh< DIMENSION > &tet_mesh, index_t size)
Definition: test-aabb.cpp:301
index_t nb_cells() const override
Gets the number of cells in the Mesh.
Definition: geogram_mesh.h:257
index_t containing_cell(const vecn< DIMENSION > &query) const
Gets the cell contining a point.
Definition: aabb.cpp:450
const std::string & category() const
Definition: common.h:165
virtual void connect_cells()=0
Retrieve the adjacencies.
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
index_t create_edges(index_t nb_edges)
Creates a contiguous chunk of edges.
Definition: mesh_builder.h:355
void test_locate_edge_on_1D_mesh(const GeogramLineMesh< DIMENSION > &mesh)
Definition: test-aabb.cpp:364
vecn< DIMENSION > create_vertex(double i, double j)
Definition: test-aabb.cpp:57
void set_edge_vertex(const EdgeLocalVertex &edge_local_vertex, index_t vertex_id)
Sets a vertex of a edge by local vertex index.
Definition: mesh_builder.h:370
std::tuple< index_t, vecn< DIMENSION >, double > closest_triangle(const vecn< DIMENSION > &query) const
Gets the closest triangle to a given point.
Definition: aabb.cpp:392
int main()
Definition: test-aabb.cpp:394
static std::unique_ptr< SurfaceMeshBuilder< DIMENSION > > create_builder(SurfaceMesh< DIMENSION > &mesh)
void RINGMESH_API default_configure()
Definition: common.cpp:99
void test_LineAABB()
Definition: test-aabb.cpp:381