| 1 | /*
 | 
|---|
| 2 |  * BoundaryLineSet.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 "BoundaryLineSet.hpp"
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <iostream>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "BoundaryPointSet.hpp"
 | 
|---|
| 20 | #include "BoundaryTriangleSet.hpp"
 | 
|---|
| 21 | #include "TesselPoint.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | #include "Helpers/Assert.hpp"
 | 
|---|
| 24 | #include "Helpers/Info.hpp"
 | 
|---|
| 25 | #include "Helpers/Log.hpp"
 | 
|---|
| 26 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 27 | #include "tesselationhelpers.hpp"
 | 
|---|
| 28 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | using namespace std;
 | 
|---|
| 31 | 
 | 
|---|
| 32 | /** Constructor of BoundaryLineSet.
 | 
|---|
| 33 |  */
 | 
|---|
| 34 | BoundaryLineSet::BoundaryLineSet() :
 | 
|---|
| 35 |   Nr(-1)
 | 
|---|
| 36 | {
 | 
|---|
| 37 |   Info FunctionInfo(__func__);
 | 
|---|
| 38 |   for (int i = 0; i < 2; i++)
 | 
|---|
| 39 |     endpoints[i] = NULL;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | ;
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /** Constructor of BoundaryLineSet with two endpoints.
 | 
|---|
| 44 |  * Adds line automatically to each endpoints' LineMap
 | 
|---|
| 45 |  * \param *Point[2] array of two boundary points
 | 
|---|
| 46 |  * \param number number of the list
 | 
|---|
| 47 |  */
 | 
|---|
| 48 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point[2], const int number)
 | 
|---|
| 49 | {
 | 
|---|
| 50 |   Info FunctionInfo(__func__);
 | 
|---|
| 51 |   // set number
 | 
|---|
| 52 |   Nr = number;
 | 
|---|
| 53 |   // set endpoints in ascending order
 | 
|---|
| 54 |   SetEndpointsOrdered(endpoints, Point[0], Point[1]);
 | 
|---|
| 55 |   // add this line to the hash maps of both endpoints
 | 
|---|
| 56 |   Point[0]->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
 | 
|---|
| 57 |   Point[1]->AddLine(this); //
 | 
|---|
| 58 |   // set skipped to false
 | 
|---|
| 59 |   skipped = false;
 | 
|---|
| 60 |   // clear triangles list
 | 
|---|
| 61 |   DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
 | 
|---|
| 62 | }
 | 
|---|
| 63 | ;
 | 
|---|
| 64 | 
 | 
|---|
| 65 | /** Constructor of BoundaryLineSet with two endpoints.
 | 
|---|
| 66 |  * Adds line automatically to each endpoints' LineMap
 | 
|---|
| 67 |  * \param *Point1 first boundary point
 | 
|---|
| 68 |  * \param *Point2 second boundary point
 | 
|---|
| 69 |  * \param number number of the list
 | 
|---|
| 70 |  */
 | 
|---|
| 71 | BoundaryLineSet::BoundaryLineSet(BoundaryPointSet * const Point1, BoundaryPointSet * const Point2, const int number) :
 | 
|---|
| 72 |   Nr(number),
 | 
|---|
| 73 |   skipped(false)
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   Info FunctionInfo(__func__);
 | 
|---|
| 76 |   // set endpoints in ascending order
 | 
|---|
| 77 |   SetEndpointsOrdered(endpoints, Point1, Point2);
 | 
|---|
| 78 |   // add this line to the hash maps of both endpoints
 | 
|---|
| 79 |   Point1->AddLine(this); //Taken out, to check whether we can avoid unwanted double adding.
 | 
|---|
| 80 |   Point2->AddLine(this); //
 | 
|---|
| 81 |   // clear triangles list
 | 
|---|
| 82 |   DoLog(0) && (Log() << Verbose(0) << "New Line with endpoints " << *this << "." << endl);
 | 
|---|
| 83 | }
 | 
|---|
| 84 | ;
 | 
|---|
| 85 | 
 | 
|---|
| 86 | /** Destructor for BoundaryLineSet.
 | 
|---|
| 87 |  * Removes itself from each endpoints' LineMap, calling RemoveTrianglePoint() when point not connected anymore.
 | 
|---|
| 88 |  * \note When removing lines from a class Tesselation, use RemoveTesselationLine()
 | 
|---|
| 89 |  */
 | 
|---|
| 90 | BoundaryLineSet::~BoundaryLineSet()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   Info FunctionInfo(__func__);
 | 
|---|
| 93 |   int Numbers[2];
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   // get other endpoint number of finding copies of same line
 | 
|---|
| 96 |   if (endpoints[1] != NULL)
 | 
|---|
| 97 |     Numbers[0] = endpoints[1]->Nr;
 | 
|---|
| 98 |   else
 | 
