| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * triangleintersectionlist.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  * This files encapsulates the TriangleIntersectionList class where all matters related to points in space being how close to
 | 
|---|
| 27 |  * a tesselated surface are answered, i.e. distance, is inside, ...
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  *  Created on: Mar 1, 2010
 | 
|---|
| 30 |  *      Author: heber
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | 
 | 
|---|
| 33 | // include config.h
 | 
|---|
| 34 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 35 | #include <config.h>
 | 
|---|
| 36 | #endif
 | 
|---|
| 37 | 
 | 
|---|
| 38 | //#include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include <boost/scoped_ptr.hpp>
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "triangleintersectionlist.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include "tesselation.hpp"
 | 
|---|
| 45 | #include "BoundaryMaps.hpp"
 | 
|---|
| 46 | #include "BoundaryPointSet.hpp"
 | 
|---|
| 47 | #include "BoundaryLineSet.hpp"
 | 
|---|
| 48 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
| 49 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 50 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 51 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 52 | #include "Helpers/defs.hpp"
 | 
|---|
| 53 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /** Constructor for class TriangleIntersectionList.
 | 
|---|
| 56 |  *
 | 
|---|
| 57 |  */
 | 
|---|
| 58 | TriangleIntersectionList::TriangleIntersectionList(const Vector &x, const Tesselation *surface, const LinkedCell_deprecated* LC) :
 | 
|---|
| 59 |   Point(x),
 | 
|---|
| 60 |   Tess(surface),
 | 
|---|
| 61 |   Vicinity(LC)
 | 
|---|
| 62 | {
 | 
|---|
| 63 |   //Info FunctionInfo(__func__);
 | 
|---|
| 64 |   GatherIntersectionsWithTriangles();
 | 
|---|
| 65 | };
 | 
|---|
| 66 | 
 | 
|---|
| 67 | /** Destructor for class TriangleIntersectionList.
 | 
|---|
| 68 |  * Free's all allocated intersection vectors
 | 
|---|
| 69 |  */
 | 
|---|
| 70 | TriangleIntersectionList::~TriangleIntersectionList()
 | 
|---|
| 71 | {
 | 
|---|
| 72 |   //Info FunctionInfo(__func__);
 | 
|---|
| 73 |   for (TriangleVectorMap::iterator Runner = IntersectionList.begin(); Runner != IntersectionList.end(); Runner++)
 | 
|---|
| 74 |     delete((*Runner).second);
 | 
|---|
| 75 | };
 | 
|---|
| 76 | 
 | 
|---|
| 77 | /** Returns the smallest distance between TriangleIntersectionList::Point and the surface given by
 | 
|---|
| 78 |  * TriangleIntersectionList::Tess.
 | 
|---|
| 79 |  * \return distance >=0, -1 if no specific distance could be found
 | 
|---|
| 80 |  */
 | 
|---|
| 81 | double TriangleIntersectionList::GetSmallestDistance() const
 | 
|---|
| 82 | {
 | 
|---|
| 83 |   //Info FunctionInfo(__func__);
 | 
|---|
| 84 |   FillDistanceList();
 | 
|---|
| 85 |   if (!DistanceList.empty())
 | 
|---|
| 86 |     return DistanceList.begin()->first;
 | 
|---|
| 87 |   else
 | 
|---|
| 88 |     // return reference, if none can be found
 | 
|---|
| 89 |     return -1.;
 | 
|---|
| 90 | };
 | 
|---|
| 91 | 
 | 
|---|
| 92 | /** Returns the vector of closest intersection between TriangleIntersectionList::Point and the surface
 | 
|---|
| 93 |  * TriangleIntersectionList::Tess.
 | 
|---|
| 94 |  * \return Intersection vector or TriangleIntersectionList::Point if none could be found
 | 
|---|
| 95 |  */
 | 
|---|
| 96 | Vector TriangleIntersectionList::GetClosestIntersection() const
 | 
