| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [d74077] | 8 | /*
 | 
|---|
 | 9 |  * BoundaryTriangleSet.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Jul 29, 2010
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [bbbad5] | 21 | 
 | 
|---|
| [d74077] | 22 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | #include <iostream>
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #include "BoundaryLineSet.hpp"
 | 
|---|
 | 27 | #include "BoundaryPointSet.hpp"
 | 
|---|
| [6f0841] | 28 | #include "Atom/TesselPoint.hpp"
 | 
|---|
| [d74077] | 29 | 
 | 
|---|
| [06aedc] | 30 | #include "Helpers/defs.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
| [ad011c] | 32 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 33 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 34 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [06aedc] | 35 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [783e88] | 36 | #include "LinearAlgebra/Exceptions.hpp"
 | 
|---|
| [06aedc] | 37 | #include "LinearAlgebra/Line.hpp"
 | 
|---|
| [8f4df1] | 38 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 39 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [d74077] | 40 | 
 | 
|---|
 | 41 | using namespace std;
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | /** Constructor for BoundaryTriangleSet.
 | 
|---|
 | 44 |  */
 | 
|---|
 | 45 | BoundaryTriangleSet::BoundaryTriangleSet() :
 | 
|---|
 | 46 |   Nr(-1)
 | 
|---|
 | 47 | {
 | 
|---|
| [2a3124] | 48 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 63 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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;
 | 
|---|
| [ce7bfd] | 81 |   LOG(4, "DEBUG: New triangle " << Nr << " with end points: ");
 | 
|---|
| [d74077] | 82 |   for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) {
 | 
|---|
 | 83 |     endpoints[Counter] = runner->second;
 | 
|---|
| [ce7bfd] | 84 |     LOG(4, "DEBUG:    " << *endpoints[Counter]);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 97 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 98 |   for (int i = 0; i < 3; i++) {
 | 
|---|
 | 99 |     if (lines[i] != NULL) {
 | 
|---|
 | 100 |       if (lines[i]->triangles.erase(Nr)) {
 | 
|---|
| [ce7bfd] | 101 |         //LOG(5, "DEBUG: Triangle Nr." << Nr << " erased in line " << *lines[i] << ".");
 | 
|---|
| [d74077] | 102 |       }
 | 
|---|
 | 103 |       if (lines[i]->triangles.empty()) {
 | 
|---|
| [ce7bfd] | 104 |         //LOG(5, "DEBUG: " << *lines[i] << " is no more attached to any triangle, erasing.");
 | 
|---|
| [d74077] | 105 |         delete (lines[i]);
 | 
|---|
 | 106 |         lines[i] = NULL;
 | 
|---|
 | 107 |       }
 | 
|---|
 | 108 |     }
 | 
|---|
 | 109 |   }
 | 
|---|
| [ce7bfd] | 110 |   //LOG(5, "DEBUG: Erasing triangle Nr." << Nr << " itself.");
 | 
|---|
| [d74077] | 111 | }
 | 
|---|
 | 112 | ;
 | 
|---|
 | 113 | 
 | 
|---|
| [64b197] | 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 | 
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 137 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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.);
 | 
|---|
| [ce7bfd] | 146 |   LOG(4, "DEBUG: Normal Vector of " << *this << " is " << NormalVector << ".");
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 165 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | 
 | 
|---|
| [ce7bfd] | 173 |     LOG(4, "DEBUG: Triangle is " << *this << ".");
 | 
|---|
 | 174 |     LOG(4, "DEBUG: Line is from " << MolCenter << " to " << x << ".");
 | 
|---|
 | 175 |     LOG(4, "DEBUG: Intersection is " << Intersection << ".");
 | 
|---|
| [d74077] | 176 | 
 | 
|---|
 | 177 |     if (Intersection.DistanceSquared(endpoints[0]->node->getPosition()) < MYEPSILON) {
 | 
|---|
| [ce7bfd] | 178 |       LOG(4, "DEBUG: Intersection coindices with first endpoint.");
 | 
|---|
| [d74077] | 179 |       return true;
 | 
|---|
 | 180 |     }   else if (Intersection.DistanceSquared(endpoints[1]->node->getPosition()) < MYEPSILON) {
 | 
|---|
| [ce7bfd] | 181 |       LOG(4, "DEBUG: Intersection coindices with second endpoint.");
 | 
|---|
| [d74077] | 182 |       return true;
 | 
|---|
 | 183 |     }   else if (Intersection.DistanceSquared(endpoints[2]->node->getPosition()) < MYEPSILON) {
 | 
|---|
| [ce7bfd] | 184 |       LOG(4, "DEBUG: Intersection coindices with third endpoint.");
 | 
|---|
| [d74077] | 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();
 | 
|---|
| [ce7bfd] | 196 |       LOG(4, "DEBUG: Factor s is " << s << ".");
 | 
|---|
| [d74077] | 197 |       if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) {
 | 
|---|
| [ce7bfd] | 198 |         LOG(4, "DEBUG: Crosspoint " << CrossPoint << "outside of triangle.");
 | 
|---|
| [d74077] | 199 |         return false;
 | 
|---|
 | 200 |       }
 | 
|---|
 | 201 |       i++;
 | 
|---|
 | 202 |     } while (i < 3);
 | 
|---|
| [ce7bfd] | 203 |     LOG(4, "DEBUG: Crosspoint " << CrossPoint << " inside of triangle.");
 | 
