libatomprobe
Library for Atom Probe Tomography (APT) computation
mesh.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 D Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #ifndef ATOMPROBE_MESH_H
18 #define ATOMPROBE_MESH_H
19 
23 
24 #include <vector>
25 #include <fstream>
26 #include <list>
27 
28 namespace AtomProbe
29 {
30 //GMSH load constants
31 //-----------
32 //Constants are defined in the GMSH documentation
33 //under section MSH-ASCII-file-format
34 //http://geuz.org/gmsh/doc/texinfo/gmsh.html#MSH-ASCII-file-format
35 const unsigned int ELEM_SINGLE_NODE_POINT=15;
36 const unsigned int ELEM_TWO_NODE_LINE=1;
37 const unsigned int ELEM_THREE_NODE_TRIANGLE=2;
38 const unsigned int ELEM_FOUR_NODE_TETRAHEDRON=4;
39 //-----------
40 
41 
42 extern const char *MESH_LOAD_ERRS[];
43 
44 enum
45 {
49 };
50 
51 class TETRAHEDRON{
52  public:
53  unsigned int p[4];
54  unsigned int physGroup;
55 };
56 
57 class TRIANGLE{
58  public:
59  unsigned int p[3];
60  unsigned int physGroup;
61 
62  //These functions exist due to swig+python
63  // if there is another way to do this without access functions
64  // then should use that
65  inline unsigned int getP(unsigned int offset) { return p[offset];};
66  inline void setP(unsigned int offset, unsigned int v) { p[offset]=v;};
67  bool isSane(size_t pLimit=(size_t)-1) const;
68  bool edgesMismatch(const TRIANGLE &tOther) const;
69 };
70 
71 class LINE{
72  public:
73  unsigned int p[2];
74  unsigned int physGroup;
75 };
76 
78 class Mesh
79 {
80  private:
81 
83  bool sameTriangle(unsigned int ui, unsigned int uj) const;
84 
86  static bool sameTriangle(const TRIANGLE &t1, const TRIANGLE &t2);
87 
89  bool trianglesCoincident(unsigned int ui, const std::vector<size_t> &v) const;
90 
92  //using vertex sharing rules
93  //i.e. this will retrieve all vector of all triangles who share edge incidence
94  // for a specified vertex, eg: <|> vertex only
95  //incident triangles eg : |>*<| , will be excluded.
96  //The returned map can be used to perform triangle -> surrounding triangle lookups for shared edge
97  // incidence
98  void getTriEdgeAdjacencyMap(std::vector<std::list<size_t> > &map) const;
99 
101  float normalAngle(size_t triOne,size_t triTwo,bool flip=false ) const;
102 
104  void flipTriNormalCoherently(size_t triOne, size_t triTwo);
105 
107  void killOrphanNodes(const std::vector<size_t> &orphans) ;
108  public:
110  std::vector<Point3D> nodes;
111 
113  std::vector<std::string> physGroupNames;
114 
115 
116  //==== Element Storage ====
118  std::vector<TETRAHEDRON> tetrahedra;
120  //triangles.size() %3 should always == 0.
121  std::vector<TRIANGLE> triangles;
123  std::vector<LINE> lines;
125  std::vector<unsigned long long> points;
126 
127  //Python helper functions
128  // not recommended for C++ use, due to speed
129  //----
131  void setNode(unsigned int index, Point3D node);
133  void resizeNodes(unsigned int newSize);
135  Point3D getNodes(unsigned int index);
136  //----
137 
138 
139 
140  //Returns 0 on OK, nonzero on error.
141  unsigned int loadGmshMesh(const char *meshfile, unsigned int &curLine, bool allowBadMeshes=true);
142  unsigned int saveGmshMesh(const char *meshfile) const;
143 
144  //Return sum of all element sizes (total lines, points, triangles, tetrahedras, etc)
145  size_t elementCount() const;
146 
147  //Set the triangle mesh from the following pt triplet, each vector being the same size.
148  // Function will clear any existing data
149  void setTriangleMesh(const std::vector<float> &ptsA,
150  const std::vector<float> &ptsB, const std::vector<float> &ptsC);
151 
153  void reassignGroups(unsigned int i);
154 
156  void removeDuplicateTris();
157 
158  //Merge vertices that lie within tolerance distance of one another into a single vertex.
159  // note that this can produced degenerate objects in the mesh, if tolerance is large compared to the smallest element in the mesh
160  void mergeDuplicateVertices(float tolerance);
161 
163  //Returning true is not a guarantee of anything however.
164  bool isSane(bool output=false,std::ostream &outStream=std::cerr) const;
165 
167  void getBounds(BoundCube &b) const;
168 
170  unsigned int countTriNodes() const;
171 
173  void translate();
175  void translate(const Point3D &origin);
176 
178  void scale(const Point3D &origin,float scaleFactor);
179 
181  void scale(float scaleFactor);
182 
184  void rotate(const Point3D &axis, const Point3D &origin, float angle);
185 
187  // triangles must be correctly oriented, and closed
188  float getVolume() const;
189 
191  void clear();
192 
194  void getContainedNodes(const BoundCube &b,
195  std::vector<size_t> &nodes) const;
196 
198  void getIntersectingPrimitives( std::vector<size_t> &searchNodes,
199  std::vector<size_t> &lines,
200  std::vector<size_t> &triangles,
201  std::vector<size_t> &tetrahedra ) const;
202 
203  unsigned int divideMeshSurface(float divisionAngle, unsigned int newPhysGroupStart,
204  const std::vector<size_t> &physGroupsToSplit) ;
205 
206  void getCurPhysGroups(std::vector<std::pair<unsigned int,size_t> > &curPhys) const;
207 
208  void erasePhysGroup(unsigned int group, unsigned int typeMask);
209 
210 
212  unsigned int numDupVertices(float tolerance) const;
213 
214 
216  unsigned int numDupTris() const;
217 
219  void print(std::ostream &o) const;
220 
222  bool isOrientedCoherently() const;
223 
225  void killOrphanNodes();
226 
228  void pointsInside(const std::vector<Point3D> &p,
229  std::vector<bool> &meshResults, unsigned int &prog) const ;
231  void pointsInside(const std::vector<Point3D> &p,
232  std::vector<bool> &meshResults) const;
233 
235 
237  size_t getNearestTri(const Point3D &p,float &distance) const;
238 
239  void getTriNormal(size_t tri, Point3D &normal) const;
240 };
241 
242 #ifdef DEBUG
243 //Run unit tests for mesh
244 bool meshTests();
245 #endif
246 }
247 
248 #endif
std::vector< std::string > physGroupNames
Physical group storage.
Definition: mesh.h:113
unsigned int physGroup
Definition: mesh.h:54
std::vector< TETRAHEDRON > tetrahedra
Storage for node connectivity in tetrahedral form.
Definition: mesh.h:118
const char * MESH_LOAD_ERRS[]
Definition: mesh.cpp:53
unsigned int getP(unsigned int offset)
Definition: mesh.h:65
std::vector< LINE > lines
Storage for line segments. .size()%2 should always==0.
Definition: mesh.h:123
Simple mesh class for storing and manipulating meshes consisting of 2 and 3D simplexes (triangles or ...
Definition: mesh.h:78
const unsigned int ELEM_FOUR_NODE_TETRAHEDRON
Definition: mesh.h:38
const unsigned int ELEM_SINGLE_NODE_POINT
Definition: mesh.h:35
const unsigned int ELEM_THREE_NODE_TRIANGLE
Definition: mesh.h:37
unsigned int physGroup
Definition: mesh.h:74
A 3D point data class storage.
Definition: point3D.h:39
unsigned int p[4]
Definition: mesh.h:53
std::vector< Point3D > nodes
Point storage for 3D Data (nodes/coords/vertices..)
Definition: mesh.h:110
const unsigned int ELEM_TWO_NODE_LINE
Definition: mesh.h:36
Helper class to define a bounding cube.
Definition: boundcube.h:29
unsigned int physGroup
Definition: mesh.h:60
void setP(unsigned int offset, unsigned int v)
Definition: mesh.h:66
std::vector< unsigned long long > points
points
Definition: mesh.h:125
std::vector< TRIANGLE > triangles
Storage for node connectivity in triangle form (take in groups of 3)
Definition: mesh.h:121