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