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