| 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>
 | 
|---|
| 22 | #include <set>
 | 
|---|
| 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;
 | 
|---|
| 81 |     double value;
 | 
|---|
| 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);
 | 
|---|
| 99 |     class BoundaryPointSet *GetOtherEndpoint(class BoundaryPointSet *);
 | 
|---|
| 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);
 | 
|---|
| 120 |     class BoundaryPointSet *GetThirdEndpoint(class BoundaryLineSet *line);
 | 
|---|
| 121 |     bool IsPresentTupel(class BoundaryPointSet *Points[3]);
 | 
|---|
| 122 |     void GetCenter(Vector *center);
 | 
|---|
| 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();
 | 
|---|
| 139 |   virtual ~TesselPoint();
 | 
|---|
| 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
 | 
|---|
| 144 | 
 | 
|---|
| 145 |   virtual ostream & operator << (ostream &ost);
 | 
|---|
| 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();
 | 
|---|
| 158 |   virtual ~PointCloud();
 | 
|---|
| 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; };
 | 
|---|
| 168 |   virtual bool IsEnd() { return false; };
 | 
|---|
| 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 |  */
 | 
|---|
| 188 | class Tesselation : public PointCloud {
 | 
|---|
| 189 |   public:
 | 
|---|
| 190 | 
 | 
|---|
| 191 |     Tesselation();
 | 
|---|
| 192 |     virtual ~Tesselation();
 | 
|---|
| 193 | 
 | 
|---|
| 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 | 
 | 
|---|
| 202 |     bool IsInside(Vector *pointer);
 | 
|---|
| 203 |     class BoundaryPointSet *GetCommonEndpoint(class BoundaryLineSet * line1, class BoundaryLineSet * line2);
 | 
|---|
| 204 | 
 | 
|---|
| 205 |     // concave envelope
 | 
|---|
| 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);
 | 
|---|
| 210 |     int CheckPresenceOfTriangle(ofstream *out, class TesselPoint *Candidates[3]);
 | 
|---|
| 211 |     class BoundaryTriangleSet * GetPresentTriangle(ofstream *out, TesselPoint *Candidates[3]);
 | 
|---|
| 212 | 
 | 
|---|
| 213 |     // convex envelope
 | 
|---|
| 214 |     void TesselateOnBoundary(ofstream *out, PointCloud *cloud);
 | 
|---|
| 215 |     void GuessStartingTriangle(ofstream *out);
 | 
|---|
| 216 |     bool InsertStraddlingPoints(ofstream *out, PointCloud *cloud, LinkedCell *LC);
 | 
|---|
| 217 |     double RemovePointFromTesselatedSurface(ofstream *out, class BoundaryPointSet *point);
 | 
|---|
| 218 |     bool FlipBaseline(ofstream *out, class BoundaryLineSet *Base);
 | 
|---|
| 219 |     bool PickFarthestofTwoBaselines(ofstream *out, class BoundaryLineSet *Base);
 | 
|---|
| 220 |     class BoundaryPointSet *IsConvexRectangle(ofstream *out, class BoundaryLineSet *Base);
 | 
|---|
| 221 |     map<int, int> FindAllDegeneratedTriangles();
 | 
|---|
| 222 |     void RemoveDegeneratedTriangles();
 | 
|---|
| 223 | 
 | 
|---|
| 224 |     set<TesselPoint*> * GetAllConnectedPoints(ofstream *out, TesselPoint* Point);
 | 
|---|
| 225 |     set<BoundaryTriangleSet*> *GetAllTriangles(ofstream *out, class BoundaryPointSet *Point);
 | 
|---|
| 226 |     list<list<TesselPoint*> *> * GetPathsOfConnectedPoints(ofstream *out, TesselPoint* Point);
 | 
|---|
| 227 |     list<list<TesselPoint*> *> * GetClosedPathsOfConnectedPoints(ofstream *out, TesselPoint* Point);
 | 
|---|
| 228 |     list<TesselPoint*> * GetCircleOfConnectedPoints(ofstream *out, TesselPoint* Point, Vector *Reference = NULL);
 | 