|---|
| 97 | {
 | 
|---|
| 98 |   //Info FunctionInfo(__func__);
 | 
|---|
| 99 |   TriangleVectorMap::const_iterator runner;
 | 
|---|
| 100 |   runner = GetIteratortoSmallestDistance();
 | 
|---|
| 101 |   if (runner != IntersectionList.end()) {
 | 
|---|
| 102 |     return *((*runner).second);
 | 
|---|
| 103 |   }
 | 
|---|
| 104 |   // return reference, if none can be found
 | 
|---|
| 105 |   return Point;
 | 
|---|
| 106 | };
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /** Returns the triangle closest to TriangleIntersectionList::Point and the surface
 | 
|---|
| 109 |  * TriangleIntersectionList::Tess.
 | 
|---|
| 110 |  * \return pointer to triangle or NULL if none could be found
 | 
|---|
| 111 |  */
 | 
|---|
| 112 | BoundaryTriangleSet * TriangleIntersectionList::GetClosestTriangle() const
 | 
|---|
| 113 | {
 | 
|---|
| 114 |   //Info FunctionInfo(__func__);
 | 
|---|
| 115 |   TriangleVectorMap::const_iterator runner;
 | 
|---|
| 116 |   runner = GetIteratortoSmallestDistance();
 | 
|---|
| 117 |   if (runner != IntersectionList.end()) {
 | 
|---|
| 118 |     return ((*runner).first);
 | 
|---|
| 119 |   }
 | 
|---|
| 120 |   // return reference, if none can be found
 | 
|---|
| 121 |   return NULL;
 | 
|---|
| 122 | };
 | 
|---|
| 123 | 
 | 
|---|
| 124 | /** Checks whether reference TriangleIntersectionList::Point of this class is inside or outside.
 | 
|---|
| 125 |  * \return true - TriangleIntersectionList::Point is inside, false - is outside
 | 
|---|
| 126 |  */
 | 
|---|
| 127 | bool TriangleIntersectionList::IsInside() const
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   //Info FunctionInfo(__func__);
 | 
|---|
| 130 |   TriangleVectorMap::const_iterator runner = GetIteratortoSmallestDistance();
 | 
|---|
| 131 |   if (runner != IntersectionList.end()) {
 | 
|---|
| 132 |     // if we have found one, check Scalarproduct between the vector
 | 
|---|
| 133 |     Vector TestVector = (Point) - (*(*runner).second);
 | 
|---|
| 134 |     LOG(4, "DEBUG: Distance vector between point " << Point
 | 
|---|
| 135 |         << " and triangle intersection at " << (*(*runner).second) << " is "
 | 
|---|
| 136 |         << TestVector << ".");
 | 
|---|
| 137 |     if (fabs(TestVector.NormSquared()) < MYEPSILON)  {//
 | 
|---|
| 138 |       LOG(3, "ACCEPT: Point is on the intersected triangle.");
 | 
|---|
| 139 |       return true;
 | 
|---|
| 140 |     }
 | 
|---|
| 141 |     const double sign = (*runner).first->NormalVector.ScalarProduct(TestVector);
 | 
|---|
| 142 |     LOG(4, "DEBUG: Checking sign of SKP between Distance vector and triangle's NormalVector "
 | 
|---|
| 143 |         << (*runner).first->NormalVector << ": " << sign << ".");
 | 
|---|
| 144 |     if (sign < 0) {
 | 
|---|
| 145 |       LOG(3, "ACCEPT: Point lies on inner side of intersected triangle.");
 | 
|---|
| 146 |       return true;
 | 
|---|
| 147 |     } else {
 | 
|---|
| 148 |       LOG(3, "REJECT: Point lies on outer side of intersected triangle.");
 | 
|---|
| 149 |       return false;
 | 
|---|
| 150 |     }
 | 
|---|
| 151 |   }
 | 
