OpenMEEG
MeshIO.h
Go to the documentation of this file.
1 // Project Name: OpenMEEG (http://openmeeg.github.io)
2 // © INRIA and ENPC under the French open source license CeCILL-B.
3 // See full copyright notice in the file LICENSE.txt
4 // If you make a copy of this file, you must either:
5 // - provide also LICENSE.txt and modify this header to refer to it.
6 // - replace this header by the LICENSE.txt content.
7 
8 #pragma once
9 
10 #include <fstream>
11 #include <map>
12 #include <string>
13 
14 #include <triangle.h>
15 #include <mesh.h>
16 #include <geometry.h>
17 #include <filenames.h>
18 #include <OMExceptions.H>
19 
20 namespace OpenMEEG {
21 
22  // Mesh class
23  // \brief Mesh is a collection of triangles associated to a geometry containing the points
24  // on which triangles are based.
25 
26  class OPENMEEG_EXPORT MeshIO {
27  public:
28 
29  virtual ~MeshIO() { }
30 
31  static MeshIO* create(const std::string& filename) {
32  const std::string& extension = tolower(getFilenameExtension(filename));
33  return registery.at(extension)->clone(filename);
34  }
35 
36  virtual const char* name() const = 0;
37 
38  void open(const std::ios_base::openmode mode=std::ios_base::in) {
39  fs.open(fname,(binary()) ? mode|std::ios_base::binary : mode);
40  if (!fs.is_open())
41  throw OpenMEEG::OpenError(name());
42  }
43 
44  virtual void load_points(Geometry& geom) = 0;
45  virtual void load_triangles(Mesh& m) = 0;
46 
47  virtual void load(Mesh& m) {
48  open(std::ios_base::in);
49  load_points(m.geometry());
50  // TODO
51  load_triangles(m);
52  fs.close();
53  m.update(true);
54  }
55 
56  virtual void save(const Mesh& mesh,std::ostream& os) const = 0;
57 
58  virtual void save(const Mesh& mesh) {
59  open(std::ios_base::out);
60  save(mesh,fs);
61  fs.close();
62  }
63 
64  protected:
65 
66  typedef std::map<std::string,MeshIO*> Registery;
67 
68  class VertexIndices {
69  public:
70 
71  VertexIndices(const Mesh& mesh) {
72  unsigned i = 0;
73  for (const auto& vertex : mesh.vertices())
74  vmap[vertex] = i++;
75  }
76 
77  unsigned operator()(const Triangle& triangle,const unsigned ind) const {
78  return vmap.at(&(triangle.vertex(ind)));
79  }
80 
81  private:
82 
83  std::map<const Vertex*,unsigned> vmap;
84  };
85 
86  virtual MeshIO* clone(const std::string& filename) const = 0;
87  virtual bool binary() const { return false; }
88 
89  void reference_vertices(Mesh& mesh) const { mesh.reference_vertices(indmap); }
90 
92 
93  MeshIO(const std::string& filename,const char* name): fname(filename) { registery.insert({ name, this }); }
94 
95  std::string fname;
96  std::fstream fs;
98  };
99 }
Geometry contains the electrophysiological model Vertices, meshes and domains are stored in this geom...
Definition: geometry.h:31
unsigned operator()(const Triangle &triangle, const unsigned ind) const
Definition: MeshIO.h:77
VertexIndices(const Mesh &mesh)
Definition: MeshIO.h:71
std::string fname
Definition: MeshIO.h:95
void open(const std::ios_base::openmode mode=std::ios_base::in)
Definition: MeshIO.h:38
std::map< std::string, MeshIO * > Registery
Definition: MeshIO.h:66
std::fstream fs
Definition: MeshIO.h:96
static Registery registery
Definition: MeshIO.h:91
virtual void load_points(Geometry &geom)=0
virtual bool binary() const
Definition: MeshIO.h:87
virtual void load_triangles(Mesh &m)=0
IndexMap indmap
Definition: MeshIO.h:97
virtual void save(const Mesh &mesh)
Definition: MeshIO.h:58
MeshIO(const std::string &filename, const char *name)
Definition: MeshIO.h:93
virtual void load(Mesh &m)
Definition: MeshIO.h:47
virtual MeshIO * clone(const std::string &filename) const =0
void reference_vertices(Mesh &mesh) const
Definition: MeshIO.h:89
virtual ~MeshIO()
Definition: MeshIO.h:29
virtual const char * name() const =0
virtual void save(const Mesh &mesh, std::ostream &os) const =0
static MeshIO * create(const std::string &filename)
Definition: MeshIO.h:31
VerticesRefs & vertices()
Definition: mesh.h:84
void update(const bool topology_changed)
Recompute triangles normals, area, and vertex triangles.
Geometry & geometry() const
Definition: mesh.h:87
Triangle Triangle class.
Definition: triangle.h:45
Vertex & vertex(const unsigned &vindex)
Definition: triangle.h:83
std::string tolower(const std::string &s)
Definition: filenames.h:26
std::map< unsigned, unsigned > IndexMap
Definition: triangle.h:141
std::string getFilenameExtension(const std::string &name)
Definition: filenames.h:18