| [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 |  * BoundaryPolygonSet.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 "BoundaryPolygonSet.hpp"
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | #include <iostream>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | #include "BoundaryLineSet.hpp"
 | 
|---|
 | 42 | #include "BoundaryPointSet.hpp"
 | 
|---|
 | 43 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
| [6f0841] | 44 | #include "Atom/TesselPoint.hpp"
 | 
|---|
| [d74077] | 45 | 
 | 
|---|
| [ad011c] | 46 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 47 | #include "CodePatterns/Info.hpp"
 | 
|---|
 | 48 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [255829] | 49 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
 | 50 | #include "Helpers/helpers.hpp"
 | 
|---|
| [8f4df1] | 51 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 52 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [d74077] | 53 | 
 | 
|---|
 | 54 | using namespace std;
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | /** Constructor for BoundaryPolygonSet.
 | 
|---|
 | 57 |  */
 | 
|---|
 | 58 | BoundaryPolygonSet::BoundaryPolygonSet() :
 | 
|---|
 | 59 |   Nr(-1)
 | 
|---|
 | 60 | {
 | 
|---|
| [ce7bfd] | 61 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 62 | }
 | 
|---|
 | 63 | ;
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | /** Destructor of BoundaryPolygonSet.
 | 
|---|
 | 66 |  * Just clears endpoints.
 | 
|---|
 | 67 |  * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
 | 
|---|
 | 68 |  */
 | 
|---|
 | 69 | BoundaryPolygonSet::~BoundaryPolygonSet()
 | 
|---|
 | 70 | {
 | 
|---|
| [ce7bfd] | 71 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 72 |   endpoints.clear();
 | 
|---|
| [ce7bfd] | 73 |   LOG(5, "DEBUG: Erasing polygon Nr." << Nr << " itself.");
 | 
|---|
| [d74077] | 74 | }
 | 
|---|
 | 75 | ;
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | /** Calculates the normal vector for this triangle.
 | 
|---|
 | 78 |  * Is made unique by comparison with \a OtherVector to point in the other direction.
 | 
|---|
 | 79 |  * \param &OtherVector direction vector to make normal vector unique.
 | 
|---|
 | 80 |  * \return allocated vector in normal direction
 | 
|---|
 | 81 |  */
 | 
|---|
 | 82 | Vector * BoundaryPolygonSet::GetNormalVector(const Vector &OtherVector) const
 | 
|---|
 | 83 | {
 | 
|---|
| [ce7bfd] | 84 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 85 |   // get normal vector
 | 
|---|
 | 86 |   Vector TemporaryNormal;
 | 
|---|
 | 87 |   Vector *TotalNormal = new Vector;
 | 
|---|
 | 88 |   PointSet::const_iterator Runner[3];
 | 
|---|
 | 89 |   for (int i = 0; i < 3; i++) {
 | 
|---|
 | 90 |     Runner[i] = endpoints.begin();
 | 
|---|
 | 91 |     for (int j = 0; j < i; j++) { // go as much further
 | 
|---|
 | 92 |       Runner[i]++;
 | 
|---|
 | 93 |       if (Runner[i] == endpoints.end()) {
 | 
|---|
| [47d041] | 94 |         ELOG(0, "There are less than three endpoints in the polygon!");
 | 
|---|
| [d74077] | 95 |         performCriticalExit();
 | 
|---|
 | 96 |       }
 | 
|---|
 | 97 |     }
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 |   TotalNormal->Zero();
 | 
|---|
 | 100 |   int counter = 0;
 | 
|---|
 | 101 |   for (; Runner[2] != endpoints.end();) {
 | 
|---|
 | 102 |     TemporaryNormal = Plane(((*Runner[0])->node->getPosition()),
 | 
|---|
 | 103 |                             ((*Runner[1])->node->getPosition()),
 | 
|---|
 | 104 |                             ((*Runner[2])->node->getPosition())).getNormal();
 | 
|---|
 | 105 |     for (int i = 0; i < 3; i++) // increase each of them
 | 
|---|
 | 106 |       Runner[i]++;
 | 
|---|
 | 107 |     (*TotalNormal) += TemporaryNormal;
 | 
|---|
 | 108 |   }
 | 
|---|
| [76096d] | 109 |   if (counter != 0.)
 | 
|---|
 | 110 |     TotalNormal->Scale(1. / (double) counter);
 | 
|---|
| [d74077] | 111 | 
 | 
|---|
 | 112 |   // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
 | 
|---|
 | 113 |   if (TotalNormal->ScalarProduct(OtherVector) > 0.)
 | 
|---|
 | 114 |     TotalNormal->Scale(-1.);
 | 
|---|
| [ce7bfd] | 115 |   LOG(4, "DEBUG: Normal Vector is " << *TotalNormal << ".");
 | 
|---|
| [d74077] | 116 | 
 | 
|---|
 | 117 |   return TotalNormal;
 | 
|---|
 | 118 | }
 | 
|---|
 | 119 | ;
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | /** Calculates the center point of the triangle.
 | 
|---|
 | 122 |  * Is third of the sum of all endpoints.
 | 
|---|
 | 123 |  * \param *center central point on return.
 | 
|---|
 | 124 |  */
 | 
|---|
 | 125 | void BoundaryPolygonSet::GetCenter(Vector * const center) const
 | 
|---|
 | 126 | {
 | 
|---|
| [ce7bfd] | 127 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 128 |   center->Zero();
 | 
|---|
 | 129 |   int counter = 0;
 | 
|---|
 | 130 |   for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
 | 
|---|
 | 131 |     (*center) += ((*Runner)->node->getPosition());
 | 
|---|
 | 132 |     counter++;
 | 
|---|
 | 133 |   }
 | 
|---|
 | 134 |   center->Scale(1. / (double) counter);
 | 
|---|
| [ce7bfd] | 135 |   LOG(4, "DEBUG: Center of BoundaryPolygonSet is at " << *center << ".");
 | 
|---|
| [d74077] | 136 | }
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 | /** Checks whether the polygons contains all three endpoints of the triangle.
 | 
|---|
 | 139 |  * \param *triangle triangle to test
 | 
|---|
 | 140 |  * \return true - triangle is contained polygon, false - is not
 | 
|---|
 | 141 |  */
 | 
|---|
 | 142 | bool BoundaryPolygonSet::ContainsBoundaryTriangle(const BoundaryTriangleSet * const triangle) const
 | 
|---|
 | 143 | {
 | 
|---|
| [ce7bfd] | 144 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 145 |   return ContainsPresentTupel(triangle->endpoints, 3);
 | 
|---|
 | 146 | }
 | 
|---|
 | 147 | ;
 | 
|---|
 | 148 | 
 | 
|---|
 | 149 | /** Checks whether the polygons contains both endpoints of the line.
 | 
|---|
 | 150 |  * \param *line line to test
 | 
|---|
 | 151 |  * \return true - line is of the triangle, false - is not
 | 
|---|
 | 152 |  */
 | 
|---|
 | 153 | bool BoundaryPolygonSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
 | 
|---|
 | 154 | {
 | 
|---|
| [ce7bfd] | 155 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 156 |   return ContainsPresentTupel(line->endpoints, 2);
 | 
|---|
 | 157 | }
 | 
|---|
 | 158 | ;
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 | /** Checks whether point is any of the three endpoints this triangle contains.
 | 
|---|
 | 161 |  * \param *point point to test
 | 
|---|
 | 162 |  * \return true - point is of the triangle, false - is not
 | 
|---|
 | 163 |  */
 | 
|---|
 | 164 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
 | 
|---|
 | 165 | {
 | 
|---|
| [ce7bfd] | 166 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 167 |   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
 | 
|---|
 | 168 |     if (point == (*Runner)) {
 | 
|---|
| [ce7bfd] | 169 |       LOG(4, "DEBUG: Checking against " << **Runner << ": Contained.");
 | 
|---|
| [d74077] | 170 |       return true;
 | 
|---|
 | 171 |     }
 | 
|---|
 | 172 |   }
 | 
|---|
 | 173 |   return false;
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | ;
 | 
|---|
 | 176 | 
 | 
|---|
 | 177 | /** Checks whether point is any of the three endpoints this triangle contains.
 | 
|---|
 | 178 |  * \param *point TesselPoint to test
 | 
|---|
 | 179 |  * \return true - point is of the triangle, false - is not
 | 
|---|
 | 180 |  */
 | 
|---|
 | 181 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const
 | 
|---|
 | 182 | {
 | 
|---|
| [ce7bfd] | 183 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 184 |   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
 | 
|---|
 | 185 |     if (point == (*Runner)->node) {
 | 
|---|
| [ce7bfd] | 186 |       LOG(4, "DEBUG: Checking against " << **Runner << ": Contained.");
 | 
|---|
| [d74077] | 187 |       return true;
 | 
|---|
 | 188 |     }
 | 
|---|
 | 189 |   return false;
 | 
|---|
 | 190 | }
 | 
|---|
 | 191 | ;
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 | /** Checks whether given array of \a *Points coincide with polygons's endpoints.
 | 
|---|
 | 194 |  * \param **Points pointer to an array of BoundaryPointSet
 | 
|---|
 | 195 |  * \param dim dimension of array
 | 
|---|
 | 196 |  * \return true - set of points is contained in polygon, false - is not
 | 
|---|
 | 197 |  */
 | 
|---|
 | 198 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const
 | 
|---|
 | 199 | {
 | 
|---|
| [ce7bfd] | 200 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 201 |   int counter = 0;
 | 
|---|
| [ce7bfd] | 202 |   LOG(5, "DEBUG Polygon is " << *this);
 | 
|---|
| [d74077] | 203 |   for (int i = 0; i < dim; i++) {
 | 
|---|
| [ce7bfd] | 204 |     LOG(5, "DEBUG: Testing endpoint " << *Points[i]);
 | 
|---|
| [d74077] | 205 |     if (ContainsBoundaryPoint(Points[i])) {
 | 
|---|
 | 206 |       counter++;
 | 
|---|
 | 207 |     }
 | 
|---|
 | 208 |   }
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 |   if (counter == dim)
 | 
|---|
 | 211 |     return true;
 | 
|---|
 | 212 |   else
 | 
|---|
 | 213 |     return false;
 | 
|---|
 | 214 | }
 | 
|---|
 | 215 | ;
 | 
|---|
 | 216 | 
 | 
|---|
 | 217 | /** Checks whether given PointList coincide with polygons's endpoints.
 | 
|---|
 | 218 |  * \param &endpoints PointList
 | 
|---|
 | 219 |  * \return true - set of points is contained in polygon, false - is not
 | 
|---|
 | 220 |  */
 | 
|---|
 | 221 | bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const
 | 
|---|
 | 222 | {
 | 
|---|
| [ce7bfd] | 223 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 224 |   size_t counter = 0;
 | 
|---|
| [ce7bfd] | 225 |   LOG(5, "DEBUG: Polygon is " << *this);
 | 
|---|
| [d74077] | 226 |   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
 | 
|---|
| [ce7bfd] | 227 |     LOG(5, "DEBUG:  Testing endpoint " << **Runner);
 | 
|---|
| [d74077] | 228 |     if (ContainsBoundaryPoint(*Runner))
 | 
|---|
 | 229 |       counter++;
 | 
|---|
 | 230 |   }
 | 
|---|
 | 231 | 
 | 
|---|
 | 232 |   if (counter == endpoints.size())
 | 
|---|
 | 233 |     return true;
 | 
|---|
 | 234 |   else
 | 
|---|
 | 235 |     return false;
 | 
|---|
 | 236 | }
 | 
