RINGMesh  Version 5.0.0
A programming library for geological model meshes
geogram_extension.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 <mutex>
41 
42 #include <geogram/basic/attributes.h>
43 #include <geogram/basic/memory.h>
44 
50 namespace GEO
51 {
52  class Mesh;
53 }
54 
55 namespace RINGMesh
56 {
62  template < typename T, typename U = T >
64  const std::vector< T >& in, index_t from, index_t to )
65  {
66  ringmesh_assert( to < in.size() + 1 );
67  ringmesh_assert( from < to );
68  index_t nb_to_copy( to - from );
69  GEO::vector< U > out( nb_to_copy );
70  for( auto i : range( nb_to_copy ) )
71  {
72  out[i] = in[from + i];
73  }
74  return out;
75  }
76 
83  template < typename T, typename U = T >
84  GEO::vector< U > copy_std_vector_to_geo_vector( const std::vector< T >& in )
85  {
86  index_t size = static_cast< index_t >( in.size() );
87  return copy_std_vector_to_geo_vector< T, U >( in, 0, size );
88  }
89 
90  /***********************************************************************/
91  /* Loading and saving a GEO::Mesh */
92 
95  void RINGMESH_API ringmesh_mesh_io_initialize();
96 
97  /******************************************************************/
98  /* Operations on a GEO::Mesh */
99 
106  double RINGMESH_API mesh_cell_signed_volume(
107  const GEO::Mesh& M, index_t c );
114  double RINGMESH_API mesh_cell_volume( const GEO::Mesh& M, index_t c );
115 
123  vec3 RINGMESH_API mesh_cell_facet_barycenter(
124  const GEO::Mesh& M, index_t cell, index_t f );
125 
133  vec3 RINGMESH_API mesh_cell_barycenter( const GEO::Mesh& M, index_t cell );
134 
142  template < typename T >
143  class AttributeVector : public std::vector< GEO::Attribute< T >* >
144  {
146 
147  public:
148  using base_class = std::vector< GEO::Attribute< T >* >;
149  AttributeVector() = default;
150  explicit AttributeVector( index_t size ) : base_class( size, nullptr )
151  {
152  }
153 
154  void bind_one_attribute( index_t i,
155  GEO::AttributesManager& manager,
156  const std::string& attribute_name )
157  {
158  base_class::operator[]( i ) =
159  new GEO::Attribute< T >( manager, attribute_name );
160  }
161 
162  GEO::Attribute< T >& operator[]( index_t i )
163  {
164  return *base_class::operator[]( i );
165  }
166 
167  const GEO::Attribute< T >& operator[]( index_t i ) const
168  {
169  return *base_class::operator[]( i );
170  }
171 
172  bool is_attribute_bound( index_t i ) const
173  {
174  return base_class::operator[]( i ) != nullptr;
175  }
176 
177  void unbind( index_t i )
178  {
179  if( base_class::operator[]( i ) )
180  {
181  // I am not sure, but unbind should do the deallocation [JP]
182  operator[]( i ).unbind();
183  delete base_class::operator[]( i );
184  base_class::operator[]( i ) = nullptr;
185  }
186  }
187 
189  {
190  for( auto i : range( base_class::size() ) )
191  {
192  unbind( i );
193  }
194  }
195  };
196 
197  void RINGMESH_API print_bounded_attributes( const GEO::Mesh& M );
198 
199  class RINGMESH_API ThreadSafeConsoleLogger : public GEO::ConsoleLogger
200  {
201  using base_class = GEO::ConsoleLogger;
202 
203  public:
204  void div( const std::string& title )
205  {
206  std::lock_guard< std::mutex > lock( lock_ );
207  base_class::div( title );
208  }
209 
210  void out( const std::string& str )
211  {
212  std::lock_guard< std::mutex > lock( lock_ );
213  base_class::out( str );
214  }
215 
216  void warn( const std::string& str )
217  {
218  std::lock_guard< std::mutex > lock( lock_ );
219  base_class::warn( str );
220  }
221 
222  void err( const std::string& str )
223  {
224  std::lock_guard< std::mutex > lock( lock_ );
225  base_class::err( str );
226  }
227 
228  void status( const std::string& str )
229  {
230  std::lock_guard< std::mutex > lock( lock_ );
231  base_class::status( str );
232  }
233 
234  private:
235  std::mutex lock_{};
236  };
237 } // namespace RINGMesh
const GEO::Attribute< T > & operator[](index_t i) const
#define ringmesh_disable_copy_and_move(Class)
Definition: common.h:76
void status(const std::string &str)
Vector of pointers to Geogram attributes.
void div(const std::string &title)
vec3 RINGMESH_API mesh_cell_barycenter(const GEO::Mesh &M, index_t cell)
vecn< 3 > vec3
Definition: types.h:76
double RINGMESH_API mesh_cell_signed_volume(const GEO::Mesh &M, index_t c)
void RINGMESH_API print_bounded_attributes(const GEO::Mesh &M)
void warn(const std::string &str)
double RINGMESH_API mesh_cell_volume(const GEO::Mesh &M, index_t c)
void out(const std::string &str)
void err(const std::string &str)
GEO::Attribute< T > & operator[](index_t i)
bool is_attribute_bound(index_t i) const
#define ringmesh_assert(x)
void bind_one_attribute(index_t i, GEO::AttributesManager &manager, const std::string &attribute_name)
GEO::vector< U > copy_std_vector_to_geo_vector(const std::vector< T > &in)
vec3 RINGMESH_API mesh_cell_facet_barycenter(const GEO::Mesh &M, index_t cell, index_t f)
std::vector< GEO::Attribute< T > *> base_class
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
void RINGMESH_API ringmesh_mesh_io_initialize()
complement the available MeshIOHandler