|---|
| 229 |     list<BoundaryTriangleSet*> *FindTriangles(TesselPoint* Points[3]);
 | 
|---|
| 230 |     list<BoundaryTriangleSet*> * FindClosestTrianglesToPoint(ofstream *out, Vector *x, LinkedCell* LC);
 | 
|---|
| 231 |     class BoundaryTriangleSet * FindClosestTriangleToPoint(ofstream *out, Vector *x, LinkedCell* LC);
 | 
|---|
| 232 |     bool IsInnerPoint(ofstream *out, Vector Point, LinkedCell* LC);
 | 
|---|
| 233 |     bool IsInnerPoint(ofstream *out, TesselPoint *Point, LinkedCell* LC);
 | 
|---|
| 234 |     bool AddBoundaryPoint(TesselPoint *Walker, int n);
 | 
|---|
| 235 | 
 | 
|---|
| 236 |     // print for debugging
 | 
|---|
| 237 |     void PrintAllBoundaryPoints(ofstream *out);
 | 
|---|
| 238 |     void PrintAllBoundaryLines(ofstream *out);
 | 
|---|
| 239 |     void PrintAllBoundaryTriangles(ofstream *out);
 | 
|---|
| 240 | 
 | 
|---|
| 241 | 
 | 
|---|
| 242 |     PointMap PointsOnBoundary;
 | 
|---|
| 243 |     LineMap LinesOnBoundary;
 | 
|---|
| 244 |     TriangleMap TrianglesOnBoundary;
 | 
|---|
| 245 |     int PointsOnBoundaryCount;
 | 
|---|
| 246 |     int LinesOnBoundaryCount;
 | 
|---|
| 247 |     int TrianglesOnBoundaryCount;
 | 
|---|
| 248 | 
 | 
|---|
| 249 |     // PointCloud implementation for PointsOnBoundary
 | 
|---|
| 250 |     virtual Vector *GetCenter(ofstream *out);
 | 
|---|
| 251 |     virtual TesselPoint *GetPoint();
 | 
|---|
| 252 |     virtual TesselPoint *GetTerminalPoint();
 | 
|---|
| 253 |     virtual void GoToNext();
 | 
|---|
| 254 |     virtual void GoToPrevious();
 | 
|---|
| 255 |     virtual void GoToFirst();
 | 
|---|
| 256 |     virtual void GoToLast();
 | 
|---|
| 257 |     virtual bool IsEmpty();
 | 
|---|
| 258 |     virtual bool IsEnd();
 | 
|---|
| 259 | 
 | 
|---|
| 260 |     class BoundaryPointSet *BPS[2];
 | 
|---|
| 261 |     class BoundaryLineSet *BLS[3];
 | 
|---|
| 262 |     class BoundaryTriangleSet *BTS;
 | 
|---|
| 263 | 
 | 
|---|
| 264 |   private:
 | 
|---|
| 265 |     class BoundaryPointSet *TPS[3]; //this is a Storage for pointers to triangle points, this and BPS[2] needed due to AddLine restrictions
 | 
|---|
| 266 | 
 | 
|---|
| 267 |     PointMap::iterator InternalPointer;
 | 
|---|
| 268 | };
 | 
|---|
| 269 | 
 | 
|---|
| 270 | bool CheckLineCriteriaForDegeneratedTriangle(class BoundaryPointSet *nodes[3]);
 | 
|---|
| 271 | bool SortCandidates(class CandidateForTesselation* candidate1, class CandidateForTesselation* candidate2);
 | 
|---|
| 272 | TesselPoint* FindClosestPoint(const Vector* Point, TesselPoint *&SecondPoint, LinkedCell* LC);
 | 
|---|
| 273 | TesselPoint* FindSecondClosestPoint(const Vector*, LinkedCell*);
 | 
|---|
| 274 | double GetAngle(const Vector &point, const Vector &reference, const Vector OrthogonalVector);
 | 
|---|
| 275 | Vector * GetClosestPointBetweenLine(ofstream *out, class BoundaryLineSet *Base, class BoundaryLineSet *OtherBase);
 | 
|---|
| 276 | 
 | 
|---|
| 277 | #endif /* TESSELATION_HPP_ */
 | 
|---|