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