| [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 | 
 | 
|---|
| [d74077] | 23 | /*
 | 
|---|
 | 24 |  * BoundaryLineSet.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Jul 29, 2010
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [ad011c] | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [bbbad5] | 36 | 
 | 
|---|
| [d74077] | 37 | #include "BoundaryLineSet.hpp"
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include <iostream>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "BoundaryPointSet.hpp"
 | 
|---|
 | 42 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
| [6f0841] | 43 | #include "Atom/TesselPoint.hpp"
 | 
|---|
| [d74077] | 44 | 
 | 
|---|
| [ad011c] | 45 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 46 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 47 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 48 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [d74077] | 49 | #include "tesselationhelpers.hpp"
 | 
|---|
| [8f4df1] | 50 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [d74077] | 51 | 
 | 
|---|
 | 52 | using namespace std;
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /** Constructor of BoundaryLineSet.
 | 
|---|
 | 55 |  */
 | 
|---|
 | 56 | BoundaryLineSet::BoundaryLineSet() :
 | 
|---|
 | 57 |   Nr(-1)
 | 
|---|
 | 58 | {
 | 
|---|
| [ce7bfd] | 59 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 60 |   for (int i = 0; i < 2; i++)
 | 
|---|
 | 61 |     endpoints[i] = NULL;
 | 
|---|
 | 62 | }
 | 
|---|
 | 63 | ;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | /** Constructor of BoundaryLineSet with two endpoints.
 | 
|---|
 | 66 |  * Adds line automatically to each endpoints' LineMap
 | 
|---|
 | 67 |  * \param *Point[2] array of two boundary points
 | 
|---|
 | 68 |  * \param number number of the list
 | 
|---|
 | 69 |  */
 | 
|---|
 | 70 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number)
 | 
|---|
 | 71 | {
 | 
|---|
| [ce7bfd] | 72 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 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 |   // set skipped to false
 | 
|---|
 | 81 |   skipped = false;
 | 
|---|
 | 82 |   // clear triangles list
 | 
|---|
| [ce7bfd] | 83 |   LOG(5, "DEBUG: New Line with endpoints " << *this << ".");
 | 
|---|
| [d74077] | 84 | }
 | 
|---|
 | 85 | ;
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | /** Constructor of BoundaryLineSet with two endpoints.
 | 
|---|
 | 88 |  * Adds line automatically to each endpoints' LineMap
 | 
|---|
 | 89 |  * \param *Point1 first boundary point
 | 
|---|
 | 90 |  * \param *Point2 second boundary point
 | 
|---|
 | 91 |  * \param number number of the list
 | 
|---|
 | 92 |  */
 | 
|---|
| [97b825] | 93 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number) :
 | 
|---|
 | 94 |   Nr(number),
 | 
|---|
 | 95 |   skipped(false)
 | 
|---|
| [d74077] | 96 | {
 | 
|---|
| [ce7bfd] | 97 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 98 |   // set endpoints in ascending order
 | 
|---|
 | 99 |   SetEndpointsOrdered(endpoints, Point1, Point2);
 | 
|---|
 | 100 |   // add this line to the hash maps of both endpoints
 | 
|---|
 | 101 |   Point1->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
 | 
|---|
 | 102 |   Point2->AddLine(this); //
 | 
|---|
 | 103 |   // clear triangles list
 | 
|---|
| [ce7bfd] | 104 |   LOG(5, "DEBUG: New Line with endpoints " << *this << ".");
 | 
|---|
| [d74077] | 105 | }
 | 
|---|
 | 106 | ;
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | /** Destructor for BoundaryLineSet.
 | 
|---|
 | 109 |  * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore.
 | 
|---|
 | 110 |  * \note When removing lines from a class Tesselation, use RemoveTesselationLine()
 | 
|---|
 | 111 |  */
 | 
|---|
 | 112 | BoundaryLineSet::~BoundaryLineSet()
 | 