|---|
| 99 |     Numbers[0] = -1;
 | 
|---|
| 100 |   if (endpoints[0] != NULL)
 | 
|---|
| 101 |     Numbers[1] = endpoints[0]->Nr;
 | 
|---|
| 102 |   else
 | 
|---|
| 103 |     Numbers[1] = -1;
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   for (int i = 0; i < 2; i++) {
 | 
|---|
| 106 |     if (endpoints[i] != NULL) {
 | 
|---|
| 107 |       if (Numbers[i] != -1) { // as there may be multiple lines with same endpoints, we have to go through each and find in the endpoint's line list this line set
 | 
|---|
| 108 |         pair<LineMap::iterator, LineMap::iterator> erasor = endpoints[i]->lines.equal_range(Numbers[i]);
 | 
|---|
| 109 |         for (LineMap::iterator Runner = erasor.first; Runner != erasor.second; Runner++)
 | 
|---|
| 110 |           if ((*Runner).second == this) {
 | 
|---|
| 111 |             //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
 | 
|---|
| 112 |             endpoints[i]->lines.erase(Runner);
 | 
|---|
| 113 |             break;
 | 
|---|
| 114 |           }
 | 
|---|
| 115 |       } else { // there's just a single line left
 | 
|---|
| 116 |         if (endpoints[i]->lines.erase(Nr)) {
 | 
|---|
| 117 |           //Log() << Verbose(0) << "Removing Line Nr. " << Nr << " in boundary point " << *endpoints[i] << "." << endl;
 | 
|---|
| 118 |         }
 | 
|---|
| 119 |       }
 | 
|---|
| 120 |       if (endpoints[i]->lines.empty()) {
 | 
|---|
| 121 |         //Log() << Verbose(0) << *endpoints[i] << " has no more lines it's attached to, erasing." << endl;
 | 
|---|
| 122 |         if (endpoints[i] != NULL) {
 | 
|---|
| 123 |           delete (endpoints[i]);
 | 
|---|
| 124 |           endpoints[i] = NULL;
 | 
|---|
| 125 |         }
 | 
|---|
| 126 |       }
 | 
|---|
| 127 |     }
 | 
|---|
| 128 |   }
 | 
|---|
| 129 |   if (!triangles.empty())
 | 
|---|
| 130 |     DoeLog(2) && (eLog() << Verbose(2) << "Memory Leak! I " << *this << " am still connected to some triangles." << endl);
 | 
|---|
| 131 | }
 | 
|---|
| 132 | ;
 | 
|---|
| 133 | 
 | 
|---|
| 134 | /** Add triangle to TriangleMap of this boundary line.
 | 
|---|
| 135 |  * \param *triangle to add
 | 
|---|
| 136 |  */
 | 
|---|
| 137 | void BoundaryLineSet::AddTriangle(BoundaryTriangleSet * const triangle)
 | 
|---|
| 138 | {
 | 
|---|
| 139 |   Info FunctionInfo(__func__);
 | 
|---|
| 140 |   DoLog(0) && (Log() << Verbose(0) << "Add " << triangle->Nr << " to line " << *this << "." << endl);
 | 
|---|
| 141 |   triangles.insert(TrianglePair(triangle->Nr, triangle));
 | 
|---|
| 142 | }
 | 
|---|
| 143 | ;
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /** Checks whether we have a common endpoint with given \a *line.
 | 
|---|
| 146 |  * \param *line other line to test
 | 
|---|
| 147 |  * \return true - common endpoint present, false - not connected
 | 
|---|
| 148 |  */
 | 
|---|
| 149 | bool BoundaryLineSet::IsConnectedTo(const BoundaryLineSet * const line) const
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   Info FunctionInfo(__func__);
 | 
|---|
| 152 |   if ((endpoints[0] == line->endpoints[0]) || (endpoints[1] == line->endpoints[0]) || (endpoints[0] == line->endpoints[1]) || (endpoints[1] == line->endpoints[1]))
 | 
|---|
| 153 |     return true;
 | 
|---|
| 154 |   else
 | 
|---|
| 155 |     return false;
 | 
|---|
| 156 | }
 | 
|---|
| 157 | ;
 | 
|---|
| 158 | 
 | 
|---|
| 159 | /** Checks whether the adjacent triangles of a baseline are convex or not.
 | 
|---|
| 160 |  * We sum the two angles of each height vector with respect to the center of the baseline.
 | 
|---|
| 161 |  * If greater/equal M_PI than we are convex.
 | 
|---|
| 162 |  * \param *out output stream for debugging
 | 
|---|
| 163 |  * \return true - triangles are convex, false - concave or less than two triangles connected
 | 
|---|
| 164 |  */
 | 
|---|
| 165 | bool BoundaryLineSet::CheckConvexityCriterion() const
 | 
