| [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 | } | 
|---|
|  | 109 | TotalNormal->Scale(1. / (double) counter); | 
|---|
|  | 110 |  | 
|---|
|  | 111 | // make it always point inward (any offset vector onto plane projected onto normal vector suffices) | 
|---|
|  | 112 | if (TotalNormal->ScalarProduct(OtherVector) > 0.) | 
|---|
|  | 113 | TotalNormal->Scale(-1.); | 
|---|
| [ce7bfd] | 114 | LOG(4, "DEBUG: Normal Vector is " << *TotalNormal << "."); | 
|---|
| [d74077] | 115 |  | 
|---|
|  | 116 | return TotalNormal; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | ; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | /** Calculates the center point of the triangle. | 
|---|
|  | 121 | * Is third of the sum of all endpoints. | 
|---|
|  | 122 | * \param *center central point on return. | 
|---|
|  | 123 | */ | 
|---|
|  | 124 | void BoundaryPolygonSet::GetCenter(Vector * const center) const | 
|---|
|  | 125 | { | 
|---|
| [ce7bfd] | 126 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 127 | center->Zero(); | 
|---|
|  | 128 | int counter = 0; | 
|---|
|  | 129 | for(PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
|  | 130 | (*center) += ((*Runner)->node->getPosition()); | 
|---|
|  | 131 | counter++; | 
|---|
|  | 132 | } | 
|---|
|  | 133 | center->Scale(1. / (double) counter); | 
|---|
| [ce7bfd] | 134 | LOG(4, "DEBUG: Center of BoundaryPolygonSet is at " << *center << "."); | 
|---|
| [d74077] | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | /** Checks whether the polygons contains all three endpoints of the triangle. | 
|---|
|  | 138 | * \param *triangle triangle to test | 
|---|
|  | 139 | * \return true - triangle is contained polygon, false - is not | 
|---|
|  | 140 | */ | 
|---|
|  | 141 | bool BoundaryPolygonSet::ContainsBoundaryTriangle(const BoundaryTriangleSet * const triangle) const | 
|---|
|  | 142 | { | 
|---|
| [ce7bfd] | 143 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 144 | return ContainsPresentTupel(triangle->endpoints, 3); | 
|---|
|  | 145 | } | 
|---|
|  | 146 | ; | 
|---|
|  | 147 |  | 
|---|
|  | 148 | /** Checks whether the polygons contains both endpoints of the line. | 
|---|
|  | 149 | * \param *line line to test | 
|---|
|  | 150 | * \return true - line is of the triangle, false - is not | 
|---|
|  | 151 | */ | 
|---|
|  | 152 | bool BoundaryPolygonSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const | 
|---|
|  | 153 | { | 
|---|
| [ce7bfd] | 154 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 155 | return ContainsPresentTupel(line->endpoints, 2); | 
|---|
|  | 156 | } | 
|---|
|  | 157 | ; | 
|---|
|  | 158 |  | 
|---|
|  | 159 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
|  | 160 | * \param *point point to test | 
|---|
|  | 161 | * \return true - point is of the triangle, false - is not | 
|---|
|  | 162 | */ | 
|---|
|  | 163 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const | 
|---|
|  | 164 | { | 
|---|
| [ce7bfd] | 165 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 166 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
|  | 167 | if (point == (*Runner)) { | 
|---|
| [ce7bfd] | 168 | LOG(4, "DEBUG: Checking against " << **Runner << ": Contained."); | 
|---|
| [d74077] | 169 | return true; | 
|---|
|  | 170 | } | 
|---|
|  | 171 | } | 
|---|
|  | 172 | return false; | 
|---|
|  | 173 | } | 
|---|
|  | 174 | ; | 
|---|
|  | 175 |  | 
|---|
|  | 176 | /** Checks whether point is any of the three endpoints this triangle contains. | 
|---|
|  | 177 | * \param *point TesselPoint to test | 
|---|
|  | 178 | * \return true - point is of the triangle, false - is not | 
|---|
|  | 179 | */ | 
|---|
|  | 180 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const | 
|---|
|  | 181 | { | 
|---|
| [ce7bfd] | 182 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 183 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) | 
|---|
|  | 184 | if (point == (*Runner)->node) { | 
|---|
| [ce7bfd] | 185 | LOG(4, "DEBUG: Checking against " << **Runner << ": Contained."); | 
|---|
| [d74077] | 186 | return true; | 
|---|
|  | 187 | } | 
|---|
|  | 188 | return false; | 
|---|
|  | 189 | } | 
|---|
|  | 190 | ; | 
|---|
|  | 191 |  | 
|---|
|  | 192 | /** Checks whether given array of \a *Points coincide with polygons's endpoints. | 
|---|
|  | 193 | * \param **Points pointer to an array of BoundaryPointSet | 
|---|
|  | 194 | * \param dim dimension of array | 
|---|
|  | 195 | * \return true - set of points is contained in polygon, false - is not | 
|---|
|  | 196 | */ | 
|---|
|  | 197 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const | 
|---|
|  | 198 | { | 
|---|
| [ce7bfd] | 199 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 200 | int counter = 0; | 
|---|
| [ce7bfd] | 201 | LOG(5, "DEBUG Polygon is " << *this); | 
|---|
| [d74077] | 202 | for (int i = 0; i < dim; i++) { | 
|---|
| [ce7bfd] | 203 | LOG(5, "DEBUG: Testing endpoint " << *Points[i]); | 
|---|
| [d74077] | 204 | if (ContainsBoundaryPoint(Points[i])) { | 
|---|
|  | 205 | counter++; | 
|---|
|  | 206 | } | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
|  | 209 | if (counter == dim) | 
|---|
|  | 210 | return true; | 
|---|
|  | 211 | else | 
|---|
|  | 212 | return false; | 
|---|
|  | 213 | } | 
|---|
|  | 214 | ; | 
|---|
|  | 215 |  | 
|---|
|  | 216 | /** Checks whether given PointList coincide with polygons's endpoints. | 
|---|
|  | 217 | * \param &endpoints PointList | 
|---|
|  | 218 | * \return true - set of points is contained in polygon, false - is not | 
|---|
|  | 219 | */ | 
|---|
|  | 220 | bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const | 
|---|
|  | 221 | { | 
|---|
| [ce7bfd] | 222 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 223 | size_t counter = 0; | 
|---|
| [ce7bfd] | 224 | LOG(5, "DEBUG: Polygon is " << *this); | 
|---|
| [d74077] | 225 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) { | 
|---|
| [ce7bfd] | 226 | LOG(5, "DEBUG:  Testing endpoint " << **Runner); | 
|---|
| [d74077] | 227 | if (ContainsBoundaryPoint(*Runner)) | 
|---|
|  | 228 | counter++; | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
|  | 231 | if (counter == endpoints.size()) | 
|---|
|  | 232 | return true; | 
|---|
|  | 233 | else | 
|---|
|  | 234 | return false; | 
|---|
|  | 235 | } | 
|---|
|  | 236 | ; | 
|---|
|  | 237 |  | 
|---|
|  | 238 | /** Checks whether given set of \a *Points coincide with polygons's endpoints. | 
|---|
|  | 239 | * \param *P pointer to BoundaryPolygonSet | 
|---|
|  | 240 | * \return true - is the very triangle, false - is not | 
|---|
|  | 241 | */ | 
|---|
|  | 242 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const | 
|---|
|  | 243 | { | 
|---|
|  | 244 | return ContainsPresentTupel((const PointSet) P->endpoints); | 
|---|
|  | 245 | } | 
|---|
|  | 246 | ; | 
|---|
|  | 247 |  | 
|---|
|  | 248 | /** Gathers all the endpoints' triangles in a unique set. | 
|---|
|  | 249 | * \return set of all triangles | 
|---|
|  | 250 | */ | 
|---|
|  | 251 | TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const | 
|---|
|  | 252 | { | 
|---|
| [ce7bfd] | 253 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 254 | pair<TriangleSet::iterator, bool> Tester; | 
|---|
|  | 255 | TriangleSet *triangles = new TriangleSet; | 
|---|
|  | 256 |  | 
|---|
|  | 257 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) | 
|---|
|  | 258 | for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++) | 
|---|
|  | 259 | for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) { | 
|---|
| [47d041] | 260 | //LOG(0, " Testing triangle " << *(Sprinter->second)); | 
|---|
| [d74077] | 261 | if (ContainsBoundaryTriangle(Sprinter->second)) { | 
|---|
|  | 262 | Tester = triangles->insert(Sprinter->second); | 
|---|
|  | 263 | if (Tester.second) | 
|---|
| [ce7bfd] | 264 | LOG(4, "DEBUG: Adding triangle " << *(Sprinter->second)); | 
|---|
| [d74077] | 265 | } | 
|---|
|  | 266 | } | 
|---|
|  | 267 |  | 
|---|
| [ce7bfd] | 268 | LOG(3, "DEBUG: The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total."); | 
|---|
| [d74077] | 269 | return triangles; | 
|---|
|  | 270 | } | 
|---|
|  | 271 | ; | 
|---|
|  | 272 |  | 
|---|
|  | 273 | /** Fills the endpoints of this polygon from the triangles attached to \a *line. | 
|---|
|  | 274 | * \param *line lines with triangles attached | 
|---|
|  | 275 | * \return true - polygon contains endpoints, false - line was NULL | 
|---|
|  | 276 | */ | 
|---|
|  | 277 | bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line) | 
|---|
|  | 278 | { | 
|---|
| [ce7bfd] | 279 | //Info FunctionInfo(__func__); | 
|---|
| [d74077] | 280 | pair<PointSet::iterator, bool> Tester; | 
|---|
|  | 281 | if (line == NULL) | 
|---|
|  | 282 | return false; | 
|---|
| [ce7bfd] | 283 | LOG(3, "DEBUG: Filling polygon from line " << *line); | 
|---|
| [d74077] | 284 | for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) { | 
|---|
|  | 285 | for (int i = 0; i < 3; i++) { | 
|---|
|  | 286 | Tester = endpoints.insert((Runner->second)->endpoints[i]); | 
|---|
|  | 287 | if (Tester.second) | 
|---|
| [ce7bfd] | 288 | LOG(4, "DEBUG:   Inserting endpoint " << *((Runner->second)->endpoints[i])); | 
|---|
| [d74077] | 289 | } | 
|---|
|  | 290 | } | 
|---|
|  | 291 |  | 
|---|
|  | 292 | return true; | 
|---|
|  | 293 | } | 
|---|
|  | 294 | ; | 
|---|
|  | 295 |  | 
|---|
|  | 296 | /** output operator for BoundaryPolygonSet. | 
|---|
|  | 297 | * \param &ost output stream | 
|---|
|  | 298 | * \param &a boundary polygon | 
|---|
|  | 299 | */ | 
|---|
|  | 300 | ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a) | 
|---|
|  | 301 | { | 
|---|
|  | 302 | ost << "[" << a.Nr << "|"; | 
|---|
|  | 303 | for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) { | 
|---|
|  | 304 | ost << (*Runner)->node->getName(); | 
|---|
|  | 305 | Runner++; | 
|---|
|  | 306 | if (Runner != a.endpoints.end()) | 
|---|
|  | 307 | ost << ","; | 
|---|
|  | 308 | } | 
|---|
|  | 309 | ost << "]"; | 
|---|
|  | 310 | return ost; | 
|---|
|  | 311 | } | 
|---|
|  | 312 | ; | 
|---|
|  | 313 |  | 
|---|