|---|
 | 113 | {
 | 
|---|
| [ce7bfd] | 114 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 115 |   int Numbers[2];
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 |   // get other endpoint number of finding copies of same line
 | 
|---|
 | 118 |   if (endpoints[1] != NULL)
 | 
|---|
 | 119 |     Numbers[0] = endpoints[1]->Nr;
 | 
|---|
 | 120 |   else
 | 
|---|
 | 121 |     Numbers[0] = -1;
 | 
|---|
 | 122 |   if (endpoints[0] != NULL)
 | 
|---|
 | 123 |     Numbers[1] = endpoints[0]->Nr;
 | 
|---|
 | 124 |   else
 | 
|---|
 | 125 |     Numbers[1] = -1;
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |   for (int i = 0; i < 2; i++) {
 | 
|---|
 | 128 |     if (endpoints[i] != NULL) {
 | 
|---|
 | 129 |       if (Numbers[i] != -1) { // 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
 | 
|---|
 | 130 |         pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]);
 | 
|---|
 | 131 |         for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
 | 
|---|
 | 132 |           if ((*Runner).second == this) {
 | 
|---|
| [47d041] | 133 |             //LOG(0, "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << ".");
 | 
|---|
| [d74077] | 134 |             endpoints[i]->lines.erase(Runner);
 | 
|---|
 | 135 |             break;
 | 
|---|
 | 136 |           }
 | 
|---|
 | 137 |       } else { // there's just a single line left
 | 
|---|
 | 138 |         if (endpoints[i]->lines.erase(Nr)) {
 | 
|---|
| [47d041] | 139 |           //LOG(0, "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << ".");
 | 
|---|
| [d74077] | 140 |         }
 | 
|---|
 | 141 |       }
 | 
|---|
 | 142 |       if (endpoints[i]->lines.empty()) {
 | 
|---|
| [47d041] | 143 |         //LOG(0, *endpoints[i] << " has no more lines it's attached to, erasing.");
 | 
|---|
| [d74077] | 144 |         if (endpoints[i] != NULL) {
 | 
|---|
 | 145 |           delete (endpoints[i]);
 | 
|---|
 | 146 |           endpoints[i] = NULL;
 | 
|---|
 | 147 |         }
 | 
|---|
 | 148 |       }
 | 
|---|
 | 149 |     }
 | 
|---|
 | 150 |   }
 | 
|---|
 | 151 |   if (!triangles.empty())
 | 
|---|
| [47d041] | 152 |     ELOG(2, "Memory Leak! I " << *this << " am still connected to some triangles.");
 | 
|---|
| [d74077] | 153 | }
 | 
|---|
 | 154 | ;
 | 
|---|
 | 155 | 
 | 
|---|
 | 156 | /** Add triangle to TriangleMap of this boundary line.
 | 
|---|
 | 157 |  * \param *triangle to add
 | 
|---|
 | 158 |  */
 | 
|---|
 | 159 | void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle)
 | 
|---|
 | 160 | {
 | 
|---|
| [ce7bfd] | 161 |   //Info FunctionInfo(__func__);
 | 
|---|
 | 162 |   LOG(5, "DEBUG: Add " << triangle->Nr << " to line " << *this << ".");
 | 
|---|
| [d74077] | 163 |   triangles.insert(TrianglePair(triangle->Nr, triangle));
 | 
|---|
 | 164 | }
 | 
|---|
 | 165 | ;
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 | /** Checks whether we have a common endpoint with given \a *line.
 | 
|---|
 | 168 |  * \param *line other line to test
 | 
|---|
 | 169 |  * \return true - common endpoint present, false - not connected
 | 
|---|
 | 170 |  */
 | 
|---|
 | 171 | bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const
 | 
|---|
 | 172 | {
 | 
|---|
| [ce7bfd] | 173 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 174 |   if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
 | 
|---|
 | 175 |     return true;
 | 
|---|
 | 176 |   else
 | 
|---|
 | 177 |     return false;
 | 
|---|
 | 178 | }
 | 
|---|
 | 179 | ;
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | /** Checks whether the adjacent triangles of a baseline are convex or not.
 | 
|---|
 | 182 |  * We sum the two angles of each height vector with respect to the center of the baseline.
 | 
|---|
 | 183 |  * If greater/equal M_PI than we are convex.
 | 
|---|
 | 184 |  * \param *out output stream for debugging
 | 
|---|
 | 185 |  * \return true - triangles are convex, false - concave or less than two triangles connected
 | 
|---|
 | 186 |  */
 | 
|---|
 | 187 | bool BoundaryLineSet::CheckConvexityCriterion() const
 | 
