OpenMEEG
Loading...
Searching...
No Matches
mesh.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 <iostream>
11
12#include <vector>
13#include <map>
14#include <string>
15#include <memory>
16
17#include <om_common.h>
18#include <triangle.h>
19#include <om_utils.h>
20
21#include <symmatrix.h>
22#include <block_matrix.h>
23
24namespace OpenMEEG {
25
26 class Geometry;
27 using maths::Range;
28 using maths::Ranges;
29
30 // Mesh class
31 // \brief Mesh is a collection of triangles associated to a geometry containing the points
32 // on which triangles are based.
33
34 class OPENMEEG_EXPORT Mesh {
35
36 static Geometry* create_geometry(Geometry* geom);
37
38 public:
39
40 friend class Geometry;
41 friend class MeshIO;
42
43 typedef std::map<const Vertex*,TrianglesRefs> VertexTriangles;
44
47
48 Mesh(Geometry* geometry=nullptr): geom(create_geometry(geometry)) { }
49
54 // Do we need this ? TODO
55
56 Mesh(const unsigned nv,const unsigned nt,Geometry* geometry=nullptr);
57
58 Mesh(const Mesh&) = default;
59 Mesh(Mesh&& m) = default;
60 // MSVC 2019 https://stackoverflow.com/questions/31264984/c-compiler-error-c2280-attempting-to-reference-a-deleted-function-in-visual
61 Mesh& operator=(const Mesh&) = default;
62
67
68 Mesh(const std::string& filename,const bool verbose,Geometry* geometry=nullptr): Mesh(geometry) {
69 load(filename,verbose);
70 }
71
74
75 Mesh(const std::string& filename,Geometry* geometry=nullptr): Mesh(filename,false,geometry) { }
76
78
79 ~Mesh() { clear(); }
80
81 std::string& name() { return mesh_name; }
82 const std::string& name() const { return mesh_name; }
83
84 VerticesRefs& vertices() { return mesh_vertices; }
85 const VerticesRefs& vertices() const { return mesh_vertices; }
86
87 Geometry& geometry() const { return *geom; }
88
89 Triangles& triangles() { return mesh_triangles; }
90 const Triangles& triangles() const { return mesh_triangles; }
91
93
94 bool current_barrier() const { return current_barrier_; }
95 bool& current_barrier() { return current_barrier_; }
96 bool isolated() const { return isolated_; }
97 bool& isolated() { return isolated_; }
98
100
102
103 Triangle& add_triangle(const TriangleIndices inds,const IndexMap& indmap) {
104 const TriangleIndices t = { indmap.at(inds[0]), indmap.at(inds[1]), indmap.at(inds[2])};
105 return add_triangle(t);
106 }
107
108 void add(const std::vector<TriangleIndices>& trgs) {
109 for (const auto& triangle : trgs)
110 add_triangle(triangle);
111 }
112
113 void add(const std::vector<TriangleIndices>& trgs,const IndexMap& indmap) {
114 for (const auto& triangle : trgs)
115 add_triangle(triangle,indmap);
116 }
117
118 bool operator==(const Mesh& m) const { return triangles()==m.triangles(); }
119 bool operator!=(const Mesh& m) const { return triangles()!=m.triangles(); }
120
124
125 void info(const bool verbose=false) const;
127 bool intersection(const Mesh&) const;
130 void update(const bool topology_changed);
131 void merge(const Mesh&,const Mesh&);
132
133 #ifdef DEBUG
134 void check_consistency(const std::string&) const;
135 #endif
136
139
141 std::vector<size_t> indices;
142 for (const auto& vertex : vertices())
143 indices.push_back(vertex->index());
144 std::sort(indices.begin(),indices.end());
145 Ranges result;
146 for (auto it=indices.begin(); it!=indices.end();) {
147 auto it1 = it;
148 for (auto it2=it1+1; it2!=indices.end() && *it2==*it1+1; it1=it2++);
149 result.push_back(Range(*it,*it1));
150 it = it1+1;
151 }
152 return result;
153 }
154
155 // Triangles always have a contiguous range as they are never shared between meshes.
156
157 Range triangles_range() const { return Range(triangles().front().index(),triangles().back().index()); }
158
160
161 TrianglesRefs triangles(const Vertex& V) const { return vertex_triangles.at(&V); }
162
164
166 std::map<Triangle*,unsigned> mapt;
167 TrianglesRefs result;
168 for (auto& vertex : triangle)
169 for (const auto& t2 : triangles(*vertex))
170 if (++mapt[t2]==2)
171 result.push_back(t2);
172 return result;
173 }
174
176
178 for (auto& triangle : triangles())
179 triangle.change_orientation();
180 }
181
184 double solid_angle(const Vect3& p) const;
185 Normal normal(const Vertex& v) const;
186 void laplacian(SymMatrix &A) const;
187
188 bool& outermost() { return outermost_; }
189 bool outermost() const { return outermost_; }
190
195
196 void smooth(const double& smoothing_intensity, const unsigned& niter);
197
199
200 void gradient_norm2(SymMatrix &A) const;
201
208
209 void load(const std::string& filename,const bool verbose=true);
210
213
214 void save(const std::string& filename) const ;
215
216 #if !defined(SWIGPYTHON) && !defined(_MSC_VER)
217 private:
218 #endif
219
220 // This private method must be accessible from swig.
221
222 void reference_vertices(const IndexMap& indmap);
223
224 private:
225
227
228 typedef std::map<std::pair<const Vertex*,const Vertex*>,int> EdgeMap;
229
230 void clear();
231
234
235 void add_mesh(const Mesh& m);
236
237 // regarding mesh orientation
238
239 const EdgeMap compute_edge_map() const;
240 bool triangle_intersection(const Triangle&,const Triangle&) const;
241
243
244 Vect3 P1gradient(const Vect3& p0,const Vect3& p1,const Vect3& p2) const { return crossprod(p1,p2)/det(p0,p1,p2); }
245
247
248 double P0gradient_norm2(const Triangle& t1,const Triangle& t2) const {
249 return sqr(dotprod(t1.normal(),t2.normal()))/(t1.center()-t2.center()).norm2();
250 }
251
252 // Create the map that for each vertex gives the triangles containing it.
253
254 void make_adjacencies() {
255 vertex_triangles.clear();
256 for (auto& triangle : triangles())
257 for (const auto& vertex : triangle)
258 vertex_triangles[vertex].push_back(&triangle);
259 }
260
261 typedef std::shared_ptr<Geometry> Geom;
262
263 std::string mesh_name = "";
264 VertexTriangles vertex_triangles;
265 Geometry* geom;
266 VerticesRefs mesh_vertices;
267 Triangles mesh_triangles;
268 bool outermost_ = false;
269
271
272 bool current_barrier_ = false;
273 bool isolated_ = false;
274 };
275
276 typedef std::vector<Mesh> Meshes;
277}
Geometry contains the electrophysiological model Vertices, meshes and domains are stored in this geom...
Definition geometry.h:31
Mesh & operator=(const Mesh &)=default
bool & current_barrier()
Definition mesh.h:95
Triangle & add_triangle(const TriangleIndices inds, const IndexMap &indmap)
Definition mesh.h:103
bool & isolated()
Definition mesh.h:97
void laplacian(SymMatrix &A) const
Compute mesh laplacian.
bool operator!=(const Mesh &m) const
Definition mesh.h:119
TriangleIndices triangle(const Triangle &t) const
bool outermost() const
Returns True if it is an outermost mesh.
Definition mesh.h:189
std::string & name()
Definition mesh.h:81
bool has_correct_orientation() const
Check local orientation of mesh triangles.
void correct_local_orientation()
Correct the local orientation of the mesh triangles.
Mesh(const unsigned nv, const unsigned nt, Geometry *geometry=nullptr)
Constructor from scratch (vertices/triangles t be added)
bool & outermost()
Definition mesh.h:188
const Triangles & triangles() const
Definition mesh.h:90
Mesh(Mesh &&m)=default
bool intersection(const Mesh &) const
Check whether the mesh intersects another mesh.
void smooth(const double &smoothing_intensity, const unsigned &niter)
Smooth Mesh.
Normal normal(const Vertex &v) const
Get normal at vertex.`.
Geometry & geometry() const
Definition mesh.h:87
void correct_global_orientation()
Correct the global orientation (if there is one).
void merge(const Mesh &, const Mesh &)
Merge two meshes.
bool isolated() const
Definition mesh.h:96
void add(const std::vector< TriangleIndices > &trgs, const IndexMap &indmap)
Definition mesh.h:113
Mesh(const std::string &filename, Geometry *geometry=nullptr)
Definition mesh.h:75
const VerticesRefs & vertices() const
Definition mesh.h:85
void add(const std::vector< TriangleIndices > &trgs)
Definition mesh.h:108
const std::string & name() const
Definition mesh.h:82
std::map< const Vertex *, TrianglesRefs > VertexTriangles
Definition mesh.h:43
~Mesh()
Destructor.
Definition mesh.h:79
bool has_self_intersection() const
Check whether the mesh self-intersects.
void generate_indices()
Generate indices (if allocate).
TrianglesRefs triangles(const Vertex &V) const
Get the triangles adjacent to vertex.
Definition mesh.h:161
void info(const bool verbose=false) const
Print info Print to std::cout some info about the mesh.
bool operator==(const Mesh &m) const
Definition mesh.h:118
void save(const std::string &filename) const
Save mesh to file.
Mesh(const std::string &filename, const bool verbose, Geometry *geometry=nullptr)
Constructors.
Definition mesh.h:68
void change_orientation()
Change mesh orientation.
Definition mesh.h:177
void update(const bool topology_changed)
Recompute triangles normals, area, and vertex triangles.
Mesh(Geometry *geometry=nullptr)
Default constructor or constructor using a provided geometry.
Definition mesh.h:48
bool current_barrier() const
Definition mesh.h:94
Triangle & add_triangle(const TriangleIndices inds)
Add a triangle specified by its indices in the geometry.
TrianglesRefs adjacent_triangles(const Triangle &triangle) const
Get the triangles adjacent to.
Definition mesh.h:165
VerticesRefs & vertices()
Definition mesh.h:84
void gradient_norm2(SymMatrix &A) const
Compute the square norm of the surfacic gradient.
Ranges vertices_ranges() const
Get the ranges of the specific mesh in the global matrix.
Definition mesh.h:140
Mesh(const Mesh &)=default
Triangles & triangles()
Definition mesh.h:89
double solid_angle(const Vect3 &p) const
Given a point p, computes the solid angle of the mesh seen from.
void load(const std::string &filename, const bool verbose=true)
Read mesh from file.
Range triangles_range() const
Definition mesh.h:157
Triangle Triangle class.
Definition triangle.h:45
Vect3 center() const
Definition triangle.h:104
Normal & normal()
Definition triangle.h:95
Vect3.
Definition vect3.h:28
Vertex.
Definition vertex.h:20
std::vector< Triangle > Triangles
Definition triangle.h:145
Vect3 crossprod(const Vect3 &V1, const Vect3 &V2)
Definition vect3.h:107
std::vector< Mesh > Meshes
Definition mesh.h:276
std::vector< Triangle * > TrianglesRefs
Definition triangle.h:146
double det(const Vect3 &V1, const Vect3 &V2, const Vect3 &V3)
Definition vect3.h:108
std::vector< Vertex * > VerticesRefs
Definition vertex.h:38
std::map< unsigned, unsigned > IndexMap
Definition triangle.h:148
double sqr(const double x)
Definition vect3.h:24
double dotprod(const Vect3 &V1, const Vect3 &V2)
Definition vect3.h:106