|---|
| 152 |   // so far away from surface that there are no triangles in list
 | 
|---|
| 153 |   return false;
 | 
|---|
| 154 | };
 | 
|---|
| 155 | 
 | 
|---|
| 156 | /** Puts all triangles in vicinity (by \a *LC) into a hash map with Intersection vectors.
 | 
|---|
| 157 |  * Private function for constructor of TriangleIntersectionList.
 | 
|---|
| 158 |  */
 | 
|---|
| 159 | void TriangleIntersectionList::GatherIntersectionsWithTriangles()
 | 
|---|
| 160 | {
 | 
|---|
| 161 |   //Info FunctionInfo(__func__);
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   // get closest points
 | 
|---|
| 164 |   boost::scoped_ptr< DistanceToPointMap > points(Tess->FindClosestBoundaryPointsToVector(Point,Vicinity));
 | 
|---|
| 165 |   if (points == NULL) {
 | 
|---|
| 166 |     ELOG(1, "There is no nearest point: too far away from the surface.");
 | 
|---|
| 167 |     return;
 | 
|---|
| 168 |   }
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   // gather all triangles in *LC-neighbourhood
 | 
|---|
| 171 |   TriangleSet triangles;
 | 
|---|
| 172 |   for (DistanceToPointMap::iterator Runner = points->begin(); Runner != points->end(); Runner++)
 | 
|---|
| 173 |     for (LineMap::iterator LineRunner = Runner->second->lines.begin(); LineRunner != Runner->second->lines.end(); LineRunner++)
 | 
|---|
| 174 |       for (TriangleMap::iterator TriangleRunner = LineRunner->second->triangles.begin(); TriangleRunner != LineRunner->second->triangles.end(); TriangleRunner++)
 | 
|---|
| 175 |         triangles.insert(TriangleRunner->second);
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   Vector *Intersection = NULL;
 | 
|---|
| 178 |   for (TriangleSet::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); TriangleRunner++) {
 | 
|---|
| 179 |     // get intersection with triangle plane
 | 
|---|
| 180 |     Intersection = new Vector;
 | 
|---|
| 181 |     (*TriangleRunner)->GetClosestPointInsideTriangle(Point, *Intersection);
 | 
|---|
| 182 |     //LOG(1, "Intersection between " << Point << " and " << **TriangleRunner << " is at " << *Intersection << ".");
 | 
|---|
| 183 |     IntersectionList.insert( pair<BoundaryTriangleSet *, Vector * > (*TriangleRunner, Intersection) );
 | 
|---|
| 184 |   }
 | 
|---|
| 185 | };
 | 
|---|
| 186 | 
 | 
|---|
| 187 | /** Fills the private distance list
 | 
|---|
| 188 |  */
 | 
|---|
| 189 | void TriangleIntersectionList::FillDistanceList() const
 | 
|---|
| 190 | {
 | 
|---|
| 191 |   //Info FunctionInfo(__func__);
 | 
|---|
| 192 |   if (DistanceList.empty())
 | 
|---|
| 193 |     for (TriangleVectorMap::const_iterator runner = IntersectionList.begin(); runner != IntersectionList.end(); runner++) {
 | 
|---|
| 194 |       // when the closest point is on the edge of a triangle (and hence
 | 
|---|
| 195 |       // we find two closes triangles due to it having an adjacent one)
 | 
|---|
| 196 |       // we should make sure that both triangles end up in the same entry
 | 
|---|
| 197 |       // in the distance multimap. Hence, we round to 6 digit precision.
 | 
|---|
| 198 |       const double distance =
 | 
|---|
| 199 |           1e-6*floor(Point.distance(*(*runner).second)*1e+6);
 | 
|---|
| 200 |       DistanceList.insert( pair<double, BoundaryTriangleSet *> (distance, (*runner).first) );
 | 
|---|
| 201 |     }
 | 
|---|
| 202 |   //for (DistanceTriangleMap::const_iterator runner = DistanceList.begin(); runner != DistanceList.end(); runner++)
 | 
|---|
| 203 |   //  LOG(1, (*runner).first << " away from " << *(*runner).second);
 | 
|---|
| 204 | };
 | 
