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