1 | /*
|
---|
2 | * tesselation.hpp
|
---|
3 | *
|
---|
4 | * The tesselation class is meant to contain the envelope (concave, convex or neither) of a set of Vectors. As we actually mean this stuff for atoms, we have to encapsulate it all a bit.
|
---|
5 | *
|
---|
6 | * Created on: Aug 3, 2009
|
---|
7 | * Author: heber
|
---|
8 | */
|
---|
9 |
|
---|
10 | #ifndef TESSELATION_HPP_
|
---|
11 | #define TESSELATION_HPP_
|
---|
12 |
|
---|
13 | using namespace std;
|
---|
14 |
|
---|
15 | /*********************************************** includes ***********************************/
|
---|
16 |
|
---|
17 | // include config.h
|
---|
18 | #ifdef HAVE_CONFIG_H
|
---|
19 | #include <config.h>
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | #include <map>
|
---|
23 | #include <list>
|
---|
24 | #include <set>
|
---|
25 | #include <stack>
|
---|
26 |
|
---|
27 | #include "atom_particleinfo.hpp"
|
---|
28 | #include "helpers.hpp"
|
---|
29 | #include "vector.hpp"
|
---|
30 |
|
---|
31 | /****************************************** forward declarations *****************************/
|
---|
32 |
|
---|
33 | class BoundaryPointSet;
|
---|
34 | class BoundaryLineSet;
|
---|
35 | class BoundaryTriangleSet;
|
---|
36 | class LinkedCell;
|
---|
37 | class TesselPoint;
|
---|
38 | class PointCloud;
|
---|
39 | class Tesselation;
|
---|
40 | class Plane;
|
---|
41 |
|
---|
42 | /********************************************** definitions *********************************/
|
---|
43 |
|
---|
44 | #define DoTecplotOutput 1
|
---|
45 | #define DoRaster3DOutput 1
|
---|
46 | #define DoVRMLOutput 0
|
---|
47 | #define TecplotSuffix ".dat"
|
---|
48 | #define Raster3DSuffix ".r3d"
|
---|
49 | #define VRMLSUffix ".wrl"
|
---|
50 |
|
---|
51 | #define ParallelEpsilon 1e-3
|
---|
52 |
|
---|
53 | // ======================================================= some template functions =========================================
|
---|
54 |
|
---|
55 | #define IndexToIndex map <int, int>
|
---|
56 |
|
---|
57 | #define PointMap map < int, class BoundaryPointSet * >
|
---|
58 | #define PointSet set < class BoundaryPointSet * >
|
---|
59 | #define PointList list < class BoundaryPointSet * >
|
---|
60 | #define PointPair pair < int, class BoundaryPointSet * >
|
---|
61 | #define PointTestPair pair < PointMap::iterator, bool >
|
---|
62 |
|
---|
63 | #define CandidateList list <class CandidateForTesselation *>
|
---|
64 | #define CandidateMap map <class BoundaryLineSet *, class CandidateForTesselation *>
|
---|
65 |
|
---|
66 | #define LineMap multimap < int, class BoundaryLineSet * >
|
---|
67 | #define LineSet set < class BoundaryLineSet * >
|
---|
68 | #define LineList list < class BoundaryLineSet * >
|
---|
69 | #define LinePair pair < int, class BoundaryLineSet * >
|
---|
70 | #define LineTestPair pair < LineMap::iterator, bool >
|
---|
71 |
|
---|
72 | #define TriangleMap map < int, class BoundaryTriangleSet * >
|
---|
73 | #define TriangleSet set < class BoundaryTriangleSet * >
|
---|
74 | #define TriangleList list < class BoundaryTriangleSet * >
|
---|
75 | #define TrianglePair pair < int, class BoundaryTriangleSet * >
|
---|
76 | #define TriangleTestPair pair < TrianglePair::iterator, bool >
|
---|
77 |
|
---|
78 | #define PolygonMap map < int, class BoundaryPolygonSet * >
|
---|
79 | #define PolygonSet set < class BoundaryPolygonSet * >
|
---|
80 | #define PolygonList list < class BoundaryPolygonSet * >
|
---|
81 |
|
---|
82 | #define DistanceToPointMap multimap <double, class BoundaryPointSet * >
|
---|
83 | #define DistanceToPointPair pair <double, class BoundaryPointSet * >
|
---|
84 |
|
---|
85 | #define DistanceMultiMap multimap <double, pair < PointMap::iterator, PointMap::iterator> >
|
---|
86 | #define DistanceMultiMapPair pair <double, pair < PointMap::iterator, PointMap::iterator> >
|
---|
87 |
|
---|
88 | #define TesselPointList list <TesselPoint *>
|
---|
89 | #define TesselPointSet set <TesselPoint *>
|
---|
90 |
|
---|
91 | #define ListOfTesselPointList list<list <TesselPoint *> *>
|
---|
92 |
|
---|
93 | enum centers {Opt, OtherOpt};
|
---|
94 |
|
---|
95 | /********************************************** declarations *******************************/
|
---|
96 |
|
---|
97 | template <typename T> void SetEndpointsOrdered(T endpoints[2], T endpoint1, T endpoint2)
|
---|
98 | {
|
---|
99 | if (endpoint1->Nr < endpoint2->Nr) {
|
---|
100 | endpoints[0] = endpoint1;
|
---|
101 | endpoints[1] = endpoint2;
|
---|
102 | } else {
|
---|
103 | endpoints[0] = endpoint2;
|
---|
104 | endpoints[1] = endpoint1;
|
---|
105 | }
|
---|
106 | };
|
---|
107 |
|
---|
108 | // ======================================================== class BoundaryPointSet =========================================
|
---|
109 |
|
---|
110 | class BoundaryPointSet {
|
---|
111 | public:
|
---|
112 | BoundaryPointSet();
|
---|
113 | BoundaryPointSet(TesselPoint * const Walker);
|
---|
114 | ~BoundaryPointSet();
|
---|
115 |
|
---|
116 | void AddLine(BoundaryLineSet * const line);
|
---|
117 |
|
---|
118 | LineMap lines;
|
---|
119 | int LinesCount;
|
---|
120 | TesselPoint *node;
|
---|
121 | double value;
|
---|
122 | int Nr;
|
---|
123 | };
|
---|
124 |
|
---|
125 | ostream & operator << (ostream &ost, const BoundaryPointSet &a);
|
---|
126 |
|
---|
127 | // ======================================================== class BoundaryLineSet ==========================================
|
---|
128 |
|
---|
129 | class BoundaryLineSet {
|
---|
130 | public:
|
---|
131 | BoundaryLineSet();
|
---|
132 | BoundaryLineSet(BoundaryPointSet * const Point[2], const int number);
|
---|
133 | BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number);
|
---|
134 | ~BoundaryLineSet();
|
---|
135 |
|
---|
136 | void AddTriangle(BoundaryTriangleSet * const triangle);
|
---|
137 | bool IsConnectedTo(const BoundaryLineSet * const line) const;
|
---|
138 | bool ContainsBoundaryPoint(const BoundaryPointSet * const point) const;
|
---|
139 | bool CheckConvexityCriterion() const;
|
---|
140 | double CalculateConvexity() const;
|
---|
141 | class BoundaryPointSet *GetOtherEndpoint(const BoundaryPointSet * const point) const;
|
---|
142 | class BoundaryTriangleSet *GetOtherTriangle(const BoundaryTriangleSet * const triangle) const;
|
---|
143 |
|
---|
144 | class BoundaryPointSet *endpoints[2];
|
---|
145 | TriangleMap triangles;
|
---|
146 | int Nr;
|
---|
147 | bool skipped;
|
---|
148 | };
|
---|
149 |
|
---|
150 | ostream & operator << (ostream &ost, const BoundaryLineSet &a);
|
---|
151 |
|
---|
152 | // ======================================================== class BoundaryTriangleSet =======================================
|
---|
153 |
|
---|
154 | class BoundaryTriangleSet {
|
---|
155 | public:
|
---|
156 | BoundaryTriangleSet();
|
---|
157 | BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number);
|
---|
158 | ~BoundaryTriangleSet();
|
---|
159 |
|
---|
160 | void GetNormalVector(const Vector &NormalVector);
|
---|
161 | void GetCenter(Vector * const center) const;
|
---|
162 | bool GetIntersectionInsideTriangle(const Vector * const MolCenter, const Vector * const x, Vector * const Intersection) const;
|
---|
163 | double GetClosestPointInsideTriangle(const Vector * const x, Vector * const ClosestPoint) const;
|
---|
164 | bool ContainsBoundaryLine(const BoundaryLineSet * const line) const;
|
---|
165 | bool ContainsBoundaryPoint(const BoundaryPointSet * const point) const;
|
---|
166 | bool ContainsBoundaryPoint(const TesselPoint * const point) const;
|
---|
167 | class BoundaryPointSet *GetThirdEndpoint(const BoundaryLineSet * const line) const;
|
---|
168 | class BoundaryLineSet *GetThirdLine(const BoundaryPointSet * const point) const;
|
---|
169 | bool IsPresentTupel(const BoundaryPointSet * const Points[3]) const;
|
---|
170 | bool IsPresentTupel(const BoundaryTriangleSet * const T) const;
|
---|
171 |
|
---|
172 | Plane getPlane() const;
|
---|
173 | Vector getEndpoint(int) const;
|
---|
174 | std::string getEndpointName(int) const;
|
---|
175 |
|
---|
176 | class BoundaryPointSet *endpoints[3];
|
---|
177 | class BoundaryLineSet *lines[3];
|
---|
178 | Vector NormalVector;
|
---|
179 | Vector SphereCenter;
|
---|
180 | int Nr;
|
---|
181 |
|
---|
182 | private:
|
---|
183 |
|
---|
184 | };
|
---|
185 |
|
---|
186 | ostream & operator << (ostream &ost, const BoundaryTriangleSet &a);
|
---|
187 |
|
---|
188 |
|
---|
189 | // ======================================================== class BoundaryTriangleSet =======================================
|
---|
190 |
|
---|
191 | /** Set of BoundaryPointSet.
|
---|
192 | * This is just meant as a container for a group of endpoints, extending the node, line, triangle concept. However, this has
|
---|
193 | * only marginally something to do with the tesselation. Hence, there is no incorporation into the bookkeeping of the Tesselation
|
---|
194 | * class (i.e. no allocation, no deletion).
|
---|
195 | * \note we assume that the set of endpoints reside (more or less) on a plane.
|
---|
196 | */
|
---|
197 | class BoundaryPolygonSet {
|
---|
198 | public:
|
---|
199 | BoundaryPolygonSet();
|
---|
200 | ~BoundaryPolygonSet();
|
---|
201 |
|
---|
202 | Vector * GetNormalVector(const Vector &NormalVector) const;
|
---|
203 | void GetCenter(Vector *center) const;
|
---|
204 | bool ContainsBoundaryLine(const BoundaryLineSet * const line) const;
|
---|
205 | bool ContainsBoundaryPoint(const BoundaryPointSet * const point) const;
|
---|
206 | bool ContainsBoundaryPoint(const TesselPoint * const point) const;
|
---|
207 | bool ContainsBoundaryTriangle(const BoundaryTriangleSet * const point) const;
|
---|
208 | bool ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const;
|
---|
209 | bool ContainsPresentTupel(const BoundaryPolygonSet * const P) const;
|
---|
210 | bool ContainsPresentTupel(const PointSet &endpoints) const;
|
---|
211 | TriangleSet * GetAllContainedTrianglesFromEndpoints() const;
|
---|
212 | bool FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line);
|
---|
213 |
|
---|
214 | PointSet endpoints;
|
---|
215 | int Nr;
|
---|
216 | };
|
---|
217 |
|
---|
218 | ostream & operator << (ostream &ost, const BoundaryPolygonSet &a);
|
---|
219 |
|
---|
220 | // =========================================================== class TESSELPOINT ===========================================
|
---|
221 |
|
---|
222 | /** Is a single point of the set of Vectors, also a super-class to be inherited and and its functions to be implemented.
|
---|
223 | */
|
---|
224 | class TesselPoint : virtual public ParticleInfo {
|
---|
225 | public:
|
---|
226 | TesselPoint();
|
---|
227 | virtual ~TesselPoint();
|
---|
228 |
|
---|
229 | Vector *node; // pointer to position of the dot in space
|
---|
230 |
|
---|
231 | virtual ostream & operator << (ostream &ost);
|
---|
232 | };
|
---|
233 |
|
---|
234 | ostream & operator << (ostream &ost, const TesselPoint &a);
|
---|
235 |
|
---|
236 | // =========================================================== class POINTCLOUD ============================================
|
---|
237 |
|
---|
238 | /** Super-class for all point clouds structures, also molecules. They have to inherit this structure and implement the virtual function to access the Vectors.
|
---|
239 | * This basically encapsulates a list structure.
|
---|
240 | */
|
---|
241 | class PointCloud {
|
---|
242 | public:
|
---|
243 | PointCloud();
|
---|
244 | virtual ~PointCloud();
|
---|
245 |
|
---|
246 | virtual const char * const GetName() const { return "unknown"; };
|
---|
247 | virtual Vector *GetCenter() const { return NULL; };
|
---|
248 | virtual TesselPoint *GetPoint() const { return NULL; };
|
---|
249 | virtual int GetMaxId() const { return 0; };
|
---|
250 | virtual void GoToNext() const {};
|
---|
251 | virtual void GoToFirst() const {};
|
---|
252 | virtual bool IsEmpty() const { return true; };
|
---|
253 | virtual bool IsEnd() const { return true; };
|
---|
254 | };
|
---|
255 |
|
---|
256 | // ======================================================== class CandidateForTesselation =========================================
|
---|
257 |
|
---|
258 | class CandidateForTesselation {
|
---|
259 | public :
|
---|
260 | CandidateForTesselation(BoundaryLineSet* currentBaseLine);
|
---|
261 | CandidateForTesselation(TesselPoint* candidate, BoundaryLineSet* currentBaseLine, BoundaryPointSet *point, Vector OptCandidateCenter, Vector OtherOptCandidateCenter);
|
---|
262 | ~CandidateForTesselation();
|
---|
263 |
|
---|
264 | bool CheckValidity(const double RADIUS, const LinkedCell *LC) const;
|
---|
265 |
|
---|
266 | TesselPointList pointlist;
|
---|
267 | const BoundaryLineSet * BaseLine;
|
---|
268 | const BoundaryPointSet * ThirdPoint;
|
---|
269 | const BoundaryTriangleSet *T;
|
---|
270 | Vector OldCenter;
|
---|
271 | Vector OptCenter;
|
---|
272 | Vector OtherOptCenter;
|
---|
273 | double ShortestAngle;
|
---|
274 | double OtherShortestAngle;
|
---|
275 | };
|
---|
276 |
|
---|
277 | ostream & operator <<(ostream &ost, const CandidateForTesselation &a);
|
---|
278 |
|
---|
279 | // =========================================================== class TESSELATION ===========================================
|
---|
280 |
|
---|
281 | /** Contains the envelope to a PointCloud.
|
---|
282 | */
|
---|
283 | class Tesselation : public PointCloud {
|
---|
284 | public:
|
---|
285 |
|
---|
286 | Tesselation();
|
---|
287 | virtual ~Tesselation();
|
---|
288 |
|
---|
289 | void AddTesselationPoint(TesselPoint* Candidate, const int n);
|
---|
290 | void SetTesselationPoint(TesselPoint* Candidate, const int n) const;
|
---|
291 | void AddTesselationLine(const Vector * const OptCenter, const BoundaryPointSet * const candidate, class BoundaryPointSet *a, class BoundaryPointSet *b, const int n);
|
---|
292 | void AddNewTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n);
|
---|
293 | void AddExistingTesselationTriangleLine(class BoundaryLineSet *FindLine, int n);
|
---|
294 | void AddTesselationTriangle();
|
---|
295 | void AddTesselationTriangle(const int nr);
|
---|
296 | void AddCandidateTriangle(CandidateForTesselation &CandidateLine, enum centers type);
|
---|
297 | void AddDegeneratedTriangle(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC);
|
---|
298 | void AddCandidatePolygon(CandidateForTesselation CandidateLine, const double RADIUS, const LinkedCell *LC);
|
---|
299 | void RemoveTesselationTriangle(class BoundaryTriangleSet *triangle);
|
---|
300 | void RemoveTesselationLine(class BoundaryLineSet *line);
|
---|
301 | void RemoveTesselationPoint(class BoundaryPointSet *point);
|
---|
302 | bool CheckDegeneracy(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) const;
|
---|
303 |
|
---|
304 |
|
---|
305 | // concave envelope
|
---|
306 | bool FindStartingTriangle(const double RADIUS, const LinkedCell *LC);
|
---|
307 | void FindSecondPointForTesselation(class TesselPoint* a, Vector Oben, class TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC);
|
---|
308 | void FindThirdPointForTesselation(const Vector &NormalVector, const Vector &SearchDirection, const Vector &OldSphereCenter, CandidateForTesselation &CandidateLine, const class BoundaryPointSet * const ThirdNode, const double RADIUS, const LinkedCell *LC) const;
|
---|
309 | bool FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, const BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC);
|
---|
310 | bool FindCandidatesforOpenLines(const double RADIUS, const LinkedCell *&LCList);
|
---|
311 | int CheckPresenceOfTriangle(class TesselPoint *Candidates[3]) const;
|
---|
312 | class BoundaryTriangleSet * GetPresentTriangle(TesselPoint *Candidates[3]);
|
---|
313 |
|
---|
314 | // convex envelope
|
---|
315 | void TesselateOnBoundary(const PointCloud * const cloud);
|
---|
316 | void GuessStartingTriangle();
|
---|
317 | bool InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC);
|
---|
318 | double RemovePointFromTesselatedSurface(class BoundaryPointSet *point);
|
---|
319 | class BoundaryLineSet * FlipBaseline(class BoundaryLineSet *Base);
|
---|
320 | double PickFarthestofTwoBaselines(class BoundaryLineSet *Base);
|
---|
321 | class BoundaryPointSet *IsConvexRectangle(class BoundaryLineSet *Base);
|
---|
322 | IndexToIndex * FindAllDegeneratedTriangles();
|
---|
323 | IndexToIndex * FindAllDegeneratedLines();
|
---|
324 | void RemoveDegeneratedTriangles();
|
---|
325 | void AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC);
|
---|
326 | int CorrectAllDegeneratedPolygons();
|
---|
327 |
|
---|
328 | TesselPointSet * GetAllConnectedPoints(const TesselPoint* const Point) const;
|
---|
329 | TriangleSet * GetAllTriangles(const BoundaryPointSet * const Point) const;
|
---|
330 | ListOfTesselPointList * GetPathsOfConnectedPoints(const TesselPoint* const Point) const;
|
---|
331 | ListOfTesselPointList * GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const;
|
---|
332 | TesselPointList * GetCircleOfSetOfPoints(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference = NULL) const;
|
---|
333 | TesselPointList * GetCircleOfConnectedTriangles(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const;
|
---|
334 | class BoundaryPointSet * GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const;
|
---|
335 | TriangleList * FindTriangles(const TesselPoint* const Points[3]) const;
|
---|
336 | TriangleList * FindClosestTrianglesToVector(const Vector *x, const LinkedCell* LC) const;
|
---|
337 | BoundaryTriangleSet * FindClosestTriangleToVector(const Vector *x, const LinkedCell* LC) const;
|
---|
338 | bool IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const;
|
---|
339 | double GetDistanceSquaredToTriangle(const Vector &Point, const BoundaryTriangleSet* const triangle) const;
|
---|
340 | double GetDistanceToSurface(const Vector &Point, const LinkedCell* const LC) const;
|
---|
341 | BoundaryTriangleSet * GetClosestTriangleOnSurface(const Vector &Point, const LinkedCell* const LC) const;
|
---|
342 | bool AddBoundaryPoint(TesselPoint * Walker, const int n);
|
---|
343 | DistanceToPointMap * FindClosestBoundaryPointsToVector(const Vector *x, const LinkedCell* LC) const;
|
---|
344 | BoundaryLineSet * FindClosestBoundaryLineToVector(const Vector *x, const LinkedCell* LC) const;
|
---|
345 |
|
---|
346 | // print for debugging
|
---|
347 | void PrintAllBoundaryPoints(ofstream *out) const;
|
---|
348 | void PrintAllBoundaryLines(ofstream *out) const;
|
---|
349 | void PrintAllBoundaryTriangles(ofstream *out) const;
|
---|
350 |
|
---|
351 | // store envelope in file
|
---|
352 | void Output(const char *filename, const PointCloud * const cloud);
|
---|
353 |
|
---|
354 | PointMap PointsOnBoundary;
|
---|
355 | LineMap LinesOnBoundary;
|
---|
356 | CandidateMap OpenLines;
|
---|
357 | TriangleMap TrianglesOnBoundary;
|
---|
358 | int PointsOnBoundaryCount;
|
---|
359 | int LinesOnBoundaryCount;
|
---|
360 | int TrianglesOnBoundaryCount;
|
---|
361 |
|
---|
362 | // PointCloud implementation for PointsOnBoundary
|
---|
363 | virtual Vector *GetCenter(ofstream *out) const;
|
---|
364 | virtual TesselPoint *GetPoint() const;
|
---|
365 | virtual void GoToNext() const;
|
---|
366 | virtual void GoToFirst() const;
|
---|
367 | virtual bool IsEmpty() const;
|
---|
368 | virtual bool IsEnd() const;
|
---|
369 |
|
---|
370 | class BoundaryPointSet *BPS[2];
|
---|
371 | class BoundaryLineSet *BLS[3];
|
---|
372 | class BoundaryTriangleSet *BTS;
|
---|
373 | class BoundaryTriangleSet *LastTriangle;
|
---|
374 | int TriangleFilesWritten;
|
---|
375 |
|
---|
376 | private:
|
---|
377 | mutable class BoundaryPointSet *TPS[3]; //this is a Storage for pointers to triangle points, this and BPS[2] needed due to AddLine restrictions
|
---|
378 |
|
---|
379 | mutable PointMap::const_iterator InternalPointer;
|
---|
380 |
|
---|
381 | //bool HasOtherBaselineBetterCandidate(const BoundaryLineSet * const BaseRay, const TesselPoint * const OptCandidate, double ShortestAngle, double RADIUS, const LinkedCell * const LC) const;
|
---|
382 | void FindDegeneratedCandidatesforOpenLines(TesselPoint * const Sprinter, const Vector * const OptCenter);
|
---|
383 | };
|
---|
384 |
|
---|
385 |
|
---|
386 | #endif /* TESSELATION_HPP_ */
|
---|