|---|
 | 188 | {
 | 
|---|
| [ce7bfd] | 189 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 190 |   double angle = CalculateConvexity();
 | 
|---|
 | 191 |   if (angle > -MYEPSILON) {
 | 
|---|
| [ce7bfd] | 192 |     LOG(3, "ACCEPT: Angle is greater than pi: convex.");
 | 
|---|
| [d74077] | 193 |     return true;
 | 
|---|
 | 194 |   } else {
 | 
|---|
| [ce7bfd] | 195 |     LOG(3, "REJECT: Angle is less than pi: concave.");
 | 
|---|
| [d74077] | 196 |     return false;
 | 
|---|
 | 197 |   }
 | 
|---|
 | 198 | }
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 | 
 | 
|---|
 | 201 | /** Calculates the angle between two triangles with respect to their normal vector.
 | 
|---|
 | 202 |  * We sum the two angles of each height vector with respect to the center of the baseline.
 | 
|---|
 | 203 |  * \return angle > 0 then convex, if < 0 then concave
 | 
|---|
 | 204 |  */
 | 
|---|
 | 205 | double BoundaryLineSet::CalculateConvexity() const
 | 
|---|
 | 206 | {
 | 
|---|
| [ce7bfd] | 207 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 208 |   Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
 | 
|---|
 | 209 |   // get the two triangles
 | 
|---|
 | 210 |   if (triangles.size() != 2) {
 | 
|---|
| [47d041] | 211 |     ELOG(0, "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!");
 | 
|---|
| [d74077] | 212 |     return true;
 | 
|---|
 | 213 |   }
 | 
|---|
 | 214 |   // check normal vectors
 | 
|---|
 | 215 |   // have a normal vector on the base line pointing outwards
 | 
|---|
| [47d041] | 216 |   //LOG(0, "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << ".");
 | 
|---|
| [d74077] | 217 |   BaseLineCenter = (1./2.)*((endpoints[0]->node->getPosition()) + (endpoints[1]->node->getPosition()));
 | 
|---|
 | 218 |   BaseLine = (endpoints[0]->node->getPosition()) - (endpoints[1]->node->getPosition());
 | 
|---|
 | 219 | 
 | 
|---|
| [47d041] | 220 |   //LOG(0, "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << ".");
 | 
|---|
| [d74077] | 221 | 
 | 
|---|
 | 222 |   BaseLineNormal.Zero();
 | 
|---|
 | 223 |   NormalCheck.Zero();
 | 
|---|
 | 224 |   double sign = -1.;
 | 
|---|
 | 225 |   int i = 0;
 | 
|---|
 | 226 |   class BoundaryPointSet *node = NULL;
 | 
|---|
 | 227 |   for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
 | 
|---|
| [47d041] | 228 |     //LOG(0, "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << ".");
 | 
|---|
| [d74077] | 229 |     NormalCheck += runner->second->NormalVector;
 | 
|---|
 | 230 |     NormalCheck *= sign;
 | 
|---|
 | 231 |     sign = -sign;
 | 
|---|
 | 232 |     if (runner->second->NormalVector.NormSquared() > MYEPSILON)
 | 
|---|
 | 233 |       BaseLineNormal = runner->second->NormalVector;   // yes, copy second on top of first
 | 
|---|
 | 234 |     else {
 | 
|---|
| [47d041] | 235 |       ELOG(0, "Triangle " << *runner->second << " has zero normal vector!");
 | 
|---|
| [d74077] | 236 |     }
 | 
|---|
 | 237 |     node = runner->second->GetThirdEndpoint(this);
 | 
|---|
 | 238 |     if (node != NULL) {
 | 
|---|
| [47d041] | 239 |       //LOG(0, "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << ".");
 | 
|---|
| [d74077] | 240 |       helper[i] = (node->node->getPosition()) - BaseLineCenter;
 | 
|---|
 | 241 |       helper[i].MakeNormalTo(BaseLine);  // we want to compare the triangle's heights' angles!
 | 
|---|
| [47d041] | 242 |       //LOG(0, "INFO: Height vector with respect to baseline is " << helper[i] << ".");
 | 
|---|
| [d74077] | 243 |       i++;
 | 
|---|
 | 244 |     } else {
 | 
|---|
| [47d041] | 245 |       ELOG(1, "I cannot find third node in triangle, something's wrong.");
 | 
|---|
| [d74077] | 246 |       return true;
 | 
|---|
 | 247 |     }
 | 
|---|
 | 248 |   }
 | 
