[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[d74077] | 8 | /*
|
---|
| 9 | * BoundaryPolygonSet.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Jul 29, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 21 |
|
---|
[d74077] | 22 | #include "BoundaryPolygonSet.hpp"
|
---|
| 23 |
|
---|
| 24 | #include <iostream>
|
---|
| 25 |
|
---|
| 26 | #include "BoundaryLineSet.hpp"
|
---|
| 27 | #include "BoundaryPointSet.hpp"
|
---|
| 28 | #include "BoundaryTriangleSet.hpp"
|
---|
| 29 | #include "TesselPoint.hpp"
|
---|
| 30 |
|
---|
[ad011c] | 31 | #include "CodePatterns/Assert.hpp"
|
---|
| 32 | #include "CodePatterns/Info.hpp"
|
---|
| 33 | #include "CodePatterns/Log.hpp"
|
---|
[255829] | 34 | #include "CodePatterns/Verbose.hpp"
|
---|
| 35 | #include "Helpers/helpers.hpp"
|
---|
[8f4df1] | 36 | #include "LinearAlgebra/Plane.hpp"
|
---|
| 37 | #include "LinearAlgebra/Vector.hpp"
|
---|
[d74077] | 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 | DoLog(1) && (Log() << Verbose(1) << "Erasing polygon Nr." << Nr << " itself." << endl);
|
---|
| 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 | DoeLog(0) && (eLog() << Verbose(0) << "There are less than three endpoints in the polygon!" << endl);
|
---|
| 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 | DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << *TotalNormal << "." << endl);
|
---|
| 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 | DoLog(1) && (Log() << Verbose(1) << "Center is at " << *center << "." << endl);
|
---|
| 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 | DoLog(0) && (Log() << Verbose(0) << "Checking against " << **Runner << endl);
|
---|
| 153 | if (point == (*Runner)) {
|
---|
| 154 | DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
|
---|
| 155 | return true;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
|
---|
| 159 | return false;
|
---|
| 160 | }
|
---|
| 161 | ;
|
---|
| 162 |
|
---|
| 163 | /** Checks whether point is any of the three endpoints this triangle contains.
|
---|
| 164 | * \param *point TesselPoint to test
|
---|
| 165 | * \return true - point is of the triangle, false - is not
|
---|
| 166 | */
|
---|
| 167 | bool BoundaryPolygonSet::ContainsBoundaryPoint(const TesselPoint * const point) const
|
---|
| 168 | {
|
---|
| 169 | Info FunctionInfo(__func__);
|
---|
| 170 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
|
---|
| 171 | if (point == (*Runner)->node) {
|
---|
| 172 | DoLog(0) && (Log() << Verbose(0) << " Contained." << endl);
|
---|
| 173 | return true;
|
---|
| 174 | }
|
---|
| 175 | DoLog(0) && (Log() << Verbose(0) << " Not contained." << endl);
|
---|
| 176 | return false;
|
---|
| 177 | }
|
---|
| 178 | ;
|
---|
| 179 |
|
---|
| 180 | /** Checks whether given array of \a *Points coincide with polygons's endpoints.
|
---|
| 181 | * \param **Points pointer to an array of BoundaryPointSet
|
---|
| 182 | * \param dim dimension of array
|
---|
| 183 | * \return true - set of points is contained in polygon, false - is not
|
---|
| 184 | */
|
---|
| 185 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPointSet * const * Points, const int dim) const
|
---|
| 186 | {
|
---|
| 187 | Info FunctionInfo(__func__);
|
---|
| 188 | int counter = 0;
|
---|
| 189 | DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
|
---|
| 190 | for (int i = 0; i < dim; i++) {
|
---|
| 191 | DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << *Points[i] << endl);
|
---|
| 192 | if (ContainsBoundaryPoint(Points[i])) {
|
---|
| 193 | counter++;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | if (counter == dim)
|
---|
| 198 | return true;
|
---|
| 199 | else
|
---|
| 200 | return false;
|
---|
| 201 | }
|
---|
| 202 | ;
|
---|
| 203 |
|
---|
| 204 | /** Checks whether given PointList coincide with polygons's endpoints.
|
---|
| 205 | * \param &endpoints PointList
|
---|
| 206 | * \return true - set of points is contained in polygon, false - is not
|
---|
| 207 | */
|
---|
| 208 | bool BoundaryPolygonSet::ContainsPresentTupel(const PointSet &endpoints) const
|
---|
| 209 | {
|
---|
| 210 | Info FunctionInfo(__func__);
|
---|
| 211 | size_t counter = 0;
|
---|
| 212 | DoLog(1) && (Log() << Verbose(1) << "Polygon is " << *this << endl);
|
---|
| 213 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++) {
|
---|
| 214 | DoLog(1) && (Log() << Verbose(1) << " Testing endpoint " << **Runner << endl);
|
---|
| 215 | if (ContainsBoundaryPoint(*Runner))
|
---|
| 216 | counter++;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | if (counter == endpoints.size())
|
---|
| 220 | return true;
|
---|
| 221 | else
|
---|
| 222 | return false;
|
---|
| 223 | }
|
---|
| 224 | ;
|
---|
| 225 |
|
---|
| 226 | /** Checks whether given set of \a *Points coincide with polygons's endpoints.
|
---|
| 227 | * \param *P pointer to BoundaryPolygonSet
|
---|
| 228 | * \return true - is the very triangle, false - is not
|
---|
| 229 | */
|
---|
| 230 | bool BoundaryPolygonSet::ContainsPresentTupel(const BoundaryPolygonSet * const P) const
|
---|
| 231 | {
|
---|
| 232 | return ContainsPresentTupel((const PointSet) P->endpoints);
|
---|
| 233 | }
|
---|
| 234 | ;
|
---|
| 235 |
|
---|
| 236 | /** Gathers all the endpoints' triangles in a unique set.
|
---|
| 237 | * \return set of all triangles
|
---|
| 238 | */
|
---|
| 239 | TriangleSet * BoundaryPolygonSet::GetAllContainedTrianglesFromEndpoints() const
|
---|
| 240 | {
|
---|
| 241 | Info FunctionInfo(__func__);
|
---|
| 242 | pair<TriangleSet::iterator, bool> Tester;
|
---|
| 243 | TriangleSet *triangles = new TriangleSet;
|
---|
| 244 |
|
---|
| 245 | for (PointSet::const_iterator Runner = endpoints.begin(); Runner != endpoints.end(); Runner++)
|
---|
| 246 | for (LineMap::const_iterator Walker = (*Runner)->lines.begin(); Walker != (*Runner)->lines.end(); Walker++)
|
---|
| 247 | for (TriangleMap::const_iterator Sprinter = (Walker->second)->triangles.begin(); Sprinter != (Walker->second)->triangles.end(); Sprinter++) {
|
---|
| 248 | //Log() << Verbose(0) << " Testing triangle " << *(Sprinter->second) << endl;
|
---|
| 249 | if (ContainsBoundaryTriangle(Sprinter->second)) {
|
---|
| 250 | Tester = triangles->insert(Sprinter->second);
|
---|
| 251 | if (Tester.second)
|
---|
| 252 | DoLog(0) && (Log() << Verbose(0) << "Adding triangle " << *(Sprinter->second) << endl);
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | DoLog(1) && (Log() << Verbose(1) << "The Polygon of " << endpoints.size() << " endpoints has " << triangles->size() << " unique triangles in total." << endl);
|
---|
| 257 | return triangles;
|
---|
| 258 | }
|
---|
| 259 | ;
|
---|
| 260 |
|
---|
| 261 | /** Fills the endpoints of this polygon from the triangles attached to \a *line.
|
---|
| 262 | * \param *line lines with triangles attached
|
---|
| 263 | * \return true - polygon contains endpoints, false - line was NULL
|
---|
| 264 | */
|
---|
| 265 | bool BoundaryPolygonSet::FillPolygonFromTrianglesOfLine(const BoundaryLineSet * const line)
|
---|
| 266 | {
|
---|
| 267 | Info FunctionInfo(__func__);
|
---|
| 268 | pair<PointSet::iterator, bool> Tester;
|
---|
| 269 | if (line == NULL)
|
---|
| 270 | return false;
|
---|
| 271 | DoLog(1) && (Log() << Verbose(1) << "Filling polygon from line " << *line << endl);
|
---|
| 272 | for (TriangleMap::const_iterator Runner = line->triangles.begin(); Runner != line->triangles.end(); Runner++) {
|
---|
| 273 | for (int i = 0; i < 3; i++) {
|
---|
| 274 | Tester = endpoints.insert((Runner->second)->endpoints[i]);
|
---|
| 275 | if (Tester.second)
|
---|
| 276 | DoLog(1) && (Log() << Verbose(1) << " Inserting endpoint " << *((Runner->second)->endpoints[i]) << endl);
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | return true;
|
---|
| 281 | }
|
---|
| 282 | ;
|
---|
| 283 |
|
---|
| 284 | /** output operator for BoundaryPolygonSet.
|
---|
| 285 | * \param &ost output stream
|
---|
| 286 | * \param &a boundary polygon
|
---|
| 287 | */
|
---|
| 288 | ostream &operator <<(ostream &ost, const BoundaryPolygonSet &a)
|
---|
| 289 | {
|
---|
| 290 | ost << "[" << a.Nr << "|";
|
---|
| 291 | for (PointSet::const_iterator Runner = a.endpoints.begin(); Runner != a.endpoints.end();) {
|
---|
| 292 | ost << (*Runner)->node->getName();
|
---|
| 293 | Runner++;
|
---|
| 294 | if (Runner != a.endpoints.end())
|
---|
| 295 | ost << ",";
|
---|
| 296 | }
|
---|
| 297 | ost << "]";
|
---|
| 298 | return ost;
|
---|
| 299 | }
|
---|
| 300 | ;
|
---|
| 301 |
|
---|