| 1 | /* | 
|---|
| 2 | * tesselation.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 3, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <fstream> | 
|---|
| 9 | #include <assert.h> | 
|---|
| 10 |  | 
|---|
| 11 | #include "helpers.hpp" | 
|---|
| 12 | #include "info.hpp" | 
|---|
| 13 | #include "linkedcell.hpp" | 
|---|
| 14 | #include "log.hpp" | 
|---|
| 15 | #include "tesselation.hpp" | 
|---|
| 16 | #include "tesselationhelpers.hpp" | 
|---|
| 17 | #include "triangleintersectionlist.hpp" | 
|---|
| 18 | #include "vector.hpp" | 
|---|
| 19 | #include "vector_ops.hpp" | 
|---|
| 20 | #include "verbose.hpp" | 
|---|
| 21 | #include "Plane.hpp" | 
|---|
| 22 | #include "Exceptions/LinearDependenceException.hpp" | 
|---|
| 23 |  | 
|---|
| 24 | class molecule; | 
|---|
| 25 |  | 
|---|
| 26 | // ======================================== Points on Boundary ================================= | 
|---|
| 27 |  | 
|---|
| 28 | /** Constructor of BoundaryPointSet. | 
|---|
| 29 | */ | 
|---|
| 30 | BoundaryPointSet::BoundaryPointSet() : | 
|---|
| 31 | LinesCount(0), value(0.), Nr(-1) | 
|---|
| 32 | { | 
|---|
| 33 | Info FunctionInfo(__func__); | 
|---|
| 34 | DoLog(1) && (Log() << Verbose(1) << "Adding noname." << endl); | 
|---|
| 35 | } | 
|---|
| 36 | ; | 
|---|
| 37 |  | 
|---|
| 38 | /** Constructor of BoundaryPointSet with Tesselpoint. | 
|---|
| 39 | * \param *Walker TesselPoint this boundary point represents | 
|---|
| 40 | */ | 
|---|
| 41 | BoundaryPointSet::BoundaryPointSet(TesselPoint * const Walker) : | 
|---|
| 42 | LinesCount(0), node(Walker), value(0.), Nr(Walker->nr) | 
|---|
| 43 | { | 
|---|
| 44 | Info FunctionInfo(__func__); | 
|---|
| 45 | DoLog(1) && (Log() << Verbose(1) << "Adding Node " << *Walker << endl); | 
|---|
| 46 | } | 
|---|
| 47 | ; | 
|---|
| 48 |  | 
|---|
| 49 | /** Destructor of BoundaryPointSet. | 
|---|
| 50 | * Sets node to NULL to avoid removing the original, represented TesselPoint. | 
|---|
| 51 | * \note When removing point from a class Tesselation, use RemoveTesselationPoint() | 
|---|
| 52 | */ | 
|---|
| 53 | BoundaryPointSet::~BoundaryPointSet() | 
|---|
| 54 | { | 
|---|
| 55 | Info FunctionInfo(__func__); | 
|---|
| 56 | //Log() << Verbose(0) << "Erasing point nr. " << Nr << "." << endl; | 
|---|
| 57 | if (!lines.empty()) | 
|---|
| 58 | DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some lines." << endl); | 
|---|
| 59 | node = NULL; | 
|---|
| 60 | } | 
|---|
| 61 | ; | 
|---|
| 62 |  | 
|---|
| 63 | /** Add a line to the LineMap of this point. | 
|---|
| 64 | * \param *line line to add | 
|---|
| 65 | */ | 
|---|
| 66 | void BoundaryPointSet::AddLine(BoundaryLineSet * const line) | 
|---|
| 67 | { | 
|---|
| 68 | Info FunctionInfo(__func__); | 
|---|
| 69 | DoLog(1) && (Log() << Verbose(1) << "Adding " << *this << " to line " << *line << "." << endl); | 
|---|
| 70 | if (line->endpoints[0] == this) { | 
|---|
| 71 | lines.insert(LinePair(line->endpoints[1]->Nr, line)); | 
|---|
| 72 | } else { | 
|---|
| 73 | lines.insert(LinePair(line->endpoints[0]->Nr, line)); | 
|---|
| 74 | } | 
|---|
| 75 | LinesCount++; | 
|---|
| 76 | } | 
|---|
| 77 | ; | 
|---|
| 78 |  | 
|---|
| 79 | /** output operator for BoundaryPointSet. | 
|---|
| 80 | * \param &ost output stream | 
|---|
| 81 | * \param &a boundary point | 
|---|
| 82 | */ | 
|---|
| 83 | ostream & operator <<(ostream &ost, const BoundaryPointSet &a) | 
|---|
| 84 | { | 
|---|
| 85 | ost << "[" << a.Nr << "|" << a.node->Name << " at " << *a.node->node << "]"; | 
|---|
| 86 | return ost; | 
|---|
| 87 | } | 
|---|
| 88 | ; | 
|---|
| 89 |  | 
|---|
| 90 | // ======================================== Lines on Boundary ================================= | 
|---|
| 91 |  | 
|---|
| 92 | /** Constructor of BoundaryLineSet. | 
|---|
| 93 | */ | 
|---|
| 94 | BoundaryLineSet::BoundaryLineSet() : | 
|---|
| 95 | Nr(-1) | 
|---|
| 96 | { | 
|---|
| 97 | Info FunctionInfo(__func__); | 
|---|
| 98 | for (int i = 0; i < 2; i++) | 
|---|
| 99 | endpoints[i] = NULL; | 
|---|
| 100 | } | 
|---|
| 101 | ; | 
|---|
| 102 |  | 
|---|
| 103 | /** Constructor of BoundaryLineSet with two endpoints. | 
|---|
| 104 | * Adds line automatically to each endpoints' LineMap | 
|---|
| 105 | * \param *Point[2] array of two boundary points | 
|---|
| 106 | * \param number number of the list | 
|---|
| 107 | */ | 
|---|
| 108 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number) | 
|---|
| 109 | { | 
|---|
| 110 | Info FunctionInfo(__func__); | 
|---|
| 111 | // set number | 
|---|
| 112 | Nr = number; | 
|---|
| 113 | // set endpoints in ascending order | 
|---|
| 114 | SetEndpointsOrdered(endpoints, Point[0], Point[1]); | 
|---|
| 115 | // add this line to the hash maps of both endpoints | 
|---|
| 116 | Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding. | 
|---|
| 117 | Point[1]->AddLine(this); // | 
|---|
| 118 | // set skipped to false | 
|---|
| 119 | skipped = false; | 
|---|
| 120 | // clear triangles list | 
|---|
| 121 | DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl); | 
|---|
| 122 | } | 
|---|
| 123 | ; | 
|---|
| 124 |  | 
|---|
| 125 | /** Constructor of BoundaryLineSet with two endpoints. | 
|---|
| 126 | * Adds line automatically to each endpoints' LineMap | 
|---|
| 127 | * \param *Point1 first boundary point | 
|---|
| 128 | * \param *Point2 second boundary point | 
|---|
| 129 | * \param number number of the list | 
|---|
| 130 | */ | 
|---|
| 131 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number) | 
|---|
| 132 | { | 
|---|
| 133 | Info FunctionInfo(__func__); | 
|---|
| 134 | // set number | 
|---|
| 135 | Nr = number; | 
|---|
| 136 | // set endpoints in ascending order | 
|---|
| 137 | SetEndpointsOrdered(endpoints, Point1, Point2); | 
|---|
| 138 | // add this line to the hash maps of both endpoints | 
|---|
| 139 | Point1->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding. | 
|---|
| 140 | Point2->AddLine(this); // | 
|---|
| 141 | // set skipped to false | 
|---|
| 142 | skipped = false; | 
|---|
| 143 | // clear triangles list | 
|---|
| 144 | DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl); | 
|---|
| 145 | } | 
|---|
| 146 | ; | 
|---|
| 147 |  | 
|---|
| 148 | /** Destructor for BoundaryLineSet. | 
|---|
| 149 | * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore. | 
|---|
| 150 | * \note When removing lines from a class Tesselation, use RemoveTesselationLine() | 
|---|
| 151 | */ | 
|---|
| 152 | BoundaryLineSet::~BoundaryLineSet() | 
|---|
| 153 | { | 
|---|
| 154 | Info FunctionInfo(__func__); | 
|---|
| 155 | int Numbers[2]; | 
|---|
| 156 |  | 
|---|
| 157 | // get other endpoint number of finding copies of same line | 
|---|
| 158 | if (endpoints[1] != NULL) | 
|---|
| 159 | Numbers[0] = endpoints[1]->Nr; | 
|---|
| 160 | else | 
|---|
| 161 | Numbers[0] = -1; | 
|---|
| 162 | if (endpoints[0] != NULL) | 
|---|
| 163 | Numbers[1] = endpoints[0]->Nr; | 
|---|
| 164 | else | 
|---|
| 165 | Numbers[1] = -1; | 
|---|
| 166 |  | 
|---|
| 167 | for (int i = 0; i < 2; i++) { | 
|---|
| 168 | if (endpoints[i] != NULL) { | 
|---|
| 169 | if (Numbers[i] != -1) { // as there may be multiple lines with same endpoints, we have to go through each and find in the endpoint's line list this line set | 
|---|
| 170 | pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]); | 
|---|
| 171 | for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++) | 
|---|
| 172 | if ((*Runner).second == this) { | 
|---|
| 173 | //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl; | 
|---|
| 174 | endpoints[i]->lines.erase(Runner); | 
|---|
| 175 | break; | 
|---|
| 176 | } | 
|---|
| 177 | } else { // there's just a single line left | 
|---|
| 178 | if (endpoints[i]->lines.erase(Nr)) { | 
|---|
| 179 | //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl; | 
|---|
| 180 | } | 
|---|
| 181 | } | 
|---|
| 182 | if (endpoints[i]->lines.empty()) { | 
|---|
| 183 | //Log() << Verbose(0) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl; | 
|---|
| 184 | if (endpoints[i] != NULL) { | 
|---|
| 185 | delete (endpoints[i]); | 
|---|
| 186 | endpoints[i] = NULL; | 
|---|
| 187 | } | 
|---|
| 188 | } | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 | if (!triangles.empty()) | 
|---|
| 192 | DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl); | 
|---|
| 193 | } | 
|---|
| 194 | ; | 
|---|
| 195 |  | 
|---|
| 196 | /** Add triangle to TriangleMap of this boundary line. | 
|---|
| 197 | * \param *triangle to add | 
|---|
| 198 | */ | 
|---|
| 199 | void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle) | 
|---|
| 200 | { | 
|---|
| 201 | Info FunctionInfo(__func__); | 
|---|
| 202 | DoLog(0) && (Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl); | 
|---|
| 203 | triangles.insert(TrianglePair(triangle->Nr, triangle)); | 
|---|
| 204 | } | 
|---|
| 205 | ; | 
|---|
| 206 |  | 
|---|
| 207 | /** Checks whether we have a common endpoint with given \a *line. | 
|---|
| 208 | * \param *line other line to test | 
|---|
| 209 | * \return true - common endpoint present, false - not connected | 
|---|
| 210 | */ | 
|---|
| 211 | bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const | 
|---|
| 212 | { | 
|---|
| 213 | Info FunctionInfo(__func__); | 
|---|
| 214 | if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1])) | 
|---|
| 215 | return true; | 
|---|
| 216 | else | 
|---|
| 217 | return false; | 
|---|
| 218 | } | 
|---|
| 219 | ; | 
|---|
| 220 |  | 
|---|
| 221 | /** Checks whether the adjacent triangles of a baseline are convex or not. | 
|---|
| 222 | * We sum the two angles of each height vector with respect to the center of the baseline. | 
|---|
| 223 | * If greater/equal M_PI than we are convex. | 
|---|
| 224 | * \param *out output stream for debugging | 
|---|
| 225 | * \return true - triangles are convex, false - concave or less than two triangles connected | 
|---|
| 226 | */ | 
|---|
| 227 | bool BoundaryLineSet::CheckConvexityCriterion() const | 
|---|
| 228 | { | 
|---|
| 229 | Info FunctionInfo(__func__); | 
|---|
| 230 | Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck; | 
|---|
| 231 | // get the two triangles | 
|---|
| 232 | if (triangles.size() != 2) { | 
|---|
| 233 | DoeLog(0) && (eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl); | 
|---|
| 234 | return true; | 
|---|
| 235 | } | 
|---|
| 236 | // check normal vectors | 
|---|
| 237 | // have a normal vector on the base line pointing outwards | 
|---|
| 238 | //Log() << Verbose(0) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl; | 
|---|
| 239 | BaseLineCenter = (1./2.)*((*endpoints[0]->node->node) + (*endpoints[1]->node->node)); | 
|---|
| 240 | BaseLine = (*endpoints[0]->node->node) - (*endpoints[1]->node->node); | 
|---|
| 241 |  | 
|---|
| 242 | //Log() << Verbose(0) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl; | 
|---|
| 243 |  | 
|---|
| 244 | BaseLineNormal.Zero(); | 
|---|
| 245 | NormalCheck.Zero(); | 
|---|
| 246 | double sign = -1.; | 
|---|
| 247 | int i = 0; | 
|---|
| 248 | class BoundaryPointSet *node = NULL; | 
|---|
| 249 | for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) { | 
|---|
| 250 | //Log() << Verbose(0) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl; | 
|---|
| 251 | NormalCheck += runner->second->NormalVector; | 
|---|
| 252 | NormalCheck *= sign; | 
|---|
| 253 | sign = -sign; | 
|---|
| 254 | if (runner->second->NormalVector.NormSquared() > MYEPSILON) | 
|---|
| 255 | BaseLineNormal = runner->second->NormalVector;   // yes, copy second on top of first | 
|---|
| 256 | else { | 
|---|
| 257 | DoeLog(0) && (eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl); | 
|---|
| 258 | } | 
|---|
| 259 | node = runner->second->GetThirdEndpoint(this); | 
|---|
| 260 | if (node != NULL) { | 
|---|
| 261 | //Log() << Verbose(0) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl; | 
|---|
| 262 | helper[i] = (*node->node->node) - BaseLineCenter; | 
|---|
| 263 | helper[i].MakeNormalTo(BaseLine);  // we want to compare the triangle's heights' angles! | 
|---|
| 264 | //Log() << Verbose(0) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl; | 
|---|
| 265 | i++; | 
|---|
| 266 | } else { | 
|---|
| 267 | DoeLog(1) && (eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl); | 
|---|
| 268 | return true; | 
|---|
| 269 | } | 
|---|
| 270 | } | 
|---|
| 271 | //Log() << Verbose(0) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl; | 
|---|
| 272 | if (NormalCheck.NormSquared() < MYEPSILON) { | 
|---|
| 273 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl); | 
|---|
| 274 | return true; | 
|---|
| 275 | } | 
|---|
| 276 | BaseLineNormal.Scale(-1.); | 
|---|
| 277 | double angle = GetAngle(helper[0], helper[1], BaseLineNormal); | 
|---|
| 278 | if ((angle - M_PI) > -MYEPSILON) { | 
|---|
| 279 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl); | 
|---|
| 280 | return true; | 
|---|
| 281 | } else { | 
|---|
| 282 | DoLog(0) && (Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl); | 
|---|
| 283 | return false; | 
|---|
| 284 | } | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | /** Checks whether point is any of the two endpoints this line contains. | 
|---|
| 288 | * \param *point point to test | 
|---|
| 289 | * \return true - point is of the line, false - is not | 
|---|
| 290 | */ | 
|---|
| 291 | bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const | 
|---|
| 292 | { | 
|---|
| 293 | Info FunctionInfo(__func__); | 
|---|
| 294 | for (int i = 0; i < 2; i++) | 
|---|
| 295 | if (point == endpoints[i]) | 
|---|
| 296 | return true; | 
|---|
| 297 | return false; | 
|---|
| 298 | } | 
|---|
| 299 | ; | 
|---|
| 300 |  | 
|---|
| 301 | /** Returns other endpoint of the line. | 
|---|
| 302 | * \param *point other endpoint | 
|---|
| 303 | * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise | 
|---|
| 304 | */ | 
|---|
| 305 | class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const | 
|---|
| 306 | { | 
|---|
| 307 | Info FunctionInfo(__func__); | 
|---|
| 308 | if (endpoints[0] == point) | 
|---|
| 309 | return endpoints[1]; | 
|---|
| 310 | else if (endpoints[1] == point) | 
|---|
| 311 | return endpoints[0]; | 
|---|
| 312 | else | 
|---|
| 313 | return NULL; | 
|---|
| 314 | } | 
|---|
| 315 | ; | 
|---|
| 316 |  | 
|---|
| 317 | /** output operator for BoundaryLineSet. | 
|---|
| 318 | * \param &ost output stream | 
|---|
| 319 | * \param &a boundary line | 
|---|
| 320 | */ | 
|---|
| 321 | ostream & operator <<(ostream &ost, const BoundaryLineSet &a) | 
|---|
| 322 | { | 
|---|
| 323 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "]"; | 
|---|
| 324 | return ost; | 
|---|
| 325 | } | 
|---|
| 326 | ; | 
|---|
| 327 |  | 
|---|
| 328 | // ======================================== Triangles on Boundary ================================= | 
|---|
| 329 |  | 
|---|
| 330 | /** Constructor for BoundaryTriangleSet. | 
|---|
| 331 | */ | 
|---|
| 332 | BoundaryTriangleSet::BoundaryTriangleSet() : | 
|---|
| 333 | Nr(-1) | 
|---|
| 334 | { | 
|---|
| 335 | Info FunctionInfo(__func__); | 
|---|
| 336 | for (int i = 0; i < 3; i++) { | 
|---|
| 337 | endpoints[i] = NULL; | 
|---|
| 338 | lines[i] = NULL; | 
|---|
| 339 | } | 
|---|
| 340 | } | 
|---|
| 341 | ; | 
|---|
| 342 |  | 
|---|
| 343 | /** Constructor for BoundaryTriangleSet with three lines. | 
|---|
| 344 | * \param *line[3] lines that make up the triangle | 
|---|
| 345 | * \param number number of triangle | 
|---|
| 346 | */ | 
|---|
| 347 | BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number) : | 
|---|
| 348 | Nr(number) | 
|---|
| 349 | { | 
|---|
| 350 | Info FunctionInfo(__func__); | 
|---|
| 351 | // set number | 
|---|
| 352 | // set lines | 
|---|
| 353 | for (int i = 0; i < 3; i++) { | 
|---|
| 354 | lines[i] = line[i]; | 
|---|
| 355 | lines[i]->AddTriangle(this); | 
|---|
| 356 | } | 
|---|
| 357 | // get ascending order of endpoints | 
|---|
| 358 | PointMap OrderMap; | 
|---|
| 359 | for (int i = 0; i < 3; i++) | 
|---|
| 360 | // for all three lines | 
|---|
| 361 | for (int j = 0; j < 2; j++) { // for both endpoints | 
|---|
| 362 | OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j])); | 
|---|
| 363 | // and we don't care whether insertion fails | 
|---|
| 364 | } | 
|---|
| 365 | // set endpoints | 
|---|
| 366 | int Counter = 0; | 
|---|
| 367 | DoLog(0) && (Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl); | 
|---|
| 368 | for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) { | 
|---|
| 369 | endpoints[Counter] = runner->second; | 
|---|
| 370 | DoLog(0) && (Log() << Verbose(0) << " " << *endpoints[Counter] << endl); | 
|---|
| 371 | Counter++; | 
|---|
| 372 | } | 
|---|
| 373 | if (Counter < 3) { | 
|---|
| 374 | DoeLog(0) && (eLog() << Verbose(0) << "We have a triangle with only two distinct endpoints!" << endl); | 
|---|
| 375 | performCriticalExit(); | 
|---|
| 376 | } | 
|---|
| 377 | } | 
|---|
| 378 | ; | 
|---|
| 379 |  | 
|---|
| 380 | /** Destructor of BoundaryTriangleSet. | 
|---|
| 381 | * Removes itself from each of its lines' LineMap and removes them if necessary. | 
|---|
| 382 | * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle() | 
|---|
| 383 | */ | 
|---|
| 384 | BoundaryTriangleSet::~BoundaryTriangleSet() | 
|---|
| 385 | { | 
|---|
| 386 | Info FunctionInfo(__func__); | 
|---|
| 387 | for (int i = 0; i < 3; i++) { | 
|---|
| 388 | if (lines[i] != NULL) { | 
|---|
| 389 | if (lines[i]->triangles.erase(Nr)) { | 
|---|
| 390 | //Log() << Verbose(0) << "Triangle Nr." << Nr << " erased in line " << *lines[i] << "." << endl; | 
|---|
| 391 | } | 
|---|
| 392 | if (lines[i]->triangles.empty()) { | 
|---|
| 393 | //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl; | 
|---|
| 394 | delete (lines[i]); | 
|---|
| 395 | lines[i] = NULL; | 
|---|
| 396 | } | 
|---|
| 397 | } | 
|---|
| 398 | } | 
|---|
| 399 | //Log() << Verbose(0) << "Erasing triangle Nr." << Nr << " itself." << endl; | 
|---|
| 400 | } | 
|---|
| 401 | ; | 
|---|
| 402 |  | 
|---|
| 403 | /** Calculates the normal vector for this triangle. | 
|---|
| 404 | * Is made unique by comparison with \a OtherVector to point in the other direction. | 
|---|
| 405 | * \param &OtherVector direction vector to make normal vector unique. | 
|---|
| 406 | */ | 
|---|
| 407 | void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector) | 
|---|
| 408 | { | 
|---|
| 409 | Info FunctionInfo(__func__); | 
|---|
| 410 | // get normal vector | 
|---|
| 411 | NormalVector = Plane(*(endpoints[0]->node->node), | 
|---|
| 412 | *(endpoints[1]->node->node), | 
|---|
| 413 | *(endpoints[2]->node->node)).getNormal(); | 
|---|
| 414 |  | 
|---|
| 415 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices) | 
|---|
| 416 | if (NormalVector.ScalarProduct(OtherVector) > 0.) | 
|---|
| 417 | NormalVector.Scale(-1.); | 
|---|
| 418 | DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl); | 
|---|
| 419 | } | 
|---|
| 420 | ; | 
|---|
| 421 |  | 
|---|
| 422 | /** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses. | 
|---|
| 423 | * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane | 
|---|
| 424 | * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not. | 
|---|
| 425 | * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line | 
|---|
| 426 | * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between | 
|---|
| 427 | * the first two basepoints) or not. | 
|---|
| 428 | * \param *out output stream for debugging | 
|---|
| 429 | * \param *MolCenter offset vector of line | 
|---|
| 430 | * \param *x second endpoint of line, minus \a *MolCenter is directional vector of line | 
|---|
| 431 | * \param *Intersection intersection on plane on return | 
|---|
| 432 | * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle. | 
|---|
| 433 | */ | 
|---|
| 434 |  | 
|---|
| 435 | bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector * const MolCenter, const Vector * const x, Vector * const Intersection) const | 
|---|
| 436 | { | 
|---|
| 437 | Info FunctionInfo(__func__); | 
|---|
| 438 | Vector CrossPoint; | 
|---|
| 439 | Vector helper; | 
|---|
| 440 |  | 
|---|
| 441 | try { | 
|---|
| 442 | *Intersection = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*MolCenter, *x); | 
|---|
| 443 | } | 
|---|
| 444 | catch (LinearDependenceException &excp) { | 
|---|
| 445 | Log() << Verbose(1) << excp; | 
|---|
| 446 | DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl); | 
|---|
| 447 | return false; | 
|---|
| 448 | } | 
|---|
| 449 |  | 
|---|
| 450 | DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl); | 
|---|
| 451 | DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << *MolCenter << " to " << *x << "." << endl); | 
|---|
| 452 | DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << *Intersection << "." << endl); | 
|---|
| 453 |  | 
|---|
| 454 | if (Intersection->DistanceSquared(*endpoints[0]->node->node) < MYEPSILON) { | 
|---|
| 455 | DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl); | 
|---|
| 456 | return true; | 
|---|
| 457 | }   else if (Intersection->DistanceSquared(*endpoints[1]->node->node) < MYEPSILON) { | 
|---|
| 458 | DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl); | 
|---|
| 459 | return true; | 
|---|
| 460 | }   else if (Intersection->DistanceSquared(*endpoints[2]->node->node) < MYEPSILON) { | 
|---|
| 461 | DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl); | 
|---|
| 462 | return true; | 
|---|
| 463 | } | 
|---|
| 464 | // Calculate cross point between one baseline and the line from the third endpoint to intersection | 
|---|
| 465 | int i = 0; | 
|---|
| 466 | do { | 
|---|
| 467 | try { | 
|---|
| 468 | CrossPoint = GetIntersectionOfTwoLinesOnPlane(*(endpoints[i%3]->node->node), | 
|---|
| 469 | *(endpoints[(i+1)%3]->node->node), | 
|---|
| 470 | *(endpoints[(i+2)%3]->node->node), | 
|---|
| 471 | *Intersection); | 
|---|
| 472 | helper = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node); | 
|---|
| 473 | CrossPoint -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector | 
|---|
| 474 | const double s = CrossPoint.ScalarProduct(helper)/helper.NormSquared(); | 
|---|
| 475 | DoLog(1) && (Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl); | 
|---|
| 476 | if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) { | 
|---|
| 477 | DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl); | 
|---|
| 478 | i=4; | 
|---|
| 479 | break; | 
|---|
| 480 | } | 
|---|
| 481 | i++; | 
|---|
| 482 | } catch (LinearDependenceException &excp){ | 
|---|
| 483 | break; | 
|---|
| 484 | } | 
|---|
| 485 | } while (i < 3); | 
|---|
| 486 | if (i == 3) { | 
|---|
| 487 | DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl); | 
|---|
| 488 | return true; | 
|---|
| 489 | } else { | 
|---|
| 490 | DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " outside of triangle." << endl); | 
|---|
| 491 | return false; | 
|---|
| 492 | } | 
|---|
| 493 | } | 
|---|
| 494 | ; | 
|---|
| 495 |  | 
|---|
| 496 | /** Finds the point on the triangle to the point \a *x. | 
|---|
| 497 | * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point. | 
|---|
| 498 | * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the | 
|---|
| 499 | * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down. | 
|---|
| 500 | * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not. | 
|---|
| 501 | * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line | 
|---|
| 502 | * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between | 
|---|
| 503 | * the first two basepoints) or not. | 
|---|
| 504 | * \param *x point | 
|---|
| 505 | * \param *ClosestPoint desired closest point inside triangle to \a *x, is absolute vector | 
|---|
| 506 | * \return Distance squared between \a *x and closest point inside triangle | 
|---|
| 507 | */ | 
|---|
| 508 | double BoundaryTriangleSet::GetClosestPointInsideTriangle(const Vector * const x, Vector * const ClosestPoint) const | 
|---|
| 509 | { | 
|---|
| 510 | Info FunctionInfo(__func__); | 
|---|
| 511 | Vector Direction; | 
|---|
| 512 |  | 
|---|
| 513 | // 1. get intersection with plane | 
|---|
| 514 | DoLog(1) && (Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << *x << "." << endl); | 
|---|
| 515 | GetCenter(&Direction); | 
|---|
| 516 | try { | 
|---|
| 517 | *ClosestPoint = Plane(NormalVector, *(endpoints[0]->node->node)).GetIntersection(*x, Direction); | 
|---|
| 518 | } | 
|---|
| 519 | catch (LinearDependenceException &excp) { | 
|---|
| 520 | (*ClosestPoint) = (*x); | 
|---|
| 521 | } | 
|---|
| 522 |  | 
|---|
| 523 | // 2. Calculate in plane part of line (x, intersection) | 
|---|
| 524 | Vector InPlane = (*x) - (*ClosestPoint); // points from plane intersection to straight-down point | 
|---|
| 525 | InPlane.ProjectOntoPlane(NormalVector); | 
|---|
| 526 | InPlane += *ClosestPoint; | 
|---|
| 527 |  | 
|---|
| 528 | DoLog(2) && (Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl); | 
|---|
| 529 | DoLog(2) && (Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << *x << "." << endl); | 
|---|
| 530 | DoLog(2) && (Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl); | 
|---|
| 531 |  | 
|---|
| 532 | // Calculate cross point between one baseline and the desired point such that distance is shortest | 
|---|
| 533 | double ShortestDistance = -1.; | 
|---|
| 534 | bool InsideFlag = false; | 
|---|
| 535 | Vector CrossDirection[3]; | 
|---|
| 536 | Vector CrossPoint[3]; | 
|---|
| 537 | Vector helper; | 
|---|
| 538 | for (int i = 0; i < 3; i++) { | 
|---|
| 539 | // treat direction of line as normal of a (cut)plane and the desired point x as the plane offset, the intersect line with point | 
|---|
| 540 | Direction = (*endpoints[(i+1)%3]->node->node) - (*endpoints[i%3]->node->node); | 
|---|
| 541 | // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal); | 
|---|
| 542 | CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(*(endpoints[i%3]->node->node), *(endpoints[(i+1)%3]->node->node)); | 
|---|
| 543 | CrossDirection[i] = CrossPoint[i] - InPlane; | 
|---|
| 544 | CrossPoint[i] -= (*endpoints[i%3]->node->node);  // cross point was returned as absolute vector | 
|---|
| 545 | const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared(); | 
|---|
| 546 | DoLog(2) && (Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl); | 
|---|
| 547 | if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) { | 
|---|
| 548 | CrossPoint[i] += (*endpoints[i%3]->node->node);  // make cross point absolute again | 
|---|
| 549 | DoLog(2) && (Log() << Verbose(2) << "INFO: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between " << *endpoints[i % 3]->node->node << " and " << *endpoints[(i + 1) % 3]->node->node << "." << endl); | 
|---|
| 550 | const double distance = CrossPoint[i].DistanceSquared(*x); | 
|---|
| 551 | if ((ShortestDistance < 0.) || (ShortestDistance > distance)) { | 
|---|
| 552 | ShortestDistance = distance; | 
|---|
| 553 | (*ClosestPoint) = CrossPoint[i]; | 
|---|
| 554 | } | 
|---|
| 555 | } else | 
|---|
| 556 | CrossPoint[i].Zero(); | 
|---|
| 557 | } | 
|---|
| 558 | InsideFlag = true; | 
|---|
| 559 | for (int i = 0; i < 3; i++) { | 
|---|
| 560 | const double sign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 1) % 3]); | 
|---|
| 561 | const double othersign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 2) % 3]); | 
|---|
| 562 |  | 
|---|
| 563 | if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign | 
|---|
| 564 | InsideFlag = false; | 
|---|
| 565 | } | 
|---|
| 566 | if (InsideFlag) { | 
|---|
| 567 | (*ClosestPoint) = InPlane; | 
|---|
| 568 | ShortestDistance = InPlane.DistanceSquared(*x); | 
|---|
| 569 | } else { // also check endnodes | 
|---|
| 570 | for (int i = 0; i < 3; i++) { | 
|---|
| 571 | const double distance = x->DistanceSquared(*endpoints[i]->node->node); | 
|---|
| 572 | if ((ShortestDistance < 0.) || (ShortestDistance > distance)) { | 
|---|
| 573 | ShortestDistance = distance; | 
|---|
| 574 | (*ClosestPoint) = (*endpoints[i]->node->node); | 
|---|
| 575 | } | 
|---|
| 576 | } | 
|---|
| 577 | } | 
|---|
| 578 | DoLog(1) && (Log() << Verbose(1) << "INFO: Closest Point is " << *ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl); | 
|---|
| 579 | return ShortestDistance; | 
|---|
| 580 | } | 
|---|
| 581 | ; | 
|---|
| 582 |  | 
|---|
| 583 | /** Checks whether lines is any of the three boundary lines this triangle contains. | 
|---|
| 584 | * \param *line line to test | 
|---|
| 585 | * \return true - line is of the triangle, false - is not | 
|---|
| 586 | */ | 
|---|
| 587 | bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const | 
|---|
| 588 | { | 
|---|
| 589 | Info FunctionInfo(__func__); | 
|---|
| 590 | for (int i = 0; i < 3; i++) | 
|---|
| 591 | if (line == lines[i]) | 
|---|
| 592 | return true; | 
|---|
| 593 | return false; | 
|---|
| 594 | } | 
|---|
| 595 | ; | 
|---|
| 596 |  | 
|---|
| 597 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 598 | * \param *point point to test | 
|---|
| 599 | * \return true - point is of the triangle, false - is not | 
|---|
| 600 | */ | 
|---|
| 601 | bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const | 
|---|
| 602 | { | 
|---|
| 603 | Info FunctionInfo(__func__); | 
|---|
| 604 | for (int i = 0; i < 3; i++) | 
|---|
| 605 | if (point == endpoints[i]) | 
|---|
| 606 | return true; | 
|---|
| 607 | return false; | 
|---|
| 608 | } | 
|---|
| 609 | ; | 
|---|
| 610 |  | 
|---|
| 611 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 612 | * \param *point TesselPoint to test | 
|---|
| 613 | * \return true - point is of the triangle, false - is not | 
|---|
| 614 | */ | 
|---|
| 615 | bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const | 
|---|
| 616 | { | 
|---|
| 617 | Info FunctionInfo(__func__); | 
|---|
| 618 | for (int i = 0; i < 3; i++) | 
|---|
| 619 | if (point == endpoints[i]->node) | 
|---|
| 620 | return true; | 
|---|
| 621 | return false; | 
|---|
| 622 | } | 
|---|
| 623 | ; | 
|---|
| 624 |  | 
|---|
| 625 | /** Checks whether three given \a *Points coincide with triangle's endpoints. | 
|---|
| 626 | * \param *Points[3] pointer to BoundaryPointSet | 
|---|
| 627 | * \return true - is the very triangle, false - is not | 
|---|
| 628 | */ | 
|---|
| 629 | bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const | 
|---|
| 630 | { | 
|---|
| 631 | Info FunctionInfo(__func__); | 
|---|
| 632 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl); | 
|---|
| 633 | return (((endpoints[0] == Points[0]) || (endpoints[0] == Points[1]) || (endpoints[0] == Points[2])) && ((endpoints[1] == Points[0]) || (endpoints[1] == Points[1]) || (endpoints[1] == Points[2])) && ((endpoints[2] == Points[0]) || (endpoints[2] == Points[1]) || (endpoints[2] == Points[2]) | 
|---|
| 634 |  | 
|---|
| 635 | )); | 
|---|
| 636 | } | 
|---|
| 637 | ; | 
|---|
| 638 |  | 
|---|
| 639 | /** Checks whether three given \a *Points coincide with triangle's endpoints. | 
|---|
| 640 | * \param *Points[3] pointer to BoundaryPointSet | 
|---|
| 641 | * \return true - is the very triangle, false - is not | 
|---|
| 642 | */ | 
|---|
| 643 | bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const | 
|---|
| 644 | { | 
|---|
| 645 | Info FunctionInfo(__func__); | 
|---|
| 646 | return (((endpoints[0] == T->endpoints[0]) || (endpoints[0] == T->endpoints[1]) || (endpoints[0] == T->endpoints[2])) && ((endpoints[1] == T->endpoints[0]) || (endpoints[1] == T->endpoints[1]) || (endpoints[1] == T->endpoints[2])) && ((endpoints[2] == T->endpoints[0]) || (endpoints[2] == T->endpoints[1]) || (endpoints[2] == T->endpoints[2]) | 
|---|
| 647 |  | 
|---|
| 648 | )); | 
|---|
| 649 | } | 
|---|
| 650 | ; | 
|---|
| 651 |  | 
|---|
| 652 | /** Returns the endpoint which is not contained in the given \a *line. | 
|---|
| 653 | * \param *line baseline defining two endpoints | 
|---|
| 654 | * \return pointer third endpoint or NULL if line does not belong to triangle. | 
|---|
| 655 | */ | 
|---|
| 656 | class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const | 
|---|
| 657 | { | 
|---|
| 658 | Info FunctionInfo(__func__); | 
|---|
| 659 | // sanity check | 
|---|
| 660 | if (!ContainsBoundaryLine(line)) | 
|---|
| 661 | return NULL; | 
|---|
| 662 | for (int i = 0; i < 3; i++) | 
|---|
| 663 | if (!line->ContainsBoundaryPoint(endpoints[i])) | 
|---|
| 664 | return endpoints[i]; | 
|---|
| 665 | // actually, that' impossible :) | 
|---|
| 666 | return NULL; | 
|---|
| 667 | } | 
|---|
| 668 | ; | 
|---|
| 669 |  | 
|---|
| 670 | /** Calculates the center point of the triangle. | 
|---|
| 671 | * Is third of the sum of all endpoints. | 
|---|
| 672 | * \param *center central point on return. | 
|---|
| 673 | */ | 
|---|
| 674 | void BoundaryTriangleSet::GetCenter(Vector * const center) const | 
|---|
| 675 | { | 
|---|
| 676 | Info FunctionInfo(__func__); | 
|---|
| 677 | center->Zero(); | 
|---|
| 678 | for (int i = 0; i < 3; i++) | 
|---|
| 679 | (*center) += (*endpoints[i]->node->node); | 
|---|
| 680 | center->Scale(1. / 3.); | 
|---|
| 681 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center is at " << *center << "." << endl); | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | /** output operator for BoundaryTriangleSet. | 
|---|
| 685 | * \param &ost output stream | 
|---|
| 686 | * \param &a boundary triangle | 
|---|
| 687 | */ | 
|---|
| 688 | ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a) | 
|---|
| 689 | { | 
|---|
| 690 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]"; | 
|---|
| 691 | //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," | 
|---|
| 692 | //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]"; | 
|---|
| 693 | return ost; | 
|---|
| 694 | } | 
|---|
| 695 | ; | 
|---|
| 696 |  | 
|---|
| 697 | // ======================================== Polygons on Boundary ================================= | 
|---|
| 698 |  | 
|---|
| 699 | /** Constructor for BoundaryPolygonSet. | 
|---|
| 700 | */ | 
|---|
| 701 | BoundaryPolygonSet::BoundaryPolygonSet() : | 
|---|
| 702 | Nr(-1) | 
|---|
| 703 | { | 
|---|
| 704 | Info FunctionInfo(__func__); | 
|---|
| 705 | } | 
|---|
| 706 | ; | 
|---|
| 707 |  | 
|---|
| 708 | /** Destructor of BoundaryPolygonSet. | 
|---|
| 709 | * Just clears endpoints. | 
|---|
| 710 | * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle() | 
|---|
| 711 | */ | 
|---|
| 712 | BoundaryPolygonSet::~BoundaryPolygonSet() | 
|---|
| 713 | { | 
|---|
| 714 | Info FunctionInfo(__func__); | 
|---|
| 715 | endpoints.clear(); | 
|---|
| 716 | DoLog(1) && (Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl); | 
|---|
| 717 | } | 
|---|
| 718 | ; | 
|---|
| 719 |  | 
|---|
| 720 | /** Calculates the normal vector for this triangle. | 
|---|
| 721 | * Is made unique by comparison with \a OtherVector to point in the other direction. | 
|---|
| 722 | * \param &OtherVector direction vector to make normal vector unique. | 
|---|
| 723 | * \return allocated vector in normal direction | 
|---|
| 724 | */ | 
|---|
| 725 | Vector * BoundaryPolygonSet::GetNormalVector(const Vector &OtherVector) const | 
|---|
| 726 | { | 
|---|
| 727 | Info FunctionInfo(__func__); | 
|---|
| 728 | // get normal vector | 
|---|
| 729 | Vector TemporaryNormal; | 
|---|
| 730 | Vector *TotalNormal = new Vector; | 
|---|
| 731 | PointSet::const_iterator Runner[3]; | 
|---|
| 732 | for (int i = 0; i < 3; i++) { | 
|---|
| 733 | Runner[i] = endpoints.begin(); | 
|---|
| 734 | for (int j = 0; j < i; j++) { // go as much further | 
|---|
| 735 | Runner[i]++; | 
|---|
| 736 | if (Runner[i] == endpoints.end()) { | 
|---|
| 737 | DoeLog(0) && (eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl); | 
|---|
| 738 | performCriticalExit(); | 
|---|
| 739 | } | 
|---|
| 740 | } | 
|---|
| 741 | } | 
|---|
| 742 | TotalNormal->Zero(); | 
|---|
| 743 | int counter = 0; | 
|---|
| 744 | for (; Runner[2] != endpoints.end();) { | 
|---|
| 745 | TemporaryNormal = Plane(*((*Runner[0])->node->node), | 
|---|
| 746 | *((*Runner[1])->node->node), | 
|---|
| 747 | *((*Runner[2])->node->node)).getNormal(); | 
|---|
| 748 | for (int i = 0; i < 3; i++) // increase each of them | 
|---|
| 749 | Runner[i]++; | 
|---|
| 750 | (*TotalNormal) += TemporaryNormal; | 
|---|
| 751 | } | 
|---|
| 752 | TotalNormal->Scale(1. / (double) counter); | 
|---|
| 753 |  | 
|---|
| 754 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices) | 
|---|
| 755 | if (TotalNormal->ScalarProduct(OtherVector) > 0.) | 
|---|
| 756 | TotalNormal->Scale(-1.); | 
|---|
| 757 | DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl); | 
|---|
| 758 |  | 
|---|
| 759 | return TotalNormal; | 
|---|
| 760 | } | 
|---|
| 761 | ; | 
|---|
| 762 |  | 
|---|
| 763 | /** Calculates the center point of the triangle. | 
|---|
| 764 | * Is third of the sum of all endpoints. | 
|---|
| 765 | * \param *center central point on return. | 
|---|
| 766 | */ | 
|---|
| 767 | void BoundaryPolygonSet::GetCenter(Vector * const center) const | 
|---|
| 768 | { | 
|---|
| 769 | Info FunctionInfo(__func__); | 
|---|
| 770 | center->Zero(); | 
|---|
| 771 | int counter = 0; | 
|---|
| 772 | for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
| 773 | (*center) += (*(*Runner)->node->node); | 
|---|
| 774 | counter++; | 
|---|
| 775 | } | 
|---|
| 776 | center->Scale(1. / (double) counter); | 
|---|
| 777 | DoLog(1) && (Log() << Verbose(1) << "Center is at " << *center << "." << endl); | 
|---|
| 778 | } | 
|---|
| 779 |  | 
|---|
| 780 | /** Checks whether the polygons contains all three endpoints of the triangle. | 
|---|
| 781 | * \param *triangle triangle to test | 
|---|
| 782 | * \return true - triangle is contained polygon, false - is not | 
|---|
| 783 | */ | 
|---|
| 784 | bool BoundaryPolygonSet::ContainsBoundaryTriangle(const BoundaryTriangleSet * const triangle) const | 
|---|
| 785 | { | 
|---|
| 786 | Info FunctionInfo(__func__); | 
|---|
| 787 | return ContainsPresentTupel(triangle->endpoints, 3); | 
|---|
| 788 | } | 
|---|
| 789 | ; | 
|---|
| 790 |  | 
|---|
| 791 | /** Checks whether the polygons contains both endpoints of the line. | 
|---|
| 792 | * \param *line line to test | 
|---|
| 793 | * \return true - line is of the triangle, false - is not | 
|---|
| 794 | */ | 
|---|
| 795 | bool BoundaryPolygonSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const | 
|---|
| 796 | { | 
|---|
| 797 | Info FunctionInfo(__func__); | 
|---|
| 798 | return ContainsPresentTupel(line->endpoints, 2); | 
|---|
| 799 | } | 
|---|
| 800 | ; | 
|---|
| 801 |  | 
|---|
| 802 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 803 | * \param *point point to test | 
|---|
| 804 | * \return true - point is of the triangle, false - is not | 
|---|
| 805 | */ | 
|---|
| 806 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const | 
|---|
| 807 | { | 
|---|
| 808 | Info FunctionInfo(__func__); | 
|---|
| 809 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
| 810 | DoLog(0) && (Log() << Verbose(0) << "Checking against " << **Runner << endl); | 
|---|
| 811 | if (point == (*Runner)) { | 
|---|
| 812 | DoLog(0) && (Log() << Verbose(0) << " Contained." << endl); | 
|---|
| 813 | return true; | 
|---|
| 814 | } | 
|---|
| 815 | } | 
|---|
| 816 | DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl); | 
|---|
| 817 | return false; | 
|---|
| 818 | } | 
|---|
| 819 | ; | 
|---|
| 820 |  | 
|---|
| 821 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 822 | * \param *point TesselPoint to test | 
|---|
| 823 | * \return true - point is of the triangle, false - is not | 
|---|
| 824 | */ | 
|---|
| 825 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const | 
|---|
| 826 | { | 
|---|
| 827 | Info FunctionInfo(__func__); | 
|---|
| 828 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) | 
|---|
| 829 | if (point == (*Runner)->node) { | 
|---|
| 830 | DoLog(0) && (Log() << Verbose(0) << " Contained." << endl); | 
|---|
| 831 | return true; | 
|---|
| 832 | } | 
|---|
| 833 | DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl); | 
|---|
| 834 | return false; | 
|---|
| 835 | } | 
|---|
| 836 | ; | 
|---|
| 837 |  | 
|---|
| 838 | /** Checks whether given array of \a *Points coincide with polygons's endpoints. | 
|---|
| 839 | * \param **Points pointer to an array of BoundaryPointSet | 
|---|
| 840 | * \param dim dimension of array | 
|---|
| 841 | * \return true - set of points is contained in polygon, false - is not | 
|---|
| 842 | */ | 
|---|
| 843 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const | 
|---|
| 844 | { | 
|---|
| 845 | Info FunctionInfo(__func__); | 
|---|
| 846 | int counter = 0; | 
|---|
| 847 | DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl); | 
|---|
| 848 | for (int i = 0; i < dim; i++) { | 
|---|
| 849 | DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl); | 
|---|
| 850 | if (ContainsBoundaryPoint(Points[i])) { | 
|---|
| 851 | counter++; | 
|---|
| 852 | } | 
|---|
| 853 | } | 
|---|
| 854 |  | 
|---|
| 855 | if (counter == dim) | 
|---|
| 856 | return true; | 
|---|
| 857 | else | 
|---|
| 858 | return false; | 
|---|
| 859 | } | 
|---|
| 860 | ; | 
|---|
| 861 |  | 
|---|
| 862 | /** Checks whether given PointList coincide with polygons's endpoints. | 
|---|
| 863 | * \param &endpoints PointList | 
|---|
| 864 | * \return true - set of points is contained in polygon, false - is not | 
|---|
| 865 | */ | 
|---|
| 866 | bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const | 
|---|
| 867 | { | 
|---|
| 868 | Info FunctionInfo(__func__); | 
|---|
| 869 | size_t counter = 0; | 
|---|
| 870 | DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl); | 
|---|
| 871 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
| 872 | DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << **Runner << endl); | 
|---|
| 873 | if (ContainsBoundaryPoint(*Runner)) | 
|---|
| 874 | counter++; | 
|---|
| 875 | } | 
|---|
| 876 |  | 
|---|
| 877 | if (counter == endpoints.size()) | 
|---|
| 878 | return true; | 
|---|
| 879 | else | 
|---|
| 880 | return false; | 
|---|
| 881 | } | 
|---|
| 882 | ; | 
|---|
| 883 |  | 
|---|
| 884 | /** Checks whether given set of \a *Points coincide with polygons's endpoints. | 
|---|
| 885 | * \param *P pointer to BoundaryPolygonSet | 
|---|
| 886 | * \return true - is the very triangle, false - is not | 
|---|
| 887 | */ | 
|---|
| 888 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const | 
|---|
| 889 | { | 
|---|
| 890 | return ContainsPresentTupel((const PointSet) P->endpoints); | 
|---|
| 891 | } | 
|---|
| 892 | ; | 
|---|
| 893 |  | 
|---|
| 894 | /** Gathers all the endpoints' triangles in a unique set. | 
|---|
| 895 | * \return set of all triangles | 
|---|
| 896 | */ | 
|---|
| 897 | TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const | 
|---|
| 898 | { | 
|---|
| 899 | Info FunctionInfo(__func__); | 
|---|
| 900 | pair<TriangleSet::iterator, bool> Tester; | 
|---|
| 901 | TriangleSet *triangles = new TriangleSet; | 
|---|
| 902 |  | 
|---|
| 903 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) | 
|---|
| 904 | for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++) | 
|---|
| 905 | for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) { | 
|---|
| 906 | //Log() << Verbose(0) << " Testing triangle " << *(Sprinter->second) << endl; | 
|---|
| 907 | if (ContainsBoundaryTriangle(Sprinter->second)) { | 
|---|
| 908 | Tester = triangles->insert(Sprinter->second); | 
|---|
| 909 | if (Tester.second) | 
|---|
| 910 | DoLog(0) && (Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl); | 
|---|
| 911 | } | 
|---|
| 912 | } | 
|---|
| 913 |  | 
|---|
| 914 | DoLog(1) && (Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl); | 
|---|
| 915 | return triangles; | 
|---|
| 916 | } | 
|---|
| 917 | ; | 
|---|
| 918 |  | 
|---|
| 919 | /** Fills the endpoints of this polygon from the triangles attached to \a *line. | 
|---|
| 920 | * \param *line lines with triangles attached | 
|---|
| 921 | * \return true - polygon contains endpoints, false - line was NULL | 
|---|
| 922 | */ | 
|---|
| 923 | bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line) | 
|---|
| 924 | { | 
|---|
| 925 | Info FunctionInfo(__func__); | 
|---|
| 926 | pair<PointSet::iterator, bool> Tester; | 
|---|
| 927 | if (line == NULL) | 
|---|
| 928 | return false; | 
|---|
| 929 | DoLog(1) && (Log() << Verbose(1) << "Filling polygon from line " << *line << endl); | 
|---|
| 930 | for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) { | 
|---|
| 931 | for (int i = 0; i < 3; i++) { | 
|---|
| 932 | Tester = endpoints.insert((Runner->second)->endpoints[i]); | 
|---|
| 933 | if (Tester.second) | 
|---|
| 934 | DoLog(1) && (Log() << Verbose(1) << "  Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl); | 
|---|
| 935 | } | 
|---|
| 936 | } | 
|---|
| 937 |  | 
|---|
| 938 | return true; | 
|---|
| 939 | } | 
|---|
| 940 | ; | 
|---|
| 941 |  | 
|---|
| 942 | /** output operator for BoundaryPolygonSet. | 
|---|
| 943 | * \param &ost output stream | 
|---|
| 944 | * \param &a boundary polygon | 
|---|
| 945 | */ | 
|---|
| 946 | ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a) | 
|---|
| 947 | { | 
|---|
| 948 | ost << "[" << a.Nr << "|"; | 
|---|
| 949 | for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) { | 
|---|
| 950 | ost << (*Runner)->node->Name; | 
|---|
| 951 | Runner++; | 
|---|
| 952 | if (Runner != a.endpoints.end()) | 
|---|
| 953 | ost << ","; | 
|---|
| 954 | } | 
|---|
| 955 | ost << "]"; | 
|---|
| 956 | return ost; | 
|---|
| 957 | } | 
|---|
| 958 | ; | 
|---|
| 959 |  | 
|---|
| 960 | // =========================================================== class TESSELPOINT =========================================== | 
|---|
| 961 |  | 
|---|
| 962 | /** Constructor of class TesselPoint. | 
|---|
| 963 | */ | 
|---|
| 964 | TesselPoint::TesselPoint() | 
|---|
| 965 | { | 
|---|
| 966 | //Info FunctionInfo(__func__); | 
|---|
| 967 | node = NULL; | 
|---|
| 968 | nr = -1; | 
|---|
| 969 | Name = NULL; | 
|---|
| 970 | } | 
|---|
| 971 | ; | 
|---|
| 972 |  | 
|---|
| 973 | /** Destructor for class TesselPoint. | 
|---|
| 974 | */ | 
|---|
| 975 | TesselPoint::~TesselPoint() | 
|---|
| 976 | { | 
|---|
| 977 | //Info FunctionInfo(__func__); | 
|---|
| 978 | } | 
|---|
| 979 | ; | 
|---|
| 980 |  | 
|---|
| 981 | /** Prints LCNode to screen. | 
|---|
| 982 | */ | 
|---|
| 983 | ostream & operator <<(ostream &ost, const TesselPoint &a) | 
|---|
| 984 | { | 
|---|
| 985 | ost << "[" << (a.Name) << "|" << a.Name << " at " << *a.node << "]"; | 
|---|
| 986 | return ost; | 
|---|
| 987 | } | 
|---|
| 988 | ; | 
|---|
| 989 |  | 
|---|
| 990 | /** Prints LCNode to screen. | 
|---|
| 991 | */ | 
|---|
| 992 | ostream & TesselPoint::operator <<(ostream &ost) | 
|---|
| 993 | { | 
|---|
| 994 | Info FunctionInfo(__func__); | 
|---|
| 995 | ost << "[" << (nr) << "|" << this << "]"; | 
|---|
| 996 | return ost; | 
|---|
| 997 | } | 
|---|
| 998 | ; | 
|---|
| 999 |  | 
|---|
| 1000 | // =========================================================== class POINTCLOUD ============================================ | 
|---|
| 1001 |  | 
|---|
| 1002 | /** Constructor of class PointCloud. | 
|---|
| 1003 | */ | 
|---|
| 1004 | PointCloud::PointCloud() | 
|---|
| 1005 | { | 
|---|
| 1006 | //Info FunctionInfo(__func__); | 
|---|
| 1007 | } | 
|---|
| 1008 | ; | 
|---|
| 1009 |  | 
|---|
| 1010 | /** Destructor for class PointCloud. | 
|---|
| 1011 | */ | 
|---|
| 1012 | PointCloud::~PointCloud() | 
|---|
| 1013 | { | 
|---|
| 1014 | //Info FunctionInfo(__func__); | 
|---|
| 1015 | } | 
|---|
| 1016 | ; | 
|---|
| 1017 |  | 
|---|
| 1018 | // ============================ CandidateForTesselation ============================= | 
|---|
| 1019 |  | 
|---|
| 1020 | /** Constructor of class CandidateForTesselation. | 
|---|
| 1021 | */ | 
|---|
| 1022 | CandidateForTesselation::CandidateForTesselation(BoundaryLineSet* line) : | 
|---|
| 1023 | BaseLine(line), ThirdPoint(NULL), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI) | 
|---|
| 1024 | { | 
|---|
| 1025 | Info FunctionInfo(__func__); | 
|---|
| 1026 | } | 
|---|
| 1027 | ; | 
|---|
| 1028 |  | 
|---|
| 1029 | /** Constructor of class CandidateForTesselation. | 
|---|
| 1030 | */ | 
|---|
| 1031 | CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, BoundaryPointSet* point, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) : | 
|---|
| 1032 | BaseLine(line), ThirdPoint(point), T(NULL), ShortestAngle(2. * M_PI), OtherShortestAngle(2. * M_PI) | 
|---|
| 1033 | { | 
|---|
| 1034 | Info FunctionInfo(__func__); | 
|---|
| 1035 | OptCenter = OptCandidateCenter; | 
|---|
| 1036 | OtherOptCenter = OtherOptCandidateCenter; | 
|---|
| 1037 | }; | 
|---|
| 1038 |  | 
|---|
| 1039 |  | 
|---|
| 1040 | /** Destructor for class CandidateForTesselation. | 
|---|
| 1041 | */ | 
|---|
| 1042 | CandidateForTesselation::~CandidateForTesselation() | 
|---|
| 1043 | { | 
|---|
| 1044 | } | 
|---|
| 1045 | ; | 
|---|
| 1046 |  | 
|---|
| 1047 | /** Checks validity of a given sphere of a candidate line. | 
|---|
| 1048 | * Sphere must touch all candidates and the baseline endpoints and there must be no other atoms inside. | 
|---|
| 1049 | * \param RADIUS radius of sphere | 
|---|
| 1050 | * \param *LC LinkedCell structure with other atoms | 
|---|
| 1051 | * \return true - sphere is valid, false - sphere contains other points | 
|---|
| 1052 | */ | 
|---|
| 1053 | bool CandidateForTesselation::CheckValidity(const double RADIUS, const LinkedCell *LC) const | 
|---|
| 1054 | { | 
|---|
| 1055 | Info FunctionInfo(__func__); | 
|---|
| 1056 |  | 
|---|
| 1057 | const double radiusSquared = RADIUS * RADIUS; | 
|---|
| 1058 | list<const Vector *> VectorList; | 
|---|
| 1059 | VectorList.push_back(&OptCenter); | 
|---|
| 1060 | //VectorList.push_back(&OtherOptCenter);  // don't check the other (wrong) center | 
|---|
| 1061 |  | 
|---|
| 1062 | if (!pointlist.empty()) | 
|---|
| 1063 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains candidate list and baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl); | 
|---|
| 1064 | else | 
|---|
| 1065 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere with no candidates contains baseline " << *BaseLine->endpoints[0] << "<->" << *BaseLine->endpoints[1] << " only ..." << endl); | 
|---|
| 1066 | // check baseline for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
| 1067 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
| 1068 | for (int i = 0; i < 2; i++) { | 
|---|
| 1069 | const double distance = fabs((*VRunner)->DistanceSquared(*BaseLine->endpoints[i]->node->node) - radiusSquared); | 
|---|
| 1070 | if (distance > HULLEPSILON) { | 
|---|
| 1071 | DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << *BaseLine->endpoints[i] << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl); | 
|---|
| 1072 | return false; | 
|---|
| 1073 | } | 
|---|
| 1074 | } | 
|---|
| 1075 | } | 
|---|
| 1076 |  | 
|---|
| 1077 | // check Candidates for OptCenter and OtherOptCenter being on sphere's surface | 
|---|
| 1078 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
| 1079 | const TesselPoint *Walker = *Runner; | 
|---|
| 1080 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
| 1081 | const double distance = fabs((*VRunner)->DistanceSquared(*Walker->node) - radiusSquared); | 
|---|
| 1082 | if (distance > HULLEPSILON) { | 
|---|
| 1083 | DoeLog(1) && (eLog() << Verbose(1) << "Candidate " << *Walker << " is out of sphere at " << *(*VRunner) << " by " << distance << "." << endl); | 
|---|
| 1084 | return false; | 
|---|
| 1085 | } else { | 
|---|
| 1086 | DoLog(1) && (Log() << Verbose(1) << "Candidate " << *Walker << " is inside by " << distance << "." << endl); | 
|---|
| 1087 | } | 
|---|
| 1088 | } | 
|---|
| 1089 | } | 
|---|
| 1090 |  | 
|---|
| 1091 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl); | 
|---|
| 1092 | bool flag = true; | 
|---|
| 1093 | for (list<const Vector *>::const_iterator VRunner = VectorList.begin(); VRunner != VectorList.end(); ++VRunner) { | 
|---|
| 1094 | // get all points inside the sphere | 
|---|
| 1095 | TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, (*VRunner)); | 
|---|
| 1096 |  | 
|---|
| 1097 | DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << OtherOptCenter << ":" << endl); | 
|---|
| 1098 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
| 1099 | DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(OtherOptCenter) << "." << endl); | 
|---|
| 1100 |  | 
|---|
| 1101 | // remove baseline's endpoints and candidates | 
|---|
| 1102 | for (int i = 0; i < 2; i++) { | 
|---|
| 1103 | DoLog(1) && (Log() << Verbose(1) << "INFO: removing baseline tesselpoint " << *BaseLine->endpoints[i]->node << "." << endl); | 
|---|
| 1104 | ListofPoints->remove(BaseLine->endpoints[i]->node); | 
|---|
| 1105 | } | 
|---|
| 1106 | for (TesselPointList::const_iterator Runner = pointlist.begin(); Runner != pointlist.end(); ++Runner) { | 
|---|
| 1107 | DoLog(1) && (Log() << Verbose(1) << "INFO: removing candidate tesselpoint " << *(*Runner) << "." << endl); | 
|---|
| 1108 | ListofPoints->remove(*Runner); | 
|---|
| 1109 | } | 
|---|
| 1110 | if (!ListofPoints->empty()) { | 
|---|
| 1111 | DoeLog(1) && (eLog() << Verbose(1) << "CheckValidity: There are still " << ListofPoints->size() << " points inside the sphere." << endl); | 
|---|
| 1112 | flag = false; | 
|---|
| 1113 | DoeLog(1) && (eLog() << Verbose(1) << "External atoms inside of sphere at " << *(*VRunner) << ":" << endl); | 
|---|
| 1114 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
| 1115 | DoeLog(1) && (eLog() << Verbose(1) << "  " << *(*Runner) << endl); | 
|---|
| 1116 | } | 
|---|
| 1117 | delete (ListofPoints); | 
|---|
| 1118 |  | 
|---|
| 1119 | // check with animate_sphere.tcl VMD script | 
|---|
| 1120 | if (ThirdPoint != NULL) { | 
|---|
| 1121 | DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " " << ThirdPoint->Nr + 1 << " " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl); | 
|---|
| 1122 | } else { | 
|---|
| 1123 | DoLog(1) && (Log() << Verbose(1) << "Check by: ... missing third point ..." << endl); | 
|---|
| 1124 | DoLog(1) && (Log() << Verbose(1) << "Check by: animate_sphere 0 " << BaseLine->endpoints[0]->Nr + 1 << " " << BaseLine->endpoints[1]->Nr + 1 << " ??? " << RADIUS << " " << OldCenter[0] << " " << OldCenter[1] << " " << OldCenter[2] << " " << (*VRunner)->at(0) << " " << (*VRunner)->at(1) << " " << (*VRunner)->at(2) << endl); | 
|---|
| 1125 | } | 
|---|
| 1126 | } | 
|---|
| 1127 | return flag; | 
|---|
| 1128 | } | 
|---|
| 1129 | ; | 
|---|
| 1130 |  | 
|---|
| 1131 | /** output operator for CandidateForTesselation. | 
|---|
| 1132 | * \param &ost output stream | 
|---|
| 1133 | * \param &a boundary line | 
|---|
| 1134 | */ | 
|---|
| 1135 | ostream & operator <<(ostream &ost, const CandidateForTesselation &a) | 
|---|
| 1136 | { | 
|---|
| 1137 | ost << "[" << a.BaseLine->Nr << "|" << a.BaseLine->endpoints[0]->node->Name << "," << a.BaseLine->endpoints[1]->node->Name << "] with "; | 
|---|
| 1138 | if (a.pointlist.empty()) | 
|---|
| 1139 | ost << "no candidate."; | 
|---|
| 1140 | else { | 
|---|
| 1141 | ost << "candidate"; | 
|---|
| 1142 | if (a.pointlist.size() != 1) | 
|---|
| 1143 | ost << "s "; | 
|---|
| 1144 | else | 
|---|
| 1145 | ost << " "; | 
|---|
| 1146 | for (TesselPointList::const_iterator Runner = a.pointlist.begin(); Runner != a.pointlist.end(); Runner++) | 
|---|
| 1147 | ost << *(*Runner) << " "; | 
|---|
| 1148 | ost << " at angle " << (a.ShortestAngle) << "."; | 
|---|
| 1149 | } | 
|---|
| 1150 |  | 
|---|
| 1151 | return ost; | 
|---|
| 1152 | } | 
|---|
| 1153 | ; | 
|---|
| 1154 |  | 
|---|
| 1155 | // =========================================================== class TESSELATION =========================================== | 
|---|
| 1156 |  | 
|---|
| 1157 | /** Constructor of class Tesselation. | 
|---|
| 1158 | */ | 
|---|
| 1159 | Tesselation::Tesselation() : | 
|---|
| 1160 | PointsOnBoundaryCount(0), LinesOnBoundaryCount(0), TrianglesOnBoundaryCount(0), LastTriangle(NULL), TriangleFilesWritten(0), InternalPointer(PointsOnBoundary.begin()) | 
|---|
| 1161 | { | 
|---|
| 1162 | Info FunctionInfo(__func__); | 
|---|
| 1163 | } | 
|---|
| 1164 | ; | 
|---|
| 1165 |  | 
|---|
| 1166 | /** Destructor of class Tesselation. | 
|---|
| 1167 | * We have to free all points, lines and triangles. | 
|---|
| 1168 | */ | 
|---|
| 1169 | Tesselation::~Tesselation() | 
|---|
| 1170 | { | 
|---|
| 1171 | Info FunctionInfo(__func__); | 
|---|
| 1172 | DoLog(0) && (Log() << Verbose(0) << "Free'ing TesselStruct ... " << endl); | 
|---|
| 1173 | for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) { | 
|---|
| 1174 | if (runner->second != NULL) { | 
|---|
| 1175 | delete (runner->second); | 
|---|
| 1176 | runner->second = NULL; | 
|---|
| 1177 | } else | 
|---|
| 1178 | DoeLog(1) && (eLog() << Verbose(1) << "The triangle " << runner->first << " has already been free'd." << endl); | 
|---|
| 1179 | } | 
|---|
| 1180 | DoLog(0) && (Log() << Verbose(0) << "This envelope was written to file " << TriangleFilesWritten << " times(s)." << endl); | 
|---|
| 1181 | } | 
|---|
| 1182 | ; | 
|---|
| 1183 |  | 
|---|
| 1184 | /** PointCloud implementation of GetCenter | 
|---|
| 1185 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1186 | */ | 
|---|
| 1187 | Vector * Tesselation::GetCenter(ofstream *out) const | 
|---|
| 1188 | { | 
|---|
| 1189 | Info FunctionInfo(__func__); | 
|---|
| 1190 | Vector *Center = new Vector(0., 0., 0.); | 
|---|
| 1191 | int num = 0; | 
|---|
| 1192 | for (GoToFirst(); (!IsEnd()); GoToNext()) { | 
|---|
| 1193 | (*Center) += (*GetPoint()->node); | 
|---|
| 1194 | num++; | 
|---|
| 1195 | } | 
|---|
| 1196 | Center->Scale(1. / num); | 
|---|
| 1197 | return Center; | 
|---|
| 1198 | } | 
|---|
| 1199 | ; | 
|---|
| 1200 |  | 
|---|
| 1201 | /** PointCloud implementation of GoPoint | 
|---|
| 1202 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1203 | */ | 
|---|
| 1204 | TesselPoint * Tesselation::GetPoint() const | 
|---|
| 1205 | { | 
|---|
| 1206 | Info FunctionInfo(__func__); | 
|---|
| 1207 | return (InternalPointer->second->node); | 
|---|
| 1208 | } | 
|---|
| 1209 | ; | 
|---|
| 1210 |  | 
|---|
| 1211 | /** PointCloud implementation of GetTerminalPoint. | 
|---|
| 1212 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1213 | */ | 
|---|
| 1214 | TesselPoint * Tesselation::GetTerminalPoint() const | 
|---|
| 1215 | { | 
|---|
| 1216 | Info FunctionInfo(__func__); | 
|---|
| 1217 | PointMap::const_iterator Runner = PointsOnBoundary.end(); | 
|---|
| 1218 | Runner--; | 
|---|
| 1219 | return (Runner->second->node); | 
|---|
| 1220 | } | 
|---|
| 1221 | ; | 
|---|
| 1222 |  | 
|---|
| 1223 | /** PointCloud implementation of GoToNext. | 
|---|
| 1224 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1225 | */ | 
|---|
| 1226 | void Tesselation::GoToNext() const | 
|---|
| 1227 | { | 
|---|
| 1228 | Info FunctionInfo(__func__); | 
|---|
| 1229 | if (InternalPointer != PointsOnBoundary.end()) | 
|---|
| 1230 | InternalPointer++; | 
|---|
| 1231 | } | 
|---|
| 1232 | ; | 
|---|
| 1233 |  | 
|---|
| 1234 | /** PointCloud implementation of GoToPrevious. | 
|---|
| 1235 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1236 | */ | 
|---|
| 1237 | void Tesselation::GoToPrevious() const | 
|---|
| 1238 | { | 
|---|
| 1239 | Info FunctionInfo(__func__); | 
|---|
| 1240 | if (InternalPointer != PointsOnBoundary.begin()) | 
|---|
| 1241 | InternalPointer--; | 
|---|
| 1242 | } | 
|---|
| 1243 | ; | 
|---|
| 1244 |  | 
|---|
| 1245 | /** PointCloud implementation of GoToFirst. | 
|---|
| 1246 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1247 | */ | 
|---|
| 1248 | void Tesselation::GoToFirst() const | 
|---|
| 1249 | { | 
|---|
| 1250 | Info FunctionInfo(__func__); | 
|---|
| 1251 | InternalPointer = PointsOnBoundary.begin(); | 
|---|
| 1252 | } | 
|---|
| 1253 | ; | 
|---|
| 1254 |  | 
|---|
| 1255 | /** PointCloud implementation of GoToLast. | 
|---|
| 1256 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1257 | */ | 
|---|
| 1258 | void Tesselation::GoToLast() const | 
|---|
| 1259 | { | 
|---|
| 1260 | Info FunctionInfo(__func__); | 
|---|
| 1261 | InternalPointer = PointsOnBoundary.end(); | 
|---|
| 1262 | InternalPointer--; | 
|---|
| 1263 | } | 
|---|
| 1264 | ; | 
|---|
| 1265 |  | 
|---|
| 1266 | /** PointCloud implementation of IsEmpty. | 
|---|
| 1267 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1268 | */ | 
|---|
| 1269 | bool Tesselation::IsEmpty() const | 
|---|
| 1270 | { | 
|---|
| 1271 | Info FunctionInfo(__func__); | 
|---|
| 1272 | return (PointsOnBoundary.empty()); | 
|---|
| 1273 | } | 
|---|
| 1274 | ; | 
|---|
| 1275 |  | 
|---|
| 1276 | /** PointCloud implementation of IsLast. | 
|---|
| 1277 | * Uses PointsOnBoundary and STL stuff. | 
|---|
| 1278 | */ | 
|---|
| 1279 | bool Tesselation::IsEnd() const | 
|---|
| 1280 | { | 
|---|
| 1281 | Info FunctionInfo(__func__); | 
|---|
| 1282 | return (InternalPointer == PointsOnBoundary.end()); | 
|---|
| 1283 | } | 
|---|
| 1284 | ; | 
|---|
| 1285 |  | 
|---|
| 1286 | /** Gueses first starting triangle of the convex envelope. | 
|---|
| 1287 | * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third. | 
|---|
| 1288 | * \param *out output stream for debugging | 
|---|
| 1289 | * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster | 
|---|
| 1290 | */ | 
|---|
| 1291 | void Tesselation::GuessStartingTriangle() | 
|---|
| 1292 | { | 
|---|
| 1293 | Info FunctionInfo(__func__); | 
|---|
| 1294 | // 4b. create a starting triangle | 
|---|
| 1295 | // 4b1. create all distances | 
|---|
| 1296 | DistanceMultiMap DistanceMMap; | 
|---|
| 1297 | double distance, tmp; | 
|---|
| 1298 | Vector PlaneVector, TrialVector; | 
|---|
| 1299 | PointMap::iterator A, B, C; // three nodes of the first triangle | 
|---|
| 1300 | A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily | 
|---|
| 1301 |  | 
|---|
| 1302 | // with A chosen, take each pair B,C and sort | 
|---|
| 1303 | if (A != PointsOnBoundary.end()) { | 
|---|
| 1304 | B = A; | 
|---|
| 1305 | B++; | 
|---|
| 1306 | for (; B != PointsOnBoundary.end(); B++) { | 
|---|
| 1307 | C = B; | 
|---|
| 1308 | C++; | 
|---|
| 1309 | for (; C != PointsOnBoundary.end(); C++) { | 
|---|
| 1310 | tmp = A->second->node->node->DistanceSquared(*B->second->node->node); | 
|---|
| 1311 | distance = tmp * tmp; | 
|---|
| 1312 | tmp = A->second->node->node->DistanceSquared(*C->second->node->node); | 
|---|
| 1313 | distance += tmp * tmp; | 
|---|
| 1314 | tmp = B->second->node->node->DistanceSquared(*C->second->node->node); | 
|---|
| 1315 | distance += tmp * tmp; | 
|---|
| 1316 | DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C))); | 
|---|
| 1317 | } | 
|---|
| 1318 | } | 
|---|
| 1319 | } | 
|---|
| 1320 | //    // listing distances | 
|---|
| 1321 | //    Log() << Verbose(1) << "Listing DistanceMMap:"; | 
|---|
| 1322 | //    for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) { | 
|---|
| 1323 | //      Log() << Verbose(0) << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")"; | 
|---|
| 1324 | //    } | 
|---|
| 1325 | //    Log() << Verbose(0) << endl; | 
|---|
| 1326 | // 4b2. pick three baselines forming a triangle | 
|---|
| 1327 | // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate | 
|---|
| 1328 | DistanceMultiMap::iterator baseline = DistanceMMap.begin(); | 
|---|
| 1329 | for (; baseline != DistanceMMap.end(); baseline++) { | 
|---|
| 1330 | // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate | 
|---|
| 1331 | // 2. next, we have to check whether all points reside on only one side of the triangle | 
|---|
| 1332 | // 3. construct plane vector | 
|---|
| 1333 | PlaneVector = Plane(*A->second->node->node, | 
|---|
| 1334 | *baseline->second.first->second->node->node, | 
|---|
| 1335 | *baseline->second.second->second->node->node).getNormal(); | 
|---|
| 1336 | DoLog(2) && (Log() << Verbose(2) << "Plane vector of candidate triangle is " << PlaneVector << endl); | 
|---|
| 1337 | // 4. loop over all points | 
|---|
| 1338 | double sign = 0.; | 
|---|
| 1339 | PointMap::iterator checker = PointsOnBoundary.begin(); | 
|---|
| 1340 | for (; checker != PointsOnBoundary.end(); checker++) { | 
|---|
| 1341 | // (neglecting A,B,C) | 
|---|
| 1342 | if ((checker == A) || (checker == baseline->second.first) || (checker == baseline->second.second)) | 
|---|
| 1343 | continue; | 
|---|
| 1344 | // 4a. project onto plane vector | 
|---|
| 1345 | TrialVector = (*checker->second->node->node); | 
|---|
| 1346 | TrialVector.SubtractVector(*A->second->node->node); | 
|---|
| 1347 | distance = TrialVector.ScalarProduct(PlaneVector); | 
|---|
| 1348 | if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok | 
|---|
| 1349 | continue; | 
|---|
| 1350 | DoLog(2) && (Log() << Verbose(2) << "Projection of " << checker->second->node->Name << " yields distance of " << distance << "." << endl); | 
|---|
| 1351 | tmp = distance / fabs(distance); | 
|---|
| 1352 | // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle) | 
|---|
| 1353 | if ((sign != 0) && (tmp != sign)) { | 
|---|
| 1354 | // 4c. If so, break 4. loop and continue with next candidate in 1. loop | 
|---|
| 1355 | DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leaves " << checker->second->node->Name << " outside the convex hull." << endl); | 
|---|
| 1356 | break; | 
|---|
| 1357 | } else { // note the sign for later | 
|---|
| 1358 | DoLog(2) && (Log() << Verbose(2) << "Current candidates: " << A->second->node->Name << "," << baseline->second.first->second->node->Name << "," << baseline->second.second->second->node->Name << " leave " << checker->second->node->Name << " inside the convex hull." << endl); | 
|---|
| 1359 | sign = tmp; | 
|---|
| 1360 | } | 
|---|
| 1361 | // 4d. Check whether the point is inside the triangle (check distance to each node | 
|---|
| 1362 | tmp = checker->second->node->node->DistanceSquared(*A->second->node->node); | 
|---|
| 1363 | int innerpoint = 0; | 
|---|
| 1364 | if ((tmp < A->second->node->node->DistanceSquared(*baseline->second.first->second->node->node)) && (tmp < A->second->node->node->DistanceSquared(*baseline->second.second->second->node->node))) | 
|---|
| 1365 | innerpoint++; | 
|---|
| 1366 | tmp = checker->second->node->node->DistanceSquared(*baseline->second.first->second->node->node); | 
|---|
| 1367 | if ((tmp < baseline->second.first->second->node->node->DistanceSquared(*A->second->node->node)) && (tmp < baseline->second.first->second->node->node->DistanceSquared(*baseline->second.second->second->node->node))) | 
|---|
| 1368 | innerpoint++; | 
|---|
| 1369 | tmp = checker->second->node->node->DistanceSquared(*baseline->second.second->second->node->node); | 
|---|
| 1370 | if ((tmp < baseline->second.second->second->node->node->DistanceSquared(*baseline->second.first->second->node->node)) && (tmp < baseline->second.second->second->node->node->DistanceSquared(*A->second->node->node))) | 
|---|
| 1371 | innerpoint++; | 
|---|
| 1372 | // 4e. If so, break 4. loop and continue with next candidate in 1. loop | 
|---|
| 1373 | if (innerpoint == 3) | 
|---|
| 1374 | break; | 
|---|
| 1375 | } | 
|---|
| 1376 | // 5. come this far, all on same side? Then break 1. loop and construct triangle | 
|---|
| 1377 | if (checker == PointsOnBoundary.end()) { | 
|---|
| 1378 | DoLog(2) && (Log() << Verbose(2) << "Looks like we have a candidate!" << endl); | 
|---|
| 1379 | break; | 
|---|
| 1380 | } | 
|---|
| 1381 | } | 
|---|
| 1382 | if (baseline != DistanceMMap.end()) { | 
|---|
| 1383 | BPS[0] = baseline->second.first->second; | 
|---|
| 1384 | BPS[1] = baseline->second.second->second; | 
|---|
| 1385 | BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1386 | BPS[0] = A->second; | 
|---|
| 1387 | BPS[1] = baseline->second.second->second; | 
|---|
| 1388 | BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1389 | BPS[0] = baseline->second.first->second; | 
|---|
| 1390 | BPS[1] = A->second; | 
|---|
| 1391 | BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1392 |  | 
|---|
| 1393 | // 4b3. insert created triangle | 
|---|
| 1394 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1395 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 1396 | TrianglesOnBoundaryCount++; | 
|---|
| 1397 | for (int i = 0; i < NDIM; i++) { | 
|---|
| 1398 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i])); | 
|---|
| 1399 | LinesOnBoundaryCount++; | 
|---|
| 1400 | } | 
|---|
| 1401 |  | 
|---|
| 1402 | DoLog(1) && (Log() << Verbose(1) << "Starting triangle is " << *BTS << "." << endl); | 
|---|
| 1403 | } else { | 
|---|
| 1404 | DoeLog(0) && (eLog() << Verbose(0) << "No starting triangle found." << endl); | 
|---|
| 1405 | } | 
|---|
| 1406 | } | 
|---|
| 1407 | ; | 
|---|
| 1408 |  | 
|---|
| 1409 | /** Tesselates the convex envelope of a cluster from a single starting triangle. | 
|---|
| 1410 | * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most | 
|---|
| 1411 | * 2 triangles. Hence, we go through all current lines: | 
|---|
| 1412 | * -# if the lines contains to only one triangle | 
|---|
| 1413 | * -# We search all points in the boundary | 
|---|
| 1414 | *    -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to | 
|---|
| 1415 | *       baseline in triangle plane pointing out of the triangle and normal vector of new triangle) | 
|---|
| 1416 | *    -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors) | 
|---|
| 1417 | *    -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount) | 
|---|
| 1418 | * \param *out output stream for debugging | 
|---|
| 1419 | * \param *configuration for IsAngstroem | 
|---|
| 1420 | * \param *cloud cluster of points | 
|---|
| 1421 | */ | 
|---|
| 1422 | void Tesselation::TesselateOnBoundary(const PointCloud * const cloud) | 
|---|
| 1423 | { | 
|---|
| 1424 | Info FunctionInfo(__func__); | 
|---|
| 1425 | bool flag; | 
|---|
| 1426 | PointMap::iterator winner; | 
|---|
| 1427 | class BoundaryPointSet *peak = NULL; | 
|---|
| 1428 | double SmallestAngle, TempAngle; | 
|---|
| 1429 | Vector NormalVector, VirtualNormalVector, CenterVector, TempVector, helper, PropagationVector, *Center = NULL; | 
|---|
| 1430 | LineMap::iterator LineChecker[2]; | 
|---|
| 1431 |  | 
|---|
| 1432 | Center = cloud->GetCenter(); | 
|---|
| 1433 | // create a first tesselation with the given BoundaryPoints | 
|---|
| 1434 | do { | 
|---|
| 1435 | flag = false; | 
|---|
| 1436 | for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++) | 
|---|
| 1437 | if (baseline->second->triangles.size() == 1) { | 
|---|
| 1438 | // 5a. go through each boundary point if not _both_ edges between either endpoint of the current line and this point exist (and belong to 2 triangles) | 
|---|
| 1439 | SmallestAngle = M_PI; | 
|---|
| 1440 |  | 
|---|
| 1441 | // get peak point with respect to this base line's only triangle | 
|---|
| 1442 | BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far | 
|---|
| 1443 | DoLog(0) && (Log() << Verbose(0) << "Current baseline is between " << *(baseline->second) << "." << endl); | 
|---|
| 1444 | for (int i = 0; i < 3; i++) | 
|---|
| 1445 | if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1])) | 
|---|
| 1446 | peak = BTS->endpoints[i]; | 
|---|
| 1447 | DoLog(1) && (Log() << Verbose(1) << " and has peak " << *peak << "." << endl); | 
|---|
| 1448 |  | 
|---|
| 1449 | // prepare some auxiliary vectors | 
|---|
| 1450 | Vector BaseLineCenter, BaseLine; | 
|---|
| 1451 | BaseLineCenter = 0.5 * ((*baseline->second->endpoints[0]->node->node) + | 
|---|
| 1452 | (*baseline->second->endpoints[1]->node->node)); | 
|---|
| 1453 | BaseLine = (*baseline->second->endpoints[0]->node->node) - (*baseline->second->endpoints[1]->node->node); | 
|---|
| 1454 |  | 
|---|
| 1455 | // offset to center of triangle | 
|---|
| 1456 | CenterVector.Zero(); | 
|---|
| 1457 | for (int i = 0; i < 3; i++) | 
|---|
| 1458 | CenterVector += (*BTS->endpoints[i]->node->node); | 
|---|
| 1459 | CenterVector.Scale(1. / 3.); | 
|---|
| 1460 | DoLog(2) && (Log() << Verbose(2) << "CenterVector of base triangle is " << CenterVector << endl); | 
|---|
| 1461 |  | 
|---|
| 1462 | // normal vector of triangle | 
|---|
| 1463 | NormalVector = (*Center) - CenterVector; | 
|---|
| 1464 | BTS->GetNormalVector(NormalVector); | 
|---|
| 1465 | NormalVector = BTS->NormalVector; | 
|---|
| 1466 | DoLog(2) && (Log() << Verbose(2) << "NormalVector of base triangle is " << NormalVector << endl); | 
|---|
| 1467 |  | 
|---|
| 1468 | // vector in propagation direction (out of triangle) | 
|---|
| 1469 | // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection) | 
|---|
| 1470 | PropagationVector = Plane(BaseLine, NormalVector,0).getNormal(); | 
|---|
| 1471 | TempVector = CenterVector - (*baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center! | 
|---|
| 1472 | //Log() << Verbose(0) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl; | 
|---|
| 1473 | if (PropagationVector.ScalarProduct(TempVector) > 0) // make sure normal propagation vector points outward from baseline | 
|---|
| 1474 | PropagationVector.Scale(-1.); | 
|---|
| 1475 | DoLog(2) && (Log() << Verbose(2) << "PropagationVector of base triangle is " << PropagationVector << endl); | 
|---|
| 1476 | winner = PointsOnBoundary.end(); | 
|---|
| 1477 |  | 
|---|
| 1478 | // loop over all points and calculate angle between normal vector of new and present triangle | 
|---|
| 1479 | for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) { | 
|---|
| 1480 | if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints | 
|---|
| 1481 | DoLog(1) && (Log() << Verbose(1) << "Target point is " << *(target->second) << ":" << endl); | 
|---|
| 1482 |  | 
|---|
| 1483 | // first check direction, so that triangles don't intersect | 
|---|
| 1484 | VirtualNormalVector = (*target->second->node->node) - BaseLineCenter; | 
|---|
| 1485 | VirtualNormalVector.ProjectOntoPlane(NormalVector); | 
|---|
| 1486 | TempAngle = VirtualNormalVector.Angle(PropagationVector); | 
|---|
| 1487 | DoLog(2) && (Log() << Verbose(2) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl); | 
|---|
| 1488 | if (TempAngle > (M_PI / 2.)) { // no bends bigger than Pi/2 (90 degrees) | 
|---|
| 1489 | DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl); | 
|---|
| 1490 | continue; | 
|---|
| 1491 | } else | 
|---|
| 1492 | DoLog(2) && (Log() << Verbose(2) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl); | 
|---|
| 1493 |  | 
|---|
| 1494 | // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle) | 
|---|
| 1495 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(target->first); | 
|---|
| 1496 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first); | 
|---|
| 1497 | if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->triangles.size() == 2))) { | 
|---|
| 1498 | DoLog(2) && (Log() << Verbose(2) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->triangles.size() << " triangles." << endl); | 
|---|
| 1499 | continue; | 
|---|
| 1500 | } | 
|---|
| 1501 | if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->triangles.size() == 2))) { | 
|---|
| 1502 | DoLog(2) && (Log() << Verbose(2) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->triangles.size() << " triangles." << endl); | 
|---|
| 1503 | continue; | 
|---|
| 1504 | } | 
|---|
| 1505 |  | 
|---|
| 1506 | // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint) | 
|---|
| 1507 | if ((((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (GetCommonEndpoint(LineChecker[0]->second, LineChecker[1]->second) == peak)))) { | 
|---|
| 1508 | DoLog(4) && (Log() << Verbose(4) << "Current target is peak!" << endl); | 
|---|
| 1509 | continue; | 
|---|
| 1510 | } | 
|---|
| 1511 |  | 
|---|
| 1512 | // check for linear dependence | 
|---|
| 1513 | TempVector = (*baseline->second->endpoints[0]->node->node) - (*target->second->node->node); | 
|---|
| 1514 | helper = (*baseline->second->endpoints[1]->node->node) - (*target->second->node->node); | 
|---|
| 1515 | helper.ProjectOntoPlane(TempVector); | 
|---|
| 1516 | if (fabs(helper.NormSquared()) < MYEPSILON) { | 
|---|
| 1517 | DoLog(2) && (Log() << Verbose(2) << "Chosen set of vectors is linear dependent." << endl); | 
|---|
| 1518 | continue; | 
|---|
| 1519 | } | 
|---|
| 1520 |  | 
|---|
| 1521 | // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle | 
|---|
| 1522 | flag = true; | 
|---|
| 1523 | VirtualNormalVector = Plane(*(baseline->second->endpoints[0]->node->node), | 
|---|
| 1524 | *(baseline->second->endpoints[1]->node->node), | 
|---|
| 1525 | *(target->second->node->node)).getNormal(); | 
|---|
| 1526 | TempVector = (1./3.) * ((*baseline->second->endpoints[0]->node->node) + | 
|---|
| 1527 | (*baseline->second->endpoints[1]->node->node) + | 
|---|
| 1528 | (*target->second->node->node)); | 
|---|
| 1529 | TempVector -= (*Center); | 
|---|
| 1530 | // make it always point outward | 
|---|
| 1531 | if (VirtualNormalVector.ScalarProduct(TempVector) < 0) | 
|---|
| 1532 | VirtualNormalVector.Scale(-1.); | 
|---|
| 1533 | // calculate angle | 
|---|
| 1534 | TempAngle = NormalVector.Angle(VirtualNormalVector); | 
|---|
| 1535 | DoLog(2) && (Log() << Verbose(2) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl); | 
|---|
| 1536 | if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner | 
|---|
| 1537 | SmallestAngle = TempAngle; | 
|---|
| 1538 | winner = target; | 
|---|
| 1539 | DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl); | 
|---|
| 1540 | } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle) | 
|---|
| 1541 | // hence, check the angles to some normal direction from our base line but in this common plane of both targets... | 
|---|
| 1542 | helper = (*target->second->node->node) - BaseLineCenter; | 
|---|
| 1543 | helper.ProjectOntoPlane(BaseLine); | 
|---|
| 1544 | // ...the one with the smaller angle is the better candidate | 
|---|
| 1545 | TempVector = (*target->second->node->node) - BaseLineCenter; | 
|---|
| 1546 | TempVector.ProjectOntoPlane(VirtualNormalVector); | 
|---|
| 1547 | TempAngle = TempVector.Angle(helper); | 
|---|
| 1548 | TempVector = (*winner->second->node->node) - BaseLineCenter; | 
|---|
| 1549 | TempVector.ProjectOntoPlane(VirtualNormalVector); | 
|---|
| 1550 | if (TempAngle < TempVector.Angle(helper)) { | 
|---|
| 1551 | TempAngle = NormalVector.Angle(VirtualNormalVector); | 
|---|
| 1552 | SmallestAngle = TempAngle; | 
|---|
| 1553 | winner = target; | 
|---|
| 1554 | DoLog(2) && (Log() << Verbose(2) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl); | 
|---|
| 1555 | } else | 
|---|
| 1556 | DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl); | 
|---|
| 1557 | } else | 
|---|
| 1558 | DoLog(2) && (Log() << Verbose(2) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl); | 
|---|
| 1559 | } | 
|---|
| 1560 | } // end of loop over all boundary points | 
|---|
| 1561 |  | 
|---|
| 1562 | // 5b. The point of the above whose triangle has the greatest angle with the triangle the current line belongs to (it only belongs to one, remember!): New triangle | 
|---|
| 1563 | if (winner != PointsOnBoundary.end()) { | 
|---|
| 1564 | DoLog(0) && (Log() << Verbose(0) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl); | 
|---|
| 1565 | // create the lins of not yet present | 
|---|
| 1566 | BLS[0] = baseline->second; | 
|---|
| 1567 | // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles) | 
|---|
| 1568 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(winner->first); | 
|---|
| 1569 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(winner->first); | 
|---|
| 1570 | if (LineChecker[0] == baseline->second->endpoints[0]->lines.end()) { // create | 
|---|
| 1571 | BPS[0] = baseline->second->endpoints[0]; | 
|---|
| 1572 | BPS[1] = winner->second; | 
|---|
| 1573 | BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1574 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[1])); | 
|---|
| 1575 | LinesOnBoundaryCount++; | 
|---|
| 1576 | } else | 
|---|
| 1577 | BLS[1] = LineChecker[0]->second; | 
|---|
| 1578 | if (LineChecker[1] == baseline->second->endpoints[1]->lines.end()) { // create | 
|---|
| 1579 | BPS[0] = baseline->second->endpoints[1]; | 
|---|
| 1580 | BPS[1] = winner->second; | 
|---|
| 1581 | BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1582 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[2])); | 
|---|
| 1583 | LinesOnBoundaryCount++; | 
|---|
| 1584 | } else | 
|---|
| 1585 | BLS[2] = LineChecker[1]->second; | 
|---|
| 1586 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1587 | BTS->GetCenter(&helper); | 
|---|
| 1588 | helper -= (*Center); | 
|---|
| 1589 | helper *= -1; | 
|---|
| 1590 | BTS->GetNormalVector(helper); | 
|---|
| 1591 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 1592 | TrianglesOnBoundaryCount++; | 
|---|
| 1593 | } else { | 
|---|
| 1594 | DoeLog(2) && (eLog() << Verbose(2) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl); | 
|---|
| 1595 | } | 
|---|
| 1596 |  | 
|---|
| 1597 | // 5d. If the set of lines is not yet empty, go to 5. and continue | 
|---|
| 1598 | } else | 
|---|
| 1599 | DoLog(0) && (Log() << Verbose(0) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->triangles.size() << "." << endl); | 
|---|
| 1600 | } while (flag); | 
|---|
| 1601 |  | 
|---|
| 1602 | // exit | 
|---|
| 1603 | delete (Center); | 
|---|
| 1604 | } | 
|---|
| 1605 | ; | 
|---|
| 1606 |  | 
|---|
| 1607 | /** Inserts all points outside of the tesselated surface into it by adding new triangles. | 
|---|
| 1608 | * \param *out output stream for debugging | 
|---|
| 1609 | * \param *cloud cluster of points | 
|---|
| 1610 | * \param *LC LinkedCell structure to find nearest point quickly | 
|---|
| 1611 | * \return true - all straddling points insert, false - something went wrong | 
|---|
| 1612 | */ | 
|---|
| 1613 | bool Tesselation::InsertStraddlingPoints(const PointCloud *cloud, const LinkedCell *LC) | 
|---|
| 1614 | { | 
|---|
| 1615 | Info FunctionInfo(__func__); | 
|---|
| 1616 | Vector Intersection, Normal; | 
|---|
| 1617 | TesselPoint *Walker = NULL; | 
|---|
| 1618 | Vector *Center = cloud->GetCenter(); | 
|---|
| 1619 | TriangleList *triangles = NULL; | 
|---|
| 1620 | bool AddFlag = false; | 
|---|
| 1621 | LinkedCell *BoundaryPoints = NULL; | 
|---|
| 1622 |  | 
|---|
| 1623 | cloud->GoToFirst(); | 
|---|
| 1624 | BoundaryPoints = new LinkedCell(this, 5.); | 
|---|
| 1625 | while (!cloud->IsEnd()) { // we only have to go once through all points, as boundary can become only bigger | 
|---|
| 1626 | if (AddFlag) { | 
|---|
| 1627 | delete (BoundaryPoints); | 
|---|
| 1628 | BoundaryPoints = new LinkedCell(this, 5.); | 
|---|
| 1629 | AddFlag = false; | 
|---|
| 1630 | } | 
|---|
| 1631 | Walker = cloud->GetPoint(); | 
|---|
| 1632 | DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Walker << "." << endl); | 
|---|
| 1633 | // get the next triangle | 
|---|
| 1634 | triangles = FindClosestTrianglesToVector(Walker->node, BoundaryPoints); | 
|---|
| 1635 | BTS = triangles->front(); | 
|---|
| 1636 | if ((triangles == NULL) || (BTS->ContainsBoundaryPoint(Walker))) { | 
|---|
| 1637 | DoLog(0) && (Log() << Verbose(0) << "No triangles found, probably a tesselation point itself." << endl); | 
|---|
| 1638 | cloud->GoToNext(); | 
|---|
| 1639 | continue; | 
|---|
| 1640 | } else { | 
|---|
| 1641 | } | 
|---|
| 1642 | DoLog(0) && (Log() << Verbose(0) << "Closest triangle is " << *BTS << "." << endl); | 
|---|
| 1643 | // get the intersection point | 
|---|
| 1644 | if (BTS->GetIntersectionInsideTriangle(Center, Walker->node, &Intersection)) { | 
|---|
| 1645 | DoLog(0) && (Log() << Verbose(0) << "We have an intersection at " << Intersection << "." << endl); | 
|---|
| 1646 | // we have the intersection, check whether in- or outside of boundary | 
|---|
| 1647 | if ((Center->DistanceSquared(*Walker->node) - Center->DistanceSquared(Intersection)) < -MYEPSILON) { | 
|---|
| 1648 | // inside, next! | 
|---|
| 1649 | DoLog(0) && (Log() << Verbose(0) << *Walker << " is inside wrt triangle " << *BTS << "." << endl); | 
|---|
| 1650 | } else { | 
|---|
| 1651 | // outside! | 
|---|
| 1652 | DoLog(0) && (Log() << Verbose(0) << *Walker << " is outside wrt triangle " << *BTS << "." << endl); | 
|---|
| 1653 | class BoundaryLineSet *OldLines[3], *NewLines[3]; | 
|---|
| 1654 | class BoundaryPointSet *OldPoints[3], *NewPoint; | 
|---|
| 1655 | // store the three old lines and old points | 
|---|
| 1656 | for (int i = 0; i < 3; i++) { | 
|---|
| 1657 | OldLines[i] = BTS->lines[i]; | 
|---|
| 1658 | OldPoints[i] = BTS->endpoints[i]; | 
|---|
| 1659 | } | 
|---|
| 1660 | Normal = BTS->NormalVector; | 
|---|
| 1661 | // add Walker to boundary points | 
|---|
| 1662 | DoLog(0) && (Log() << Verbose(0) << "Adding " << *Walker << " to BoundaryPoints." << endl); | 
|---|
| 1663 | AddFlag = true; | 
|---|
| 1664 | if (AddBoundaryPoint(Walker, 0)) | 
|---|
| 1665 | NewPoint = BPS[0]; | 
|---|
| 1666 | else | 
|---|
| 1667 | continue; | 
|---|
| 1668 | // remove triangle | 
|---|
| 1669 | DoLog(0) && (Log() << Verbose(0) << "Erasing triangle " << *BTS << "." << endl); | 
|---|
| 1670 | TrianglesOnBoundary.erase(BTS->Nr); | 
|---|
| 1671 | delete (BTS); | 
|---|
| 1672 | // create three new boundary lines | 
|---|
| 1673 | for (int i = 0; i < 3; i++) { | 
|---|
| 1674 | BPS[0] = NewPoint; | 
|---|
| 1675 | BPS[1] = OldPoints[i]; | 
|---|
| 1676 | NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 1677 | DoLog(1) && (Log() << Verbose(1) << "Creating new line " << *NewLines[i] << "." << endl); | 
|---|
| 1678 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one | 
|---|
| 1679 | LinesOnBoundaryCount++; | 
|---|
| 1680 | } | 
|---|
| 1681 | // create three new triangle with new point | 
|---|
| 1682 | for (int i = 0; i < 3; i++) { // find all baselines | 
|---|
| 1683 | BLS[0] = OldLines[i]; | 
|---|
| 1684 | int n = 1; | 
|---|
| 1685 | for (int j = 0; j < 3; j++) { | 
|---|
| 1686 | if (NewLines[j]->IsConnectedTo(BLS[0])) { | 
|---|
| 1687 | if (n > 2) { | 
|---|
| 1688 | DoeLog(2) && (eLog() << Verbose(2) << BLS[0] << " connects to all of the new lines?!" << endl); | 
|---|
| 1689 | return false; | 
|---|
| 1690 | } else | 
|---|
| 1691 | BLS[n++] = NewLines[j]; | 
|---|
| 1692 | } | 
|---|
| 1693 | } | 
|---|
| 1694 | // create the triangle | 
|---|
| 1695 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1696 | Normal.Scale(-1.); | 
|---|
| 1697 | BTS->GetNormalVector(Normal); | 
|---|
| 1698 | Normal.Scale(-1.); | 
|---|
| 1699 | DoLog(0) && (Log() << Verbose(0) << "Created new triangle " << *BTS << "." << endl); | 
|---|
| 1700 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 1701 | TrianglesOnBoundaryCount++; | 
|---|
| 1702 | } | 
|---|
| 1703 | } | 
|---|
| 1704 | } else { // something is wrong with FindClosestTriangleToPoint! | 
|---|
| 1705 | DoeLog(1) && (eLog() << Verbose(1) << "The closest triangle did not produce an intersection!" << endl); | 
|---|
| 1706 | return false; | 
|---|
| 1707 | } | 
|---|
| 1708 | cloud->GoToNext(); | 
|---|
| 1709 | } | 
|---|
| 1710 |  | 
|---|
| 1711 | // exit | 
|---|
| 1712 | delete (Center); | 
|---|
| 1713 | return true; | 
|---|
| 1714 | } | 
|---|
| 1715 | ; | 
|---|
| 1716 |  | 
|---|
| 1717 | /** Adds a point to the tesselation::PointsOnBoundary list. | 
|---|
| 1718 | * \param *Walker point to add | 
|---|
| 1719 | * \param n TesselStruct::BPS index to put pointer into | 
|---|
| 1720 | * \return true - new point was added, false - point already present | 
|---|
| 1721 | */ | 
|---|
| 1722 | bool Tesselation::AddBoundaryPoint(TesselPoint * Walker, const int n) | 
|---|
| 1723 | { | 
|---|
| 1724 | Info FunctionInfo(__func__); | 
|---|
| 1725 | PointTestPair InsertUnique; | 
|---|
| 1726 | BPS[n] = new class BoundaryPointSet(Walker); | 
|---|
| 1727 | InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[n])); | 
|---|
| 1728 | if (InsertUnique.second) { // if new point was not present before, increase counter | 
|---|
| 1729 | PointsOnBoundaryCount++; | 
|---|
| 1730 | return true; | 
|---|
| 1731 | } else { | 
|---|
| 1732 | delete (BPS[n]); | 
|---|
| 1733 | BPS[n] = InsertUnique.first->second; | 
|---|
| 1734 | return false; | 
|---|
| 1735 | } | 
|---|
| 1736 | } | 
|---|
| 1737 | ; | 
|---|
| 1738 |  | 
|---|
| 1739 | /** Adds point to Tesselation::PointsOnBoundary if not yet present. | 
|---|
| 1740 | * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique. | 
|---|
| 1741 | * @param Candidate point to add | 
|---|
| 1742 | * @param n index for this point in Tesselation::TPS array | 
|---|
| 1743 | */ | 
|---|
| 1744 | void Tesselation::AddTesselationPoint(TesselPoint* Candidate, const int n) | 
|---|
| 1745 | { | 
|---|
| 1746 | Info FunctionInfo(__func__); | 
|---|
| 1747 | PointTestPair InsertUnique; | 
|---|
| 1748 | TPS[n] = new class BoundaryPointSet(Candidate); | 
|---|
| 1749 | InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n])); | 
|---|
| 1750 | if (InsertUnique.second) { // if new point was not present before, increase counter | 
|---|
| 1751 | PointsOnBoundaryCount++; | 
|---|
| 1752 | } else { | 
|---|
| 1753 | delete TPS[n]; | 
|---|
| 1754 | DoLog(0) && (Log() << Verbose(0) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl); | 
|---|
| 1755 | TPS[n] = (InsertUnique.first)->second; | 
|---|
| 1756 | } | 
|---|
| 1757 | } | 
|---|
| 1758 | ; | 
|---|
| 1759 |  | 
|---|
| 1760 | /** Sets point to a present Tesselation::PointsOnBoundary. | 
|---|
| 1761 | * Tesselation::TPS is set to the existing one or NULL if not found. | 
|---|
| 1762 | * @param Candidate point to set to | 
|---|
| 1763 | * @param n index for this point in Tesselation::TPS array | 
|---|
| 1764 | */ | 
|---|
| 1765 | void Tesselation::SetTesselationPoint(TesselPoint* Candidate, const int n) const | 
|---|
| 1766 | { | 
|---|
| 1767 | Info FunctionInfo(__func__); | 
|---|
| 1768 | PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidate->nr); | 
|---|
| 1769 | if (FindPoint != PointsOnBoundary.end()) | 
|---|
| 1770 | TPS[n] = FindPoint->second; | 
|---|
| 1771 | else | 
|---|
| 1772 | TPS[n] = NULL; | 
|---|
| 1773 | } | 
|---|
| 1774 | ; | 
|---|
| 1775 |  | 
|---|
| 1776 | /** Function tries to add line from current Points in BPS to BoundaryLineSet. | 
|---|
| 1777 | * If successful it raises the line count and inserts the new line into the BLS, | 
|---|
| 1778 | * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one. | 
|---|
| 1779 | * @param *OptCenter desired OptCenter if there are more than one candidate line | 
|---|
| 1780 | * @param *candidate third point of the triangle to be, for checking between multiple open line candidates | 
|---|
| 1781 | * @param *a first endpoint | 
|---|
| 1782 | * @param *b second endpoint | 
|---|
| 1783 | * @param n index of Tesselation::BLS giving the line with both endpoints | 
|---|
| 1784 | */ | 
|---|
| 1785 | void Tesselation::AddTesselationLine(const Vector * const OptCenter, const BoundaryPointSet * const candidate, class BoundaryPointSet *a, class BoundaryPointSet *b, const int n) | 
|---|
| 1786 | { | 
|---|
| 1787 | bool insertNewLine = true; | 
|---|
| 1788 | LineMap::iterator FindLine = a->lines.find(b->node->nr); | 
|---|
| 1789 | BoundaryLineSet *WinningLine = NULL; | 
|---|
| 1790 | if (FindLine != a->lines.end()) { | 
|---|
| 1791 | DoLog(1) && (Log() << Verbose(1) << "INFO: There is at least one line between " << *a << " and " << *b << ": " << *(FindLine->second) << "." << endl); | 
|---|
| 1792 |  | 
|---|
| 1793 | pair<LineMap::iterator, LineMap::iterator> FindPair; | 
|---|
| 1794 | FindPair = a->lines.equal_range(b->node->nr); | 
|---|
| 1795 |  | 
|---|
| 1796 | for (FindLine = FindPair.first; (FindLine != FindPair.second) && (insertNewLine); FindLine++) { | 
|---|
| 1797 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl); | 
|---|
| 1798 | // If there is a line with less than two attached triangles, we don't need a new line. | 
|---|
| 1799 | if (FindLine->second->triangles.size() == 1) { | 
|---|
| 1800 | CandidateMap::iterator Finder = OpenLines.find(FindLine->second); | 
|---|
| 1801 | if (!Finder->second->pointlist.empty()) | 
|---|
| 1802 | DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl); | 
|---|
| 1803 | else | 
|---|
| 1804 | DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate." << endl); | 
|---|
| 1805 | // get open line | 
|---|
| 1806 | for (TesselPointList::const_iterator CandidateChecker = Finder->second->pointlist.begin(); CandidateChecker != Finder->second->pointlist.end(); ++CandidateChecker) { | 
|---|
| 1807 | if ((*(CandidateChecker) == candidate->node) && (OptCenter == NULL || OptCenter->DistanceSquared(Finder->second->OptCenter) < MYEPSILON )) { // stop searching if candidate matches | 
|---|
| 1808 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Candidate " << *(*CandidateChecker) << " has the right center " << Finder->second->OptCenter << "." << endl); | 
|---|
| 1809 | insertNewLine = false; | 
|---|
| 1810 | WinningLine = FindLine->second; | 
|---|
| 1811 | break; | 
|---|
| 1812 | } else { | 
|---|
| 1813 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *(*CandidateChecker) << "'s center " << Finder->second->OptCenter << " does not match desired on " << *OptCenter << "." << endl); | 
|---|
| 1814 | } | 
|---|
| 1815 | } | 
|---|
| 1816 | } | 
|---|
| 1817 | } | 
|---|
| 1818 | } | 
|---|
| 1819 |  | 
|---|
| 1820 | if (insertNewLine) { | 
|---|
| 1821 | AddNewTesselationTriangleLine(a, b, n); | 
|---|
| 1822 | } else { | 
|---|
| 1823 | AddExistingTesselationTriangleLine(WinningLine, n); | 
|---|
| 1824 | } | 
|---|
| 1825 | } | 
|---|
| 1826 | ; | 
|---|
| 1827 |  | 
|---|
| 1828 | /** | 
|---|
| 1829 | * Adds lines from each of the current points in the BPS to BoundaryLineSet. | 
|---|
| 1830 | * Raises the line count and inserts the new line into the BLS. | 
|---|
| 1831 | * | 
|---|
| 1832 | * @param *a first endpoint | 
|---|
| 1833 | * @param *b second endpoint | 
|---|
| 1834 | * @param n index of Tesselation::BLS giving the line with both endpoints | 
|---|
| 1835 | */ | 
|---|
| 1836 | void Tesselation::AddNewTesselationTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, const int n) | 
|---|
| 1837 | { | 
|---|
| 1838 | Info FunctionInfo(__func__); | 
|---|
| 1839 | DoLog(0) && (Log() << Verbose(0) << "Adding open line [" << LinesOnBoundaryCount << "|" << *(a->node) << " and " << *(b->node) << "." << endl); | 
|---|
| 1840 | BPS[0] = a; | 
|---|
| 1841 | BPS[1] = b; | 
|---|
| 1842 | BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); // this also adds the line to the local maps | 
|---|
| 1843 | // add line to global map | 
|---|
| 1844 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n])); | 
|---|
| 1845 | // increase counter | 
|---|
| 1846 | LinesOnBoundaryCount++; | 
|---|
| 1847 | // also add to open lines | 
|---|
| 1848 | CandidateForTesselation *CFT = new CandidateForTesselation(BLS[n]); | 
|---|
| 1849 | OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (BLS[n], CFT)); | 
|---|
| 1850 | } | 
|---|
| 1851 | ; | 
|---|
| 1852 |  | 
|---|
| 1853 | /** Uses an existing line for a new triangle. | 
|---|
| 1854 | * Sets Tesselation::BLS[\a n] and removes the lines from Tesselation::OpenLines. | 
|---|
| 1855 | * \param *FindLine the line to add | 
|---|
| 1856 | * \param n index of the line to set in Tesselation::BLS | 
|---|
| 1857 | */ | 
|---|
| 1858 | void Tesselation::AddExistingTesselationTriangleLine(class BoundaryLineSet *Line, int n) | 
|---|
| 1859 | { | 
|---|
| 1860 | Info FunctionInfo(__func__); | 
|---|
| 1861 | DoLog(0) && (Log() << Verbose(0) << "Using existing line " << *Line << endl); | 
|---|
| 1862 |  | 
|---|
| 1863 | // set endpoints and line | 
|---|
| 1864 | BPS[0] = Line->endpoints[0]; | 
|---|
| 1865 | BPS[1] = Line->endpoints[1]; | 
|---|
| 1866 | BLS[n] = Line; | 
|---|
| 1867 | // remove existing line from OpenLines | 
|---|
| 1868 | CandidateMap::iterator CandidateLine = OpenLines.find(BLS[n]); | 
|---|
| 1869 | if (CandidateLine != OpenLines.end()) { | 
|---|
| 1870 | DoLog(1) && (Log() << Verbose(1) << " Removing line from OpenLines." << endl); | 
|---|
| 1871 | delete (CandidateLine->second); | 
|---|
| 1872 | OpenLines.erase(CandidateLine); | 
|---|
| 1873 | } else { | 
|---|
| 1874 | DoeLog(1) && (eLog() << Verbose(1) << "Line exists and is attached to less than two triangles, but not in OpenLines!" << endl); | 
|---|
| 1875 | } | 
|---|
| 1876 | } | 
|---|
| 1877 | ; | 
|---|
| 1878 |  | 
|---|
| 1879 | /** Function adds triangle to global list. | 
|---|
| 1880 | * Furthermore, the triangle receives the next free id and id counter \a TrianglesOnBoundaryCount is increased. | 
|---|
| 1881 | */ | 
|---|
| 1882 | void Tesselation::AddTesselationTriangle() | 
|---|
| 1883 | { | 
|---|
| 1884 | Info FunctionInfo(__func__); | 
|---|
| 1885 | DoLog(1) && (Log() << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl); | 
|---|
| 1886 |  | 
|---|
| 1887 | // add triangle to global map | 
|---|
| 1888 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 1889 | TrianglesOnBoundaryCount++; | 
|---|
| 1890 |  | 
|---|
| 1891 | // set as last new triangle | 
|---|
| 1892 | LastTriangle = BTS; | 
|---|
| 1893 |  | 
|---|
| 1894 | // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet | 
|---|
| 1895 | } | 
|---|
| 1896 | ; | 
|---|
| 1897 |  | 
|---|
| 1898 | /** Function adds triangle to global list. | 
|---|
| 1899 | * Furthermore, the triangle number is set to \a nr. | 
|---|
| 1900 | * \param nr triangle number | 
|---|
| 1901 | */ | 
|---|
| 1902 | void Tesselation::AddTesselationTriangle(const int nr) | 
|---|
| 1903 | { | 
|---|
| 1904 | Info FunctionInfo(__func__); | 
|---|
| 1905 | DoLog(0) && (Log() << Verbose(0) << "Adding triangle to global TrianglesOnBoundary map." << endl); | 
|---|
| 1906 |  | 
|---|
| 1907 | // add triangle to global map | 
|---|
| 1908 | TrianglesOnBoundary.insert(TrianglePair(nr, BTS)); | 
|---|
| 1909 |  | 
|---|
| 1910 | // set as last new triangle | 
|---|
| 1911 | LastTriangle = BTS; | 
|---|
| 1912 |  | 
|---|
| 1913 | // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet | 
|---|
| 1914 | } | 
|---|
| 1915 | ; | 
|---|
| 1916 |  | 
|---|
| 1917 | /** Removes a triangle from the tesselation. | 
|---|
| 1918 | * Removes itself from the TriangleMap's of its lines, calls for them RemoveTriangleLine() if they are no more connected. | 
|---|
| 1919 | * Removes itself from memory. | 
|---|
| 1920 | * \param *triangle to remove | 
|---|
| 1921 | */ | 
|---|
| 1922 | void Tesselation::RemoveTesselationTriangle(class BoundaryTriangleSet *triangle) | 
|---|
| 1923 | { | 
|---|
| 1924 | Info FunctionInfo(__func__); | 
|---|
| 1925 | if (triangle == NULL) | 
|---|
| 1926 | return; | 
|---|
| 1927 | for (int i = 0; i < 3; i++) { | 
|---|
| 1928 | if (triangle->lines[i] != NULL) { | 
|---|
| 1929 | DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr." << triangle->Nr << " in line " << *triangle->lines[i] << "." << endl); | 
|---|
| 1930 | triangle->lines[i]->triangles.erase(triangle->Nr); | 
|---|
| 1931 | if (triangle->lines[i]->triangles.empty()) { | 
|---|
| 1932 | DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is no more attached to any triangle, erasing." << endl); | 
|---|
| 1933 | RemoveTesselationLine(triangle->lines[i]); | 
|---|
| 1934 | } else { | 
|---|
| 1935 | DoLog(0) && (Log() << Verbose(0) << *triangle->lines[i] << " is still attached to another triangle: "); | 
|---|
| 1936 | OpenLines.insert(pair<BoundaryLineSet *, CandidateForTesselation *> (triangle->lines[i], NULL)); | 
|---|
| 1937 | for (TriangleMap::iterator TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); TriangleRunner++) | 
|---|
| 1938 | DoLog(0) && (Log() << Verbose(0) << "[" << (TriangleRunner->second)->Nr << "|" << *((TriangleRunner->second)->endpoints[0]) << ", " << *((TriangleRunner->second)->endpoints[1]) << ", " << *((TriangleRunner->second)->endpoints[2]) << "] \t"); | 
|---|
| 1939 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 1940 | //        for (int j=0;j<2;j++) { | 
|---|
| 1941 | //          Log() << Verbose(0) << "Lines of endpoint " << *(triangle->lines[i]->endpoints[j]) << ": "; | 
|---|
| 1942 | //          for(LineMap::iterator LineRunner = triangle->lines[i]->endpoints[j]->lines.begin(); LineRunner != triangle->lines[i]->endpoints[j]->lines.end(); LineRunner++) | 
|---|
| 1943 | //            Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t"; | 
|---|
| 1944 | //          Log() << Verbose(0) << endl; | 
|---|
| 1945 | //        } | 
|---|
| 1946 | } | 
|---|
| 1947 | triangle->lines[i] = NULL; // free'd or not: disconnect | 
|---|
| 1948 | } else | 
|---|
| 1949 | DoeLog(1) && (eLog() << Verbose(1) << "This line " << i << " has already been free'd." << endl); | 
|---|
| 1950 | } | 
|---|
| 1951 |  | 
|---|
| 1952 | if (TrianglesOnBoundary.erase(triangle->Nr)) | 
|---|
| 1953 | DoLog(0) && (Log() << Verbose(0) << "Removing triangle Nr. " << triangle->Nr << "." << endl); | 
|---|
| 1954 | delete (triangle); | 
|---|
| 1955 | } | 
|---|
| 1956 | ; | 
|---|
| 1957 |  | 
|---|
| 1958 | /** Removes a line from the tesselation. | 
|---|
| 1959 | * Removes itself from each endpoints' LineMap, then removes itself from global LinesOnBoundary list and free's the line. | 
|---|
| 1960 | * \param *line line to remove | 
|---|
| 1961 | */ | 
|---|
| 1962 | void Tesselation::RemoveTesselationLine(class BoundaryLineSet *line) | 
|---|
| 1963 | { | 
|---|
| 1964 | Info FunctionInfo(__func__); | 
|---|
| 1965 | int Numbers[2]; | 
|---|
| 1966 |  | 
|---|
| 1967 | if (line == NULL) | 
|---|
| 1968 | return; | 
|---|
| 1969 | // get other endpoint number for finding copies of same line | 
|---|
| 1970 | if (line->endpoints[1] != NULL) | 
|---|
| 1971 | Numbers[0] = line->endpoints[1]->Nr; | 
|---|
| 1972 | else | 
|---|
| 1973 | Numbers[0] = -1; | 
|---|
| 1974 | if (line->endpoints[0] != NULL) | 
|---|
| 1975 | Numbers[1] = line->endpoints[0]->Nr; | 
|---|
| 1976 | else | 
|---|
| 1977 | Numbers[1] = -1; | 
|---|
| 1978 |  | 
|---|
| 1979 | for (int i = 0; i < 2; i++) { | 
|---|
| 1980 | if (line->endpoints[i] != NULL) { | 
|---|
| 1981 | if (Numbers[i] != -1) { // as there may be multiple lines with same endpoints, we have to go through each and find in the endpoint's line list this line set | 
|---|
| 1982 | pair<LineMap::iterator, LineMap::iterator> erasor = line->endpoints[i]->lines.equal_range(Numbers[i]); | 
|---|
| 1983 | for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++) | 
|---|
| 1984 | if ((*Runner).second == line) { | 
|---|
| 1985 | DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl); | 
|---|
| 1986 | line->endpoints[i]->lines.erase(Runner); | 
|---|
| 1987 | break; | 
|---|
| 1988 | } | 
|---|
| 1989 | } else { // there's just a single line left | 
|---|
| 1990 | if (line->endpoints[i]->lines.erase(line->Nr)) | 
|---|
| 1991 | DoLog(0) && (Log() << Verbose(0) << "Removing Line Nr. " << line->Nr << " in boundary point " << *line->endpoints[i] << "." << endl); | 
|---|
| 1992 | } | 
|---|
| 1993 | if (line->endpoints[i]->lines.empty()) { | 
|---|
| 1994 | DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has no more lines it's attached to, erasing." << endl); | 
|---|
| 1995 | RemoveTesselationPoint(line->endpoints[i]); | 
|---|
| 1996 | } else { | 
|---|
| 1997 | DoLog(0) && (Log() << Verbose(0) << *line->endpoints[i] << " has still lines it's attached to: "); | 
|---|
| 1998 | for (LineMap::iterator LineRunner = line->endpoints[i]->lines.begin(); LineRunner != line->endpoints[i]->lines.end(); LineRunner++) | 
|---|
| 1999 | DoLog(0) && (Log() << Verbose(0) << "[" << *(LineRunner->second) << "] \t"); | 
|---|
| 2000 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 2001 | } | 
|---|
| 2002 | line->endpoints[i] = NULL; // free'd or not: disconnect | 
|---|
| 2003 | } else | 
|---|
| 2004 | DoeLog(1) && (eLog() << Verbose(1) << "Endpoint " << i << " has already been free'd." << endl); | 
|---|
| 2005 | } | 
|---|
| 2006 | if (!line->triangles.empty()) | 
|---|
| 2007 | DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *line << " am still connected to some triangles." << endl); | 
|---|
| 2008 |  | 
|---|
| 2009 | if (LinesOnBoundary.erase(line->Nr)) | 
|---|
| 2010 | DoLog(0) && (Log() << Verbose(0) << "Removing line Nr. " << line->Nr << "." << endl); | 
|---|
| 2011 | delete (line); | 
|---|
| 2012 | } | 
|---|
| 2013 | ; | 
|---|
| 2014 |  | 
|---|
| 2015 | /** Removes a point from the tesselation. | 
|---|
| 2016 | * Checks whether there are still lines connected, removes from global PointsOnBoundary list, then free's the point. | 
|---|
| 2017 | * \note If a point should be removed, while keep the tesselated surface intact (i.e. closed), use RemovePointFromTesselatedSurface() | 
|---|
| 2018 | * \param *point point to remove | 
|---|
| 2019 | */ | 
|---|
| 2020 | void Tesselation::RemoveTesselationPoint(class BoundaryPointSet *point) | 
|---|
| 2021 | { | 
|---|
| 2022 | Info FunctionInfo(__func__); | 
|---|
| 2023 | if (point == NULL) | 
|---|
| 2024 | return; | 
|---|
| 2025 | if (PointsOnBoundary.erase(point->Nr)) | 
|---|
| 2026 | DoLog(0) && (Log() << Verbose(0) << "Removing point Nr. " << point->Nr << "." << endl); | 
|---|
| 2027 | delete (point); | 
|---|
| 2028 | } | 
|---|
| 2029 | ; | 
|---|
| 2030 |  | 
|---|
| 2031 | /** Checks validity of a given sphere of a candidate line. | 
|---|
| 2032 | * \sa CandidateForTesselation::CheckValidity(), which is more evolved. | 
|---|
| 2033 | * We check CandidateForTesselation::OtherOptCenter | 
|---|
| 2034 | * \param &CandidateLine contains other degenerated candidates which we have to subtract as well | 
|---|
| 2035 | * \param RADIUS radius of sphere | 
|---|
| 2036 | * \param *LC LinkedCell structure with other atoms | 
|---|
| 2037 | * \return true - candidate triangle is degenerated, false - candidate triangle is not degenerated | 
|---|
| 2038 | */ | 
|---|
| 2039 | bool Tesselation::CheckDegeneracy(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) const | 
|---|
| 2040 | { | 
|---|
| 2041 | Info FunctionInfo(__func__); | 
|---|
| 2042 |  | 
|---|
| 2043 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking whether sphere contains no others points ..." << endl); | 
|---|
| 2044 | bool flag = true; | 
|---|
| 2045 |  | 
|---|
| 2046 | DoLog(1) && (Log() << Verbose(1) << "Check by: draw sphere {" << CandidateLine.OtherOptCenter[0] << " " << CandidateLine.OtherOptCenter[1] << " " << CandidateLine.OtherOptCenter[2] << "} radius " << RADIUS << " resolution 30" << endl); | 
|---|
| 2047 | // get all points inside the sphere | 
|---|
| 2048 | TesselPointList *ListofPoints = LC->GetPointsInsideSphere(RADIUS, &CandidateLine.OtherOptCenter); | 
|---|
| 2049 |  | 
|---|
| 2050 | DoLog(1) && (Log() << Verbose(1) << "The following atoms are inside sphere at " << CandidateLine.OtherOptCenter << ":" << endl); | 
|---|
| 2051 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
| 2052 | DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(CandidateLine.OtherOptCenter) << "." << endl); | 
|---|
| 2053 |  | 
|---|
| 2054 | // remove triangles's endpoints | 
|---|
| 2055 | for (int i = 0; i < 2; i++) | 
|---|
| 2056 | ListofPoints->remove(CandidateLine.BaseLine->endpoints[i]->node); | 
|---|
| 2057 |  | 
|---|
| 2058 | // remove other candidates | 
|---|
| 2059 | for (TesselPointList::const_iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); ++Runner) | 
|---|
| 2060 | ListofPoints->remove(*Runner); | 
|---|
| 2061 |  | 
|---|
| 2062 | // check for other points | 
|---|
| 2063 | if (!ListofPoints->empty()) { | 
|---|
| 2064 | DoLog(1) && (Log() << Verbose(1) << "CheckDegeneracy: There are still " << ListofPoints->size() << " points inside the sphere." << endl); | 
|---|
| 2065 | flag = false; | 
|---|
| 2066 | DoLog(1) && (Log() << Verbose(1) << "External atoms inside of sphere at " << CandidateLine.OtherOptCenter << ":" << endl); | 
|---|
| 2067 | for (TesselPointList::const_iterator Runner = ListofPoints->begin(); Runner != ListofPoints->end(); ++Runner) | 
|---|
| 2068 | DoLog(1) && (Log() << Verbose(1) << "  " << *(*Runner) << " with distance " << (*Runner)->node->Distance(CandidateLine.OtherOptCenter) << "." << endl); | 
|---|
| 2069 | } | 
|---|
| 2070 | delete (ListofPoints); | 
|---|
| 2071 |  | 
|---|
| 2072 | return flag; | 
|---|
| 2073 | } | 
|---|
| 2074 | ; | 
|---|
| 2075 |  | 
|---|
| 2076 | /** Checks whether the triangle consisting of the three points is already present. | 
|---|
| 2077 | * Searches for the points in Tesselation::PointsOnBoundary and checks their | 
|---|
| 2078 | * lines. If any of the three edges already has two triangles attached, false is | 
|---|
| 2079 | * returned. | 
|---|
| 2080 | * \param *out output stream for debugging | 
|---|
| 2081 | * \param *Candidates endpoints of the triangle candidate | 
|---|
| 2082 | * \return integer 0 if no triangle exists, 1 if one triangle exists, 2 if two | 
|---|
| 2083 | *                 triangles exist which is the maximum for three points | 
|---|
| 2084 | */ | 
|---|
| 2085 | int Tesselation::CheckPresenceOfTriangle(TesselPoint *Candidates[3]) const | 
|---|
| 2086 | { | 
|---|
| 2087 | Info FunctionInfo(__func__); | 
|---|
| 2088 | int adjacentTriangleCount = 0; | 
|---|
| 2089 | class BoundaryPointSet *Points[3]; | 
|---|
| 2090 |  | 
|---|
| 2091 | // builds a triangle point set (Points) of the end points | 
|---|
| 2092 | for (int i = 0; i < 3; i++) { | 
|---|
| 2093 | PointMap::const_iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr); | 
|---|
| 2094 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 2095 | Points[i] = FindPoint->second; | 
|---|
| 2096 | } else { | 
|---|
| 2097 | Points[i] = NULL; | 
|---|
| 2098 | } | 
|---|
| 2099 | } | 
|---|
| 2100 |  | 
|---|
| 2101 | // checks lines between the points in the Points for their adjacent triangles | 
|---|
| 2102 | for (int i = 0; i < 3; i++) { | 
|---|
| 2103 | if (Points[i] != NULL) { | 
|---|
| 2104 | for (int j = i; j < 3; j++) { | 
|---|
| 2105 | if (Points[j] != NULL) { | 
|---|
| 2106 | LineMap::const_iterator FindLine = Points[i]->lines.find(Points[j]->node->nr); | 
|---|
| 2107 | for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) { | 
|---|
| 2108 | TriangleMap *triangles = &FindLine->second->triangles; | 
|---|
| 2109 | DoLog(1) && (Log() << Verbose(1) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl); | 
|---|
| 2110 | for (TriangleMap::const_iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) { | 
|---|
| 2111 | if (FindTriangle->second->IsPresentTupel(Points)) { | 
|---|
| 2112 | adjacentTriangleCount++; | 
|---|
| 2113 | } | 
|---|
| 2114 | } | 
|---|
| 2115 | DoLog(1) && (Log() << Verbose(1) << "end." << endl); | 
|---|
| 2116 | } | 
|---|
| 2117 | // Only one of the triangle lines must be considered for the triangle count. | 
|---|
| 2118 | //Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl; | 
|---|
| 2119 | //return adjacentTriangleCount; | 
|---|
| 2120 | } | 
|---|
| 2121 | } | 
|---|
| 2122 | } | 
|---|
| 2123 | } | 
|---|
| 2124 |  | 
|---|
| 2125 | DoLog(0) && (Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl); | 
|---|
| 2126 | return adjacentTriangleCount; | 
|---|
| 2127 | } | 
|---|
| 2128 | ; | 
|---|
| 2129 |  | 
|---|
| 2130 | /** Checks whether the triangle consisting of the three points is already present. | 
|---|
| 2131 | * Searches for the points in Tesselation::PointsOnBoundary and checks their | 
|---|
| 2132 | * lines. If any of the three edges already has two triangles attached, false is | 
|---|
| 2133 | * returned. | 
|---|
| 2134 | * \param *out output stream for debugging | 
|---|
| 2135 | * \param *Candidates endpoints of the triangle candidate | 
|---|
| 2136 | * \return NULL - none found or pointer to triangle | 
|---|
| 2137 | */ | 
|---|
| 2138 | class BoundaryTriangleSet * Tesselation::GetPresentTriangle(TesselPoint *Candidates[3]) | 
|---|
| 2139 | { | 
|---|
| 2140 | Info FunctionInfo(__func__); | 
|---|
| 2141 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
| 2142 | class BoundaryPointSet *Points[3]; | 
|---|
| 2143 |  | 
|---|
| 2144 | // builds a triangle point set (Points) of the end points | 
|---|
| 2145 | for (int i = 0; i < 3; i++) { | 
|---|
| 2146 | PointMap::iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr); | 
|---|
| 2147 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 2148 | Points[i] = FindPoint->second; | 
|---|
| 2149 | } else { | 
|---|
| 2150 | Points[i] = NULL; | 
|---|
| 2151 | } | 
|---|
| 2152 | } | 
|---|
| 2153 |  | 
|---|
| 2154 | // checks lines between the points in the Points for their adjacent triangles | 
|---|
| 2155 | for (int i = 0; i < 3; i++) { | 
|---|
| 2156 | if (Points[i] != NULL) { | 
|---|
| 2157 | for (int j = i; j < 3; j++) { | 
|---|
| 2158 | if (Points[j] != NULL) { | 
|---|
| 2159 | LineMap::iterator FindLine = Points[i]->lines.find(Points[j]->node->nr); | 
|---|
| 2160 | for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) { | 
|---|
| 2161 | TriangleMap *triangles = &FindLine->second->triangles; | 
|---|
| 2162 | for (TriangleMap::iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) { | 
|---|
| 2163 | if (FindTriangle->second->IsPresentTupel(Points)) { | 
|---|
| 2164 | if ((triangle == NULL) || (triangle->Nr > FindTriangle->second->Nr)) | 
|---|
| 2165 | triangle = FindTriangle->second; | 
|---|
| 2166 | } | 
|---|
| 2167 | } | 
|---|
| 2168 | } | 
|---|
| 2169 | // Only one of the triangle lines must be considered for the triangle count. | 
|---|
| 2170 | //Log() << Verbose(0) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl; | 
|---|
| 2171 | //return adjacentTriangleCount; | 
|---|
| 2172 | } | 
|---|
| 2173 | } | 
|---|
| 2174 | } | 
|---|
| 2175 | } | 
|---|
| 2176 |  | 
|---|
| 2177 | return triangle; | 
|---|
| 2178 | } | 
|---|
| 2179 | ; | 
|---|
| 2180 |  | 
|---|
| 2181 | /** Finds the starting triangle for FindNonConvexBorder(). | 
|---|
| 2182 | * Looks at the outermost point per axis, then FindSecondPointForTesselation() | 
|---|
| 2183 | * for the second and FindNextSuitablePointViaAngleOfSphere() for the third | 
|---|
| 2184 | * point are called. | 
|---|
| 2185 | * \param *out output stream for debugging | 
|---|
| 2186 | * \param RADIUS radius of virtual rolling sphere | 
|---|
| 2187 | * \param *LC LinkedCell structure with neighbouring TesselPoint's | 
|---|
| 2188 | * \return true - a starting triangle has been created, false - no valid triple of points found | 
|---|
| 2189 | */ | 
|---|
| 2190 | bool Tesselation::FindStartingTriangle(const double RADIUS, const LinkedCell *LC) | 
|---|
| 2191 | { | 
|---|
| 2192 | Info FunctionInfo(__func__); | 
|---|
| 2193 | int i = 0; | 
|---|
| 2194 | TesselPoint* MaxPoint[NDIM]; | 
|---|
| 2195 | TesselPoint* Temporary; | 
|---|
| 2196 | double maxCoordinate[NDIM]; | 
|---|
| 2197 | BoundaryLineSet *BaseLine = NULL; | 
|---|
| 2198 | Vector helper; | 
|---|
| 2199 | Vector Chord; | 
|---|
| 2200 | Vector SearchDirection; | 
|---|
| 2201 | Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers | 
|---|
| 2202 | Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in | 
|---|
| 2203 | Vector SphereCenter; | 
|---|
| 2204 | Vector NormalVector; | 
|---|
| 2205 |  | 
|---|
| 2206 | NormalVector.Zero(); | 
|---|
| 2207 |  | 
|---|
| 2208 | for (i = 0; i < 3; i++) { | 
|---|
| 2209 | MaxPoint[i] = NULL; | 
|---|
| 2210 | maxCoordinate[i] = -1; | 
|---|
| 2211 | } | 
|---|
| 2212 |  | 
|---|
| 2213 | // 1. searching topmost point with respect to each axis | 
|---|
| 2214 | for (int i = 0; i < NDIM; i++) { // each axis | 
|---|
| 2215 | LC->n[i] = LC->N[i] - 1; // current axis is topmost cell | 
|---|
| 2216 | for (LC->n[(i + 1) % NDIM] = 0; LC->n[(i + 1) % NDIM] < LC->N[(i + 1) % NDIM]; LC->n[(i + 1) % NDIM]++) | 
|---|
| 2217 | for (LC->n[(i + 2) % NDIM] = 0; LC->n[(i + 2) % NDIM] < LC->N[(i + 2) % NDIM]; LC->n[(i + 2) % NDIM]++) { | 
|---|
| 2218 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 2219 | //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 2220 | if (List != NULL) { | 
|---|
| 2221 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 2222 | if ((*Runner)->node->at(i) > maxCoordinate[i]) { | 
|---|
| 2223 | DoLog(1) && (Log() << Verbose(1) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl); | 
|---|
| 2224 | maxCoordinate[i] = (*Runner)->node->at(i); | 
|---|
| 2225 | MaxPoint[i] = (*Runner); | 
|---|
| 2226 | } | 
|---|
| 2227 | } | 
|---|
| 2228 | } else { | 
|---|
| 2229 | DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl); | 
|---|
| 2230 | } | 
|---|
| 2231 | } | 
|---|
| 2232 | } | 
|---|
| 2233 |  | 
|---|
| 2234 | DoLog(1) && (Log() << Verbose(1) << "Found maximum coordinates: "); | 
|---|
| 2235 | for (int i = 0; i < NDIM; i++) | 
|---|
| 2236 | DoLog(0) && (Log() << Verbose(0) << i << ": " << *MaxPoint[i] << "\t"); | 
|---|
| 2237 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 2238 |  | 
|---|
| 2239 | BTS = NULL; | 
|---|
| 2240 | for (int k = 0; k < NDIM; k++) { | 
|---|
| 2241 | NormalVector.Zero(); | 
|---|
| 2242 | NormalVector[k] = 1.; | 
|---|
| 2243 | BaseLine = new BoundaryLineSet(); | 
|---|
| 2244 | BaseLine->endpoints[0] = new BoundaryPointSet(MaxPoint[k]); | 
|---|
| 2245 | DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl); | 
|---|
| 2246 |  | 
|---|
| 2247 | double ShortestAngle; | 
|---|
| 2248 | ShortestAngle = 999999.; // This will contain the angle, which will be always positive (when looking for second point), when looking for third point this will be the quadrant. | 
|---|
| 2249 |  | 
|---|
| 2250 | Temporary = NULL; | 
|---|
| 2251 | FindSecondPointForTesselation(BaseLine->endpoints[0]->node, NormalVector, Temporary, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_... | 
|---|
| 2252 | if (Temporary == NULL) { | 
|---|
| 2253 | // have we found a second point? | 
|---|
| 2254 | delete BaseLine; | 
|---|
| 2255 | continue; | 
|---|
| 2256 | } | 
|---|
| 2257 | BaseLine->endpoints[1] = new BoundaryPointSet(Temporary); | 
|---|
| 2258 |  | 
|---|
| 2259 | // construct center of circle | 
|---|
| 2260 | CircleCenter = 0.5 * ((*BaseLine->endpoints[0]->node->node) + (*BaseLine->endpoints[1]->node->node)); | 
|---|
| 2261 |  | 
|---|
| 2262 | // construct normal vector of circle | 
|---|
| 2263 | CirclePlaneNormal = (*BaseLine->endpoints[0]->node->node) - (*BaseLine->endpoints[1]->node->node); | 
|---|
| 2264 |  | 
|---|
| 2265 | double radius = CirclePlaneNormal.NormSquared(); | 
|---|
| 2266 | double CircleRadius = sqrt(RADIUS * RADIUS - radius / 4.); | 
|---|
| 2267 |  | 
|---|
| 2268 | NormalVector.ProjectOntoPlane(CirclePlaneNormal); | 
|---|
| 2269 | NormalVector.Normalize(); | 
|---|
| 2270 | ShortestAngle = 2. * M_PI; // This will indicate the quadrant. | 
|---|
| 2271 |  | 
|---|
| 2272 | SphereCenter = (CircleRadius * NormalVector) +  CircleCenter; | 
|---|
| 2273 | // Now, NormalVector and SphereCenter are two orthonormalized vectors in the plane defined by CirclePlaneNormal (not normalized) | 
|---|
| 2274 |  | 
|---|
| 2275 | // look in one direction of baseline for initial candidate | 
|---|
| 2276 | SearchDirection = Plane(CirclePlaneNormal, NormalVector,0).getNormal();  // whether we look "left" first or "right" first is not important ... | 
|---|
| 2277 |  | 
|---|
| 2278 | // adding point 1 and point 2 and add the line between them | 
|---|
| 2279 | DoLog(0) && (Log() << Verbose(0) << "Coordinates of start node at " << *BaseLine->endpoints[0]->node << "." << endl); | 
|---|
| 2280 | DoLog(0) && (Log() << Verbose(0) << "Found second point is at " << *BaseLine->endpoints[1]->node << ".\n"); | 
|---|
| 2281 |  | 
|---|
| 2282 | //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << helper << ".\n"; | 
|---|
| 2283 | CandidateForTesselation OptCandidates(BaseLine); | 
|---|
| 2284 | FindThirdPointForTesselation(NormalVector, SearchDirection, SphereCenter, OptCandidates, NULL, RADIUS, LC); | 
|---|
| 2285 | DoLog(0) && (Log() << Verbose(0) << "List of third Points is:" << endl); | 
|---|
| 2286 | for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); it++) { | 
|---|
| 2287 | DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl); | 
|---|
| 2288 | } | 
|---|
| 2289 | if (!OptCandidates.pointlist.empty()) { | 
|---|
| 2290 | BTS = NULL; | 
|---|
| 2291 | AddCandidatePolygon(OptCandidates, RADIUS, LC); | 
|---|
| 2292 | } else { | 
|---|
| 2293 | delete BaseLine; | 
|---|
| 2294 | continue; | 
|---|
| 2295 | } | 
|---|
| 2296 |  | 
|---|
| 2297 | if (BTS != NULL) { // we have created one starting triangle | 
|---|
| 2298 | delete BaseLine; | 
|---|
| 2299 | break; | 
|---|
| 2300 | } else { | 
|---|
| 2301 | // remove all candidates from the list and then the list itself | 
|---|
| 2302 | OptCandidates.pointlist.clear(); | 
|---|
| 2303 | } | 
|---|
| 2304 | delete BaseLine; | 
|---|
| 2305 | } | 
|---|
| 2306 |  | 
|---|
| 2307 | return (BTS != NULL); | 
|---|
| 2308 | } | 
|---|
| 2309 | ; | 
|---|
| 2310 |  | 
|---|
| 2311 | /** Checks for a given baseline and a third point candidate whether baselines of the found triangle don't have even better candidates. | 
|---|
| 2312 | * This is supposed to prevent early closing of the tesselation. | 
|---|
| 2313 | * \param CandidateLine CandidateForTesselation with baseline and shortestangle , i.e. not \a *OptCandidate | 
|---|
| 2314 | * \param *ThirdNode third point in triangle, not in BoundaryLineSet::endpoints | 
|---|
| 2315 | * \param RADIUS radius of sphere | 
|---|
| 2316 | * \param *LC LinkedCell structure | 
|---|
| 2317 | * \return true - there is a better candidate (smaller angle than \a ShortestAngle), false - no better TesselPoint candidate found | 
|---|
| 2318 | */ | 
|---|
| 2319 | //bool Tesselation::HasOtherBaselineBetterCandidate(CandidateForTesselation &CandidateLine, const TesselPoint * const ThirdNode, double RADIUS, const LinkedCell * const LC) const | 
|---|
| 2320 | //{ | 
|---|
| 2321 | //      Info FunctionInfo(__func__); | 
|---|
| 2322 | //  bool result = false; | 
|---|
| 2323 | //  Vector CircleCenter; | 
|---|
| 2324 | //  Vector CirclePlaneNormal; | 
|---|
| 2325 | //  Vector OldSphereCenter; | 
|---|
| 2326 | //  Vector SearchDirection; | 
|---|
| 2327 | //  Vector helper; | 
|---|
| 2328 | //  TesselPoint *OtherOptCandidate = NULL; | 
|---|
| 2329 | //  double OtherShortestAngle = 2.*M_PI; // This will indicate the quadrant. | 
|---|
| 2330 | //  double radius, CircleRadius; | 
|---|
| 2331 | //  BoundaryLineSet *Line = NULL; | 
|---|
| 2332 | //  BoundaryTriangleSet *T = NULL; | 
|---|
| 2333 | // | 
|---|
| 2334 | //  // check both other lines | 
|---|
| 2335 | //  PointMap::const_iterator FindPoint = PointsOnBoundary.find(ThirdNode->nr); | 
|---|
| 2336 | //  if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 2337 | //    for (int i=0;i<2;i++) { | 
|---|
| 2338 | //      LineMap::const_iterator FindLine = (FindPoint->second)->lines.find(BaseRay->endpoints[0]->node->nr); | 
|---|
| 2339 | //      if (FindLine != (FindPoint->second)->lines.end()) { | 
|---|
| 2340 | //        Line = FindLine->second; | 
|---|
| 2341 | //        Log() << Verbose(0) << "Found line " << *Line << "." << endl; | 
|---|
| 2342 | //        if (Line->triangles.size() == 1) { | 
|---|
| 2343 | //          T = Line->triangles.begin()->second; | 
|---|
| 2344 | //          // construct center of circle | 
|---|
| 2345 | //          CircleCenter.CopyVector(Line->endpoints[0]->node->node); | 
|---|
| 2346 | //          CircleCenter.AddVector(Line->endpoints[1]->node->node); | 
|---|
| 2347 | //          CircleCenter.Scale(0.5); | 
|---|
| 2348 | // | 
|---|
| 2349 | //          // construct normal vector of circle | 
|---|
| 2350 | //          CirclePlaneNormal.CopyVector(Line->endpoints[0]->node->node); | 
|---|
| 2351 | //          CirclePlaneNormal.SubtractVector(Line->endpoints[1]->node->node); | 
|---|
| 2352 | // | 
|---|
| 2353 | //          // calculate squared radius of circle | 
|---|
| 2354 | //          radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal); | 
|---|
| 2355 | //          if (radius/4. < RADIUS*RADIUS) { | 
|---|
| 2356 | //            CircleRadius = RADIUS*RADIUS - radius/4.; | 
|---|
| 2357 | //            CirclePlaneNormal.Normalize(); | 
|---|
| 2358 | //            //Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl; | 
|---|
| 2359 | // | 
|---|
| 2360 | //            // construct old center | 
|---|
| 2361 | //            GetCenterofCircumcircle(&OldSphereCenter, *T->endpoints[0]->node->node, *T->endpoints[1]->node->node, *T->endpoints[2]->node->node); | 
|---|
| 2362 | //            helper.CopyVector(&T->NormalVector);  // normal vector ensures that this is correct center of the two possible ones | 
|---|
| 2363 | //            radius = Line->endpoints[0]->node->node->DistanceSquared(&OldSphereCenter); | 
|---|
| 2364 | //            helper.Scale(sqrt(RADIUS*RADIUS - radius)); | 
|---|
| 2365 | //            OldSphereCenter.AddVector(&helper); | 
|---|
| 2366 | //            OldSphereCenter.SubtractVector(&CircleCenter); | 
|---|
| 2367 | //            //Log() << Verbose(1) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl; | 
|---|
| 2368 | // | 
|---|
| 2369 | //            // construct SearchDirection | 
|---|
| 2370 | //            SearchDirection.MakeNormalVector(&T->NormalVector, &CirclePlaneNormal); | 
|---|
| 2371 | //            helper.CopyVector(Line->endpoints[0]->node->node); | 
|---|
| 2372 | //            helper.SubtractVector(ThirdNode->node); | 
|---|
| 2373 | //            if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards! | 
|---|
| 2374 | //              SearchDirection.Scale(-1.); | 
|---|
| 2375 | //            SearchDirection.ProjectOntoPlane(&OldSphereCenter); | 
|---|
| 2376 | //            SearchDirection.Normalize(); | 
|---|
| 2377 | //            Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl; | 
|---|
| 2378 | //            if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { | 
|---|
| 2379 | //              // rotated the wrong way! | 
|---|
| 2380 | //              DoeLog(1) && (eLog()<< Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl); | 
|---|
| 2381 | //            } | 
|---|
| 2382 | // | 
|---|
| 2383 | //            // add third point | 
|---|
| 2384 | //            FindThirdPointForTesselation(T->NormalVector, SearchDirection, OldSphereCenter, OptCandidates, ThirdNode, RADIUS, LC); | 
|---|
| 2385 | //            for (TesselPointList::iterator it = OptCandidates.pointlist.begin(); it != OptCandidates.pointlist.end(); ++it) { | 
|---|
| 2386 | //              if (((*it) == BaseRay->endpoints[0]->node) || ((*it) == BaseRay->endpoints[1]->node)) // skip if it's the same triangle than suggested | 
|---|
| 2387 | //                continue; | 
|---|
| 2388 | //              Log() << Verbose(0) << " Third point candidate is " << (*it) | 
|---|
| 2389 | //              << " with circumsphere's center at " << (*it)->OptCenter << "." << endl; | 
|---|
| 2390 | //              Log() << Verbose(0) << " Baseline is " << *BaseRay << endl; | 
|---|
| 2391 | // | 
|---|
| 2392 | //              // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2) | 
|---|
| 2393 | //              TesselPoint *PointCandidates[3]; | 
|---|
| 2394 | //              PointCandidates[0] = (*it); | 
|---|
| 2395 | //              PointCandidates[1] = BaseRay->endpoints[0]->node; | 
|---|
| 2396 | //              PointCandidates[2] = BaseRay->endpoints[1]->node; | 
|---|
| 2397 | //              bool check=false; | 
|---|
| 2398 | //              int existentTrianglesCount = CheckPresenceOfTriangle(PointCandidates); | 
|---|
| 2399 | //              // If there is no triangle, add it regularly. | 
|---|
| 2400 | //              if (existentTrianglesCount == 0) { | 
|---|
| 2401 | //                SetTesselationPoint((*it), 0); | 
|---|
| 2402 | //                SetTesselationPoint(BaseRay->endpoints[0]->node, 1); | 
|---|
| 2403 | //                SetTesselationPoint(BaseRay->endpoints[1]->node, 2); | 
|---|
| 2404 | // | 
|---|
| 2405 | //                if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const )TPS)) { | 
|---|
| 2406 | //                  OtherOptCandidate = (*it); | 
|---|
| 2407 | //                  check = true; | 
|---|
| 2408 | //                } | 
|---|
| 2409 | //              } else if ((existentTrianglesCount >= 1) && (existentTrianglesCount <= 3)) { // If there is a planar region within the structure, we need this triangle a second time. | 
|---|
| 2410 | //                SetTesselationPoint((*it), 0); | 
|---|
| 2411 | //                SetTesselationPoint(BaseRay->endpoints[0]->node, 1); | 
|---|
| 2412 | //                SetTesselationPoint(BaseRay->endpoints[1]->node, 2); | 
|---|
| 2413 | // | 
|---|
| 2414 | //                // We demand that at most one new degenerate line is created and that this line also already exists (which has to be the case due to existentTrianglesCount == 1) | 
|---|
| 2415 | //                // i.e. at least one of the three lines must be present with TriangleCount <= 1 | 
|---|
| 2416 | //                if (CheckLineCriteriaForDegeneratedTriangle((const BoundaryPointSet ** const)TPS)) { | 
|---|
| 2417 | //                  OtherOptCandidate = (*it); | 
|---|
| 2418 | //                  check = true; | 
|---|
| 2419 | //                } | 
|---|
| 2420 | //              } | 
|---|
| 2421 | // | 
|---|
| 2422 | //              if (check) { | 
|---|
| 2423 | //                if (ShortestAngle > OtherShortestAngle) { | 
|---|
| 2424 | //                  Log() << Verbose(0) << "There is a better candidate than " << *ThirdNode << " with " << ShortestAngle << " from baseline " << *Line << ": " << *OtherOptCandidate << " with " << OtherShortestAngle << "." << endl; | 
|---|
| 2425 | //                  result = true; | 
|---|
| 2426 | //                  break; | 
|---|
| 2427 | //                } | 
|---|
| 2428 | //              } | 
|---|
| 2429 | //            } | 
|---|
| 2430 | //            delete(OptCandidates); | 
|---|
| 2431 | //            if (result) | 
|---|
| 2432 | //              break; | 
|---|
| 2433 | //          } else { | 
|---|
| 2434 | //            Log() << Verbose(0) << "Circumcircle for base line " << *Line << " and base triangle " << T << " is too big!" << endl; | 
|---|
| 2435 | //          } | 
|---|
| 2436 | //        } else { | 
|---|
| 2437 | //          DoeLog(2) && (eLog()<< Verbose(2) << "Baseline is connected to two triangles already?" << endl); | 
|---|
| 2438 | //        } | 
|---|
| 2439 | //      } else { | 
|---|
| 2440 | //        Log() << Verbose(1) << "No present baseline between " << BaseRay->endpoints[0] << " and candidate " << *ThirdNode << "." << endl; | 
|---|
| 2441 | //      } | 
|---|
| 2442 | //    } | 
|---|
| 2443 | //  } else { | 
|---|
| 2444 | //    DoeLog(1) && (eLog()<< Verbose(1) << "Could not find the TesselPoint " << *ThirdNode << "." << endl); | 
|---|
| 2445 | //  } | 
|---|
| 2446 | // | 
|---|
| 2447 | //  return result; | 
|---|
| 2448 | //}; | 
|---|
| 2449 |  | 
|---|
| 2450 | /** This function finds a triangle to a line, adjacent to an existing one. | 
|---|
| 2451 | * @param out output stream for debugging | 
|---|
| 2452 | * @param CandidateLine current cadndiate baseline to search from | 
|---|
| 2453 | * @param T current triangle which \a Line is edge of | 
|---|
| 2454 | * @param RADIUS radius of the rolling ball | 
|---|
| 2455 | * @param N number of found triangles | 
|---|
| 2456 | * @param *LC LinkedCell structure with neighbouring points | 
|---|
| 2457 | */ | 
|---|
| 2458 | bool Tesselation::FindNextSuitableTriangle(CandidateForTesselation &CandidateLine, const BoundaryTriangleSet &T, const double& RADIUS, const LinkedCell *LC) | 
|---|
| 2459 | { | 
|---|
| 2460 | Info FunctionInfo(__func__); | 
|---|
| 2461 | Vector CircleCenter; | 
|---|
| 2462 | Vector CirclePlaneNormal; | 
|---|
| 2463 | Vector RelativeSphereCenter; | 
|---|
| 2464 | Vector SearchDirection; | 
|---|
| 2465 | Vector helper; | 
|---|
| 2466 | BoundaryPointSet *ThirdPoint = NULL; | 
|---|
| 2467 | LineMap::iterator testline; | 
|---|
| 2468 | double radius, CircleRadius; | 
|---|
| 2469 |  | 
|---|
| 2470 | for (int i = 0; i < 3; i++) | 
|---|
| 2471 | if ((T.endpoints[i] != CandidateLine.BaseLine->endpoints[0]) && (T.endpoints[i] != CandidateLine.BaseLine->endpoints[1])) { | 
|---|
| 2472 | ThirdPoint = T.endpoints[i]; | 
|---|
| 2473 | break; | 
|---|
| 2474 | } | 
|---|
| 2475 | DoLog(0) && (Log() << Verbose(0) << "Current baseline is " << *CandidateLine.BaseLine << " with ThirdPoint " << *ThirdPoint << " of triangle " << T << "." << endl); | 
|---|
| 2476 |  | 
|---|
| 2477 | CandidateLine.T = &T; | 
|---|
| 2478 |  | 
|---|
| 2479 | // construct center of circle | 
|---|
| 2480 | CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) + | 
|---|
| 2481 | (*CandidateLine.BaseLine->endpoints[1]->node->node)); | 
|---|
| 2482 |  | 
|---|
| 2483 | // construct normal vector of circle | 
|---|
| 2484 | CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) - | 
|---|
| 2485 | (*CandidateLine.BaseLine->endpoints[1]->node->node); | 
|---|
| 2486 |  | 
|---|
| 2487 | // calculate squared radius of circle | 
|---|
| 2488 | radius = CirclePlaneNormal.ScalarProduct(CirclePlaneNormal); | 
|---|
| 2489 | if (radius / 4. < RADIUS * RADIUS) { | 
|---|
| 2490 | // construct relative sphere center with now known CircleCenter | 
|---|
| 2491 | RelativeSphereCenter = T.SphereCenter - CircleCenter; | 
|---|
| 2492 |  | 
|---|
| 2493 | CircleRadius = RADIUS * RADIUS - radius / 4.; | 
|---|
| 2494 | CirclePlaneNormal.Normalize(); | 
|---|
| 2495 | DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl); | 
|---|
| 2496 |  | 
|---|
| 2497 | DoLog(1) && (Log() << Verbose(1) << "INFO: OldSphereCenter is at " << T.SphereCenter << "." << endl); | 
|---|
| 2498 |  | 
|---|
| 2499 | // construct SearchDirection and an "outward pointer" | 
|---|
| 2500 | SearchDirection = Plane(RelativeSphereCenter, CirclePlaneNormal,0).getNormal(); | 
|---|
| 2501 | helper = CircleCenter - (*ThirdPoint->node->node); | 
|---|
| 2502 | if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards! | 
|---|
| 2503 | SearchDirection.Scale(-1.); | 
|---|
| 2504 | DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl); | 
|---|
| 2505 | if (fabs(RelativeSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) { | 
|---|
| 2506 | // rotated the wrong way! | 
|---|
| 2507 | DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl); | 
|---|
| 2508 | } | 
|---|
| 2509 |  | 
|---|
| 2510 | // add third point | 
|---|
| 2511 | FindThirdPointForTesselation(T.NormalVector, SearchDirection, T.SphereCenter, CandidateLine, ThirdPoint, RADIUS, LC); | 
|---|
| 2512 |  | 
|---|
| 2513 | } else { | 
|---|
| 2514 | DoLog(0) && (Log() << Verbose(0) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and base triangle " << T << " is too big!" << endl); | 
|---|
| 2515 | } | 
|---|
| 2516 |  | 
|---|
| 2517 | if (CandidateLine.pointlist.empty()) { | 
|---|
| 2518 | DoeLog(2) && (eLog() << Verbose(2) << "Could not find a suitable candidate." << endl); | 
|---|
| 2519 | return false; | 
|---|
| 2520 | } | 
|---|
| 2521 | DoLog(0) && (Log() << Verbose(0) << "Third Points are: " << endl); | 
|---|
| 2522 | for (TesselPointList::iterator it = CandidateLine.pointlist.begin(); it != CandidateLine.pointlist.end(); ++it) { | 
|---|
| 2523 | DoLog(0) && (Log() << Verbose(0) << " " << *(*it) << endl); | 
|---|
| 2524 | } | 
|---|
| 2525 |  | 
|---|
| 2526 | return true; | 
|---|
| 2527 | } | 
|---|
| 2528 | ; | 
|---|
| 2529 |  | 
|---|
| 2530 | /** Walks through Tesselation::OpenLines() and finds candidates for newly created ones. | 
|---|
| 2531 | * \param *&LCList atoms in LinkedCell list | 
|---|
| 2532 | * \param RADIUS radius of the virtual sphere | 
|---|
| 2533 | * \return true - for all open lines without candidates so far, a candidate has been found, | 
|---|
| 2534 | *         false - at least one open line without candidate still | 
|---|
| 2535 | */ | 
|---|
| 2536 | bool Tesselation::FindCandidatesforOpenLines(const double RADIUS, const LinkedCell *&LCList) | 
|---|
| 2537 | { | 
|---|
| 2538 | bool TesselationFailFlag = true; | 
|---|
| 2539 | CandidateForTesselation *baseline = NULL; | 
|---|
| 2540 | BoundaryTriangleSet *T = NULL; | 
|---|
| 2541 |  | 
|---|
| 2542 | for (CandidateMap::iterator Runner = OpenLines.begin(); Runner != OpenLines.end(); Runner++) { | 
|---|
| 2543 | baseline = Runner->second; | 
|---|
| 2544 | if (baseline->pointlist.empty()) { | 
|---|
| 2545 | assert((baseline->BaseLine->triangles.size() == 1) && ("Open line without exactly one attached triangle")); | 
|---|
| 2546 | T = (((baseline->BaseLine->triangles.begin()))->second); | 
|---|
| 2547 | DoLog(1) && (Log() << Verbose(1) << "Finding best candidate for open line " << *baseline->BaseLine << " of triangle " << *T << endl); | 
|---|
| 2548 | TesselationFailFlag = TesselationFailFlag && FindNextSuitableTriangle(*baseline, *T, RADIUS, LCList); //the line is there, so there is a triangle, but only one. | 
|---|
| 2549 | } | 
|---|
| 2550 | } | 
|---|
| 2551 | return TesselationFailFlag; | 
|---|
| 2552 | } | 
|---|
| 2553 | ; | 
|---|
| 2554 |  | 
|---|
| 2555 | /** Adds the present line and candidate point from \a &CandidateLine to the Tesselation. | 
|---|
| 2556 | * \param CandidateLine triangle to add | 
|---|
| 2557 | * \param RADIUS Radius of sphere | 
|---|
| 2558 | * \param *LC LinkedCell structure | 
|---|
| 2559 | * \NOTE we need the copy operator here as the original CandidateForTesselation is removed in | 
|---|
| 2560 | * AddTesselationLine() in AddCandidateTriangle() | 
|---|
| 2561 | */ | 
|---|
| 2562 | void Tesselation::AddCandidatePolygon(CandidateForTesselation CandidateLine, const double RADIUS, const LinkedCell *LC) | 
|---|
| 2563 | { | 
|---|
| 2564 | Info FunctionInfo(__func__); | 
|---|
| 2565 | Vector Center; | 
|---|
| 2566 | TesselPoint * const TurningPoint = CandidateLine.BaseLine->endpoints[0]->node; | 
|---|
| 2567 | TesselPointList::iterator Runner; | 
|---|
| 2568 | TesselPointList::iterator Sprinter; | 
|---|
| 2569 |  | 
|---|
| 2570 | // fill the set of neighbours | 
|---|
| 2571 | TesselPointSet SetOfNeighbours; | 
|---|
| 2572 | SetOfNeighbours.insert(CandidateLine.BaseLine->endpoints[1]->node); | 
|---|
| 2573 | for (TesselPointList::iterator Runner = CandidateLine.pointlist.begin(); Runner != CandidateLine.pointlist.end(); Runner++) | 
|---|
| 2574 | SetOfNeighbours.insert(*Runner); | 
|---|
| 2575 | TesselPointList *connectedClosestPoints = GetCircleOfSetOfPoints(&SetOfNeighbours, TurningPoint, CandidateLine.BaseLine->endpoints[1]->node->node); | 
|---|
| 2576 |  | 
|---|
| 2577 | DoLog(0) && (Log() << Verbose(0) << "List of Candidates for Turning Point " << *TurningPoint << ":" << endl); | 
|---|
| 2578 | for (TesselPointList::iterator TesselRunner = connectedClosestPoints->begin(); TesselRunner != connectedClosestPoints->end(); ++TesselRunner) | 
|---|
| 2579 | DoLog(0) && (Log() << Verbose(0) << " " << **TesselRunner << endl); | 
|---|
| 2580 |  | 
|---|
| 2581 | // go through all angle-sorted candidates (in degenerate n-nodes case we may have to add multiple triangles) | 
|---|
| 2582 | Runner = connectedClosestPoints->begin(); | 
|---|
| 2583 | Sprinter = Runner; | 
|---|
| 2584 | Sprinter++; | 
|---|
| 2585 | while (Sprinter != connectedClosestPoints->end()) { | 
|---|
| 2586 | DoLog(0) && (Log() << Verbose(0) << "Current Runner is " << *(*Runner) << " and sprinter is " << *(*Sprinter) << "." << endl); | 
|---|
| 2587 |  | 
|---|
| 2588 | AddTesselationPoint(TurningPoint, 0); | 
|---|
| 2589 | AddTesselationPoint(*Runner, 1); | 
|---|
| 2590 | AddTesselationPoint(*Sprinter, 2); | 
|---|
| 2591 |  | 
|---|
| 2592 | AddCandidateTriangle(CandidateLine, Opt); | 
|---|
| 2593 |  | 
|---|
| 2594 | Runner = Sprinter; | 
|---|
| 2595 | Sprinter++; | 
|---|
| 2596 | if (Sprinter != connectedClosestPoints->end()) { | 
|---|
| 2597 | // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked) | 
|---|
| 2598 | FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OptCenter); // Assume BTS contains last triangle | 
|---|
| 2599 | DoLog(0) && (Log() << Verbose(0) << " There are still more triangles to add." << endl); | 
|---|
| 2600 | } | 
|---|
| 2601 | // pick candidates for other open lines as well | 
|---|
| 2602 | FindCandidatesforOpenLines(RADIUS, LC); | 
|---|
| 2603 |  | 
|---|
| 2604 | // check whether we add a degenerate or a normal triangle | 
|---|
| 2605 | if (CheckDegeneracy(CandidateLine, RADIUS, LC)) { | 
|---|
| 2606 | // add normal and degenerate triangles | 
|---|
| 2607 | DoLog(1) && (Log() << Verbose(1) << "Triangle of endpoints " << *TPS[0] << "," << *TPS[1] << " and " << *TPS[2] << " is degenerated, adding both sides." << endl); | 
|---|
| 2608 | AddCandidateTriangle(CandidateLine, OtherOpt); | 
|---|
| 2609 |  | 
|---|
| 2610 | if (Sprinter != connectedClosestPoints->end()) { | 
|---|
| 2611 | // fill the internal open lines with its respective candidate (otherwise lines in degenerate case are not picked) | 
|---|
| 2612 | FindDegeneratedCandidatesforOpenLines(*Sprinter, &CandidateLine.OtherOptCenter); | 
|---|
| 2613 | } | 
|---|
| 2614 | // pick candidates for other open lines as well | 
|---|
| 2615 | FindCandidatesforOpenLines(RADIUS, LC); | 
|---|
| 2616 | } | 
|---|
| 2617 | } | 
|---|
| 2618 | delete (connectedClosestPoints); | 
|---|
| 2619 | }; | 
|---|
| 2620 |  | 
|---|
| 2621 | /** for polygons (multiple candidates for a baseline) sets internal edges to the correct next candidate. | 
|---|
| 2622 | * \param *Sprinter next candidate to which internal open lines are set | 
|---|
| 2623 | * \param *OptCenter OptCenter for this candidate | 
|---|
| 2624 | */ | 
|---|
| 2625 | void Tesselation::FindDegeneratedCandidatesforOpenLines(TesselPoint * const Sprinter, const Vector * const OptCenter) | 
|---|
| 2626 | { | 
|---|
| 2627 | Info FunctionInfo(__func__); | 
|---|
| 2628 |  | 
|---|
| 2629 | pair<LineMap::iterator, LineMap::iterator> FindPair = TPS[0]->lines.equal_range(TPS[2]->node->nr); | 
|---|
| 2630 | for (LineMap::const_iterator FindLine = FindPair.first; FindLine != FindPair.second; FindLine++) { | 
|---|
| 2631 | DoLog(1) && (Log() << Verbose(1) << "INFO: Checking line " << *(FindLine->second) << " ..." << endl); | 
|---|
| 2632 | // If there is a line with less than two attached triangles, we don't need a new line. | 
|---|
| 2633 | if (FindLine->second->triangles.size() == 1) { | 
|---|
| 2634 | CandidateMap::iterator Finder = OpenLines.find(FindLine->second); | 
|---|
| 2635 | if (!Finder->second->pointlist.empty()) | 
|---|
| 2636 | DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with candidate " << **(Finder->second->pointlist.begin()) << "." << endl); | 
|---|
| 2637 | else { | 
|---|
| 2638 | DoLog(1) && (Log() << Verbose(1) << "INFO: line " << *(FindLine->second) << " is open with no candidate, setting to next Sprinter" << (*Sprinter) << endl); | 
|---|
| 2639 | Finder->second->T = BTS;  // is last triangle | 
|---|
| 2640 | Finder->second->pointlist.push_back(Sprinter); | 
|---|
| 2641 | Finder->second->ShortestAngle = 0.; | 
|---|
| 2642 | Finder->second->OptCenter = *OptCenter; | 
|---|
| 2643 | } | 
|---|
| 2644 | } | 
|---|
| 2645 | } | 
|---|
| 2646 | }; | 
|---|
| 2647 |  | 
|---|
| 2648 | /** If a given \a *triangle is degenerated, this adds both sides. | 
|---|
| 2649 | * i.e. the triangle with same BoundaryPointSet's but NormalVector in opposite direction. | 
|---|
| 2650 | * Note that endpoints are stored in Tesselation::TPS | 
|---|
| 2651 | * \param CandidateLine CanddiateForTesselation structure for the desired BoundaryLine | 
|---|
| 2652 | * \param RADIUS radius of sphere | 
|---|
| 2653 | * \param *LC pointer to LinkedCell structure | 
|---|
| 2654 | */ | 
|---|
| 2655 | void Tesselation::AddDegeneratedTriangle(CandidateForTesselation &CandidateLine, const double RADIUS, const LinkedCell *LC) | 
|---|
| 2656 | { | 
|---|
| 2657 | Info FunctionInfo(__func__); | 
|---|
| 2658 | Vector Center; | 
|---|
| 2659 | CandidateMap::const_iterator CandidateCheck = OpenLines.end(); | 
|---|
| 2660 | BoundaryTriangleSet *triangle = NULL; | 
|---|
| 2661 |  | 
|---|
| 2662 | /// 1. Create or pick the lines for the first triangle | 
|---|
| 2663 | DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for first triangle ..." << endl); | 
|---|
| 2664 | for (int i = 0; i < 3; i++) { | 
|---|
| 2665 | BLS[i] = NULL; | 
|---|
| 2666 | DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl); | 
|---|
| 2667 | AddTesselationLine(&CandidateLine.OptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i); | 
|---|
| 2668 | } | 
|---|
| 2669 |  | 
|---|
| 2670 | /// 2. create the first triangle and NormalVector and so on | 
|---|
| 2671 | DoLog(0) && (Log() << Verbose(0) << "INFO: Adding first triangle with center at " << CandidateLine.OptCenter << " ..." << endl); | 
|---|
| 2672 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 2673 | AddTesselationTriangle(); | 
|---|
| 2674 |  | 
|---|
| 2675 | // create normal vector | 
|---|
| 2676 | BTS->GetCenter(&Center); | 
|---|
| 2677 | Center -= CandidateLine.OptCenter; | 
|---|
| 2678 | BTS->SphereCenter = CandidateLine.OptCenter; | 
|---|
| 2679 | BTS->GetNormalVector(Center); | 
|---|
| 2680 | // give some verbose output about the whole procedure | 
|---|
| 2681 | if (CandidateLine.T != NULL) | 
|---|
| 2682 | DoLog(0) && (Log() << Verbose(0) << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl); | 
|---|
| 2683 | else | 
|---|
| 2684 | DoLog(0) && (Log() << Verbose(0) << "--> New starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl); | 
|---|
| 2685 | triangle = BTS; | 
|---|
| 2686 |  | 
|---|
| 2687 | /// 3. Gather candidates for each new line | 
|---|
| 2688 | DoLog(0) && (Log() << Verbose(0) << "INFO: Adding candidates to new lines ..." << endl); | 
|---|
| 2689 | for (int i = 0; i < 3; i++) { | 
|---|
| 2690 | DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl); | 
|---|
| 2691 | CandidateCheck = OpenLines.find(BLS[i]); | 
|---|
| 2692 | if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) { | 
|---|
| 2693 | if (CandidateCheck->second->T == NULL) | 
|---|
| 2694 | CandidateCheck->second->T = triangle; | 
|---|
| 2695 | FindNextSuitableTriangle(*(CandidateCheck->second), *CandidateCheck->second->T, RADIUS, LC); | 
|---|
| 2696 | } | 
|---|
| 2697 | } | 
|---|
| 2698 |  | 
|---|
| 2699 | /// 4. Create or pick the lines for the second triangle | 
|---|
| 2700 | DoLog(0) && (Log() << Verbose(0) << "INFO: Creating/Picking lines for second triangle ..." << endl); | 
|---|
| 2701 | for (int i = 0; i < 3; i++) { | 
|---|
| 2702 | DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl); | 
|---|
| 2703 | AddTesselationLine(&CandidateLine.OtherOptCenter, TPS[(i + 2) % 3], TPS[(i + 0) % 3], TPS[(i + 1) % 3], i); | 
|---|
| 2704 | } | 
|---|
| 2705 |  | 
|---|
| 2706 | /// 5. create the second triangle and NormalVector and so on | 
|---|
| 2707 | DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangle with center at " << CandidateLine.OtherOptCenter << " ..." << endl); | 
|---|
| 2708 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 2709 | AddTesselationTriangle(); | 
|---|
| 2710 |  | 
|---|
| 2711 | BTS->SphereCenter = CandidateLine.OtherOptCenter; | 
|---|
| 2712 | // create normal vector in other direction | 
|---|
| 2713 | BTS->GetNormalVector(triangle->NormalVector); | 
|---|
| 2714 | BTS->NormalVector.Scale(-1.); | 
|---|
| 2715 | // give some verbose output about the whole procedure | 
|---|
| 2716 | if (CandidateLine.T != NULL) | 
|---|
| 2717 | DoLog(0) && (Log() << Verbose(0) << "--> New degenerate triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl); | 
|---|
| 2718 | else | 
|---|
| 2719 | DoLog(0) && (Log() << Verbose(0) << "--> New degenerate starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl); | 
|---|
| 2720 |  | 
|---|
| 2721 | /// 6. Adding triangle to new lines | 
|---|
| 2722 | DoLog(0) && (Log() << Verbose(0) << "INFO: Adding second triangles to new lines ..." << endl); | 
|---|
| 2723 | for (int i = 0; i < 3; i++) { | 
|---|
| 2724 | DoLog(0) && (Log() << Verbose(0) << "Current line is between " << *TPS[(i + 0) % 3] << " and " << *TPS[(i + 1) % 3] << ":" << endl); | 
|---|
| 2725 | CandidateCheck = OpenLines.find(BLS[i]); | 
|---|
| 2726 | if ((CandidateCheck != OpenLines.end()) && (CandidateCheck->second->pointlist.empty())) { | 
|---|
| 2727 | if (CandidateCheck->second->T == NULL) | 
|---|
| 2728 | CandidateCheck->second->T = BTS; | 
|---|
| 2729 | } | 
|---|
| 2730 | } | 
|---|
| 2731 | } | 
|---|
| 2732 | ; | 
|---|
| 2733 |  | 
|---|
| 2734 | /** Adds a triangle to the Tesselation structure from three given TesselPoint's. | 
|---|
| 2735 | * Note that endpoints are in Tesselation::TPS. | 
|---|
| 2736 | * \param CandidateLine CandidateForTesselation structure contains other information | 
|---|
| 2737 | * \param type which opt center to add (i.e. which side) and thus which NormalVector to take | 
|---|
| 2738 | */ | 
|---|
| 2739 | void Tesselation::AddCandidateTriangle(CandidateForTesselation &CandidateLine, enum centers type) | 
|---|
| 2740 | { | 
|---|
| 2741 | Info FunctionInfo(__func__); | 
|---|
| 2742 | Vector Center; | 
|---|
| 2743 | Vector *OptCenter = (type == Opt) ? &CandidateLine.OptCenter : &CandidateLine.OtherOptCenter; | 
|---|
| 2744 |  | 
|---|
| 2745 | // add the lines | 
|---|
| 2746 | AddTesselationLine(OptCenter, TPS[2], TPS[0], TPS[1], 0); | 
|---|
| 2747 | AddTesselationLine(OptCenter, TPS[1], TPS[0], TPS[2], 1); | 
|---|
| 2748 | AddTesselationLine(OptCenter, TPS[0], TPS[1], TPS[2], 2); | 
|---|
| 2749 |  | 
|---|
| 2750 | // add the triangles | 
|---|
| 2751 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 2752 | AddTesselationTriangle(); | 
|---|
| 2753 |  | 
|---|
| 2754 | // create normal vector | 
|---|
| 2755 | BTS->GetCenter(&Center); | 
|---|
| 2756 | Center.SubtractVector(*OptCenter); | 
|---|
| 2757 | BTS->SphereCenter = *OptCenter; | 
|---|
| 2758 | BTS->GetNormalVector(Center); | 
|---|
| 2759 |  | 
|---|
| 2760 | // give some verbose output about the whole procedure | 
|---|
| 2761 | if (CandidateLine.T != NULL) | 
|---|
| 2762 | DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "triangle with " << *BTS << " and normal vector " << BTS->NormalVector << ", from " << *CandidateLine.T << " and angle " << CandidateLine.ShortestAngle << "." << endl); | 
|---|
| 2763 | else | 
|---|
| 2764 | DoLog(0) && (Log() << Verbose(0) << "--> New" << ((type == OtherOpt) ? " degenerate " : " ") << "starting triangle with " << *BTS << " and normal vector " << BTS->NormalVector << " and no top triangle." << endl); | 
|---|
| 2765 | } | 
|---|
| 2766 | ; | 
|---|
| 2767 |  | 
|---|
| 2768 | /** Checks whether the quadragon of the two triangles connect to \a *Base is convex. | 
|---|
| 2769 | * We look whether the closest point on \a *Base with respect to the other baseline is outside | 
|---|
| 2770 | * of the segment formed by both endpoints (concave) or not (convex). | 
|---|
| 2771 | * \param *out output stream for debugging | 
|---|
| 2772 | * \param *Base line to be flipped | 
|---|
| 2773 | * \return NULL - convex, otherwise endpoint that makes it concave | 
|---|
| 2774 | */ | 
|---|
| 2775 | class BoundaryPointSet *Tesselation::IsConvexRectangle(class BoundaryLineSet *Base) | 
|---|
| 2776 | { | 
|---|
| 2777 | Info FunctionInfo(__func__); | 
|---|
| 2778 | class BoundaryPointSet *Spot = NULL; | 
|---|
| 2779 | class BoundaryLineSet *OtherBase; | 
|---|
| 2780 | Vector *ClosestPoint; | 
|---|
| 2781 |  | 
|---|
| 2782 | int m = 0; | 
|---|
| 2783 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 2784 | for (int j = 0; j < 3; j++) // all of their endpoints and baselines | 
|---|
| 2785 | if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints | 
|---|
| 2786 | BPS[m++] = runner->second->endpoints[j]; | 
|---|
| 2787 | OtherBase = new class BoundaryLineSet(BPS, -1); | 
|---|
| 2788 |  | 
|---|
| 2789 | DoLog(1) && (Log() << Verbose(1) << "INFO: Current base line is " << *Base << "." << endl); | 
|---|
| 2790 | DoLog(1) && (Log() << Verbose(1) << "INFO: Other base line is " << *OtherBase << "." << endl); | 
|---|
| 2791 |  | 
|---|
| 2792 | // get the closest point on each line to the other line | 
|---|
| 2793 | ClosestPoint = GetClosestPointBetweenLine(Base, OtherBase); | 
|---|
| 2794 |  | 
|---|
| 2795 | // delete the temporary other base line | 
|---|
| 2796 | delete (OtherBase); | 
|---|
| 2797 |  | 
|---|
| 2798 | // get the distance vector from Base line to OtherBase line | 
|---|
| 2799 | Vector DistanceToIntersection[2], BaseLine; | 
|---|
| 2800 | double distance[2]; | 
|---|
| 2801 | BaseLine = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node); | 
|---|
| 2802 | for (int i = 0; i < 2; i++) { | 
|---|
| 2803 | DistanceToIntersection[i] = (*ClosestPoint) - (*Base->endpoints[i]->node->node); | 
|---|
| 2804 | distance[i] = BaseLine.ScalarProduct(DistanceToIntersection[i]); | 
|---|
| 2805 | } | 
|---|
| 2806 | delete (ClosestPoint); | 
|---|
| 2807 | if ((distance[0] * distance[1]) > 0) { // have same sign? | 
|---|
| 2808 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Both SKPs have same sign: " << distance[0] << " and " << distance[1] << ". " << *Base << "' rectangle is concave." << endl); | 
|---|
| 2809 | if (distance[0] < distance[1]) { | 
|---|
| 2810 | Spot = Base->endpoints[0]; | 
|---|
| 2811 | } else { | 
|---|
| 2812 | Spot = Base->endpoints[1]; | 
|---|
| 2813 | } | 
|---|
| 2814 | return Spot; | 
|---|
| 2815 | } else { // different sign, i.e. we are in between | 
|---|
| 2816 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Rectangle of triangles of base line " << *Base << " is convex." << endl); | 
|---|
| 2817 | return NULL; | 
|---|
| 2818 | } | 
|---|
| 2819 |  | 
|---|
| 2820 | } | 
|---|
| 2821 | ; | 
|---|
| 2822 |  | 
|---|
| 2823 | void Tesselation::PrintAllBoundaryPoints(ofstream *out) const | 
|---|
| 2824 | { | 
|---|
| 2825 | Info FunctionInfo(__func__); | 
|---|
| 2826 | // print all lines | 
|---|
| 2827 | DoLog(0) && (Log() << Verbose(0) << "Printing all boundary points for debugging:" << endl); | 
|---|
| 2828 | for (PointMap::const_iterator PointRunner = PointsOnBoundary.begin(); PointRunner != PointsOnBoundary.end(); PointRunner++) | 
|---|
| 2829 | DoLog(0) && (Log() << Verbose(0) << *(PointRunner->second) << endl); | 
|---|
| 2830 | } | 
|---|
| 2831 | ; | 
|---|
| 2832 |  | 
|---|
| 2833 | void Tesselation::PrintAllBoundaryLines(ofstream *out) const | 
|---|
| 2834 | { | 
|---|
| 2835 | Info FunctionInfo(__func__); | 
|---|
| 2836 | // print all lines | 
|---|
| 2837 | DoLog(0) && (Log() << Verbose(0) << "Printing all boundary lines for debugging:" << endl); | 
|---|
| 2838 | for (LineMap::const_iterator LineRunner = LinesOnBoundary.begin(); LineRunner != LinesOnBoundary.end(); LineRunner++) | 
|---|
| 2839 | DoLog(0) && (Log() << Verbose(0) << *(LineRunner->second) << endl); | 
|---|
| 2840 | } | 
|---|
| 2841 | ; | 
|---|
| 2842 |  | 
|---|
| 2843 | void Tesselation::PrintAllBoundaryTriangles(ofstream *out) const | 
|---|
| 2844 | { | 
|---|
| 2845 | Info FunctionInfo(__func__); | 
|---|
| 2846 | // print all triangles | 
|---|
| 2847 | DoLog(0) && (Log() << Verbose(0) << "Printing all boundary triangles for debugging:" << endl); | 
|---|
| 2848 | for (TriangleMap::const_iterator TriangleRunner = TrianglesOnBoundary.begin(); TriangleRunner != TrianglesOnBoundary.end(); TriangleRunner++) | 
|---|
| 2849 | DoLog(0) && (Log() << Verbose(0) << *(TriangleRunner->second) << endl); | 
|---|
| 2850 | } | 
|---|
| 2851 | ; | 
|---|
| 2852 |  | 
|---|
| 2853 | /** For a given boundary line \a *Base and its two triangles, picks the central baseline that is "higher". | 
|---|
| 2854 | * \param *out output stream for debugging | 
|---|
| 2855 | * \param *Base line to be flipped | 
|---|
| 2856 | * \return volume change due to flipping (0 - then no flipped occured) | 
|---|
| 2857 | */ | 
|---|
| 2858 | double Tesselation::PickFarthestofTwoBaselines(class BoundaryLineSet *Base) | 
|---|
| 2859 | { | 
|---|
| 2860 | Info FunctionInfo(__func__); | 
|---|
| 2861 | class BoundaryLineSet *OtherBase; | 
|---|
| 2862 | Vector *ClosestPoint[2]; | 
|---|
| 2863 | double volume; | 
|---|
| 2864 |  | 
|---|
| 2865 | int m = 0; | 
|---|
| 2866 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 2867 | for (int j = 0; j < 3; j++) // all of their endpoints and baselines | 
|---|
| 2868 | if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) // and neither of its endpoints | 
|---|
| 2869 | BPS[m++] = runner->second->endpoints[j]; | 
|---|
| 2870 | OtherBase = new class BoundaryLineSet(BPS, -1); | 
|---|
| 2871 |  | 
|---|
| 2872 | DoLog(0) && (Log() << Verbose(0) << "INFO: Current base line is " << *Base << "." << endl); | 
|---|
| 2873 | DoLog(0) && (Log() << Verbose(0) << "INFO: Other base line is " << *OtherBase << "." << endl); | 
|---|
| 2874 |  | 
|---|
| 2875 | // get the closest point on each line to the other line | 
|---|
| 2876 | ClosestPoint[0] = GetClosestPointBetweenLine(Base, OtherBase); | 
|---|
| 2877 | ClosestPoint[1] = GetClosestPointBetweenLine(OtherBase, Base); | 
|---|
| 2878 |  | 
|---|
| 2879 | // get the distance vector from Base line to OtherBase line | 
|---|
| 2880 | Vector Distance = (*ClosestPoint[1]) - (*ClosestPoint[0]); | 
|---|
| 2881 |  | 
|---|
| 2882 | // calculate volume | 
|---|
| 2883 | volume = CalculateVolumeofGeneralTetraeder(*Base->endpoints[1]->node->node, *OtherBase->endpoints[0]->node->node, *OtherBase->endpoints[1]->node->node, *Base->endpoints[0]->node->node); | 
|---|
| 2884 |  | 
|---|
| 2885 | // delete the temporary other base line and the closest points | 
|---|
| 2886 | delete (ClosestPoint[0]); | 
|---|
| 2887 | delete (ClosestPoint[1]); | 
|---|
| 2888 | delete (OtherBase); | 
|---|
| 2889 |  | 
|---|
| 2890 | if (Distance.NormSquared() < MYEPSILON) { // check for intersection | 
|---|
| 2891 | DoLog(0) && (Log() << Verbose(0) << "REJECT: Both lines have an intersection: Nothing to do." << endl); | 
|---|
| 2892 | return false; | 
|---|
| 2893 | } else { // check for sign against BaseLineNormal | 
|---|
| 2894 | Vector BaseLineNormal; | 
|---|
| 2895 | BaseLineNormal.Zero(); | 
|---|
| 2896 | if (Base->triangles.size() < 2) { | 
|---|
| 2897 | DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl); | 
|---|
| 2898 | return 0.; | 
|---|
| 2899 | } | 
|---|
| 2900 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) { | 
|---|
| 2901 | DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl); | 
|---|
| 2902 | BaseLineNormal += (runner->second->NormalVector); | 
|---|
| 2903 | } | 
|---|
| 2904 | BaseLineNormal.Scale(1. / 2.); | 
|---|
| 2905 |  | 
|---|
| 2906 | if (Distance.ScalarProduct(BaseLineNormal) > MYEPSILON) { // Distance points outwards, hence OtherBase higher than Base -> flip | 
|---|
| 2907 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Other base line would be higher: Flipping baseline." << endl); | 
|---|
| 2908 | // calculate volume summand as a general tetraeder | 
|---|
| 2909 | return volume; | 
|---|
| 2910 | } else { // Base higher than OtherBase -> do nothing | 
|---|
| 2911 | DoLog(0) && (Log() << Verbose(0) << "REJECT: Base line is higher: Nothing to do." << endl); | 
|---|
| 2912 | return 0.; | 
|---|
| 2913 | } | 
|---|
| 2914 | } | 
|---|
| 2915 | } | 
|---|
| 2916 | ; | 
|---|
| 2917 |  | 
|---|
| 2918 | /** For a given baseline and its two connected triangles, flips the baseline. | 
|---|
| 2919 | * I.e. we create the new baseline between the other two endpoints of these four | 
|---|
| 2920 | * endpoints and reconstruct the two triangles accordingly. | 
|---|
| 2921 | * \param *out output stream for debugging | 
|---|
| 2922 | * \param *Base line to be flipped | 
|---|
| 2923 | * \return pointer to allocated new baseline - flipping successful, NULL - something went awry | 
|---|
| 2924 | */ | 
|---|
| 2925 | class BoundaryLineSet * Tesselation::FlipBaseline(class BoundaryLineSet *Base) | 
|---|
| 2926 | { | 
|---|
| 2927 | Info FunctionInfo(__func__); | 
|---|
| 2928 | class BoundaryLineSet *OldLines[4], *NewLine; | 
|---|
| 2929 | class BoundaryPointSet *OldPoints[2]; | 
|---|
| 2930 | Vector BaseLineNormal; | 
|---|
| 2931 | int OldTriangleNrs[2], OldBaseLineNr; | 
|---|
| 2932 | int i, m; | 
|---|
| 2933 |  | 
|---|
| 2934 | // calculate NormalVector for later use | 
|---|
| 2935 | BaseLineNormal.Zero(); | 
|---|
| 2936 | if (Base->triangles.size() < 2) { | 
|---|
| 2937 | DoeLog(1) && (eLog() << Verbose(1) << "Less than two triangles are attached to this baseline!" << endl); | 
|---|
| 2938 | return NULL; | 
|---|
| 2939 | } | 
|---|
| 2940 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) { | 
|---|
| 2941 | DoLog(1) && (Log() << Verbose(1) << "INFO: Adding NormalVector " << runner->second->NormalVector << " of triangle " << *(runner->second) << "." << endl); | 
|---|
| 2942 | BaseLineNormal += (runner->second->NormalVector); | 
|---|
| 2943 | } | 
|---|
| 2944 | BaseLineNormal.Scale(-1. / 2.); // has to point inside for BoundaryTriangleSet::GetNormalVector() | 
|---|
| 2945 |  | 
|---|
| 2946 | // get the two triangles | 
|---|
| 2947 | // gather four endpoints and four lines | 
|---|
| 2948 | for (int j = 0; j < 4; j++) | 
|---|
| 2949 | OldLines[j] = NULL; | 
|---|
| 2950 | for (int j = 0; j < 2; j++) | 
|---|
| 2951 | OldPoints[j] = NULL; | 
|---|
| 2952 | i = 0; | 
|---|
| 2953 | m = 0; | 
|---|
| 2954 | DoLog(0) && (Log() << Verbose(0) << "The four old lines are: "); | 
|---|
| 2955 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 2956 | for (int j = 0; j < 3; j++) // all of their endpoints and baselines | 
|---|
| 2957 | if (runner->second->lines[j] != Base) { // pick not the central baseline | 
|---|
| 2958 | OldLines[i++] = runner->second->lines[j]; | 
|---|
| 2959 | DoLog(0) && (Log() << Verbose(0) << *runner->second->lines[j] << "\t"); | 
|---|
| 2960 | } | 
|---|
| 2961 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 2962 | DoLog(0) && (Log() << Verbose(0) << "The two old points are: "); | 
|---|
| 2963 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 2964 | for (int j = 0; j < 3; j++) // all of their endpoints and baselines | 
|---|
| 2965 | if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints | 
|---|
| 2966 | OldPoints[m++] = runner->second->endpoints[j]; | 
|---|
| 2967 | DoLog(0) && (Log() << Verbose(0) << *runner->second->endpoints[j] << "\t"); | 
|---|
| 2968 | } | 
|---|
| 2969 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| 2970 |  | 
|---|
| 2971 | // check whether everything is in place to create new lines and triangles | 
|---|
| 2972 | if (i < 4) { | 
|---|
| 2973 | DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl); | 
|---|
| 2974 | return NULL; | 
|---|
| 2975 | } | 
|---|
| 2976 | for (int j = 0; j < 4; j++) | 
|---|
| 2977 | if (OldLines[j] == NULL) { | 
|---|
| 2978 | DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough baselines!" << endl); | 
|---|
| 2979 | return NULL; | 
|---|
| 2980 | } | 
|---|
| 2981 | for (int j = 0; j < 2; j++) | 
|---|
| 2982 | if (OldPoints[j] == NULL) { | 
|---|
| 2983 | DoeLog(1) && (eLog() << Verbose(1) << "We have not gathered enough endpoints!" << endl); | 
|---|
| 2984 | return NULL; | 
|---|
| 2985 | } | 
|---|
| 2986 |  | 
|---|
| 2987 | // remove triangles and baseline removes itself | 
|---|
| 2988 | DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting baseline " << *Base << " from global list." << endl); | 
|---|
| 2989 | OldBaseLineNr = Base->Nr; | 
|---|
| 2990 | m = 0; | 
|---|
| 2991 | for (TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) { | 
|---|
| 2992 | DoLog(0) && (Log() << Verbose(0) << "INFO: Deleting triangle " << *(runner->second) << "." << endl); | 
|---|
| 2993 | OldTriangleNrs[m++] = runner->second->Nr; | 
|---|
| 2994 | RemoveTesselationTriangle(runner->second); | 
|---|
| 2995 | } | 
|---|
| 2996 |  | 
|---|
| 2997 | // construct new baseline (with same number as old one) | 
|---|
| 2998 | BPS[0] = OldPoints[0]; | 
|---|
| 2999 | BPS[1] = OldPoints[1]; | 
|---|
| 3000 | NewLine = new class BoundaryLineSet(BPS, OldBaseLineNr); | 
|---|
| 3001 | LinesOnBoundary.insert(LinePair(OldBaseLineNr, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one | 
|---|
| 3002 | DoLog(0) && (Log() << Verbose(0) << "INFO: Created new baseline " << *NewLine << "." << endl); | 
|---|
| 3003 |  | 
|---|
| 3004 | // construct new triangles with flipped baseline | 
|---|
| 3005 | i = -1; | 
|---|
| 3006 | if (OldLines[0]->IsConnectedTo(OldLines[2])) | 
|---|
| 3007 | i = 2; | 
|---|
| 3008 | if (OldLines[0]->IsConnectedTo(OldLines[3])) | 
|---|
| 3009 | i = 3; | 
|---|
| 3010 | if (i != -1) { | 
|---|
| 3011 | BLS[0] = OldLines[0]; | 
|---|
| 3012 | BLS[1] = OldLines[i]; | 
|---|
| 3013 | BLS[2] = NewLine; | 
|---|
| 3014 | BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[0]); | 
|---|
| 3015 | BTS->GetNormalVector(BaseLineNormal); | 
|---|
| 3016 | AddTesselationTriangle(OldTriangleNrs[0]); | 
|---|
| 3017 | DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl); | 
|---|
| 3018 |  | 
|---|
| 3019 | BLS[0] = (i == 2 ? OldLines[3] : OldLines[2]); | 
|---|
| 3020 | BLS[1] = OldLines[1]; | 
|---|
| 3021 | BLS[2] = NewLine; | 
|---|
| 3022 | BTS = new class BoundaryTriangleSet(BLS, OldTriangleNrs[1]); | 
|---|
| 3023 | BTS->GetNormalVector(BaseLineNormal); | 
|---|
| 3024 | AddTesselationTriangle(OldTriangleNrs[1]); | 
|---|
| 3025 | DoLog(0) && (Log() << Verbose(0) << "INFO: Created new triangle " << *BTS << "." << endl); | 
|---|
| 3026 | } else { | 
|---|
| 3027 | DoeLog(0) && (eLog() << Verbose(0) << "The four old lines do not connect, something's utterly wrong here!" << endl); | 
|---|
| 3028 | return NULL; | 
|---|
| 3029 | } | 
|---|
| 3030 |  | 
|---|
| 3031 | return NewLine; | 
|---|
| 3032 | } | 
|---|
| 3033 | ; | 
|---|
| 3034 |  | 
|---|
| 3035 | /** Finds the second point of starting triangle. | 
|---|
| 3036 | * \param *a first node | 
|---|
| 3037 | * \param Oben vector indicating the outside | 
|---|
| 3038 | * \param OptCandidate reference to recommended candidate on return | 
|---|
| 3039 | * \param Storage[3] array storing angles and other candidate information | 
|---|
| 3040 | * \param RADIUS radius of virtual sphere | 
|---|
| 3041 | * \param *LC LinkedCell structure with neighbouring points | 
|---|
| 3042 | */ | 
|---|
| 3043 | void Tesselation::FindSecondPointForTesselation(TesselPoint* a, Vector Oben, TesselPoint*& OptCandidate, double Storage[3], double RADIUS, const LinkedCell *LC) | 
|---|
| 3044 | { | 
|---|
| 3045 | Info FunctionInfo(__func__); | 
|---|
| 3046 | Vector AngleCheck; | 
|---|
| 3047 | class TesselPoint* Candidate = NULL; | 
|---|
| 3048 | double norm = -1.; | 
|---|
| 3049 | double angle = 0.; | 
|---|
| 3050 | int N[NDIM]; | 
|---|
| 3051 | int Nlower[NDIM]; | 
|---|
| 3052 | int Nupper[NDIM]; | 
|---|
| 3053 |  | 
|---|
| 3054 | if (LC->SetIndexToNode(a)) { // get cell for the starting point | 
|---|
| 3055 | for (int i = 0; i < NDIM; i++) // store indices of this cell | 
|---|
| 3056 | N[i] = LC->n[i]; | 
|---|
| 3057 | } else { | 
|---|
| 3058 | DoeLog(1) && (eLog() << Verbose(1) << "Point " << *a << " is not found in cell " << LC->index << "." << endl); | 
|---|
| 3059 | return; | 
|---|
| 3060 | } | 
|---|
| 3061 | // then go through the current and all neighbouring cells and check the contained points for possible candidates | 
|---|
| 3062 | for (int i = 0; i < NDIM; i++) { | 
|---|
| 3063 | Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0; | 
|---|
| 3064 | Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1; | 
|---|
| 3065 | } | 
|---|
| 3066 | DoLog(0) && (Log() << Verbose(0) << "LC Intervals from [" << N[0] << "<->" << LC->N[0] << ", " << N[1] << "<->" << LC->N[1] << ", " << N[2] << "<->" << LC->N[2] << "] :" << " [" << Nlower[0] << "," << Nupper[0] << "], " << " [" << Nlower[1] << "," << Nupper[1] << "], " << " [" << Nlower[2] << "," << Nupper[2] << "], " << endl); | 
|---|
| 3067 |  | 
|---|
| 3068 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 3069 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 3070 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 3071 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 3072 | //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 3073 | if (List != NULL) { | 
|---|
| 3074 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 3075 | Candidate = (*Runner); | 
|---|
| 3076 | // check if we only have one unique point yet ... | 
|---|
| 3077 | if (a != Candidate) { | 
|---|
| 3078 | // Calculate center of the circle with radius RADIUS through points a and Candidate | 
|---|
| 3079 | Vector OrthogonalizedOben, aCandidate, Center; | 
|---|
| 3080 | double distance, scaleFactor; | 
|---|
| 3081 |  | 
|---|
| 3082 | OrthogonalizedOben = Oben; | 
|---|
| 3083 | aCandidate = (*a->node) - (*Candidate->node); | 
|---|
| 3084 | OrthogonalizedOben.ProjectOntoPlane(aCandidate); | 
|---|
| 3085 | OrthogonalizedOben.Normalize(); | 
|---|
| 3086 | distance = 0.5 * aCandidate.Norm(); | 
|---|
| 3087 | scaleFactor = sqrt(((RADIUS * RADIUS) - (distance * distance))); | 
|---|
| 3088 | OrthogonalizedOben.Scale(scaleFactor); | 
|---|
| 3089 |  | 
|---|
| 3090 | Center = 0.5 * ((*Candidate->node) + (*a->node)); | 
|---|
| 3091 | Center += OrthogonalizedOben; | 
|---|
| 3092 |  | 
|---|
| 3093 | AngleCheck = Center - (*a->node); | 
|---|
| 3094 | norm = aCandidate.Norm(); | 
|---|
| 3095 | // second point shall have smallest angle with respect to Oben vector | 
|---|
| 3096 | if (norm < RADIUS * 2.) { | 
|---|
| 3097 | angle = AngleCheck.Angle(Oben); | 
|---|
| 3098 | if (angle < Storage[0]) { | 
|---|
| 3099 | //Log() << Verbose(1) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]); | 
|---|
| 3100 | DoLog(1) && (Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n"); | 
|---|
| 3101 | OptCandidate = Candidate; | 
|---|
| 3102 | Storage[0] = angle; | 
|---|
| 3103 | //Log() << Verbose(1) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]); | 
|---|
| 3104 | } else { | 
|---|
| 3105 | //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Looses with angle " << angle << " to a better candidate " << *OptCandidate << endl; | 
|---|
| 3106 | } | 
|---|
| 3107 | } else { | 
|---|
| 3108 | //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Refused due to Radius " << norm << endl; | 
|---|
| 3109 | } | 
|---|
| 3110 | } else { | 
|---|
| 3111 | //Log() << Verbose(1) << "Current candidate is " << *Candidate << ": Candidate is equal to first endpoint." << *a << "." << endl; | 
|---|
| 3112 | } | 
|---|
| 3113 | } | 
|---|
| 3114 | } else { | 
|---|
| 3115 | DoLog(0) && (Log() << Verbose(0) << "Linked cell list is empty." << endl); | 
|---|
| 3116 | } | 
|---|
| 3117 | } | 
|---|
| 3118 | } | 
|---|
| 3119 | ; | 
|---|
| 3120 |  | 
|---|
| 3121 | /** This recursive function finds a third point, to form a triangle with two given ones. | 
|---|
| 3122 | * Note that this function is for the starting triangle. | 
|---|
| 3123 | * The idea is as follows: A sphere with fixed radius is (almost) uniquely defined in space by three points | 
|---|
| 3124 | * that sit on its boundary. Hence, when two points are given and we look for the (next) third point, then | 
|---|
| 3125 | * the center of the sphere is still fixed up to a single parameter. The band of possible values | 
|---|
| 3126 | * describes a circle in 3D-space. The old center of the sphere for the current base triangle gives | 
|---|
| 3127 | * us the "null" on this circle, the new center of the candidate point will be some way along this | 
|---|
| 3128 | * circle. The shorter the way the better is the candidate. Note that the direction is clearly given | 
|---|
| 3129 | * by the normal vector of the base triangle that always points outwards by construction. | 
|---|
| 3130 | * Hence, we construct a Center of this circle which sits right in the middle of the current base line. | 
|---|
| 3131 | * We construct the normal vector that defines the plane this circle lies in, it is just in the | 
|---|
| 3132 | * direction of the baseline. And finally, we need the radius of the circle, which is given by the rest | 
|---|
| 3133 | * with respect to the length of the baseline and the sphere's fixed \a RADIUS. | 
|---|
| 3134 | * Note that there is one difficulty: The circumcircle is uniquely defined, but for the circumsphere's center | 
|---|
| 3135 | * there are two possibilities which becomes clear from the construction as seen below. Hence, we must check | 
|---|
| 3136 | * both. | 
|---|
| 3137 | * Note also that the acos() function is not unique on [0, 2.*M_PI). Hence, we need an additional check | 
|---|
| 3138 | * to decide for one of the two possible angles. Therefore we need a SearchDirection and to make this check | 
|---|
| 3139 | * sensible we need OldSphereCenter to be orthogonal to it. Either we construct SearchDirection orthogonal | 
|---|
| 3140 | * right away, or -- what we do here -- we rotate the relative sphere centers such that this orthogonality | 
|---|
| 3141 | * holds. Then, the normalized projection onto the SearchDirection is either +1 or -1 and thus states whether | 
|---|
| 3142 | * the angle is uniquely in either (0,M_PI] or [M_PI, 2.*M_PI). | 
|---|
| 3143 | * @param NormalVector normal direction of the base triangle (here the unit axis vector, \sa FindStartingTriangle()) | 
|---|
| 3144 | * @param SearchDirection general direction where to search for the next point, relative to center of BaseLine | 
|---|
| 3145 | * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle | 
|---|
| 3146 | * @param CandidateLine CandidateForTesselation with the current base line and list of candidates and ShortestAngle | 
|---|
| 3147 | * @param ThirdPoint third point to avoid in search | 
|---|
| 3148 | * @param RADIUS radius of sphere | 
|---|
| 3149 | * @param *LC LinkedCell structure with neighbouring points | 
|---|
| 3150 | */ | 
|---|
| 3151 | void Tesselation::FindThirdPointForTesselation(const Vector &NormalVector, const Vector &SearchDirection, const Vector &OldSphereCenter, CandidateForTesselation &CandidateLine, const class BoundaryPointSet * const ThirdPoint, const double RADIUS, const LinkedCell *LC) const | 
|---|
| 3152 | { | 
|---|
| 3153 | Info FunctionInfo(__func__); | 
|---|
| 3154 | Vector CircleCenter; // center of the circle, i.e. of the band of sphere's centers | 
|---|
| 3155 | Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in | 
|---|
| 3156 | Vector SphereCenter; | 
|---|
| 3157 | Vector NewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility | 
|---|
| 3158 | Vector OtherNewSphereCenter; // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility | 
|---|
| 3159 | Vector NewNormalVector; // normal vector of the Candidate's triangle | 
|---|
| 3160 | Vector helper, OptCandidateCenter, OtherOptCandidateCenter; | 
|---|
| 3161 | Vector RelativeOldSphereCenter; | 
|---|
| 3162 | Vector NewPlaneCenter; | 
|---|
| 3163 | double CircleRadius; // radius of this circle | 
|---|
| 3164 | double radius; | 
|---|
| 3165 | double otherradius; | 
|---|
| 3166 | double alpha, Otheralpha; // angles (i.e. parameter for the circle). | 
|---|
| 3167 | int N[NDIM], Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 3168 | TesselPoint *Candidate = NULL; | 
|---|
| 3169 |  | 
|---|
| 3170 | DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl); | 
|---|
| 3171 |  | 
|---|
| 3172 | // copy old center | 
|---|
| 3173 | CandidateLine.OldCenter = OldSphereCenter; | 
|---|
| 3174 | CandidateLine.ThirdPoint = ThirdPoint; | 
|---|
| 3175 | CandidateLine.pointlist.clear(); | 
|---|
| 3176 |  | 
|---|
| 3177 | // construct center of circle | 
|---|
| 3178 | CircleCenter = 0.5 * ((*CandidateLine.BaseLine->endpoints[0]->node->node) + | 
|---|
| 3179 | (*CandidateLine.BaseLine->endpoints[1]->node->node)); | 
|---|
| 3180 |  | 
|---|
| 3181 | // construct normal vector of circle | 
|---|
| 3182 | CirclePlaneNormal = (*CandidateLine.BaseLine->endpoints[0]->node->node) - | 
|---|
| 3183 | (*CandidateLine.BaseLine->endpoints[1]->node->node); | 
|---|
| 3184 |  | 
|---|
| 3185 | RelativeOldSphereCenter = OldSphereCenter - CircleCenter; | 
|---|
| 3186 |  | 
|---|
| 3187 | // calculate squared radius TesselPoint *ThirdPoint,f circle | 
|---|
| 3188 | radius = CirclePlaneNormal.NormSquared() / 4.; | 
|---|
| 3189 | if (radius < RADIUS * RADIUS) { | 
|---|
| 3190 | CircleRadius = RADIUS * RADIUS - radius; | 
|---|
| 3191 | CirclePlaneNormal.Normalize(); | 
|---|
| 3192 | DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl); | 
|---|
| 3193 |  | 
|---|
| 3194 | // test whether old center is on the band's plane | 
|---|
| 3195 | if (fabs(RelativeOldSphereCenter.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) { | 
|---|
| 3196 | DoeLog(1) && (eLog() << Verbose(1) << "Something's very wrong here: RelativeOldSphereCenter is not on the band's plane as desired by " << fabs(RelativeOldSphereCenter.ScalarProduct(CirclePlaneNormal)) << "!" << endl); | 
|---|
| 3197 | RelativeOldSphereCenter.ProjectOntoPlane(CirclePlaneNormal); | 
|---|
| 3198 | } | 
|---|
| 3199 | radius = RelativeOldSphereCenter.NormSquared(); | 
|---|
| 3200 | if (fabs(radius - CircleRadius) < HULLEPSILON) { | 
|---|
| 3201 | DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeOldSphereCenter is at " << RelativeOldSphereCenter << "." << endl); | 
|---|
| 3202 |  | 
|---|
| 3203 | // check SearchDirection | 
|---|
| 3204 | DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl); | 
|---|
| 3205 | if (fabs(RelativeOldSphereCenter.ScalarProduct(SearchDirection)) > HULLEPSILON) { // rotated the wrong way! | 
|---|
| 3206 | DoeLog(1) && (eLog() << Verbose(1) << "SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl); | 
|---|
| 3207 | } | 
|---|
| 3208 |  | 
|---|
| 3209 | // get cell for the starting point | 
|---|
| 3210 | if (LC->SetIndexToVector(&CircleCenter)) { | 
|---|
| 3211 | for (int i = 0; i < NDIM; i++) // store indices of this cell | 
|---|
| 3212 | N[i] = LC->n[i]; | 
|---|
| 3213 | //Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 3214 | } else { | 
|---|
| 3215 | DoeLog(1) && (eLog() << Verbose(1) << "Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl); | 
|---|
| 3216 | return; | 
|---|
| 3217 | } | 
|---|
| 3218 | // then go through the current and all neighbouring cells and check the contained points for possible candidates | 
|---|
| 3219 | //Log() << Verbose(1) << "LC Intervals:"; | 
|---|
| 3220 | for (int i = 0; i < NDIM; i++) { | 
|---|
| 3221 | Nlower[i] = ((N[i] - 1) >= 0) ? N[i] - 1 : 0; | 
|---|
| 3222 | Nupper[i] = ((N[i] + 1) < LC->N[i]) ? N[i] + 1 : LC->N[i] - 1; | 
|---|
| 3223 | //Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] "; | 
|---|
| 3224 | } | 
|---|
| 3225 | //Log() << Verbose(0) << endl; | 
|---|
| 3226 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 3227 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 3228 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 3229 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 3230 | //Log() << Verbose(1) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 3231 | if (List != NULL) { | 
|---|
| 3232 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 3233 | Candidate = (*Runner); | 
|---|
| 3234 |  | 
|---|
| 3235 | // check for three unique points | 
|---|
| 3236 | DoLog(2) && (Log() << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " for BaseLine " << *CandidateLine.BaseLine << " with OldSphereCenter " << OldSphereCenter << "." << endl); | 
|---|
| 3237 | if ((Candidate != CandidateLine.BaseLine->endpoints[0]->node) && (Candidate != CandidateLine.BaseLine->endpoints[1]->node)) { | 
|---|
| 3238 |  | 
|---|
| 3239 | // find center on the plane | 
|---|
| 3240 | GetCenterofCircumcircle(&NewPlaneCenter, *CandidateLine.BaseLine->endpoints[0]->node->node, *CandidateLine.BaseLine->endpoints[1]->node->node, *Candidate->node); | 
|---|
| 3241 | DoLog(1) && (Log() << Verbose(1) << "INFO: NewPlaneCenter is " << NewPlaneCenter << "." << endl); | 
|---|
| 3242 |  | 
|---|
| 3243 | try { | 
|---|
| 3244 | NewNormalVector = Plane(*(CandidateLine.BaseLine->endpoints[0]->node->node), | 
|---|
| 3245 | *(CandidateLine.BaseLine->endpoints[1]->node->node), | 
|---|
| 3246 | *(Candidate->node)).getNormal(); | 
|---|
| 3247 | DoLog(1) && (Log() << Verbose(1) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl); | 
|---|
| 3248 | radius = CandidateLine.BaseLine->endpoints[0]->node->node->DistanceSquared(NewPlaneCenter); | 
|---|
| 3249 | DoLog(1) && (Log() << Verbose(1) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl); | 
|---|
| 3250 | DoLog(1) && (Log() << Verbose(1) << "INFO: SearchDirection is " << SearchDirection << "." << endl); | 
|---|
| 3251 | DoLog(1) && (Log() << Verbose(1) << "INFO: Radius of CircumCenterCircle is " << radius << "." << endl); | 
|---|
| 3252 | if (radius < RADIUS * RADIUS) { | 
|---|
| 3253 | otherradius = CandidateLine.BaseLine->endpoints[1]->node->node->DistanceSquared(NewPlaneCenter); | 
|---|
| 3254 | if (fabs(radius - otherradius) < HULLEPSILON) { | 
|---|
| 3255 | // construct both new centers | 
|---|
| 3256 | NewSphereCenter = NewPlaneCenter; | 
|---|
| 3257 | OtherNewSphereCenter= NewPlaneCenter; | 
|---|
| 3258 | helper = NewNormalVector; | 
|---|
| 3259 | helper.Scale(sqrt(RADIUS * RADIUS - radius)); | 
|---|
| 3260 | DoLog(2) && (Log() << Verbose(2) << "INFO: Distance of NewPlaneCenter " << NewPlaneCenter << " to either NewSphereCenter is " << helper.Norm() << " of vector " << helper << " with sphere radius " << RADIUS << "." << endl); | 
|---|
| 3261 | NewSphereCenter += helper; | 
|---|
| 3262 | DoLog(2) && (Log() << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl); | 
|---|
| 3263 | // OtherNewSphereCenter is created by the same vector just in the other direction | 
|---|
| 3264 | helper.Scale(-1.); | 
|---|
| 3265 | OtherNewSphereCenter += helper; | 
|---|
| 3266 | DoLog(2) && (Log() << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl); | 
|---|
| 3267 | alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection); | 
|---|
| 3268 | Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection); | 
|---|
| 3269 | if ((ThirdPoint != NULL) && (Candidate == ThirdPoint->node)) { // in that case only the other circlecenter is valid | 
|---|
| 3270 | if (OldSphereCenter.DistanceSquared(NewSphereCenter) < OldSphereCenter.DistanceSquared(OtherNewSphereCenter)) | 
|---|
| 3271 | alpha = Otheralpha; | 
|---|
| 3272 | } else | 
|---|
| 3273 | alpha = min(alpha, Otheralpha); | 
|---|
| 3274 | // if there is a better candidate, drop the current list and add the new candidate | 
|---|
| 3275 | // otherwise ignore the new candidate and keep the list | 
|---|
| 3276 | if (CandidateLine.ShortestAngle > (alpha - HULLEPSILON)) { | 
|---|
| 3277 | if (fabs(alpha - Otheralpha) > MYEPSILON) { | 
|---|
| 3278 | CandidateLine.OptCenter = NewSphereCenter; | 
|---|
| 3279 | CandidateLine.OtherOptCenter = OtherNewSphereCenter; | 
|---|
| 3280 | } else { | 
|---|
| 3281 | CandidateLine.OptCenter = OtherNewSphereCenter; | 
|---|
| 3282 | CandidateLine.OtherOptCenter = NewSphereCenter; | 
|---|
| 3283 | } | 
|---|
| 3284 | // if there is an equal candidate, add it to the list without clearing the list | 
|---|
| 3285 | if ((CandidateLine.ShortestAngle - HULLEPSILON) < alpha) { | 
|---|
| 3286 | CandidateLine.pointlist.push_back(Candidate); | 
|---|
| 3287 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found an equally good candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl); | 
|---|
| 3288 | } else { | 
|---|
| 3289 | // remove all candidates from the list and then the list itself | 
|---|
| 3290 | CandidateLine.pointlist.clear(); | 
|---|
| 3291 | CandidateLine.pointlist.push_back(Candidate); | 
|---|
| 3292 | DoLog(0) && (Log() << Verbose(0) << "ACCEPT: We have found a better candidate: " << *(Candidate) << " with " << alpha << " and circumsphere's center at " << CandidateLine.OptCenter << "." << endl); | 
|---|
| 3293 | } | 
|---|
| 3294 | CandidateLine.ShortestAngle = alpha; | 
|---|
| 3295 | DoLog(0) && (Log() << Verbose(0) << "INFO: There are " << CandidateLine.pointlist.size() << " candidates in the list now." << endl); | 
|---|
| 3296 | } else { | 
|---|
| 3297 | if ((Candidate != NULL) && (CandidateLine.pointlist.begin() != CandidateLine.pointlist.end())) { | 
|---|
| 3298 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Old candidate " << *(*CandidateLine.pointlist.begin()) << " with " << CandidateLine.ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl); | 
|---|
| 3299 | } else { | 
|---|
| 3300 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl); | 
|---|
| 3301 | } | 
|---|
| 3302 | } | 
|---|
| 3303 | } else { | 
|---|
| 3304 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Distance to center of circumcircle is not the same from each corner of the triangle: " << fabs(radius - otherradius) << endl); | 
|---|
| 3305 | } | 
|---|
| 3306 | } else { | 
|---|
| 3307 | DoLog(1) && (Log() << Verbose(1) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl); | 
|---|
| 3308 | } | 
|---|
| 3309 | } | 
|---|
| 3310 | catch (LinearDependenceException &excp){ | 
|---|
| 3311 | Log() << Verbose(1) << excp; | 
|---|
| 3312 | Log() << Verbose(1) << "REJECT: Three points from " << *CandidateLine.BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl; | 
|---|
| 3313 | } | 
|---|
| 3314 | } else { | 
|---|
| 3315 | if (ThirdPoint != NULL) { | 
|---|
| 3316 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " and " << *ThirdPoint << " contains Candidate " << *Candidate << "." << endl); | 
|---|
| 3317 | } else { | 
|---|
| 3318 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Base triangle " << *CandidateLine.BaseLine << " contains Candidate " << *Candidate << "." << endl); | 
|---|
| 3319 | } | 
|---|
| 3320 | } | 
|---|
| 3321 | } | 
|---|
| 3322 | } | 
|---|
| 3323 | } | 
|---|
| 3324 | } else { | 
|---|
| 3325 | DoeLog(1) && (eLog() << Verbose(1) << "The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl); | 
|---|
| 3326 | } | 
|---|
| 3327 | } else { | 
|---|
| 3328 | if (ThirdPoint != NULL) | 
|---|
| 3329 | DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " and third node " << *ThirdPoint << " is too big!" << endl); | 
|---|
| 3330 | else | 
|---|
| 3331 | DoLog(1) && (Log() << Verbose(1) << "Circumcircle for base line " << *CandidateLine.BaseLine << " is too big!" << endl); | 
|---|
| 3332 | } | 
|---|
| 3333 |  | 
|---|
| 3334 | DoLog(1) && (Log() << Verbose(1) << "INFO: Sorting candidate list ..." << endl); | 
|---|
| 3335 | if (CandidateLine.pointlist.size() > 1) { | 
|---|
| 3336 | CandidateLine.pointlist.unique(); | 
|---|
| 3337 | CandidateLine.pointlist.sort(); //SortCandidates); | 
|---|
| 3338 | } | 
|---|
| 3339 |  | 
|---|
| 3340 | if ((!CandidateLine.pointlist.empty()) && (!CandidateLine.CheckValidity(RADIUS, LC))) { | 
|---|
| 3341 | DoeLog(0) && (eLog() << Verbose(0) << "There were other points contained in the rolling sphere as well!" << endl); | 
|---|
| 3342 | performCriticalExit(); | 
|---|
| 3343 | } | 
|---|
| 3344 | } | 
|---|
| 3345 | ; | 
|---|
| 3346 |  | 
|---|
| 3347 | /** Finds the endpoint two lines are sharing. | 
|---|
| 3348 | * \param *line1 first line | 
|---|
| 3349 | * \param *line2 second line | 
|---|
| 3350 | * \return point which is shared or NULL if none | 
|---|
| 3351 | */ | 
|---|
| 3352 | class BoundaryPointSet *Tesselation::GetCommonEndpoint(const BoundaryLineSet * line1, const BoundaryLineSet * line2) const | 
|---|
| 3353 | { | 
|---|
| 3354 | Info FunctionInfo(__func__); | 
|---|
| 3355 | const BoundaryLineSet * lines[2] = { line1, line2 }; | 
|---|
| 3356 | class BoundaryPointSet *node = NULL; | 
|---|
| 3357 | PointMap OrderMap; | 
|---|
| 3358 | PointTestPair OrderTest; | 
|---|
| 3359 | for (int i = 0; i < 2; i++) | 
|---|
| 3360 | // for both lines | 
|---|
| 3361 | for (int j = 0; j < 2; j++) { // for both endpoints | 
|---|
| 3362 | OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> (lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j])); | 
|---|
| 3363 | if (!OrderTest.second) { // if insertion fails, we have common endpoint | 
|---|
| 3364 | node = OrderTest.first->second; | 
|---|
| 3365 | DoLog(1) && (Log() << Verbose(1) << "Common endpoint of lines " << *line1 << " and " << *line2 << " is: " << *node << "." << endl); | 
|---|
| 3366 | j = 2; | 
|---|
| 3367 | i = 2; | 
|---|
| 3368 | break; | 
|---|
| 3369 | } | 
|---|
| 3370 | } | 
|---|
| 3371 | return node; | 
|---|
| 3372 | } | 
|---|
| 3373 | ; | 
|---|
| 3374 |  | 
|---|
| 3375 | /** Finds the boundary points that are closest to a given Vector \a *x. | 
|---|
| 3376 | * \param *out output stream for debugging | 
|---|
| 3377 | * \param *x Vector to look from | 
|---|
| 3378 | * \return map of BoundaryPointSet of closest points sorted by squared distance or NULL. | 
|---|
| 3379 | */ | 
|---|
| 3380 | DistanceToPointMap * Tesselation::FindClosestBoundaryPointsToVector(const Vector *x, const LinkedCell* LC) const | 
|---|
| 3381 | { | 
|---|
| 3382 | Info FunctionInfo(__func__); | 
|---|
| 3383 | PointMap::const_iterator FindPoint; | 
|---|
| 3384 | int N[NDIM], Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 3385 |  | 
|---|
| 3386 | if (LinesOnBoundary.empty()) { | 
|---|
| 3387 | DoeLog(1) && (eLog() << Verbose(1) << "There is no tesselation structure to compare the point with, please create one first." << endl); | 
|---|
| 3388 | return NULL; | 
|---|
| 3389 | } | 
|---|
| 3390 |  | 
|---|
| 3391 | // gather all points close to the desired one | 
|---|
| 3392 | LC->SetIndexToVector(x); // ignore status as we calculate bounds below sensibly | 
|---|
| 3393 | for (int i = 0; i < NDIM; i++) // store indices of this cell | 
|---|
| 3394 | N[i] = LC->n[i]; | 
|---|
| 3395 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl); | 
|---|
| 3396 | DistanceToPointMap * points = new DistanceToPointMap; | 
|---|
| 3397 | LC->GetNeighbourBounds(Nlower, Nupper); | 
|---|
| 3398 | //Log() << Verbose(1) << endl; | 
|---|
| 3399 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 3400 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 3401 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 3402 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell(); | 
|---|
| 3403 | //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl; | 
|---|
| 3404 | if (List != NULL) { | 
|---|
| 3405 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 3406 | FindPoint = PointsOnBoundary.find((*Runner)->nr); | 
|---|
| 3407 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 3408 | points->insert(DistanceToPointPair(FindPoint->second->node->node->DistanceSquared(*x), FindPoint->second)); | 
|---|
| 3409 | DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *FindPoint->second << " into the list." << endl); | 
|---|
| 3410 | } | 
|---|
| 3411 | } | 
|---|
| 3412 | } else { | 
|---|
| 3413 | DoeLog(1) && (eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl); | 
|---|
| 3414 | } | 
|---|
| 3415 | } | 
|---|
| 3416 |  | 
|---|
| 3417 | // check whether we found some points | 
|---|
| 3418 | if (points->empty()) { | 
|---|
| 3419 | DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl); | 
|---|
| 3420 | delete (points); | 
|---|
| 3421 | return NULL; | 
|---|
| 3422 | } | 
|---|
| 3423 | return points; | 
|---|
| 3424 | } | 
|---|
| 3425 | ; | 
|---|
| 3426 |  | 
|---|
| 3427 | /** Finds the boundary line that is closest to a given Vector \a *x. | 
|---|
| 3428 | * \param *out output stream for debugging | 
|---|
| 3429 | * \param *x Vector to look from | 
|---|
| 3430 | * \return closest BoundaryLineSet or NULL in degenerate case. | 
|---|
| 3431 | */ | 
|---|
| 3432 | BoundaryLineSet * Tesselation::FindClosestBoundaryLineToVector(const Vector *x, const LinkedCell* LC) const | 
|---|
| 3433 | { | 
|---|
| 3434 | Info FunctionInfo(__func__); | 
|---|
| 3435 | // get closest points | 
|---|
| 3436 | DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC); | 
|---|
| 3437 | if (points == NULL) { | 
|---|
| 3438 | DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl); | 
|---|
| 3439 | return NULL; | 
|---|
| 3440 | } | 
|---|
| 3441 |  | 
|---|
| 3442 | // for each point, check its lines, remember closest | 
|---|
| 3443 | DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryLine to " << *x << " ... " << endl); | 
|---|
| 3444 | BoundaryLineSet *ClosestLine = NULL; | 
|---|
| 3445 | double MinDistance = -1.; | 
|---|
| 3446 | Vector helper; | 
|---|
| 3447 | Vector Center; | 
|---|
| 3448 | Vector BaseLine; | 
|---|
| 3449 | for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) { | 
|---|
| 3450 | for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) { | 
|---|
| 3451 | // calculate closest point on line to desired point | 
|---|
| 3452 | helper = 0.5 * ((*(LineRunner->second)->endpoints[0]->node->node) + | 
|---|
| 3453 | (*(LineRunner->second)->endpoints[1]->node->node)); | 
|---|
| 3454 | Center = (*x) - helper; | 
|---|
| 3455 | BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) - | 
|---|
| 3456 | (*(LineRunner->second)->endpoints[1]->node->node); | 
|---|
| 3457 | Center.ProjectOntoPlane(BaseLine); | 
|---|
| 3458 | const double distance = Center.NormSquared(); | 
|---|
| 3459 | if ((ClosestLine == NULL) || (distance < MinDistance)) { | 
|---|
| 3460 | // additionally calculate intersection on line (whether it's on the line section or not) | 
|---|
| 3461 | helper = (*x) - (*(LineRunner->second)->endpoints[0]->node->node) - Center; | 
|---|
| 3462 | const double lengthA = helper.ScalarProduct(BaseLine); | 
|---|
| 3463 | helper = (*x) - (*(LineRunner->second)->endpoints[1]->node->node) - Center; | 
|---|
| 3464 | const double lengthB = helper.ScalarProduct(BaseLine); | 
|---|
| 3465 | if (lengthB * lengthA < 0) { // if have different sign | 
|---|
| 3466 | ClosestLine = LineRunner->second; | 
|---|
| 3467 | MinDistance = distance; | 
|---|
| 3468 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: New closest line is " << *ClosestLine << " with projected distance " << MinDistance << "." << endl); | 
|---|
| 3469 | } else { | 
|---|
| 3470 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Intersection is outside of the line section: " << lengthA << " and " << lengthB << "." << endl); | 
|---|
| 3471 | } | 
|---|
| 3472 | } else { | 
|---|
| 3473 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Point is too further away than present line: " << distance << " >> " << MinDistance << "." << endl); | 
|---|
| 3474 | } | 
|---|
| 3475 | } | 
|---|
| 3476 | } | 
|---|
| 3477 | delete (points); | 
|---|
| 3478 | // check whether closest line is "too close" :), then it's inside | 
|---|
| 3479 | if (ClosestLine == NULL) { | 
|---|
| 3480 | DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl); | 
|---|
| 3481 | return NULL; | 
|---|
| 3482 | } | 
|---|
| 3483 | return ClosestLine; | 
|---|
| 3484 | } | 
|---|
| 3485 | ; | 
|---|
| 3486 |  | 
|---|
| 3487 | /** Finds the triangle that is closest to a given Vector \a *x. | 
|---|
| 3488 | * \param *out output stream for debugging | 
|---|
| 3489 | * \param *x Vector to look from | 
|---|
| 3490 | * \return BoundaryTriangleSet of nearest triangle or NULL. | 
|---|
| 3491 | */ | 
|---|
| 3492 | TriangleList * Tesselation::FindClosestTrianglesToVector(const Vector *x, const LinkedCell* LC) const | 
|---|
| 3493 | { | 
|---|
| 3494 | Info FunctionInfo(__func__); | 
|---|
| 3495 | // get closest points | 
|---|
| 3496 | DistanceToPointMap * points = FindClosestBoundaryPointsToVector(x, LC); | 
|---|
| 3497 | if (points == NULL) { | 
|---|
| 3498 | DoeLog(1) && (eLog() << Verbose(1) << "There is no nearest point: too far away from the surface." << endl); | 
|---|
| 3499 | return NULL; | 
|---|
| 3500 | } | 
|---|
| 3501 |  | 
|---|
| 3502 | // for each point, check its lines, remember closest | 
|---|
| 3503 | DoLog(1) && (Log() << Verbose(1) << "Finding closest BoundaryTriangle to " << *x << " ... " << endl); | 
|---|
| 3504 | LineSet ClosestLines; | 
|---|
| 3505 | double MinDistance = 1e+16; | 
|---|
| 3506 | Vector BaseLineIntersection; | 
|---|
| 3507 | Vector Center; | 
|---|
| 3508 | Vector BaseLine; | 
|---|
| 3509 | Vector BaseLineCenter; | 
|---|
| 3510 | for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++) { | 
|---|
| 3511 | for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++) { | 
|---|
| 3512 |  | 
|---|
| 3513 | BaseLine = (*(LineRunner->second)->endpoints[0]->node->node) - | 
|---|
| 3514 | (*(LineRunner->second)->endpoints[1]->node->node); | 
|---|
| 3515 | const double lengthBase = BaseLine.NormSquared(); | 
|---|
| 3516 |  | 
|---|
| 3517 | BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[0]->node->node); | 
|---|
| 3518 | const double lengthEndA = BaseLineIntersection.NormSquared(); | 
|---|
| 3519 |  | 
|---|
| 3520 | BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node); | 
|---|
| 3521 | const double lengthEndB = BaseLineIntersection.NormSquared(); | 
|---|
| 3522 |  | 
|---|
| 3523 | if ((lengthEndA > lengthBase) || (lengthEndB > lengthBase) || ((lengthEndA < MYEPSILON) || (lengthEndB < MYEPSILON))) { // intersection would be outside, take closer endpoint | 
|---|
| 3524 | const double lengthEnd = Min(lengthEndA, lengthEndB); | 
|---|
| 3525 | if (lengthEnd - MinDistance < -MYEPSILON) { // new best line | 
|---|
| 3526 | ClosestLines.clear(); | 
|---|
| 3527 | ClosestLines.insert(LineRunner->second); | 
|---|
| 3528 | MinDistance = lengthEnd; | 
|---|
| 3529 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[0]->node << " is closer with " << lengthEnd << "." << endl); | 
|---|
| 3530 | } else if (fabs(lengthEnd - MinDistance) < MYEPSILON) { // additional best candidate | 
|---|
| 3531 | ClosestLines.insert(LineRunner->second); | 
|---|
| 3532 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Line " << *LineRunner->second << " to endpoint " << *LineRunner->second->endpoints[1]->node << " is equally good with " << lengthEnd << "." << endl); | 
|---|
| 3533 | } else { // line is worse | 
|---|
| 3534 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Line " << *LineRunner->second << " to either endpoints is further away than present closest line candidate: " << lengthEndA << ", " << lengthEndB << ", and distance is longer than baseline:" << lengthBase << "." << endl); | 
|---|
| 3535 | } | 
|---|
| 3536 | } else { // intersection is closer, calculate | 
|---|
| 3537 | // calculate closest point on line to desired point | 
|---|
| 3538 | BaseLineIntersection = (*x) - (*(LineRunner->second)->endpoints[1]->node->node); | 
|---|
| 3539 | Center = BaseLineIntersection; | 
|---|
| 3540 | Center.ProjectOntoPlane(BaseLine); | 
|---|
| 3541 | BaseLineIntersection -= Center; | 
|---|
| 3542 | const double distance = BaseLineIntersection.NormSquared(); | 
|---|
| 3543 | if (Center.NormSquared() > BaseLine.NormSquared()) { | 
|---|
| 3544 | DoeLog(0) && (eLog() << Verbose(0) << "Algorithmic error: In second case we have intersection outside of baseline!" << endl); | 
|---|
| 3545 | } | 
|---|
| 3546 | if ((ClosestLines.empty()) || (distance < MinDistance)) { | 
|---|
| 3547 | ClosestLines.insert(LineRunner->second); | 
|---|
| 3548 | MinDistance = distance; | 
|---|
| 3549 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Intersection in between endpoints, new closest line " << *LineRunner->second << " is " << *ClosestLines.begin() << " with projected distance " << MinDistance << "." << endl); | 
|---|
| 3550 | } else { | 
|---|
| 3551 | DoLog(2) && (Log() << Verbose(2) << "REJECT: Point is further away from line " << *LineRunner->second << " than present closest line: " << distance << " >> " << MinDistance << "." << endl); | 
|---|
| 3552 | } | 
|---|
| 3553 | } | 
|---|
| 3554 | } | 
|---|
| 3555 | } | 
|---|
| 3556 | delete (points); | 
|---|
| 3557 |  | 
|---|
| 3558 | // check whether closest line is "too close" :), then it's inside | 
|---|
| 3559 | if (ClosestLines.empty()) { | 
|---|
| 3560 | DoLog(0) && (Log() << Verbose(0) << "Is the only point, no one else is closeby." << endl); | 
|---|
| 3561 | return NULL; | 
|---|
| 3562 | } | 
|---|
| 3563 | TriangleList * candidates = new TriangleList; | 
|---|
| 3564 | for (LineSet::iterator LineRunner = ClosestLines.begin(); LineRunner != ClosestLines.end(); LineRunner++) | 
|---|
| 3565 | for (TriangleMap::iterator Runner = (*LineRunner)->triangles.begin(); Runner != (*LineRunner)->triangles.end(); Runner++) { | 
|---|
| 3566 | candidates->push_back(Runner->second); | 
|---|
| 3567 | } | 
|---|
| 3568 | return candidates; | 
|---|
| 3569 | } | 
|---|
| 3570 | ; | 
|---|
| 3571 |  | 
|---|
| 3572 | /** Finds closest triangle to a point. | 
|---|
| 3573 | * This basically just takes care of the degenerate case, which is not handled in FindClosestTrianglesToPoint(). | 
|---|
| 3574 | * \param *out output stream for debugging | 
|---|
| 3575 | * \param *x Vector to look from | 
|---|
| 3576 | * \param &distance contains found distance on return | 
|---|
| 3577 | * \return list of BoundaryTriangleSet of nearest triangles or NULL. | 
|---|
| 3578 | */ | 
|---|
| 3579 | class BoundaryTriangleSet * Tesselation::FindClosestTriangleToVector(const Vector *x, const LinkedCell* LC) const | 
|---|
| 3580 | { | 
|---|
| 3581 | Info FunctionInfo(__func__); | 
|---|
| 3582 | class BoundaryTriangleSet *result = NULL; | 
|---|
| 3583 | TriangleList *triangles = FindClosestTrianglesToVector(x, LC); | 
|---|
| 3584 | TriangleList candidates; | 
|---|
| 3585 | Vector Center; | 
|---|
| 3586 | Vector helper; | 
|---|
| 3587 |  | 
|---|
| 3588 | if ((triangles == NULL) || (triangles->empty())) | 
|---|
| 3589 | return NULL; | 
|---|
| 3590 |  | 
|---|
| 3591 | // go through all and pick the one with the best alignment to x | 
|---|
| 3592 | double MinAlignment = 2. * M_PI; | 
|---|
| 3593 | for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++) { | 
|---|
| 3594 | (*Runner)->GetCenter(&Center); | 
|---|
| 3595 | helper = (*x) - Center; | 
|---|
| 3596 | const double Alignment = helper.Angle((*Runner)->NormalVector); | 
|---|
| 3597 | if (Alignment < MinAlignment) { | 
|---|
| 3598 | result = *Runner; | 
|---|
| 3599 | MinAlignment = Alignment; | 
|---|
| 3600 | DoLog(1) && (Log() << Verbose(1) << "ACCEPT: Triangle " << *result << " is better aligned with " << MinAlignment << "." << endl); | 
|---|
| 3601 | } else { | 
|---|
| 3602 | DoLog(1) && (Log() << Verbose(1) << "REJECT: Triangle " << *result << " is worse aligned with " << MinAlignment << "." << endl); | 
|---|
| 3603 | } | 
|---|
| 3604 | } | 
|---|
| 3605 | delete (triangles); | 
|---|
| 3606 |  | 
|---|
| 3607 | return result; | 
|---|
| 3608 | } | 
|---|
| 3609 | ; | 
|---|
| 3610 |  | 
|---|
| 3611 | /** Checks whether the provided Vector is within the Tesselation structure. | 
|---|
| 3612 | * Basically calls Tesselation::GetDistanceToSurface() and checks the sign of the return value. | 
|---|
| 3613 | * @param point of which to check the position | 
|---|
| 3614 | * @param *LC LinkedCell structure | 
|---|
| 3615 | * | 
|---|
| 3616 | * @return true if the point is inside the Tesselation structure, false otherwise | 
|---|
| 3617 | */ | 
|---|
| 3618 | bool Tesselation::IsInnerPoint(const Vector &Point, const LinkedCell* const LC) const | 
|---|
| 3619 | { | 
|---|
| 3620 | Info FunctionInfo(__func__); | 
|---|
| 3621 | TriangleIntersectionList Intersections(&Point, this, LC); | 
|---|
| 3622 |  | 
|---|
| 3623 | return Intersections.IsInside(); | 
|---|
| 3624 | } | 
|---|
| 3625 | ; | 
|---|
| 3626 |  | 
|---|
| 3627 | /** Returns the distance to the surface given by the tesselation. | 
|---|
| 3628 | * Calls FindClosestTriangleToVector() and checks whether the resulting triangle's BoundaryTriangleSet#NormalVector points | 
|---|
| 3629 | * towards or away from the given \a &Point. Additionally, we check whether it's normal to the normal vector, i.e. on the | 
|---|
| 3630 | * closest triangle's plane. Then, we have to check whether \a Point is inside the triangle or not to determine whether it's | 
|---|
| 3631 | * an inside or outside point. This is done by calling BoundaryTriangleSet::GetIntersectionInsideTriangle(). | 
|---|
| 3632 | * In the end we additionally find the point on the triangle who was smallest distance to \a Point: | 
|---|
| 3633 | *  -# Separate distance from point to center in vector in NormalDirection and on the triangle plane. | 
|---|
| 3634 | *  -# Check whether vector on triangle plane points inside the triangle or crosses triangle bounds. | 
|---|
| 3635 | *  -# If inside, take it to calculate closest distance | 
|---|
| 3636 | *  -# If not, take intersection with BoundaryLine as distance | 
|---|
| 3637 | * | 
|---|
| 3638 | * @note distance is squared despite it still contains a sign to determine in-/outside! | 
|---|
| 3639 | * | 
|---|
| 3640 | * @param point of which to check the position | 
|---|
| 3641 | * @param *LC LinkedCell structure | 
|---|
| 3642 | * | 
|---|
| 3643 | * @return >0 if outside, ==0 if on surface, <0 if inside | 
|---|
| 3644 | */ | 
|---|
| 3645 | double Tesselation::GetDistanceSquaredToTriangle(const Vector &Point, const BoundaryTriangleSet* const triangle) const | 
|---|
| 3646 | { | 
|---|
| 3647 | Info FunctionInfo(__func__); | 
|---|
| 3648 | Vector Center; | 
|---|
| 3649 | Vector helper; | 
|---|
| 3650 | Vector DistanceToCenter; | 
|---|
| 3651 | Vector Intersection; | 
|---|
| 3652 | double distance = 0.; | 
|---|
| 3653 |  | 
|---|
| 3654 | if (triangle == NULL) {// is boundary point or only point in point cloud? | 
|---|
| 3655 | DoLog(1) && (Log() << Verbose(1) << "No triangle given!" << endl); | 
|---|
| 3656 | return -1.; | 
|---|
| 3657 | } else { | 
|---|
| 3658 | DoLog(1) && (Log() << Verbose(1) << "INFO: Closest triangle found is " << *triangle << " with normal vector " << triangle->NormalVector << "." << endl); | 
|---|
| 3659 | } | 
|---|
| 3660 |  | 
|---|
| 3661 | triangle->GetCenter(&Center); | 
|---|
| 3662 | DoLog(2) && (Log() << Verbose(2) << "INFO: Central point of the triangle is " << Center << "." << endl); | 
|---|
| 3663 | DistanceToCenter = Center - Point; | 
|---|
| 3664 | DoLog(2) && (Log() << Verbose(2) << "INFO: Vector from point to test to center is " << DistanceToCenter << "." << endl); | 
|---|
| 3665 |  | 
|---|
| 3666 | // check whether we are on boundary | 
|---|
| 3667 | if (fabs(DistanceToCenter.ScalarProduct(triangle->NormalVector)) < MYEPSILON) { | 
|---|
| 3668 | // calculate whether inside of triangle | 
|---|
| 3669 | DistanceToCenter = Point + triangle->NormalVector; // points outside | 
|---|
| 3670 | Center = Point - triangle->NormalVector; // points towards MolCenter | 
|---|
| 3671 | DoLog(1) && (Log() << Verbose(1) << "INFO: Calling Intersection with " << Center << " and " << DistanceToCenter << "." << endl); | 
|---|
| 3672 | if (triangle->GetIntersectionInsideTriangle(&Center, &DistanceToCenter, &Intersection)) { | 
|---|
| 3673 | DoLog(1) && (Log() << Verbose(1) << Point << " is inner point: sufficiently close to boundary, " << Intersection << "." << endl); | 
|---|
| 3674 | return 0.; | 
|---|
| 3675 | } else { | 
|---|
| 3676 | DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point: on triangle plane but outside of triangle bounds." << endl); | 
|---|
| 3677 | return false; | 
|---|
| 3678 | } | 
|---|
| 3679 | } else { | 
|---|
| 3680 | // calculate smallest distance | 
|---|
| 3681 | distance = triangle->GetClosestPointInsideTriangle(&Point, &Intersection); | 
|---|
| 3682 | DoLog(1) && (Log() << Verbose(1) << "Closest point on triangle is " << Intersection << "." << endl); | 
|---|
| 3683 |  | 
|---|
| 3684 | // then check direction to boundary | 
|---|
| 3685 | if (DistanceToCenter.ScalarProduct(triangle->NormalVector) > MYEPSILON) { | 
|---|
| 3686 | DoLog(1) && (Log() << Verbose(1) << Point << " is an inner point, " << distance << " below surface." << endl); | 
|---|
| 3687 | return -distance; | 
|---|
| 3688 | } else { | 
|---|
| 3689 | DoLog(1) && (Log() << Verbose(1) << Point << " is NOT an inner point, " << distance << " above surface." << endl); | 
|---|
| 3690 | return +distance; | 
|---|
| 3691 | } | 
|---|
| 3692 | } | 
|---|
| 3693 | } | 
|---|
| 3694 | ; | 
|---|
| 3695 |  | 
|---|
| 3696 | /** Calculates minimum distance from \a&Point to a tesselated surface. | 
|---|
| 3697 | * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle(). | 
|---|
| 3698 | * \param &Point point to calculate distance from | 
|---|
| 3699 | * \param *LC needed for finding closest points fast | 
|---|
| 3700 | * \return distance squared to closest point on surface | 
|---|
| 3701 | */ | 
|---|
| 3702 | double Tesselation::GetDistanceToSurface(const Vector &Point, const LinkedCell* const LC) const | 
|---|
| 3703 | { | 
|---|
| 3704 | Info FunctionInfo(__func__); | 
|---|
| 3705 | TriangleIntersectionList Intersections(&Point, this, LC); | 
|---|
| 3706 |  | 
|---|
| 3707 | return Intersections.GetSmallestDistance(); | 
|---|
| 3708 | } | 
|---|
| 3709 | ; | 
|---|
| 3710 |  | 
|---|
| 3711 | /** Calculates minimum distance from \a&Point to a tesselated surface. | 
|---|
| 3712 | * Combines \sa FindClosestTrianglesToVector() and \sa GetDistanceSquaredToTriangle(). | 
|---|
| 3713 | * \param &Point point to calculate distance from | 
|---|
| 3714 | * \param *LC needed for finding closest points fast | 
|---|
| 3715 | * \return distance squared to closest point on surface | 
|---|
| 3716 | */ | 
|---|
| 3717 | BoundaryTriangleSet * Tesselation::GetClosestTriangleOnSurface(const Vector &Point, const LinkedCell* const LC) const | 
|---|
| 3718 | { | 
|---|
| 3719 | Info FunctionInfo(__func__); | 
|---|
| 3720 | TriangleIntersectionList Intersections(&Point, this, LC); | 
|---|
| 3721 |  | 
|---|
| 3722 | return Intersections.GetClosestTriangle(); | 
|---|
| 3723 | } | 
|---|
| 3724 | ; | 
|---|
| 3725 |  | 
|---|
| 3726 | /** Gets all points connected to the provided point by triangulation lines. | 
|---|
| 3727 | * | 
|---|
| 3728 | * @param *Point of which get all connected points | 
|---|
| 3729 | * | 
|---|
| 3730 | * @return set of the all points linked to the provided one | 
|---|
| 3731 | */ | 
|---|
| 3732 | TesselPointSet * Tesselation::GetAllConnectedPoints(const TesselPoint* const Point) const | 
|---|
| 3733 | { | 
|---|
| 3734 | Info FunctionInfo(__func__); | 
|---|
| 3735 | TesselPointSet *connectedPoints = new TesselPointSet; | 
|---|
| 3736 | class BoundaryPointSet *ReferencePoint = NULL; | 
|---|
| 3737 | TesselPoint* current; | 
|---|
| 3738 | bool takePoint = false; | 
|---|
| 3739 | // find the respective boundary point | 
|---|
| 3740 | PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr); | 
|---|
| 3741 | if (PointRunner != PointsOnBoundary.end()) { | 
|---|
| 3742 | ReferencePoint = PointRunner->second; | 
|---|
| 3743 | } else { | 
|---|
| 3744 | DoeLog(2) && (eLog() << Verbose(2) << "GetAllConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl); | 
|---|
| 3745 | ReferencePoint = NULL; | 
|---|
| 3746 | } | 
|---|
| 3747 |  | 
|---|
| 3748 | // little trick so that we look just through lines connect to the BoundaryPoint | 
|---|
| 3749 | // OR fall-back to look through all lines if there is no such BoundaryPoint | 
|---|
| 3750 | const LineMap *Lines; | 
|---|
| 3751 | ; | 
|---|
| 3752 | if (ReferencePoint != NULL) | 
|---|
| 3753 | Lines = &(ReferencePoint->lines); | 
|---|
| 3754 | else | 
|---|
| 3755 | Lines = &LinesOnBoundary; | 
|---|
| 3756 | LineMap::const_iterator findLines = Lines->begin(); | 
|---|
| 3757 | while (findLines != Lines->end()) { | 
|---|
| 3758 | takePoint = false; | 
|---|
| 3759 |  | 
|---|
| 3760 | if (findLines->second->endpoints[0]->Nr == Point->nr) { | 
|---|
| 3761 | takePoint = true; | 
|---|
| 3762 | current = findLines->second->endpoints[1]->node; | 
|---|
| 3763 | } else if (findLines->second->endpoints[1]->Nr == Point->nr) { | 
|---|
| 3764 | takePoint = true; | 
|---|
| 3765 | current = findLines->second->endpoints[0]->node; | 
|---|
| 3766 | } | 
|---|
| 3767 |  | 
|---|
| 3768 | if (takePoint) { | 
|---|
| 3769 | DoLog(1) && (Log() << Verbose(1) << "INFO: Endpoint " << *current << " of line " << *(findLines->second) << " is enlisted." << endl); | 
|---|
| 3770 | connectedPoints->insert(current); | 
|---|
| 3771 | } | 
|---|
| 3772 |  | 
|---|
| 3773 | findLines++; | 
|---|
| 3774 | } | 
|---|
| 3775 |  | 
|---|
| 3776 | if (connectedPoints->empty()) { // if have not found any points | 
|---|
| 3777 | DoeLog(1) && (eLog() << Verbose(1) << "We have not found any connected points to " << *Point << "." << endl); | 
|---|
| 3778 | return NULL; | 
|---|
| 3779 | } | 
|---|
| 3780 |  | 
|---|
| 3781 | return connectedPoints; | 
|---|
| 3782 | } | 
|---|
| 3783 | ; | 
|---|
| 3784 |  | 
|---|
| 3785 | /** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point. | 
|---|
| 3786 | * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points | 
|---|
| 3787 | * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given | 
|---|
| 3788 | * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the | 
|---|
| 3789 | * triangle we are looking for. | 
|---|
| 3790 | * | 
|---|
| 3791 | * @param *out output stream for debugging | 
|---|
| 3792 | * @param *SetOfNeighbours all points for which the angle should be calculated | 
|---|
| 3793 | * @param *Point of which get all connected points | 
|---|
| 3794 | * @param *Reference Reference vector for zero angle or NULL for no preference | 
|---|
| 3795 | * @return list of the all points linked to the provided one | 
|---|
| 3796 | */ | 
|---|
| 3797 | TesselPointList * Tesselation::GetCircleOfConnectedTriangles(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const | 
|---|
| 3798 | { | 
|---|
| 3799 | Info FunctionInfo(__func__); | 
|---|
| 3800 | map<double, TesselPoint*> anglesOfPoints; | 
|---|
| 3801 | TesselPointList *connectedCircle = new TesselPointList; | 
|---|
| 3802 | Vector PlaneNormal; | 
|---|
| 3803 | Vector AngleZero; | 
|---|
| 3804 | Vector OrthogonalVector; | 
|---|
| 3805 | Vector helper; | 
|---|
| 3806 | const TesselPoint * const TrianglePoints[3] = { Point, NULL, NULL }; | 
|---|
| 3807 | TriangleList *triangles = NULL; | 
|---|
| 3808 |  | 
|---|
| 3809 | if (SetOfNeighbours == NULL) { | 
|---|
| 3810 | DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl); | 
|---|
| 3811 | delete (connectedCircle); | 
|---|
| 3812 | return NULL; | 
|---|
| 3813 | } | 
|---|
| 3814 |  | 
|---|
| 3815 | // calculate central point | 
|---|
| 3816 | triangles = FindTriangles(TrianglePoints); | 
|---|
| 3817 | if ((triangles != NULL) && (!triangles->empty())) { | 
|---|
| 3818 | for (TriangleList::iterator Runner = triangles->begin(); Runner != triangles->end(); Runner++) | 
|---|
| 3819 | PlaneNormal += (*Runner)->NormalVector; | 
|---|
| 3820 | } else { | 
|---|
| 3821 | DoeLog(0) && (eLog() << Verbose(0) << "Could not find any triangles for point " << *Point << "." << endl); | 
|---|
| 3822 | performCriticalExit(); | 
|---|
| 3823 | } | 
|---|
| 3824 | PlaneNormal.Scale(1.0 / triangles->size()); | 
|---|
| 3825 | DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated PlaneNormal of all circle points is " << PlaneNormal << "." << endl); | 
|---|
| 3826 | PlaneNormal.Normalize(); | 
|---|
| 3827 |  | 
|---|
| 3828 | // construct one orthogonal vector | 
|---|
| 3829 | if (Reference != NULL) { | 
|---|
| 3830 | AngleZero = (*Reference) - (*Point->node); | 
|---|
| 3831 | AngleZero.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3832 | } | 
|---|
| 3833 | if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON)) { | 
|---|
| 3834 | DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl); | 
|---|
| 3835 | AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node); | 
|---|
| 3836 | AngleZero.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3837 | if (AngleZero.NormSquared() < MYEPSILON) { | 
|---|
| 3838 | DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl); | 
|---|
| 3839 | performCriticalExit(); | 
|---|
| 3840 | } | 
|---|
| 3841 | } | 
|---|
| 3842 | DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl); | 
|---|
| 3843 | if (AngleZero.NormSquared() > MYEPSILON) | 
|---|
| 3844 | OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal(); | 
|---|
| 3845 | else | 
|---|
| 3846 | OrthogonalVector.MakeNormalTo(PlaneNormal); | 
|---|
| 3847 | DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl); | 
|---|
| 3848 |  | 
|---|
| 3849 | // go through all connected points and calculate angle | 
|---|
| 3850 | for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) { | 
|---|
| 3851 | helper = (*(*listRunner)->node) - (*Point->node); | 
|---|
| 3852 | helper.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3853 | double angle = GetAngle(helper, AngleZero, OrthogonalVector); | 
|---|
| 3854 | DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle is " << angle << " for point " << **listRunner << "." << endl); | 
|---|
| 3855 | anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner))); | 
|---|
| 3856 | } | 
|---|
| 3857 |  | 
|---|
| 3858 | for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) { | 
|---|
| 3859 | connectedCircle->push_back(AngleRunner->second); | 
|---|
| 3860 | } | 
|---|
| 3861 |  | 
|---|
| 3862 | return connectedCircle; | 
|---|
| 3863 | } | 
|---|
| 3864 |  | 
|---|
| 3865 | /** Gets all points connected to the provided point by triangulation lines, ordered such that we have the circle round the point. | 
|---|
| 3866 | * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points | 
|---|
| 3867 | * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given | 
|---|
| 3868 | * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the | 
|---|
| 3869 | * triangle we are looking for. | 
|---|
| 3870 | * | 
|---|
| 3871 | * @param *SetOfNeighbours all points for which the angle should be calculated | 
|---|
| 3872 | * @param *Point of which get all connected points | 
|---|
| 3873 | * @param *Reference Reference vector for zero angle or NULL for no preference | 
|---|
| 3874 | * @return list of the all points linked to the provided one | 
|---|
| 3875 | */ | 
|---|
| 3876 | TesselPointList * Tesselation::GetCircleOfSetOfPoints(TesselPointSet *SetOfNeighbours, const TesselPoint* const Point, const Vector * const Reference) const | 
|---|
| 3877 | { | 
|---|
| 3878 | Info FunctionInfo(__func__); | 
|---|
| 3879 | map<double, TesselPoint*> anglesOfPoints; | 
|---|
| 3880 | TesselPointList *connectedCircle = new TesselPointList; | 
|---|
| 3881 | Vector center; | 
|---|
| 3882 | Vector PlaneNormal; | 
|---|
| 3883 | Vector AngleZero; | 
|---|
| 3884 | Vector OrthogonalVector; | 
|---|
| 3885 | Vector helper; | 
|---|
| 3886 |  | 
|---|
| 3887 | if (SetOfNeighbours == NULL) { | 
|---|
| 3888 | DoeLog(2) && (eLog() << Verbose(2) << "Could not find any connected points!" << endl); | 
|---|
| 3889 | delete (connectedCircle); | 
|---|
| 3890 | return NULL; | 
|---|
| 3891 | } | 
|---|
| 3892 |  | 
|---|
| 3893 | // check whether there's something to do | 
|---|
| 3894 | if (SetOfNeighbours->size() < 3) { | 
|---|
| 3895 | for (TesselPointSet::iterator TesselRunner = SetOfNeighbours->begin(); TesselRunner != SetOfNeighbours->end(); TesselRunner++) | 
|---|
| 3896 | connectedCircle->push_back(*TesselRunner); | 
|---|
| 3897 | return connectedCircle; | 
|---|
| 3898 | } | 
|---|
| 3899 |  | 
|---|
| 3900 | DoLog(1) && (Log() << Verbose(1) << "INFO: Point is " << *Point << " and Reference is " << *Reference << "." << endl); | 
|---|
| 3901 | // calculate central point | 
|---|
| 3902 | TesselPointSet::const_iterator TesselA = SetOfNeighbours->begin(); | 
|---|
| 3903 | TesselPointSet::const_iterator TesselB = SetOfNeighbours->begin(); | 
|---|
| 3904 | TesselPointSet::const_iterator TesselC = SetOfNeighbours->begin(); | 
|---|
| 3905 | TesselB++; | 
|---|
| 3906 | TesselC++; | 
|---|
| 3907 | TesselC++; | 
|---|
| 3908 | int counter = 0; | 
|---|
| 3909 | while (TesselC != SetOfNeighbours->end()) { | 
|---|
| 3910 | helper = Plane(*((*TesselA)->node), | 
|---|
| 3911 | *((*TesselB)->node), | 
|---|
| 3912 | *((*TesselC)->node)).getNormal(); | 
|---|
| 3913 | DoLog(0) && (Log() << Verbose(0) << "Making normal vector out of " << *(*TesselA) << ", " << *(*TesselB) << " and " << *(*TesselC) << ":" << helper << endl); | 
|---|
| 3914 | counter++; | 
|---|
| 3915 | TesselA++; | 
|---|
| 3916 | TesselB++; | 
|---|
| 3917 | TesselC++; | 
|---|
| 3918 | PlaneNormal += helper; | 
|---|
| 3919 | } | 
|---|
| 3920 | //Log() << Verbose(0) << "Summed vectors " << center << "; number of points " << connectedPoints.size() | 
|---|
| 3921 | //  << "; scale factor " << counter; | 
|---|
| 3922 | PlaneNormal.Scale(1.0 / (double) counter); | 
|---|
| 3923 | //  Log() << Verbose(1) << "INFO: Calculated center of all circle points is " << center << "." << endl; | 
|---|
| 3924 | // | 
|---|
| 3925 | //  // projection plane of the circle is at the closes Point and normal is pointing away from center of all circle points | 
|---|
| 3926 | //  PlaneNormal.CopyVector(Point->node); | 
|---|
| 3927 | //  PlaneNormal.SubtractVector(¢er); | 
|---|
| 3928 | //  PlaneNormal.Normalize(); | 
|---|
| 3929 | DoLog(1) && (Log() << Verbose(1) << "INFO: Calculated plane normal of circle is " << PlaneNormal << "." << endl); | 
|---|
| 3930 |  | 
|---|
| 3931 | // construct one orthogonal vector | 
|---|
| 3932 | if (Reference != NULL) { | 
|---|
| 3933 | AngleZero = (*Reference) - (*Point->node); | 
|---|
| 3934 | AngleZero.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3935 | } | 
|---|
| 3936 | if ((Reference == NULL) || (AngleZero.NormSquared() < MYEPSILON )) { | 
|---|
| 3937 | DoLog(1) && (Log() << Verbose(1) << "Using alternatively " << *(*SetOfNeighbours->begin())->node << " as angle 0 referencer." << endl); | 
|---|
| 3938 | AngleZero = (*(*SetOfNeighbours->begin())->node) - (*Point->node); | 
|---|
| 3939 | AngleZero.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3940 | if (AngleZero.NormSquared() < MYEPSILON) { | 
|---|
| 3941 | DoeLog(0) && (eLog() << Verbose(0) << "CRITIAL: AngleZero is 0 even with alternative reference. The algorithm has to be changed here!" << endl); | 
|---|
| 3942 | performCriticalExit(); | 
|---|
| 3943 | } | 
|---|
| 3944 | } | 
|---|
| 3945 | DoLog(1) && (Log() << Verbose(1) << "INFO: Reference vector on this plane representing angle 0 is " << AngleZero << "." << endl); | 
|---|
| 3946 | if (AngleZero.NormSquared() > MYEPSILON) | 
|---|
| 3947 | OrthogonalVector = Plane(PlaneNormal, AngleZero,0).getNormal(); | 
|---|
| 3948 | else | 
|---|
| 3949 | OrthogonalVector.MakeNormalTo(PlaneNormal); | 
|---|
| 3950 | DoLog(1) && (Log() << Verbose(1) << "INFO: OrthogonalVector on plane is " << OrthogonalVector << "." << endl); | 
|---|
| 3951 |  | 
|---|
| 3952 | // go through all connected points and calculate angle | 
|---|
| 3953 | pair<map<double, TesselPoint*>::iterator, bool> InserterTest; | 
|---|
| 3954 | for (TesselPointSet::iterator listRunner = SetOfNeighbours->begin(); listRunner != SetOfNeighbours->end(); listRunner++) { | 
|---|
| 3955 | helper = (*(*listRunner)->node) - (*Point->node); | 
|---|
| 3956 | helper.ProjectOntoPlane(PlaneNormal); | 
|---|
| 3957 | double angle = GetAngle(helper, AngleZero, OrthogonalVector); | 
|---|
| 3958 | if (angle > M_PI) // the correction is of no use here (and not desired) | 
|---|
| 3959 | angle = 2. * M_PI - angle; | 
|---|
| 3960 | DoLog(0) && (Log() << Verbose(0) << "INFO: Calculated angle between " << helper << " and " << AngleZero << " is " << angle << " for point " << **listRunner << "." << endl); | 
|---|
| 3961 | InserterTest = anglesOfPoints.insert(pair<double, TesselPoint*> (angle, (*listRunner))); | 
|---|
| 3962 | if (!InserterTest.second) { | 
|---|
| 3963 | DoeLog(0) && (eLog() << Verbose(0) << "GetCircleOfSetOfPoints() got two atoms with same angle: " << *((InserterTest.first)->second) << " and " << (*listRunner) << endl); | 
|---|
| 3964 | performCriticalExit(); | 
|---|
| 3965 | } | 
|---|
| 3966 | } | 
|---|
| 3967 |  | 
|---|
| 3968 | for (map<double, TesselPoint*>::iterator AngleRunner = anglesOfPoints.begin(); AngleRunner != anglesOfPoints.end(); AngleRunner++) { | 
|---|
| 3969 | connectedCircle->push_back(AngleRunner->second); | 
|---|
| 3970 | } | 
|---|
| 3971 |  | 
|---|
| 3972 | return connectedCircle; | 
|---|
| 3973 | } | 
|---|
| 3974 |  | 
|---|
| 3975 | /** Gets all points connected to the provided point by triangulation lines, ordered such that we walk along a closed path. | 
|---|
| 3976 | * | 
|---|
| 3977 | * @param *out output stream for debugging | 
|---|
| 3978 | * @param *Point of which get all connected points | 
|---|
| 3979 | * @return list of the all points linked to the provided one | 
|---|
| 3980 | */ | 
|---|
| 3981 | ListOfTesselPointList * Tesselation::GetPathsOfConnectedPoints(const TesselPoint* const Point) const | 
|---|
| 3982 | { | 
|---|
| 3983 | Info FunctionInfo(__func__); | 
|---|
| 3984 | map<double, TesselPoint*> anglesOfPoints; | 
|---|
| 3985 | list<TesselPointList *> *ListOfPaths = new list<TesselPointList *> ; | 
|---|
| 3986 | TesselPointList *connectedPath = NULL; | 
|---|
| 3987 | Vector center; | 
|---|
| 3988 | Vector PlaneNormal; | 
|---|
| 3989 | Vector AngleZero; | 
|---|
| 3990 | Vector OrthogonalVector; | 
|---|
| 3991 | Vector helper; | 
|---|
| 3992 | class BoundaryPointSet *ReferencePoint = NULL; | 
|---|
| 3993 | class BoundaryPointSet *CurrentPoint = NULL; | 
|---|
| 3994 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
| 3995 | class BoundaryLineSet *CurrentLine = NULL; | 
|---|
| 3996 | class BoundaryLineSet *StartLine = NULL; | 
|---|
| 3997 | // find the respective boundary point | 
|---|
| 3998 | PointMap::const_iterator PointRunner = PointsOnBoundary.find(Point->nr); | 
|---|
| 3999 | if (PointRunner != PointsOnBoundary.end()) { | 
|---|
| 4000 | ReferencePoint = PointRunner->second; | 
|---|
| 4001 | } else { | 
|---|
| 4002 | DoeLog(1) && (eLog() << Verbose(1) << "GetPathOfConnectedPoints() could not find the BoundaryPoint belonging to " << *Point << "." << endl); | 
|---|
| 4003 | return NULL; | 
|---|
| 4004 | } | 
|---|
| 4005 |  | 
|---|
| 4006 | map<class BoundaryLineSet *, bool> TouchedLine; | 
|---|
| 4007 | map<class BoundaryTriangleSet *, bool> TouchedTriangle; | 
|---|
| 4008 | map<class BoundaryLineSet *, bool>::iterator LineRunner; | 
|---|
| 4009 | map<class BoundaryTriangleSet *, bool>::iterator TriangleRunner; | 
|---|
| 4010 | for (LineMap::iterator Runner = ReferencePoint->lines.begin(); Runner != ReferencePoint->lines.end(); Runner++) { | 
|---|
| 4011 | TouchedLine.insert(pair<class BoundaryLineSet *, bool> (Runner->second, false)); | 
|---|
| 4012 | for (TriangleMap::iterator Sprinter = Runner->second->triangles.begin(); Sprinter != Runner->second->triangles.end(); Sprinter++) | 
|---|
| 4013 | TouchedTriangle.insert(pair<class BoundaryTriangleSet *, bool> (Sprinter->second, false)); | 
|---|
| 4014 | } | 
|---|
| 4015 | if (!ReferencePoint->lines.empty()) { | 
|---|
| 4016 | for (LineMap::iterator runner = ReferencePoint->lines.begin(); runner != ReferencePoint->lines.end(); runner++) { | 
|---|
| 4017 | LineRunner = TouchedLine.find(runner->second); | 
|---|
| 4018 | if (LineRunner == TouchedLine.end()) { | 
|---|
| 4019 | DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *runner->second << " in the touched list." << endl); | 
|---|
| 4020 | } else if (!LineRunner->second) { | 
|---|
| 4021 | LineRunner->second = true; | 
|---|
| 4022 | connectedPath = new TesselPointList; | 
|---|
| 4023 | triangle = NULL; | 
|---|
| 4024 | CurrentLine = runner->second; | 
|---|
| 4025 | StartLine = CurrentLine; | 
|---|
| 4026 | CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint); | 
|---|
| 4027 | DoLog(1) && (Log() << Verbose(1) << "INFO: Beginning path retrieval at " << *CurrentPoint << " of line " << *CurrentLine << "." << endl); | 
|---|
| 4028 | do { | 
|---|
| 4029 | // push current one | 
|---|
| 4030 | DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl); | 
|---|
| 4031 | connectedPath->push_back(CurrentPoint->node); | 
|---|
| 4032 |  | 
|---|
| 4033 | // find next triangle | 
|---|
| 4034 | for (TriangleMap::iterator Runner = CurrentLine->triangles.begin(); Runner != CurrentLine->triangles.end(); Runner++) { | 
|---|
| 4035 | DoLog(1) && (Log() << Verbose(1) << "INFO: Inspecting triangle " << *Runner->second << "." << endl); | 
|---|
| 4036 | if ((Runner->second != triangle)) { // look for first triangle not equal to old one | 
|---|
| 4037 | triangle = Runner->second; | 
|---|
| 4038 | TriangleRunner = TouchedTriangle.find(triangle); | 
|---|
| 4039 | if (TriangleRunner != TouchedTriangle.end()) { | 
|---|
| 4040 | if (!TriangleRunner->second) { | 
|---|
| 4041 | TriangleRunner->second = true; | 
|---|
| 4042 | DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting triangle is " << *triangle << "." << endl); | 
|---|
| 4043 | break; | 
|---|
| 4044 | } else { | 
|---|
| 4045 | DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *triangle << ", as we have already visited it." << endl); | 
|---|
| 4046 | triangle = NULL; | 
|---|
| 4047 | } | 
|---|
| 4048 | } else { | 
|---|
| 4049 | DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *triangle << " in the touched list." << endl); | 
|---|
| 4050 | triangle = NULL; | 
|---|
| 4051 | } | 
|---|
| 4052 | } | 
|---|
| 4053 | } | 
|---|
| 4054 | if (triangle == NULL) | 
|---|
| 4055 | break; | 
|---|
| 4056 | // find next line | 
|---|
| 4057 | for (int i = 0; i < 3; i++) { | 
|---|
| 4058 | if ((triangle->lines[i] != CurrentLine) && (triangle->lines[i]->ContainsBoundaryPoint(ReferencePoint))) { // not the current line and still containing Point | 
|---|
| 4059 | CurrentLine = triangle->lines[i]; | 
|---|
| 4060 | DoLog(1) && (Log() << Verbose(1) << "INFO: Connecting line is " << *CurrentLine << "." << endl); | 
|---|
| 4061 | break; | 
|---|
| 4062 | } | 
|---|
| 4063 | } | 
|---|
| 4064 | LineRunner = TouchedLine.find(CurrentLine); | 
|---|
| 4065 | if (LineRunner == TouchedLine.end()) | 
|---|
| 4066 | DoeLog(1) && (eLog() << Verbose(1) << "I could not find " << *CurrentLine << " in the touched list." << endl); | 
|---|
| 4067 | else | 
|---|
| 4068 | LineRunner->second = true; | 
|---|
| 4069 | // find next point | 
|---|
| 4070 | CurrentPoint = CurrentLine->GetOtherEndpoint(ReferencePoint); | 
|---|
| 4071 |  | 
|---|
| 4072 | } while (CurrentLine != StartLine); | 
|---|
| 4073 | // last point is missing, as it's on start line | 
|---|
| 4074 | DoLog(1) && (Log() << Verbose(1) << "INFO: Putting " << *CurrentPoint << " at end of path." << endl); | 
|---|
| 4075 | if (StartLine->GetOtherEndpoint(ReferencePoint)->node != connectedPath->back()) | 
|---|
| 4076 | connectedPath->push_back(StartLine->GetOtherEndpoint(ReferencePoint)->node); | 
|---|
| 4077 |  | 
|---|
| 4078 | ListOfPaths->push_back(connectedPath); | 
|---|
| 4079 | } else { | 
|---|
| 4080 | DoLog(1) && (Log() << Verbose(1) << "INFO: Skipping " << *runner->second << ", as we have already visited it." << endl); | 
|---|
| 4081 | } | 
|---|
| 4082 | } | 
|---|
| 4083 | } else { | 
|---|
| 4084 | DoeLog(1) && (eLog() << Verbose(1) << "There are no lines attached to " << *ReferencePoint << "." << endl); | 
|---|
| 4085 | } | 
|---|
| 4086 |  | 
|---|
| 4087 | return ListOfPaths; | 
|---|
| 4088 | } | 
|---|
| 4089 |  | 
|---|
| 4090 | /** Gets all closed paths on the circle of points connected to the provided point by triangulation lines, if this very point is removed. | 
|---|
| 4091 | * From GetPathsOfConnectedPoints() extracts all single loops of intracrossing paths in the list of closed paths. | 
|---|
| 4092 | * @param *out output stream for debugging | 
|---|
| 4093 | * @param *Point of which get all connected points | 
|---|
| 4094 | * @return list of the closed paths | 
|---|
| 4095 | */ | 
|---|
| 4096 | ListOfTesselPointList * Tesselation::GetClosedPathsOfConnectedPoints(const TesselPoint* const Point) const | 
|---|
| 4097 | { | 
|---|
| 4098 | Info FunctionInfo(__func__); | 
|---|
| 4099 | list<TesselPointList *> *ListofPaths = GetPathsOfConnectedPoints(Point); | 
|---|
| 4100 | list<TesselPointList *> *ListofClosedPaths = new list<TesselPointList *> ; | 
|---|
| 4101 | TesselPointList *connectedPath = NULL; | 
|---|
| 4102 | TesselPointList *newPath = NULL; | 
|---|
| 4103 | int count = 0; | 
|---|
| 4104 | TesselPointList::iterator CircleRunner; | 
|---|
| 4105 | TesselPointList::iterator CircleStart; | 
|---|
| 4106 |  | 
|---|
| 4107 | for (list<TesselPointList *>::iterator ListRunner = ListofPaths->begin(); ListRunner != ListofPaths->end(); ListRunner++) { | 
|---|
| 4108 | connectedPath = *ListRunner; | 
|---|
| 4109 |  | 
|---|
| 4110 | DoLog(1) && (Log() << Verbose(1) << "INFO: Current path is " << connectedPath << "." << endl); | 
|---|
| 4111 |  | 
|---|
| 4112 | // go through list, look for reappearance of starting Point and count | 
|---|
| 4113 | CircleStart = connectedPath->begin(); | 
|---|
| 4114 | // go through list, look for reappearance of starting Point and create list | 
|---|
| 4115 | TesselPointList::iterator Marker = CircleStart; | 
|---|
| 4116 | for (CircleRunner = CircleStart; CircleRunner != connectedPath->end(); CircleRunner++) { | 
|---|
| 4117 | if ((*CircleRunner == *CircleStart) && (CircleRunner != CircleStart)) { // is not the very first point | 
|---|
| 4118 | // we have a closed circle from Marker to new Marker | 
|---|
| 4119 | DoLog(1) && (Log() << Verbose(1) << count + 1 << ". closed path consists of: "); | 
|---|
| 4120 | newPath = new TesselPointList; | 
|---|
| 4121 | TesselPointList::iterator CircleSprinter = Marker; | 
|---|
| 4122 | for (; CircleSprinter != CircleRunner; CircleSprinter++) { | 
|---|
| 4123 | newPath->push_back(*CircleSprinter); | 
|---|
| 4124 | DoLog(0) && (Log() << Verbose(0) << (**CircleSprinter) << " <-> "); | 
|---|
| 4125 | } | 
|---|
| 4126 | DoLog(0) && (Log() << Verbose(0) << ".." << endl); | 
|---|
| 4127 | count++; | 
|---|
| 4128 | Marker = CircleRunner; | 
|---|
| 4129 |  | 
|---|
| 4130 | // add to list | 
|---|
| 4131 | ListofClosedPaths->push_back(newPath); | 
|---|
| 4132 | } | 
|---|
| 4133 | } | 
|---|
| 4134 | } | 
|---|
| 4135 | DoLog(1) && (Log() << Verbose(1) << "INFO: " << count << " closed additional path(s) have been created." << endl); | 
|---|
| 4136 |  | 
|---|
| 4137 | // delete list of paths | 
|---|
| 4138 | while (!ListofPaths->empty()) { | 
|---|
| 4139 | connectedPath = *(ListofPaths->begin()); | 
|---|
| 4140 | ListofPaths->remove(connectedPath); | 
|---|
| 4141 | delete (connectedPath); | 
|---|
| 4142 | } | 
|---|
| 4143 | delete (ListofPaths); | 
|---|
| 4144 |  | 
|---|
| 4145 | // exit | 
|---|
| 4146 | return ListofClosedPaths; | 
|---|
| 4147 | } | 
|---|
| 4148 | ; | 
|---|
| 4149 |  | 
|---|
| 4150 | /** Gets all belonging triangles for a given BoundaryPointSet. | 
|---|
| 4151 | * \param *out output stream for debugging | 
|---|
| 4152 | * \param *Point BoundaryPoint | 
|---|
| 4153 | * \return pointer to allocated list of triangles | 
|---|
| 4154 | */ | 
|---|
| 4155 | TriangleSet *Tesselation::GetAllTriangles(const BoundaryPointSet * const Point) const | 
|---|
| 4156 | { | 
|---|
| 4157 | Info FunctionInfo(__func__); | 
|---|
| 4158 | TriangleSet *connectedTriangles = new TriangleSet; | 
|---|
| 4159 |  | 
|---|
| 4160 | if (Point == NULL) { | 
|---|
| 4161 | DoeLog(1) && (eLog() << Verbose(1) << "Point given is NULL." << endl); | 
|---|
| 4162 | } else { | 
|---|
| 4163 | // go through its lines and insert all triangles | 
|---|
| 4164 | for (LineMap::const_iterator LineRunner = Point->lines.begin(); LineRunner != Point->lines.end(); LineRunner++) | 
|---|
| 4165 | for (TriangleMap::iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) { | 
|---|
| 4166 | connectedTriangles->insert(TriangleRunner->second); | 
|---|
| 4167 | } | 
|---|
| 4168 | } | 
|---|
| 4169 |  | 
|---|
| 4170 | return connectedTriangles; | 
|---|
| 4171 | } | 
|---|
| 4172 | ; | 
|---|
| 4173 |  | 
|---|
| 4174 | /** Removes a boundary point from the envelope while keeping it closed. | 
|---|
| 4175 | * We remove the old triangles connected to the point and re-create new triangles to close the surface following this ansatz: | 
|---|
| 4176 | *  -# a closed path(s) of boundary points surrounding the point to be removed is constructed | 
|---|
| 4177 | *  -# on each closed path, we pick three adjacent points, create a triangle with them and subtract the middle point from the path | 
|---|
| 4178 | *  -# we advance two points (i.e. the next triangle will start at the ending point of the last triangle) and continue as before | 
|---|
| 4179 | *  -# the surface is closed, when the path is empty | 
|---|
| 4180 | * Thereby, we (hopefully) make sure that the removed points remains beneath the surface (this is checked via IsInnerPoint eventually). | 
|---|
| 4181 | * \param *out output stream for debugging | 
|---|
| 4182 | * \param *point point to be removed | 
|---|
| 4183 | * \return volume added to the volume inside the tesselated surface by the removal | 
|---|
| 4184 | */ | 
|---|
| 4185 | double Tesselation::RemovePointFromTesselatedSurface(class BoundaryPointSet *point) | 
|---|
| 4186 | { | 
|---|
| 4187 | class BoundaryLineSet *line = NULL; | 
|---|
| 4188 | class BoundaryTriangleSet *triangle = NULL; | 
|---|
| 4189 | Vector OldPoint, NormalVector; | 
|---|
| 4190 | double volume = 0; | 
|---|
| 4191 | int count = 0; | 
|---|
| 4192 |  | 
|---|
| 4193 | if (point == NULL) { | 
|---|
| 4194 | DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << point << ", it's NULL!" << endl); | 
|---|
| 4195 | return 0.; | 
|---|
| 4196 | } else | 
|---|
| 4197 | DoLog(0) && (Log() << Verbose(0) << "Removing point " << *point << " from tesselated boundary ..." << endl); | 
|---|
| 4198 |  | 
|---|
| 4199 | // copy old location for the volume | 
|---|
| 4200 | OldPoint = (*point->node->node); | 
|---|
| 4201 |  | 
|---|
| 4202 | // get list of connected points | 
|---|
| 4203 | if (point->lines.empty()) { | 
|---|
| 4204 | DoeLog(1) && (eLog() << Verbose(1) << "Cannot remove the point " << *point << ", it's connected to no lines!" << endl); | 
|---|
| 4205 | return 0.; | 
|---|
| 4206 | } | 
|---|
| 4207 |  | 
|---|
| 4208 | list<TesselPointList *> *ListOfClosedPaths = GetClosedPathsOfConnectedPoints(point->node); | 
|---|
| 4209 | TesselPointList *connectedPath = NULL; | 
|---|
| 4210 |  | 
|---|
| 4211 | // gather all triangles | 
|---|
| 4212 | for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) | 
|---|
| 4213 | count += LineRunner->second->triangles.size(); | 
|---|
| 4214 | TriangleMap Candidates; | 
|---|
| 4215 | for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) { | 
|---|
| 4216 | line = LineRunner->second; | 
|---|
| 4217 | for (TriangleMap::iterator TriangleRunner = line->triangles.begin(); TriangleRunner != line->triangles.end(); TriangleRunner++) { | 
|---|
| 4218 | triangle = TriangleRunner->second; | 
|---|
| 4219 | Candidates.insert(TrianglePair(triangle->Nr, triangle)); | 
|---|
| 4220 | } | 
|---|
| 4221 | } | 
|---|
| 4222 |  | 
|---|
| 4223 | // remove all triangles | 
|---|
| 4224 | count = 0; | 
|---|
| 4225 | NormalVector.Zero(); | 
|---|
| 4226 | for (TriangleMap::iterator Runner = Candidates.begin(); Runner != Candidates.end(); Runner++) { | 
|---|
| 4227 | DoLog(1) && (Log() << Verbose(1) << "INFO: Removing triangle " << *(Runner->second) << "." << endl); | 
|---|
| 4228 | NormalVector -= Runner->second->NormalVector; // has to point inward | 
|---|
| 4229 | RemoveTesselationTriangle(Runner->second); | 
|---|
| 4230 | count++; | 
|---|
| 4231 | } | 
|---|
| 4232 | DoLog(1) && (Log() << Verbose(1) << count << " triangles were removed." << endl); | 
|---|
| 4233 |  | 
|---|
| 4234 | list<TesselPointList *>::iterator ListAdvance = ListOfClosedPaths->begin(); | 
|---|
| 4235 | list<TesselPointList *>::iterator ListRunner = ListAdvance; | 
|---|
| 4236 | TriangleMap::iterator NumberRunner = Candidates.begin(); | 
|---|
| 4237 | TesselPointList::iterator StartNode, MiddleNode, EndNode; | 
|---|
| 4238 | double angle; | 
|---|
| 4239 | double smallestangle; | 
|---|
| 4240 | Vector Point, Reference, OrthogonalVector; | 
|---|
| 4241 | if (count > 2) { // less than three triangles, then nothing will be created | 
|---|
| 4242 | class TesselPoint *TriangleCandidates[3]; | 
|---|
| 4243 | count = 0; | 
|---|
| 4244 | for (; ListRunner != ListOfClosedPaths->end(); ListRunner = ListAdvance) { // go through all closed paths | 
|---|
| 4245 | if (ListAdvance != ListOfClosedPaths->end()) | 
|---|
| 4246 | ListAdvance++; | 
|---|
| 4247 |  | 
|---|
| 4248 | connectedPath = *ListRunner; | 
|---|
| 4249 | // re-create all triangles by going through connected points list | 
|---|
| 4250 | LineList NewLines; | 
|---|
| 4251 | for (; !connectedPath->empty();) { | 
|---|
| 4252 | // search middle node with widest angle to next neighbours | 
|---|
| 4253 | EndNode = connectedPath->end(); | 
|---|
| 4254 | smallestangle = 0.; | 
|---|
| 4255 | for (MiddleNode = connectedPath->begin(); MiddleNode != connectedPath->end(); MiddleNode++) { | 
|---|
| 4256 | DoLog(1) && (Log() << Verbose(1) << "INFO: MiddleNode is " << **MiddleNode << "." << endl); | 
|---|
| 4257 | // construct vectors to next and previous neighbour | 
|---|
| 4258 | StartNode = MiddleNode; | 
|---|
| 4259 | if (StartNode == connectedPath->begin()) | 
|---|
| 4260 | StartNode = connectedPath->end(); | 
|---|
| 4261 | StartNode--; | 
|---|
| 4262 | //Log() << Verbose(3) << "INFO: StartNode is " << **StartNode << "." << endl; | 
|---|
| 4263 | Point = (*(*StartNode)->node) - (*(*MiddleNode)->node); | 
|---|
| 4264 | StartNode = MiddleNode; | 
|---|
| 4265 | StartNode++; | 
|---|
| 4266 | if (StartNode == connectedPath->end()) | 
|---|
| 4267 | StartNode = connectedPath->begin(); | 
|---|
| 4268 | //Log() << Verbose(3) << "INFO: EndNode is " << **StartNode << "." << endl; | 
|---|
| 4269 | Reference = (*(*StartNode)->node) - (*(*MiddleNode)->node); | 
|---|
| 4270 | OrthogonalVector = (*(*MiddleNode)->node) - OldPoint; | 
|---|
| 4271 | OrthogonalVector.MakeNormalTo(Reference); | 
|---|
| 4272 | angle = GetAngle(Point, Reference, OrthogonalVector); | 
|---|
| 4273 | //if (angle < M_PI)  // no wrong-sided triangles, please? | 
|---|
| 4274 | if (fabs(angle - M_PI) < fabs(smallestangle - M_PI)) { // get straightest angle (i.e. construct those triangles with smallest area first) | 
|---|
| 4275 | smallestangle = angle; | 
|---|
| 4276 | EndNode = MiddleNode; | 
|---|
| 4277 | } | 
|---|
| 4278 | } | 
|---|
| 4279 | MiddleNode = EndNode; | 
|---|
| 4280 | if (MiddleNode == connectedPath->end()) { | 
|---|
| 4281 | DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: Could not find a smallest angle!" << endl); | 
|---|
| 4282 | performCriticalExit(); | 
|---|
| 4283 | } | 
|---|
| 4284 | StartNode = MiddleNode; | 
|---|
| 4285 | if (StartNode == connectedPath->begin()) | 
|---|
| 4286 | StartNode = connectedPath->end(); | 
|---|
| 4287 | StartNode--; | 
|---|
| 4288 | EndNode++; | 
|---|
| 4289 | if (EndNode == connectedPath->end()) | 
|---|
| 4290 | EndNode = connectedPath->begin(); | 
|---|
| 4291 | DoLog(2) && (Log() << Verbose(2) << "INFO: StartNode is " << **StartNode << "." << endl); | 
|---|
| 4292 | DoLog(2) && (Log() << Verbose(2) << "INFO: MiddleNode is " << **MiddleNode << "." << endl); | 
|---|
| 4293 | DoLog(2) && (Log() << Verbose(2) << "INFO: EndNode is " << **EndNode << "." << endl); | 
|---|
| 4294 | DoLog(1) && (Log() << Verbose(1) << "INFO: Attempting to create triangle " << (*StartNode)->Name << ", " << (*MiddleNode)->Name << " and " << (*EndNode)->Name << "." << endl); | 
|---|
| 4295 | TriangleCandidates[0] = *StartNode; | 
|---|
| 4296 | TriangleCandidates[1] = *MiddleNode; | 
|---|
| 4297 | TriangleCandidates[2] = *EndNode; | 
|---|
| 4298 | triangle = GetPresentTriangle(TriangleCandidates); | 
|---|
| 4299 | if (triangle != NULL) { | 
|---|
| 4300 | DoeLog(0) && (eLog() << Verbose(0) << "New triangle already present, skipping!" << endl); | 
|---|
| 4301 | StartNode++; | 
|---|
| 4302 | MiddleNode++; | 
|---|
| 4303 | EndNode++; | 
|---|
| 4304 | if (StartNode == connectedPath->end()) | 
|---|
| 4305 | StartNode = connectedPath->begin(); | 
|---|
| 4306 | if (MiddleNode == connectedPath->end()) | 
|---|
| 4307 | MiddleNode = connectedPath->begin(); | 
|---|
| 4308 | if (EndNode == connectedPath->end()) | 
|---|
| 4309 | EndNode = connectedPath->begin(); | 
|---|
| 4310 | continue; | 
|---|
| 4311 | } | 
|---|
| 4312 | DoLog(3) && (Log() << Verbose(3) << "Adding new triangle points." << endl); | 
|---|
| 4313 | AddTesselationPoint(*StartNode, 0); | 
|---|
| 4314 | AddTesselationPoint(*MiddleNode, 1); | 
|---|
| 4315 | AddTesselationPoint(*EndNode, 2); | 
|---|
| 4316 | DoLog(3) && (Log() << Verbose(3) << "Adding new triangle lines." << endl); | 
|---|
| 4317 | AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0); | 
|---|
| 4318 | AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1); | 
|---|
| 4319 | NewLines.push_back(BLS[1]); | 
|---|
| 4320 | AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2); | 
|---|
| 4321 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 4322 | BTS->GetNormalVector(NormalVector); | 
|---|
| 4323 | AddTesselationTriangle(); | 
|---|
| 4324 | // calculate volume summand as a general tetraeder | 
|---|
| 4325 | volume += CalculateVolumeofGeneralTetraeder(*TPS[0]->node->node, *TPS[1]->node->node, *TPS[2]->node->node, OldPoint); | 
|---|
| 4326 | // advance number | 
|---|
| 4327 | count++; | 
|---|
| 4328 |  | 
|---|
| 4329 | // prepare nodes for next triangle | 
|---|
| 4330 | StartNode = EndNode; | 
|---|
| 4331 | DoLog(2) && (Log() << Verbose(2) << "Removing " << **MiddleNode << " from closed path, remaining points: " << connectedPath->size() << "." << endl); | 
|---|
| 4332 | connectedPath->remove(*MiddleNode); // remove the middle node (it is surrounded by triangles) | 
|---|
| 4333 | if (connectedPath->size() == 2) { // we are done | 
|---|
| 4334 | connectedPath->remove(*StartNode); // remove the start node | 
|---|
| 4335 | connectedPath->remove(*EndNode); // remove the end node | 
|---|
| 4336 | break; | 
|---|
| 4337 | } else if (connectedPath->size() < 2) { // something's gone wrong! | 
|---|
| 4338 | DoeLog(0) && (eLog() << Verbose(0) << "CRITICAL: There are only two endpoints left!" << endl); | 
|---|
| 4339 | performCriticalExit(); | 
|---|
| 4340 | } else { | 
|---|
| 4341 | MiddleNode = StartNode; | 
|---|
| 4342 | MiddleNode++; | 
|---|
| 4343 | if (MiddleNode == connectedPath->end()) | 
|---|
| 4344 | MiddleNode = connectedPath->begin(); | 
|---|
| 4345 | EndNode = MiddleNode; | 
|---|
| 4346 | EndNode++; | 
|---|
| 4347 | if (EndNode == connectedPath->end()) | 
|---|
| 4348 | EndNode = connectedPath->begin(); | 
|---|
| 4349 | } | 
|---|
| 4350 | } | 
|---|
| 4351 | // maximize the inner lines (we preferentially created lines with a huge angle, which is for the tesselation not wanted though useful for the closing) | 
|---|
| 4352 | if (NewLines.size() > 1) { | 
|---|
| 4353 | LineList::iterator Candidate; | 
|---|
| 4354 | class BoundaryLineSet *OtherBase = NULL; | 
|---|
| 4355 | double tmp, maxgain; | 
|---|
| 4356 | do { | 
|---|
| 4357 | maxgain = 0; | 
|---|
| 4358 | for (LineList::iterator Runner = NewLines.begin(); Runner != NewLines.end(); Runner++) { | 
|---|
| 4359 | tmp = PickFarthestofTwoBaselines(*Runner); | 
|---|
| 4360 | if (maxgain < tmp) { | 
|---|
| 4361 | maxgain = tmp; | 
|---|
| 4362 | Candidate = Runner; | 
|---|
| 4363 | } | 
|---|
| 4364 | } | 
|---|
| 4365 | if (maxgain != 0) { | 
|---|
| 4366 | volume += maxgain; | 
|---|
| 4367 | DoLog(1) && (Log() << Verbose(1) << "Flipping baseline with highest volume" << **Candidate << "." << endl); | 
|---|
| 4368 | OtherBase = FlipBaseline(*Candidate); | 
|---|
| 4369 | NewLines.erase(Candidate); | 
|---|
| 4370 | NewLines.push_back(OtherBase); | 
|---|
| 4371 | } | 
|---|
| 4372 | } while (maxgain != 0.); | 
|---|
| 4373 | } | 
|---|
| 4374 |  | 
|---|
| 4375 | ListOfClosedPaths->remove(connectedPath); | 
|---|
| 4376 | delete (connectedPath); | 
|---|
| 4377 | } | 
|---|
| 4378 | DoLog(0) && (Log() << Verbose(0) << count << " triangles were created." << endl); | 
|---|
| 4379 | } else { | 
|---|
| 4380 | while (!ListOfClosedPaths->empty()) { | 
|---|
| 4381 | ListRunner = ListOfClosedPaths->begin(); | 
|---|
| 4382 | connectedPath = *ListRunner; | 
|---|
| 4383 | ListOfClosedPaths->remove(connectedPath); | 
|---|
| 4384 | delete (connectedPath); | 
|---|
| 4385 | } | 
|---|
| 4386 | DoLog(0) && (Log() << Verbose(0) << "No need to create any triangles." << endl); | 
|---|
| 4387 | } | 
|---|
| 4388 | delete (ListOfClosedPaths); | 
|---|
| 4389 |  | 
|---|
| 4390 | DoLog(0) && (Log() << Verbose(0) << "Removed volume is " << volume << "." << endl); | 
|---|
| 4391 |  | 
|---|
| 4392 | return volume; | 
|---|
| 4393 | } | 
|---|
| 4394 | ; | 
|---|
| 4395 |  | 
|---|
| 4396 | /** | 
|---|
| 4397 | * Finds triangles belonging to the three provided points. | 
|---|
| 4398 | * | 
|---|
| 4399 | * @param *Points[3] list, is expected to contain three points (NULL means wildcard) | 
|---|
| 4400 | * | 
|---|
| 4401 | * @return triangles which belong to the provided points, will be empty if there are none, | 
|---|
| 4402 | *         will usually be one, in case of degeneration, there will be two | 
|---|
| 4403 | */ | 
|---|
| 4404 | TriangleList *Tesselation::FindTriangles(const TesselPoint* const Points[3]) const | 
|---|
| 4405 | { | 
|---|
| 4406 | Info FunctionInfo(__func__); | 
|---|
| 4407 | TriangleList *result = new TriangleList; | 
|---|
| 4408 | LineMap::const_iterator FindLine; | 
|---|
| 4409 | TriangleMap::const_iterator FindTriangle; | 
|---|
| 4410 | class BoundaryPointSet *TrianglePoints[3]; | 
|---|
| 4411 | size_t NoOfWildcards = 0; | 
|---|
| 4412 |  | 
|---|
| 4413 | for (int i = 0; i < 3; i++) { | 
|---|
| 4414 | if (Points[i] == NULL) { | 
|---|
| 4415 | NoOfWildcards++; | 
|---|
| 4416 | TrianglePoints[i] = NULL; | 
|---|
| 4417 | } else { | 
|---|
| 4418 | PointMap::const_iterator FindPoint = PointsOnBoundary.find(Points[i]->nr); | 
|---|
| 4419 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 4420 | TrianglePoints[i] = FindPoint->second; | 
|---|
| 4421 | } else { | 
|---|
| 4422 | TrianglePoints[i] = NULL; | 
|---|
| 4423 | } | 
|---|
| 4424 | } | 
|---|
| 4425 | } | 
|---|
| 4426 |  | 
|---|
| 4427 | switch (NoOfWildcards) { | 
|---|
| 4428 | case 0: // checks lines between the points in the Points for their adjacent triangles | 
|---|
| 4429 | for (int i = 0; i < 3; i++) { | 
|---|
| 4430 | if (TrianglePoints[i] != NULL) { | 
|---|
| 4431 | for (int j = i + 1; j < 3; j++) { | 
|---|
| 4432 | if (TrianglePoints[j] != NULL) { | 
|---|
| 4433 | for (FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); // is a multimap! | 
|---|
| 4434 | (FindLine != TrianglePoints[i]->lines.end()) && (FindLine->first == TrianglePoints[j]->node->nr); FindLine++) { | 
|---|
| 4435 | for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) { | 
|---|
| 4436 | if (FindTriangle->second->IsPresentTupel(TrianglePoints)) { | 
|---|
| 4437 | result->push_back(FindTriangle->second); | 
|---|
| 4438 | } | 
|---|
| 4439 | } | 
|---|
| 4440 | } | 
|---|
| 4441 | // Is it sufficient to consider one of the triangle lines for this. | 
|---|
| 4442 | return result; | 
|---|
| 4443 | } | 
|---|
| 4444 | } | 
|---|
| 4445 | } | 
|---|
| 4446 | } | 
|---|
| 4447 | break; | 
|---|
| 4448 | case 1: // copy all triangles of the respective line | 
|---|
| 4449 | { | 
|---|
| 4450 | int i = 0; | 
|---|
| 4451 | for (; i < 3; i++) | 
|---|
| 4452 | if (TrianglePoints[i] == NULL) | 
|---|
| 4453 | break; | 
|---|
| 4454 | for (FindLine = TrianglePoints[(i + 1) % 3]->lines.find(TrianglePoints[(i + 2) % 3]->node->nr); // is a multimap! | 
|---|
| 4455 | (FindLine != TrianglePoints[(i + 1) % 3]->lines.end()) && (FindLine->first == TrianglePoints[(i + 2) % 3]->node->nr); FindLine++) { | 
|---|
| 4456 | for (FindTriangle = FindLine->second->triangles.begin(); FindTriangle != FindLine->second->triangles.end(); FindTriangle++) { | 
|---|
| 4457 | if (FindTriangle->second->IsPresentTupel(TrianglePoints)) { | 
|---|
| 4458 | result->push_back(FindTriangle->second); | 
|---|
| 4459 | } | 
|---|
| 4460 | } | 
|---|
| 4461 | } | 
|---|
| 4462 | break; | 
|---|
| 4463 | } | 
|---|
| 4464 | case 2: // copy all triangles of the respective point | 
|---|
| 4465 | { | 
|---|
| 4466 | int i = 0; | 
|---|
| 4467 | for (; i < 3; i++) | 
|---|
| 4468 | if (TrianglePoints[i] != NULL) | 
|---|
| 4469 | break; | 
|---|
| 4470 | for (LineMap::const_iterator line = TrianglePoints[i]->lines.begin(); line != TrianglePoints[i]->lines.end(); line++) | 
|---|
| 4471 | for (TriangleMap::const_iterator triangle = line->second->triangles.begin(); triangle != line->second->triangles.end(); triangle++) | 
|---|
| 4472 | result->push_back(triangle->second); | 
|---|
| 4473 | result->sort(); | 
|---|
| 4474 | result->unique(); | 
|---|
| 4475 | break; | 
|---|
| 4476 | } | 
|---|
| 4477 | case 3: // copy all triangles | 
|---|
| 4478 | { | 
|---|
| 4479 | for (TriangleMap::const_iterator triangle = TrianglesOnBoundary.begin(); triangle != TrianglesOnBoundary.end(); triangle++) | 
|---|
| 4480 | result->push_back(triangle->second); | 
|---|
| 4481 | break; | 
|---|
| 4482 | } | 
|---|
| 4483 | default: | 
|---|
| 4484 | DoeLog(0) && (eLog() << Verbose(0) << "Number of wildcards is greater than 3, cannot happen!" << endl); | 
|---|
| 4485 | performCriticalExit(); | 
|---|
| 4486 | break; | 
|---|
| 4487 | } | 
|---|
| 4488 |  | 
|---|
| 4489 | return result; | 
|---|
| 4490 | } | 
|---|
| 4491 |  | 
|---|
| 4492 | struct BoundaryLineSetCompare | 
|---|
| 4493 | { | 
|---|
| 4494 | bool operator()(const BoundaryLineSet * const a, const BoundaryLineSet * const b) | 
|---|
| 4495 | { | 
|---|
| 4496 | int lowerNra = -1; | 
|---|
| 4497 | int lowerNrb = -1; | 
|---|
| 4498 |  | 
|---|
| 4499 | if (a->endpoints[0] < a->endpoints[1]) | 
|---|
| 4500 | lowerNra = 0; | 
|---|
| 4501 | else | 
|---|
| 4502 | lowerNra = 1; | 
|---|
| 4503 |  | 
|---|
| 4504 | if (b->endpoints[0] < b->endpoints[1]) | 
|---|
| 4505 | lowerNrb = 0; | 
|---|
| 4506 | else | 
|---|
| 4507 | lowerNrb = 1; | 
|---|
| 4508 |  | 
|---|
| 4509 | if (a->endpoints[lowerNra] < b->endpoints[lowerNrb]) | 
|---|
| 4510 | return true; | 
|---|
| 4511 | else if (a->endpoints[lowerNra] > b->endpoints[lowerNrb]) | 
|---|
| 4512 | return false; | 
|---|
| 4513 | else { // both lower-numbered endpoints are the same ... | 
|---|
| 4514 | if (a->endpoints[(lowerNra + 1) % 2] < b->endpoints[(lowerNrb + 1) % 2]) | 
|---|
| 4515 | return true; | 
|---|
| 4516 | else if (a->endpoints[(lowerNra + 1) % 2] > b->endpoints[(lowerNrb + 1) % 2]) | 
|---|
| 4517 | return false; | 
|---|
| 4518 | } | 
|---|
| 4519 | return false; | 
|---|
| 4520 | } | 
|---|
| 4521 | ; | 
|---|
| 4522 | }; | 
|---|
| 4523 |  | 
|---|
| 4524 | #define UniqueLines set < class BoundaryLineSet *, BoundaryLineSetCompare> | 
|---|
| 4525 |  | 
|---|
| 4526 | /** | 
|---|
| 4527 | * Finds all degenerated lines within the tesselation structure. | 
|---|
| 4528 | * | 
|---|
| 4529 | * @return map of keys of degenerated line pairs, each line occurs twice | 
|---|
| 4530 | *         in the list, once as key and once as value | 
|---|
| 4531 | */ | 
|---|
| 4532 | IndexToIndex * Tesselation::FindAllDegeneratedLines() | 
|---|
| 4533 | { | 
|---|
| 4534 | Info FunctionInfo(__func__); | 
|---|
| 4535 | UniqueLines AllLines; | 
|---|
| 4536 | IndexToIndex * DegeneratedLines = new IndexToIndex; | 
|---|
| 4537 |  | 
|---|
| 4538 | // sanity check | 
|---|
| 4539 | if (LinesOnBoundary.empty()) { | 
|---|
| 4540 | DoeLog(2) && (eLog() << Verbose(2) << "FindAllDegeneratedTriangles() was called without any tesselation structure."); | 
|---|
| 4541 | return DegeneratedLines; | 
|---|
| 4542 | } | 
|---|
| 4543 | LineMap::iterator LineRunner1; | 
|---|
| 4544 | pair<UniqueLines::iterator, bool> tester; | 
|---|
| 4545 | for (LineRunner1 = LinesOnBoundary.begin(); LineRunner1 != LinesOnBoundary.end(); ++LineRunner1) { | 
|---|
| 4546 | tester = AllLines.insert(LineRunner1->second); | 
|---|
| 4547 | if (!tester.second) { // found degenerated line | 
|---|
| 4548 | DegeneratedLines->insert(pair<int, int> (LineRunner1->second->Nr, (*tester.first)->Nr)); | 
|---|
| 4549 | DegeneratedLines->insert(pair<int, int> ((*tester.first)->Nr, LineRunner1->second->Nr)); | 
|---|
| 4550 | } | 
|---|
| 4551 | } | 
|---|
| 4552 |  | 
|---|
| 4553 | AllLines.clear(); | 
|---|
| 4554 |  | 
|---|
| 4555 | DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedLines() found " << DegeneratedLines->size() << " lines." << endl); | 
|---|
| 4556 | IndexToIndex::iterator it; | 
|---|
| 4557 | for (it = DegeneratedLines->begin(); it != DegeneratedLines->end(); it++) { | 
|---|
| 4558 | const LineMap::const_iterator Line1 = LinesOnBoundary.find((*it).first); | 
|---|
| 4559 | const LineMap::const_iterator Line2 = LinesOnBoundary.find((*it).second); | 
|---|
| 4560 | if (Line1 != LinesOnBoundary.end() && Line2 != LinesOnBoundary.end()) | 
|---|
| 4561 | DoLog(0) && (Log() << Verbose(0) << *Line1->second << " => " << *Line2->second << endl); | 
|---|
| 4562 | else | 
|---|
| 4563 | DoeLog(1) && (eLog() << Verbose(1) << "Either " << (*it).first << " or " << (*it).second << " are not in LinesOnBoundary!" << endl); | 
|---|
| 4564 | } | 
|---|
| 4565 |  | 
|---|
| 4566 | return DegeneratedLines; | 
|---|
| 4567 | } | 
|---|
| 4568 |  | 
|---|
| 4569 | /** | 
|---|
| 4570 | * Finds all degenerated triangles within the tesselation structure. | 
|---|
| 4571 | * | 
|---|
| 4572 | * @return map of keys of degenerated triangle pairs, each triangle occurs twice | 
|---|
| 4573 | *         in the list, once as key and once as value | 
|---|
| 4574 | */ | 
|---|
| 4575 | IndexToIndex * Tesselation::FindAllDegeneratedTriangles() | 
|---|
| 4576 | { | 
|---|
| 4577 | Info FunctionInfo(__func__); | 
|---|
| 4578 | IndexToIndex * DegeneratedLines = FindAllDegeneratedLines(); | 
|---|
| 4579 | IndexToIndex * DegeneratedTriangles = new IndexToIndex; | 
|---|
| 4580 | TriangleMap::iterator TriangleRunner1, TriangleRunner2; | 
|---|
| 4581 | LineMap::iterator Liner; | 
|---|
| 4582 | class BoundaryLineSet *line1 = NULL, *line2 = NULL; | 
|---|
| 4583 |  | 
|---|
| 4584 | for (IndexToIndex::iterator LineRunner = DegeneratedLines->begin(); LineRunner != DegeneratedLines->end(); ++LineRunner) { | 
|---|
| 4585 | // run over both lines' triangles | 
|---|
| 4586 | Liner = LinesOnBoundary.find(LineRunner->first); | 
|---|
| 4587 | if (Liner != LinesOnBoundary.end()) | 
|---|
| 4588 | line1 = Liner->second; | 
|---|
| 4589 | Liner = LinesOnBoundary.find(LineRunner->second); | 
|---|
| 4590 | if (Liner != LinesOnBoundary.end()) | 
|---|
| 4591 | line2 = Liner->second; | 
|---|
| 4592 | for (TriangleRunner1 = line1->triangles.begin(); TriangleRunner1 != line1->triangles.end(); ++TriangleRunner1) { | 
|---|
| 4593 | for (TriangleRunner2 = line2->triangles.begin(); TriangleRunner2 != line2->triangles.end(); ++TriangleRunner2) { | 
|---|
| 4594 | if ((TriangleRunner1->second != TriangleRunner2->second) && (TriangleRunner1->second->IsPresentTupel(TriangleRunner2->second))) { | 
|---|
| 4595 | DegeneratedTriangles->insert(pair<int, int> (TriangleRunner1->second->Nr, TriangleRunner2->second->Nr)); | 
|---|
| 4596 | DegeneratedTriangles->insert(pair<int, int> (TriangleRunner2->second->Nr, TriangleRunner1->second->Nr)); | 
|---|
| 4597 | } | 
|---|
| 4598 | } | 
|---|
| 4599 | } | 
|---|
| 4600 | } | 
|---|
| 4601 | delete (DegeneratedLines); | 
|---|
| 4602 |  | 
|---|
| 4603 | DoLog(0) && (Log() << Verbose(0) << "FindAllDegeneratedTriangles() found " << DegeneratedTriangles->size() << " triangles:" << endl); | 
|---|
| 4604 | IndexToIndex::iterator it; | 
|---|
| 4605 | for (it = DegeneratedTriangles->begin(); it != DegeneratedTriangles->end(); it++) | 
|---|
| 4606 | DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl); | 
|---|
| 4607 |  | 
|---|
| 4608 | return DegeneratedTriangles; | 
|---|
| 4609 | } | 
|---|
| 4610 |  | 
|---|
| 4611 | /** | 
|---|
| 4612 | * Purges degenerated triangles from the tesselation structure if they are not | 
|---|
| 4613 | * necessary to keep a single point within the structure. | 
|---|
| 4614 | */ | 
|---|
| 4615 | void Tesselation::RemoveDegeneratedTriangles() | 
|---|
| 4616 | { | 
|---|
| 4617 | Info FunctionInfo(__func__); | 
|---|
| 4618 | IndexToIndex * DegeneratedTriangles = FindAllDegeneratedTriangles(); | 
|---|
| 4619 | TriangleMap::iterator finder; | 
|---|
| 4620 | BoundaryTriangleSet *triangle = NULL, *partnerTriangle = NULL; | 
|---|
| 4621 | int count = 0; | 
|---|
| 4622 |  | 
|---|
| 4623 | for (IndexToIndex::iterator TriangleKeyRunner = DegeneratedTriangles->begin(); TriangleKeyRunner != DegeneratedTriangles->end(); ++TriangleKeyRunner) { | 
|---|
| 4624 | finder = TrianglesOnBoundary.find(TriangleKeyRunner->first); | 
|---|
| 4625 | if (finder != TrianglesOnBoundary.end()) | 
|---|
| 4626 | triangle = finder->second; | 
|---|
| 4627 | else | 
|---|
| 4628 | break; | 
|---|
| 4629 | finder = TrianglesOnBoundary.find(TriangleKeyRunner->second); | 
|---|
| 4630 | if (finder != TrianglesOnBoundary.end()) | 
|---|
| 4631 | partnerTriangle = finder->second; | 
|---|
| 4632 | else | 
|---|
| 4633 | break; | 
|---|
| 4634 |  | 
|---|
| 4635 | bool trianglesShareLine = false; | 
|---|
| 4636 | for (int i = 0; i < 3; ++i) | 
|---|
| 4637 | for (int j = 0; j < 3; ++j) | 
|---|
| 4638 | trianglesShareLine = trianglesShareLine || triangle->lines[i] == partnerTriangle->lines[j]; | 
|---|
| 4639 |  | 
|---|
| 4640 | if (trianglesShareLine && (triangle->endpoints[1]->LinesCount > 2) && (triangle->endpoints[2]->LinesCount > 2) && (triangle->endpoints[0]->LinesCount > 2)) { | 
|---|
| 4641 | // check whether we have to fix lines | 
|---|
| 4642 | BoundaryTriangleSet *Othertriangle = NULL; | 
|---|
| 4643 | BoundaryTriangleSet *OtherpartnerTriangle = NULL; | 
|---|
| 4644 | TriangleMap::iterator TriangleRunner; | 
|---|
| 4645 | for (int i = 0; i < 3; ++i) | 
|---|
| 4646 | for (int j = 0; j < 3; ++j) | 
|---|
| 4647 | if (triangle->lines[i] != partnerTriangle->lines[j]) { | 
|---|
| 4648 | // get the other two triangles | 
|---|
| 4649 | for (TriangleRunner = triangle->lines[i]->triangles.begin(); TriangleRunner != triangle->lines[i]->triangles.end(); ++TriangleRunner) | 
|---|
| 4650 | if (TriangleRunner->second != triangle) { | 
|---|
| 4651 | Othertriangle = TriangleRunner->second; | 
|---|
| 4652 | } | 
|---|
| 4653 | for (TriangleRunner = partnerTriangle->lines[i]->triangles.begin(); TriangleRunner != partnerTriangle->lines[i]->triangles.end(); ++TriangleRunner) | 
|---|
| 4654 | if (TriangleRunner->second != partnerTriangle) { | 
|---|
| 4655 | OtherpartnerTriangle = TriangleRunner->second; | 
|---|
| 4656 | } | 
|---|
| 4657 | /// interchanges their lines so that triangle->lines[i] == partnerTriangle->lines[j] | 
|---|
| 4658 | // the line of triangle receives the degenerated ones | 
|---|
| 4659 | triangle->lines[i]->triangles.erase(Othertriangle->Nr); | 
|---|
| 4660 | triangle->lines[i]->triangles.insert(TrianglePair(partnerTriangle->Nr, partnerTriangle)); | 
|---|
| 4661 | for (int k = 0; k < 3; k++) | 
|---|
| 4662 | if (triangle->lines[i] == Othertriangle->lines[k]) { | 
|---|
| 4663 | Othertriangle->lines[k] = partnerTriangle->lines[j]; | 
|---|
| 4664 | break; | 
|---|
| 4665 | } | 
|---|
| 4666 | // the line of partnerTriangle receives the non-degenerated ones | 
|---|
| 4667 | partnerTriangle->lines[j]->triangles.erase(partnerTriangle->Nr); | 
|---|
| 4668 | partnerTriangle->lines[j]->triangles.insert(TrianglePair(Othertriangle->Nr, Othertriangle)); | 
|---|
| 4669 | partnerTriangle->lines[j] = triangle->lines[i]; | 
|---|
| 4670 | } | 
|---|
| 4671 |  | 
|---|
| 4672 | // erase the pair | 
|---|
| 4673 | count += (int) DegeneratedTriangles->erase(triangle->Nr); | 
|---|
| 4674 | DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *triangle << "." << endl); | 
|---|
| 4675 | RemoveTesselationTriangle(triangle); | 
|---|
| 4676 | count += (int) DegeneratedTriangles->erase(partnerTriangle->Nr); | 
|---|
| 4677 | DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removes triangle " << *partnerTriangle << "." << endl); | 
|---|
| 4678 | RemoveTesselationTriangle(partnerTriangle); | 
|---|
| 4679 | } else { | 
|---|
| 4680 | DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() does not remove triangle " << *triangle << " and its partner " << *partnerTriangle << " because it is essential for at" << " least one of the endpoints to be kept in the tesselation structure." << endl); | 
|---|
| 4681 | } | 
|---|
| 4682 | } | 
|---|
| 4683 | delete (DegeneratedTriangles); | 
|---|
| 4684 | if (count > 0) | 
|---|
| 4685 | LastTriangle = NULL; | 
|---|
| 4686 |  | 
|---|
| 4687 | DoLog(0) && (Log() << Verbose(0) << "RemoveDegeneratedTriangles() removed " << count << " triangles:" << endl); | 
|---|
| 4688 | } | 
|---|
| 4689 |  | 
|---|
| 4690 | /** Adds an outside Tesselpoint to the envelope via (two) degenerated triangles. | 
|---|
| 4691 | * We look for the closest point on the boundary, we look through its connected boundary lines and | 
|---|
| 4692 | * seek the one with the minimum angle between its center point and the new point and this base line. | 
|---|
| 4693 | * We open up the line by adding a degenerated triangle, whose other side closes the base line again. | 
|---|
| 4694 | * \param *out output stream for debugging | 
|---|
| 4695 | * \param *point point to add | 
|---|
| 4696 | * \param *LC Linked Cell structure to find nearest point | 
|---|
| 4697 | */ | 
|---|
| 4698 | void Tesselation::AddBoundaryPointByDegeneratedTriangle(class TesselPoint *point, LinkedCell *LC) | 
|---|
| 4699 | { | 
|---|
| 4700 | Info FunctionInfo(__func__); | 
|---|
| 4701 | // find nearest boundary point | 
|---|
| 4702 | class TesselPoint *BackupPoint = NULL; | 
|---|
| 4703 | class TesselPoint *NearestPoint = FindClosestTesselPoint(point->node, BackupPoint, LC); | 
|---|
| 4704 | class BoundaryPointSet *NearestBoundaryPoint = NULL; | 
|---|
| 4705 | PointMap::iterator PointRunner; | 
|---|
| 4706 |  | 
|---|
| 4707 | if (NearestPoint == point) | 
|---|
| 4708 | NearestPoint = BackupPoint; | 
|---|
| 4709 | PointRunner = PointsOnBoundary.find(NearestPoint->nr); | 
|---|
| 4710 | if (PointRunner != PointsOnBoundary.end()) { | 
|---|
| 4711 | NearestBoundaryPoint = PointRunner->second; | 
|---|
| 4712 | } else { | 
|---|
| 4713 | DoeLog(1) && (eLog() << Verbose(1) << "I cannot find the boundary point." << endl); | 
|---|
| 4714 | return; | 
|---|
| 4715 | } | 
|---|
| 4716 | DoLog(0) && (Log() << Verbose(0) << "Nearest point on boundary is " << NearestPoint->Name << "." << endl); | 
|---|
| 4717 |  | 
|---|
| 4718 | // go through its lines and find the best one to split | 
|---|
| 4719 | Vector CenterToPoint; | 
|---|
| 4720 | Vector BaseLine; | 
|---|
| 4721 | double angle, BestAngle = 0.; | 
|---|
| 4722 | class BoundaryLineSet *BestLine = NULL; | 
|---|
| 4723 | for (LineMap::iterator Runner = NearestBoundaryPoint->lines.begin(); Runner != NearestBoundaryPoint->lines.end(); Runner++) { | 
|---|
| 4724 | BaseLine = (*Runner->second->endpoints[0]->node->node) - | 
|---|
| 4725 | (*Runner->second->endpoints[1]->node->node); | 
|---|
| 4726 | CenterToPoint = 0.5 * ((*Runner->second->endpoints[0]->node->node) + | 
|---|
| 4727 | (*Runner->second->endpoints[1]->node->node)); | 
|---|
| 4728 | CenterToPoint -= (*point->node); | 
|---|
| 4729 | angle = CenterToPoint.Angle(BaseLine); | 
|---|
| 4730 | if (fabs(angle - M_PI/2.) < fabs(BestAngle - M_PI/2.)) { | 
|---|
| 4731 | BestAngle = angle; | 
|---|
| 4732 | BestLine = Runner->second; | 
|---|
| 4733 | } | 
|---|
| 4734 | } | 
|---|
| 4735 |  | 
|---|
| 4736 | // remove one triangle from the chosen line | 
|---|
| 4737 | class BoundaryTriangleSet *TempTriangle = (BestLine->triangles.begin())->second; | 
|---|
| 4738 | BestLine->triangles.erase(TempTriangle->Nr); | 
|---|
| 4739 | int nr = -1; | 
|---|
| 4740 | for (int i = 0; i < 3; i++) { | 
|---|
| 4741 | if (TempTriangle->lines[i] == BestLine) { | 
|---|
| 4742 | nr = i; | 
|---|
| 4743 | break; | 
|---|
| 4744 | } | 
|---|
| 4745 | } | 
|---|
| 4746 |  | 
|---|
| 4747 | // create new triangle to connect point (connects automatically with the missing spot of the chosen line) | 
|---|
| 4748 | DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl); | 
|---|
| 4749 | AddTesselationPoint((BestLine->endpoints[0]->node), 0); | 
|---|
| 4750 | AddTesselationPoint((BestLine->endpoints[1]->node), 1); | 
|---|
| 4751 | AddTesselationPoint(point, 2); | 
|---|
| 4752 | DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl); | 
|---|
| 4753 | AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0); | 
|---|
| 4754 | AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1); | 
|---|
| 4755 | AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2); | 
|---|
| 4756 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 4757 | BTS->GetNormalVector(TempTriangle->NormalVector); | 
|---|
| 4758 | BTS->NormalVector.Scale(-1.); | 
|---|
| 4759 | DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of new triangle is " << BTS->NormalVector << "." << endl); | 
|---|
| 4760 | AddTesselationTriangle(); | 
|---|
| 4761 |  | 
|---|
| 4762 | // create other side of this triangle and close both new sides of the first created triangle | 
|---|
| 4763 | DoLog(2) && (Log() << Verbose(2) << "Adding new triangle points." << endl); | 
|---|
| 4764 | AddTesselationPoint((BestLine->endpoints[0]->node), 0); | 
|---|
| 4765 | AddTesselationPoint((BestLine->endpoints[1]->node), 1); | 
|---|
| 4766 | AddTesselationPoint(point, 2); | 
|---|
| 4767 | DoLog(2) && (Log() << Verbose(2) << "Adding new triangle lines." << endl); | 
|---|
| 4768 | AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0); | 
|---|
| 4769 | AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1); | 
|---|
| 4770 | AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2); | 
|---|
| 4771 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 4772 | BTS->GetNormalVector(TempTriangle->NormalVector); | 
|---|
| 4773 | DoLog(1) && (Log() << Verbose(1) << "INFO: NormalVector of other new triangle is " << BTS->NormalVector << "." << endl); | 
|---|
| 4774 | AddTesselationTriangle(); | 
|---|
| 4775 |  | 
|---|
| 4776 | // add removed triangle to the last open line of the second triangle | 
|---|
| 4777 | for (int i = 0; i < 3; i++) { // look for the same line as BestLine (only it's its degenerated companion) | 
|---|
| 4778 | if ((BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[0])) && (BTS->lines[i]->ContainsBoundaryPoint(BestLine->endpoints[1]))) { | 
|---|
| 4779 | if (BestLine == BTS->lines[i]) { | 
|---|
| 4780 | DoeLog(0) && (eLog() << Verbose(0) << "BestLine is same as found line, something's wrong here!" << endl); | 
|---|
| 4781 | performCriticalExit(); | 
|---|
| 4782 | } | 
|---|
| 4783 | BTS->lines[i]->triangles.insert(pair<int, class BoundaryTriangleSet *> (TempTriangle->Nr, TempTriangle)); | 
|---|
| 4784 | TempTriangle->lines[nr] = BTS->lines[i]; | 
|---|
| 4785 | break; | 
|---|
| 4786 | } | 
|---|
| 4787 | } | 
|---|
| 4788 | } | 
|---|
| 4789 | ; | 
|---|
| 4790 |  | 
|---|
| 4791 | /** Writes the envelope to file. | 
|---|
| 4792 | * \param *out otuput stream for debugging | 
|---|
| 4793 | * \param *filename basename of output file | 
|---|
| 4794 | * \param *cloud PointCloud structure with all nodes | 
|---|
| 4795 | */ | 
|---|
| 4796 | void Tesselation::Output(const char *filename, const PointCloud * const cloud) | 
|---|
| 4797 | { | 
|---|
| 4798 | Info FunctionInfo(__func__); | 
|---|
| 4799 | ofstream *tempstream = NULL; | 
|---|
| 4800 | string NameofTempFile; | 
|---|
| 4801 | char NumberName[255]; | 
|---|
| 4802 |  | 
|---|
| 4803 | if (LastTriangle != NULL) { | 
|---|
| 4804 | sprintf(NumberName, "-%04d-%s_%s_%s", (int) TrianglesOnBoundary.size(), LastTriangle->endpoints[0]->node->Name, LastTriangle->endpoints[1]->node->Name, LastTriangle->endpoints[2]->node->Name); | 
|---|
| 4805 | if (DoTecplotOutput) { | 
|---|
| 4806 | string NameofTempFile(filename); | 
|---|
| 4807 | NameofTempFile.append(NumberName); | 
|---|
| 4808 | for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos)) | 
|---|
| 4809 | NameofTempFile.erase(npos, 1); | 
|---|
| 4810 | NameofTempFile.append(TecplotSuffix); | 
|---|
| 4811 | DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n"); | 
|---|
| 4812 | tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc); | 
|---|
| 4813 | WriteTecplotFile(tempstream, this, cloud, TriangleFilesWritten); | 
|---|
| 4814 | tempstream->close(); | 
|---|
| 4815 | tempstream->flush(); | 
|---|
| 4816 | delete (tempstream); | 
|---|
| 4817 | } | 
|---|
| 4818 |  | 
|---|
| 4819 | if (DoRaster3DOutput) { | 
|---|
| 4820 | string NameofTempFile(filename); | 
|---|
| 4821 | NameofTempFile.append(NumberName); | 
|---|
| 4822 | for (size_t npos = NameofTempFile.find_first_of(' '); npos != string::npos; npos = NameofTempFile.find(' ', npos)) | 
|---|
| 4823 | NameofTempFile.erase(npos, 1); | 
|---|
| 4824 | NameofTempFile.append(Raster3DSuffix); | 
|---|
| 4825 | DoLog(0) && (Log() << Verbose(0) << "Writing temporary non convex hull to file " << NameofTempFile << ".\n"); | 
|---|
| 4826 | tempstream = new ofstream(NameofTempFile.c_str(), ios::trunc); | 
|---|
| 4827 | WriteRaster3dFile(tempstream, this, cloud); | 
|---|
| 4828 | IncludeSphereinRaster3D(tempstream, this, cloud); | 
|---|
| 4829 | tempstream->close(); | 
|---|
| 4830 | tempstream->flush(); | 
|---|
| 4831 | delete (tempstream); | 
|---|
| 4832 | } | 
|---|
| 4833 | } | 
|---|
| 4834 | if (DoTecplotOutput || DoRaster3DOutput) | 
|---|
| 4835 | TriangleFilesWritten++; | 
|---|
| 4836 | } | 
|---|
| 4837 | ; | 
|---|
| 4838 |  | 
|---|
| 4839 | struct BoundaryPolygonSetCompare | 
|---|
| 4840 | { | 
|---|
| 4841 | bool operator()(const BoundaryPolygonSet * s1, const BoundaryPolygonSet * s2) const | 
|---|
| 4842 | { | 
|---|
| 4843 | if (s1->endpoints.size() < s2->endpoints.size()) | 
|---|
| 4844 | return true; | 
|---|
| 4845 | else if (s1->endpoints.size() > s2->endpoints.size()) | 
|---|
| 4846 | return false; | 
|---|
| 4847 | else { // equality of number of endpoints | 
|---|
| 4848 | PointSet::const_iterator Walker1 = s1->endpoints.begin(); | 
|---|
| 4849 | PointSet::const_iterator Walker2 = s2->endpoints.begin(); | 
|---|
| 4850 | while ((Walker1 != s1->endpoints.end()) || (Walker2 != s2->endpoints.end())) { | 
|---|
| 4851 | if ((*Walker1)->Nr < (*Walker2)->Nr) | 
|---|
| 4852 | return true; | 
|---|
| 4853 | else if ((*Walker1)->Nr > (*Walker2)->Nr) | 
|---|
| 4854 | return false; | 
|---|
| 4855 | Walker1++; | 
|---|
| 4856 | Walker2++; | 
|---|
| 4857 | } | 
|---|
| 4858 | return false; | 
|---|
| 4859 | } | 
|---|
| 4860 | } | 
|---|
| 4861 | }; | 
|---|
| 4862 |  | 
|---|
| 4863 | #define UniquePolygonSet set < BoundaryPolygonSet *, BoundaryPolygonSetCompare> | 
|---|
| 4864 |  | 
|---|
| 4865 | /** Finds all degenerated polygons and calls ReTesselateDegeneratedPolygon()/ | 
|---|
| 4866 | * \return number of polygons found | 
|---|
| 4867 | */ | 
|---|
| 4868 | int Tesselation::CorrectAllDegeneratedPolygons() | 
|---|
| 4869 | { | 
|---|
| 4870 | Info FunctionInfo(__func__); | 
|---|
| 4871 | /// 2. Go through all BoundaryPointSet's, check their triangles' NormalVector | 
|---|
| 4872 | IndexToIndex *DegeneratedTriangles = FindAllDegeneratedTriangles(); | 
|---|
| 4873 | set<BoundaryPointSet *> EndpointCandidateList; | 
|---|
| 4874 | pair<set<BoundaryPointSet *>::iterator, bool> InsertionTester; | 
|---|
| 4875 | pair<map<int, Vector *>::iterator, bool> TriangleInsertionTester; | 
|---|
| 4876 | for (PointMap::const_iterator Runner = PointsOnBoundary.begin(); Runner != PointsOnBoundary.end(); Runner++) { | 
|---|
| 4877 | DoLog(0) && (Log() << Verbose(0) << "Current point is " << *Runner->second << "." << endl); | 
|---|
| 4878 | map<int, Vector *> TriangleVectors; | 
|---|
| 4879 | // gather all NormalVectors | 
|---|
| 4880 | DoLog(1) && (Log() << Verbose(1) << "Gathering triangles ..." << endl); | 
|---|
| 4881 | for (LineMap::const_iterator LineRunner = (Runner->second)->lines.begin(); LineRunner != (Runner->second)->lines.end(); LineRunner++) | 
|---|
| 4882 | for (TriangleMap::const_iterator TriangleRunner = (LineRunner->second)->triangles.begin(); TriangleRunner != (LineRunner->second)->triangles.end(); TriangleRunner++) { | 
|---|
| 4883 | if (DegeneratedTriangles->find(TriangleRunner->second->Nr) == DegeneratedTriangles->end()) { | 
|---|
| 4884 | TriangleInsertionTester = TriangleVectors.insert(pair<int, Vector *> ((TriangleRunner->second)->Nr, &((TriangleRunner->second)->NormalVector))); | 
|---|
| 4885 | if (TriangleInsertionTester.second) | 
|---|
| 4886 | DoLog(1) && (Log() << Verbose(1) << " Adding triangle " << *(TriangleRunner->second) << " to triangles to check-list." << endl); | 
|---|
| 4887 | } else { | 
|---|
| 4888 | DoLog(1) && (Log() << Verbose(1) << " NOT adding triangle " << *(TriangleRunner->second) << " as it's a simply degenerated one." << endl); | 
|---|
| 4889 | } | 
|---|
| 4890 | } | 
|---|
| 4891 | // check whether there are two that are parallel | 
|---|
| 4892 | DoLog(1) && (Log() << Verbose(1) << "Finding two parallel triangles ..." << endl); | 
|---|
| 4893 | for (map<int, Vector *>::iterator VectorWalker = TriangleVectors.begin(); VectorWalker != TriangleVectors.end(); VectorWalker++) | 
|---|
| 4894 | for (map<int, Vector *>::iterator VectorRunner = VectorWalker; VectorRunner != TriangleVectors.end(); VectorRunner++) | 
|---|
| 4895 | if (VectorWalker != VectorRunner) { // skip equals | 
|---|
| 4896 | const double SCP = VectorWalker->second->ScalarProduct(*VectorRunner->second); // ScalarProduct should result in -1. for degenerated triangles | 
|---|
| 4897 | DoLog(1) && (Log() << Verbose(1) << "Checking " << *VectorWalker->second << " against " << *VectorRunner->second << ": " << SCP << endl); | 
|---|
| 4898 | if (fabs(SCP + 1.) < ParallelEpsilon) { | 
|---|
| 4899 | InsertionTester = EndpointCandidateList.insert((Runner->second)); | 
|---|
| 4900 | if (InsertionTester.second) | 
|---|
| 4901 | DoLog(0) && (Log() << Verbose(0) << " Adding " << *Runner->second << " to endpoint candidate list." << endl); | 
|---|
| 4902 | // and break out of both loops | 
|---|
| 4903 | VectorWalker = TriangleVectors.end(); | 
|---|
| 4904 | VectorRunner = TriangleVectors.end(); | 
|---|
| 4905 | break; | 
|---|
| 4906 | } | 
|---|
| 4907 | } | 
|---|
| 4908 | } | 
|---|
| 4909 | delete DegeneratedTriangles; | 
|---|
| 4910 |  | 
|---|
| 4911 | /// 3. Find connected endpoint candidates and put them into a polygon | 
|---|
| 4912 | UniquePolygonSet ListofDegeneratedPolygons; | 
|---|
| 4913 | BoundaryPointSet *Walker = NULL; | 
|---|
| 4914 | BoundaryPointSet *OtherWalker = NULL; | 
|---|
| 4915 | BoundaryPolygonSet *Current = NULL; | 
|---|
| 4916 | stack<BoundaryPointSet*> ToCheckConnecteds; | 
|---|
| 4917 | while (!EndpointCandidateList.empty()) { | 
|---|
| 4918 | Walker = *(EndpointCandidateList.begin()); | 
|---|
| 4919 | if (Current == NULL) { // create a new polygon with current candidate | 
|---|
| 4920 | DoLog(0) && (Log() << Verbose(0) << "Starting new polygon set at point " << *Walker << endl); | 
|---|
| 4921 | Current = new BoundaryPolygonSet; | 
|---|
| 4922 | Current->endpoints.insert(Walker); | 
|---|
| 4923 | EndpointCandidateList.erase(Walker); | 
|---|
| 4924 | ToCheckConnecteds.push(Walker); | 
|---|
| 4925 | } | 
|---|
| 4926 |  | 
|---|
| 4927 | // go through to-check stack | 
|---|
| 4928 | while (!ToCheckConnecteds.empty()) { | 
|---|
| 4929 | Walker = ToCheckConnecteds.top(); // fetch ... | 
|---|
| 4930 | ToCheckConnecteds.pop(); // ... and remove | 
|---|
| 4931 | for (LineMap::const_iterator LineWalker = Walker->lines.begin(); LineWalker != Walker->lines.end(); LineWalker++) { | 
|---|
| 4932 | OtherWalker = (LineWalker->second)->GetOtherEndpoint(Walker); | 
|---|
| 4933 | DoLog(1) && (Log() << Verbose(1) << "Checking " << *OtherWalker << endl); | 
|---|
| 4934 | set<BoundaryPointSet *>::iterator Finder = EndpointCandidateList.find(OtherWalker); | 
|---|
| 4935 | if (Finder != EndpointCandidateList.end()) { // found a connected partner | 
|---|
| 4936 | DoLog(1) && (Log() << Verbose(1) << " Adding to polygon." << endl); | 
|---|
| 4937 | Current->endpoints.insert(OtherWalker); | 
|---|
| 4938 | EndpointCandidateList.erase(Finder); // remove from candidates | 
|---|
| 4939 | ToCheckConnecteds.push(OtherWalker); // but check its partners too | 
|---|
| 4940 | } else { | 
|---|
| 4941 | DoLog(1) && (Log() << Verbose(1) << " is not connected to " << *Walker << endl); | 
|---|
| 4942 | } | 
|---|
| 4943 | } | 
|---|
| 4944 | } | 
|---|
| 4945 |  | 
|---|
| 4946 | DoLog(0) && (Log() << Verbose(0) << "Final polygon is " << *Current << endl); | 
|---|
| 4947 | ListofDegeneratedPolygons.insert(Current); | 
|---|
| 4948 | Current = NULL; | 
|---|
| 4949 | } | 
|---|
| 4950 |  | 
|---|
| 4951 | const int counter = ListofDegeneratedPolygons.size(); | 
|---|
| 4952 |  | 
|---|
| 4953 | DoLog(0) && (Log() << Verbose(0) << "The following " << counter << " degenerated polygons have been found: " << endl); | 
|---|
| 4954 | for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) | 
|---|
| 4955 | DoLog(0) && (Log() << Verbose(0) << " " << **PolygonRunner << endl); | 
|---|
| 4956 |  | 
|---|
| 4957 | /// 4. Go through all these degenerated polygons | 
|---|
| 4958 | for (UniquePolygonSet::iterator PolygonRunner = ListofDegeneratedPolygons.begin(); PolygonRunner != ListofDegeneratedPolygons.end(); PolygonRunner++) { | 
|---|
| 4959 | stack<int> TriangleNrs; | 
|---|
| 4960 | Vector NormalVector; | 
|---|
| 4961 | /// 4a. Gather all triangles of this polygon | 
|---|
| 4962 | TriangleSet *T = (*PolygonRunner)->GetAllContainedTrianglesFromEndpoints(); | 
|---|
| 4963 |  | 
|---|
| 4964 | // check whether number is bigger than 2, otherwise it's just a simply degenerated one and nothing to do. | 
|---|
| 4965 | if (T->size() == 2) { | 
|---|
| 4966 | DoLog(1) && (Log() << Verbose(1) << " Skipping degenerated polygon, is just a (already simply degenerated) triangle." << endl); | 
|---|
| 4967 | delete (T); | 
|---|
| 4968 | continue; | 
|---|
| 4969 | } | 
|---|
| 4970 |  | 
|---|
| 4971 | // check whether number is even | 
|---|
| 4972 | // If this case occurs, we have to think about it! | 
|---|
| 4973 | // The Problem is probably due to two degenerated polygons being connected by a bridging, non-degenerated polygon, as somehow one node has | 
|---|
| 4974 | // connections to either polygon ... | 
|---|
| 4975 | if (T->size() % 2 != 0) { | 
|---|
| 4976 | DoeLog(0) && (eLog() << Verbose(0) << " degenerated polygon contains an odd number of triangles, probably contains bridging non-degenerated ones, too!" << endl); | 
|---|
| 4977 | performCriticalExit(); | 
|---|
| 4978 | } | 
|---|
| 4979 | TriangleSet::iterator TriangleWalker = T->begin(); // is the inner iterator | 
|---|
| 4980 | /// 4a. Get NormalVector for one side (this is "front") | 
|---|
| 4981 | NormalVector = (*TriangleWalker)->NormalVector; | 
|---|
| 4982 | DoLog(1) && (Log() << Verbose(1) << "\"front\" defining triangle is " << **TriangleWalker << " and Normal vector of \"front\" side is " << NormalVector << endl); | 
|---|
| 4983 | TriangleWalker++; | 
|---|
| 4984 | TriangleSet::iterator TriangleSprinter = TriangleWalker; // is the inner advanced iterator | 
|---|
| 4985 | /// 4b. Remove all triangles whose NormalVector is in opposite direction (i.e. "back") | 
|---|
| 4986 | BoundaryTriangleSet *triangle = NULL; | 
|---|
| 4987 | while (TriangleSprinter != T->end()) { | 
|---|
| 4988 | TriangleWalker = TriangleSprinter; | 
|---|
| 4989 | triangle = *TriangleWalker; | 
|---|
| 4990 | TriangleSprinter++; | 
|---|
| 4991 | DoLog(1) && (Log() << Verbose(1) << "Current triangle to test for removal: " << *triangle << endl); | 
|---|
| 4992 | if (triangle->NormalVector.ScalarProduct(NormalVector) < 0) { // if from other side, then delete and remove from list | 
|---|
| 4993 | DoLog(1) && (Log() << Verbose(1) << " Removing ... " << endl); | 
|---|
| 4994 | TriangleNrs.push(triangle->Nr); | 
|---|
| 4995 | T->erase(TriangleWalker); | 
|---|
| 4996 | RemoveTesselationTriangle(triangle); | 
|---|
| 4997 | } else | 
|---|
| 4998 | DoLog(1) && (Log() << Verbose(1) << " Keeping ... " << endl); | 
|---|
| 4999 | } | 
|---|
| 5000 | /// 4c. Copy all "front" triangles but with inverse NormalVector | 
|---|
| 5001 | TriangleWalker = T->begin(); | 
|---|
| 5002 | while (TriangleWalker != T->end()) { // go through all front triangles | 
|---|
| 5003 | DoLog(1) && (Log() << Verbose(1) << " Re-creating triangle " << **TriangleWalker << " with NormalVector " << (*TriangleWalker)->NormalVector << endl); | 
|---|
| 5004 | for (int i = 0; i < 3; i++) | 
|---|
| 5005 | AddTesselationPoint((*TriangleWalker)->endpoints[i]->node, i); | 
|---|
| 5006 | AddTesselationLine(NULL, NULL, TPS[0], TPS[1], 0); | 
|---|
| 5007 | AddTesselationLine(NULL, NULL, TPS[0], TPS[2], 1); | 
|---|
| 5008 | AddTesselationLine(NULL, NULL, TPS[1], TPS[2], 2); | 
|---|
| 5009 | if (TriangleNrs.empty()) | 
|---|
| 5010 | DoeLog(0) && (eLog() << Verbose(0) << "No more free triangle numbers!" << endl); | 
|---|
| 5011 | BTS = new BoundaryTriangleSet(BLS, TriangleNrs.top()); // copy triangle ... | 
|---|
| 5012 | AddTesselationTriangle(); // ... and add | 
|---|
| 5013 | TriangleNrs.pop(); | 
|---|
| 5014 | BTS->NormalVector = -1 * (*TriangleWalker)->NormalVector; | 
|---|
| 5015 | TriangleWalker++; | 
|---|
| 5016 | } | 
|---|
| 5017 | if (!TriangleNrs.empty()) { | 
|---|
| 5018 | DoeLog(0) && (eLog() << Verbose(0) << "There have been less triangles created than removed!" << endl); | 
|---|
| 5019 | } | 
|---|
| 5020 | delete (T); // remove the triangleset | 
|---|
| 5021 | } | 
|---|
| 5022 | IndexToIndex * SimplyDegeneratedTriangles = FindAllDegeneratedTriangles(); | 
|---|
| 5023 | DoLog(0) && (Log() << Verbose(0) << "Final list of simply degenerated triangles found, containing " << SimplyDegeneratedTriangles->size() << " triangles:" << endl); | 
|---|
| 5024 | IndexToIndex::iterator it; | 
|---|
| 5025 | for (it = SimplyDegeneratedTriangles->begin(); it != SimplyDegeneratedTriangles->end(); it++) | 
|---|
| 5026 | DoLog(0) && (Log() << Verbose(0) << (*it).first << " => " << (*it).second << endl); | 
|---|
| 5027 | delete (SimplyDegeneratedTriangles); | 
|---|
| 5028 | /// 5. exit | 
|---|
| 5029 | UniquePolygonSet::iterator PolygonRunner; | 
|---|
| 5030 | while (!ListofDegeneratedPolygons.empty()) { | 
|---|
| 5031 | PolygonRunner = ListofDegeneratedPolygons.begin(); | 
|---|
| 5032 | delete (*PolygonRunner); | 
|---|
| 5033 | ListofDegeneratedPolygons.erase(PolygonRunner); | 
|---|
| 5034 | } | 
|---|
| 5035 |  | 
|---|
| 5036 | return counter; | 
|---|
| 5037 | } | 
|---|
| 5038 | ; | 
|---|