| 1 | /* | 
|---|
| 2 | * tesselation.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Aug 3, 2009 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "tesselation.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | // ======================================== Points on Boundary ================================= | 
|---|
| 11 |  | 
|---|
| 12 | BoundaryPointSet::BoundaryPointSet() | 
|---|
| 13 | { | 
|---|
| 14 | LinesCount = 0; | 
|---|
| 15 | Nr = -1; | 
|---|
| 16 | } | 
|---|
| 17 | ; | 
|---|
| 18 |  | 
|---|
| 19 | BoundaryPointSet::BoundaryPointSet(TesselPoint *Walker) | 
|---|
| 20 | { | 
|---|
| 21 | node = Walker; | 
|---|
| 22 | LinesCount = 0; | 
|---|
| 23 | Nr = Walker->nr; | 
|---|
| 24 | } | 
|---|
| 25 | ; | 
|---|
| 26 |  | 
|---|
| 27 | BoundaryPointSet::~BoundaryPointSet() | 
|---|
| 28 | { | 
|---|
| 29 | cout << Verbose(5) << "Erasing point nr. " << Nr << "." << endl; | 
|---|
| 30 | if (!lines.empty()) | 
|---|
| 31 | cerr << "WARNING: Memory Leak! I " << *this << " am still connected to some lines." << endl; | 
|---|
| 32 | node = NULL; | 
|---|
| 33 | } | 
|---|
| 34 | ; | 
|---|
| 35 |  | 
|---|
| 36 | void BoundaryPointSet::AddLine(class BoundaryLineSet *line) | 
|---|
| 37 | { | 
|---|
| 38 | cout << Verbose(6) << "Adding " << *this << " to line " << *line << "." | 
|---|
| 39 | << endl; | 
|---|
| 40 | if (line->endpoints[0] == this) | 
|---|
| 41 | { | 
|---|
| 42 | lines.insert(LinePair(line->endpoints[1]->Nr, line)); | 
|---|
| 43 | } | 
|---|
| 44 | else | 
|---|
| 45 | { | 
|---|
| 46 | lines.insert(LinePair(line->endpoints[0]->Nr, line)); | 
|---|
| 47 | } | 
|---|
| 48 | LinesCount++; | 
|---|
| 49 | } | 
|---|
| 50 | ; | 
|---|
| 51 |  | 
|---|
| 52 | ostream & | 
|---|
| 53 | operator <<(ostream &ost, BoundaryPointSet &a) | 
|---|
| 54 | { | 
|---|
| 55 | ost << "[" << a.Nr << "|" << a.node->Name << "]"; | 
|---|
| 56 | return ost; | 
|---|
| 57 | } | 
|---|
| 58 | ; | 
|---|
| 59 |  | 
|---|
| 60 | // ======================================== Lines on Boundary ================================= | 
|---|
| 61 |  | 
|---|
| 62 | BoundaryLineSet::BoundaryLineSet() | 
|---|
| 63 | { | 
|---|
| 64 | for (int i = 0; i < 2; i++) | 
|---|
| 65 | endpoints[i] = NULL; | 
|---|
| 66 | TrianglesCount = 0; | 
|---|
| 67 | Nr = -1; | 
|---|
| 68 | } | 
|---|
| 69 | ; | 
|---|
| 70 |  | 
|---|
| 71 | BoundaryLineSet::BoundaryLineSet(class BoundaryPointSet *Point[2], int number) | 
|---|
| 72 | { | 
|---|
| 73 | // set number | 
|---|
| 74 | Nr = number; | 
|---|
| 75 | // set endpoints in ascending order | 
|---|
| 76 | SetEndpointsOrdered(endpoints, Point[0], Point[1]); | 
|---|
| 77 | // add this line to the hash maps of both endpoints | 
|---|
| 78 | Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding. | 
|---|
| 79 | Point[1]->AddLine(this); // | 
|---|
| 80 | // clear triangles list | 
|---|
| 81 | TrianglesCount = 0; | 
|---|
| 82 | cout << Verbose(5) << "New Line with endpoints " << *this << "." << endl; | 
|---|
| 83 | } | 
|---|
| 84 | ; | 
|---|
| 85 |  | 
|---|
| 86 | BoundaryLineSet::~BoundaryLineSet() | 
|---|
| 87 | { | 
|---|
| 88 | int Numbers[2]; | 
|---|
| 89 | Numbers[0] = endpoints[1]->Nr; | 
|---|
| 90 | Numbers[1] = endpoints[0]->Nr; | 
|---|
| 91 | for (int i = 0; i < 2; i++) { | 
|---|
| 92 | cout << Verbose(5) << "Erasing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl; | 
|---|
| 93 | // 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 | 
|---|
| 94 | pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]); | 
|---|
| 95 | for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++) | 
|---|
| 96 | if ((*Runner).second == this) { | 
|---|
| 97 | endpoints[i]->lines.erase(Runner); | 
|---|
| 98 | break; | 
|---|
| 99 | } | 
|---|
| 100 | if (endpoints[i]->lines.empty()) { | 
|---|
| 101 | cout << Verbose(5) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl; | 
|---|
| 102 | if (endpoints[i] != NULL) { | 
|---|
| 103 | delete(endpoints[i]); | 
|---|
| 104 | endpoints[i] = NULL; | 
|---|
| 105 | } else | 
|---|
| 106 | cerr << "ERROR: Endpoint " << i << " has already been free'd." << endl; | 
|---|
| 107 | } else | 
|---|
| 108 | cout << Verbose(5) << *endpoints[i] << " has still lines it's attached to." << endl; | 
|---|
| 109 | } | 
|---|
| 110 | if (!triangles.empty()) | 
|---|
| 111 | cerr << "WARNING: Memory Leak! I " << *this << " am still connected to some triangles." << endl; | 
|---|
| 112 | } | 
|---|
| 113 | ; | 
|---|
| 114 |  | 
|---|
| 115 | void | 
|---|
| 116 | BoundaryLineSet::AddTriangle(class BoundaryTriangleSet *triangle) | 
|---|
| 117 | { | 
|---|
| 118 | cout << Verbose(6) << "Add " << triangle->Nr << " to line " << *this << "." | 
|---|
| 119 | << endl; | 
|---|
| 120 | triangles.insert(TrianglePair(triangle->Nr, triangle)); | 
|---|
| 121 | TrianglesCount++; | 
|---|
| 122 | } | 
|---|
| 123 | ; | 
|---|
| 124 |  | 
|---|
| 125 | /** Checks whether we have a common endpoint with given \a *line. | 
|---|
| 126 | * \param *line other line to test | 
|---|
| 127 | * \return true - common endpoint present, false - not connected | 
|---|
| 128 | */ | 
|---|
| 129 | bool BoundaryLineSet::IsConnectedTo(class BoundaryLineSet *line) | 
|---|
| 130 | { | 
|---|
| 131 | if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1])) | 
|---|
| 132 | return true; | 
|---|
| 133 | else | 
|---|
| 134 | return false; | 
|---|
| 135 | }; | 
|---|
| 136 |  | 
|---|
| 137 | /** Checks whether the adjacent triangles of a baseline are convex or not. | 
|---|
| 138 | * We sum the two angles of each normal vector with a ficticious normnal vector from this baselinbe pointing outwards. | 
|---|
| 139 | * If greater/equal M_PI than we are convex. | 
|---|
| 140 | * \param *out output stream for debugging | 
|---|
| 141 | * \return true - triangles are convex, false - concave or less than two triangles connected | 
|---|
| 142 | */ | 
|---|
| 143 | bool BoundaryLineSet::CheckConvexityCriterion(ofstream *out) | 
|---|
| 144 | { | 
|---|
| 145 | Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2]; | 
|---|
| 146 | // get the two triangles | 
|---|
| 147 | if (TrianglesCount != 2) { | 
|---|
| 148 | *out << Verbose(1) << "ERROR: Baseline " << this << " is connect to less than two triangles, Tesselation incomplete!" << endl; | 
|---|
| 149 | return false; | 
|---|
| 150 | } | 
|---|
| 151 | // have a normal vector on the base line pointing outwards | 
|---|
| 152 | *out << Verbose(3) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl; | 
|---|
| 153 | BaseLineCenter.CopyVector(endpoints[0]->node->node); | 
|---|
| 154 | BaseLineCenter.AddVector(endpoints[1]->node->node); | 
|---|
| 155 | BaseLineCenter.Scale(1./2.); | 
|---|
| 156 | BaseLine.CopyVector(endpoints[0]->node->node); | 
|---|
| 157 | BaseLine.SubtractVector(endpoints[1]->node->node); | 
|---|
| 158 | *out << Verbose(3) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl; | 
|---|
| 159 |  | 
|---|
| 160 | BaseLineNormal.Zero(); | 
|---|
| 161 | int i=0; | 
|---|
| 162 | class BoundaryPointSet *node = NULL; | 
|---|
| 163 | for(TriangleMap::iterator runner = triangles.begin(); runner != triangles.end(); runner++) { | 
|---|
| 164 | *out << Verbose(3) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl; | 
|---|
| 165 | BaseLineNormal.SubtractVector(&runner->second->NormalVector);   // we subtract as BaseLineNormal has to point inward in direction of [pi,2pi] | 
|---|
| 166 | node = runner->second->GetThirdEndpoint(this); | 
|---|
| 167 | if (node != NULL) { | 
|---|
| 168 | *out << Verbose(3) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl; | 
|---|
| 169 | helper[i].CopyVector(node->node->node); | 
|---|
| 170 | helper[i].SubtractVector(&BaseLineCenter); | 
|---|
| 171 | helper[i].MakeNormalVector(&BaseLine);  // we want to compare the triangle's heights' angles! | 
|---|
| 172 | *out << Verbose(4) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl; | 
|---|
| 173 | i++; | 
|---|
| 174 | } else { | 
|---|
| 175 | *out << Verbose(2) << "WARNING: I cannot find third node in triangle, something's wrong." << endl; | 
|---|
| 176 | return true; | 
|---|
| 177 | } | 
|---|
| 178 | } | 
|---|
| 179 | *out << Verbose(3) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl; | 
|---|
| 180 | double angle = helper[0].Angle(&helper[1]); | 
|---|
| 181 | if (BaseLineNormal.ScalarProduct(&helper[1]) > 0) { | 
|---|
| 182 | angle = 2.*M_PI - angle; | 
|---|
| 183 | } | 
|---|
| 184 | *out << Verbose(3) << "The angle is " << angle << "." << endl; | 
|---|
| 185 | if ((angle - M_PI) > -MYEPSILON) | 
|---|
| 186 | return true; | 
|---|
| 187 | else | 
|---|
| 188 | return false; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | /** Checks whether point is any of the two endpoints this line contains. | 
|---|
| 192 | * \param *point point to test | 
|---|
| 193 | * \return true - point is of the line, false - is not | 
|---|
| 194 | */ | 
|---|
| 195 | bool BoundaryLineSet::ContainsBoundaryPoint(class BoundaryPointSet *point) | 
|---|
| 196 | { | 
|---|
| 197 | for(int i=0;i<2;i++) | 
|---|
| 198 | if (point == endpoints[i]) | 
|---|
| 199 | return true; | 
|---|
| 200 | return false; | 
|---|
| 201 | }; | 
|---|
| 202 |  | 
|---|
| 203 | /** Returns other endpoint of the line. | 
|---|
| 204 | * \param *point other endpoint | 
|---|
| 205 | * \return NULL - if endpoint not contained in BoundaryLineSet, or pointer to BoundaryPointSet otherwise | 
|---|
| 206 | */ | 
|---|
| 207 | inline class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(class BoundaryPointSet *point) | 
|---|
| 208 | { | 
|---|
| 209 | if (endpoints[0] == point) | 
|---|
| 210 | return endpoints[1]; | 
|---|
| 211 | else if (endpoints[1] == point) | 
|---|
| 212 | return endpoints[0]; | 
|---|
| 213 | else | 
|---|
| 214 | return NULL; | 
|---|
| 215 | }; | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 | ostream & | 
|---|
| 219 | operator <<(ostream &ost, BoundaryLineSet &a) | 
|---|
| 220 | { | 
|---|
| 221 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," << a.endpoints[1]->node->Name << "]"; | 
|---|
| 222 | return ost; | 
|---|
| 223 | } | 
|---|
| 224 | ; | 
|---|
| 225 |  | 
|---|
| 226 | // ======================================== Triangles on Boundary ================================= | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 | BoundaryTriangleSet::BoundaryTriangleSet() | 
|---|
| 230 | { | 
|---|
| 231 | for (int i = 0; i < 3; i++) | 
|---|
| 232 | { | 
|---|
| 233 | endpoints[i] = NULL; | 
|---|
| 234 | lines[i] = NULL; | 
|---|
| 235 | } | 
|---|
| 236 | Nr = -1; | 
|---|
| 237 | } | 
|---|
| 238 | ; | 
|---|
| 239 |  | 
|---|
| 240 | BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet *line[3], int number) | 
|---|
| 241 | { | 
|---|
| 242 | // set number | 
|---|
| 243 | Nr = number; | 
|---|
| 244 | // set lines | 
|---|
| 245 | cout << Verbose(5) << "New triangle " << Nr << ":" << endl; | 
|---|
| 246 | for (int i = 0; i < 3; i++) | 
|---|
| 247 | { | 
|---|
| 248 | lines[i] = line[i]; | 
|---|
| 249 | lines[i]->AddTriangle(this); | 
|---|
| 250 | } | 
|---|
| 251 | // get ascending order of endpoints | 
|---|
| 252 | map<int, class BoundaryPointSet *> OrderMap; | 
|---|
| 253 | for (int i = 0; i < 3; i++) | 
|---|
| 254 | // for all three lines | 
|---|
| 255 | for (int j = 0; j < 2; j++) | 
|---|
| 256 | { // for both endpoints | 
|---|
| 257 | OrderMap.insert(pair<int, class BoundaryPointSet *> ( | 
|---|
| 258 | line[i]->endpoints[j]->Nr, line[i]->endpoints[j])); | 
|---|
| 259 | // and we don't care whether insertion fails | 
|---|
| 260 | } | 
|---|
| 261 | // set endpoints | 
|---|
| 262 | int Counter = 0; | 
|---|
| 263 | cout << Verbose(6) << " with end points "; | 
|---|
| 264 | for (map<int, class BoundaryPointSet *>::iterator runner = OrderMap.begin(); runner | 
|---|
| 265 | != OrderMap.end(); runner++) | 
|---|
| 266 | { | 
|---|
| 267 | endpoints[Counter] = runner->second; | 
|---|
| 268 | cout << " " << *endpoints[Counter]; | 
|---|
| 269 | Counter++; | 
|---|
| 270 | } | 
|---|
| 271 | if (Counter < 3) | 
|---|
| 272 | { | 
|---|
| 273 | cerr << "ERROR! We have a triangle with only two distinct endpoints!" | 
|---|
| 274 | << endl; | 
|---|
| 275 | //exit(1); | 
|---|
| 276 | } | 
|---|
| 277 | cout << "." << endl; | 
|---|
| 278 | } | 
|---|
| 279 | ; | 
|---|
| 280 |  | 
|---|
| 281 | BoundaryTriangleSet::~BoundaryTriangleSet() | 
|---|
| 282 | { | 
|---|
| 283 | for (int i = 0; i < 3; i++) { | 
|---|
| 284 | cout << Verbose(5) << "Erasing triangle Nr." << Nr << endl; | 
|---|
| 285 | lines[i]->triangles.erase(Nr); | 
|---|
| 286 | if (lines[i]->triangles.empty()) { | 
|---|
| 287 | if (lines[i] != NULL) { | 
|---|
| 288 | cout << Verbose(5) << *lines[i] << " is no more attached to any triangle, erasing." << endl; | 
|---|
| 289 | delete (lines[i]); | 
|---|
| 290 | lines[i] = NULL; | 
|---|
| 291 | } else | 
|---|
| 292 | cerr << "ERROR: This line " << i << " has already been free'd." << endl; | 
|---|
| 293 | } else | 
|---|
| 294 | cout << Verbose(5) << *lines[i] << " is still attached to another triangle." << endl; | 
|---|
| 295 | } | 
|---|
| 296 | } | 
|---|
| 297 | ; | 
|---|
| 298 |  | 
|---|
| 299 | /** Calculates the normal vector for this triangle. | 
|---|
| 300 | * Is made unique by comparison with \a OtherVector to point in the other direction. | 
|---|
| 301 | * \param &OtherVector direction vector to make normal vector unique. | 
|---|
| 302 | */ | 
|---|
| 303 | void BoundaryTriangleSet::GetNormalVector(Vector &OtherVector) | 
|---|
| 304 | { | 
|---|
| 305 | // get normal vector | 
|---|
| 306 | NormalVector.MakeNormalVector(endpoints[0]->node->node, endpoints[1]->node->node, endpoints[2]->node->node); | 
|---|
| 307 |  | 
|---|
| 308 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices) | 
|---|
| 309 | if (NormalVector.Projection(&OtherVector) > 0) | 
|---|
| 310 | NormalVector.Scale(-1.); | 
|---|
| 311 | }; | 
|---|
| 312 |  | 
|---|
| 313 | /** Finds the point on the triangle \a *BTS the line defined by \a *MolCenter and \a *x crosses through. | 
|---|
| 314 | * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane | 
|---|
| 315 | * This we test if it's really on the plane and whether it's inside the triangle on the plane or not. | 
|---|
| 316 | * The latter is done as follows: if it's really outside, then for any endpoint of the triangle and it's opposite | 
|---|
| 317 | * base line, the intersection between the line from endpoint to intersection and the base line will have a Vector::NormSquared() | 
|---|
| 318 | * smaller than the first line. | 
|---|
| 319 | * \param *out output stream for debugging | 
|---|
| 320 | * \param *MolCenter offset vector of line | 
|---|
| 321 | * \param *x second endpoint of line, minus \a *MolCenter is directional vector of line | 
|---|
| 322 | * \param *Intersection intersection on plane on return | 
|---|
| 323 | * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle. | 
|---|
| 324 | */ | 
|---|
| 325 | bool BoundaryTriangleSet::GetIntersectionInsideTriangle(ofstream *out, Vector *MolCenter, Vector *x, Vector *Intersection) | 
|---|
| 326 | { | 
|---|
| 327 | Vector CrossPoint; | 
|---|
| 328 | Vector helper; | 
|---|
| 329 | int i=0; | 
|---|
| 330 |  | 
|---|
| 331 | if (Intersection->GetIntersectionWithPlane(out, &NormalVector, endpoints[0]->node->node, MolCenter, x)) { | 
|---|
| 332 | *out << Verbose(1) << "Alas! [Bronstein] failed - at least numerically - the intersection is not on the plane!" << endl; | 
|---|
| 333 | return false; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | // Calculate cross point between one baseline and the line from the third endpoint to intersection | 
|---|
| 337 | do { | 
|---|
| 338 | CrossPoint.GetIntersectionOfTwoLinesOnPlane(out, endpoints[i%3]->node->node, endpoints[(i+1)%3]->node->node, endpoints[(i+2)%3]->node->node, Intersection); | 
|---|
| 339 | helper.CopyVector(endpoints[(i+1)%3]->node->node); | 
|---|
| 340 | helper.SubtractVector(endpoints[i%3]->node->node); | 
|---|
| 341 | i++; | 
|---|
| 342 | if (i>3) | 
|---|
| 343 | break; | 
|---|
| 344 | } while (CrossPoint.NormSquared() < MYEPSILON); | 
|---|
| 345 | if (i>3) { | 
|---|
| 346 | *out << Verbose(1) << "ERROR: Could not find any cross points, something's utterly wrong here!" << endl; | 
|---|
| 347 | exit(255); | 
|---|
| 348 | } | 
|---|
| 349 | CrossPoint.SubtractVector(endpoints[i%3]->node->node); | 
|---|
| 350 |  | 
|---|
| 351 | // check whether intersection is inside or not by comparing length of intersection and length of cross point | 
|---|
| 352 | if ((CrossPoint.NormSquared() - helper.NormSquared()) > -MYEPSILON) { // inside | 
|---|
| 353 | return true; | 
|---|
| 354 | } else { // outside! | 
|---|
| 355 | Intersection->Zero(); | 
|---|
| 356 | return false; | 
|---|
| 357 | } | 
|---|
| 358 | }; | 
|---|
| 359 |  | 
|---|
| 360 | /** Checks whether lines is any of the three boundary lines this triangle contains. | 
|---|
| 361 | * \param *line line to test | 
|---|
| 362 | * \return true - line is of the triangle, false - is not | 
|---|
| 363 | */ | 
|---|
| 364 | bool BoundaryTriangleSet::ContainsBoundaryLine(class BoundaryLineSet *line) | 
|---|
| 365 | { | 
|---|
| 366 | for(int i=0;i<3;i++) | 
|---|
| 367 | if (line == lines[i]) | 
|---|
| 368 | return true; | 
|---|
| 369 | return false; | 
|---|
| 370 | }; | 
|---|
| 371 |  | 
|---|
| 372 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 373 | * \param *point point to test | 
|---|
| 374 | * \return true - point is of the triangle, false - is not | 
|---|
| 375 | */ | 
|---|
| 376 | bool BoundaryTriangleSet::ContainsBoundaryPoint(class BoundaryPointSet *point) | 
|---|
| 377 | { | 
|---|
| 378 | for(int i=0;i<3;i++) | 
|---|
| 379 | if (point == endpoints[i]) | 
|---|
| 380 | return true; | 
|---|
| 381 | return false; | 
|---|
| 382 | }; | 
|---|
| 383 |  | 
|---|
| 384 | /** Checks whether three given \a *Points coincide with triangle's endpoints. | 
|---|
| 385 | * \param *Points[3] pointer to BoundaryPointSet | 
|---|
| 386 | * \return true - is the very triangle, false - is not | 
|---|
| 387 | */ | 
|---|
| 388 | bool BoundaryTriangleSet::IsPresentTupel(class BoundaryPointSet *Points[3]) | 
|---|
| 389 | { | 
|---|
| 390 | return (((endpoints[0] == Points[0]) | 
|---|
| 391 | || (endpoints[0] == Points[1]) | 
|---|
| 392 | || (endpoints[0] == Points[2]) | 
|---|
| 393 | ) && ( | 
|---|
| 394 | (endpoints[1] == Points[0]) | 
|---|
| 395 | || (endpoints[1] == Points[1]) | 
|---|
| 396 | || (endpoints[1] == Points[2]) | 
|---|
| 397 | ) && ( | 
|---|
| 398 | (endpoints[2] == Points[0]) | 
|---|
| 399 | || (endpoints[2] == Points[1]) | 
|---|
| 400 | || (endpoints[2] == Points[2]) | 
|---|
| 401 |  | 
|---|
| 402 | )); | 
|---|
| 403 | }; | 
|---|
| 404 |  | 
|---|
| 405 | /** Returns the endpoint which is not contained in the given \a *line. | 
|---|
| 406 | * \param *line baseline defining two endpoints | 
|---|
| 407 | * \return pointer third endpoint or NULL if line does not belong to triangle. | 
|---|
| 408 | */ | 
|---|
| 409 | class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(class BoundaryLineSet *line) | 
|---|
| 410 | { | 
|---|
| 411 | // sanity check | 
|---|
| 412 | if (!ContainsBoundaryLine(line)) | 
|---|
| 413 | return NULL; | 
|---|
| 414 | for(int i=0;i<3;i++) | 
|---|
| 415 | if (!line->ContainsBoundaryPoint(endpoints[i])) | 
|---|
| 416 | return endpoints[i]; | 
|---|
| 417 | // actually, that' impossible :) | 
|---|
| 418 | return NULL; | 
|---|
| 419 | }; | 
|---|
| 420 |  | 
|---|
| 421 | /** Calculates the center point of the triangle. | 
|---|
| 422 | * Is third of the sum of all endpoints. | 
|---|
| 423 | * \param *center central point on return. | 
|---|
| 424 | */ | 
|---|
| 425 | void BoundaryTriangleSet::GetCenter(Vector *center) | 
|---|
| 426 | { | 
|---|
| 427 | center->Zero(); | 
|---|
| 428 | for(int i=0;i<3;i++) | 
|---|
| 429 | center->AddVector(endpoints[i]->node->node); | 
|---|
| 430 | center->Scale(1./3.); | 
|---|
| 431 | } | 
|---|
| 432 |  | 
|---|
| 433 | ostream & | 
|---|
| 434 | operator <<(ostream &ost, BoundaryTriangleSet &a) | 
|---|
| 435 | { | 
|---|
| 436 | ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << "," | 
|---|
| 437 | << a.endpoints[1]->node->Name << "," << a.endpoints[2]->node->Name << "]"; | 
|---|
| 438 | return ost; | 
|---|
| 439 | } | 
|---|
| 440 | ; | 
|---|
| 441 |  | 
|---|
| 442 | // =========================================================== class TESSELPOINT =========================================== | 
|---|
| 443 |  | 
|---|
| 444 | /** Constructor of class TesselPoint. | 
|---|
| 445 | */ | 
|---|
| 446 | TesselPoint::TesselPoint() | 
|---|
| 447 | { | 
|---|
| 448 | node = NULL; | 
|---|
| 449 | nr = -1; | 
|---|
| 450 | Name =  NULL; | 
|---|
| 451 | }; | 
|---|
| 452 |  | 
|---|
| 453 | /** Destructor for class TesselPoint. | 
|---|
| 454 | */ | 
|---|
| 455 | TesselPoint::~TesselPoint() | 
|---|
| 456 | { | 
|---|
| 457 | Free((void **)&Name, "TesselPoint::~TesselPoint: *Name"); | 
|---|
| 458 | }; | 
|---|
| 459 |  | 
|---|
| 460 | /** Prints LCNode to screen. | 
|---|
| 461 | */ | 
|---|
| 462 | ostream & operator << (ostream &ost, const TesselPoint &a) | 
|---|
| 463 | { | 
|---|
| 464 | ost << "[" << (a.Name) << "|" << &a << "]"; | 
|---|
| 465 | return ost; | 
|---|
| 466 | }; | 
|---|
| 467 |  | 
|---|
| 468 |  | 
|---|
| 469 | // =========================================================== class POINTCLOUD ============================================ | 
|---|
| 470 |  | 
|---|
| 471 | /** Constructor of class PointCloud. | 
|---|
| 472 | */ | 
|---|
| 473 | PointCloud::PointCloud() | 
|---|
| 474 | { | 
|---|
| 475 |  | 
|---|
| 476 | }; | 
|---|
| 477 |  | 
|---|
| 478 | /** Destructor for class PointCloud. | 
|---|
| 479 | */ | 
|---|
| 480 | PointCloud::~PointCloud() | 
|---|
| 481 | { | 
|---|
| 482 |  | 
|---|
| 483 | }; | 
|---|
| 484 |  | 
|---|
| 485 | // ============================ CandidateForTesselation ============================= | 
|---|
| 486 |  | 
|---|
| 487 | /** Constructor of class CandidateForTesselation. | 
|---|
| 488 | */ | 
|---|
| 489 | CandidateForTesselation::CandidateForTesselation(TesselPoint *candidate, BoundaryLineSet* line, Vector OptCandidateCenter, Vector OtherOptCandidateCenter) { | 
|---|
| 490 | point = candidate; | 
|---|
| 491 | BaseLine = line; | 
|---|
| 492 | OptCenter.CopyVector(&OptCandidateCenter); | 
|---|
| 493 | OtherOptCenter.CopyVector(&OtherOptCandidateCenter); | 
|---|
| 494 | }; | 
|---|
| 495 |  | 
|---|
| 496 | /** Destructor for class CandidateForTesselation. | 
|---|
| 497 | */ | 
|---|
| 498 | CandidateForTesselation::~CandidateForTesselation() { | 
|---|
| 499 | point = NULL; | 
|---|
| 500 | BaseLine = NULL; | 
|---|
| 501 | }; | 
|---|
| 502 |  | 
|---|
| 503 | // =========================================================== class TESSELATION =========================================== | 
|---|
| 504 |  | 
|---|
| 505 | /** Constructor of class Tesselation. | 
|---|
| 506 | */ | 
|---|
| 507 | Tesselation::Tesselation() | 
|---|
| 508 | { | 
|---|
| 509 | PointsOnBoundaryCount = 0; | 
|---|
| 510 | LinesOnBoundaryCount = 0; | 
|---|
| 511 | TrianglesOnBoundaryCount = 0; | 
|---|
| 512 | } | 
|---|
| 513 | ; | 
|---|
| 514 |  | 
|---|
| 515 | /** Destructor of class Tesselation. | 
|---|
| 516 | * We have to free all points, lines and triangles. | 
|---|
| 517 | */ | 
|---|
| 518 | Tesselation::~Tesselation() | 
|---|
| 519 | { | 
|---|
| 520 | cout << Verbose(1) << "Free'ing TesselStruct ... " << endl; | 
|---|
| 521 | for (TriangleMap::iterator runner = TrianglesOnBoundary.begin(); runner != TrianglesOnBoundary.end(); runner++) { | 
|---|
| 522 | if (runner->second != NULL) { | 
|---|
| 523 | delete (runner->second); | 
|---|
| 524 | runner->second = NULL; | 
|---|
| 525 | } else | 
|---|
| 526 | cerr << "ERROR: The triangle " << runner->first << " has already been free'd." << endl; | 
|---|
| 527 | } | 
|---|
| 528 | } | 
|---|
| 529 | ; | 
|---|
| 530 |  | 
|---|
| 531 | /** Gueses first starting triangle of the convex envelope. | 
|---|
| 532 | * We guess the starting triangle by taking the smallest distance between two points and looking for a fitting third. | 
|---|
| 533 | * \param *out output stream for debugging | 
|---|
| 534 | * \param PointsOnBoundary set of boundary points defining the convex envelope of the cluster | 
|---|
| 535 | */ | 
|---|
| 536 | void | 
|---|
| 537 | Tesselation::GuessStartingTriangle(ofstream *out) | 
|---|
| 538 | { | 
|---|
| 539 | // 4b. create a starting triangle | 
|---|
| 540 | // 4b1. create all distances | 
|---|
| 541 | DistanceMultiMap DistanceMMap; | 
|---|
| 542 | double distance, tmp; | 
|---|
| 543 | Vector PlaneVector, TrialVector; | 
|---|
| 544 | PointMap::iterator A, B, C; // three nodes of the first triangle | 
|---|
| 545 | A = PointsOnBoundary.begin(); // the first may be chosen arbitrarily | 
|---|
| 546 |  | 
|---|
| 547 | // with A chosen, take each pair B,C and sort | 
|---|
| 548 | if (A != PointsOnBoundary.end()) | 
|---|
| 549 | { | 
|---|
| 550 | B = A; | 
|---|
| 551 | B++; | 
|---|
| 552 | for (; B != PointsOnBoundary.end(); B++) | 
|---|
| 553 | { | 
|---|
| 554 | C = B; | 
|---|
| 555 | C++; | 
|---|
| 556 | for (; C != PointsOnBoundary.end(); C++) | 
|---|
| 557 | { | 
|---|
| 558 | tmp = A->second->node->node->DistanceSquared(B->second->node->node); | 
|---|
| 559 | distance = tmp * tmp; | 
|---|
| 560 | tmp = A->second->node->node->DistanceSquared(C->second->node->node); | 
|---|
| 561 | distance += tmp * tmp; | 
|---|
| 562 | tmp = B->second->node->node->DistanceSquared(C->second->node->node); | 
|---|
| 563 | distance += tmp * tmp; | 
|---|
| 564 | DistanceMMap.insert(DistanceMultiMapPair(distance, pair<PointMap::iterator, PointMap::iterator> (B, C))); | 
|---|
| 565 | } | 
|---|
| 566 | } | 
|---|
| 567 | } | 
|---|
| 568 | //    // listing distances | 
|---|
| 569 | //    *out << Verbose(1) << "Listing DistanceMMap:"; | 
|---|
| 570 | //    for(DistanceMultiMap::iterator runner = DistanceMMap.begin(); runner != DistanceMMap.end(); runner++) { | 
|---|
| 571 | //      *out << " " << runner->first << "(" << *runner->second.first->second << ", " << *runner->second.second->second << ")"; | 
|---|
| 572 | //    } | 
|---|
| 573 | //    *out << endl; | 
|---|
| 574 | // 4b2. pick three baselines forming a triangle | 
|---|
| 575 | // 1. we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate | 
|---|
| 576 | DistanceMultiMap::iterator baseline = DistanceMMap.begin(); | 
|---|
| 577 | for (; baseline != DistanceMMap.end(); baseline++) | 
|---|
| 578 | { | 
|---|
| 579 | // we take from the smallest sum of squared distance as the base line BC (with peak A) onward as the triangle candidate | 
|---|
| 580 | // 2. next, we have to check whether all points reside on only one side of the triangle | 
|---|
| 581 | // 3. construct plane vector | 
|---|
| 582 | PlaneVector.MakeNormalVector(A->second->node->node, | 
|---|
| 583 | baseline->second.first->second->node->node, | 
|---|
| 584 | baseline->second.second->second->node->node); | 
|---|
| 585 | *out << Verbose(2) << "Plane vector of candidate triangle is "; | 
|---|
| 586 | PlaneVector.Output(out); | 
|---|
| 587 | *out << endl; | 
|---|
| 588 | // 4. loop over all points | 
|---|
| 589 | double sign = 0.; | 
|---|
| 590 | PointMap::iterator checker = PointsOnBoundary.begin(); | 
|---|
| 591 | for (; checker != PointsOnBoundary.end(); checker++) | 
|---|
| 592 | { | 
|---|
| 593 | // (neglecting A,B,C) | 
|---|
| 594 | if ((checker == A) || (checker == baseline->second.first) || (checker | 
|---|
| 595 | == baseline->second.second)) | 
|---|
| 596 | continue; | 
|---|
| 597 | // 4a. project onto plane vector | 
|---|
| 598 | TrialVector.CopyVector(checker->second->node->node); | 
|---|
| 599 | TrialVector.SubtractVector(A->second->node->node); | 
|---|
| 600 | distance = TrialVector.Projection(&PlaneVector); | 
|---|
| 601 | if (fabs(distance) < 1e-4) // we need to have a small epsilon around 0 which is still ok | 
|---|
| 602 | continue; | 
|---|
| 603 | *out << Verbose(3) << "Projection of " << checker->second->node->Name | 
|---|
| 604 | << " yields distance of " << distance << "." << endl; | 
|---|
| 605 | tmp = distance / fabs(distance); | 
|---|
| 606 | // 4b. Any have different sign to than before? (i.e. would lie outside convex hull with this starting triangle) | 
|---|
| 607 | if ((sign != 0) && (tmp != sign)) | 
|---|
| 608 | { | 
|---|
| 609 | // 4c. If so, break 4. loop and continue with next candidate in 1. loop | 
|---|
| 610 | *out << Verbose(2) << "Current candidates: " | 
|---|
| 611 | << A->second->node->Name << "," | 
|---|
| 612 | << baseline->second.first->second->node->Name << "," | 
|---|
| 613 | << baseline->second.second->second->node->Name << " leaves " | 
|---|
| 614 | << checker->second->node->Name << " outside the convex hull." | 
|---|
| 615 | << endl; | 
|---|
| 616 | break; | 
|---|
| 617 | } | 
|---|
| 618 | else | 
|---|
| 619 | { // note the sign for later | 
|---|
| 620 | *out << Verbose(2) << "Current candidates: " | 
|---|
| 621 | << A->second->node->Name << "," | 
|---|
| 622 | << baseline->second.first->second->node->Name << "," | 
|---|
| 623 | << baseline->second.second->second->node->Name << " leave " | 
|---|
| 624 | << checker->second->node->Name << " inside the convex hull." | 
|---|
| 625 | << endl; | 
|---|
| 626 | sign = tmp; | 
|---|
| 627 | } | 
|---|
| 628 | // 4d. Check whether the point is inside the triangle (check distance to each node | 
|---|
| 629 | tmp = checker->second->node->node->DistanceSquared(A->second->node->node); | 
|---|
| 630 | int innerpoint = 0; | 
|---|
| 631 | if ((tmp < A->second->node->node->DistanceSquared( | 
|---|
| 632 | baseline->second.first->second->node->node)) && (tmp | 
|---|
| 633 | < A->second->node->node->DistanceSquared( | 
|---|
| 634 | baseline->second.second->second->node->node))) | 
|---|
| 635 | innerpoint++; | 
|---|
| 636 | tmp = checker->second->node->node->DistanceSquared( | 
|---|
| 637 | baseline->second.first->second->node->node); | 
|---|
| 638 | if ((tmp < baseline->second.first->second->node->node->DistanceSquared( | 
|---|
| 639 | A->second->node->node)) && (tmp | 
|---|
| 640 | < baseline->second.first->second->node->node->DistanceSquared( | 
|---|
| 641 | baseline->second.second->second->node->node))) | 
|---|
| 642 | innerpoint++; | 
|---|
| 643 | tmp = checker->second->node->node->DistanceSquared( | 
|---|
| 644 | baseline->second.second->second->node->node); | 
|---|
| 645 | if ((tmp < baseline->second.second->second->node->node->DistanceSquared( | 
|---|
| 646 | baseline->second.first->second->node->node)) && (tmp | 
|---|
| 647 | < baseline->second.second->second->node->node->DistanceSquared( | 
|---|
| 648 | A->second->node->node))) | 
|---|
| 649 | innerpoint++; | 
|---|
| 650 | // 4e. If so, break 4. loop and continue with next candidate in 1. loop | 
|---|
| 651 | if (innerpoint == 3) | 
|---|
| 652 | break; | 
|---|
| 653 | } | 
|---|
| 654 | // 5. come this far, all on same side? Then break 1. loop and construct triangle | 
|---|
| 655 | if (checker == PointsOnBoundary.end()) | 
|---|
| 656 | { | 
|---|
| 657 | *out << "Looks like we have a candidate!" << endl; | 
|---|
| 658 | break; | 
|---|
| 659 | } | 
|---|
| 660 | } | 
|---|
| 661 | if (baseline != DistanceMMap.end()) | 
|---|
| 662 | { | 
|---|
| 663 | BPS[0] = baseline->second.first->second; | 
|---|
| 664 | BPS[1] = baseline->second.second->second; | 
|---|
| 665 | BLS[0] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 666 | BPS[0] = A->second; | 
|---|
| 667 | BPS[1] = baseline->second.second->second; | 
|---|
| 668 | BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 669 | BPS[0] = baseline->second.first->second; | 
|---|
| 670 | BPS[1] = A->second; | 
|---|
| 671 | BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 672 |  | 
|---|
| 673 | // 4b3. insert created triangle | 
|---|
| 674 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 675 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 676 | TrianglesOnBoundaryCount++; | 
|---|
| 677 | for (int i = 0; i < NDIM; i++) | 
|---|
| 678 | { | 
|---|
| 679 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BTS->lines[i])); | 
|---|
| 680 | LinesOnBoundaryCount++; | 
|---|
| 681 | } | 
|---|
| 682 |  | 
|---|
| 683 | *out << Verbose(1) << "Starting triangle is " << *BTS << "." << endl; | 
|---|
| 684 | } | 
|---|
| 685 | else | 
|---|
| 686 | { | 
|---|
| 687 | *out << Verbose(1) << "No starting triangle found." << endl; | 
|---|
| 688 | exit(255); | 
|---|
| 689 | } | 
|---|
| 690 | } | 
|---|
| 691 | ; | 
|---|
| 692 |  | 
|---|
| 693 | /** Tesselates the convex envelope of a cluster from a single starting triangle. | 
|---|
| 694 | * The starting triangle is made out of three baselines. Each line in the final tesselated cluster may belong to at most | 
|---|
| 695 | * 2 triangles. Hence, we go through all current lines: | 
|---|
| 696 | * -# if the lines contains to only one triangle | 
|---|
| 697 | * -# We search all points in the boundary | 
|---|
| 698 | *    -# if the triangle is in forward direction of the baseline (at most 90 degrees angle between vector orthogonal to | 
|---|
| 699 | *       baseline in triangle plane pointing out of the triangle and normal vector of new triangle) | 
|---|
| 700 | *    -# if the triangle with the baseline and the current point has the smallest of angles (comparison between normal vectors) | 
|---|
| 701 | *    -# then we have a new triangle, whose baselines we again add (or increase their TriangleCount) | 
|---|
| 702 | * \param *out output stream for debugging | 
|---|
| 703 | * \param *configuration for IsAngstroem | 
|---|
| 704 | * \param *cloud cluster of points | 
|---|
| 705 | */ | 
|---|
| 706 | void Tesselation::TesselateOnBoundary(ofstream *out, PointCloud *cloud) | 
|---|
| 707 | { | 
|---|
| 708 | bool flag; | 
|---|
| 709 | PointMap::iterator winner; | 
|---|
| 710 | class BoundaryPointSet *peak = NULL; | 
|---|
| 711 | double SmallestAngle, TempAngle; | 
|---|
| 712 | Vector NormalVector, VirtualNormalVector, CenterVector, TempVector, helper, PropagationVector, *Center = NULL; | 
|---|
| 713 | LineMap::iterator LineChecker[2]; | 
|---|
| 714 |  | 
|---|
| 715 | Center = cloud->GetCenter(out); | 
|---|
| 716 | // create a first tesselation with the given BoundaryPoints | 
|---|
| 717 | do { | 
|---|
| 718 | flag = false; | 
|---|
| 719 | for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++) | 
|---|
| 720 | if (baseline->second->TrianglesCount == 1) { | 
|---|
| 721 | // 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) | 
|---|
| 722 | SmallestAngle = M_PI; | 
|---|
| 723 |  | 
|---|
| 724 | // get peak point with respect to this base line's only triangle | 
|---|
| 725 | BTS = baseline->second->triangles.begin()->second; // there is only one triangle so far | 
|---|
| 726 | *out << Verbose(2) << "Current baseline is between " << *(baseline->second) << "." << endl; | 
|---|
| 727 | for (int i = 0; i < 3; i++) | 
|---|
| 728 | if ((BTS->endpoints[i] != baseline->second->endpoints[0]) && (BTS->endpoints[i] != baseline->second->endpoints[1])) | 
|---|
| 729 | peak = BTS->endpoints[i]; | 
|---|
| 730 | *out << Verbose(3) << " and has peak " << *peak << "." << endl; | 
|---|
| 731 |  | 
|---|
| 732 | // prepare some auxiliary vectors | 
|---|
| 733 | Vector BaseLineCenter, BaseLine; | 
|---|
| 734 | BaseLineCenter.CopyVector(baseline->second->endpoints[0]->node->node); | 
|---|
| 735 | BaseLineCenter.AddVector(baseline->second->endpoints[1]->node->node); | 
|---|
| 736 | BaseLineCenter.Scale(1. / 2.); // points now to center of base line | 
|---|
| 737 | BaseLine.CopyVector(baseline->second->endpoints[0]->node->node); | 
|---|
| 738 | BaseLine.SubtractVector(baseline->second->endpoints[1]->node->node); | 
|---|
| 739 |  | 
|---|
| 740 | // offset to center of triangle | 
|---|
| 741 | CenterVector.Zero(); | 
|---|
| 742 | for (int i = 0; i < 3; i++) | 
|---|
| 743 | CenterVector.AddVector(BTS->endpoints[i]->node->node); | 
|---|
| 744 | CenterVector.Scale(1. / 3.); | 
|---|
| 745 | *out << Verbose(4) << "CenterVector of base triangle is " << CenterVector << endl; | 
|---|
| 746 |  | 
|---|
| 747 | // normal vector of triangle | 
|---|
| 748 | NormalVector.CopyVector(Center); | 
|---|
| 749 | NormalVector.SubtractVector(&CenterVector); | 
|---|
| 750 | BTS->GetNormalVector(NormalVector); | 
|---|
| 751 | NormalVector.CopyVector(&BTS->NormalVector); | 
|---|
| 752 | *out << Verbose(4) << "NormalVector of base triangle is " << NormalVector << endl; | 
|---|
| 753 |  | 
|---|
| 754 | // vector in propagation direction (out of triangle) | 
|---|
| 755 | // project center vector onto triangle plane (points from intersection plane-NormalVector to plane-CenterVector intersection) | 
|---|
| 756 | PropagationVector.MakeNormalVector(&BaseLine, &NormalVector); | 
|---|
| 757 | TempVector.CopyVector(&CenterVector); | 
|---|
| 758 | TempVector.SubtractVector(baseline->second->endpoints[0]->node->node); // TempVector is vector on triangle plane pointing from one baseline egde towards center! | 
|---|
| 759 | //*out << Verbose(2) << "Projection of propagation onto temp: " << PropagationVector.Projection(&TempVector) << "." << endl; | 
|---|
| 760 | if (PropagationVector.Projection(&TempVector) > 0) // make sure normal propagation vector points outward from baseline | 
|---|
| 761 | PropagationVector.Scale(-1.); | 
|---|
| 762 | *out << Verbose(4) << "PropagationVector of base triangle is " << PropagationVector << endl; | 
|---|
| 763 | winner = PointsOnBoundary.end(); | 
|---|
| 764 |  | 
|---|
| 765 | // loop over all points and calculate angle between normal vector of new and present triangle | 
|---|
| 766 | for (PointMap::iterator target = PointsOnBoundary.begin(); target != PointsOnBoundary.end(); target++) { | 
|---|
| 767 | if ((target->second != baseline->second->endpoints[0]) && (target->second != baseline->second->endpoints[1])) { // don't take the same endpoints | 
|---|
| 768 | *out << Verbose(3) << "Target point is " << *(target->second) << ":" << endl; | 
|---|
| 769 |  | 
|---|
| 770 | // first check direction, so that triangles don't intersect | 
|---|
| 771 | VirtualNormalVector.CopyVector(target->second->node->node); | 
|---|
| 772 | VirtualNormalVector.SubtractVector(&BaseLineCenter); // points from center of base line to target | 
|---|
| 773 | VirtualNormalVector.ProjectOntoPlane(&NormalVector); | 
|---|
| 774 | TempAngle = VirtualNormalVector.Angle(&PropagationVector); | 
|---|
| 775 | *out << Verbose(4) << "VirtualNormalVector is " << VirtualNormalVector << " and PropagationVector is " << PropagationVector << "." << endl; | 
|---|
| 776 | if (TempAngle > (M_PI/2.)) { // no bends bigger than Pi/2 (90 degrees) | 
|---|
| 777 | *out << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", bad direction!" << endl; | 
|---|
| 778 | continue; | 
|---|
| 779 | } else | 
|---|
| 780 | *out << Verbose(4) << "Angle on triangle plane between propagation direction and base line to " << *(target->second) << " is " << TempAngle << ", good direction!" << endl; | 
|---|
| 781 |  | 
|---|
| 782 | // check first and second endpoint (if any connecting line goes to target has at least not more than 1 triangle) | 
|---|
| 783 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(target->first); | 
|---|
| 784 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(target->first); | 
|---|
| 785 | if (((LineChecker[0] != baseline->second->endpoints[0]->lines.end()) && (LineChecker[0]->second->TrianglesCount == 2))) { | 
|---|
| 786 | *out << Verbose(4) << *(baseline->second->endpoints[0]) << " has line " << *(LineChecker[0]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[0]->second->TrianglesCount << " triangles." << endl; | 
|---|
| 787 | continue; | 
|---|
| 788 | } | 
|---|
| 789 | if (((LineChecker[1] != baseline->second->endpoints[1]->lines.end()) && (LineChecker[1]->second->TrianglesCount == 2))) { | 
|---|
| 790 | *out << Verbose(4) << *(baseline->second->endpoints[1]) << " has line " << *(LineChecker[1]->second) << " to " << *(target->second) << " as endpoint with " << LineChecker[1]->second->TrianglesCount << " triangles." << endl; | 
|---|
| 791 | continue; | 
|---|
| 792 | } | 
|---|
| 793 |  | 
|---|
| 794 | // check whether the envisaged triangle does not already exist (if both lines exist and have same endpoint) | 
|---|
| 795 | 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)))) { | 
|---|
| 796 | *out << Verbose(4) << "Current target is peak!" << endl; | 
|---|
| 797 | continue; | 
|---|
| 798 | } | 
|---|
| 799 |  | 
|---|
| 800 | // check for linear dependence | 
|---|
| 801 | TempVector.CopyVector(baseline->second->endpoints[0]->node->node); | 
|---|
| 802 | TempVector.SubtractVector(target->second->node->node); | 
|---|
| 803 | helper.CopyVector(baseline->second->endpoints[1]->node->node); | 
|---|
| 804 | helper.SubtractVector(target->second->node->node); | 
|---|
| 805 | helper.ProjectOntoPlane(&TempVector); | 
|---|
| 806 | if (fabs(helper.NormSquared()) < MYEPSILON) { | 
|---|
| 807 | *out << Verbose(4) << "Chosen set of vectors is linear dependent." << endl; | 
|---|
| 808 | continue; | 
|---|
| 809 | } | 
|---|
| 810 |  | 
|---|
| 811 | // in case NOT both were found, create virtually this triangle, get its normal vector, calculate angle | 
|---|
| 812 | flag = true; | 
|---|
| 813 | VirtualNormalVector.MakeNormalVector(baseline->second->endpoints[0]->node->node, baseline->second->endpoints[1]->node->node, target->second->node->node); | 
|---|
| 814 | TempVector.CopyVector(baseline->second->endpoints[0]->node->node); | 
|---|
| 815 | TempVector.AddVector(baseline->second->endpoints[1]->node->node); | 
|---|
| 816 | TempVector.AddVector(target->second->node->node); | 
|---|
| 817 | TempVector.Scale(1./3.); | 
|---|
| 818 | TempVector.SubtractVector(Center); | 
|---|
| 819 | // make it always point outward | 
|---|
| 820 | if (VirtualNormalVector.Projection(&TempVector) < 0) | 
|---|
| 821 | VirtualNormalVector.Scale(-1.); | 
|---|
| 822 | // calculate angle | 
|---|
| 823 | TempAngle = NormalVector.Angle(&VirtualNormalVector); | 
|---|
| 824 | *out << Verbose(4) << "NormalVector is " << VirtualNormalVector << " and the angle is " << TempAngle << "." << endl; | 
|---|
| 825 | if ((SmallestAngle - TempAngle) > MYEPSILON) { // set to new possible winner | 
|---|
| 826 | SmallestAngle = TempAngle; | 
|---|
| 827 | winner = target; | 
|---|
| 828 | *out << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl; | 
|---|
| 829 | } else if (fabs(SmallestAngle - TempAngle) < MYEPSILON) { // check the angle to propagation, both possible targets are in one plane! (their normals have same angle) | 
|---|
| 830 | // hence, check the angles to some normal direction from our base line but in this common plane of both targets... | 
|---|
| 831 | helper.CopyVector(target->second->node->node); | 
|---|
| 832 | helper.SubtractVector(&BaseLineCenter); | 
|---|
| 833 | helper.ProjectOntoPlane(&BaseLine); | 
|---|
| 834 | // ...the one with the smaller angle is the better candidate | 
|---|
| 835 | TempVector.CopyVector(target->second->node->node); | 
|---|
| 836 | TempVector.SubtractVector(&BaseLineCenter); | 
|---|
| 837 | TempVector.ProjectOntoPlane(&VirtualNormalVector); | 
|---|
| 838 | TempAngle = TempVector.Angle(&helper); | 
|---|
| 839 | TempVector.CopyVector(winner->second->node->node); | 
|---|
| 840 | TempVector.SubtractVector(&BaseLineCenter); | 
|---|
| 841 | TempVector.ProjectOntoPlane(&VirtualNormalVector); | 
|---|
| 842 | if (TempAngle < TempVector.Angle(&helper)) { | 
|---|
| 843 | TempAngle = NormalVector.Angle(&VirtualNormalVector); | 
|---|
| 844 | SmallestAngle = TempAngle; | 
|---|
| 845 | winner = target; | 
|---|
| 846 | *out << Verbose(4) << "New winner " << *winner->second->node << " due to smaller angle " << TempAngle << " to propagation direction." << endl; | 
|---|
| 847 | } else | 
|---|
| 848 | *out << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle to propagation direction." << endl; | 
|---|
| 849 | } else | 
|---|
| 850 | *out << Verbose(4) << "Keeping old winner " << *winner->second->node << " due to smaller angle between normal vectors." << endl; | 
|---|
| 851 | } | 
|---|
| 852 | } // end of loop over all boundary points | 
|---|
| 853 |  | 
|---|
| 854 | // 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 | 
|---|
| 855 | if (winner != PointsOnBoundary.end()) { | 
|---|
| 856 | *out << Verbose(2) << "Winning target point is " << *(winner->second) << " with angle " << SmallestAngle << "." << endl; | 
|---|
| 857 | // create the lins of not yet present | 
|---|
| 858 | BLS[0] = baseline->second; | 
|---|
| 859 | // 5c. add lines to the line set if those were new (not yet part of a triangle), delete lines that belong to two triangles) | 
|---|
| 860 | LineChecker[0] = baseline->second->endpoints[0]->lines.find(winner->first); | 
|---|
| 861 | LineChecker[1] = baseline->second->endpoints[1]->lines.find(winner->first); | 
|---|
| 862 | if (LineChecker[0] == baseline->second->endpoints[0]->lines.end()) { // create | 
|---|
| 863 | BPS[0] = baseline->second->endpoints[0]; | 
|---|
| 864 | BPS[1] = winner->second; | 
|---|
| 865 | BLS[1] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 866 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[1])); | 
|---|
| 867 | LinesOnBoundaryCount++; | 
|---|
| 868 | } else | 
|---|
| 869 | BLS[1] = LineChecker[0]->second; | 
|---|
| 870 | if (LineChecker[1] == baseline->second->endpoints[1]->lines.end()) { // create | 
|---|
| 871 | BPS[0] = baseline->second->endpoints[1]; | 
|---|
| 872 | BPS[1] = winner->second; | 
|---|
| 873 | BLS[2] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 874 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[2])); | 
|---|
| 875 | LinesOnBoundaryCount++; | 
|---|
| 876 | } else | 
|---|
| 877 | BLS[2] = LineChecker[1]->second; | 
|---|
| 878 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 879 | BTS->GetCenter(&helper); | 
|---|
| 880 | helper.SubtractVector(Center); | 
|---|
| 881 | helper.Scale(-1); | 
|---|
| 882 | BTS->GetNormalVector(helper); | 
|---|
| 883 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 884 | TrianglesOnBoundaryCount++; | 
|---|
| 885 | } else { | 
|---|
| 886 | *out << Verbose(1) << "I could not determine a winner for this baseline " << *(baseline->second) << "." << endl; | 
|---|
| 887 | } | 
|---|
| 888 |  | 
|---|
| 889 | // 5d. If the set of lines is not yet empty, go to 5. and continue | 
|---|
| 890 | } else | 
|---|
| 891 | *out << Verbose(2) << "Baseline candidate " << *(baseline->second) << " has a triangle count of " << baseline->second->TrianglesCount << "." << endl; | 
|---|
| 892 | } while (flag); | 
|---|
| 893 |  | 
|---|
| 894 | // exit | 
|---|
| 895 | delete(Center); | 
|---|
| 896 | }; | 
|---|
| 897 |  | 
|---|
| 898 | /** Inserts all points outside of the tesselated surface into it by adding new triangles. | 
|---|
| 899 | * \param *out output stream for debugging | 
|---|
| 900 | * \param *cloud cluster of points | 
|---|
| 901 | * \param *LC LinkedCell structure to find nearest point quickly | 
|---|
| 902 | * \return true - all straddling points insert, false - something went wrong | 
|---|
| 903 | */ | 
|---|
| 904 | bool Tesselation::InsertStraddlingPoints(ofstream *out, PointCloud *cloud, LinkedCell *LC) | 
|---|
| 905 | { | 
|---|
| 906 | Vector Intersection; | 
|---|
| 907 | TesselPoint *Walker = NULL; | 
|---|
| 908 | Vector *Center = cloud->GetCenter(out); | 
|---|
| 909 | list<BoundaryTriangleSet*> *triangles = NULL; | 
|---|
| 910 |  | 
|---|
| 911 | *out << Verbose(1) << "Begin of InsertStraddlingPoints" << endl; | 
|---|
| 912 |  | 
|---|
| 913 | cloud->GoToFirst(); | 
|---|
| 914 | while (!cloud->IsEnd()) {  // we only have to go once through all points, as boundary can become only bigger | 
|---|
| 915 | Walker = cloud->GetPoint(); | 
|---|
| 916 | *out << Verbose(2) << "Current point is " << *Walker << "." << endl; | 
|---|
| 917 | // get the next triangle | 
|---|
| 918 | triangles = FindClosestTrianglesToPoint(out, Walker->node, LC); | 
|---|
| 919 | if (triangles == NULL) { | 
|---|
| 920 | *out << Verbose(1) << "No triangles found, probably a tesselation point itself." << endl; | 
|---|
| 921 | cloud->GoToNext(); | 
|---|
| 922 | continue; | 
|---|
| 923 | } else { | 
|---|
| 924 | BTS = triangles->front(); | 
|---|
| 925 | } | 
|---|
| 926 | *out << Verbose(2) << "Closest triangle is " << BTS << "." << endl; | 
|---|
| 927 | // get the intersection point | 
|---|
| 928 | if (BTS->GetIntersectionInsideTriangle(out, Center, Walker->node, &Intersection)) { | 
|---|
| 929 | *out << Verbose(2) << "We have an intersection at " << Intersection << "." << endl; | 
|---|
| 930 | // we have the intersection, check whether in- or outside of boundary | 
|---|
| 931 | if ((Center->DistanceSquared(Walker->node) - Center->DistanceSquared(&Intersection)) < -MYEPSILON) { | 
|---|
| 932 | // inside, next! | 
|---|
| 933 | *out << Verbose(4) << Walker << " is inside wrt triangle " << BTS << "." << endl; | 
|---|
| 934 | } else { | 
|---|
| 935 | // outside! | 
|---|
| 936 | *out << Verbose(3) << Walker << " is outside wrt triangle " << BTS << "." << endl; | 
|---|
| 937 | class BoundaryLineSet *OldLines[3], *NewLines[3]; | 
|---|
| 938 | class BoundaryPointSet *OldPoints[3], *NewPoint; | 
|---|
| 939 | // store the three old lines and old points | 
|---|
| 940 | for (int i=0;i<3;i++) { | 
|---|
| 941 | OldLines[i] = BTS->lines[i]; | 
|---|
| 942 | OldPoints[i] = BTS->endpoints[i]; | 
|---|
| 943 | } | 
|---|
| 944 | // add Walker to boundary points | 
|---|
| 945 | AddPoint(Walker); | 
|---|
| 946 | if (BPS[0] == NULL) | 
|---|
| 947 | NewPoint = BPS[0]; | 
|---|
| 948 | else | 
|---|
| 949 | continue; | 
|---|
| 950 | // remove triangle | 
|---|
| 951 | TrianglesOnBoundary.erase(BTS->Nr); | 
|---|
| 952 | // create three new boundary lines | 
|---|
| 953 | for (int i=0;i<3;i++) { | 
|---|
| 954 | BPS[0] = NewPoint; | 
|---|
| 955 | BPS[1] = OldPoints[i]; | 
|---|
| 956 | NewLines[i] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount); | 
|---|
| 957 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, NewLines[i])); // no need for check for unique insertion as BPS[0] is definitely a new one | 
|---|
| 958 | LinesOnBoundaryCount++; | 
|---|
| 959 | } | 
|---|
| 960 | // create three new triangle with new point | 
|---|
| 961 | for (int i=0;i<3;i++) { // find all baselines | 
|---|
| 962 | BLS[0] = OldLines[i]; | 
|---|
| 963 | int n = 1; | 
|---|
| 964 | for (int j=0;j<3;j++) { | 
|---|
| 965 | if (NewLines[j]->IsConnectedTo(BLS[0])) { | 
|---|
| 966 | if (n>2) { | 
|---|
| 967 | *out << Verbose(1) << "ERROR: " << BLS[0] << " connects to all of the new lines?!" << endl; | 
|---|
| 968 | return false; | 
|---|
| 969 | } else | 
|---|
| 970 | BLS[n++] = NewLines[j]; | 
|---|
| 971 | } | 
|---|
| 972 | } | 
|---|
| 973 | // create the triangle | 
|---|
| 974 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 975 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 976 | TrianglesOnBoundaryCount++; | 
|---|
| 977 | } | 
|---|
| 978 | } | 
|---|
| 979 | } else { // something is wrong with FindClosestTriangleToPoint! | 
|---|
| 980 | *out << Verbose(1) << "ERROR: The closest triangle did not produce an intersection!" << endl; | 
|---|
| 981 | return false; | 
|---|
| 982 | } | 
|---|
| 983 | cloud->GoToNext(); | 
|---|
| 984 | } | 
|---|
| 985 |  | 
|---|
| 986 | // exit | 
|---|
| 987 | delete(Center); | 
|---|
| 988 | *out << Verbose(1) << "End of InsertStraddlingPoints" << endl; | 
|---|
| 989 | return true; | 
|---|
| 990 | }; | 
|---|
| 991 |  | 
|---|
| 992 | /** Adds an point to the tesselation::PointsOnBoundary list. | 
|---|
| 993 | * \param *Walker point to add | 
|---|
| 994 | */ | 
|---|
| 995 | void | 
|---|
| 996 | Tesselation::AddPoint(TesselPoint *Walker) | 
|---|
| 997 | { | 
|---|
| 998 | PointTestPair InsertUnique; | 
|---|
| 999 | BPS[0] = new class BoundaryPointSet(Walker); | 
|---|
| 1000 | InsertUnique = PointsOnBoundary.insert(PointPair(Walker->nr, BPS[0])); | 
|---|
| 1001 | if (InsertUnique.second) // if new point was not present before, increase counter | 
|---|
| 1002 | PointsOnBoundaryCount++; | 
|---|
| 1003 | else { | 
|---|
| 1004 | delete(BPS[0]); | 
|---|
| 1005 | BPS[0] = NULL; | 
|---|
| 1006 | } | 
|---|
| 1007 | } | 
|---|
| 1008 | ; | 
|---|
| 1009 |  | 
|---|
| 1010 | /** Adds point to Tesselation::PointsOnBoundary if not yet present. | 
|---|
| 1011 | * Tesselation::TPS is set to either this new BoundaryPointSet or to the existing one of not unique. | 
|---|
| 1012 | * @param Candidate point to add | 
|---|
| 1013 | * @param n index for this point in Tesselation::TPS array | 
|---|
| 1014 | */ | 
|---|
| 1015 | void | 
|---|
| 1016 | Tesselation::AddTrianglePoint(TesselPoint* Candidate, int n) | 
|---|
| 1017 | { | 
|---|
| 1018 | PointTestPair InsertUnique; | 
|---|
| 1019 | TPS[n] = new class BoundaryPointSet(Candidate); | 
|---|
| 1020 | InsertUnique = PointsOnBoundary.insert(PointPair(Candidate->nr, TPS[n])); | 
|---|
| 1021 | if (InsertUnique.second) { // if new point was not present before, increase counter | 
|---|
| 1022 | PointsOnBoundaryCount++; | 
|---|
| 1023 | } else { | 
|---|
| 1024 | delete TPS[n]; | 
|---|
| 1025 | cout << Verbose(3) << "Node " << *((InsertUnique.first)->second->node) << " is already present in PointsOnBoundary." << endl; | 
|---|
| 1026 | TPS[n] = (InsertUnique.first)->second; | 
|---|
| 1027 | } | 
|---|
| 1028 | } | 
|---|
| 1029 | ; | 
|---|
| 1030 |  | 
|---|
| 1031 | /** Function tries to add line from current Points in BPS to BoundaryLineSet. | 
|---|
| 1032 | * If successful it raises the line count and inserts the new line into the BLS, | 
|---|
| 1033 | * if unsuccessful, it writes the line which had been present into the BLS, deleting the new constructed one. | 
|---|
| 1034 | * @param *a first endpoint | 
|---|
| 1035 | * @param *b second endpoint | 
|---|
| 1036 | * @param n index of Tesselation::BLS giving the line with both endpoints | 
|---|
| 1037 | */ | 
|---|
| 1038 | void Tesselation::AddTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n) { | 
|---|
| 1039 | bool insertNewLine = true; | 
|---|
| 1040 |  | 
|---|
| 1041 | if (a->lines.find(b->node->nr) != a->lines.end()) { | 
|---|
| 1042 | LineMap::iterator FindLine; | 
|---|
| 1043 | pair<LineMap::iterator,LineMap::iterator> FindPair; | 
|---|
| 1044 | FindPair = a->lines.equal_range(b->node->nr); | 
|---|
| 1045 |  | 
|---|
| 1046 | for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) { | 
|---|
| 1047 | // If there is a line with less than two attached triangles, we don't need a new line. | 
|---|
| 1048 | if (FindLine->second->TrianglesCount < 2) { | 
|---|
| 1049 | insertNewLine = false; | 
|---|
| 1050 | cout << Verbose(3) << "Using existing line " << *FindLine->second << endl; | 
|---|
| 1051 |  | 
|---|
| 1052 | BPS[0] = FindLine->second->endpoints[0]; | 
|---|
| 1053 | BPS[1] = FindLine->second->endpoints[1]; | 
|---|
| 1054 | BLS[n] = FindLine->second; | 
|---|
| 1055 |  | 
|---|
| 1056 | break; | 
|---|
| 1057 | } | 
|---|
| 1058 | } | 
|---|
| 1059 | } | 
|---|
| 1060 |  | 
|---|
| 1061 | if (insertNewLine) { | 
|---|
| 1062 | AlwaysAddTriangleLine(a, b, n); | 
|---|
| 1063 | } | 
|---|
| 1064 | } | 
|---|
| 1065 | ; | 
|---|
| 1066 |  | 
|---|
| 1067 | /** | 
|---|
| 1068 | * Adds lines from each of the current points in the BPS to BoundaryLineSet. | 
|---|
| 1069 | * Raises the line count and inserts the new line into the BLS. | 
|---|
| 1070 | * | 
|---|
| 1071 | * @param *a first endpoint | 
|---|
| 1072 | * @param *b second endpoint | 
|---|
| 1073 | * @param n index of Tesselation::BLS giving the line with both endpoints | 
|---|
| 1074 | */ | 
|---|
| 1075 | void Tesselation::AlwaysAddTriangleLine(class BoundaryPointSet *a, class BoundaryPointSet *b, int n) | 
|---|
| 1076 | { | 
|---|
| 1077 | cout << Verbose(3) << "Adding line between " << *(a->node) << " and " << *(b->node) << "." << endl; | 
|---|
| 1078 | BPS[0] = a; | 
|---|
| 1079 | BPS[1] = b; | 
|---|
| 1080 | BLS[n] = new class BoundaryLineSet(BPS, LinesOnBoundaryCount);  // this also adds the line to the local maps | 
|---|
| 1081 | // add line to global map | 
|---|
| 1082 | LinesOnBoundary.insert(LinePair(LinesOnBoundaryCount, BLS[n])); | 
|---|
| 1083 | // increase counter | 
|---|
| 1084 | LinesOnBoundaryCount++; | 
|---|
| 1085 | }; | 
|---|
| 1086 |  | 
|---|
| 1087 | /** Function tries to add Triangle just created to Triangle and remarks if already existent (Failure of algorithm). | 
|---|
| 1088 | * Furthermore it adds the triangle to all of its lines, in order to recognize those which are saturated later. | 
|---|
| 1089 | */ | 
|---|
| 1090 | void | 
|---|
| 1091 | Tesselation::AddTriangle() | 
|---|
| 1092 | { | 
|---|
| 1093 | cout << Verbose(1) << "Adding triangle to global TrianglesOnBoundary map." << endl; | 
|---|
| 1094 |  | 
|---|
| 1095 | // add triangle to global map | 
|---|
| 1096 | TrianglesOnBoundary.insert(TrianglePair(TrianglesOnBoundaryCount, BTS)); | 
|---|
| 1097 | TrianglesOnBoundaryCount++; | 
|---|
| 1098 |  | 
|---|
| 1099 | // NOTE: add triangle to local maps is done in constructor of BoundaryTriangleSet | 
|---|
| 1100 | } | 
|---|
| 1101 | ; | 
|---|
| 1102 |  | 
|---|
| 1103 | /** Checks whether the triangle consisting of the three points is already present. | 
|---|
| 1104 | * Searches for the points in Tesselation::PointsOnBoundary and checks their | 
|---|
| 1105 | * lines. If any of the three edges already has two triangles attached, false is | 
|---|
| 1106 | * returned. | 
|---|
| 1107 | * \param *out output stream for debugging | 
|---|
| 1108 | * \param *Candidates endpoints of the triangle candidate | 
|---|
| 1109 | * \return integer 0 if no triangle exists, 1 if one triangle exists, 2 if two | 
|---|
| 1110 | *                 triangles exist which is the maximum for three points | 
|---|
| 1111 | */ | 
|---|
| 1112 | int Tesselation::CheckPresenceOfTriangle(ofstream *out, TesselPoint *Candidates[3]) { | 
|---|
| 1113 | int adjacentTriangleCount = 0; | 
|---|
| 1114 | class BoundaryPointSet *Points[3]; | 
|---|
| 1115 |  | 
|---|
| 1116 | *out << Verbose(2) << "Begin of CheckPresenceOfTriangle" << endl; | 
|---|
| 1117 | // builds a triangle point set (Points) of the end points | 
|---|
| 1118 | for (int i = 0; i < 3; i++) { | 
|---|
| 1119 | PointMap::iterator FindPoint = PointsOnBoundary.find(Candidates[i]->nr); | 
|---|
| 1120 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 1121 | Points[i] = FindPoint->second; | 
|---|
| 1122 | } else { | 
|---|
| 1123 | Points[i] = NULL; | 
|---|
| 1124 | } | 
|---|
| 1125 | } | 
|---|
| 1126 |  | 
|---|
| 1127 | // checks lines between the points in the Points for their adjacent triangles | 
|---|
| 1128 | for (int i = 0; i < 3; i++) { | 
|---|
| 1129 | if (Points[i] != NULL) { | 
|---|
| 1130 | for (int j = i; j < 3; j++) { | 
|---|
| 1131 | if (Points[j] != NULL) { | 
|---|
| 1132 | LineMap::iterator FindLine = Points[i]->lines.find(Points[j]->node->nr); | 
|---|
| 1133 | for (; (FindLine != Points[i]->lines.end()) && (FindLine->first == Points[j]->node->nr); FindLine++) { | 
|---|
| 1134 | TriangleMap *triangles = &FindLine->second->triangles; | 
|---|
| 1135 | *out << Verbose(3) << "Current line is " << FindLine->first << ": " << *(FindLine->second) << " with triangles " << triangles << "." << endl; | 
|---|
| 1136 | for (TriangleMap::iterator FindTriangle = triangles->begin(); FindTriangle != triangles->end(); FindTriangle++) { | 
|---|
| 1137 | if (FindTriangle->second->IsPresentTupel(Points)) { | 
|---|
| 1138 | adjacentTriangleCount++; | 
|---|
| 1139 | } | 
|---|
| 1140 | } | 
|---|
| 1141 | *out << Verbose(3) << "end." << endl; | 
|---|
| 1142 | } | 
|---|
| 1143 | // Only one of the triangle lines must be considered for the triangle count. | 
|---|
| 1144 | *out << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl; | 
|---|
| 1145 | return adjacentTriangleCount; | 
|---|
| 1146 | } | 
|---|
| 1147 | } | 
|---|
| 1148 | } | 
|---|
| 1149 | } | 
|---|
| 1150 |  | 
|---|
| 1151 | *out << Verbose(2) << "Found " << adjacentTriangleCount << " adjacent triangles for the point set." << endl; | 
|---|
| 1152 | *out << Verbose(2) << "End of CheckPresenceOfTriangle" << endl; | 
|---|
| 1153 | return adjacentTriangleCount; | 
|---|
| 1154 | }; | 
|---|
| 1155 |  | 
|---|
| 1156 |  | 
|---|
| 1157 | /** Finds the starting triangle for find_non_convex_border(). | 
|---|
| 1158 | * Looks at the outermost point per axis, then Find_second_point_for_Tesselation() | 
|---|
| 1159 | * for the second and Find_next_suitable_point_via_Angle_of_Sphere() for the third | 
|---|
| 1160 | * point are called. | 
|---|
| 1161 | * \param *out output stream for debugging | 
|---|
| 1162 | * \param RADIUS radius of virtual rolling sphere | 
|---|
| 1163 | * \param *LC LinkedCell structure with neighbouring TesselPoint's | 
|---|
| 1164 | */ | 
|---|
| 1165 | void Tesselation::Find_starting_triangle(ofstream *out, const double RADIUS, LinkedCell *LC) | 
|---|
| 1166 | { | 
|---|
| 1167 | cout << Verbose(1) << "Begin of Find_starting_triangle\n"; | 
|---|
| 1168 | int i = 0; | 
|---|
| 1169 | LinkedNodes *List = NULL; | 
|---|
| 1170 | TesselPoint* FirstPoint = NULL; | 
|---|
| 1171 | TesselPoint* SecondPoint = NULL; | 
|---|
| 1172 | TesselPoint* MaxPoint[NDIM]; | 
|---|
| 1173 | double max_coordinate[NDIM]; | 
|---|
| 1174 | Vector Oben; | 
|---|
| 1175 | Vector helper; | 
|---|
| 1176 | Vector Chord; | 
|---|
| 1177 | Vector SearchDirection; | 
|---|
| 1178 |  | 
|---|
| 1179 | Oben.Zero(); | 
|---|
| 1180 |  | 
|---|
| 1181 | for (i = 0; i < 3; i++) { | 
|---|
| 1182 | MaxPoint[i] = NULL; | 
|---|
| 1183 | max_coordinate[i] = -1; | 
|---|
| 1184 | } | 
|---|
| 1185 |  | 
|---|
| 1186 | // 1. searching topmost point with respect to each axis | 
|---|
| 1187 | for (int i=0;i<NDIM;i++) { // each axis | 
|---|
| 1188 | LC->n[i] = LC->N[i]-1; // current axis is topmost cell | 
|---|
| 1189 | for (LC->n[(i+1)%NDIM]=0;LC->n[(i+1)%NDIM]<LC->N[(i+1)%NDIM];LC->n[(i+1)%NDIM]++) | 
|---|
| 1190 | for (LC->n[(i+2)%NDIM]=0;LC->n[(i+2)%NDIM]<LC->N[(i+2)%NDIM];LC->n[(i+2)%NDIM]++) { | 
|---|
| 1191 | List = LC->GetCurrentCell(); | 
|---|
| 1192 | //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 1193 | if (List != NULL) { | 
|---|
| 1194 | for (LinkedNodes::iterator Runner = List->begin();Runner != List->end();Runner++) { | 
|---|
| 1195 | if ((*Runner)->node->x[i] > max_coordinate[i]) { | 
|---|
| 1196 | cout << Verbose(2) << "New maximal for axis " << i << " node is " << *(*Runner) << " at " << *(*Runner)->node << "." << endl; | 
|---|
| 1197 | max_coordinate[i] = (*Runner)->node->x[i]; | 
|---|
| 1198 | MaxPoint[i] = (*Runner); | 
|---|
| 1199 | } | 
|---|
| 1200 | } | 
|---|
| 1201 | } else { | 
|---|
| 1202 | cerr << "ERROR: The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << " is invalid!" << endl; | 
|---|
| 1203 | } | 
|---|
| 1204 | } | 
|---|
| 1205 | } | 
|---|
| 1206 |  | 
|---|
| 1207 | cout << Verbose(2) << "Found maximum coordinates: "; | 
|---|
| 1208 | for (int i=0;i<NDIM;i++) | 
|---|
| 1209 | cout << i << ": " << *MaxPoint[i] << "\t"; | 
|---|
| 1210 | cout << endl; | 
|---|
| 1211 |  | 
|---|
| 1212 | BTS = NULL; | 
|---|
| 1213 | CandidateList *Opt_Candidates = new CandidateList(); | 
|---|
| 1214 | for (int k=0;k<NDIM;k++) { | 
|---|
| 1215 | Oben.x[k] = 1.; | 
|---|
| 1216 | FirstPoint = MaxPoint[k]; | 
|---|
| 1217 | cout << Verbose(1) << "Coordinates of start node at " << *FirstPoint->node << "." << endl; | 
|---|
| 1218 |  | 
|---|
| 1219 | double ShortestAngle; | 
|---|
| 1220 | TesselPoint* Opt_Candidate = NULL; | 
|---|
| 1221 | 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. | 
|---|
| 1222 |  | 
|---|
| 1223 | Find_second_point_for_Tesselation(FirstPoint, NULL, Oben, Opt_Candidate, &ShortestAngle, RADIUS, LC); // we give same point as next candidate as its bonds are looked into in find_second_... | 
|---|
| 1224 | SecondPoint = Opt_Candidate; | 
|---|
| 1225 | if (SecondPoint == NULL)  // have we found a second point? | 
|---|
| 1226 | continue; | 
|---|
| 1227 | else | 
|---|
| 1228 | cout << Verbose(1) << "Found second point is at " << *SecondPoint->node << ".\n"; | 
|---|
| 1229 |  | 
|---|
| 1230 | helper.CopyVector(FirstPoint->node); | 
|---|
| 1231 | helper.SubtractVector(SecondPoint->node); | 
|---|
| 1232 | helper.Normalize(); | 
|---|
| 1233 | Oben.ProjectOntoPlane(&helper); | 
|---|
| 1234 | Oben.Normalize(); | 
|---|
| 1235 | helper.VectorProduct(&Oben); | 
|---|
| 1236 | ShortestAngle = 2.*M_PI; // This will indicate the quadrant. | 
|---|
| 1237 |  | 
|---|
| 1238 | Chord.CopyVector(FirstPoint->node); // bring into calling function | 
|---|
| 1239 | Chord.SubtractVector(SecondPoint->node); | 
|---|
| 1240 | double radius = Chord.ScalarProduct(&Chord); | 
|---|
| 1241 | double CircleRadius = sqrt(RADIUS*RADIUS - radius/4.); | 
|---|
| 1242 | helper.CopyVector(&Oben); | 
|---|
| 1243 | helper.Scale(CircleRadius); | 
|---|
| 1244 | // Now, oben and helper are two orthonormalized vectors in the plane defined by Chord (not normalized) | 
|---|
| 1245 |  | 
|---|
| 1246 | // look in one direction of baseline for initial candidate | 
|---|
| 1247 | SearchDirection.MakeNormalVector(&Chord, &Oben);  // whether we look "left" first or "right" first is not important ... | 
|---|
| 1248 |  | 
|---|
| 1249 | // adding point 1 and point 2 and the line between them | 
|---|
| 1250 | AddTrianglePoint(FirstPoint, 0); | 
|---|
| 1251 | AddTrianglePoint(SecondPoint, 1); | 
|---|
| 1252 | AddTriangleLine(TPS[0], TPS[1], 0); | 
|---|
| 1253 |  | 
|---|
| 1254 | //cout << Verbose(2) << "INFO: OldSphereCenter is at " << helper << ".\n"; | 
|---|
| 1255 | Find_third_point_for_Tesselation( | 
|---|
| 1256 | Oben, SearchDirection, helper, BLS[0], NULL, *&Opt_Candidates, &ShortestAngle, RADIUS, LC | 
|---|
| 1257 | ); | 
|---|
| 1258 | cout << Verbose(1) << "List of third Points is "; | 
|---|
| 1259 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1260 | cout << " " << *(*it)->point; | 
|---|
| 1261 | } | 
|---|
| 1262 | cout << endl; | 
|---|
| 1263 |  | 
|---|
| 1264 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1265 | // add third triangle point | 
|---|
| 1266 | AddTrianglePoint((*it)->point, 2); | 
|---|
| 1267 | // add the second and third line | 
|---|
| 1268 | AddTriangleLine(TPS[1], TPS[2], 1); | 
|---|
| 1269 | AddTriangleLine(TPS[0], TPS[2], 2); | 
|---|
| 1270 | // ... and triangles to the Maps of the Tesselation class | 
|---|
| 1271 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1272 | AddTriangle(); | 
|---|
| 1273 | // ... and calculate its normal vector (with correct orientation) | 
|---|
| 1274 | (*it)->OptCenter.Scale(-1.); | 
|---|
| 1275 | cout << Verbose(2) << "Anti-Oben is currently " << (*it)->OptCenter << "." << endl; | 
|---|
| 1276 | BTS->GetNormalVector((*it)->OptCenter);  // vector to compare with should point inwards | 
|---|
| 1277 | cout << Verbose(0) << "==> Found starting triangle consists of " << *FirstPoint << ", " << *SecondPoint << " and " | 
|---|
| 1278 | << *(*it)->point << " with normal vector " << BTS->NormalVector << ".\n"; | 
|---|
| 1279 |  | 
|---|
| 1280 | // if we do not reach the end with the next step of iteration, we need to setup a new first line | 
|---|
| 1281 | if (it != Opt_Candidates->end()--) { | 
|---|
| 1282 | FirstPoint = (*it)->BaseLine->endpoints[0]->node; | 
|---|
| 1283 | SecondPoint = (*it)->point; | 
|---|
| 1284 | // adding point 1 and point 2 and the line between them | 
|---|
| 1285 | AddTrianglePoint(FirstPoint, 0); | 
|---|
| 1286 | AddTrianglePoint(SecondPoint, 1); | 
|---|
| 1287 | AddTriangleLine(TPS[0], TPS[1], 0); | 
|---|
| 1288 | } | 
|---|
| 1289 | cout << Verbose(2) << "Projection is " << BTS->NormalVector.Projection(&Oben) << "." << endl; | 
|---|
| 1290 | } | 
|---|
| 1291 | if (BTS != NULL) // we have created one starting triangle | 
|---|
| 1292 | break; | 
|---|
| 1293 | else { | 
|---|
| 1294 | // remove all candidates from the list and then the list itself | 
|---|
| 1295 | class CandidateForTesselation *remover = NULL; | 
|---|
| 1296 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1297 | remover = *it; | 
|---|
| 1298 | delete(remover); | 
|---|
| 1299 | } | 
|---|
| 1300 | Opt_Candidates->clear(); | 
|---|
| 1301 | } | 
|---|
| 1302 | } | 
|---|
| 1303 |  | 
|---|
| 1304 | // remove all candidates from the list and then the list itself | 
|---|
| 1305 | class CandidateForTesselation *remover = NULL; | 
|---|
| 1306 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1307 | remover = *it; | 
|---|
| 1308 | delete(remover); | 
|---|
| 1309 | } | 
|---|
| 1310 | delete(Opt_Candidates); | 
|---|
| 1311 | cout << Verbose(1) << "End of Find_starting_triangle\n"; | 
|---|
| 1312 | }; | 
|---|
| 1313 |  | 
|---|
| 1314 |  | 
|---|
| 1315 | /** This function finds a triangle to a line, adjacent to an existing one. | 
|---|
| 1316 | * @param out output stream for debugging | 
|---|
| 1317 | * @param Line current baseline to search from | 
|---|
| 1318 | * @param T current triangle which \a Line is edge of | 
|---|
| 1319 | * @param RADIUS radius of the rolling ball | 
|---|
| 1320 | * @param N number of found triangles | 
|---|
| 1321 | * @param *LC LinkedCell structure with neighbouring points | 
|---|
| 1322 | */ | 
|---|
| 1323 | bool Tesselation::Find_next_suitable_triangle(ofstream *out, BoundaryLineSet &Line, BoundaryTriangleSet &T, const double& RADIUS, int N, LinkedCell *LC) | 
|---|
| 1324 | { | 
|---|
| 1325 | cout << Verbose(0) << "Begin of Find_next_suitable_triangle\n"; | 
|---|
| 1326 | bool result = true; | 
|---|
| 1327 | CandidateList *Opt_Candidates = new CandidateList(); | 
|---|
| 1328 |  | 
|---|
| 1329 | Vector CircleCenter; | 
|---|
| 1330 | Vector CirclePlaneNormal; | 
|---|
| 1331 | Vector OldSphereCenter; | 
|---|
| 1332 | Vector SearchDirection; | 
|---|
| 1333 | Vector helper; | 
|---|
| 1334 | TesselPoint *ThirdNode = NULL; | 
|---|
| 1335 | LineMap::iterator testline; | 
|---|
| 1336 | double ShortestAngle = 2.*M_PI; // This will indicate the quadrant. | 
|---|
| 1337 | double radius, CircleRadius; | 
|---|
| 1338 |  | 
|---|
| 1339 | cout << Verbose(1) << "Current baseline is " << Line << " of triangle " << T << "." << endl; | 
|---|
| 1340 | for (int i=0;i<3;i++) | 
|---|
| 1341 | if ((T.endpoints[i]->node != Line.endpoints[0]->node) && (T.endpoints[i]->node != Line.endpoints[1]->node)) | 
|---|
| 1342 | ThirdNode = T.endpoints[i]->node; | 
|---|
| 1343 |  | 
|---|
| 1344 | // construct center of circle | 
|---|
| 1345 | CircleCenter.CopyVector(Line.endpoints[0]->node->node); | 
|---|
| 1346 | CircleCenter.AddVector(Line.endpoints[1]->node->node); | 
|---|
| 1347 | CircleCenter.Scale(0.5); | 
|---|
| 1348 |  | 
|---|
| 1349 | // construct normal vector of circle | 
|---|
| 1350 | CirclePlaneNormal.CopyVector(Line.endpoints[0]->node->node); | 
|---|
| 1351 | CirclePlaneNormal.SubtractVector(Line.endpoints[1]->node->node); | 
|---|
| 1352 |  | 
|---|
| 1353 | // calculate squared radius of circle | 
|---|
| 1354 | radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal); | 
|---|
| 1355 | if (radius/4. < RADIUS*RADIUS) { | 
|---|
| 1356 | CircleRadius = RADIUS*RADIUS - radius/4.; | 
|---|
| 1357 | CirclePlaneNormal.Normalize(); | 
|---|
| 1358 | cout << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl; | 
|---|
| 1359 |  | 
|---|
| 1360 | // construct old center | 
|---|
| 1361 | GetCenterofCircumcircle(&OldSphereCenter, T.endpoints[0]->node->node, T.endpoints[1]->node->node, T.endpoints[2]->node->node); | 
|---|
| 1362 | helper.CopyVector(&T.NormalVector);  // normal vector ensures that this is correct center of the two possible ones | 
|---|
| 1363 | radius = Line.endpoints[0]->node->node->DistanceSquared(&OldSphereCenter); | 
|---|
| 1364 | helper.Scale(sqrt(RADIUS*RADIUS - radius)); | 
|---|
| 1365 | OldSphereCenter.AddVector(&helper); | 
|---|
| 1366 | OldSphereCenter.SubtractVector(&CircleCenter); | 
|---|
| 1367 | //cout << Verbose(2) << "INFO: OldSphereCenter is at " << OldSphereCenter << "." << endl; | 
|---|
| 1368 |  | 
|---|
| 1369 | // construct SearchDirection | 
|---|
| 1370 | SearchDirection.MakeNormalVector(&T.NormalVector, &CirclePlaneNormal); | 
|---|
| 1371 | helper.CopyVector(Line.endpoints[0]->node->node); | 
|---|
| 1372 | helper.SubtractVector(ThirdNode->node); | 
|---|
| 1373 | if (helper.ScalarProduct(&SearchDirection) < -HULLEPSILON)// ohoh, SearchDirection points inwards! | 
|---|
| 1374 | SearchDirection.Scale(-1.); | 
|---|
| 1375 | SearchDirection.ProjectOntoPlane(&OldSphereCenter); | 
|---|
| 1376 | SearchDirection.Normalize(); | 
|---|
| 1377 | cout << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl; | 
|---|
| 1378 | if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) { | 
|---|
| 1379 | // rotated the wrong way! | 
|---|
| 1380 | cerr << "ERROR: SearchDirection and RelativeOldSphereCenter are still not orthogonal!" << endl; | 
|---|
| 1381 | } | 
|---|
| 1382 |  | 
|---|
| 1383 | // add third point | 
|---|
| 1384 | Find_third_point_for_Tesselation( | 
|---|
| 1385 | T.NormalVector, SearchDirection, OldSphereCenter, &Line, ThirdNode, Opt_Candidates, | 
|---|
| 1386 | &ShortestAngle, RADIUS, LC | 
|---|
| 1387 | ); | 
|---|
| 1388 |  | 
|---|
| 1389 | } else { | 
|---|
| 1390 | cout << Verbose(1) << "Circumcircle for base line " << Line << " and base triangle " << T << " is too big!" << endl; | 
|---|
| 1391 | } | 
|---|
| 1392 |  | 
|---|
| 1393 | if (Opt_Candidates->begin() == Opt_Candidates->end()) { | 
|---|
| 1394 | cerr << "WARNING: Could not find a suitable candidate." << endl; | 
|---|
| 1395 | return false; | 
|---|
| 1396 | } | 
|---|
| 1397 | cout << Verbose(1) << "Third Points are "; | 
|---|
| 1398 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1399 | cout << " " << *(*it)->point; | 
|---|
| 1400 | } | 
|---|
| 1401 | cout << endl; | 
|---|
| 1402 |  | 
|---|
| 1403 | BoundaryLineSet *BaseRay = &Line; | 
|---|
| 1404 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1405 | cout << Verbose(1) << " Third point candidate is " << *(*it)->point | 
|---|
| 1406 | << " with circumsphere's center at " << (*it)->OptCenter << "." << endl; | 
|---|
| 1407 | cout << Verbose(1) << " Baseline is " << *BaseRay << endl; | 
|---|
| 1408 |  | 
|---|
| 1409 | // check whether all edges of the new triangle still have space for one more triangle (i.e. TriangleCount <2) | 
|---|
| 1410 | TesselPoint *PointCandidates[3]; | 
|---|
| 1411 | PointCandidates[0] = (*it)->point; | 
|---|
| 1412 | PointCandidates[1] = BaseRay->endpoints[0]->node; | 
|---|
| 1413 | PointCandidates[2] = BaseRay->endpoints[1]->node; | 
|---|
| 1414 | int existentTrianglesCount = CheckPresenceOfTriangle(out, PointCandidates); | 
|---|
| 1415 |  | 
|---|
| 1416 | BTS = NULL; | 
|---|
| 1417 | // If there is no triangle, add it regularly. | 
|---|
| 1418 | if (existentTrianglesCount == 0) { | 
|---|
| 1419 | AddTrianglePoint((*it)->point, 0); | 
|---|
| 1420 | AddTrianglePoint(BaseRay->endpoints[0]->node, 1); | 
|---|
| 1421 | AddTrianglePoint(BaseRay->endpoints[1]->node, 2); | 
|---|
| 1422 |  | 
|---|
| 1423 | if (CheckLineCriteriaforDegeneratedTriangle(TPS)) { | 
|---|
| 1424 | AddTriangleLine(TPS[0], TPS[1], 0); | 
|---|
| 1425 | AddTriangleLine(TPS[0], TPS[2], 1); | 
|---|
| 1426 | AddTriangleLine(TPS[1], TPS[2], 2); | 
|---|
| 1427 |  | 
|---|
| 1428 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1429 | AddTriangle(); | 
|---|
| 1430 | (*it)->OptCenter.Scale(-1.); | 
|---|
| 1431 | BTS->GetNormalVector((*it)->OptCenter); | 
|---|
| 1432 | (*it)->OptCenter.Scale(-1.); | 
|---|
| 1433 |  | 
|---|
| 1434 | cout << "--> New triangle with " << *BTS << " and normal vector " << BTS->NormalVector | 
|---|
| 1435 | << " for this triangle ... " << endl; | 
|---|
| 1436 | //cout << Verbose(1) << "We have "<< TrianglesOnBoundaryCount << " for line " << *BaseRay << "." << endl; | 
|---|
| 1437 | } else { | 
|---|
| 1438 | cout << Verbose(1) << "WARNING: This triangle consisting of "; | 
|---|
| 1439 | cout << *(*it)->point << ", "; | 
|---|
| 1440 | cout << *BaseRay->endpoints[0]->node << " and "; | 
|---|
| 1441 | cout << *BaseRay->endpoints[1]->node << " "; | 
|---|
| 1442 | cout << "exists and is not added, as it does not seem helpful!" << endl; | 
|---|
| 1443 | result = false; | 
|---|
| 1444 | } | 
|---|
| 1445 | } else if (existentTrianglesCount == 1) { // If there is a planar region within the structure, we need this triangle a second time. | 
|---|
| 1446 | AddTrianglePoint((*it)->point, 0); | 
|---|
| 1447 | AddTrianglePoint(BaseRay->endpoints[0]->node, 1); | 
|---|
| 1448 | AddTrianglePoint(BaseRay->endpoints[1]->node, 2); | 
|---|
| 1449 |  | 
|---|
| 1450 | // 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) | 
|---|
| 1451 | // i.e. at least one of the three lines must be present with TriangleCount <= 1 | 
|---|
| 1452 | if (CheckLineCriteriaforDegeneratedTriangle(TPS)) { | 
|---|
| 1453 | AddTriangleLine(TPS[0], TPS[1], 0); | 
|---|
| 1454 | AddTriangleLine(TPS[0], TPS[2], 1); | 
|---|
| 1455 | AddTriangleLine(TPS[1], TPS[2], 2); | 
|---|
| 1456 |  | 
|---|
| 1457 | BTS = new class BoundaryTriangleSet(BLS, TrianglesOnBoundaryCount); | 
|---|
| 1458 | AddTriangle();  // add to global map | 
|---|
| 1459 |  | 
|---|
| 1460 | (*it)->OtherOptCenter.Scale(-1.); | 
|---|
| 1461 | BTS->GetNormalVector((*it)->OtherOptCenter); | 
|---|
| 1462 | (*it)->OtherOptCenter.Scale(-1.); | 
|---|
| 1463 |  | 
|---|
| 1464 | cout << "--> WARNING: Special new triangle with " << *BTS << " and normal vector " << BTS->NormalVector | 
|---|
| 1465 | << " for this triangle ... " << endl; | 
|---|
| 1466 | cout << Verbose(1) << "We have "<< BaseRay->TrianglesCount << " for line " << BaseRay << "." << endl; | 
|---|
| 1467 | } else { | 
|---|
| 1468 | cout << Verbose(1) << "WARNING: This triangle consisting of "; | 
|---|
| 1469 | cout << *(*it)->point << ", "; | 
|---|
| 1470 | cout << *BaseRay->endpoints[0]->node << " and "; | 
|---|
| 1471 | cout << *BaseRay->endpoints[1]->node << " "; | 
|---|
| 1472 | cout << "exists and is not added, as it does not seem helpful!" << endl; | 
|---|
| 1473 | result = false; | 
|---|
| 1474 | } | 
|---|
| 1475 | } else { | 
|---|
| 1476 | cout << Verbose(1) << "This triangle consisting of "; | 
|---|
| 1477 | cout << *(*it)->point << ", "; | 
|---|
| 1478 | cout << *BaseRay->endpoints[0]->node << " and "; | 
|---|
| 1479 | cout << *BaseRay->endpoints[1]->node << " "; | 
|---|
| 1480 | cout << "is invalid!" << endl; | 
|---|
| 1481 | result = false; | 
|---|
| 1482 | } | 
|---|
| 1483 |  | 
|---|
| 1484 | // set baseline to new ray from ref point (here endpoints[0]->node) to current candidate (here (*it)->point)) | 
|---|
| 1485 | BaseRay = BLS[0]; | 
|---|
| 1486 | } | 
|---|
| 1487 |  | 
|---|
| 1488 | // remove all candidates from the list and then the list itself | 
|---|
| 1489 | class CandidateForTesselation *remover = NULL; | 
|---|
| 1490 | for (CandidateList::iterator it = Opt_Candidates->begin(); it != Opt_Candidates->end(); ++it) { | 
|---|
| 1491 | remover = *it; | 
|---|
| 1492 | delete(remover); | 
|---|
| 1493 | } | 
|---|
| 1494 | delete(Opt_Candidates); | 
|---|
| 1495 | cout << Verbose(0) << "End of Find_next_suitable_triangle\n"; | 
|---|
| 1496 | return result; | 
|---|
| 1497 | }; | 
|---|
| 1498 |  | 
|---|
| 1499 |  | 
|---|
| 1500 | /** Goes over all baselines and checks whether adjacent triangles and convex to each other. | 
|---|
| 1501 | * \param *out output stream for debugging | 
|---|
| 1502 | * \return true - all baselines were corrected, false - there are still concave pieces | 
|---|
| 1503 | */ | 
|---|
| 1504 | bool Tesselation::CorrectConcaveBaselines(ofstream *out) | 
|---|
| 1505 | { | 
|---|
| 1506 | class BoundaryLineSet *OldLines[4], *NewLine; | 
|---|
| 1507 | class BoundaryPointSet *OldPoints[2]; | 
|---|
| 1508 | Vector BaseLineNormal; | 
|---|
| 1509 | class BoundaryLineSet *Base = NULL; | 
|---|
| 1510 | int OldTriangles[2], OldBaseLine; | 
|---|
| 1511 | int i,m; | 
|---|
| 1512 |  | 
|---|
| 1513 | *out << Verbose(1) << "Begin of CorrectConcaveBaselines" << endl; | 
|---|
| 1514 |  | 
|---|
| 1515 | for (LineMap::iterator baseline = LinesOnBoundary.begin(); baseline != LinesOnBoundary.end(); baseline++) { | 
|---|
| 1516 | Base = baseline->second; | 
|---|
| 1517 | *out << Verbose(2) << "Current baseline is " << *Base << " ... " << endl; | 
|---|
| 1518 | // check convexity | 
|---|
| 1519 | if (Base->CheckConvexityCriterion(out)) { // triangles are convex | 
|---|
| 1520 | *out << Verbose(2) << "... has two convex triangles." << endl; | 
|---|
| 1521 | } else { // not convex! | 
|---|
| 1522 | *out << Verbose(2) << "... has two concave triangles!" << endl; | 
|---|
| 1523 | // get the two triangles | 
|---|
| 1524 | // gather four endpoints and four lines | 
|---|
| 1525 | for (int j=0;j<4;j++) | 
|---|
| 1526 | OldLines[j] = NULL; | 
|---|
| 1527 | for (int j=0;j<2;j++) | 
|---|
| 1528 | OldPoints[j] = NULL; | 
|---|
| 1529 | i=0; | 
|---|
| 1530 | m=0; | 
|---|
| 1531 | *out << Verbose(3) << "The four old lines are: "; | 
|---|
| 1532 | for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 1533 | for (int j=0;j<3;j++) // all of their endpoints and baselines | 
|---|
| 1534 | if (runner->second->lines[j] != Base) { // pick not the central baseline | 
|---|
| 1535 | OldLines[i++] = runner->second->lines[j]; | 
|---|
| 1536 | *out << *runner->second->lines[j] << "\t"; | 
|---|
| 1537 | } | 
|---|
| 1538 | *out << endl; | 
|---|
| 1539 | *out << Verbose(3) << "The two old points are: "; | 
|---|
| 1540 | for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) | 
|---|
| 1541 | for (int j=0;j<3;j++) // all of their endpoints and baselines | 
|---|
| 1542 | if (!Base->ContainsBoundaryPoint(runner->second->endpoints[j])) { // and neither of its endpoints | 
|---|
| 1543 | OldPoints[m++] = runner->second->endpoints[j]; | 
|---|
| 1544 | *out << *runner->second->endpoints[j] << "\t"; | 
|---|
| 1545 | } | 
|---|
| 1546 | *out << endl; | 
|---|
| 1547 | if (i<4) { | 
|---|
| 1548 | *out << Verbose(1) << "ERROR: We have not gathered enough baselines!" << endl; | 
|---|
| 1549 | return false; | 
|---|
| 1550 | } | 
|---|
| 1551 | for (int j=0;j<4;j++) | 
|---|
| 1552 | if (OldLines[j] == NULL) { | 
|---|
| 1553 | *out << Verbose(1) << "ERROR: We have not gathered enough baselines!" << endl; | 
|---|
| 1554 | return false; | 
|---|
| 1555 | } | 
|---|
| 1556 | for (int j=0;j<2;j++) | 
|---|
| 1557 | if (OldPoints[j] == NULL) { | 
|---|
| 1558 | *out << Verbose(1) << "ERROR: We have not gathered enough endpoints!" << endl; | 
|---|
| 1559 | return false; | 
|---|
| 1560 | } | 
|---|
| 1561 |  | 
|---|
| 1562 | // remove triangles and baseline removes itself | 
|---|
| 1563 | m=0; | 
|---|
| 1564 | OldBaseLine = Base->Nr; | 
|---|
| 1565 | LinesOnBoundary.erase(OldBaseLine); | 
|---|
| 1566 | for(TriangleMap::iterator runner = Base->triangles.begin(); runner != Base->triangles.end(); runner++) { | 
|---|
| 1567 | TrianglesOnBoundary.erase(OldTriangles[m++] = runner->second->Nr); | 
|---|
| 1568 | delete(runner->second); | 
|---|
| 1569 | } | 
|---|
| 1570 |  | 
|---|
| 1571 | // construct new baseline (with same number as old one) | 
|---|
| 1572 | BPS[0] = OldPoints[0]; | 
|---|
| 1573 | BPS[1] = OldPoints[1]; | 
|---|
| 1574 | NewLine = new class BoundaryLineSet(BPS, OldBaseLine); | 
|---|
| 1575 | LinesOnBoundary.insert(LinePair(OldBaseLine, NewLine)); // no need for check for unique insertion as NewLine is definitely a new one | 
|---|
| 1576 |  | 
|---|
| 1577 | // construct new triangles with flipped baseline | 
|---|
| 1578 | i=-1; | 
|---|
| 1579 | if (OldLines[0]->IsConnectedTo(OldLines[2])) | 
|---|
| 1580 | i=2; | 
|---|
| 1581 | if (OldLines[0]->IsConnectedTo(OldLines[3])) | 
|---|
| 1582 | i=3; | 
|---|
| 1583 | if (i!=-1) { | 
|---|
| 1584 | BLS[0] = OldLines[0]; | 
|---|
| 1585 | BLS[1] = OldLines[i]; | 
|---|
| 1586 | BLS[2] = NewLine; | 
|---|
| 1587 | BTS = new class BoundaryTriangleSet(BLS, OldTriangles[0]); | 
|---|
| 1588 | TrianglesOnBoundary.insert(TrianglePair(OldTriangles[0], BTS)); | 
|---|
| 1589 |  | 
|---|
| 1590 | BLS[0] = (i==2 ? OldLines[3] : OldLines[2]); | 
|---|
| 1591 | BLS[1] = OldLines[1]; | 
|---|
| 1592 | BLS[2] = NewLine; | 
|---|
| 1593 | BTS = new class BoundaryTriangleSet(BLS, OldTriangles[1]); | 
|---|
| 1594 | TrianglesOnBoundary.insert(TrianglePair(OldTriangles[1], BTS)); | 
|---|
| 1595 | } else { | 
|---|
| 1596 | *out << Verbose(1) << "The four old lines do not connect, something's utterly wrong here!" << endl; | 
|---|
| 1597 | return false; | 
|---|
| 1598 | } | 
|---|
| 1599 | } | 
|---|
| 1600 | } | 
|---|
| 1601 | *out << Verbose(1) << "End of CorrectConcaveBaselines" << endl; | 
|---|
| 1602 | return true; | 
|---|
| 1603 | }; | 
|---|
| 1604 |  | 
|---|
| 1605 | /** Finds the second point of starting triangle. | 
|---|
| 1606 | * \param *a first node | 
|---|
| 1607 | * \param *Candidate pointer to candidate node on return | 
|---|
| 1608 | * \param Oben vector indicating the outside | 
|---|
| 1609 | * \param Opt_Candidate reference to recommended candidate on return | 
|---|
| 1610 | * \param Storage[3] array storing angles and other candidate information | 
|---|
| 1611 | * \param RADIUS radius of virtual sphere | 
|---|
| 1612 | * \param *LC LinkedCell structure with neighbouring points | 
|---|
| 1613 | */ | 
|---|
| 1614 | void Tesselation::Find_second_point_for_Tesselation(TesselPoint* a, TesselPoint* Candidate, Vector Oben, TesselPoint*& Opt_Candidate, double Storage[3], double RADIUS, LinkedCell *LC) | 
|---|
| 1615 | { | 
|---|
| 1616 | cout << Verbose(2) << "Begin of Find_second_point_for_Tesselation" << endl; | 
|---|
| 1617 | Vector AngleCheck; | 
|---|
| 1618 | double norm = -1., angle; | 
|---|
| 1619 | LinkedNodes *List = NULL; | 
|---|
| 1620 | int N[NDIM], Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 1621 |  | 
|---|
| 1622 | if (LC->SetIndexToNode(a)) {  // get cell for the starting point | 
|---|
| 1623 | for(int i=0;i<NDIM;i++) // store indices of this cell | 
|---|
| 1624 | N[i] = LC->n[i]; | 
|---|
| 1625 | } else { | 
|---|
| 1626 | cerr << "ERROR: Point " << *a << " is not found in cell " << LC->index << "." << endl; | 
|---|
| 1627 | return; | 
|---|
| 1628 | } | 
|---|
| 1629 | // then go through the current and all neighbouring cells and check the contained points for possible candidates | 
|---|
| 1630 | cout << Verbose(3) << "LC Intervals from ["; | 
|---|
| 1631 | for (int i=0;i<NDIM;i++) { | 
|---|
| 1632 | cout << " " << N[i] << "<->" << LC->N[i]; | 
|---|
| 1633 | } | 
|---|
| 1634 | cout << "] :"; | 
|---|
| 1635 | for (int i=0;i<NDIM;i++) { | 
|---|
| 1636 | Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0; | 
|---|
| 1637 | Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1; | 
|---|
| 1638 | cout << " [" << Nlower[i] << "," << Nupper[i] << "] "; | 
|---|
| 1639 | } | 
|---|
| 1640 | cout << endl; | 
|---|
| 1641 |  | 
|---|
| 1642 |  | 
|---|
| 1643 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 1644 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 1645 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 1646 | List = LC->GetCurrentCell(); | 
|---|
| 1647 | //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 1648 | if (List != NULL) { | 
|---|
| 1649 | for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 1650 | Candidate = (*Runner); | 
|---|
| 1651 | // check if we only have one unique point yet ... | 
|---|
| 1652 | if (a != Candidate) { | 
|---|
| 1653 | // Calculate center of the circle with radius RADIUS through points a and Candidate | 
|---|
| 1654 | Vector OrthogonalizedOben, a_Candidate, Center; | 
|---|
| 1655 | double distance, scaleFactor; | 
|---|
| 1656 |  | 
|---|
| 1657 | OrthogonalizedOben.CopyVector(&Oben); | 
|---|
| 1658 | a_Candidate.CopyVector(a->node); | 
|---|
| 1659 | a_Candidate.SubtractVector(Candidate->node); | 
|---|
| 1660 | OrthogonalizedOben.ProjectOntoPlane(&a_Candidate); | 
|---|
| 1661 | OrthogonalizedOben.Normalize(); | 
|---|
| 1662 | distance = 0.5 * a_Candidate.Norm(); | 
|---|
| 1663 | scaleFactor = sqrt(((RADIUS * RADIUS) - (distance * distance))); | 
|---|
| 1664 | OrthogonalizedOben.Scale(scaleFactor); | 
|---|
| 1665 |  | 
|---|
| 1666 | Center.CopyVector(Candidate->node); | 
|---|
| 1667 | Center.AddVector(a->node); | 
|---|
| 1668 | Center.Scale(0.5); | 
|---|
| 1669 | Center.AddVector(&OrthogonalizedOben); | 
|---|
| 1670 |  | 
|---|
| 1671 | AngleCheck.CopyVector(&Center); | 
|---|
| 1672 | AngleCheck.SubtractVector(a->node); | 
|---|
| 1673 | norm = a_Candidate.Norm(); | 
|---|
| 1674 | // second point shall have smallest angle with respect to Oben vector | 
|---|
| 1675 | if (norm < RADIUS*2.) { | 
|---|
| 1676 | angle = AngleCheck.Angle(&Oben); | 
|---|
| 1677 | if (angle < Storage[0]) { | 
|---|
| 1678 | //cout << Verbose(3) << "Old values of Storage: %lf %lf \n", Storage[0], Storage[1]); | 
|---|
| 1679 | cout << Verbose(3) << "Current candidate is " << *Candidate << ": Is a better candidate with distance " << norm << " and angle " << angle << " to oben " << Oben << ".\n"; | 
|---|
| 1680 | Opt_Candidate = Candidate; | 
|---|
| 1681 | Storage[0] = angle; | 
|---|
| 1682 | //cout << Verbose(3) << "Changing something in Storage: %lf %lf. \n", Storage[0], Storage[2]); | 
|---|
| 1683 | } else { | 
|---|
| 1684 | //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Looses with angle " << angle << " to a better candidate " << *Opt_Candidate << endl; | 
|---|
| 1685 | } | 
|---|
| 1686 | } else { | 
|---|
| 1687 | //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Refused due to Radius " << norm << endl; | 
|---|
| 1688 | } | 
|---|
| 1689 | } else { | 
|---|
| 1690 | //cout << Verbose(3) << "Current candidate is " << *Candidate << ": Candidate is equal to first endpoint." << *a << "." << endl; | 
|---|
| 1691 | } | 
|---|
| 1692 | } | 
|---|
| 1693 | } else { | 
|---|
| 1694 | cout << Verbose(3) << "Linked cell list is empty." << endl; | 
|---|
| 1695 | } | 
|---|
| 1696 | } | 
|---|
| 1697 | cout << Verbose(2) << "End of Find_second_point_for_Tesselation" << endl; | 
|---|
| 1698 | }; | 
|---|
| 1699 |  | 
|---|
| 1700 |  | 
|---|
| 1701 | /** This recursive function finds a third point, to form a triangle with two given ones. | 
|---|
| 1702 | * Note that this function is for the starting triangle. | 
|---|
| 1703 | * The idea is as follows: A sphere with fixed radius is (almost) uniquely defined in space by three points | 
|---|
| 1704 | * that sit on its boundary. Hence, when two points are given and we look for the (next) third point, then | 
|---|
| 1705 | * the center of the sphere is still fixed up to a single parameter. The band of possible values | 
|---|
| 1706 | * describes a circle in 3D-space. The old center of the sphere for the current base triangle gives | 
|---|
| 1707 | * us the "null" on this circle, the new center of the candidate point will be some way along this | 
|---|
| 1708 | * circle. The shorter the way the better is the candidate. Note that the direction is clearly given | 
|---|
| 1709 | * by the normal vector of the base triangle that always points outwards by construction. | 
|---|
| 1710 | * Hence, we construct a Center of this circle which sits right in the middle of the current base line. | 
|---|
| 1711 | * We construct the normal vector that defines the plane this circle lies in, it is just in the | 
|---|
| 1712 | * direction of the baseline. And finally, we need the radius of the circle, which is given by the rest | 
|---|
| 1713 | * with respect to the length of the baseline and the sphere's fixed \a RADIUS. | 
|---|
| 1714 | * Note that there is one difficulty: The circumcircle is uniquely defined, but for the circumsphere's center | 
|---|
| 1715 | * there are two possibilities which becomes clear from the construction as seen below. Hence, we must check | 
|---|
| 1716 | * both. | 
|---|
| 1717 | * Note also that the acos() function is not unique on [0, 2.*M_PI). Hence, we need an additional check | 
|---|
| 1718 | * to decide for one of the two possible angles. Therefore we need a SearchDirection and to make this check | 
|---|
| 1719 | * sensible we need OldSphereCenter to be orthogonal to it. Either we construct SearchDirection orthogonal | 
|---|
| 1720 | * right away, or -- what we do here -- we rotate the relative sphere centers such that this orthogonality | 
|---|
| 1721 | * holds. Then, the normalized projection onto the SearchDirection is either +1 or -1 and thus states whether | 
|---|
| 1722 | * the angle is uniquely in either (0,M_PI] or [M_PI, 2.*M_PI). | 
|---|
| 1723 | * @param NormalVector normal direction of the base triangle (here the unit axis vector, \sa Find_starting_triangle()) | 
|---|
| 1724 | * @param SearchDirection general direction where to search for the next point, relative to center of BaseLine | 
|---|
| 1725 | * @param OldSphereCenter center of sphere for base triangle, relative to center of BaseLine, giving null angle for the parameter circle | 
|---|
| 1726 | * @param BaseLine BoundaryLineSet with the current base line | 
|---|
| 1727 | * @param ThirdNode third point to avoid in search | 
|---|
| 1728 | * @param candidates list of equally good candidates to return | 
|---|
| 1729 | * @param ShortestAngle the current path length on this circle band for the current Opt_Candidate | 
|---|
| 1730 | * @param RADIUS radius of sphere | 
|---|
| 1731 | * @param *LC LinkedCell structure with neighbouring points | 
|---|
| 1732 | */ | 
|---|
| 1733 | void Tesselation::Find_third_point_for_Tesselation(Vector NormalVector, Vector SearchDirection, Vector OldSphereCenter, class BoundaryLineSet *BaseLine, class TesselPoint  *ThirdNode, CandidateList* &candidates, double *ShortestAngle, const double RADIUS, LinkedCell *LC) | 
|---|
| 1734 | { | 
|---|
| 1735 | Vector CircleCenter;  // center of the circle, i.e. of the band of sphere's centers | 
|---|
| 1736 | Vector CirclePlaneNormal; // normal vector defining the plane this circle lives in | 
|---|
| 1737 | Vector SphereCenter; | 
|---|
| 1738 | Vector NewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, first possibility | 
|---|
| 1739 | Vector OtherNewSphereCenter;   // center of the sphere defined by the two points of BaseLine and the one of Candidate, second possibility | 
|---|
| 1740 | Vector NewNormalVector;   // normal vector of the Candidate's triangle | 
|---|
| 1741 | Vector helper, OptCandidateCenter, OtherOptCandidateCenter; | 
|---|
| 1742 | LinkedNodes *List = NULL; | 
|---|
| 1743 | double CircleRadius; // radius of this circle | 
|---|
| 1744 | double radius; | 
|---|
| 1745 | double alpha, Otheralpha; // angles (i.e. parameter for the circle). | 
|---|
| 1746 | int N[NDIM], Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 1747 | TesselPoint *Candidate = NULL; | 
|---|
| 1748 | CandidateForTesselation *optCandidate = NULL; | 
|---|
| 1749 |  | 
|---|
| 1750 | cout << Verbose(1) << "Begin of Find_third_point_for_Tesselation" << endl; | 
|---|
| 1751 |  | 
|---|
| 1752 | //cout << Verbose(2) << "INFO: NormalVector of BaseTriangle is " << NormalVector << "." << endl; | 
|---|
| 1753 |  | 
|---|
| 1754 | // construct center of circle | 
|---|
| 1755 | CircleCenter.CopyVector(BaseLine->endpoints[0]->node->node); | 
|---|
| 1756 | CircleCenter.AddVector(BaseLine->endpoints[1]->node->node); | 
|---|
| 1757 | CircleCenter.Scale(0.5); | 
|---|
| 1758 |  | 
|---|
| 1759 | // construct normal vector of circle | 
|---|
| 1760 | CirclePlaneNormal.CopyVector(BaseLine->endpoints[0]->node->node); | 
|---|
| 1761 | CirclePlaneNormal.SubtractVector(BaseLine->endpoints[1]->node->node); | 
|---|
| 1762 |  | 
|---|
| 1763 | // calculate squared radius TesselPoint *ThirdNode,f circle | 
|---|
| 1764 | radius = CirclePlaneNormal.ScalarProduct(&CirclePlaneNormal); | 
|---|
| 1765 | if (radius/4. < RADIUS*RADIUS) { | 
|---|
| 1766 | CircleRadius = RADIUS*RADIUS - radius/4.; | 
|---|
| 1767 | CirclePlaneNormal.Normalize(); | 
|---|
| 1768 | //cout << Verbose(2) << "INFO: CircleCenter is at " << CircleCenter << ", CirclePlaneNormal is " << CirclePlaneNormal << " with circle radius " << sqrt(CircleRadius) << "." << endl; | 
|---|
| 1769 |  | 
|---|
| 1770 | // test whether old center is on the band's plane | 
|---|
| 1771 | if (fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) > HULLEPSILON) { | 
|---|
| 1772 | cerr << "ERROR: Something's very wrong here: OldSphereCenter is not on the band's plane as desired by " << fabs(OldSphereCenter.ScalarProduct(&CirclePlaneNormal)) << "!" << endl; | 
|---|
| 1773 | OldSphereCenter.ProjectOntoPlane(&CirclePlaneNormal); | 
|---|
| 1774 | } | 
|---|
| 1775 | radius = OldSphereCenter.ScalarProduct(&OldSphereCenter); | 
|---|
| 1776 | if (fabs(radius - CircleRadius) < HULLEPSILON) { | 
|---|
| 1777 |  | 
|---|
| 1778 | // check SearchDirection | 
|---|
| 1779 | //cout << Verbose(2) << "INFO: SearchDirection is " << SearchDirection << "." << endl; | 
|---|
| 1780 | if (fabs(OldSphereCenter.ScalarProduct(&SearchDirection)) > HULLEPSILON) {  // rotated the wrong way! | 
|---|
| 1781 | cerr << "ERROR: SearchDirection and RelativeOldSphereCenter are not orthogonal!" << endl; | 
|---|
| 1782 | } | 
|---|
| 1783 |  | 
|---|
| 1784 | // get cell for the starting point | 
|---|
| 1785 | if (LC->SetIndexToVector(&CircleCenter)) { | 
|---|
| 1786 | for(int i=0;i<NDIM;i++) // store indices of this cell | 
|---|
| 1787 | N[i] = LC->n[i]; | 
|---|
| 1788 | //cout << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 1789 | } else { | 
|---|
| 1790 | cerr << "ERROR: Vector " << CircleCenter << " is outside of LinkedCell's bounding box." << endl; | 
|---|
| 1791 | return; | 
|---|
| 1792 | } | 
|---|
| 1793 | // then go through the current and all neighbouring cells and check the contained points for possible candidates | 
|---|
| 1794 | //cout << Verbose(2) << "LC Intervals:"; | 
|---|
| 1795 | for (int i=0;i<NDIM;i++) { | 
|---|
| 1796 | Nlower[i] = ((N[i]-1) >= 0) ? N[i]-1 : 0; | 
|---|
| 1797 | Nupper[i] = ((N[i]+1) < LC->N[i]) ? N[i]+1 : LC->N[i]-1; | 
|---|
| 1798 | //cout << " [" << Nlower[i] << "," << Nupper[i] << "] "; | 
|---|
| 1799 | } | 
|---|
| 1800 | //cout << endl; | 
|---|
| 1801 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 1802 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 1803 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 1804 | List = LC->GetCurrentCell(); | 
|---|
| 1805 | //cout << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 1806 | if (List != NULL) { | 
|---|
| 1807 | for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 1808 | Candidate = (*Runner); | 
|---|
| 1809 |  | 
|---|
| 1810 | // check for three unique points | 
|---|
| 1811 | //cout << Verbose(2) << "INFO: Current Candidate is " << *Candidate << " at " << Candidate->node << "." << endl; | 
|---|
| 1812 | if ((Candidate != BaseLine->endpoints[0]->node) && (Candidate != BaseLine->endpoints[1]->node) ){ | 
|---|
| 1813 |  | 
|---|
| 1814 | // construct both new centers | 
|---|
| 1815 | GetCenterofCircumcircle(&NewSphereCenter, BaseLine->endpoints[0]->node->node, BaseLine->endpoints[1]->node->node, Candidate->node); | 
|---|
| 1816 | OtherNewSphereCenter.CopyVector(&NewSphereCenter); | 
|---|
| 1817 |  | 
|---|
| 1818 | if ((NewNormalVector.MakeNormalVector(BaseLine->endpoints[0]->node->node, BaseLine->endpoints[1]->node->node, Candidate->node)) | 
|---|
| 1819 | && (fabs(NewNormalVector.ScalarProduct(&NewNormalVector)) > HULLEPSILON) | 
|---|
| 1820 | ) { | 
|---|
| 1821 | helper.CopyVector(&NewNormalVector); | 
|---|
| 1822 | //cout << Verbose(2) << "INFO: NewNormalVector is " << NewNormalVector << "." << endl; | 
|---|
| 1823 | radius = BaseLine->endpoints[0]->node->node->DistanceSquared(&NewSphereCenter); | 
|---|
| 1824 | if (radius < RADIUS*RADIUS) { | 
|---|
| 1825 | helper.Scale(sqrt(RADIUS*RADIUS - radius)); | 
|---|
| 1826 | //cout << Verbose(2) << "INFO: Distance of NewCircleCenter to NewSphereCenter is " << helper.Norm() << " with sphere radius " << RADIUS << "." << endl; | 
|---|
| 1827 | NewSphereCenter.AddVector(&helper); | 
|---|
| 1828 | NewSphereCenter.SubtractVector(&CircleCenter); | 
|---|
| 1829 | //cout << Verbose(2) << "INFO: NewSphereCenter is at " << NewSphereCenter << "." << endl; | 
|---|
| 1830 |  | 
|---|
| 1831 | // OtherNewSphereCenter is created by the same vector just in the other direction | 
|---|
| 1832 | helper.Scale(-1.); | 
|---|
| 1833 | OtherNewSphereCenter.AddVector(&helper); | 
|---|
| 1834 | OtherNewSphereCenter.SubtractVector(&CircleCenter); | 
|---|
| 1835 | //cout << Verbose(2) << "INFO: OtherNewSphereCenter is at " << OtherNewSphereCenter << "." << endl; | 
|---|
| 1836 |  | 
|---|
| 1837 | alpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, NewSphereCenter, OldSphereCenter, NormalVector, SearchDirection); | 
|---|
| 1838 | Otheralpha = GetPathLengthonCircumCircle(CircleCenter, CirclePlaneNormal, CircleRadius, OtherNewSphereCenter, OldSphereCenter, NormalVector, SearchDirection); | 
|---|
| 1839 | alpha = min(alpha, Otheralpha); | 
|---|
| 1840 | // if there is a better candidate, drop the current list and add the new candidate | 
|---|
| 1841 | // otherwise ignore the new candidate and keep the list | 
|---|
| 1842 | if (*ShortestAngle > (alpha - HULLEPSILON)) { | 
|---|
| 1843 | optCandidate = new CandidateForTesselation(Candidate, BaseLine, OptCandidateCenter, OtherOptCandidateCenter); | 
|---|
| 1844 | if (fabs(alpha - Otheralpha) > MYEPSILON) { | 
|---|
| 1845 | optCandidate->OptCenter.CopyVector(&NewSphereCenter); | 
|---|
| 1846 | optCandidate->OtherOptCenter.CopyVector(&OtherNewSphereCenter); | 
|---|
| 1847 | } else { | 
|---|
| 1848 | optCandidate->OptCenter.CopyVector(&OtherNewSphereCenter); | 
|---|
| 1849 | optCandidate->OtherOptCenter.CopyVector(&NewSphereCenter); | 
|---|
| 1850 | } | 
|---|
| 1851 | // if there is an equal candidate, add it to the list without clearing the list | 
|---|
| 1852 | if ((*ShortestAngle - HULLEPSILON) < alpha) { | 
|---|
| 1853 | candidates->push_back(optCandidate); | 
|---|
| 1854 | cout << Verbose(2) << "ACCEPT: We have found an equally good candidate: " << *(optCandidate->point) << " with " | 
|---|
| 1855 | << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl; | 
|---|
| 1856 | } else { | 
|---|
| 1857 | // remove all candidates from the list and then the list itself | 
|---|
| 1858 | class CandidateForTesselation *remover = NULL; | 
|---|
| 1859 | for (CandidateList::iterator it = candidates->begin(); it != candidates->end(); ++it) { | 
|---|
| 1860 | remover = *it; | 
|---|
| 1861 | delete(remover); | 
|---|
| 1862 | } | 
|---|
| 1863 | candidates->clear(); | 
|---|
| 1864 | candidates->push_back(optCandidate); | 
|---|
| 1865 | cout << Verbose(2) << "ACCEPT: We have found a better candidate: " << *(optCandidate->point) << " with " | 
|---|
| 1866 | << alpha << " and circumsphere's center at " << optCandidate->OptCenter << "." << endl; | 
|---|
| 1867 | } | 
|---|
| 1868 | *ShortestAngle = alpha; | 
|---|
| 1869 | //cout << Verbose(2) << "INFO: There are " << candidates->size() << " candidates in the list now." << endl; | 
|---|
| 1870 | } else { | 
|---|
| 1871 | if ((optCandidate != NULL) && (optCandidate->point != NULL)) { | 
|---|
| 1872 | //cout << Verbose(2) << "REJECT: Old candidate " << *(optCandidate->point) << " with " << *ShortestAngle << " is better than new one " << *Candidate << " with " << alpha << " ." << endl; | 
|---|
| 1873 | } else { | 
|---|
| 1874 | //cout << Verbose(2) << "REJECT: Candidate " << *Candidate << " with " << alpha << " was rejected." << endl; | 
|---|
| 1875 | } | 
|---|
| 1876 | } | 
|---|
| 1877 |  | 
|---|
| 1878 | } else { | 
|---|
| 1879 | //cout << Verbose(2) << "REJECT: NewSphereCenter " << NewSphereCenter << " for " << *Candidate << " is too far away: " << radius << "." << endl; | 
|---|
| 1880 | } | 
|---|
| 1881 | } else { | 
|---|
| 1882 | //cout << Verbose(2) << "REJECT: Three points from " << *BaseLine << " and Candidate " << *Candidate << " are linear-dependent." << endl; | 
|---|
| 1883 | } | 
|---|
| 1884 | } else { | 
|---|
| 1885 | if (ThirdNode != NULL) { | 
|---|
| 1886 | //cout << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " and " << *ThirdNode << " contains Candidate " << *Candidate << "." << endl; | 
|---|
| 1887 | } else { | 
|---|
| 1888 | //cout << Verbose(2) << "REJECT: Base triangle " << *BaseLine << " contains Candidate " << *Candidate << "." << endl; | 
|---|
| 1889 | } | 
|---|
| 1890 | } | 
|---|
| 1891 | } | 
|---|
| 1892 | } | 
|---|
| 1893 | } | 
|---|
| 1894 | } else { | 
|---|
| 1895 | cerr << Verbose(2) << "ERROR: The projected center of the old sphere has radius " << radius << " instead of " << CircleRadius << "." << endl; | 
|---|
| 1896 | } | 
|---|
| 1897 | } else { | 
|---|
| 1898 | if (ThirdNode != NULL) | 
|---|
| 1899 | cout << Verbose(2) << "Circumcircle for base line " << *BaseLine << " and third node " << *ThirdNode << " is too big!" << endl; | 
|---|
| 1900 | else | 
|---|
| 1901 | cout << Verbose(2) << "Circumcircle for base line " << *BaseLine << " is too big!" << endl; | 
|---|
| 1902 | } | 
|---|
| 1903 |  | 
|---|
| 1904 | //cout << Verbose(2) << "INFO: Sorting candidate list ..." << endl; | 
|---|
| 1905 | if (candidates->size() > 1) { | 
|---|
| 1906 | candidates->unique(); | 
|---|
| 1907 | candidates->sort(sortCandidates); | 
|---|
| 1908 | } | 
|---|
| 1909 |  | 
|---|
| 1910 | cout << Verbose(1) << "End of Find_third_point_for_Tesselation" << endl; | 
|---|
| 1911 | }; | 
|---|
| 1912 |  | 
|---|
| 1913 | /** Finds the endpoint two lines are sharing. | 
|---|
| 1914 | * \param *line1 first line | 
|---|
| 1915 | * \param *line2 second line | 
|---|
| 1916 | * \return point which is shared or NULL if none | 
|---|
| 1917 | */ | 
|---|
| 1918 | class BoundaryPointSet *Tesselation::GetCommonEndpoint(class BoundaryLineSet * line1, class BoundaryLineSet * line2) | 
|---|
| 1919 | { | 
|---|
| 1920 | class BoundaryLineSet * lines[2] = | 
|---|
| 1921 | { line1, line2 }; | 
|---|
| 1922 | class BoundaryPointSet *node = NULL; | 
|---|
| 1923 | map<int, class BoundaryPointSet *> OrderMap; | 
|---|
| 1924 | pair<map<int, class BoundaryPointSet *>::iterator, bool> OrderTest; | 
|---|
| 1925 | for (int i = 0; i < 2; i++) | 
|---|
| 1926 | // for both lines | 
|---|
| 1927 | for (int j = 0; j < 2; j++) | 
|---|
| 1928 | { // for both endpoints | 
|---|
| 1929 | OrderTest = OrderMap.insert(pair<int, class BoundaryPointSet *> ( | 
|---|
| 1930 | lines[i]->endpoints[j]->Nr, lines[i]->endpoints[j])); | 
|---|
| 1931 | if (!OrderTest.second) | 
|---|
| 1932 | { // if insertion fails, we have common endpoint | 
|---|
| 1933 | node = OrderTest.first->second; | 
|---|
| 1934 | cout << Verbose(5) << "Common endpoint of lines " << *line1 | 
|---|
| 1935 | << " and " << *line2 << " is: " << *node << "." << endl; | 
|---|
| 1936 | j = 2; | 
|---|
| 1937 | i = 2; | 
|---|
| 1938 | break; | 
|---|
| 1939 | } | 
|---|
| 1940 | } | 
|---|
| 1941 | return node; | 
|---|
| 1942 | }; | 
|---|
| 1943 |  | 
|---|
| 1944 | /** Finds the triangle that is closest to a given Vector \a *x. | 
|---|
| 1945 | * \param *out output stream for debugging | 
|---|
| 1946 | * \param *x Vector to look from | 
|---|
| 1947 | * \return list of BoundaryTriangleSet of nearest triangles or NULL in degenerate case. | 
|---|
| 1948 | */ | 
|---|
| 1949 | list<BoundaryTriangleSet*> * Tesselation::FindClosestTrianglesToPoint(ofstream *out, Vector *x, LinkedCell* LC) | 
|---|
| 1950 | { | 
|---|
| 1951 | class TesselPoint *trianglePoints[3]; | 
|---|
| 1952 |  | 
|---|
| 1953 | if (LinesOnBoundary.empty()) { | 
|---|
| 1954 | *out << Verbose(0) << "Error: There is no tesselation structure to compare the point with, " << "please create one first."; | 
|---|
| 1955 | return NULL; | 
|---|
| 1956 | } | 
|---|
| 1957 |  | 
|---|
| 1958 | trianglePoints[0] = findClosestPoint(x, LC); | 
|---|
| 1959 | // check whether closest point is "too close" :), then it's inside | 
|---|
| 1960 | if (trianglePoints[0]->node->DistanceSquared(x) < MYEPSILON) { | 
|---|
| 1961 | *out << Verbose(1) << "Point is right on a tesselation point, no nearest triangle." << endl; | 
|---|
| 1962 | return NULL; | 
|---|
| 1963 | } | 
|---|
| 1964 | list<TesselPoint*> *connectedClosestPoints = getCircleOfConnectedPoints(out, trianglePoints[0], x); | 
|---|
| 1965 | trianglePoints[1] = connectedClosestPoints->front(); | 
|---|
| 1966 | trianglePoints[2] = connectedClosestPoints->back(); | 
|---|
| 1967 | for (int i=0;i<3;i++) { | 
|---|
| 1968 | if (trianglePoints[i] == NULL) { | 
|---|
| 1969 | *out << Verbose(1) << "IsInnerPoint encounters serious error, point " << i << " not found." << endl; | 
|---|
| 1970 | } | 
|---|
| 1971 | *out << Verbose(1) << "List of possible points:" << endl; | 
|---|
| 1972 | *out << *trianglePoints[i] << endl; | 
|---|
| 1973 | } | 
|---|
| 1974 | delete(connectedClosestPoints); | 
|---|
| 1975 |  | 
|---|
| 1976 | list<BoundaryTriangleSet*> *triangles = FindTriangles(trianglePoints); | 
|---|
| 1977 |  | 
|---|
| 1978 | if (triangles->empty()) { | 
|---|
| 1979 | *out << Verbose(0) << "Error: There is no nearest triangle. Please check the tesselation structure."; | 
|---|
| 1980 | return NULL; | 
|---|
| 1981 | } else | 
|---|
| 1982 | return triangles; | 
|---|
| 1983 | }; | 
|---|
| 1984 |  | 
|---|
| 1985 | /** Finds closest triangle to a point. | 
|---|
| 1986 | * This basically just takes care of the degenerate case, which is not handled in FindClosestTrianglesToPoint(). | 
|---|
| 1987 | * \param *out output stream for debugging | 
|---|
| 1988 | * \param *x Vector to look from | 
|---|
| 1989 | * \return list of BoundaryTriangleSet of nearest triangles or NULL. | 
|---|
| 1990 | */ | 
|---|
| 1991 | class BoundaryTriangleSet * Tesselation::FindClosestTriangleToPoint(ofstream *out, Vector *x, LinkedCell* LC) | 
|---|
| 1992 | { | 
|---|
| 1993 | class BoundaryTriangleSet *result = NULL; | 
|---|
| 1994 | list<BoundaryTriangleSet*> *triangles = FindClosestTrianglesToPoint(out, x, LC); | 
|---|
| 1995 |  | 
|---|
| 1996 | if (triangles == NULL) | 
|---|
| 1997 | return NULL; | 
|---|
| 1998 |  | 
|---|
| 1999 | if (x->ScalarProduct(&triangles->front()->NormalVector) < 0) | 
|---|
| 2000 | result = triangles->back(); | 
|---|
| 2001 | else | 
|---|
| 2002 | result = triangles->front(); | 
|---|
| 2003 |  | 
|---|
| 2004 | delete(triangles); | 
|---|
| 2005 | return result; | 
|---|
| 2006 | }; | 
|---|
| 2007 |  | 
|---|
| 2008 | /** Checks whether the provided Vector is within the tesselation structure. | 
|---|
| 2009 | * | 
|---|
| 2010 | * @param point of which to check the position | 
|---|
| 2011 | * @param *LC LinkedCell structure | 
|---|
| 2012 | * | 
|---|
| 2013 | * @return true if the point is inside the tesselation structure, false otherwise | 
|---|
| 2014 | */ | 
|---|
| 2015 | bool Tesselation::IsInnerPoint(ofstream *out, Vector Point, LinkedCell* LC) | 
|---|
| 2016 | { | 
|---|
| 2017 | class BoundaryTriangleSet *result = FindClosestTriangleToPoint(out, &Point, LC); | 
|---|
| 2018 | if (result == NULL) | 
|---|
| 2019 | return true; | 
|---|
| 2020 | if (Point.ScalarProduct(&result->NormalVector) < 0) | 
|---|
| 2021 | return true; | 
|---|
| 2022 | else | 
|---|
| 2023 | return false; | 
|---|
| 2024 | } | 
|---|
| 2025 |  | 
|---|
| 2026 | /** Checks whether the provided TesselPoint is within the tesselation structure. | 
|---|
| 2027 | * | 
|---|
| 2028 | * @param *Point of which to check the position | 
|---|
| 2029 | * @param *LC Linked Cell structure | 
|---|
| 2030 | * | 
|---|
| 2031 | * @return true if the point is inside the tesselation structure, false otherwise | 
|---|
| 2032 | */ | 
|---|
| 2033 | bool Tesselation::IsInnerPoint(ofstream *out, TesselPoint *Point, LinkedCell* LC) | 
|---|
| 2034 | { | 
|---|
| 2035 | class BoundaryTriangleSet *result = FindClosestTriangleToPoint(out, Point->node, LC); | 
|---|
| 2036 | if (result == NULL) | 
|---|
| 2037 | return true; | 
|---|
| 2038 | if (Point->node->ScalarProduct(&result->NormalVector) < 0) | 
|---|
| 2039 | return true; | 
|---|
| 2040 | else | 
|---|
| 2041 | return false; | 
|---|
| 2042 | } | 
|---|
| 2043 |  | 
|---|
| 2044 | /** Gets all points connected to the provided point by triangulation lines. | 
|---|
| 2045 | * Maps them down onto the plane designated by the axis \a *Point and \a *Reference. The center of all points | 
|---|
| 2046 | * connected in the tesselation to \a *Point is mapped to spherical coordinates with the zero angle being given | 
|---|
| 2047 | * by the mapped down \a *Reference. Hence, the biggest and the smallest angles are those of the two shanks of the | 
|---|
| 2048 | * triangle we are looking for. | 
|---|
| 2049 | * | 
|---|
| 2050 | * @param *Point of which get all connected points | 
|---|
| 2051 | * @param *Reference Vector to be checked whether it is an inner point | 
|---|
| 2052 | * | 
|---|
| 2053 | * @return list of the two points linked to the provided one and closest to the point to be checked, | 
|---|
| 2054 | */ | 
|---|
| 2055 | list<TesselPoint*> * Tesselation::getCircleOfConnectedPoints(ofstream *out, TesselPoint* Point, Vector* Reference) | 
|---|
| 2056 | { | 
|---|
| 2057 | list<TesselPoint*> connectedPoints; | 
|---|
| 2058 | map<double, TesselPoint*> anglesOfPoints; | 
|---|
| 2059 | map<double, TesselPoint*>::iterator runner; | 
|---|
| 2060 | list<TesselPoint*>::iterator listRunner; | 
|---|
| 2061 | Vector center, planeNorm, currentPoint, OrthogonalVector, helper; | 
|---|
| 2062 | TesselPoint* current; | 
|---|
| 2063 | bool takePoint = false; | 
|---|
| 2064 |  | 
|---|
| 2065 | planeNorm.CopyVector(Point->node); | 
|---|
| 2066 | planeNorm.SubtractVector(Reference); | 
|---|
| 2067 | planeNorm.Normalize(); | 
|---|
| 2068 |  | 
|---|
| 2069 | LineMap::iterator findLines = LinesOnBoundary.begin(); | 
|---|
| 2070 | while (findLines != LinesOnBoundary.end()) { | 
|---|
| 2071 | takePoint = false; | 
|---|
| 2072 |  | 
|---|
| 2073 | if (findLines->second->endpoints[0]->Nr == Point->nr) { | 
|---|
| 2074 | takePoint = true; | 
|---|
| 2075 | current = findLines->second->endpoints[1]->node; | 
|---|
| 2076 | } else if (findLines->second->endpoints[1]->Nr == Point->nr) { | 
|---|
| 2077 | takePoint = true; | 
|---|
| 2078 | current = findLines->second->endpoints[0]->node; | 
|---|
| 2079 | } | 
|---|
| 2080 |  | 
|---|
| 2081 | if (takePoint) { | 
|---|
| 2082 | connectedPoints.push_back(current); | 
|---|
| 2083 | currentPoint.CopyVector(current->node); | 
|---|
| 2084 | currentPoint.ProjectOntoPlane(&planeNorm); | 
|---|
| 2085 | center.AddVector(¤tPoint); | 
|---|
| 2086 | } | 
|---|
| 2087 |  | 
|---|
| 2088 | findLines++; | 
|---|
| 2089 | } | 
|---|
| 2090 |  | 
|---|
| 2091 | *out << "Summed vectors " << center << "; number of points " << connectedPoints.size() | 
|---|
| 2092 | << "; scale factor " << 1.0/connectedPoints.size(); | 
|---|
| 2093 |  | 
|---|
| 2094 | center.Scale(1.0/connectedPoints.size()); | 
|---|
| 2095 | listRunner = connectedPoints.begin(); | 
|---|
| 2096 |  | 
|---|
| 2097 | *out << " calculated center " << center <<  endl; | 
|---|
| 2098 |  | 
|---|
| 2099 | // construct one orthogonal vector | 
|---|
| 2100 | helper.CopyVector(Reference); | 
|---|
| 2101 | helper.ProjectOntoPlane(&planeNorm); | 
|---|
| 2102 | OrthogonalVector.MakeNormalVector(¢er, &helper, (*listRunner)->node); | 
|---|
| 2103 | while (listRunner != connectedPoints.end()) { | 
|---|
| 2104 | double angle = getAngle(*((*listRunner)->node), *(Reference), center, OrthogonalVector); | 
|---|
| 2105 | *out << "Calculated angle " << angle << " for point " << **listRunner << endl; | 
|---|
| 2106 | anglesOfPoints.insert(pair<double, TesselPoint*>(angle, (*listRunner))); | 
|---|
| 2107 | listRunner++; | 
|---|
| 2108 | } | 
|---|
| 2109 |  | 
|---|
| 2110 | list<TesselPoint*> *result = new list<TesselPoint*>; | 
|---|
| 2111 | runner = anglesOfPoints.begin(); | 
|---|
| 2112 | *out << "First value is " << *runner->second << endl; | 
|---|
| 2113 | result->push_back(runner->second); | 
|---|
| 2114 | runner = anglesOfPoints.end(); | 
|---|
| 2115 | runner--; | 
|---|
| 2116 | *out << "Second value is " << *runner->second << endl; | 
|---|
| 2117 | result->push_back(runner->second); | 
|---|
| 2118 |  | 
|---|
| 2119 | *out << "List of closest points has " << result->size() << " elements, which are " | 
|---|
| 2120 | << *(result->front()) << " and " << *(result->back()) << endl; | 
|---|
| 2121 |  | 
|---|
| 2122 | return result; | 
|---|
| 2123 | } | 
|---|
| 2124 |  | 
|---|
| 2125 | /** Checks for a new special triangle whether one of its edges is already present with one one triangle connected. | 
|---|
| 2126 | * This enforces that special triangles (i.e. degenerated ones) should at last close the open-edge frontier and not | 
|---|
| 2127 | * make it bigger (i.e. closing one (the baseline) and opening two new ones). | 
|---|
| 2128 | * \param TPS[3] nodes of the triangle | 
|---|
| 2129 | * \return true - there is such a line (i.e. creation of degenerated triangle is valid), false - no such line (don't create) | 
|---|
| 2130 | */ | 
|---|
| 2131 | bool CheckLineCriteriaforDegeneratedTriangle(class BoundaryPointSet *nodes[3]) | 
|---|
| 2132 | { | 
|---|
| 2133 | bool result = false; | 
|---|
| 2134 | int counter = 0; | 
|---|
| 2135 |  | 
|---|
| 2136 | // check all three points | 
|---|
| 2137 | for (int i=0;i<3;i++) | 
|---|
| 2138 | for (int j=i+1; j<3; j++) { | 
|---|
| 2139 | if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) {  // there already is a line | 
|---|
| 2140 | LineMap::iterator FindLine; | 
|---|
| 2141 | pair<LineMap::iterator,LineMap::iterator> FindPair; | 
|---|
| 2142 | FindPair = nodes[i]->lines.equal_range(nodes[j]->node->nr); | 
|---|
| 2143 | for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) { | 
|---|
| 2144 | // If there is a line with less than two attached triangles, we don't need a new line. | 
|---|
| 2145 | if (FindLine->second->TrianglesCount < 2) { | 
|---|
| 2146 | counter++; | 
|---|
| 2147 | break;  // increase counter only once per edge | 
|---|
| 2148 | } | 
|---|
| 2149 | } | 
|---|
| 2150 | } else { // no line | 
|---|
| 2151 | cout << Verbose(1) << "ERROR: The line between " << nodes[i] << " and " << nodes[j] << " is not yet present, hence no need for a degenerate triangle!" << endl; | 
|---|
| 2152 | result = true; | 
|---|
| 2153 | } | 
|---|
| 2154 | } | 
|---|
| 2155 | if (counter > 1) { | 
|---|
| 2156 | cout << Verbose(2) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl; | 
|---|
| 2157 | result = true; | 
|---|
| 2158 | } | 
|---|
| 2159 | return result; | 
|---|
| 2160 | }; | 
|---|
| 2161 |  | 
|---|
| 2162 |  | 
|---|
| 2163 | /** Sort function for the candidate list. | 
|---|
| 2164 | */ | 
|---|
| 2165 | bool sortCandidates(CandidateForTesselation* candidate1, CandidateForTesselation* candidate2) | 
|---|
| 2166 | { | 
|---|
| 2167 | Vector BaseLineVector, OrthogonalVector, helper; | 
|---|
| 2168 | if (candidate1->BaseLine != candidate2->BaseLine) {  // sanity check | 
|---|
| 2169 | cout << Verbose(0) << "ERROR: sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl; | 
|---|
| 2170 | //return false; | 
|---|
| 2171 | exit(1); | 
|---|
| 2172 | } | 
|---|
| 2173 | // create baseline vector | 
|---|
| 2174 | BaseLineVector.CopyVector(candidate1->BaseLine->endpoints[1]->node->node); | 
|---|
| 2175 | BaseLineVector.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node); | 
|---|
| 2176 | BaseLineVector.Normalize(); | 
|---|
| 2177 |  | 
|---|
| 2178 | // create normal in-plane vector to cope with acos() non-uniqueness on [0,2pi] (note that is pointing in the "right" direction already, hence ">0" test!) | 
|---|
| 2179 | helper.CopyVector(candidate1->BaseLine->endpoints[0]->node->node); | 
|---|
| 2180 | helper.SubtractVector(candidate1->point->node); | 
|---|
| 2181 | OrthogonalVector.CopyVector(&helper); | 
|---|
| 2182 | helper.VectorProduct(&BaseLineVector); | 
|---|
| 2183 | OrthogonalVector.SubtractVector(&helper); | 
|---|
| 2184 | OrthogonalVector.Normalize(); | 
|---|
| 2185 |  | 
|---|
| 2186 | // calculate both angles and correct with in-plane vector | 
|---|
| 2187 | helper.CopyVector(candidate1->point->node); | 
|---|
| 2188 | helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node); | 
|---|
| 2189 | double phi = BaseLineVector.Angle(&helper); | 
|---|
| 2190 | if (OrthogonalVector.ScalarProduct(&helper) > 0) { | 
|---|
| 2191 | phi = 2.*M_PI - phi; | 
|---|
| 2192 | } | 
|---|
| 2193 | helper.CopyVector(candidate2->point->node); | 
|---|
| 2194 | helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node); | 
|---|
| 2195 | double psi = BaseLineVector.Angle(&helper); | 
|---|
| 2196 | if (OrthogonalVector.ScalarProduct(&helper) > 0) { | 
|---|
| 2197 | psi = 2.*M_PI - psi; | 
|---|
| 2198 | } | 
|---|
| 2199 |  | 
|---|
| 2200 | cout << Verbose(2) << *candidate1->point << " has angle " << phi << endl; | 
|---|
| 2201 | cout << Verbose(2) << *candidate2->point << " has angle " << psi << endl; | 
|---|
| 2202 |  | 
|---|
| 2203 | // return comparison | 
|---|
| 2204 | return phi < psi; | 
|---|
| 2205 | }; | 
|---|
| 2206 |  | 
|---|
| 2207 |  | 
|---|
| 2208 |  | 
|---|
| 2209 | /** | 
|---|
| 2210 | * Finds the point which is closest to the provided one. | 
|---|
| 2211 | * | 
|---|
| 2212 | * @param Point to which to find the closest other point | 
|---|
| 2213 | * @param linked cell structure | 
|---|
| 2214 | * | 
|---|
| 2215 | * @return point which is closest to the provided one | 
|---|
| 2216 | */ | 
|---|
| 2217 | TesselPoint* findClosestPoint(const Vector* Point, LinkedCell* LC) | 
|---|
| 2218 | { | 
|---|
| 2219 | LinkedNodes *List = NULL; | 
|---|
| 2220 | TesselPoint* closestPoint = NULL; | 
|---|
| 2221 | double distance = 1e16; | 
|---|
| 2222 | Vector helper; | 
|---|
| 2223 | int N[NDIM], Nlower[NDIM], Nupper[NDIM]; | 
|---|
| 2224 |  | 
|---|
| 2225 | LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly | 
|---|
| 2226 | for(int i=0;i<NDIM;i++) // store indices of this cell | 
|---|
| 2227 | N[i] = LC->n[i]; | 
|---|
| 2228 | //cout << Verbose(2) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl; | 
|---|
| 2229 |  | 
|---|
| 2230 | LC->GetNeighbourBounds(Nlower, Nupper); | 
|---|
| 2231 | //cout << endl; | 
|---|
| 2232 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++) | 
|---|
| 2233 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++) | 
|---|
| 2234 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) { | 
|---|
| 2235 | List = LC->GetCurrentCell(); | 
|---|
| 2236 | //cout << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl; | 
|---|
| 2237 | if (List != NULL) { | 
|---|
| 2238 | for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) { | 
|---|
| 2239 | helper.CopyVector(Point); | 
|---|
| 2240 | helper.SubtractVector((*Runner)->node); | 
|---|
| 2241 | double currentNorm = helper. Norm(); | 
|---|
| 2242 | if (currentNorm < distance) { | 
|---|
| 2243 | distance = currentNorm; | 
|---|
| 2244 | closestPoint = (*Runner); | 
|---|
| 2245 | } | 
|---|
| 2246 | } | 
|---|
| 2247 | } else { | 
|---|
| 2248 | cerr << "ERROR: The current cell " << LC->n[0] << "," << LC->n[1] << "," | 
|---|
| 2249 | << LC->n[2] << " is invalid!" << endl; | 
|---|
| 2250 | } | 
|---|
| 2251 | } | 
|---|
| 2252 |  | 
|---|
| 2253 | return closestPoint; | 
|---|
| 2254 | } | 
|---|
| 2255 |  | 
|---|
| 2256 |  | 
|---|
| 2257 | /** | 
|---|
| 2258 | * Finds triangles belonging to the three provided points. | 
|---|
| 2259 | * | 
|---|
| 2260 | * @param *Points[3] list, is expected to contain three points | 
|---|
| 2261 | * | 
|---|
| 2262 | * @return triangles which belong to the provided points, will be empty if there are none, | 
|---|
| 2263 | *         will usually be one, in case of degeneration, there will be two | 
|---|
| 2264 | */ | 
|---|
| 2265 | list<BoundaryTriangleSet*> *Tesselation::FindTriangles(TesselPoint* Points[3]) | 
|---|
| 2266 | { | 
|---|
| 2267 | list<BoundaryTriangleSet*> *result = new list<BoundaryTriangleSet*>; | 
|---|
| 2268 | LineMap::iterator FindLine; | 
|---|
| 2269 | PointMap::iterator FindPoint; | 
|---|
| 2270 | TriangleMap::iterator FindTriangle; | 
|---|
| 2271 | class BoundaryPointSet *TrianglePoints[3]; | 
|---|
| 2272 |  | 
|---|
| 2273 | for (int i = 0; i < 3; i++) { | 
|---|
| 2274 | FindPoint = PointsOnBoundary.find(Points[i]->nr); | 
|---|
| 2275 | if (FindPoint != PointsOnBoundary.end()) { | 
|---|
| 2276 | TrianglePoints[i] = FindPoint->second; | 
|---|
| 2277 | } else { | 
|---|
| 2278 | TrianglePoints[i] = NULL; | 
|---|
| 2279 | } | 
|---|
| 2280 | } | 
|---|
| 2281 |  | 
|---|
| 2282 | // checks lines between the points in the Points for their adjacent triangles | 
|---|
| 2283 | for (int i = 0; i < 3; i++) { | 
|---|
| 2284 | if (TrianglePoints[i] != NULL) { | 
|---|
| 2285 | for (int j = i; j < 3; j++) { | 
|---|
| 2286 | if (TrianglePoints[j] != NULL) { | 
|---|
| 2287 | FindLine = TrianglePoints[i]->lines.find(TrianglePoints[j]->node->nr); | 
|---|
| 2288 | if (FindLine != TrianglePoints[i]->lines.end()) { | 
|---|
| 2289 | for (; FindLine->first == TrianglePoints[j]->node->nr; FindLine++) { | 
|---|
| 2290 | FindTriangle = FindLine->second->triangles.begin(); | 
|---|
| 2291 | for (; FindTriangle != FindLine->second->triangles.end(); FindTriangle++) { | 
|---|
| 2292 | if (( | 
|---|
| 2293 | (FindTriangle->second->endpoints[0] == TrianglePoints[0]) | 
|---|
| 2294 | || (FindTriangle->second->endpoints[0] == TrianglePoints[1]) | 
|---|
| 2295 | || (FindTriangle->second->endpoints[0] == TrianglePoints[2]) | 
|---|
| 2296 | ) && ( | 
|---|
| 2297 | (FindTriangle->second->endpoints[1] == TrianglePoints[0]) | 
|---|
| 2298 | || (FindTriangle->second->endpoints[1] == TrianglePoints[1]) | 
|---|
| 2299 | || (FindTriangle->second->endpoints[1] == TrianglePoints[2]) | 
|---|
| 2300 | ) && ( | 
|---|
| 2301 | (FindTriangle->second->endpoints[2] == TrianglePoints[0]) | 
|---|
| 2302 | || (FindTriangle->second->endpoints[2] == TrianglePoints[1]) | 
|---|
| 2303 | || (FindTriangle->second->endpoints[2] == TrianglePoints[2]) | 
|---|
| 2304 | ) | 
|---|
| 2305 | ) { | 
|---|
| 2306 | result->push_back(FindTriangle->second); | 
|---|
| 2307 | } | 
|---|
| 2308 | } | 
|---|
| 2309 | } | 
|---|
| 2310 | // Is it sufficient to consider one of the triangle lines for this. | 
|---|
| 2311 | return result; | 
|---|
| 2312 |  | 
|---|
| 2313 | } | 
|---|
| 2314 | } | 
|---|
| 2315 | } | 
|---|
| 2316 | } | 
|---|
| 2317 | } | 
|---|
| 2318 |  | 
|---|
| 2319 | return result; | 
|---|
| 2320 | } | 
|---|
| 2321 |  | 
|---|
| 2322 | /** Gets the angle between a point and a reference relative to the provided center. | 
|---|
| 2323 | * We have two shanks (point, center) and (reference, center) between which the angle is calculated | 
|---|
| 2324 | * and by scalar product with OrthogonalVector we decide the interval. | 
|---|
| 2325 | * @param point to calculate the angle for | 
|---|
| 2326 | * @param reference to which to calculate the angle | 
|---|
| 2327 | * @param center for which to calculate the angle between the vectors | 
|---|
| 2328 | * @param OrthogonalVector points in direction of [pi,2pi] interval | 
|---|
| 2329 | * | 
|---|
| 2330 | * @return angle between point and reference | 
|---|
| 2331 | */ | 
|---|
| 2332 | double getAngle(const Vector &point, const Vector &reference, const Vector ¢er, Vector OrthogonalVector) | 
|---|
| 2333 | { | 
|---|
| 2334 | Vector ReferenceVector, helper; | 
|---|
| 2335 | cout << Verbose(4) << center << " is our center " << endl; | 
|---|
| 2336 | // create baseline vector | 
|---|
| 2337 | ReferenceVector.CopyVector(&reference); | 
|---|
| 2338 | ReferenceVector.SubtractVector(¢er); | 
|---|
| 2339 | OrthogonalVector.MakeNormalVector(&ReferenceVector); | 
|---|
| 2340 | cout << Verbose(4) << ReferenceVector << " is our reference " << endl; | 
|---|
| 2341 | if (ReferenceVector.IsNull()) | 
|---|
| 2342 | return M_PI; | 
|---|
| 2343 |  | 
|---|
| 2344 | // calculate both angles and correct with in-plane vector | 
|---|
| 2345 | helper.CopyVector(&point); | 
|---|
| 2346 | helper.SubtractVector(¢er); | 
|---|
| 2347 | if (helper.IsNull()) | 
|---|
| 2348 | return M_PI; | 
|---|
| 2349 | double phi = ReferenceVector.Angle(&helper); | 
|---|
| 2350 | if (OrthogonalVector.ScalarProduct(&helper) > 0) { | 
|---|
| 2351 | phi = 2.*M_PI - phi; | 
|---|
| 2352 | } | 
|---|
| 2353 |  | 
|---|
| 2354 | cout << Verbose(3) << helper << " has angle " << phi << " with respect to reference." << endl; | 
|---|
| 2355 |  | 
|---|
| 2356 | return phi; | 
|---|
| 2357 | } | 
|---|
| 2358 |  | 
|---|