|---|
| [47d041] | 249 |   //LOG(0, "INFO: BaselineNormal is " << BaseLineNormal << ".");
 | 
|---|
| [d74077] | 250 |   if (NormalCheck.NormSquared() < MYEPSILON) {
 | 
|---|
| [ce7bfd] | 251 |     LOG(3, "ACCEPT: Normalvectors of both triangles are the same: convex.");
 | 
|---|
| [d74077] | 252 |     return true;
 | 
|---|
 | 253 |   }
 | 
|---|
 | 254 |   BaseLineNormal.Scale(-1.);
 | 
|---|
 | 255 |   double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
 | 
|---|
 | 256 |   return (angle - M_PI);
 | 
|---|
 | 257 | }
 | 
|---|
 | 258 | 
 | 
|---|
 | 259 | /** Checks whether point is any of the two endpoints this line contains.
 | 
|---|
 | 260 |  * \param *point point to test
 | 
|---|
 | 261 |  * \return true - point is of the line, false - is not
 | 
|---|
 | 262 |  */
 | 
|---|
 | 263 | bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
 | 
|---|
 | 264 | {
 | 
|---|
| [ce7bfd] | 265 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 266 |   for (int i = 0; i < 2; i++)
 | 
|---|
 | 267 |     if (point == endpoints[i])
 | 
|---|
 | 268 |       return true;
 | 
|---|
 | 269 |   return false;
 | 
|---|
 | 270 | }
 | 
|---|
 | 271 | ;
 | 
|---|
 | 272 | 
 | 
|---|
 | 273 | /** Returns other endpoint of the line.
 | 
|---|
 | 274 |  * \param *point other endpoint
 | 
|---|
 | 275 |  * \return NULL - if endpoint not contained in BoundaryLineSet::lines, or pointer to BoundaryPointSet otherwise
 | 
|---|
 | 276 |  */
 | 
|---|
 | 277 | class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const
 | 
|---|
 | 278 | {
 | 
|---|
| [ce7bfd] | 279 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 280 |   if (endpoints[0] == point)
 | 
|---|
 | 281 |     return endpoints[1];
 | 
|---|
 | 282 |   else if (endpoints[1] == point)
 | 
|---|
 | 283 |     return endpoints[0];
 | 
|---|
 | 284 |   else
 | 
|---|
 | 285 |     return NULL;
 | 
|---|
 | 286 | }
 | 
|---|
 | 287 | ;
 | 
|---|
 | 288 | 
 | 
|---|
 | 289 | /** Returns other triangle of the line.
 | 
|---|
 | 290 |  * \param *point other endpoint
 | 
|---|
 | 291 |  * \return NULL - if triangle not contained in BoundaryLineSet::triangles, or pointer to BoundaryTriangleSet otherwise
 | 
|---|
 | 292 |  */
 | 
|---|
 | 293 | class BoundaryTriangleSet *BoundaryLineSet::GetOtherTriangle(const BoundaryTriangleSet * const triangle) const
 | 
|---|
 | 294 | {
 | 
|---|
| [ce7bfd] | 295 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 296 |   if (triangles.size() == 2) {
 | 
|---|
 | 297 |     for (TriangleMap::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); ++TriangleRunner)
 | 
|---|
 | 298 |       if (TriangleRunner->second != triangle)
 | 
|---|
 | 299 |         return TriangleRunner->second;
 | 
|---|
 | 300 |   }
 | 
|---|
 | 301 |   return NULL;
 | 
|---|
 | 302 | }
 | 
|---|
 | 303 | ;
 | 
|---|
 | 304 | 
 | 
|---|
 | 305 | /** output operator for BoundaryLineSet.
 | 
|---|
 | 306 |  * \param &ost output stream
 | 
|---|
 | 307 |  * \param &a boundary line
 | 
|---|
 | 308 |  */
 | 
|---|
 | 309 | ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
 | 
|---|
 | 310 | {
 | 
|---|
| [b8d215] | 311 |   ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << "," << a.endpoints[1]->node->getName() << "]";
 | 
|---|
 | 312 |   //ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << " at " << a.endpoints[0]->node->getPosition() << "," << a.endpoints[1]->node->getName() << " at " << a.endpoints[1]->node->getPosition() << "]";
 | 
|---|
| [d74077] | 313 |   return ost;
 | 
|---|
 | 314 | }
 | 
|---|
 | 315 | ;
 | 
|---|