|---|
| 166 | {
 | 
|---|
| 167 |   Info FunctionInfo(__func__);
 | 
|---|
| 168 |   double angle = CalculateConvexity();
 | 
|---|
| 169 |   if (angle > -MYEPSILON) {
 | 
|---|
| 170 |     DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Angle is greater than pi: convex." << endl);
 | 
|---|
| 171 |     return true;
 | 
|---|
| 172 |   } else {
 | 
|---|
| 173 |     DoLog(0) && (Log() << Verbose(0) << "REJECT: Angle is less than pi: concave." << endl);
 | 
|---|
| 174 |     return false;
 | 
|---|
| 175 |   }
 | 
|---|
| 176 | }
 | 
|---|
| 177 | 
 | 
|---|
| 178 | 
 | 
|---|
| 179 | /** Calculates the angle between two triangles with respect to their normal vector.
 | 
|---|
| 180 |  * We sum the two angles of each height vector with respect to the center of the baseline.
 | 
|---|
| 181 |  * \return angle > 0 then convex, if < 0 then concave
 | 
|---|
| 182 |  */
 | 
|---|
| 183 | double BoundaryLineSet::CalculateConvexity() const
 | 
|---|
| 184 | {
 | 
|---|
| 185 |   Info FunctionInfo(__func__);
 | 
|---|
| 186 |   Vector BaseLineCenter, BaseLineNormal, BaseLine, helper[2], NormalCheck;
 | 
|---|
| 187 |   // get the two triangles
 | 
|---|
| 188 |   if (triangles.size() != 2) {
 | 
|---|
| 189 |     DoeLog(0) && (eLog() << Verbose(0) << "Baseline " << *this << " is connected to less than two triangles, Tesselation incomplete!" << endl);
 | 
|---|
| 190 |     return true;
 | 
|---|
| 191 |   }
 | 
|---|
| 192 |   // check normal vectors
 | 
|---|
| 193 |   // have a normal vector on the base line pointing outwards
 | 
|---|
| 194 |   //Log() << Verbose(0) << "INFO: " << *this << " has vectors at " << *(endpoints[0]->node->node) << " and at " << *(endpoints[1]->node->node) << "." << endl;
 | 
|---|
| 195 |   BaseLineCenter = (1./2.)*((endpoints[0]->node->getPosition()) + (endpoints[1]->node->getPosition()));
 | 
|---|
| 196 |   BaseLine = (endpoints[0]->node->getPosition()) - (endpoints[1]->node->getPosition());
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   //Log() << Verbose(0) << "INFO: Baseline is " << BaseLine << " and its center is at " << BaseLineCenter << "." << endl;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   BaseLineNormal.Zero();
 | 
|---|
| 201 |   NormalCheck.Zero();
 | 
|---|
| 202 |   double sign = -1.;
 | 
|---|
| 203 |   int i = 0;
 | 
|---|
| 204 |   class BoundaryPointSet *node = NULL;
 | 
|---|
| 205 |   for (TriangleMap::const_iterator runner = triangles.begin(); runner != triangles.end(); runner++) {
 | 
|---|
| 206 |     //Log() << Verbose(0) << "INFO: NormalVector of " << *(runner->second) << " is " << runner->second->NormalVector << "." << endl;
 | 
|---|
| 207 |     NormalCheck += runner->second->NormalVector;
 | 
|---|
| 208 |     NormalCheck *= sign;
 | 
|---|
| 209 |     sign = -sign;
 | 
|---|
| 210 |     if (runner->second->NormalVector.NormSquared() > MYEPSILON)
 | 
|---|
| 211 |       BaseLineNormal = runner->second->NormalVector;   // yes, copy second on top of first
 | 
|---|
| 212 |     else {
 | 
|---|
| 213 |       DoeLog(0) && (eLog() << Verbose(0) << "Triangle " << *runner->second << " has zero normal vector!" << endl);
 | 
|---|
| 214 |     }
 | 
|---|
| 215 |     node = runner->second->GetThirdEndpoint(this);
 | 
|---|
| 216 |     if (node != NULL) {
 | 
|---|
| 217 |       //Log() << Verbose(0) << "INFO: Third node for triangle " << *(runner->second) << " is " << *node << " at " << *(node->node->node) << "." << endl;
 | 
|---|
| 218 |       helper[i] = (node->node->getPosition()) - BaseLineCenter;
 | 
|---|
| 219 |       helper[i].MakeNormalTo(BaseLine);  // we want to compare the triangle's heights' angles!
 | 
|---|
| 220 |       //Log() << Verbose(0) << "INFO: Height vector with respect to baseline is " << helper[i] << "." << endl;
 | 
|---|
| 221 |       i++;
 | 
|---|
| 222 |     } else {
 | 
|---|
| 223 |       DoeLog(1) && (eLog() << Verbose(1) << "I cannot find third node in triangle, something's wrong." << endl);
 | 
|---|
| 224 |       return true;
 | 
|---|
| 225 |     }
 | 
|---|
| 226 |   }
 | 
|---|
| 227 |   //Log() << Verbose(0) << "INFO: BaselineNormal is " << BaseLineNormal << "." << endl;
 | 
|---|
| 228 |   if (NormalCheck.NormSquared() < MYEPSILON) {
 | 
|---|
| 229 |     DoLog(0) && (Log() << Verbose(0) << "ACCEPT: Normalvectors of both triangles are the same: convex." << endl);
 | 
|---|
| 230 |     return true;
 | 
|---|
| 231 |   }
 | 
|---|
| 232 |   BaseLineNormal.Scale(-1.);
 | 
|---|
| 233 |   double angle = GetAngle(helper[0], helper[1], BaseLineNormal);
 | 
|---|
| 234 |   return (angle - M_PI);
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | /** Checks whether point is any of the two endpoints this line contains.
 | 
|---|
| 238 |  * \param *point point to test
 | 
|---|
| 239 |  * \return true - point is of the line, false - is not
 | 
|---|
| 240 |  */
 | 
|---|
| 241 | bool BoundaryLineSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
 | 
|---|
| 242 | {
 | 
|---|
| 243 |   Info FunctionInfo(__func__);
 | 
|---|
| 244 |   for (int i = 0; i < 2; i++)
 | 
|---|
| 245 |     if (point == endpoints[i])
 | 
|---|
| 246 |       return true;
 | 
|---|
| 247 |   return false;
 | 
|---|
| 248 | }
 | 
|---|
| 249 | ;
 | 
|---|
| 250 | 
 | 
|---|
| 251 | /** Returns other endpoint of the line.
 | 
|---|
| 252 |  * \param *point other endpoint
 | 
|---|
| 253 |  * \return NULL - if endpoint not contained in BoundaryLineSet::lines, or pointer to BoundaryPointSet otherwise
 | 
|---|
| 254 |  */
 | 
|---|
| 255 | class BoundaryPointSet *BoundaryLineSet::GetOtherEndpoint(const BoundaryPointSet * const point) const
 | 
|---|
| 256 | {
 | 
|---|
| 257 |   Info FunctionInfo(__func__);
 | 
|---|
| 258 |   if (endpoints[0] == point)
 | 
|---|
| 259 |     return endpoints[1];
 | 
|---|
| 260 |   else if (endpoints[1] == point)
 | 
|---|
| 261 |     return endpoints[0];
 | 
|---|
| 262 |   else
 | 
|---|
| 263 |     return NULL;
 | 
|---|
| 264 | }
 | 
|---|
| 265 | ;
 | 
|---|
| 266 | 
 | 
|---|
| 267 | /** Returns other triangle of the line.
 | 
|---|
| 268 |  * \param *point other endpoint
 | 
|---|
| 269 |  * \return NULL - if triangle not contained in BoundaryLineSet::triangles, or pointer to BoundaryTriangleSet otherwise
 | 
|---|
| 270 |  */
 | 
|---|
| 271 | class BoundaryTriangleSet *BoundaryLineSet::GetOtherTriangle(const BoundaryTriangleSet * const triangle) const
 | 
|---|
| 272 | {
 | 
|---|
| 273 |   Info FunctionInfo(__func__);
 | 
|---|
| 274 |   if (triangles.size() == 2) {
 | 
|---|
| 275 |     for (TriangleMap::const_iterator TriangleRunner = triangles.begin(); TriangleRunner != triangles.end(); ++TriangleRunner)
 | 
|---|
| 276 |       if (TriangleRunner->second != triangle)
 | 
|---|
| 277 |         return TriangleRunner->second;
 | 
|---|
| 278 |   }
 | 
|---|
| 279 |   return NULL;
 | 
|---|
| 280 | }
 | 
|---|
| 281 | ;
 | 
|---|
| 282 | 
 | 
|---|
| 283 | /** output operator for BoundaryLineSet.
 | 
|---|
| 284 |  * \param &ost output stream
 | 
|---|
| 285 |  * \param &a boundary line
 | 
|---|
| 286 |  */
 | 
|---|
| 287 | ostream & operator <<(ostream &ost, const BoundaryLineSet &a)
 | 
|---|
| 288 | {
 | 
|---|
| 289 |   ost << "[" << a.Nr << "|" << a.endpoints[0]->node->getName() << " at " << a.endpoints[0]->node->getPosition() << "," << a.endpoints[1]->node->getName() << " at " << a.endpoints[1]->node->getPosition() << "]";
 | 
|---|
| 290 |   return ost;
 | 
|---|
| 291 | }
 | 
|---|
| 292 | ;
 | 
|---|