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