[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[bcf653] | 21 | */
|
---|
| 22 |
|
---|
[8db598] | 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 |
|
---|
[bf3817] | 33 | // include config.h
|
---|
| 34 | #ifdef HAVE_CONFIG_H
|
---|
| 35 | #include <config.h>
|
---|
| 36 | #endif
|
---|
| 37 |
|
---|
[ad011c] | 38 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 39 |
|
---|
[bdc91e] | 40 | #include <boost/scoped_ptr.hpp>
|
---|
| 41 |
|
---|
[8db598] | 42 | #include "triangleintersectionlist.hpp"
|
---|
| 43 |
|
---|
[255829] | 44 | #include "tesselation.hpp"
|
---|
[8f4df1] | 45 | #include "BoundaryMaps.hpp"
|
---|
[d74077] | 46 | #include "BoundaryPointSet.hpp"
|
---|
| 47 | #include "BoundaryLineSet.hpp"
|
---|
| 48 | #include "BoundaryTriangleSet.hpp"
|
---|
[255829] | 49 | #include "CodePatterns/Info.hpp"
|
---|
| 50 | #include "CodePatterns/Log.hpp"
|
---|
[ad011c] | 51 | #include "CodePatterns/Verbose.hpp"
|
---|
[255829] | 52 | #include "Helpers/defs.hpp"
|
---|
| 53 | #include "LinearAlgebra/Vector.hpp"
|
---|
[8db598] | 54 |
|
---|
| 55 | /** Constructor for class TriangleIntersectionList.
|
---|
| 56 | *
|
---|
| 57 | */
|
---|
[6bd7e0] | 58 | TriangleIntersectionList::TriangleIntersectionList(const Vector &x, const Tesselation *surface, const LinkedCell_deprecated* LC) :
|
---|
[8db598] | 59 | Point(x),
|
---|
| 60 | Tess(surface),
|
---|
| 61 | Vicinity(LC)
|
---|
| 62 | {
|
---|
[2a3124] | 63 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 64 | GatherIntersectionsWithTriangles();
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | /** Destructor for class TriangleIntersectionList.
|
---|
| 68 | * Free's all allocated intersection vectors
|
---|
| 69 | */
|
---|
| 70 | TriangleIntersectionList::~TriangleIntersectionList()
|
---|
| 71 | {
|
---|
[2a3124] | 72 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 83 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 98 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 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
|
---|
[d74077] | 105 | return Point;
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 114 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 129 | //Info FunctionInfo(__func__);
|
---|
[418b5e] | 130 | TriangleVectorMap::const_iterator runner = GetIteratortoSmallestDistance();
|
---|
[8db598] | 131 | if (runner != IntersectionList.end()) {
|
---|
| 132 | // if we have found one, check Scalarproduct between the vector
|
---|
[d74077] | 133 | Vector TestVector = (Point) - (*(*runner).second);
|
---|
[418b5e] | 134 | LOG(1, "INFO: Distance vector between point " << Point
|
---|
| 135 | << " and triangle intersection at " << (*(*runner).second) << " is "
|
---|
| 136 | << TestVector << ".");
|
---|
| 137 | if (fabs(TestVector.NormSquared()) < MYEPSILON) {//
|
---|
| 138 | LOG(1, "ACCEPT: Point is on the intersected triangle.");
|
---|
[8db598] | 139 | return true;
|
---|
[418b5e] | 140 | }
|
---|
[8cbb97] | 141 | const double sign = (*runner).first->NormalVector.ScalarProduct(TestVector);
|
---|
[418b5e] | 142 | LOG(1, "INFO: Checking sign of SKP between Distance vector and triangle's NormalVector "
|
---|
| 143 | << (*runner).first->NormalVector << ": " << sign << ".");
|
---|
[8db598] | 144 | if (sign < 0) {
|
---|
[418b5e] | 145 | LOG(1, "ACCEPT: Point lies on inner side of intersected triangle.");
|
---|
[8db598] | 146 | return true;
|
---|
| 147 | } else {
|
---|
[418b5e] | 148 | LOG(1, "REJECT: Point lies on outer side of intersected triangle.");
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 161 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 162 |
|
---|
| 163 | // get closest points
|
---|
[bdc91e] | 164 | boost::scoped_ptr< DistanceToPointMap > points(Tess->FindClosestBoundaryPointsToVector(Point,Vicinity));
|
---|
[8db598] | 165 | if (points == NULL) {
|
---|
[47d041] | 166 | ELOG(1, "There is no nearest point: too far away from the surface.");
|
---|
[8db598] | 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;
|
---|
[d74077] | 181 | (*TriangleRunner)->GetClosestPointInsideTriangle(Point, *Intersection);
|
---|
[47d041] | 182 | //LOG(1, "Intersection between " << Point << " and " << **TriangleRunner << " is at " << *Intersection << ".");
|
---|
[8db598] | 183 | IntersectionList.insert( pair<BoundaryTriangleSet *, Vector * > (*TriangleRunner, Intersection) );
|
---|
| 184 | }
|
---|
| 185 | };
|
---|
| 186 |
|
---|
| 187 | /** Fills the private distance list
|
---|
| 188 | */
|
---|
| 189 | void TriangleIntersectionList::FillDistanceList() const
|
---|
| 190 | {
|
---|
[2a3124] | 191 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 192 | if (DistanceList.empty())
|
---|
[c29c0a] | 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 | }
|
---|
[8db598] | 202 | //for (DistanceTriangleMap::const_iterator runner = DistanceList.begin(); runner != DistanceList.end(); runner++)
|
---|
[47d041] | 203 | // LOG(1, (*runner).first << " away from " << *(*runner).second);
|
---|
[8db598] | 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 | {
|
---|
[2a3124] | 213 | //Info FunctionInfo(__func__);
|
---|
[8db598] | 214 | FillDistanceList();
|
---|
| 215 |
|
---|
| 216 | // do we have any intersections?
|
---|
[418b5e] | 217 | TriangleVectorMap::const_iterator runner = IntersectionList.end();
|
---|
[8db598] | 218 | if (!DistanceList.empty()) {
|
---|
[955b91] | 219 | // get closest triangle(s)
|
---|
[368207] | 220 | std::pair< DistanceTriangleMap::const_iterator, DistanceTriangleMap::const_iterator > DistanceRange =
|
---|
[955b91] | 221 | DistanceList.equal_range(DistanceList.begin()->first);
|
---|
[368207] | 222 | {
|
---|
| 223 | size_t count = 0;
|
---|
| 224 | for (DistanceTriangleMap::const_iterator iter = DistanceRange.first;
|
---|
[c29c0a] | 225 | iter != DistanceRange.second; ++iter) {
|
---|
| 226 | LOG(3, "DEBUG: " << *(iter->second) << " is in the list with " << iter->first << ".");
|
---|
[368207] | 227 | ++count;
|
---|
[c29c0a] | 228 | }
|
---|
[368207] | 229 | LOG(1, "INFO: There are " << count << " possible triangles at the smallest distance.");
|
---|
| 230 | }
|
---|
[955b91] | 231 | // if there is more than one, check all of their normal vectors
|
---|
[368207] | 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(1, "INFO: 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(1, "INFO: 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);
|
---|
[8db598] | 251 | }
|
---|
| 252 | return runner;
|
---|
| 253 | };
|
---|
| 254 |
|
---|