|---|
| [d74077] | 204 |     return true;
 | 
|---|
 | 205 |   }
 | 
|---|
| [783e88] | 206 |   catch (LinearAlgebraException &excp) {
 | 
|---|
| [47d041] | 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!");
 | 
|---|
| [d74077] | 209 |     return false;
 | 
|---|
 | 210 |   }
 | 
|---|
| [68c923] | 211 |   return true;
 | 
|---|
| [d74077] | 212 | }
 | 
|---|
| [68c923] | 213 | 
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 229 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 230 |   Vector Direction;
 | 
|---|
 | 231 | 
 | 
|---|
| [ce7bfd] | 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() << ","
 | 
|---|
| [6a7fcbb] | 235 |       << endpoints[1]->node->getPosition() << ", and " << endpoints[2]->node->getPosition() << ".");
 | 
|---|
| [d74077] | 236 |   try {
 | 
|---|
| [6a7fcbb] | 237 |     ClosestPoint = Plane(NormalVector, (endpoints[0]->node->getPosition())).getClosestPoint(x);
 | 
|---|
| [d74077] | 238 |   }
 | 
|---|
| [783e88] | 239 |   catch (LinearAlgebraException &excp) {
 | 
|---|
| [d74077] | 240 |     (ClosestPoint) = (x);
 | 
|---|
 | 241 |   }
 | 
|---|
| [6a7fcbb] | 242 |   Vector InPlane(ClosestPoint); // points from plane intersection to straight-down point
 | 
|---|
| [ce7bfd] | 243 |   LOG(4, "DEBUG: Closest point on triangle plane is " << ClosestPoint << ".");
 | 
|---|
| [d74077] | 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++) {
 | 
|---|
| [6a7fcbb] | 251 |     const Vector Direction = (endpoints[i%3]->node->getPosition()) - (endpoints[(i+1)%3]->node->getPosition());
 | 
|---|
| [d74077] | 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()));
 | 
|---|
| [6a7fcbb] | 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
 | 
|---|
| [ce7bfd] | 256 |     LOG(4, "DEBUG: Closest point on line from " << (endpoints[(i+1)%3]->node->getPosition())
 | 
|---|
| [6a7fcbb] | 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
 | 
|---|
| [d74077] | 259 |     const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared();
 | 
|---|
| [ce7bfd] | 260 |     LOG(5, "DEBUG: Factor s is " << s << ".");
 | 
|---|
| [d74077] | 261 |     if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) {
 | 
|---|
| [6a7fcbb] | 262 |       CrossPoint[i] += (endpoints[(i+1)%3]->node->getPosition());  // make cross point absolute again
 | 
|---|
| [ce7bfd] | 263 |       LOG(5, "DEBUG: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between "
 | 
|---|
| [6a7fcbb] | 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());
 | 
|---|
| [ce7bfd] | 272 |       LOG(5, "DEBUG: Crosspoint is " << CrossPoint[i] << ", intersecting outside of BoundaryLine between "
 | 
|---|
| [6a7fcbb] | 273 |           << endpoints[i % 3]->node->getPosition() << " and "
 | 
|---|
 | 274 |           << endpoints[(i + 1) % 3]->node->getPosition() << ".");
 | 
|---|
 | 275 |     }
 | 
|---|
 | 276 |     CrossDirection[i] = CrossPoint[i] - InPlane;
 | 
|---|
| [d74077] | 277 |   }
 | 
|---|
| [6a7fcbb] | 278 | 
 | 
|---|
 | 279 |   bool InsideFlag = true;
 | 
|---|
 | 280 |   double ShortestDistance = -1.;
 | 
|---|
| [d74077] | 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;
 | 
|---|
| [6a7fcbb] | 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 |     }
 | 
|---|
| [d74077] | 293 |   }
 | 
|---|
| [6a7fcbb] | 294 | 
 | 
|---|
| [d74077] | 295 |   if (InsideFlag) {
 | 
|---|
 | 296 |     (ClosestPoint) = InPlane;
 | 
|---|
 | 297 |     ShortestDistance = InPlane.DistanceSquared(x);
 | 
|---|
 | 298 |   }
 | 
|---|
| [ce7bfd] | 299 |   LOG(3, "DEBUG: Closest Point is " << ClosestPoint << " with shortest squared distance is " << ShortestDistance << ".");
 | 
|---|
| [6a7fcbb] | 300 | 
 | 
|---|
| [d74077] | 301 |   return ShortestDistance;
 | 
|---|
 | 302 | }
 | 
|---|
| [ce7bfd] | 303 | 
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 311 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 325 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 339 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 353 |   //Info FunctionInfo(__func__);
 | 
|---|
| [47d041] | 354 |   LOG(1, "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << ".");
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 367 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | 
 | 
|---|
| [471dec] | 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 | 
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 455 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 473 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 | {
 | 
|---|
| [2a3124] | 491 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 492 |   center.Zero();
 | 
|---|
 | 493 |   for (int i = 0; i < 3; i++)
 | 
|---|
 | 494 |     (center) += (endpoints[i]->node->getPosition());
 | 
|---|
 | 495 |   center.Scale(1. / 3.);
 | 
|---|
| [ce7bfd] | 496 |   LOG(4, "DEBUG: Center of BoundaryTriangleSet is at " << center << ".");
 | 
|---|
| [d74077] | 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 | 
 | 
|---|