|---|
| 205 | 
 | 
|---|
| 206 | 
 | 
|---|
| 207 | /** Returns the iterator in VectorTriangleMap to the smallest distance.
 | 
|---|
| 208 |  * This is a private helper function as the iterator is then evaluated in some other functions.
 | 
|---|
| 209 |  * \return iterator or TriangleIntersectionList::IntersectionList.end() if none could be found
 | 
|---|
| 210 |  */
 | 
|---|
| 211 | TriangleVectorMap::const_iterator TriangleIntersectionList::GetIteratortoSmallestDistance() const
 | 
|---|
| 212 | {
 | 
|---|
| 213 |   //Info FunctionInfo(__func__);
 | 
|---|
| 214 |   FillDistanceList();
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   // do we have any intersections?
 | 
|---|
| 217 |   TriangleVectorMap::const_iterator runner = IntersectionList.end();
 | 
|---|
| 218 |   if (!DistanceList.empty()) {
 | 
|---|
| 219 |     // get closest triangle(s)
 | 
|---|
| 220 |     std::pair< DistanceTriangleMap::const_iterator, DistanceTriangleMap::const_iterator >  DistanceRange =
 | 
|---|
| 221 |         DistanceList.equal_range(DistanceList.begin()->first);
 | 
|---|
| 222 |     {
 | 
|---|
| 223 |       size_t count = 0;
 | 
|---|
| 224 |       for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
 | 
|---|
| 225 |           iter != DistanceRange.second; ++iter) {
 | 
|---|
| 226 |         LOG(4, "DEBUG: " << *(iter->second) << " is in the list with " << iter->first << ".");
 | 
|---|
| 227 |         ++count;
 | 
|---|
| 228 |       }
 | 
|---|
| 229 |       LOG(3, "INFO: There are " << count << " possible triangles at the smallest distance.");
 | 
|---|
| 230 |     }
 | 
|---|
| 231 |     // if there is more than one, check all of their normal vectors
 | 
|---|
| 232 |     // return the one with positive SKP if present because if the point
 | 
|---|
| 233 |     // would be truely inside, all of the closest triangles would have to show
 | 
|---|
| 234 |     // their inner side
 | 
|---|
| 235 |     LOG(4, "DEBUG: Looking for first SKP greater than zero ...");
 | 
|---|
| 236 |     for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
 | 
|---|
| 237 |         iter != DistanceRange.second; ++iter) {
 | 
|---|
| 238 |       // find the entry by the triangle key in IntersectionList to get the Intersection point
 | 
|---|
| 239 |       runner = IntersectionList.find(iter->second);
 | 
|---|
| 240 |       Vector TestVector = (Point) - (*(*runner).second);
 | 
|---|
| 241 |       // construct SKP
 | 
|---|
| 242 |       const double sign = (*iter).second->NormalVector.ScalarProduct(TestVector);
 | 
|---|
| 243 |       LOG(4, "DEBUG: Checking SKP of closest triangle " << *(iter->second) << " = " << sign << ".");
 | 
|---|
| 244 |       // if positive return
 | 
|---|
| 245 |       if (sign > 0)
 | 
|---|
| 246 |         break;
 | 
|---|
| 247 |     }
 | 
|---|
| 248 |     // if there's just one or all are negative, runner is not set yet
 | 
|---|
| 249 |     if (runner == IntersectionList.end())
 | 
|---|
| 250 |       runner = IntersectionList.find(DistanceList.begin()->second);
 | 
|---|
| 251 |   }
 | 
|---|
| 252 |   return runner;
 | 
|---|
| 253 | };
 | 
|---|
| 254 | 
 | 
|---|