RINGMesh  Version 5.0.0
A programming library for geological model meshes
geogram_mesh_builder.h
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 #pragma once
37 
38 #include <ringmesh/basic/common.h>
39 
40 #include <geogram/basic/command_line.h>
41 
42 #include <geogram/mesh/mesh_preprocessing.h>
43 
44 #include <geogram/voronoi/CVT.h>
45 
48 
49 namespace RINGMesh
50 {
51 #define COMMON_GEOGRAM_MESH_BUILDER_IMPLEMENTATION( Class ) \
52 public: \
53  void do_copy( const MeshBase< DIMENSION >& rhs, bool copy_attributes ) \
54  override \
55  { \
56  const auto& geogrammesh = \
57  dynamic_cast< const Class< DIMENSION >& >( rhs ); \
58  mesh_.mesh_->copy( \
59  *geogrammesh.mesh_, copy_attributes, GEO::MESH_ALL_ELEMENTS ); \
60  } \
61  void load_mesh( const std::string& filename ) override \
62  { \
63  GEO::MeshIOFlags ioflags; \
64  ioflags.set_attribute( GEO::MESH_ALL_ATTRIBUTES ); \
65  GEO::mesh_load( filename, *mesh_.mesh_, ioflags ); \
66  } \
67  void do_clear( bool keep_attributes, bool keep_memory ) override \
68  { \
69  mesh_.mesh_->clear( keep_attributes, keep_memory ); \
70  } \
71  void do_repair( GEO::MeshRepairMode mode, double colocate_epsilon ) \
72  override \
73  { \
74  GEO::mesh_repair( *mesh_.mesh_, mode, colocate_epsilon ); \
75  } \
76  void do_set_vertex( index_t v_id, const vecn< DIMENSION >& vertex ) \
77  override \
78  { \
79  mesh_.ref_vertex( v_id ) = vertex; \
80  } \
81  index_t do_create_vertex() override \
82  { \
83  return mesh_.mesh_->vertices.create_vertex(); \
84  } \
85  index_t do_create_vertices( index_t nb ) override \
86  { \
87  return mesh_.mesh_->vertices.create_vertices( nb ); \
88  } \
89  void do_assign_vertices( const std::vector< double >& point_coordinates ) \
90  override \
91  { \
92  GEO::vector< double > point_coordinates_cp = \
93  copy_std_vector_to_geo_vector( point_coordinates ); \
94  mesh_.mesh_->vertices.assign_points( \
95  point_coordinates_cp, DIMENSION, false ); \
96  } \
97  void do_delete_vertices( const std::vector< bool >& to_delete ) override \
98  { \
99  GEO::vector< index_t > vertices_to_delete = \
100  copy_std_vector_to_geo_vector< bool, index_t >( to_delete ); \
101  mesh_.mesh_->vertices.delete_elements( vertices_to_delete, false ); \
102  } \
103  void do_clear_vertices( bool keep_attributes, bool keep_memory ) override \
104  { \
105  mesh_.mesh_->vertices.clear( keep_attributes, keep_memory ); \
106  } \
107  void do_permute_vertices( const std::vector< index_t >& permutation ) \
108  override \
109  { \
110  GEO::vector< index_t > geo_vector_permutation = \
111  copy_std_vector_to_geo_vector( permutation ); \
112  mesh_.mesh_->vertices.permute_elements( geo_vector_permutation ); \
113  } \
114  \
115 private: \
116  Class< DIMENSION >& mesh_
117 
118  template < index_t DIMENSION >
120  {
123 
124  public:
126  : PointSetMeshBuilder< DIMENSION >( mesh ),
127  mesh_( dynamic_cast< GeogramPointSetMesh< DIMENSION >& >( mesh ) )
128  {
129  }
130  };
131 
133 
134  template < index_t DIMENSION >
135  class GeogramLineMeshBuilder : public LineMeshBuilder< DIMENSION >
136  {
139 
140  public:
142  : LineMeshBuilder< DIMENSION >( mesh ),
143  mesh_( dynamic_cast< GeogramLineMesh< DIMENSION >& >( mesh ) )
144  {
145  }
146 
147  void do_create_edge( index_t v1_id, index_t v2_id ) override
148  {
149  mesh_.mesh_->edges.create_edge( v1_id, v2_id );
150  }
151 
152  index_t do_create_edges( index_t nb_edges ) override
153  {
154  return mesh_.mesh_->edges.create_edges( nb_edges );
155  }
156 
157  void do_set_edge_vertex( const EdgeLocalVertex& edge_local_vertex,
158  index_t vertex_id ) override
159  {
160  mesh_.mesh_->edges.set_vertex( edge_local_vertex.edge_id_,
161  edge_local_vertex.local_vertex_id_, vertex_id );
162  }
163 
164  void do_delete_edges( const std::vector< bool >& to_delete ) override
165  {
166  GEO::vector< index_t > edges_to_delete =
167  copy_std_vector_to_geo_vector< bool, index_t >( to_delete );
168  mesh_.mesh_->edges.delete_elements( edges_to_delete, false );
169  }
170 
171  void do_clear_edges( bool keep_attributes, bool keep_memory ) override
172  {
173  mesh_.mesh_->edges.clear( keep_attributes, keep_memory );
174  }
175 
177  const std::vector< index_t >& permutation ) override
178  {
179  GEO::vector< index_t > geo_vector_permutation =
180  copy_std_vector_to_geo_vector( permutation );
181  mesh_.mesh_->edges.permute_elements( geo_vector_permutation );
182  }
183  };
184 
186 
187  template < index_t DIMENSION >
188  class GeogramSurfaceMeshBuilder : public SurfaceMeshBuilder< DIMENSION >
189  {
192 
193  public:
195  : SurfaceMeshBuilder< DIMENSION >( mesh ),
196  mesh_( dynamic_cast< GeogramSurfaceMesh< DIMENSION >& >( mesh ) )
197  {
198  }
199 
201  double min_area, index_t min_polygons ) override
202  {
203  GEO::remove_small_connected_components(
204  *mesh_.mesh_, min_area, min_polygons );
205  }
206 
208  const SurfaceMeshBase< DIMENSION >& surface_in ) override
209  {
210  Logger::instance()->set_minimal( true );
211  const auto& geogram_surf_in =
212  dynamic_cast< const GeogramSurfaceMesh< DIMENSION >& >(
213  surface_in );
214  GEO::CentroidalVoronoiTesselation CVT( geogram_surf_in.mesh_.get(),
215  3, GEO::CmdLine::get_arg( "algo:delaunay" ) );
216  CVT.set_points(
217  mesh_.nb_vertices(), mesh_.mesh_->vertices.point_ptr( 0 ) );
218  CVT.compute_surface( mesh_.mesh_.get(), false );
219  Logger::instance()->set_minimal( false );
220  }
221 
222  void do_create_polygons( const std::vector< index_t >& polygons,
223  const std::vector< index_t >& polygon_ptr ) override
224  {
225  for( auto p : range( polygon_ptr.size() - 1 ) )
226  {
227  index_t start = polygon_ptr[p];
228  index_t end = polygon_ptr[p + 1];
229  GEO::vector< index_t > polygon_vertices =
230  copy_std_vector_to_geo_vector( polygons, start, end );
231  mesh_.mesh_->facets.create_polygon( polygon_vertices );
232  }
233  }
234 
236  const std::vector< index_t >& vertices ) override
237  {
238  GEO::vector< index_t > polygon_vertices =
239  copy_std_vector_to_geo_vector( vertices );
240  return mesh_.mesh_->facets.create_polygon( polygon_vertices );
241  }
242 
243  index_t do_create_triangles( index_t nb_triangles ) override
244  {
245  return mesh_.mesh_->facets.create_triangles( nb_triangles );
246  }
247 
248  index_t do_create_quads( index_t nb_quads ) override
249  {
250  return mesh_.mesh_->facets.create_quads( nb_quads );
251  }
252 
254  const RINGMesh::PolygonLocalEdge& polygon_local_edge,
255  index_t vertex_id ) override
256  {
257  mesh_.mesh_->facets.set_vertex( polygon_local_edge.polygon_id_,
258  polygon_local_edge.local_edge_id_, vertex_id );
259  }
260 
262  const RINGMesh::PolygonLocalEdge& polygon_local_edge,
263  index_t specifies ) override
264  {
265  mesh_.mesh_->facets.set_adjacent( polygon_local_edge.polygon_id_,
266  polygon_local_edge.local_edge_id_, specifies );
267  }
268 
270  bool keep_attributes, bool keep_memory ) override
271  {
272  mesh_.mesh_->facets.clear( keep_attributes, keep_memory );
273  }
274 
276  const std::vector< index_t >& permutation ) override
277  {
278  GEO::vector< index_t > geo_vector_permutation =
279  copy_std_vector_to_geo_vector( permutation );
280  mesh_.mesh_->facets.permute_elements( geo_vector_permutation );
281  }
282 
283  void do_delete_polygons( const std::vector< bool >& to_delete ) override
284  {
285  GEO::vector< index_t > polygons_to_delete =
286  copy_std_vector_to_geo_vector< bool, index_t >( to_delete );
287  mesh_.mesh_->facets.delete_elements( polygons_to_delete, false );
288  }
289  };
290 
292 
293  template < index_t DIMENSION >
294  class GeogramVolumeMeshBuilder : public VolumeMeshBuilder< DIMENSION >
295  {
297  ringmesh_template_assert_3d( DIMENSION );
298 
299  public:
301  : VolumeMeshBuilder< DIMENSION >( mesh ),
302  mesh_( dynamic_cast< GeogramVolumeMesh< DIMENSION >& >( mesh ) )
303  {
304  }
305 
306  index_t do_create_cells( index_t nb_cells, CellType type ) override
307  {
308  return mesh_.mesh_->cells.create_cells(
309  nb_cells, static_cast< GEO::MeshCellType >( type ) );
310  }
311 
313  const std::vector< index_t >& tets ) override
314  {
315  GEO::vector< index_t > copy = copy_std_vector_to_geo_vector( tets );
316  mesh_.mesh_->cells.assign_tet_mesh( copy, false );
317  }
318 
319  void do_set_cell_vertex( const ElementLocalVertex& cell_local_vertex,
320  index_t vertex_id ) override
321  {
322  mesh_.mesh_->cells.set_vertex( cell_local_vertex.element_id_,
323  cell_local_vertex.local_vertex_id_, vertex_id );
324  }
325 
327  index_t corner_index, index_t vertex_index ) override
328  {
329  mesh_.mesh_->cell_corners.set_vertex( corner_index, vertex_index );
330  }
331 
332  void do_set_cell_adjacent( const CellLocalFacet& cell_local_facet,
333  index_t cell_adjacent ) override
334  {
335  mesh_.mesh_->cells.set_adjacent( cell_local_facet.cell_id_,
336  cell_local_facet.local_facet_id_, cell_adjacent );
337  }
338 
339  void connect_cells() override
340  {
341  mesh_.mesh_->cells.connect();
342  }
343 
344  void do_clear_cells( bool keep_attributes, bool keep_memory ) override
345  {
346  mesh_.mesh_->cells.clear( keep_attributes, keep_memory );
347  }
348 
350  const std::vector< index_t >& permutation ) override
351  {
352  GEO::vector< index_t > geo_vector_permutation =
353  copy_std_vector_to_geo_vector( permutation );
354  mesh_.mesh_->cells.permute_elements( geo_vector_permutation );
355  }
356 
357  void do_delete_cells( const std::vector< bool >& to_delete ) override
358  {
359  GEO::vector< index_t > geo_to_delete =
360  copy_std_vector_to_geo_vector< bool, index_t >( to_delete );
361  mesh_.mesh_->cells.delete_elements( geo_to_delete, false );
362  }
363  };
364 
366 
367 } // namespace RINGMesh
void do_permute_polygons(const std::vector< index_t > &permutation) override
void do_permute_cells(const std::vector< index_t > &permutation) override
Applies a permutation to the entities and their attributes. On exit, permutation is modified (used fo...
void do_clear_cells(bool keep_attributes, bool keep_memory) override
Removes all the cells and attributes.
void do_delete_edges(const std::vector< bool > &to_delete) override
Deletes a set of edges.
index_t do_create_triangles(index_t nb_triangles) override
Creates a contiguous chunk of triangles.
void do_permute_edges(const std::vector< index_t > &permutation) override
void do_set_edge_vertex(const EdgeLocalVertex &edge_local_vertex, index_t vertex_id) override
Sets a vertex of a edge by local vertex index.
void do_create_edge(index_t v1_id, index_t v2_id) override
Create a new edge.
index_t do_create_edges(index_t nb_edges) override
Creates a contiguous chunk of edges.
ALIAS_2D_AND_3D(Box)
#define ringmesh_template_assert_3d(type)
Definition: common.h:84
void do_set_cell_vertex(const ElementLocalVertex &cell_local_vertex, index_t vertex_id) override
Sets a vertex of a cell by local vertex index.
void copy(const MeshBase< DIMENSION > &rhs, bool copy_attributes)
Copy a mesh into this one.
Definition: mesh_builder.h:75
void remove_small_connected_components(double min_area, index_t min_polygons) override
Removes the connected components that have an area smaller than a given threshold.
void do_assign_cell_tet_mesh(const std::vector< index_t > &tets) override
COMMON_GEOGRAM_MESH_BUILDER_IMPLEMENTATION(GeogramPointSetMesh)
void do_create_polygons(const std::vector< index_t > &polygons, const std::vector< index_t > &polygon_ptr) override
CellType
Definition: types.h:89
static GEO::Logger * instance()
Definition: logger.h:81
void triangulate(const SurfaceMeshBase< DIMENSION > &surface_in) override
index_t local_vertex_id_
Definition: mesh.h:99
index_t do_create_cells(index_t nb_cells, CellType type) override
Creates a contiguous chunk of cells of the same type.
void do_clear_edges(bool keep_attributes, bool keep_memory) override
Removes all the edges and attributes.
void do_delete_cells(const std::vector< bool > &to_delete) override
Deletes a set of cells.
void do_set_cell_corner_vertex_index(index_t corner_index, index_t vertex_index) override
Sets the vertex that a corner is incident to.
void do_delete_polygons(const std::vector< bool > &to_delete) override
Deletes a set of polygons.
void do_set_cell_adjacent(const CellLocalFacet &cell_local_facet, index_t cell_adjacent) override
Sets the cell adjacent.
void do_set_polygon_vertex(const RINGMesh::PolygonLocalEdge &polygon_local_edge, index_t vertex_id) override
Sets a vertex of a polygon by local vertex index.
GEO::vector< U > copy_std_vector_to_geo_vector(const std::vector< T > &in, index_t from, index_t to)
GeogramLineMeshBuilder(LineMesh< DIMENSION > &mesh)
void do_clear_polygons(bool keep_attributes, bool keep_memory) override
Removes all the polygons and attributes.
GeogramSurfaceMeshBuilder(SurfaceMesh< DIMENSION > &mesh)
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
GeogramPointSetMeshBuilder(PointSetMesh< DIMENSION > &mesh)
void do_set_polygon_adjacent(const RINGMesh::PolygonLocalEdge &polygon_local_edge, index_t specifies) override
Sets an adjacent polygon by both its polygon.
index_t do_create_quads(index_t nb_quads) override
Creates a contiguous chunk of quads.
void connect_cells() override
Retrieve the adjacencies.
index_t do_create_polygon(const std::vector< index_t > &vertices) override
Creates a polygon.
index_t local_facet_id_
Definition: mesh.h:127
GeogramVolumeMeshBuilder(VolumeMesh< DIMENSION > &mesh)