| [357fba] | 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 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
|  | 20 | #include <map> | 
|---|
|  | 21 | #include <list> | 
|---|
| [7c14ec] | 22 | #include <set> | 
|---|
| [357fba] | 23 |  | 
|---|
|  | 24 |  | 
|---|
|  | 25 | #include "helpers.hpp" | 
|---|
|  | 26 | #include "linkedcell.hpp" | 
|---|
|  | 27 | #include "tesselationhelpers.hpp" | 
|---|
|  | 28 | #include "vector.hpp" | 
|---|
|  | 29 |  | 
|---|
|  | 30 | class BoundaryPointSet; | 
|---|
|  | 31 | class BoundaryLineSet; | 
|---|
|  | 32 | class BoundaryTriangleSet; | 
|---|
|  | 33 | class TesselPoint; | 
|---|
|  | 34 | class PointCloud; | 
|---|
|  | 35 | class Tesselation; | 
|---|
|  | 36 |  | 
|---|
|  | 37 | // ======================================================= some template functions ========================================= | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #define PointMap map < int, class BoundaryPointSet * > | 
|---|
|  | 40 | #define PointPair pair < int, class BoundaryPointSet * > | 
|---|
|  | 41 | #define PointTestPair pair < PointMap::iterator, bool > | 
|---|
|  | 42 | #define CandidateList list <class CandidateForTesselation *> | 
|---|
|  | 43 |  | 
|---|
|  | 44 | #define LineMap multimap < int, class BoundaryLineSet * > | 
|---|
|  | 45 | #define LinePair pair < int, class BoundaryLineSet * > | 
|---|
|  | 46 | #define LineTestPair pair < LineMap::iterator, bool > | 
|---|
|  | 47 |  | 
|---|
|  | 48 | #define TriangleMap map < int, class BoundaryTriangleSet * > | 
|---|
|  | 49 | #define TrianglePair pair < int, class BoundaryTriangleSet * > | 
|---|
|  | 50 | #define TriangleTestPair pair < TrianglePair::iterator, bool > | 
|---|
|  | 51 |  | 
|---|
|  | 52 | #define DistanceMultiMap multimap <double, pair < PointMap::iterator, PointMap::iterator> > | 
|---|
|  | 53 | #define DistanceMultiMapPair pair <double, pair < PointMap::iterator, PointMap::iterator> > | 
|---|
|  | 54 |  | 
|---|
|  | 55 |  | 
|---|
|  | 56 |  | 
|---|
|  | 57 | template <typename T> void SetEndpointsOrdered(T endpoints[2], T endpoint1, T endpoint2) | 
|---|
|  | 58 | { | 
|---|
|  | 59 | if (endpoint1->Nr < endpoint2->Nr) { | 
|---|
|  | 60 | endpoints[0] = endpoint1; | 
|---|
|  | 61 | endpoints[1] = endpoint2; | 
|---|
|  | 62 | } else { | 
|---|
|  | 63 | endpoints[0] = endpoint2; | 
|---|
|  | 64 | endpoints[1] = endpoint1; | 
|---|
|  | 65 | } | 
|---|
|  | 66 | }; | 
|---|
|  | 67 |  | 
|---|
|  | 68 | // ======================================================== class BoundaryPointSet ========================================= | 
|---|
|  | 69 |  | 
|---|
|  | 70 | class BoundaryPointSet { | 
|---|
|  | 71 | public: | 
|---|
|  | 72 | BoundaryPointSet(); | 
|---|
|  | 73 | BoundaryPointSet(TesselPoint *Walker); | 
|---|
|  | 74 | ~BoundaryPointSet(); | 
|---|
|  | 75 |  | 
|---|
|  | 76 | void AddLine(class BoundaryLineSet *line); | 
|---|
|  | 77 |  | 
|---|
|  | 78 | LineMap lines; | 
|---|
|  | 79 | int LinesCount; | 
|---|
|  | 80 | TesselPoint *node; | 
|---|
| [16d866] | 81 | double value; | 
|---|
| [357fba] | 82 | int Nr; | 
|---|
|  | 83 | }; | 
|---|
|  | 84 |  | 
|---|
|  | 85 | ostream & operator << (ostream &ost, BoundaryPointSet &a); | 
|---|
|  | 86 |  | 
|---|
|  | 87 | // ======================================================== class BoundaryLineSet ========================================== | 
|---|
|  | 88 |  | 
|---|
|  | 89 | class BoundaryLineSet { | 
|---|
|  | 90 | public: | 
|---|
|  | 91 | BoundaryLineSet(); | 
|---|
|  | 92 | BoundaryLineSet(class BoundaryPointSet *Point[2], int number); | 
|---|
|  | 93 | ~BoundaryLineSet(); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | void AddTriangle(class BoundaryTriangleSet *triangle); | 
|---|
|  | 96 | bool IsConnectedTo(class BoundaryLineSet *line); | 
|---|
|  | 97 | bool ContainsBoundaryPoint(class BoundaryPointSet *point); | 
|---|
|  | 98 | bool CheckConvexityCriterion(ofstream *out); | 
|---|
| [62bb91] | 99 | class BoundaryPointSet *GetOtherEndpoint(class BoundaryPointSet *); | 
|---|
| [357fba] | 100 |  | 
|---|
|  | 101 | class BoundaryPointSet *endpoints[2]; | 
|---|
|  | 102 | TriangleMap triangles; | 
|---|
|  | 103 | int Nr; | 
|---|
|  | 104 | }; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | ostream & operator << (ostream &ost, BoundaryLineSet &a); | 
|---|
|  | 107 |  | 
|---|
|  | 108 | // ======================================================== class BoundaryTriangleSet ======================================= | 
|---|
|  | 109 |  | 
|---|
|  | 110 | class BoundaryTriangleSet { | 
|---|
|  | 111 | public: | 
|---|
|  | 112 | BoundaryTriangleSet(); | 
|---|
|  | 113 | BoundaryTriangleSet(class BoundaryLineSet *line[3], int number); | 
|---|
|  | 114 | ~BoundaryTriangleSet(); | 
|---|
|  | 115 |  | 
|---|
|  | 116 | void GetNormalVector(Vector &NormalVector); | 
|---|
|  | 117 | bool GetIntersectionInsideTriangle(ofstream *out, Vector *MolCenter, Vector *x, Vector *Intersection); | 
|---|
|  | 118 | bool ContainsBoundaryLine(class BoundaryLineSet *line); | 
|---|
|  | 119 | bool ContainsBoundaryPoint(class BoundaryPointSet *point); | 
|---|
| [62bb91] | 120 | class BoundaryPointSet *GetThirdEndpoint(class BoundaryLineSet *line); | 
|---|
| [357fba] | 121 | bool IsPresentTupel(class BoundaryPointSet *Points[3]); | 
|---|
| [62bb91] | 122 | void GetCenter(Vector *center); | 
|---|
| [357fba] | 123 |  | 
|---|
|  | 124 | class BoundaryPointSet *endpoints[3]; | 
|---|
|  | 125 | class BoundaryLineSet *lines[3]; | 
|---|
|  | 126 | Vector NormalVector; | 
|---|
|  | 127 | int Nr; | 
|---|
|  | 128 | }; | 
|---|
|  | 129 |  | 
|---|
|  | 130 | ostream & operator << (ostream &ost, BoundaryTriangleSet &a); | 
|---|
|  | 131 |  | 
|---|
|  | 132 | // =========================================================== class TESSELPOINT =========================================== | 
|---|
|  | 133 |  | 
|---|
|  | 134 | /** Is a single point of the set of Vectors, also a super-class to be inherited and and its functions to be implemented. | 
|---|
|  | 135 | */ | 
|---|
|  | 136 | class TesselPoint { | 
|---|
|  | 137 | public: | 
|---|
|  | 138 | TesselPoint(); | 
|---|
| [5c7bf8] | 139 | virtual ~TesselPoint(); | 
|---|
| [357fba] | 140 |  | 
|---|
|  | 141 | Vector *node;   // pointer to position of the dot in space | 
|---|
|  | 142 | int nr;       // index to easierly identify | 
|---|
|  | 143 | char *Name;   // some name to reference to on output | 
|---|
| [5c7bf8] | 144 |  | 
|---|
|  | 145 | virtual ostream & operator << (ostream &ost); | 
|---|
| [357fba] | 146 | }; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | ostream & operator << (ostream &ost, const TesselPoint &a); | 
|---|
|  | 149 |  | 
|---|
|  | 150 | // =========================================================== class POINTCLOUD ============================================ | 
|---|
|  | 151 |  | 
|---|
|  | 152 | /** Super-class for all point clouds structures, also molecules. They have to inherit this structure and implement the virtual function to access the Vectors. | 
|---|
|  | 153 | * This basically encapsulates a list structure. | 
|---|
|  | 154 | */ | 
|---|
|  | 155 | class PointCloud { | 
|---|
|  | 156 | public: | 
|---|
|  | 157 | PointCloud(); | 
|---|
| [ab1932] | 158 | virtual ~PointCloud(); | 
|---|
| [357fba] | 159 |  | 
|---|
|  | 160 | virtual Vector *GetCenter(ofstream *out) { return NULL; }; | 
|---|
|  | 161 | virtual TesselPoint *GetPoint() { return NULL; }; | 
|---|
|  | 162 | virtual TesselPoint *GetTerminalPoint() { return NULL; }; | 
|---|
|  | 163 | virtual void GoToNext() {}; | 
|---|
|  | 164 | virtual void GoToPrevious() {}; | 
|---|
|  | 165 | virtual void GoToFirst() {}; | 
|---|
|  | 166 | virtual void GoToLast() {}; | 
|---|
|  | 167 | virtual bool IsEmpty() { return false; }; | 
|---|
| [1999d8] | 168 | virtual bool IsEnd() { return false; }; | 
|---|
| [357fba] | 169 | }; | 
|---|
|  | 170 |  | 
|---|
|  | 171 | // ======================================================== class CandidateForTesselation ========================================= | 
|---|
|  | 172 |  | 
|---|
|  | 173 | class CandidateForTesselation { | 
|---|
|  | 174 | public : | 
|---|
|  | 175 | CandidateForTesselation(TesselPoint* candidate, BoundaryLineSet* currentBaseLine, Vector OptCandidateCenter, Vector OtherOptCandidateCenter); | 
|---|
|  | 176 | ~CandidateForTesselation(); | 
|---|
|  | 177 |  | 
|---|
|  | 178 | TesselPoint *point; | 
|---|
|  | 179 | BoundaryLineSet *BaseLine; | 
|---|
|  | 180 | Vector OptCenter; | 
|---|
|  | 181 | Vector OtherOptCenter; | 
|---|
|  | 182 | }; | 
|---|
|  | 183 |  | 
|---|
|  | 184 | // =========================================================== class TESSELATION =========================================== | 
|---|
|  | 185 |  | 
|---|
|  | 186 | /** Contains the envelope to a PointCloud. | 
|---|
|  | 187 | */ | 
|---|
| [5c7bf8] | 188 | class Tesselation : public PointCloud { | 
|---|
| [357fba] | 189 | public: | 
|---|
|  | 190 |  | 
|---|
|  | 191 | Tesselation(); | 
|---|
| [5c7bf8] | 192 | virtual ~Tesselation(); | 
|---|
| [357fba] | 193 |  | 
|---|
| [16d866] | 194 | void AddTesselationPoint(TesselPoint* Candidate, int n); | 
|---|
|  | 195 | void AddTesselationLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n); | 
|---|
|  | 196 | void AlwaysAddTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n); | 
|---|
|  | 197 | void AddTesselationTriangle(); | 
|---|
|  | 198 | void RemoveTesselationTriangle(class BoundaryTriangleSet *triangle); | 
|---|
|  | 199 | void RemoveTesselationLine(class BoundaryLineSet *line); | 
|---|
|  | 200 | void RemoveTesselationPoint(class BoundaryPointSet *point); | 
|---|
|  | 201 |  | 
|---|
| [357fba] | 202 | bool IsInside(Vector *pointer); | 
|---|
|  | 203 | class BoundaryPointSet *GetCommonEndpoint(class BoundaryLineSet * line1, class BoundaryLineSet * line2); | 
|---|
|  | 204 |  | 
|---|
|  | 205 | // concave envelope | 
|---|
| [f1cccd] | 206 | void FindStartingTriangle(ofstream *out, const double RADIUS, class LinkedCell *LC); | 
|---|
|  | 207 | void FindSecondPointForTesselation(class TesselPoint* a, class TesselPoint* Candidate, Vector Oben, class TesselPoint*& OptCandidate, double Storage[3], double RADIUS, class LinkedCell *LC); | 
|---|
|  | 208 | void FindThirdPointForTesselation(Vector NormalVector, Vector SearchDirection, Vector OldSphereCenter, class BoundaryLineSet *BaseLine, class TesselPoint *ThirdNode, CandidateList* &candidates, double *ShortestAngle, const double RADIUS, class LinkedCell *LC); | 
|---|
|  | 209 | bool FindNextSuitableTriangle(ofstream *out, BoundaryLineSet &Line, BoundaryTriangleSet &T, const double& RADIUS, int N, LinkedCell *LC); | 
|---|
| [357fba] | 210 | int CheckPresenceOfTriangle(ofstream *out, class TesselPoint *Candidates[3]); | 
|---|
|  | 211 |  | 
|---|
|  | 212 | // convex envelope | 
|---|
|  | 213 | void TesselateOnBoundary(ofstream *out, PointCloud *cloud); | 
|---|
|  | 214 | void GuessStartingTriangle(ofstream *out); | 
|---|
| [62bb91] | 215 | bool InsertStraddlingPoints(ofstream *out, PointCloud *cloud, LinkedCell *LC); | 
|---|
| [16d866] | 216 | double RemovePointFromTesselatedSurface(ofstream *out, class BoundaryPointSet *point); | 
|---|
|  | 217 | bool FlipBaseline(ofstream *out, class BoundaryLineSet *Base); | 
|---|
|  | 218 | bool PickFarthestofTwoBaselines(ofstream *out, class BoundaryLineSet *Base); | 
|---|
|  | 219 | class BoundaryPointSet *IsConvexRectangle(ofstream *out, class BoundaryLineSet *Base); | 
|---|
| [7c14ec] | 220 | map<int, int> FindAllDegeneratedTriangles(); | 
|---|
|  | 221 | void RemoveDegeneratedTriangles(); | 
|---|
| [16d866] | 222 |  | 
|---|
| [f1cccd] | 223 | list<TesselPoint*> * GetCircleOfConnectedPoints(ofstream *out, TesselPoint* Point); | 
|---|
|  | 224 | list<TesselPoint*> * GetNeighboursOnCircleOfConnectedPoints(ofstream *out, list<TesselPoint*> *connectedPoints, TesselPoint* Point, Vector* Reference); | 
|---|
| [ab1932] | 225 | list<BoundaryTriangleSet*> *FindTriangles(TesselPoint* Points[3]); | 
|---|
| [62bb91] | 226 | list<BoundaryTriangleSet*> * FindClosestTrianglesToPoint(ofstream *out, Vector *x, LinkedCell* LC); | 
|---|
|  | 227 | class BoundaryTriangleSet * FindClosestTriangleToPoint(ofstream *out, Vector *x, LinkedCell* LC); | 
|---|
|  | 228 | bool IsInnerPoint(ofstream *out, Vector Point, LinkedCell* LC); | 
|---|
|  | 229 | bool IsInnerPoint(ofstream *out, TesselPoint *Point, LinkedCell* LC); | 
|---|
| [16d866] | 230 | bool AddBoundaryPoint(TesselPoint *Walker, int n); | 
|---|
| [ab1932] | 231 |  | 
|---|
| [0077b5] | 232 | // print for debugging | 
|---|
|  | 233 | void PrintAllBoundaryPoints(ofstream *out); | 
|---|
|  | 234 | void PrintAllBoundaryLines(ofstream *out); | 
|---|
|  | 235 | void PrintAllBoundaryTriangles(ofstream *out); | 
|---|
|  | 236 |  | 
|---|
|  | 237 |  | 
|---|
| [357fba] | 238 | PointMap PointsOnBoundary; | 
|---|
|  | 239 | LineMap LinesOnBoundary; | 
|---|
|  | 240 | TriangleMap TrianglesOnBoundary; | 
|---|
|  | 241 | int PointsOnBoundaryCount; | 
|---|
|  | 242 | int LinesOnBoundaryCount; | 
|---|
|  | 243 | int TrianglesOnBoundaryCount; | 
|---|
|  | 244 |  | 
|---|
| [5c7bf8] | 245 | // PointCloud implementation for PointsOnBoundary | 
|---|
|  | 246 | virtual Vector *GetCenter(ofstream *out); | 
|---|
|  | 247 | virtual TesselPoint *GetPoint(); | 
|---|
|  | 248 | virtual TesselPoint *GetTerminalPoint(); | 
|---|
|  | 249 | virtual void GoToNext(); | 
|---|
|  | 250 | virtual void GoToPrevious(); | 
|---|
|  | 251 | virtual void GoToFirst(); | 
|---|
|  | 252 | virtual void GoToLast(); | 
|---|
|  | 253 | virtual bool IsEmpty(); | 
|---|
|  | 254 | virtual bool IsEnd(); | 
|---|
|  | 255 |  | 
|---|
| [357fba] | 256 | class BoundaryPointSet *BPS[2]; | 
|---|
|  | 257 | class BoundaryLineSet *BLS[3]; | 
|---|
|  | 258 | class BoundaryTriangleSet *BTS; | 
|---|
| [5c7bf8] | 259 |  | 
|---|
| [08ef35] | 260 | private: | 
|---|
|  | 261 | class BoundaryPointSet *TPS[3]; //this is a Storage for pointers to triangle points, this and BPS[2] needed due to AddLine restrictions | 
|---|
|  | 262 |  | 
|---|
| [5c7bf8] | 263 | PointMap::iterator InternalPointer; | 
|---|
| [357fba] | 264 | }; | 
|---|
|  | 265 |  | 
|---|
| [f1cccd] | 266 | bool CheckLineCriteriaForDegeneratedTriangle(class BoundaryPointSet *nodes[3]); | 
|---|
|  | 267 | bool SortCandidates(class CandidateForTesselation* candidate1, class CandidateForTesselation* candidate2); | 
|---|
|  | 268 | TesselPoint* FindClosestPoint(const Vector* Point, TesselPoint *&SecondPoint, LinkedCell* LC); | 
|---|
|  | 269 | TesselPoint* FindSecondClosestPoint(const Vector*, LinkedCell*); | 
|---|
|  | 270 | double GetAngle(const Vector &point, const Vector &reference, const Vector OrthogonalVector); | 
|---|
| [16d866] | 271 | Vector * GetClosestPointBetweenLine(ofstream *out, class BoundaryLineSet *Base, class BoundaryLineSet *OtherBase); | 
|---|
| [357fba] | 272 |  | 
|---|
|  | 273 | #endif /* TESSELATION_HPP_ */ | 
|---|