Geogram  Version 1.9.1-rc
A programming library of geometric algorithms
mesh_io.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2022 Inria
3  * 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  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of the ALICE Project-Team nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Contact: Bruno Levy
30  *
31  * https://www.inria.fr/fr/bruno-levy
32  *
33  * Inria,
34  * Domaine de Voluceau,
35  * 78150 Le Chesnay - Rocquencourt
36  * FRANCE
37  *
38  */
39 
40 #ifndef GEOGRAM_MESH_NEW_MESH_IO
41 #define GEOGRAM_MESH_NEW_MESH_IO
42 
43 #include <geogram/basic/common.h>
44 #include <geogram/basic/numeric.h>
45 #include <geogram/basic/factory.h>
47 #include <geogram/mesh/mesh.h>
48 #include <string>
49 
55 namespace GEO {
56 
57  class InputGeoFile;
58  class OutputGeoFile;
59 
67  MESH_NO_ATTRIBUTES = 0,
68  MESH_VERTEX_REGION = 1,
69  MESH_VERTEX_TEX_COORD = 2,
70  MESH_VERTEX_COLOR = 4,
71  MESH_FACET_REGION = 8,
72  MESH_CELL_REGION = 16,
73  MESH_EDGE_REGION = 32,
74  MESH_ALL_ATTRIBUTES = 255
75  };
76 
77 
83  class GEOGRAM_API MeshIOFlags {
84  public:
89 
97  return dimension_;
98  }
99 
106  dimension_ = x;
107  }
108 
114  return attributes_;
115  }
116 
122  attributes_ = x;
123  }
124 
131  attributes_ = MeshAttributesFlags(attributes_ | x);
132  }
133 
140  attributes_ = MeshAttributesFlags(attributes_ & ~x);
141  }
142 
152  return (attributes_ & x) != 0;
153  }
154 
159  MeshElementsFlags elements() const {
160  return elements_;
161  }
162 
167  void set_elements(MeshElementsFlags x) {
168  elements_ = x;
169  }
170 
177  void set_element(MeshElementsFlags x) {
178  elements_ = MeshElementsFlags(elements_ | x);
179  }
180 
187  void reset_element(MeshElementsFlags x) {
188  elements_ = MeshElementsFlags(elements_ & ~x);
189  }
190 
199  bool has_element(MeshElementsFlags x) const {
200  return (elements_ & x) != 0;
201  }
202 
210  void set_texture_filename(const std::string& x) {
211  texture_filename_ = x;
212  }
213 
219  const std::string& get_texture_filename() const {
220  return texture_filename_;
221  }
222 
230  void set_verbose(bool x) {
231  verbose_ = x;
232  }
233 
242  bool verbose() const {
243  return verbose_;
244  }
245 
246  private:
247  coord_index_t dimension_;
248  MeshAttributesFlags attributes_;
249  MeshElementsFlags elements_;
250  std::string texture_filename_;
251  bool verbose_;
252  };
253 
254 
269  bool GEOGRAM_API mesh_load(
270  const std::string& filename, Mesh& M,
271  const MeshIOFlags& ioflags = MeshIOFlags()
272  );
273 
286  bool GEOGRAM_API mesh_load(
287  InputGeoFile& geofile, Mesh& M,
288  const MeshIOFlags& ioflags = MeshIOFlags()
289  );
290 
291 
305  bool GEOGRAM_API mesh_save(
306  const Mesh& M, const std::string& filename,
307  const MeshIOFlags& ioflags = MeshIOFlags()
308  );
309 
321  bool GEOGRAM_API mesh_save(
322  const Mesh& M, OutputGeoFile& geofile,
323  const MeshIOFlags& ioflags = MeshIOFlags()
324  );
325 
326 
327  /*************************************************************************/
328 
352  class GEOGRAM_API MeshIOHandler : public Counted {
353  public:
365  static MeshIOHandler* create(const std::string& format);
366 
377  static MeshIOHandler* get_handler(const std::string& filename);
378 
386  virtual bool load(
387  const std::string& filename, Mesh& M,
388  const MeshIOFlags& ioflags = MeshIOFlags()
389  ) = 0;
390 
399  virtual bool save(
400  const Mesh& M, const std::string& filename,
401  const MeshIOFlags& ioflags = MeshIOFlags()
402  ) = 0;
403 
404  protected:
409  }
410 
414  ~MeshIOHandler() override;
415 
416  virtual void bind_attributes(
417  const Mesh& M, const MeshIOFlags& flags, bool create
418  );
419  virtual void unbind_attributes();
420 
421  protected:
422  Attribute<index_t> vertex_region_;
423  Attribute<index_t> edge_region_;
424  Attribute<index_t> facet_region_;
425  Attribute<index_t> cell_region_;
426  };
427 
433 
445 
451 #define geo_register_MeshIOHandler_creator(type, name) \
452  geo_register_creator(GEO::MeshIOHandlerFactory, type, name)
453 
454 
455  void GEOGRAM_API mesh_io_initialize() ;
456 }
457 
458 #endif
Generic mechanism for attributes.
Base class for reference-counted objects.
Definition: counted.h:71
Factory for types without constructor arguments.
Definition: factory.h:292
Used to read a structured binary file.
Definition: geofile.h:667
Mesh load/save flags.
Definition: mesh_io.h:83
void set_element(MeshElementsFlags x)
Sets a mesh element.
Definition: mesh_io.h:177
bool has_element(MeshElementsFlags x) const
Tests whether a mesh element is set.
Definition: mesh_io.h:199
void set_attribute(MeshAttributesFlags x)
Sets a mesh attribute.
Definition: mesh_io.h:130
const std::string & get_texture_filename() const
Gets the name of the texture image file.
Definition: mesh_io.h:219
void reset_element(MeshElementsFlags x)
Resets a mesh element.
Definition: mesh_io.h:187
void reset_attribute(MeshAttributesFlags &x)
Resets a mesh attribute..
Definition: mesh_io.h:139
MeshAttributesFlags attributes() const
Gets the attributes that should be loaded or saved.
Definition: mesh_io.h:113
MeshIOFlags()
Constructs a new MeshIOFlags with default attributes.
void set_dimension(coord_index_t x)
Sets the dimension of the mesh (number of coordinates of the vertices).
Definition: mesh_io.h:105
void set_elements(MeshElementsFlags x)
Sets the set of mesh elements that should be loaded or stored.
Definition: mesh_io.h:167
void set_attributes(MeshAttributesFlags x)
Sets the attributes that should be loaded or stored.
Definition: mesh_io.h:121
bool has_attribute(MeshAttributesFlags x) const
Tests whether a mesh attribute is set.
Definition: mesh_io.h:151
void set_verbose(bool x)
Sets verbosity.
Definition: mesh_io.h:230
void set_texture_filename(const std::string &x)
Sets the name of the texture image file associated with this mesh.
Definition: mesh_io.h:210
MeshElementsFlags elements() const
Gets the set of mesh elements that should be loaded or stored.
Definition: mesh_io.h:159
bool verbose() const
Tests whether messages should be displayed.
Definition: mesh_io.h:242
coord_index_t dimension() const
Gets the dimension of the mesh (number of coordinates of the vertices).
Definition: mesh_io.h:96
Mesh loader and saver.
Definition: mesh_io.h:352
virtual bool load(const std::string &filename, Mesh &M, const MeshIOFlags &ioflags=MeshIOFlags())=0
Loads a double precision mesh from a file.
SmartPointer< MeshIOHandler > MeshIOHandler_var
A smart pointer that contains a MeshIOHandler object.
Definition: mesh_io.h:432
MeshIOHandler()
MeshIOHandler default constructor.
Definition: mesh_io.h:408
static MeshIOHandler * create(const std::string &format)
Creates a MeshIOHandler.
~MeshIOHandler() override
MeshIOHandler destructor.
virtual bool save(const Mesh &M, const std::string &filename, const MeshIOFlags &ioflags=MeshIOFlags())=0
Saves a mesh to a file.
Factory0< MeshIOHandler > MeshIOHandlerFactory
MeshIOHandler Factory.
Definition: mesh_io.h:444
static MeshIOHandler * get_handler(const std::string &filename)
Gets the MeshIOHandler for a file.
Represents a mesh.
Definition: mesh.h:2693
Used to write a structured binary file.
Definition: geofile.h:771
A smart pointer with reference-counted copy semantics.
Definition: smart_pointer.h:76
Generic factory mechanism.
Common include file, providing basic definitions. Should be included before anything else by all head...
The class that represents a mesh.
Global Vorpaline namespace.
Definition: basic.h:55
bool mesh_save(const Mesh &M, const std::string &filename, const MeshIOFlags &ioflags=MeshIOFlags())
Saves a mesh to a file.
MeshAttributesFlags
Indicates the attributes stored in a mesh and attached to the mesh elements (vertices,...
Definition: mesh_io.h:66
bool mesh_load(const std::string &filename, Mesh &M, const MeshIOFlags &ioflags=MeshIOFlags())
Loads a mesh from a file.
geo_coord_index_t coord_index_t
The type for storing coordinate indices, and iterating on the coordinates of a point.
Definition: numeric.h:363
Types and functions for numbers manipulation.