Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/stlgeom/stltopology.hpp
3206 views
1
#ifndef FILE_STLTOPOLOGY
2
#define FILE_STLTOPOLOGY
3
4
/**************************************************************************/
5
/* File: stltopology.hpp */
6
/* Author: Joachim Schoeberl */
7
/* Author2: Johannes Gerstmayr */
8
/* Date: 26. Jul. 99 */
9
/**************************************************************************/
10
11
/*
12
The STLTopology contains topologic information as
13
triangle->point, point->triangles, triangle->edge, 2-points->edge,...
14
*/
15
16
17
class STLGeometry;
18
19
#define STLBASE 1
20
21
class STLPointIndex
22
{
23
int i;
24
public:
25
STLPointIndex () { ; }
26
STLPointIndex (int ai) : i(ai) { ; }
27
STLPointIndex & operator= (const STLPointIndex & ai) { i = ai.i; return *this; }
28
STLPointIndex & operator= (int ai) { i = ai; return *this; }
29
operator int () const { return i; }
30
STLPointIndex operator++ (int) { return i++; }
31
STLPointIndex operator-- (int) { return i--; }
32
};
33
34
35
36
class STLTrigIndex
37
{
38
int i;
39
public:
40
STLTrigIndex () { ; }
41
STLTrigIndex (int ai) : i(ai) { ; }
42
STLTrigIndex & operator= (const STLTrigIndex & ai) { i = ai.i; return *this; }
43
STLTrigIndex & operator= (int ai) { i = ai; return *this; }
44
operator int () const { return i; }
45
STLTrigIndex operator++ (int) { return i++; }
46
STLTrigIndex operator-- (int) { return i--; }
47
};
48
49
50
51
52
53
// triangle structure for loading stl files
54
class STLReadTriangle
55
{
56
Vec<3> normal;
57
Point<3> pts[3];
58
public:
59
STLReadTriangle (const Point<3> * apts, const Vec<3> & anormal);
60
STLReadTriangle () {};
61
const Point<3> & operator[] (int i) const { return pts[i]; }
62
const Vec<3> & Normal() const { return normal; }
63
};
64
65
66
67
class STLTriangle
68
{
69
// topology edges of triangle, edge[i] opposite to point[i]
70
int topedges[3];
71
// neighbour triangles, trig[i] opposite to point[i]
72
int nbtrigs[2][3];
73
// normalized stored normal vector ??
74
Vec<3> normal;
75
// point numbers of triangle
76
int pts[3];
77
// front-side and back-side domains
78
int domains[2];
79
80
81
public:
82
83
Box<3> box;
84
Point<3> center;
85
double rad;
86
int facenum;
87
88
struct
89
{
90
unsigned int toperror : 1;
91
} flags;
92
93
94
95
96
STLTriangle (const int * apts);
97
STLTriangle () {pts[0]=0;pts[1]=0;pts[2]=0;}
98
99
int operator[] (int i) const { return pts[i]; }
100
int & operator[] (int i) { return pts[i]; }
101
102
int EdgeNum(int i) const { return topedges[(i-1)]; }
103
int & EdgeNum(int i) { return topedges[(i-1)]; }
104
105
int NBTrig (bool side, int i) const { return nbtrigs[side][i]; }
106
int & NBTrig (bool side, int i) { return nbtrigs[side][i]; }
107
108
109
int Domain (bool side) const { return domains[side]; }
110
int & Domain (bool side) { return domains[side]; }
111
112
113
114
// obsolete:
115
int PNum(int i) const { return pts[(i-1)]; }
116
int & PNum(int i) { return pts[(i-1)]; }
117
int PNumMod(int i) const { return pts[(i-1)%3]; }
118
int & PNumMod(int i) { return pts[(i-1)%3]; }
119
120
int EdgeNumMod(int i) const { return topedges[(i-1)%3]; }
121
int & EdgeNumMod(int i) { return topedges[(i-1)%3]; }
122
123
int NBTrigNum(int i) const { return nbtrigs[0][(i-1)]; }
124
int & NBTrigNum(int i) { return nbtrigs[0][(i-1)]; }
125
int NBTrigNumMod(int i) const { return nbtrigs[0][(i-1)%3]; }
126
int & NBTrigNumMod(int i) { return nbtrigs[0][(i-1)%3]; }
127
128
129
// consistently oriented neighbour:
130
int IsNeighbourFrom(const STLTriangle& t) const;
131
// opposite to consistently oriented neighbour:
132
int IsWrongNeighbourFrom(const STLTriangle& t) const;
133
134
///Get the two points of neighbour-Triangles in orientation of this-Triangle
135
void GetNeighbourPoints(const STLTriangle& t, int& p1, int& p2) const;
136
int GetNeighbourPointsAndOpposite(const STLTriangle& t, int& p1, int& p2, int& po) const;
137
138
139
140
// NON-normalized geometry - normal vector
141
Vec<3> GeomNormal(const ARRAY<Point<3> >& ap) const;
142
143
// Stored normal vector, normalized
144
void SetNormal (const Vec<3> & n);
145
const Vec<3> & Normal () const { return normal; }
146
147
148
void ChangeOrientation();
149
150
//project with a certain normal vector in plane
151
void ProjectInPlain(const ARRAY<Point<3> >& ap,
152
const Vec<3> & n, Point<3> & pp) const;
153
//project with the triangle's normal vector in plane
154
void ProjectInPlain(const ARRAY<Point<3> > & ap, Point<3> & pp) const;
155
156
157
/*
158
Project the point pp along the nproj into the plane of
159
the triangle. The triangle normal is given by ntrig to
160
avoid numerical instabilities.
161
The local coordinates lam are defined by
162
163
pp(input) = P1 + lam1 v1 + lam2 v2 + lam3 n
164
165
the result is
166
167
pp(output) = P1 + lam1 v1 + lam2 v2
168
*/
169
int ProjectInPlain (const ARRAY<Point<3> >& ap,
170
const Vec<3> & nproj,
171
Point<3> & pp, Vec<3> & lam) const;
172
173
int PointInside(const ARRAY<Point<3> >& ap, const Point<3> & pp) const;
174
175
//get nearest point on triangle and distance to it
176
double GetNearestPoint(const ARRAY<Point<3> >& ap,
177
Point<3> & p3d) const;
178
179
double Area(const ARRAY<Point<3> >& ap) const;
180
181
double MinHeight(const ARRAY<Point<3> >& ap) const;
182
double MaxLength(const ARRAY<Point<3> >& ap) const;
183
//max length of a side of triangle
184
185
int GetFaceNum() {return facenum;}
186
void SetFaceNum(int i) {facenum = i;}
187
188
int HasEdge(int p1, int p2) const;
189
};
190
191
192
/**
193
Topology Edge:
194
Useful unside a face.
195
A edges sharing more than 2 faces: trigs are undefined
196
*/
197
class STLTopEdge
198
{
199
int pts[2];
200
int trigs[2];
201
double cosangle;
202
int status; // excluded, confirmed, candidate, undefined
203
public:
204
STLTopEdge ();
205
STLTopEdge (int p1, int p2, int trig1, int trig2);
206
207
int operator[] (int i) const { return pts[i]; }
208
int & operator[] (int i) { return pts[i]; }
209
210
211
int PNum(int i) const { return pts[(i-1)]; }
212
int & PNum(int i) { return pts[(i-1)]; }
213
int PNumMod(int i) const { return pts[(i-1)%2]; }
214
int & PNumMod(int i) { return pts[(i-1)%2]; }
215
216
int TrigNum(int i) const { return trigs[(i-1)]; }
217
int & TrigNum(int i) { return trigs[(i-1)]; }
218
int TrigNumMod(int i) const { return trigs[(i-1)%2]; }
219
int & TrigNumMod(int i) { return trigs[(i-1)%2]; }
220
221
void SetCosAngle (double ca) { cosangle = ca; }
222
double CosAngle () const { return cosangle; }
223
double Angle () const { return acos (cosangle); }
224
225
void SetStatus (int stat) { status = stat; }
226
int GetStatus () const { return status; }
227
};
228
229
230
231
ostream& operator<<(ostream& os, const STLTriangle& t);
232
233
234
235
236
237
238
239
class STLTopology
240
{
241
protected:
242
ARRAY<STLTriangle> trias;
243
ARRAY<STLTopEdge> topedges;
244
ARRAY<Point<3> > points;
245
246
// mapping of sorted pair of points to topedge
247
INDEX_2_HASHTABLE<int> * ht_topedges;
248
// mapping of node to trigs
249
TABLE<int> trigsperpoint;
250
// mapping of node to edges
251
TABLE<int> topedgesperpoint;
252
253
// searchtree for trigs and points
254
255
Box3dTree * searchtree; // ADT
256
Point3dTree * pointtree;
257
258
Box<3> boundingbox;
259
double pointtol;
260
261
public:
262
enum STL_GEOM_STATUS { STL_GOOD, STL_WARNING, STL_ERROR };
263
264
protected:
265
STL_GEOM_STATUS status;
266
string statustext;
267
268
bool topology_ok;
269
bool orientation_ok;
270
271
public:
272
STLTopology();
273
virtual ~STLTopology();
274
275
static STLGeometry * LoadNaomi (istream & ist);
276
static STLGeometry * Load (istream & ist);
277
static STLGeometry * LoadBinary (istream & ist);
278
279
void Save (const char* filename);
280
void SaveBinary (const char* filename, const char* aname);
281
void SaveSTLE (const char * filename); // stores trigs and edges
282
283
virtual void InitSTLGeometry (const ARRAY<STLReadTriangle> & readtrigs);
284
285
virtual void TopologyChanged() {}; //do some things, if topology changed!
286
287
/// Generate topology tables
288
void FindNeighbourTrigs();
289
290
291
void GetTrianglesInBox (const Box<3> & box,
292
ARRAY<int> & trias) const;
293
294
295
int GetNP() const { return points.Size(); }
296
int AddPoint(const Point<3> & p) { return points.Append(p); }
297
const Point<3> & GetPoint(int nr) const { return points.Get(nr); }
298
int GetPointNum (const Point<3> & p);
299
void SetPoint(int nr, const Point<3> & p) { points.Elem(nr) = p; }
300
const ARRAY<Point<3> >& GetPoints() const { return points; }
301
302
const Point<3> & operator[] (STLPointIndex i) const { return points[i]; }
303
Point<3> & operator[] (STLPointIndex i) { return points[i]; }
304
305
306
307
308
int GetNT() const { return trias.Size(); }
309
void AddTriangle(const STLTriangle& t);
310
const STLTriangle & GetTriangle (int nr) const { return trias.Get(nr); }
311
STLTriangle & GetTriangle (int nr) { return trias.Elem(nr); }
312
313
const STLTriangle & operator[] (STLTrigIndex i) const { return trias[i]; }
314
STLTriangle & operator[] (STLTrigIndex i) { return trias[i]; }
315
316
317
int GetNTE() const { return topedges.Size(); }
318
const STLTopEdge & GetTopEdge (int nr) const { return topedges.Get(nr); }
319
STLTopEdge & GetTopEdge (int nr) { return topedges.Elem(nr); }
320
int GetTopEdgeNum (int pi1, int pi2) const;
321
322
323
int NOTrigsPerPoint(int pn) { return trigsperpoint.EntrySize(pn); }
324
int TrigPerPoint(int pn, int i) { return trigsperpoint.Get(pn, i); }
325
326
327
int NTopEdgesPerPoint (int pn) const { return topedgesperpoint.EntrySize(pn); }
328
int TopEdgePerPoint (int pn, int ei) const { return topedgesperpoint.Get(pn, ei); }
329
330
331
bool Topology_Ok() const { return topology_ok; }
332
bool Orientation_Ok() const { return orientation_ok; }
333
334
STL_GEOM_STATUS GetStatus () const { return status; }
335
const string & GetStatusText () const { return statustext; }
336
337
void InvertTrig (int trig);
338
void DeleteTrig (int trig);
339
void OrientAfterTrig (int trig);
340
341
342
// Table will be constructed, if topology is not ok
343
/// neighbourtrigs for surfacetrigs
344
TABLE<int> neighbourtrigs;
345
346
/// get nr-th neighbour Triangle for triangle trig
347
int NONeighbourTrigs(int trig) const { return neighbourtrigs.EntrySize(trig); }
348
int NeighbourTrig(int trig, int nr) const { return neighbourtrigs.Get(trig,nr); }
349
int NeighbourTrigSorted(int trig, int nr) const;
350
void AddNeighbourTrig(int i, int nt) { neighbourtrigs.Add1(i, nt); }
351
352
353
354
355
int GetLeftTrig (int p1, int p2) const;
356
int GetRightTrig (int p1, int p2) const;
357
358
const Box<3> & GetBoundingBox () const { return boundingbox; }
359
};
360
361
362
#endif
363
364