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