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