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