|---|
 | 237 | ;
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 | /** Checks whether given set of \a *Points coincide with polygons's endpoints.
 | 
|---|
 | 240 |  * \param *P pointer to BoundaryPolygonSet
 | 
|---|
 | 241 |  * \return true - is the very triangle, false - is not
 | 
|---|
 | 242 |  */
 | 
|---|
 | 243 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const
 | 
|---|
 | 244 | {
 | 
|---|
 | 245 |   return ContainsPresentTupel((const PointSet) P->endpoints);
 | 
|---|
 | 246 | }
 | 
|---|
 | 247 | ;
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 | /** Gathers all the endpoints' triangles in a unique set.
 | 
|---|
 | 250 |  * \return set of all triangles
 | 
|---|
 | 251 |  */
 | 
|---|
 | 252 | TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const
 | 
|---|
 | 253 | {
 | 
|---|
| [ce7bfd] | 254 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 255 |   pair<TriangleSet::iterator, bool> Tester;
 | 
|---|
 | 256 |   TriangleSet *triangles = new TriangleSet;
 | 
|---|
 | 257 | 
 | 
|---|
 | 258 |   for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
 | 
|---|
 | 259 |     for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
 | 
|---|
 | 260 |       for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
 | 
|---|
| [47d041] | 261 |         //LOG(0, " Testing triangle " << *(Sprinter->second));
 | 
|---|
| [d74077] | 262 |         if (ContainsBoundaryTriangle(Sprinter->second)) {
 | 
|---|
 | 263 |           Tester = triangles->insert(Sprinter->second);
 | 
|---|
 | 264 |           if (Tester.second)
 | 
|---|
| [ce7bfd] | 265 |             LOG(4, "DEBUG: Adding triangle " << *(Sprinter->second));
 | 
|---|
| [d74077] | 266 |         }
 | 
|---|
 | 267 |       }
 | 
