libgenua
Basic Geometry, Numerical Algorithms and Interfaces
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
triface.h
1 
2 /* Copyright (C) 2015 David Eller <david@larosterna.com>
3  *
4  * Commercial License Usage
5  * Licensees holding valid commercial licenses may use this file in accordance
6  * with the terms contained in their respective non-exclusive license agreement.
7  * For further information contact david@larosterna.com .
8  *
9  * GNU General Public License Usage
10  * Alternatively, this file may be used under the terms of the GNU General
11  * Public License version 3.0 as published by the Free Software Foundation and
12  * appearing in the file gpl.txt included in the packaging of this file.
13  */
14 
15 #ifndef GENUA_TRIFACE_H
16 #define GENUA_TRIFACE_H
17 
18 #include "smatrix.h"
19 #include "dvector.h"
20 #include "point.h"
21 #include "triedge.h"
22 
23 class TriMesh;
24 class Plane;
25 
32 class TriFace
33 {
34  public:
35 
37  TriFace() : ftag(0), msh(0) {}
38 
40  TriFace(const TriMesh *m, uint a, uint b, uint c)
41  : ftag(0), msh(m) {order(a,b,c);}
42 
44  void assign(const TriMesh *m, uint a, uint b, uint c) {
45  order(a, b, c);
46  msh = m;
47  }
48 
50  bool isValid() const {
51  if (v[0] == v[1] or v[0] == v[2] or v[1] == v[2])
52  return false;
53  else
54  return true;
55  }
56 
58  bool inRange() const;
59 
61  void invalidate() { v[0]=v[1]=v[2] = NotFound; }
62 
64  const TriMesh *mesh() const {return msh;}
65 
67  const uint *vertices() const {return v;}
68 
70  uint *vertices() {return v;}
71 
73  void getVertices(uint *vi) const {
74  vi[0] = v[0];
75  vi[1] = v[1];
76  vi[2] = v[2];
77  }
78 
80  uint opposed(const TriEdge & e) const {
81  uint s = e.source();
82  uint t = e.target();
83  for (uint k=0; k<3; ++k) {
84  uint vk = v[k];
85  if (vk != s and vk != t)
86  return vk;
87  }
88  return NotFound;
89  }
90 
92  void bind(const TriMesh *m, uint off = 0) {
93  msh = m;
94  v[0] += off;
95  v[1] += off;
96  v[2] += off;
97  }
98 
100  void reverse() {std::swap(v[1], v[2]);}
101 
103  void itranslate(const Indices & repl) {
104  order(repl[v[0]], repl[v[1]], repl[v[2]]);
105  }
106 
108  uint replace(uint iold, uint inew) {
109  for (uint k=0; k<3; ++k) {
110  if (v[k] == iold) {
111  v[k] = inew;
112  order(v[0], v[1], v[2]);
113  return k;
114  }
115  }
116  return NotFound;
117  }
118 
120  Vct3 eval(Real up, Real vp) const;
121 
123  Vct3 center() const;
124 
126  Vct3 normal() const;
127 
129  Real area() const;
130 
132  Real normal(Vct3 & nrm) const;
133 
135  Real corner(uint i) const;
136 
138  Real solidAngle(uint idx) const;
139 
141  void edgeLengths(Vct3 & elen) const;
142 
144  Vct3 project(const Vct3 & pt) const;
145 
149  Vct3 pierce(const Vct3 & a, const Vct3 & b) const;
150 
152  Real minDistance(const Vct3 & pt, Vct2 & foot) const;
153 
155  bool intersect(const Plane & pln, Vct3 & src, Vct3 & trg) const;
156 
159  void gradient(Mtx33 & gm) const;
160 
162  Vct3 gradient(const Vector & x) const;
163 
165  CpxVct3 gradient(const CpxVector & x) const;
166 
168  void xIntegrate(const Vector & p, const Vct3 & ref, Vct3 & pn, Vct3 & rxpn) const;
169 
171  Real dotIntegrate(const Vector & p, const PointList<3> & z) const;
172 
174  bool operator< (const TriFace & a) const {
175  if (v[0] < a.v[0])
176  return true;
177  else if (v[0] > a.v[0])
178  return false;
179  else if (v[1] < a.v[1])
180  return true;
181  else if (v[1] > a.v[1])
182  return false;
183  else
184  return (v[2] < a.v[2]);
185  }
186 
188  bool operator== (const TriFace & a) const {
189  if (v[0] != a.v[0])
190  return false;
191  else if (v[1] != a.v[1])
192  return false;
193  else if (v[2] != a.v[2])
194  return false;
195  else
196  return true;
197  }
198 
200  bool equivalent(const TriFace & a) const {
201  if (v[0] != a.v[0])
202  return false;
203  else if (v[1] == a.v[1] and v[2] == a.v[2])
204  return true;
205  else if (v[2] == a.v[1] and v[1] == a.v[2])
206  return true;
207  else
208  return false;
209  }
210 
212  bool operator!= (const TriFace & a) const {
213  return !(*this == a);
214  }
215 
217  int tag() const {return ftag;}
218 
220  void tag(int t) {ftag = t;}
221 
223  uint64_t hash() const {
224  uint64_t a = uint64_t(v[0]);
225  uint64_t b = uint64_t(v[1]);
226  uint64_t c = uint64_t(v[2]);
227  uint64_t d = uint64_t(ptrdiff_t(msh));
228  return jenkins_hash(a, b, c, d);
229  }
230 
231  protected:
232 
234  void order(uint a, uint b, uint c) {
235  if (a < b and a < c) {
236  v[0] = a;
237  v[1] = b;
238  v[2] = c;
239  } else if (b < a and b < c) {
240  v[0] = b;
241  v[1] = c;
242  v[2] = a;
243  } else {
244  v[0] = c;
245  v[1] = a;
246  v[2] = b;
247  }
248  }
249 
250  protected:
251 
253  uint v[3];
254 
256  int ftag;
257 
259  const TriMesh *msh;
260 };
261 
262 struct global_face_less
263 {
264  bool operator()(const TriFace & a, const TriFace & b) const
265  {
266  const TriMesh *am(a.mesh());
267  const TriMesh *bm(b.mesh());
268  if (am == bm)
269  return a < b;
270  else
271  return am < bm;
272  }
273 };
274 
275 struct global_face_equal
276 {
277  bool operator()(const TriFace & a, const TriFace & b) const
278  {
279  const TriMesh *am(a.mesh());
280  const TriMesh *bm(b.mesh());
281  if (am != bm)
282  return false;
283  else
284  return a == b;
285  }
286 };
287 
288 struct face_hash
289 {
290  size_t operator() (const TriFace & a) const {return a.hash();}
291 };
292 
293 #endif
294 
bool inRange() const
check if vertex indices are in range
Definition: triface.cpp:25
uint * vertices()
access vertices
Definition: triface.h:70
void xIntegrate(const Vector &p, const Vct3 &ref, Vct3 &pn, Vct3 &rxpn) const
surface integration, add int(p*n dA) and int(r x pn dA) to sums
Definition: triface.cpp:369
void tag(int t)
change tag value
Definition: triface.h:220
Vct3 project(const Vct3 &pt) const
project, return parameters and signed distance to projection
Definition: triface.cpp:144
uint v[3]
vertex indices
Definition: triface.h:253
Real corner(uint i) const
compute the internal angle at vertex i
Definition: triface.cpp:81
Vct3 eval(Real up, Real vp) const
compute point on triangle
Definition: triface.cpp:33
const uint * vertices() const
access vertices
Definition: triface.h:67
Real area() const
compute area
Definition: triface.cpp:62
void invalidate()
make triangle invalid, to force elimination by TriMesh::fixate()
Definition: triface.h:61
bool operator<(const TriFace &a) const
sorting criterion
Definition: triface.h:174
TriFace()
construct unconnected face
Definition: triface.h:37
Specialized triangular surface mesh.
Definition: trimesh.h:52
int tag() const
access tag value
Definition: triface.h:217
uint replace(uint iold, uint inew)
replace a single vertex index, fix ordering
Definition: triface.h:108
Triangular face of a TriMesh.
Definition: triface.h:32
void itranslate(const Indices &repl)
translate vertex indices
Definition: triface.h:103
uint64_t hash() const
compute a hash value
Definition: triface.h:223
bool isValid() const
check if all three vertices are distinct
Definition: triface.h:50
Vct3 center() const
compute triangle center
Definition: triface.cpp:44
bool intersect(const Plane &pln, Vct3 &src, Vct3 &trg) const
Determine intersection segment with plane pln.
Definition: triface.cpp:256
Vct3 pierce(const Vct3 &a, const Vct3 &b) const
Find the point where a line (a-b) would pierce this face, return the projection parameter (u...
Definition: triface.cpp:169
Fixed size matrix.
Definition: forward.h:412
void bind(const TriMesh *m, uint off=0)
rebind to different mesh and offset vertex indices
Definition: triface.h:92
void order(uint a, uint b, uint c)
set vertices in correct order
Definition: triface.h:234
A plane in three dimensions.
Definition: plane.h:42
Real solidAngle(uint idx) const
compute solid angle with respect to vertex idx
Definition: triface.cpp:98
Vct3 normal() const
compute normal vector (not normalized)
Definition: triface.cpp:53
void gradient(Mtx33 &gm) const
Gradient.
Definition: triface.cpp:309
bool equivalent(const TriFace &a) const
equivalent, but possibly flipped
Definition: triface.h:200
Real minDistance(const Vct3 &pt, Vct2 &foot) const
return minimum signed distance of pt to this triangle, set foot point parameter
Definition: triface.cpp:187
uint opposed(const TriEdge &e) const
find vertex opposed to edge ei
Definition: triface.h:80
TriFace(const TriMesh *m, uint a, uint b, uint c)
construct connected face
Definition: triface.h:40
bool operator==(const TriFace &a) const
equivalence
Definition: triface.h:188
Real dotIntegrate(const Vector &p, const PointList< 3 > &z) const
surface integration, return int( dot(pn,z) dA )
Definition: triface.cpp:404
void reverse()
flip normal direction
Definition: triface.h:100
bool operator!=(const TriFace &a) const
difference
Definition: triface.h:212
void getVertices(uint *vi) const
copy vertex indices
Definition: triface.h:73
void edgeLengths(Vct3 &elen) const
compute the length of all three edges
Definition: triface.cpp:433
const TriMesh * mesh() const
access mesh
Definition: triface.h:64
int ftag
marker tag
Definition: triface.h:256
const TriMesh * msh
connected mesh
Definition: triface.h:259
void assign(const TriMesh *m, uint a, uint b, uint c)
set mesh and vertices
Definition: triface.h:44