| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * BoundaryTriangleSet.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Jul 29, 2010 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include "BoundaryTriangleSet.hpp" | 
|---|
| 23 |  | 
|---|
| 24 | #include <iostream> | 
|---|
| 25 |  | 
|---|
| 26 | #include "BoundaryLineSet.hpp" | 
|---|
| 27 | #include "BoundaryPointSet.hpp" | 
|---|
| 28 | #include "Atom/TesselPoint.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include "Helpers/defs.hpp" | 
|---|
| 31 |  | 
|---|
| 32 | #include "CodePatterns/Assert.hpp" | 
|---|
| 33 | #include "CodePatterns/Info.hpp" | 
|---|
| 34 | #include "CodePatterns/Log.hpp" | 
|---|
| 35 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 36 | #include "LinearAlgebra/Exceptions.hpp" | 
|---|
| 37 | #include "LinearAlgebra/Line.hpp" | 
|---|
| 38 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| 39 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 40 |  | 
|---|
| 41 | using namespace std; | 
|---|
| 42 |  | 
|---|
| 43 | /** Constructor for BoundaryTriangleSet. | 
|---|
| 44 | */ | 
|---|
| 45 | BoundaryTriangleSet::BoundaryTriangleSet() : | 
|---|
| 46 | Nr(-1) | 
|---|
| 47 | { | 
|---|
| 48 | //Info FunctionInfo(__func__); | 
|---|
| 49 | for (int i = 0; i < 3; i++) { | 
|---|
| 50 | endpoints[i] = NULL; | 
|---|
| 51 | lines[i] = NULL; | 
|---|
| 52 | } | 
|---|
| 53 | } | 
|---|
| 54 | ; | 
|---|
| 55 |  | 
|---|
| 56 | /** Constructor for BoundaryTriangleSet with three lines. | 
|---|
| 57 | * \param *line[3] lines that make up the triangle | 
|---|
| 58 | * \param number number of triangle | 
|---|
| 59 | */ | 
|---|
| 60 | BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number) : | 
|---|
| 61 | Nr(number) | 
|---|
| 62 | { | 
|---|
| 63 | //Info FunctionInfo(__func__); | 
|---|
| 64 | // set number | 
|---|
| 65 | // set lines | 
|---|
| 66 | for (int i = 0; i < 3; i++) { | 
|---|
| 67 | lines[i] = line[i]; | 
|---|
| 68 | lines[i]->AddTriangle(this); | 
|---|
| 69 | } | 
|---|
| 70 | // get ascending order of endpoints | 
|---|
| 71 | PointMap OrderMap; | 
|---|
| 72 | for (int i = 0; i < 3; i++) { | 
|---|
| 73 | // for all three lines | 
|---|
| 74 | for (int j = 0; j < 2; j++) { // for both endpoints | 
|---|
| 75 | OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j])); | 
|---|
| 76 | // and we don't care whether insertion fails | 
|---|
| 77 | } | 
|---|
| 78 | } | 
|---|
| 79 | // set endpoints | 
|---|
| 80 | int Counter = 0; | 
|---|
| 81 | LOG(4, "DEBUG: New triangle " << Nr << " with end points: "); | 
|---|
| 82 | for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) { | 
|---|
| 83 | endpoints[Counter] = runner->second; | 
|---|
| 84 | LOG(4, "DEBUG:    " << *endpoints[Counter]); | 
|---|
| 85 | Counter++; | 
|---|
| 86 | } | 
|---|
| 87 | ASSERT(Counter >= 3,"We have a triangle with only two distinct endpoints!"); | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | /** Destructor of BoundaryTriangleSet. | 
|---|
| 92 | * Removes itself from each of its lines' LineMap and removes them if necessary. | 
|---|
| 93 | * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle() | 
|---|
| 94 | */ | 
|---|
| 95 | BoundaryTriangleSet::~BoundaryTriangleSet() | 
|---|
| 96 | { | 
|---|
| 97 | //Info FunctionInfo(__func__); | 
|---|
| 98 | for (int i = 0; i < 3; i++) { | 
|---|
| 99 | if (lines[i] != NULL) { | 
|---|
| 100 | if (lines[i]->triangles.erase(Nr)) { | 
|---|
| 101 | //LOG(5, "DEBUG: Triangle Nr." << Nr << " erased in line " << *lines[i] << "."); | 
|---|
| 102 | } | 
|---|
| 103 | if (lines[i]->triangles.empty()) { | 
|---|
| 104 | //LOG(5, "DEBUG: " << *lines[i] << " is no more attached to any triangle, erasing."); | 
|---|
| 105 | delete (lines[i]); | 
|---|
| 106 | lines[i] = NULL; | 
|---|
| 107 | } | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 | //LOG(5, "DEBUG: Erasing triangle Nr." << Nr << " itself."); | 
|---|
| 111 | } | 
|---|
| 112 | ; | 
|---|
| 113 |  | 
|---|
| 114 | /** Calculates the area of this triangle. | 
|---|
| 115 | * | 
|---|
| 116 | * @return surface area in between the tree points of this triangle | 
|---|
| 117 | */ | 
|---|
| 118 | double BoundaryTriangleSet::getArea() const | 
|---|
| 119 | { | 
|---|
| 120 | Vector x; | 
|---|
| 121 | Vector y; | 
|---|
| 122 | x = getEndpoint(0) - getEndpoint(1); | 
|---|
| 123 | y = getEndpoint(0) - getEndpoint(2); | 
|---|
| 124 | const double a = x.Norm(); | 
|---|
| 125 | const double b = y.Norm(); | 
|---|
| 126 | const double c = getEndpoint(2).distance(getEndpoint(1)); | 
|---|
| 127 | const double area = sqrt(((a + b + c) * (a + b + c) - 2 * (a * a + b * b + c * c)) / 16.); // area of tesselated triangle | 
|---|
| 128 | return area; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /** Calculates the normal vector for this triangle. | 
|---|
| 132 | * Is made unique by comparison with \a OtherVector to point in the other direction. | 
|---|
| 133 | * \param &OtherVector direction vector to make normal vector unique. | 
|---|
| 134 | */ | 
|---|
| 135 | void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector) | 
|---|
| 136 | { | 
|---|
| 137 | //Info FunctionInfo(__func__); | 
|---|
| 138 | // get normal vector | 
|---|
| 139 | NormalVector = Plane((endpoints[0]->node->getPosition()), | 
|---|
| 140 | (endpoints[1]->node->getPosition()), | 
|---|
| 141 | (endpoints[2]->node->getPosition())).getNormal(); | 
|---|
| 142 |  | 
|---|
| 143 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices) | 
|---|
| 144 | if (NormalVector.ScalarProduct(OtherVector) > 0.) | 
|---|
| 145 | NormalVector.Scale(-1.); | 
|---|
| 146 | LOG(4, "DEBUG: Normal Vector of " << *this << " is " << NormalVector << "."); | 
|---|
| 147 | } | 
|---|
| 148 | ; | 
|---|
| 149 |  | 
|---|
| 150 | /** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses. | 
|---|
| 151 | * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane | 
|---|
| 152 | * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not. | 
|---|
| 153 | * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line | 
|---|
| 154 | * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between | 
|---|
| 155 | * the first two basepoints) or not. | 
|---|
| 156 | * \param *out output stream for debugging | 
|---|
| 157 | * \param &MolCenter offset vector of line | 
|---|
| 158 | * \param &x second endpoint of line, minus \a *MolCenter is directional vector of line | 
|---|
| 159 | * \param &Intersection intersection on plane on return | 
|---|
| 160 | * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle. | 
|---|
| 161 | */ | 
|---|
| 162 |  | 
|---|
| 163 | bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector & MolCenter, const Vector & x, Vector &Intersection) const | 
|---|
| 164 | { | 
|---|
| 165 | //Info FunctionInfo(__func__); | 
|---|
| 166 | Vector CrossPoint; | 
|---|
| 167 | Vector helper; | 
|---|
| 168 |  | 
|---|
| 169 | try { | 
|---|
| 170 | Line centerLine = makeLineThrough(MolCenter, x); | 
|---|
| 171 | Intersection = Plane(NormalVector, (endpoints[0]->node->getPosition())).GetIntersection(centerLine); | 
|---|
| 172 |  | 
|---|
| 173 | LOG(4, "DEBUG: Triangle is " << *this << "."); | 
|---|
| 174 | LOG(4, "DEBUG: Line is from " << MolCenter << " to " << x << "."); | 
|---|
| 175 | LOG(4, "DEBUG: Intersection is " << Intersection << "."); | 
|---|
| 176 |  | 
|---|
| 177 | if (Intersection.DistanceSquared(endpoints[0]->node->getPosition()) < MYEPSILON) { | 
|---|
| 178 | LOG(4, "DEBUG: Intersection coindices with first endpoint."); | 
|---|
| 179 | return true; | 
|---|
| 180 | }   else if (Intersection.DistanceSquared(endpoints[1]->node->getPosition()) < MYEPSILON) { | 
|---|
| 181 | LOG(4, "DEBUG: Intersection coindices with second endpoint."); | 
|---|
| 182 | return true; | 
|---|
| 183 | }   else if (Intersection.DistanceSquared(endpoints[2]->node->getPosition()) < MYEPSILON) { | 
|---|
| 184 | LOG(4, "DEBUG: Intersection coindices with third endpoint."); | 
|---|
| 185 | return true; | 
|---|
| 186 | } | 
|---|
| 187 | // Calculate cross point between one baseline and the line from the third endpoint to intersection | 
|---|
| 188 | int i = 0; | 
|---|
| 189 | do { | 
|---|
| 190 | Line line1 = makeLineThrough((endpoints[i%3]->node->getPosition()),(endpoints[(i+1)%3]->node->getPosition())); | 
|---|
| 191 | Line line2 = makeLineThrough((endpoints[(i+2)%3]->node->getPosition()),Intersection); | 
|---|
| 192 | CrossPoint = line1.getIntersection(line2); | 
|---|
| 193 | helper = (endpoints[(i+1)%3]->node->getPosition()) - (endpoints[i%3]->node->getPosition()); | 
|---|
| 194 | CrossPoint -= (endpoints[i%3]->node->getPosition());  // cross point was returned as absolute vector | 
|---|
| 195 | const double s = CrossPoint.ScalarProduct(helper)/helper.NormSquared(); | 
|---|
| 196 | LOG(4, "DEBUG: Factor s is " << s << "."); | 
|---|
| 197 | if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) { | 
|---|
| 198 | LOG(4, "DEBUG: Crosspoint " << CrossPoint << "outside of triangle."); | 
|---|
| 199 | return false; | 
|---|
| 200 | } | 
|---|
| 201 | i++; | 
|---|
| 202 | } while (i < 3); | 
|---|
| 203 | LOG(4, "DEBUG: Crosspoint " << CrossPoint << " inside of triangle."); | 
|---|
| 204 | return true; | 
|---|
| 205 | } | 
|---|
| 206 | catch (LinearAlgebraException &excp) { | 
|---|
| 207 | LOG(1, boost::diagnostic_information(excp)); | 
|---|
| 208 | ELOG(1, "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!"); | 
|---|
| 209 | return false; | 
|---|
| 210 | } | 
|---|
| 211 | return true; | 
|---|
| 212 | } | 
|---|
| 213 |  | 
|---|
| 214 |  | 
|---|
| 215 | /** Finds the point on the triangle to the point \a *x. | 
|---|
| 216 | * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point. | 
|---|
| 217 | * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the | 
|---|
| 218 | * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down. | 
|---|
| 219 | * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not. | 
|---|
| 220 | * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line | 
|---|
| 221 | * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between | 
|---|
| 222 | * the first two basepoints) or not. | 
|---|
| 223 | * \param *x point | 
|---|
| 224 | * \param *ClosestPoint desired closest point inside triangle to \a *x, is absolute vector | 
|---|
| 225 | * \return Distance squared between \a *x and closest point inside triangle | 
|---|
| 226 | */ | 
|---|
| 227 | double BoundaryTriangleSet::GetClosestPointInsideTriangle(const Vector &x, Vector &ClosestPoint) const | 
|---|
| 228 | { | 
|---|
| 229 | //Info FunctionInfo(__func__); | 
|---|
| 230 | Vector Direction; | 
|---|
| 231 |  | 
|---|
| 232 | // 1. get intersection with plane | 
|---|
| 233 | LOG(3, "DEBUG: Looking for closest point of triangle " << *this << " to " << x << "."); | 
|---|
| 234 | LOG(3, "DEBUG: endpoints are " << endpoints[0]->node->getPosition() << "," | 
|---|
| 235 | << endpoints[1]->node->getPosition() << ", and " << endpoints[2]->node->getPosition() << "."); | 
|---|
| 236 | try { | 
|---|
| 237 | ClosestPoint = Plane(NormalVector, (endpoints[0]->node->getPosition())).getClosestPoint(x); | 
|---|
| 238 | } | 
|---|
| 239 | catch (LinearAlgebraException &excp) { | 
|---|
| 240 | (ClosestPoint) = (x); | 
|---|
| 241 | } | 
|---|
| 242 | Vector InPlane(ClosestPoint); // points from plane intersection to straight-down point | 
|---|
| 243 | LOG(4, "DEBUG: Closest point on triangle plane is " << ClosestPoint << "."); | 
|---|
| 244 |  | 
|---|
| 245 | // 2. Calculate in plane part of line (x, intersection) | 
|---|
| 246 |  | 
|---|
| 247 | // Calculate cross point between one baseline and the desired point such that distance is shortest | 
|---|
| 248 | Vector CrossDirection[3]; | 
|---|
| 249 | Vector CrossPoint[3]; | 
|---|
| 250 | for (int i = 0; i < 3; i++) { | 
|---|
| 251 | const Vector Direction = (endpoints[i%3]->node->getPosition()) - (endpoints[(i+1)%3]->node->getPosition()); | 
|---|
| 252 | // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal); | 
|---|
| 253 | Line l = makeLineThrough((endpoints[i%3]->node->getPosition()), (endpoints[(i+1)%3]->node->getPosition())); | 
|---|
| 254 | CrossPoint[i] = l.getClosestPoint(InPlane); | 
|---|
| 255 | // NOTE: direction of line is normalized, hence s must not necessarily be in [0,1] for the baseline | 
|---|
| 256 | LOG(4, "DEBUG: Closest point on line from " << (endpoints[(i+1)%3]->node->getPosition()) | 
|---|
| 257 | << " to " << (endpoints[i%3]->node->getPosition()) << " is " << CrossPoint[i] << "."); | 
|---|
| 258 | CrossPoint[i] -= (endpoints[(i+1)%3]->node->getPosition());  // cross point was returned as absolute vector | 
|---|
| 259 | const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared(); | 
|---|
| 260 | LOG(5, "DEBUG: Factor s is " << s << "."); | 
|---|
| 261 | if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) { | 
|---|
| 262 | CrossPoint[i] += (endpoints[(i+1)%3]->node->getPosition());  // make cross point absolute again | 
|---|
| 263 | LOG(5, "DEBUG: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between " | 
|---|
| 264 | << endpoints[i % 3]->node->getPosition() << " and " | 
|---|
| 265 | << endpoints[(i + 1) % 3]->node->getPosition() << "."); | 
|---|
| 266 | } else { | 
|---|
| 267 | // set to either endpoint of BoundaryLine | 
|---|
| 268 | if (s < -MYEPSILON) | 
|---|
| 269 | CrossPoint[i] = (endpoints[(i+1)%3]->node->getPosition()); | 
|---|
| 270 | else | 
|---|
| 271 | CrossPoint[i] = (endpoints[i%3]->node->getPosition()); | 
|---|
| 272 | LOG(5, "DEBUG: Crosspoint is " << CrossPoint[i] << ", intersecting outside of BoundaryLine between " | 
|---|
| 273 | << endpoints[i % 3]->node->getPosition() << " and " | 
|---|
| 274 | << endpoints[(i + 1) % 3]->node->getPosition() << "."); | 
|---|
| 275 | } | 
|---|
| 276 | CrossDirection[i] = CrossPoint[i] - InPlane; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | bool InsideFlag = true; | 
|---|
| 280 | double ShortestDistance = -1.; | 
|---|
| 281 | for (int i = 0; i < 3; i++) { | 
|---|
| 282 | const double sign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 1) % 3]); | 
|---|
| 283 | const double othersign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 2) % 3]); | 
|---|
| 284 |  | 
|---|
| 285 | if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign | 
|---|
| 286 | InsideFlag = false; | 
|---|
| 287 | // update current best candidate | 
|---|
| 288 | const double distance = CrossPoint[i].DistanceSquared(x); | 
|---|
| 289 | if ((ShortestDistance < 0.) || (ShortestDistance > distance)) { | 
|---|
| 290 | ShortestDistance = distance; | 
|---|
| 291 | (ClosestPoint) = CrossPoint[i]; | 
|---|
| 292 | } | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | if (InsideFlag) { | 
|---|
| 296 | (ClosestPoint) = InPlane; | 
|---|
| 297 | ShortestDistance = InPlane.DistanceSquared(x); | 
|---|
| 298 | } | 
|---|
| 299 | LOG(3, "DEBUG: Closest Point is " << ClosestPoint << " with shortest squared distance is " << ShortestDistance << "."); | 
|---|
| 300 |  | 
|---|
| 301 | return ShortestDistance; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 |  | 
|---|
| 305 | /** Checks whether lines is any of the three boundary lines this triangle contains. | 
|---|
| 306 | * \param *line line to test | 
|---|
| 307 | * \return true - line is of the triangle, false - is not | 
|---|
| 308 | */ | 
|---|
| 309 | bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const | 
|---|
| 310 | { | 
|---|
| 311 | //Info FunctionInfo(__func__); | 
|---|
| 312 | for (int i = 0; i < 3; i++) | 
|---|
| 313 | if (line == lines[i]) | 
|---|
| 314 | return true; | 
|---|
| 315 | return false; | 
|---|
| 316 | } | 
|---|
| 317 | ; | 
|---|
| 318 |  | 
|---|
| 319 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 320 | * \param *point point to test | 
|---|
| 321 | * \return true - point is of the triangle, false - is not | 
|---|
| 322 | */ | 
|---|
| 323 | bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const | 
|---|
| 324 | { | 
|---|
| 325 | //Info FunctionInfo(__func__); | 
|---|
| 326 | for (int i = 0; i < 3; i++) | 
|---|
| 327 | if (point == endpoints[i]) | 
|---|
| 328 | return true; | 
|---|
| 329 | return false; | 
|---|
| 330 | } | 
|---|
| 331 | ; | 
|---|
| 332 |  | 
|---|
| 333 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
| 334 | * \param *point TesselPoint to test | 
|---|
| 335 | * \return true - point is of the triangle, false - is not | 
|---|
| 336 | */ | 
|---|
| 337 | bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const | 
|---|
| 338 | { | 
|---|
| 339 | //Info FunctionInfo(__func__); | 
|---|
| 340 | for (int i = 0; i < 3; i++) | 
|---|
| 341 | if (point == endpoints[i]->node) | 
|---|
| 342 | return true; | 
|---|
| 343 | return false; | 
|---|
| 344 | } | 
|---|
| 345 | ; | 
|---|
| 346 |  | 
|---|
| 347 | /** Checks whether three given \a *Points coincide with triangle's endpoints. | 
|---|
| 348 | * \param *Points[3] pointer to BoundaryPointSet | 
|---|
| 349 | * \return true - is the very triangle, false - is not | 
|---|
| 350 | */ | 
|---|
| 351 | bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const | 
|---|
| 352 | { | 
|---|
| 353 | //Info FunctionInfo(__func__); | 
|---|
| 354 | LOG(1, "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "."); | 
|---|
| 355 | return (((endpoints[0] == Points[0]) || (endpoints[0] == Points[1]) || (endpoints[0] == Points[2])) && ((endpoints[1] == Points[0]) || (endpoints[1] == Points[1]) || (endpoints[1] == Points[2])) && ((endpoints[2] == Points[0]) || (endpoints[2] == Points[1]) || (endpoints[2] == Points[2]) | 
|---|
| 356 |  | 
|---|
| 357 | )); | 
|---|
| 358 | } | 
|---|
| 359 | ; | 
|---|
| 360 |  | 
|---|
| 361 | /** Checks whether three given \a *Points coincide with triangle's endpoints. | 
|---|
| 362 | * \param *Points[3] pointer to BoundaryPointSet | 
|---|
| 363 | * \return true - is the very triangle, false - is not | 
|---|
| 364 | */ | 
|---|
| 365 | bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const | 
|---|
| 366 | { | 
|---|
| 367 | //Info FunctionInfo(__func__); | 
|---|
| 368 | return (((endpoints[0] == T->endpoints[0]) || (endpoints[0] == T->endpoints[1]) || (endpoints[0] == T->endpoints[2])) && ((endpoints[1] == T->endpoints[0]) || (endpoints[1] == T->endpoints[1]) || (endpoints[1] == T->endpoints[2])) && ((endpoints[2] == T->endpoints[0]) || (endpoints[2] == T->endpoints[1]) || (endpoints[2] == T->endpoints[2]) | 
|---|
| 369 |  | 
|---|
| 370 | )); | 
|---|
| 371 | } | 
|---|
| 372 | ; | 
|---|
| 373 |  | 
|---|
| 374 | /** Checks whether a given point is inside the plane of the triangle and inside the | 
|---|
| 375 | * bounds defined by its BoundaryLineSet's. | 
|---|
| 376 | * | 
|---|
| 377 | * @param point point to check | 
|---|
| 378 | * @return true - point is inside place and inside all BoundaryLine's | 
|---|
| 379 | */ | 
|---|
| 380 | bool BoundaryTriangleSet::IsInsideTriangle(const Vector &point) const | 
|---|
| 381 | { | 
|---|
| 382 | Info FunctionInfo(__func__); | 
|---|
| 383 |  | 
|---|
| 384 | // check if it's inside the plane | 
|---|
| 385 | try { | 
|---|
| 386 | Plane trianglePlane( | 
|---|
| 387 | endpoints[0]->node->getPosition(), | 
|---|
| 388 | endpoints[1]->node->getPosition(), | 
|---|
| 389 | endpoints[2]->node->getPosition()); | 
|---|
| 390 | if (!trianglePlane.isContained(point)) { | 
|---|
| 391 | LOG(1, "INFO: Point " << point << " is not inside plane " << trianglePlane << " by " | 
|---|
| 392 | << trianglePlane.distance(point) << "."); | 
|---|
| 393 | return false; | 
|---|
| 394 | } | 
|---|
| 395 | } catch(LinearDependenceException) { | 
|---|
| 396 | // triangle is degenerated, it's just a line (i.e. one endpoint is right in between two others | 
|---|
| 397 | for (size_t i = 0; i < NDIM; ++i) { | 
|---|
| 398 | try { | 
|---|
| 399 | Line l = makeLineThrough( | 
|---|
| 400 | lines[i]->endpoints[0]->node->getPosition(), | 
|---|
| 401 | lines[i]->endpoints[1]->node->getPosition()); | 
|---|
| 402 | if (l.isContained(GetThirdEndpoint(lines[i])->node->getPosition())) { | 
|---|
| 403 | // we have the largest of the three lines | 
|---|
| 404 | LOG(1, "INFO: Linear-dependent case where point " << point << " is on line " << l << "."); | 
|---|
| 405 | return (l.isContained(point)); | 
|---|
| 406 | } | 
|---|
| 407 | } catch(ZeroVectorException) { | 
|---|
| 408 | // two points actually coincide | 
|---|
| 409 | try { | 
|---|
| 410 | Line l = makeLineThrough( | 
|---|
| 411 | lines[i]->endpoints[0]->node->getPosition(), | 
|---|
| 412 | GetThirdEndpoint(lines[i])->node->getPosition()); | 
|---|
| 413 | LOG(1, "INFO: Degenerated case where point " << point << " is on line " << l << "."); | 
|---|
| 414 | return (l.isContained(point)); | 
|---|
| 415 | } catch(ZeroVectorException) { | 
|---|
| 416 | // all three points coincide | 
|---|
| 417 | if (point.DistanceSquared(lines[i]->endpoints[0]->node->getPosition()) < MYEPSILON) { | 
|---|
| 418 | LOG(1, "INFO: Full-Degenerated case where point " << point << " is on three endpoints " | 
|---|
| 419 | << lines[i]->endpoints[0]->node->getPosition() << "."); | 
|---|
| 420 | return true; | 
|---|
| 421 | } | 
|---|
| 422 | else return false; | 
|---|
| 423 | } | 
|---|
| 424 | } | 
|---|
| 425 | } | 
|---|
| 426 | } | 
|---|
| 427 |  | 
|---|
| 428 | // check whether it lies on the correct side as given by third endpoint for | 
|---|
| 429 | // each BoundaryLine. | 
|---|
| 430 | // NOTE: we assume here that endpoints are linear independent, as the case | 
|---|
| 431 | // has been caught before already extensively | 
|---|
| 432 | for (size_t i = 0; i < NDIM; ++i) { | 
|---|
| 433 | Line l = makeLineThrough( | 
|---|
| 434 | lines[i]->endpoints[0]->node->getPosition(), | 
|---|
| 435 | lines[i]->endpoints[1]->node->getPosition()); | 
|---|
| 436 | Vector onLine( l.getClosestPoint(point) ); | 
|---|
| 437 | LOG(1, "INFO: Closest point on boundary line is " << onLine << "."); | 
|---|
| 438 | Vector inTriangleDirection( GetThirdEndpoint(lines[i])->node->getPosition() - onLine ); | 
|---|
| 439 | Vector inPointDirection(point - onLine); | 
|---|
| 440 | if ((inTriangleDirection.NormSquared() > MYEPSILON) && (inPointDirection.NormSquared() > MYEPSILON)) | 
|---|
| 441 | if (inTriangleDirection.ScalarProduct(inPointDirection) < -MYEPSILON) | 
|---|
| 442 | return false; | 
|---|
| 443 | } | 
|---|
| 444 |  | 
|---|
| 445 | return true; | 
|---|
| 446 | } | 
|---|
| 447 |  | 
|---|
| 448 |  | 
|---|
| 449 | /** Returns the endpoint which is not contained in the given \a *line. | 
|---|
| 450 | * \param *line baseline defining two endpoints | 
|---|
| 451 | * \return pointer third endpoint or NULL if line does not belong to triangle. | 
|---|
| 452 | */ | 
|---|
| 453 | class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const | 
|---|
| 454 | { | 
|---|
| 455 | //Info FunctionInfo(__func__); | 
|---|
| 456 | // sanity check | 
|---|
| 457 | if (!ContainsBoundaryLine(line)) | 
|---|
| 458 | return NULL; | 
|---|
| 459 | for (int i = 0; i < 3; i++) | 
|---|
| 460 | if (!line->ContainsBoundaryPoint(endpoints[i])) | 
|---|
| 461 | return endpoints[i]; | 
|---|
| 462 | // actually, that' impossible :) | 
|---|
| 463 | return NULL; | 
|---|
| 464 | } | 
|---|
| 465 | ; | 
|---|
| 466 |  | 
|---|
| 467 | /** Returns the baseline which does not contain the given boundary point \a *point. | 
|---|
| 468 | * \param *point endpoint which is neither endpoint of the desired line | 
|---|
| 469 | * \return pointer to desired third baseline | 
|---|
| 470 | */ | 
|---|
| 471 | class BoundaryLineSet *BoundaryTriangleSet::GetThirdLine(const BoundaryPointSet * const point) const | 
|---|
| 472 | { | 
|---|
| 473 | //Info FunctionInfo(__func__); | 
|---|
| 474 | // sanity check | 
|---|
| 475 | if (!ContainsBoundaryPoint(point)) | 
|---|
| 476 | return NULL; | 
|---|
| 477 | for (int i = 0; i < 3; i++) | 
|---|
| 478 | if (!lines[i]->ContainsBoundaryPoint(point)) | 
|---|
| 479 | return lines[i]; | 
|---|
| 480 | // actually, that' impossible :) | 
|---|
| 481 | return NULL; | 
|---|
| 482 | } | 
|---|
| 483 | ; | 
|---|
| 484 |  | 
|---|
| 485 | /** Calculates the center point of the triangle. | 
|---|
| 486 | * Is third of the sum of all endpoints. | 
|---|
| 487 | * \param *center central point on return. | 
|---|
| 488 | */ | 
|---|
| 489 | void BoundaryTriangleSet::GetCenter(Vector & center) const | 
|---|
| 490 | { | 
|---|
| 491 | //Info FunctionInfo(__func__); | 
|---|
| 492 | center.Zero(); | 
|---|
| 493 | for (int i = 0; i < 3; i++) | 
|---|
| 494 | (center) += (endpoints[i]->node->getPosition()); | 
|---|
| 495 | center.Scale(1. / 3.); | 
|---|
| 496 | LOG(4, "DEBUG: Center of BoundaryTriangleSet is at " << center << "."); | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | /** | 
|---|
| 500 | * gets the Plane defined by the three triangle Basepoints | 
|---|
| 501 | */ | 
|---|
| 502 | Plane BoundaryTriangleSet::getPlane() const{ | 
|---|
| 503 | ASSERT(endpoints[0] && endpoints[1] && endpoints[2], "Triangle not fully defined"); | 
|---|
| 504 |  | 
|---|
| 505 | return Plane(endpoints[0]->node->getPosition(), | 
|---|
| 506 | endpoints[1]->node->getPosition(), | 
|---|
| 507 | endpoints[2]->node->getPosition()); | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | Vector BoundaryTriangleSet::getEndpoint(int i) const{ | 
|---|
| 511 | ASSERT(i>=0 && i<3,"Index of Endpoint out of Range"); | 
|---|
| 512 |  | 
|---|
| 513 | return endpoints[i]->node->getPosition(); | 
|---|
| 514 | } | 
|---|
| 515 |  | 
|---|
| 516 | string BoundaryTriangleSet::getEndpointName(int i) const{ | 
|---|
| 517 | ASSERT(i>=0 && i<3,"Index of Endpoint out of Range"); | 
|---|
| 518 |  | 
|---|
| 519 | return endpoints[i]->node->getName(); | 
|---|
| 520 | } | 
|---|
| 521 |  | 
|---|
| 522 | /** output operator for BoundaryTriangleSet. | 
|---|
| 523 | * \param &ost output stream | 
|---|
| 524 | * \param &a boundary triangle | 
|---|
| 525 | */ | 
|---|
| 526 | ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a) | 
|---|
| 527 | { | 
|---|
| 528 | ost << "[" << a.Nr << "|" << a.getEndpointName(0) << "," << a.getEndpointName(1) << "," << a.getEndpointName(2) << "]"; | 
|---|
| 529 | //  ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << "," | 
|---|
| 530 | //      << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]"; | 
|---|
| 531 | return ost; | 
|---|
| 532 | } | 
|---|
| 533 | ; | 
|---|
| 534 |  | 
|---|