|---|
 | 268 | 
 | 
|---|
| [ce7bfd] | 269 |   LOG(3, "DEBUG: The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total.");
 | 
|---|
| [d74077] | 270 |   return triangles;
 | 
|---|
 | 271 | }
 | 
|---|
 | 272 | ;
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 | /** Fills the endpoints of this polygon from the triangles attached to \a *line.
 | 
|---|
 | 275 |  * \param *line lines with triangles attached
 | 
|---|
 | 276 |  * \return true - polygon contains endpoints, false - line was NULL
 | 
|---|
 | 277 |  */
 | 
|---|
 | 278 | bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line)
 | 
|---|
 | 279 | {
 | 
|---|
| [ce7bfd] | 280 |   //Info FunctionInfo(__func__);
 | 
|---|
| [d74077] | 281 |   pair<PointSet::iterator, bool> Tester;
 | 
|---|
 | 282 |   if (line == NULL)
 | 
|---|
 | 283 |     return false;
 | 
|---|
| [ce7bfd] | 284 |   LOG(3, "DEBUG: Filling polygon from line " << *line);
 | 
|---|
| [d74077] | 285 |   for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
 | 
|---|
 | 286 |     for (int i = 0; i < 3; i++) {
 | 
|---|
 | 287 |       Tester = endpoints.insert((Runner->second)->endpoints[i]);
 | 
|---|
 | 288 |       if (Tester.second)
 | 
|---|
| [ce7bfd] | 289 |         LOG(4, "DEBUG:   Inserting endpoint " << *((Runner->second)->endpoints[i]));
 | 
|---|
| [d74077] | 290 |     }
 | 
|---|
 | 291 |   }
 | 
|---|
 | 292 | 
 | 
|---|
 | 293 |   return true;
 | 
|---|
 | 294 | }
 | 
|---|
 | 295 | ;
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 | /** output operator for BoundaryPolygonSet.
 | 
|---|
 | 298 |  * \param &ost output stream
 | 
|---|
 | 299 |  * \param &a boundary polygon
 | 
|---|
 | 300 |  */
 | 
|---|
 | 301 | ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a)
 | 
|---|
 | 302 | {
 | 
|---|
 | 303 |   ost << "[" << a.Nr << "|";
 | 
|---|
 | 304 |   for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
 | 
|---|
 | 305 |     ost << (*Runner)->node->getName();
 | 
|---|
 | 306 |     Runner++;
 | 
|---|
 | 307 |     if (Runner != a.endpoints.end())
 | 
|---|
 | 308 |       ost << ",";
 | 
|---|
 | 309 |   }
 | 
|---|
 | 310 |   ost << "]";
 | 
|---|
 | 311 |   return ost;
 | 
|---|
 | 312 | }
 | 
|---|
 | 313 | ;
 | 
|---|
 | 314 | 
 | 
|---|