1 | /*
|
---|
2 | * TesselationHelpers.cpp
|
---|
3 | *
|
---|
4 | * Created on: Aug 3, 2009
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <fstream>
|
---|
9 |
|
---|
10 | #include "info.hpp"
|
---|
11 | #include "linkedcell.hpp"
|
---|
12 | #include "log.hpp"
|
---|
13 | #include "tesselation.hpp"
|
---|
14 | #include "tesselationhelpers.hpp"
|
---|
15 | #include "vector.hpp"
|
---|
16 | #include "vector_ops.hpp"
|
---|
17 | #include "verbose.hpp"
|
---|
18 | #include "Plane.hpp"
|
---|
19 |
|
---|
20 | double DetGet(gsl_matrix * const A, const int inPlace)
|
---|
21 | {
|
---|
22 | Info FunctionInfo(__func__);
|
---|
23 | /*
|
---|
24 | inPlace = 1 => A is replaced with the LU decomposed copy.
|
---|
25 | inPlace = 0 => A is retained, and a copy is used for LU.
|
---|
26 | */
|
---|
27 |
|
---|
28 | double det;
|
---|
29 | int signum;
|
---|
30 | gsl_permutation *p = gsl_permutation_alloc(A->size1);
|
---|
31 | gsl_matrix *tmpA=0;
|
---|
32 |
|
---|
33 | if (inPlace)
|
---|
34 | tmpA = A;
|
---|
35 | else {
|
---|
36 | gsl_matrix *tmpA = gsl_matrix_alloc(A->size1, A->size2);
|
---|
37 | gsl_matrix_memcpy(tmpA , A);
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | gsl_linalg_LU_decomp(tmpA , p , &signum);
|
---|
42 | det = gsl_linalg_LU_det(tmpA , signum);
|
---|
43 | gsl_permutation_free(p);
|
---|
44 | if (! inPlace)
|
---|
45 | gsl_matrix_free(tmpA);
|
---|
46 |
|
---|
47 | return det;
|
---|
48 | };
|
---|
49 |
|
---|
50 | void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS)
|
---|
51 | {
|
---|
52 | Info FunctionInfo(__func__);
|
---|
53 | gsl_matrix *A = gsl_matrix_calloc(3,3);
|
---|
54 | double m11, m12, m13, m14;
|
---|
55 |
|
---|
56 | for(int i=0;i<3;i++) {
|
---|
57 | gsl_matrix_set(A, i, 0, a[i]);
|
---|
58 | gsl_matrix_set(A, i, 1, b[i]);
|
---|
59 | gsl_matrix_set(A, i, 2, c[i]);
|
---|
60 | }
|
---|
61 | m11 = DetGet(A, 1);
|
---|
62 |
|
---|
63 | for(int i=0;i<3;i++) {
|
---|
64 | gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
|
---|
65 | gsl_matrix_set(A, i, 1, b[i]);
|
---|
66 | gsl_matrix_set(A, i, 2, c[i]);
|
---|
67 | }
|
---|
68 | m12 = DetGet(A, 1);
|
---|
69 |
|
---|
70 | for(int i=0;i<3;i++) {
|
---|
71 | gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
|
---|
72 | gsl_matrix_set(A, i, 1, a[i]);
|
---|
73 | gsl_matrix_set(A, i, 2, c[i]);
|
---|
74 | }
|
---|
75 | m13 = DetGet(A, 1);
|
---|
76 |
|
---|
77 | for(int i=0;i<3;i++) {
|
---|
78 | gsl_matrix_set(A, i, 0, a[i]*a[i] + b[i]*b[i] + c[i]*c[i]);
|
---|
79 | gsl_matrix_set(A, i, 1, a[i]);
|
---|
80 | gsl_matrix_set(A, i, 2, b[i]);
|
---|
81 | }
|
---|
82 | m14 = DetGet(A, 1);
|
---|
83 |
|
---|
84 | if (fabs(m11) < MYEPSILON)
|
---|
85 | DoeLog(1) && (eLog()<< Verbose(1) << "three points are colinear." << endl);
|
---|
86 |
|
---|
87 | center->at(0) = 0.5 * m12/ m11;
|
---|
88 | center->at(1) = -0.5 * m13/ m11;
|
---|
89 | center->at(2) = 0.5 * m14/ m11;
|
---|
90 |
|
---|
91 | if (fabs(a.distance(*center) - RADIUS) > MYEPSILON)
|
---|
92 | DoeLog(1) && (eLog()<< Verbose(1) << "The given center is further way by " << fabs(a.distance(*center) - RADIUS) << " from a than RADIUS." << endl);
|
---|
93 |
|
---|
94 | gsl_matrix_free(A);
|
---|
95 | };
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Function returns center of sphere with RADIUS, which rests on points a, b, c
|
---|
101 | * @param Center this vector will be used for return
|
---|
102 | * @param a vector first point of triangle
|
---|
103 | * @param b vector second point of triangle
|
---|
104 | * @param c vector third point of triangle
|
---|
105 | * @param *Umkreismittelpunkt new center point of circumference
|
---|
106 | * @param Direction vector indicates up/down
|
---|
107 | * @param AlternativeDirection Vector, needed in case the triangles have 90 deg angle
|
---|
108 | * @param Halfplaneindicator double indicates whether Direction is up or down
|
---|
109 | * @param AlternativeIndicator double indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
|
---|
110 | * @param alpha double angle at a
|
---|
111 | * @param beta double, angle at b
|
---|
112 | * @param gamma, double, angle at c
|
---|
113 | * @param Radius, double
|
---|
114 | * @param Umkreisradius double radius of circumscribing circle
|
---|
115 | */
|
---|
116 | void GetCenterOfSphere(Vector* const & Center, const Vector &a, const Vector &b, const Vector &c, Vector * const NewUmkreismittelpunkt, const Vector* const Direction, const Vector* const AlternativeDirection,
|
---|
117 | const double HalfplaneIndicator, const double AlternativeIndicator, const double alpha, const double beta, const double gamma, const double RADIUS, const double Umkreisradius)
|
---|
118 | {
|
---|
119 | Info FunctionInfo(__func__);
|
---|
120 | Vector TempNormal, helper;
|
---|
121 | double Restradius;
|
---|
122 | Vector OtherCenter;
|
---|
123 | Center->Zero();
|
---|
124 | helper = sin(2.*alpha) * a;
|
---|
125 | (*Center) += helper;
|
---|
126 | helper = sin(2.*beta) * b;
|
---|
127 | (*Center) += helper;
|
---|
128 | helper = sin(2.*gamma) * c;
|
---|
129 | (*Center) += helper;
|
---|
130 | //*Center = a * sin(2.*alpha) + b * sin(2.*beta) + c * sin(2.*gamma) ;
|
---|
131 | Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
|
---|
132 | (*NewUmkreismittelpunkt) = (*Center);
|
---|
133 | DoLog(1) && (Log() << Verbose(1) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n");
|
---|
134 | // Here we calculated center of circumscribing circle, using barycentric coordinates
|
---|
135 | DoLog(1) && (Log() << Verbose(1) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n");
|
---|
136 |
|
---|
137 | TempNormal = a - b;
|
---|
138 | helper = a - c;
|
---|
139 | TempNormal.VectorProduct(helper);
|
---|
140 | if (fabs(HalfplaneIndicator) < MYEPSILON)
|
---|
141 | {
|
---|
142 | if ((TempNormal.ScalarProduct(*AlternativeDirection) <0 && AlternativeIndicator >0) || (TempNormal.ScalarProduct(*AlternativeDirection) >0 && AlternativeIndicator <0))
|
---|
143 | {
|
---|
144 | TempNormal *= -1;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | else
|
---|
148 | {
|
---|
149 | if (((TempNormal.ScalarProduct(*Direction)<0) && (HalfplaneIndicator >0)) || ((TempNormal.ScalarProduct(*Direction)>0) && (HalfplaneIndicator<0)))
|
---|
150 | {
|
---|
151 | TempNormal *= -1;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | TempNormal.Normalize();
|
---|
156 | Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
|
---|
157 | DoLog(1) && (Log() << Verbose(1) << "Height of center of circumference to center of sphere is " << Restradius << ".\n");
|
---|
158 | TempNormal.Scale(Restradius);
|
---|
159 | DoLog(1) && (Log() << Verbose(1) << "Shift vector to sphere of circumference is " << TempNormal << ".\n");
|
---|
160 | (*Center) += TempNormal;
|
---|
161 | DoLog(1) && (Log() << Verbose(1) << "Center of sphere of circumference is " << *Center << ".\n");
|
---|
162 | GetSphere(&OtherCenter, a, b, c, RADIUS);
|
---|
163 | DoLog(1) && (Log() << Verbose(1) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n");
|
---|
164 | };
|
---|
165 |
|
---|
166 |
|
---|
167 | /** Constructs the center of the circumcircle defined by three points \a *a, \a *b and \a *c.
|
---|
168 | * \param *Center new center on return
|
---|
169 | * \param *a first point
|
---|
170 | * \param *b second point
|
---|
171 | * \param *c third point
|
---|
172 | */
|
---|
173 | void GetCenterofCircumcircle(Vector * const Center, const Vector &a, const Vector &b, const Vector &c)
|
---|
174 | {
|
---|
175 | Info FunctionInfo(__func__);
|
---|
176 | Vector helper;
|
---|
177 | double alpha, beta, gamma;
|
---|
178 | Vector SideA = b - c;
|
---|
179 | Vector SideB = c - a;
|
---|
180 | Vector SideC = a - b;
|
---|
181 | alpha = M_PI - SideB.Angle(SideC);
|
---|
182 | beta = M_PI - SideC.Angle(SideA);
|
---|
183 | gamma = M_PI - SideA.Angle(SideB);
|
---|
184 | //Log() << Verbose(1) << "INFO: alpha = " << alpha/M_PI*180. << ", beta = " << beta/M_PI*180. << ", gamma = " << gamma/M_PI*180. << "." << endl;
|
---|
185 | if (fabs(M_PI - alpha - beta - gamma) > HULLEPSILON) {
|
---|
186 | DoeLog(2) && (eLog()<< Verbose(2) << "GetCenterofCircumcircle: Sum of angles " << (alpha+beta+gamma)/M_PI*180. << " > 180 degrees by " << fabs(M_PI - alpha - beta - gamma)/M_PI*180. << "!" << endl);
|
---|
187 | }
|
---|
188 |
|
---|
189 | Center->Zero();
|
---|
190 | helper = sin(2.*alpha) * a;
|
---|
191 | (*Center) += helper;
|
---|
192 | helper = sin(2.*beta) * b;
|
---|
193 | (*Center) += helper;
|
---|
194 | helper = sin(2.*gamma) * c;
|
---|
195 | (*Center) += helper;
|
---|
196 | Center->Scale(1./(sin(2.*alpha) + sin(2.*beta) + sin(2.*gamma)));
|
---|
197 | };
|
---|
198 |
|
---|
199 | /** Returns the parameter "path length" for a given \a NewSphereCenter relative to \a OldSphereCenter on a circle on the plane \a CirclePlaneNormal with center \a CircleCenter and radius \a CircleRadius.
|
---|
200 | * Test whether the \a NewSphereCenter is really on the given plane and in distance \a CircleRadius from \a CircleCenter.
|
---|
201 | * It calculates the angle, making it unique on [0,2.*M_PI) by comparing to SearchDirection.
|
---|
202 | * Also the new center is invalid if it the same as the old one and does not lie right above (\a NormalVector) the base line (\a CircleCenter).
|
---|
203 | * \param CircleCenter Center of the parameter circle
|
---|
204 | * \param CirclePlaneNormal normal vector to plane of the parameter circle
|
---|
205 | * \param CircleRadius radius of the parameter circle
|
---|
206 | * \param NewSphereCenter new center of a circumcircle
|
---|
207 | * \param OldSphereCenter old center of a circumcircle, defining the zero "path length" on the parameter circle
|
---|
208 | * \param NormalVector normal vector
|
---|
209 | * \param SearchDirection search direction to make angle unique on return.
|
---|
210 | * \return Angle between \a NewSphereCenter and \a OldSphereCenter relative to \a CircleCenter, 2.*M_PI if one test fails
|
---|
211 | */
|
---|
212 | double GetPathLengthonCircumCircle(const Vector &CircleCenter, const Vector &CirclePlaneNormal, const double CircleRadius, const Vector &NewSphereCenter, const Vector &OldSphereCenter, const Vector &NormalVector, const Vector &SearchDirection)
|
---|
213 | {
|
---|
214 | Info FunctionInfo(__func__);
|
---|
215 | Vector helper;
|
---|
216 | double radius, alpha;
|
---|
217 |
|
---|
218 | Vector RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
|
---|
219 | Vector RelativeNewSphereCenter = NewSphereCenter - CircleCenter;
|
---|
220 | helper = RelativeNewSphereCenter;
|
---|
221 | // test whether new center is on the parameter circle's plane
|
---|
222 | if (fabs(helper.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
|
---|
223 | DoeLog(1) && (eLog()<< Verbose(1) << "Something's very wrong here: NewSphereCenter is not on the band's plane as desired by " <<fabs(helper.ScalarProduct(CirclePlaneNormal)) << "!" << endl);
|
---|
224 | helper.ProjectOntoPlane(CirclePlaneNormal);
|
---|
225 | }
|
---|
226 | radius = helper.NormSquared();
|
---|
227 | // test whether the new center vector has length of CircleRadius
|
---|
228 | if (fabs(radius - CircleRadius) > HULLEPSILON)
|
---|
229 | DoeLog(1) && (eLog()<< Verbose(1) << "The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
|
---|
230 | alpha = helper.Angle(RelativeOldSphereCenter);
|
---|
231 | // make the angle unique by checking the halfplanes/search direction
|
---|
232 | if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON) // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals
|
---|
233 | alpha = 2.*M_PI - alpha;
|
---|
234 | DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << RelativeOldSphereCenter << " and resulting angle is " << alpha << "." << endl);
|
---|
235 | radius = helper.distance(RelativeOldSphereCenter);
|
---|
236 | helper.ProjectOntoPlane(NormalVector);
|
---|
237 | // check whether new center is somewhat away or at least right over the current baseline to prevent intersecting triangles
|
---|
238 | if ((radius > HULLEPSILON) || (helper.Norm() < HULLEPSILON)) {
|
---|
239 | DoLog(1) && (Log() << Verbose(1) << "INFO: Distance between old and new center is " << radius << " and between new center and baseline center is " << helper.Norm() << "." << endl);
|
---|
240 | return alpha;
|
---|
241 | } else {
|
---|
242 | DoLog(1) && (Log() << Verbose(1) << "INFO: NewSphereCenter " << RelativeNewSphereCenter << " is too close to RelativeOldSphereCenter" << RelativeOldSphereCenter << "." << endl);
|
---|
243 | return 2.*M_PI;
|
---|
244 | }
|
---|
245 | };
|
---|
246 |
|
---|
247 | struct Intersection {
|
---|
248 | Vector x1;
|
---|
249 | Vector x2;
|
---|
250 | Vector x3;
|
---|
251 | Vector x4;
|
---|
252 | };
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Intersection calculation function.
|
---|
256 | *
|
---|
257 | * @param x to find the result for
|
---|
258 | * @param function parameter
|
---|
259 | */
|
---|
260 | double MinIntersectDistance(const gsl_vector * x, void *params)
|
---|
261 | {
|
---|
262 | Info FunctionInfo(__func__);
|
---|
263 | double retval = 0;
|
---|
264 | struct Intersection *I = (struct Intersection *)params;
|
---|
265 | Vector intersection;
|
---|
266 | for (int i=0;i<NDIM;i++)
|
---|
267 | intersection[i] = gsl_vector_get(x, i);
|
---|
268 |
|
---|
269 | Vector SideA = I->x1 -I->x2 ;
|
---|
270 | Vector HeightA = intersection - I->x1;
|
---|
271 | HeightA.ProjectOntoPlane(SideA);
|
---|
272 |
|
---|
273 | Vector SideB = I->x3 - I->x4;
|
---|
274 | Vector HeightB = intersection - I->x3;
|
---|
275 | HeightB.ProjectOntoPlane(SideB);
|
---|
276 |
|
---|
277 | retval = HeightA.ScalarProduct(HeightA) + HeightB.ScalarProduct(HeightB);
|
---|
278 | //Log() << Verbose(1) << "MinIntersectDistance called, result: " << retval << endl;
|
---|
279 |
|
---|
280 | return retval;
|
---|
281 | };
|
---|
282 |
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Calculates whether there is an intersection between two lines. The first line
|
---|
286 | * always goes through point 1 and point 2 and the second line is given by the
|
---|
287 | * connection between point 4 and point 5.
|
---|
288 | *
|
---|
289 | * @param point 1 of line 1
|
---|
290 | * @param point 2 of line 1
|
---|
291 | * @param point 1 of line 2
|
---|
292 | * @param point 2 of line 2
|
---|
293 | *
|
---|
294 | * @return true if there is an intersection between the given lines, false otherwise
|
---|
295 | */
|
---|
296 | bool existsIntersection(const Vector &point1, const Vector &point2, const Vector &point3, const Vector &point4)
|
---|
297 | {
|
---|
298 | Info FunctionInfo(__func__);
|
---|
299 | bool result;
|
---|
300 |
|
---|
301 | struct Intersection par;
|
---|
302 | par.x1 = point1;
|
---|
303 | par.x2 = point2;
|
---|
304 | par.x3 = point3;
|
---|
305 | par.x4 = point4;
|
---|
306 |
|
---|
307 | const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
|
---|
308 | gsl_multimin_fminimizer *s = NULL;
|
---|
309 | gsl_vector *ss, *x;
|
---|
310 | gsl_multimin_function minexFunction;
|
---|
311 |
|
---|
312 | size_t iter = 0;
|
---|
313 | int status;
|
---|
314 | double size;
|
---|
315 |
|
---|
316 | /* Starting point */
|
---|
317 | x = gsl_vector_alloc(NDIM);
|
---|
318 | gsl_vector_set(x, 0, point1[0]);
|
---|
319 | gsl_vector_set(x, 1, point1[1]);
|
---|
320 | gsl_vector_set(x, 2, point1[2]);
|
---|
321 |
|
---|
322 | /* Set initial step sizes to 1 */
|
---|
323 | ss = gsl_vector_alloc(NDIM);
|
---|
324 | gsl_vector_set_all(ss, 1.0);
|
---|
325 |
|
---|
326 | /* Initialize method and iterate */
|
---|
327 | minexFunction.n = NDIM;
|
---|
328 | minexFunction.f = &MinIntersectDistance;
|
---|
329 | minexFunction.params = (void *)∥
|
---|
330 |
|
---|
331 | s = gsl_multimin_fminimizer_alloc(T, NDIM);
|
---|
332 | gsl_multimin_fminimizer_set(s, &minexFunction, x, ss);
|
---|
333 |
|
---|
334 | do {
|
---|
335 | iter++;
|
---|
336 | status = gsl_multimin_fminimizer_iterate(s);
|
---|
337 |
|
---|
338 | if (status) {
|
---|
339 | break;
|
---|
340 | }
|
---|
341 |
|
---|
342 | size = gsl_multimin_fminimizer_size(s);
|
---|
343 | status = gsl_multimin_test_size(size, 1e-2);
|
---|
344 |
|
---|
345 | if (status == GSL_SUCCESS) {
|
---|
346 | DoLog(1) && (Log() << Verbose(1) << "converged to minimum" << endl);
|
---|
347 | }
|
---|
348 | } while (status == GSL_CONTINUE && iter < 100);
|
---|
349 |
|
---|
350 | // check whether intersection is in between or not
|
---|
351 | Vector intersection;
|
---|
352 | double t1, t2;
|
---|
353 | for (int i = 0; i < NDIM; i++) {
|
---|
354 | intersection[i] = gsl_vector_get(s->x, i);
|
---|
355 | }
|
---|
356 |
|
---|
357 | Vector SideA = par.x2 - par.x1;
|
---|
358 | Vector HeightA = intersection - par.x1;
|
---|
359 |
|
---|
360 | t1 = HeightA.ScalarProduct(SideA)/SideA.ScalarProduct(SideA);
|
---|
361 |
|
---|
362 | Vector SideB = par.x4 - par.x3;
|
---|
363 | Vector HeightB = intersection - par.x3;
|
---|
364 |
|
---|
365 | t2 = HeightB.ScalarProduct(SideB)/SideB.ScalarProduct(SideB);
|
---|
366 |
|
---|
367 | Log() << Verbose(1) << "Intersection " << intersection << " is at "
|
---|
368 | << t1 << " for (" << point1 << "," << point2 << ") and at "
|
---|
369 | << t2 << " for (" << point3 << "," << point4 << "): ";
|
---|
370 |
|
---|
371 | if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) {
|
---|
372 | DoLog(1) && (Log() << Verbose(1) << "true intersection." << endl);
|
---|
373 | result = true;
|
---|
374 | } else {
|
---|
375 | DoLog(1) && (Log() << Verbose(1) << "intersection out of region of interest." << endl);
|
---|
376 | result = false;
|
---|
377 | }
|
---|
378 |
|
---|
379 | // free minimizer stuff
|
---|
380 | gsl_vector_free(x);
|
---|
381 | gsl_vector_free(ss);
|
---|
382 | gsl_multimin_fminimizer_free(s);
|
---|
383 |
|
---|
384 | return result;
|
---|
385 | };
|
---|
386 |
|
---|
387 | /** Gets the angle between a point and a reference relative to the provided center.
|
---|
388 | * We have two shanks point and reference between which the angle is calculated
|
---|
389 | * and by scalar product with OrthogonalVector we decide the interval.
|
---|
390 | * @param point to calculate the angle for
|
---|
391 | * @param reference to which to calculate the angle
|
---|
392 | * @param OrthogonalVector points in direction of [pi,2pi] interval
|
---|
393 | *
|
---|
394 | * @return angle between point and reference
|
---|
395 | */
|
---|
396 | double GetAngle(const Vector &point, const Vector &reference, const Vector &OrthogonalVector)
|
---|
397 | {
|
---|
398 | Info FunctionInfo(__func__);
|
---|
399 | if (reference.IsZero())
|
---|
400 | return M_PI;
|
---|
401 |
|
---|
402 | // calculate both angles and correct with in-plane vector
|
---|
403 | if (point.IsZero())
|
---|
404 | return M_PI;
|
---|
405 | double phi = point.Angle(reference);
|
---|
406 | if (OrthogonalVector.ScalarProduct(point) > 0) {
|
---|
407 | phi = 2.*M_PI - phi;
|
---|
408 | }
|
---|
409 |
|
---|
410 | DoLog(1) && (Log() << Verbose(1) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl);
|
---|
411 |
|
---|
412 | return phi;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | /** Calculates the volume of a general tetraeder.
|
---|
417 | * \param *a first vector
|
---|
418 | * \param *a first vector
|
---|
419 | * \param *a first vector
|
---|
420 | * \param *a first vector
|
---|
421 | * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot (a-b)) \f$
|
---|
422 | */
|
---|
423 | double CalculateVolumeofGeneralTetraeder(const Vector &a, const Vector &b, const Vector &c, const Vector &d)
|
---|
424 | {
|
---|
425 | Info FunctionInfo(__func__);
|
---|
426 | Vector Point, TetraederVector[3];
|
---|
427 | double volume;
|
---|
428 |
|
---|
429 | TetraederVector[0] = a;
|
---|
430 | TetraederVector[1] = b;
|
---|
431 | TetraederVector[2] = c;
|
---|
432 | for (int j=0;j<3;j++)
|
---|
433 | TetraederVector[j].SubtractVector(d);
|
---|
434 | Point = TetraederVector[0];
|
---|
435 | Point.VectorProduct(TetraederVector[1]);
|
---|
436 | volume = 1./6. * fabs(Point.ScalarProduct(TetraederVector[2]));
|
---|
437 | return volume;
|
---|
438 | };
|
---|
439 |
|
---|
440 |
|
---|
441 | /** Checks for a new special triangle whether one of its edges is already present with one one triangle connected.
|
---|
442 | * This enforces that special triangles (i.e. degenerated ones) should at last close the open-edge frontier and not
|
---|
443 | * make it bigger (i.e. closing one (the baseline) and opening two new ones).
|
---|
444 | * \param TPS[3] nodes of the triangle
|
---|
445 | * \return true - there is such a line (i.e. creation of degenerated triangle is valid), false - no such line (don't create)
|
---|
446 | */
|
---|
447 | bool CheckLineCriteriaForDegeneratedTriangle(const BoundaryPointSet * const nodes[3])
|
---|
448 | {
|
---|
449 | Info FunctionInfo(__func__);
|
---|
450 | bool result = false;
|
---|
451 | int counter = 0;
|
---|
452 |
|
---|
453 | // check all three points
|
---|
454 | for (int i=0;i<3;i++)
|
---|
455 | for (int j=i+1; j<3; j++) {
|
---|
456 | if (nodes[i] == NULL) {
|
---|
457 | DoLog(1) && (Log() << Verbose(1) << "Node nr. " << i << " is not yet present." << endl);
|
---|
458 | result = true;
|
---|
459 | } else if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) { // there already is a line
|
---|
460 | LineMap::const_iterator FindLine;
|
---|
461 | pair<LineMap::const_iterator,LineMap::const_iterator> FindPair;
|
---|
462 | FindPair = nodes[i]->lines.equal_range(nodes[j]->node->nr);
|
---|
463 | for (FindLine = FindPair.first; FindLine != FindPair.second; ++FindLine) {
|
---|
464 | // If there is a line with less than two attached triangles, we don't need a new line.
|
---|
465 | if (FindLine->second->triangles.size() < 2) {
|
---|
466 | counter++;
|
---|
467 | break; // increase counter only once per edge
|
---|
468 | }
|
---|
469 | }
|
---|
470 | } else { // no line
|
---|
471 | DoLog(1) && (Log() << Verbose(1) << "The line between " << *nodes[i] << " and " << *nodes[j] << " is not yet present, hence no need for a degenerate triangle." << endl);
|
---|
472 | result = true;
|
---|
473 | }
|
---|
474 | }
|
---|
475 | if ((!result) && (counter > 1)) {
|
---|
476 | DoLog(1) && (Log() << Verbose(1) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl);
|
---|
477 | result = true;
|
---|
478 | }
|
---|
479 | return result;
|
---|
480 | };
|
---|
481 |
|
---|
482 |
|
---|
483 | ///** Sort function for the candidate list.
|
---|
484 | // */
|
---|
485 | //bool SortCandidates(const CandidateForTesselation* candidate1, const CandidateForTesselation* candidate2)
|
---|
486 | //{
|
---|
487 | // Info FunctionInfo(__func__);
|
---|
488 | // Vector BaseLineVector, OrthogonalVector, helper;
|
---|
489 | // if (candidate1->BaseLine != candidate2->BaseLine) { // sanity check
|
---|
490 | // DoeLog(1) && (eLog()<< Verbose(1) << "sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl);
|
---|
491 | // //return false;
|
---|
492 | // exit(1);
|
---|
493 | // }
|
---|
494 | // // create baseline vector
|
---|
495 | // BaseLineVector.CopyVector(candidate1->BaseLine->endpoints[1]->node->node);
|
---|
496 | // BaseLineVector.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
|
---|
497 | // BaseLineVector.Normalize();
|
---|
498 | //
|
---|
499 | // // create normal in-plane vector to cope with acos() non-uniqueness on [0,2pi] (note that is pointing in the "right" direction already, hence ">0" test!)
|
---|
500 | // helper.CopyVector(candidate1->BaseLine->endpoints[0]->node->node);
|
---|
501 | // helper.SubtractVector(candidate1->point->node);
|
---|
502 | // OrthogonalVector.CopyVector(&helper);
|
---|
503 | // helper.VectorProduct(&BaseLineVector);
|
---|
504 | // OrthogonalVector.SubtractVector(&helper);
|
---|
505 | // OrthogonalVector.Normalize();
|
---|
506 | //
|
---|
507 | // // calculate both angles and correct with in-plane vector
|
---|
508 | // helper.CopyVector(candidate1->point->node);
|
---|
509 | // helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
|
---|
510 | // double phi = BaseLineVector.Angle(&helper);
|
---|
511 | // if (OrthogonalVector.ScalarProduct(&helper) > 0) {
|
---|
512 | // phi = 2.*M_PI - phi;
|
---|
513 | // }
|
---|
514 | // helper.CopyVector(candidate2->point->node);
|
---|
515 | // helper.SubtractVector(candidate1->BaseLine->endpoints[0]->node->node);
|
---|
516 | // double psi = BaseLineVector.Angle(&helper);
|
---|
517 | // if (OrthogonalVector.ScalarProduct(&helper) > 0) {
|
---|
518 | // psi = 2.*M_PI - psi;
|
---|
519 | // }
|
---|
520 | //
|
---|
521 | // Log() << Verbose(1) << *candidate1->point << " has angle " << phi << endl;
|
---|
522 | // Log() << Verbose(1) << *candidate2->point << " has angle " << psi << endl;
|
---|
523 | //
|
---|
524 | // // return comparison
|
---|
525 | // return phi < psi;
|
---|
526 | //};
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Finds the point which is second closest to the provided one.
|
---|
530 | *
|
---|
531 | * @param Point to which to find the second closest other point
|
---|
532 | * @param linked cell structure
|
---|
533 | *
|
---|
534 | * @return point which is second closest to the provided one
|
---|
535 | */
|
---|
536 | TesselPoint* FindSecondClosestTesselPoint(const Vector* Point, const LinkedCell* const LC)
|
---|
537 | {
|
---|
538 | Info FunctionInfo(__func__);
|
---|
539 | TesselPoint* closestPoint = NULL;
|
---|
540 | TesselPoint* secondClosestPoint = NULL;
|
---|
541 | double distance = 1e16;
|
---|
542 | double secondDistance = 1e16;
|
---|
543 | Vector helper;
|
---|
544 | int N[NDIM], Nlower[NDIM], Nupper[NDIM];
|
---|
545 |
|
---|
546 | LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
|
---|
547 | for(int i=0;i<NDIM;i++) // store indices of this cell
|
---|
548 | N[i] = LC->n[i];
|
---|
549 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
|
---|
550 |
|
---|
551 | LC->GetNeighbourBounds(Nlower, Nupper);
|
---|
552 | //Log() << Verbose(1) << endl;
|
---|
553 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
|
---|
554 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
|
---|
555 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
|
---|
556 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
|
---|
557 | //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
|
---|
558 | if (List != NULL) {
|
---|
559 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
|
---|
560 | helper = (*Point) - (*(*Runner)->node);
|
---|
561 | double currentNorm = helper. Norm();
|
---|
562 | if (currentNorm < distance) {
|
---|
563 | // remember second point
|
---|
564 | secondDistance = distance;
|
---|
565 | secondClosestPoint = closestPoint;
|
---|
566 | // mark down new closest point
|
---|
567 | distance = currentNorm;
|
---|
568 | closestPoint = (*Runner);
|
---|
569 | //Log() << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *secondClosestPoint << "." << endl;
|
---|
570 | }
|
---|
571 | }
|
---|
572 | } else {
|
---|
573 | eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
|
---|
574 | << LC->n[2] << " is invalid!" << endl;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | return secondClosestPoint;
|
---|
579 | };
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Finds the point which is closest to the provided one.
|
---|
583 | *
|
---|
584 | * @param Point to which to find the closest other point
|
---|
585 | * @param SecondPoint the second closest other point on return, NULL if none found
|
---|
586 | * @param linked cell structure
|
---|
587 | *
|
---|
588 | * @return point which is closest to the provided one, NULL if none found
|
---|
589 | */
|
---|
590 | TesselPoint* FindClosestTesselPoint(const Vector* Point, TesselPoint *&SecondPoint, const LinkedCell* const LC)
|
---|
591 | {
|
---|
592 | Info FunctionInfo(__func__);
|
---|
593 | TesselPoint* closestPoint = NULL;
|
---|
594 | SecondPoint = NULL;
|
---|
595 | double distance = 1e16;
|
---|
596 | double secondDistance = 1e16;
|
---|
597 | Vector helper;
|
---|
598 | int N[NDIM], Nlower[NDIM], Nupper[NDIM];
|
---|
599 |
|
---|
600 | LC->SetIndexToVector(Point); // ignore status as we calculate bounds below sensibly
|
---|
601 | for(int i=0;i<NDIM;i++) // store indices of this cell
|
---|
602 | N[i] = LC->n[i];
|
---|
603 | DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
|
---|
604 |
|
---|
605 | LC->GetNeighbourBounds(Nlower, Nupper);
|
---|
606 | //Log() << Verbose(1) << endl;
|
---|
607 | for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
|
---|
608 | for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
|
---|
609 | for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
|
---|
610 | const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
|
---|
611 | //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
|
---|
612 | if (List != NULL) {
|
---|
613 | for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
|
---|
614 | helper = (*Point) - (*(*Runner)->node);
|
---|
615 | double currentNorm = helper.NormSquared();
|
---|
616 | if (currentNorm < distance) {
|
---|
617 | secondDistance = distance;
|
---|
618 | SecondPoint = closestPoint;
|
---|
619 | distance = currentNorm;
|
---|
620 | closestPoint = (*Runner);
|
---|
621 | //Log() << Verbose(1) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
|
---|
622 | } else if (currentNorm < secondDistance) {
|
---|
623 | secondDistance = currentNorm;
|
---|
624 | SecondPoint = (*Runner);
|
---|
625 | //Log() << Verbose(1) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl;
|
---|
626 | }
|
---|
627 | }
|
---|
628 | } else {
|
---|
629 | eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
|
---|
630 | << LC->n[2] << " is invalid!" << endl;
|
---|
631 | }
|
---|
632 | }
|
---|
633 | // output
|
---|
634 | if (closestPoint != NULL) {
|
---|
635 | DoLog(1) && (Log() << Verbose(1) << "Closest point is " << *closestPoint);
|
---|
636 | if (SecondPoint != NULL)
|
---|
637 | DoLog(0) && (Log() << Verbose(0) << " and second closest is " << *SecondPoint);
|
---|
638 | DoLog(0) && (Log() << Verbose(0) << "." << endl);
|
---|
639 | }
|
---|
640 | return closestPoint;
|
---|
641 | };
|
---|
642 |
|
---|
643 | /** Returns the closest point on \a *Base with respect to \a *OtherBase.
|
---|
644 | * \param *out output stream for debugging
|
---|
645 | * \param *Base reference line
|
---|
646 | * \param *OtherBase other base line
|
---|
647 | * \return Vector on reference line that has closest distance
|
---|
648 | */
|
---|
649 | Vector * GetClosestPointBetweenLine(const BoundaryLineSet * const Base, const BoundaryLineSet * const OtherBase)
|
---|
650 | {
|
---|
651 | Info FunctionInfo(__func__);
|
---|
652 | // construct the plane of the two baselines (i.e. take both their directional vectors)
|
---|
653 | Vector Baseline = (*Base->endpoints[1]->node->node) - (*Base->endpoints[0]->node->node);
|
---|
654 | Vector OtherBaseline = (*OtherBase->endpoints[1]->node->node) - (*OtherBase->endpoints[0]->node->node);
|
---|
655 | Vector Normal = Baseline;
|
---|
656 | Normal.VectorProduct(OtherBaseline);
|
---|
657 | Normal.Normalize();
|
---|
658 | DoLog(1) && (Log() << Verbose(1) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl);
|
---|
659 |
|
---|
660 | // project one offset point of OtherBase onto this plane (and add plane offset vector)
|
---|
661 | Vector NewOffset = (*OtherBase->endpoints[0]->node->node) - (*Base->endpoints[0]->node->node);
|
---|
662 | NewOffset.ProjectOntoPlane(Normal);
|
---|
663 | NewOffset += (*Base->endpoints[0]->node->node);
|
---|
664 | Vector NewDirection = NewOffset + OtherBaseline;
|
---|
665 |
|
---|
666 | // calculate the intersection between this projected baseline and Base
|
---|
667 | Vector *Intersection = new Vector;
|
---|
668 | *Intersection = GetIntersectionOfTwoLinesOnPlane(*(Base->endpoints[0]->node->node),
|
---|
669 | *(Base->endpoints[1]->node->node),
|
---|
670 | NewOffset, NewDirection);
|
---|
671 | Normal = (*Intersection) - (*Base->endpoints[0]->node->node);
|
---|
672 | DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl);
|
---|
673 |
|
---|
674 | return Intersection;
|
---|
675 | };
|
---|
676 |
|
---|
677 | /** Returns the distance to the plane defined by \a *triangle
|
---|
678 | * \param *out output stream for debugging
|
---|
679 | * \param *x Vector to calculate distance to
|
---|
680 | * \param *triangle triangle defining plane
|
---|
681 | * \return distance between \a *x and plane defined by \a *triangle, -1 - if something went wrong
|
---|
682 | */
|
---|
683 | double DistanceToTrianglePlane(const Vector *x, const BoundaryTriangleSet * const triangle)
|
---|
684 | {
|
---|
685 | Info FunctionInfo(__func__);
|
---|
686 | double distance = 0.;
|
---|
687 | if (x == NULL) {
|
---|
688 | return -1;
|
---|
689 | }
|
---|
690 | distance = x->DistanceToSpace(triangle->getPlane());
|
---|
691 | return distance;
|
---|
692 | };
|
---|
693 |
|
---|
694 | /** Creates the objects in a VRML file.
|
---|
695 | * \param *out output stream for debugging
|
---|
696 | * \param *vrmlfile output stream for tecplot data
|
---|
697 | * \param *Tess Tesselation structure with constructed triangles
|
---|
698 | * \param *mol molecule structure with atom positions
|
---|
699 | */
|
---|
700 | void WriteVrmlFile(ofstream * const vrmlfile, const Tesselation * const Tess, const PointCloud * const cloud)
|
---|
701 | {
|
---|
702 | Info FunctionInfo(__func__);
|
---|
703 | TesselPoint *Walker = NULL;
|
---|
704 | int i;
|
---|
705 | Vector *center = cloud->GetCenter();
|
---|
706 | if (vrmlfile != NULL) {
|
---|
707 | //Log() << Verbose(1) << "Writing Raster3D file ... ";
|
---|
708 | *vrmlfile << "#VRML V2.0 utf8" << endl;
|
---|
709 | *vrmlfile << "#Created by molecuilder" << endl;
|
---|
710 | *vrmlfile << "#All atoms as spheres" << endl;
|
---|
711 | cloud->GoToFirst();
|
---|
712 | while (!cloud->IsEnd()) {
|
---|
713 | Walker = cloud->GetPoint();
|
---|
714 | *vrmlfile << "Sphere {" << endl << " "; // 2 is sphere type
|
---|
715 | for (i=0;i<NDIM;i++)
|
---|
716 | *vrmlfile << Walker->node->at(i)-center->at(i) << " ";
|
---|
717 | *vrmlfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
|
---|
718 | cloud->GoToNext();
|
---|
719 | }
|
---|
720 |
|
---|
721 | *vrmlfile << "# All tesselation triangles" << endl;
|
---|
722 | for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
|
---|
723 | *vrmlfile << "1" << endl << " "; // 1 is triangle type
|
---|
724 | for (i=0;i<3;i++) { // print each node
|
---|
725 | for (int j=0;j<NDIM;j++) // and for each node all NDIM coordinates
|
---|
726 | *vrmlfile << TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j) << " ";
|
---|
727 | *vrmlfile << "\t";
|
---|
728 | }
|
---|
729 | *vrmlfile << "1. 0. 0." << endl; // red as colour
|
---|
730 | *vrmlfile << "18" << endl << " 0.5 0.5 0.5" << endl; // 18 is transparency type for previous object
|
---|
731 | }
|
---|
732 | } else {
|
---|
733 | DoeLog(1) && (eLog()<< Verbose(1) << "Given vrmlfile is " << vrmlfile << "." << endl);
|
---|
734 | }
|
---|
735 | delete(center);
|
---|
736 | };
|
---|
737 |
|
---|
738 | /** Writes additionally the current sphere (i.e. the last triangle to file).
|
---|
739 | * \param *out output stream for debugging
|
---|
740 | * \param *rasterfile output stream for tecplot data
|
---|
741 | * \param *Tess Tesselation structure with constructed triangles
|
---|
742 | * \param *mol molecule structure with atom positions
|
---|
743 | */
|
---|
744 | void IncludeSphereinRaster3D(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
|
---|
745 | {
|
---|
746 | Info FunctionInfo(__func__);
|
---|
747 | Vector helper;
|
---|
748 |
|
---|
749 | if (Tess->LastTriangle != NULL) {
|
---|
750 | // include the current position of the virtual sphere in the temporary raster3d file
|
---|
751 | Vector *center = cloud->GetCenter();
|
---|
752 | // make the circumsphere's center absolute again
|
---|
753 | Vector helper = (1./3.) * ((*Tess->LastTriangle->endpoints[0]->node->node) +
|
---|
754 | (*Tess->LastTriangle->endpoints[1]->node->node) +
|
---|
755 | (*Tess->LastTriangle->endpoints[2]->node->node));
|
---|
756 | helper -= (*center);
|
---|
757 | // and add to file plus translucency object
|
---|
758 | *rasterfile << "# current virtual sphere\n";
|
---|
759 | *rasterfile << "8\n 25.0 0.6 -1.0 -1.0 -1.0 0.2 0 0 0 0\n";
|
---|
760 | *rasterfile << "2\n " << helper[0] << " " << helper[1] << " " << helper[2] << "\t" << 5. << "\t1 0 0\n";
|
---|
761 | *rasterfile << "9\n terminating special property\n";
|
---|
762 | delete(center);
|
---|
763 | }
|
---|
764 | };
|
---|
765 |
|
---|
766 | /** Creates the objects in a raster3d file (renderable with a header.r3d).
|
---|
767 | * \param *out output stream for debugging
|
---|
768 | * \param *rasterfile output stream for tecplot data
|
---|
769 | * \param *Tess Tesselation structure with constructed triangles
|
---|
770 | * \param *mol molecule structure with atom positions
|
---|
771 | */
|
---|
772 | void WriteRaster3dFile(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
|
---|
773 | {
|
---|
774 | Info FunctionInfo(__func__);
|
---|
775 | TesselPoint *Walker = NULL;
|
---|
776 | int i;
|
---|
777 | Vector *center = cloud->GetCenter();
|
---|
778 | if (rasterfile != NULL) {
|
---|
779 | //Log() << Verbose(1) << "Writing Raster3D file ... ";
|
---|
780 | *rasterfile << "# Raster3D object description, created by MoleCuilder" << endl;
|
---|
781 | *rasterfile << "@header.r3d" << endl;
|
---|
782 | *rasterfile << "# All atoms as spheres" << endl;
|
---|
783 | cloud->GoToFirst();
|
---|
784 | while (!cloud->IsEnd()) {
|
---|
785 | Walker = cloud->GetPoint();
|
---|
786 | *rasterfile << "2" << endl << " "; // 2 is sphere type
|
---|
787 | for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
|
---|
788 | const double tmp = Walker->node->at(j)-center->at(j);
|
---|
789 | *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
|
---|
790 | }
|
---|
791 | *rasterfile << "\t0.1\t1. 1. 1." << endl; // radius 0.05 and white as colour
|
---|
792 | cloud->GoToNext();
|
---|
793 | }
|
---|
794 |
|
---|
795 | *rasterfile << "# All tesselation triangles" << endl;
|
---|
796 | *rasterfile << "8\n 25. -1. 1. 1. 1. 0.0 0 0 0 2\n SOLID 1.0 0.0 0.0\n BACKFACE 0.3 0.3 1.0 0 0\n";
|
---|
797 | for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
|
---|
798 | *rasterfile << "1" << endl << " "; // 1 is triangle type
|
---|
799 | for (i=0;i<3;i++) { // print each node
|
---|
800 | for (int j=0;j<NDIM;j++) { // and for each node all NDIM coordinates
|
---|
801 | const double tmp = TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j);
|
---|
802 | *rasterfile << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
|
---|
803 | }
|
---|
804 | *rasterfile << "\t";
|
---|
805 | }
|
---|
806 | *rasterfile << "1. 0. 0." << endl; // red as colour
|
---|
807 | //*rasterfile << "18" << endl << " 0.5 0.5 0.5" << endl; // 18 is transparency type for previous object
|
---|
808 | }
|
---|
809 | *rasterfile << "9\n# terminating special property\n";
|
---|
810 | } else {
|
---|
811 | DoeLog(1) && (eLog()<< Verbose(1) << "Given rasterfile is " << rasterfile << "." << endl);
|
---|
812 | }
|
---|
813 | IncludeSphereinRaster3D(rasterfile, Tess, cloud);
|
---|
814 | delete(center);
|
---|
815 | };
|
---|
816 |
|
---|
817 | /** This function creates the tecplot file, displaying the tesselation of the hull.
|
---|
818 | * \param *out output stream for debugging
|
---|
819 | * \param *tecplot output stream for tecplot data
|
---|
820 | * \param N arbitrary number to differentiate various zones in the tecplot format
|
---|
821 | */
|
---|
822 | void WriteTecplotFile(ofstream * const tecplot, const Tesselation * const TesselStruct, const PointCloud * const cloud, const int N)
|
---|
823 | {
|
---|
824 | Info FunctionInfo(__func__);
|
---|
825 | if ((tecplot != NULL) && (TesselStruct != NULL)) {
|
---|
826 | // write header
|
---|
827 | *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl;
|
---|
828 | *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\" \"U\"" << endl;
|
---|
829 | *tecplot << "ZONE T=\"";
|
---|
830 | if (N < 0) {
|
---|
831 | *tecplot << cloud->GetName();
|
---|
832 | } else {
|
---|
833 | *tecplot << N << "-";
|
---|
834 | if (TesselStruct->LastTriangle != NULL) {
|
---|
835 | for (int i=0;i<3;i++)
|
---|
836 | *tecplot << (i==0 ? "" : "_") << TesselStruct->LastTriangle->endpoints[i]->node->getName();
|
---|
837 | } else {
|
---|
838 | *tecplot << "none";
|
---|
839 | }
|
---|
840 | }
|
---|
841 | *tecplot << "\", N=" << TesselStruct->PointsOnBoundary.size() << ", E=" << TesselStruct->TrianglesOnBoundary.size() << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl;
|
---|
842 | const int MaxId=cloud->GetMaxId();
|
---|
843 | int *LookupList = new int[MaxId];
|
---|
844 | for (int i=0; i< MaxId ; i++){
|
---|
845 | LookupList[i] = -1;
|
---|
846 | }
|
---|
847 |
|
---|
848 | // print atom coordinates
|
---|
849 | int Counter = 1;
|
---|
850 | TesselPoint *Walker = NULL;
|
---|
851 | for (PointMap::const_iterator target = TesselStruct->PointsOnBoundary.begin(); target != TesselStruct->PointsOnBoundary.end(); ++target) {
|
---|
852 | Walker = target->second->node;
|
---|
853 | LookupList[Walker->nr] = Counter++;
|
---|
854 | for (int i=0;i<NDIM;i++) {
|
---|
855 | const double tmp = Walker->node->at(i);
|
---|
856 | *tecplot << ((fabs(tmp) < MYEPSILON) ? 0 : tmp) << " ";
|
---|
857 | }
|
---|
858 | *tecplot << target->second->value << endl;
|
---|
859 | }
|
---|
860 | *tecplot << endl;
|
---|
861 | // print connectivity
|
---|
862 | DoLog(1) && (Log() << Verbose(1) << "The following triangles were created:" << endl);
|
---|
863 | for (TriangleMap::const_iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++) {
|
---|
864 | DoLog(1) && (Log() << Verbose(1) << " " << runner->second->endpoints[0]->node->getName() << "<->" << runner->second->endpoints[1]->node->getName() << "<->" << runner->second->endpoints[2]->node->getName() << endl);
|
---|
865 | *tecplot << LookupList[runner->second->endpoints[0]->node->nr] << " " << LookupList[runner->second->endpoints[1]->node->nr] << " " << LookupList[runner->second->endpoints[2]->node->nr] << endl;
|
---|
866 | }
|
---|
867 | delete[] (LookupList);
|
---|
868 | }
|
---|
869 | };
|
---|
870 |
|
---|
871 | /** Calculates the concavity for each of the BoundaryPointSet's in a Tesselation.
|
---|
872 | * Sets BoundaryPointSet::value equal to the number of connected lines that are not convex.
|
---|
873 | * \param *out output stream for debugging
|
---|
874 | * \param *TesselStruct pointer to Tesselation structure
|
---|
875 | */
|
---|
876 | void CalculateConcavityPerBoundaryPoint(const Tesselation * const TesselStruct)
|
---|
877 | {
|
---|
878 | Info FunctionInfo(__func__);
|
---|
879 | class BoundaryPointSet *point = NULL;
|
---|
880 | class BoundaryLineSet *line = NULL;
|
---|
881 |
|
---|
882 | // calculate remaining concavity
|
---|
883 | for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
|
---|
884 | point = PointRunner->second;
|
---|
885 | DoLog(1) && (Log() << Verbose(1) << "INFO: Current point is " << *point << "." << endl);
|
---|
886 | point->value = 0;
|
---|
887 | for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
|
---|
888 | line = LineRunner->second;
|
---|
889 | //Log() << Verbose(1) << "INFO: Current line of point " << *point << " is " << *line << "." << endl;
|
---|
890 | if (!line->CheckConvexityCriterion())
|
---|
891 | point->value += 1;
|
---|
892 | }
|
---|
893 | }
|
---|
894 | };
|
---|
895 |
|
---|
896 |
|
---|
897 | /** Checks whether each BoundaryLineSet in the Tesselation has two triangles.
|
---|
898 | * \param *out output stream for debugging
|
---|
899 | * \param *TesselStruct
|
---|
900 | * \return true - all have exactly two triangles, false - some not, list is printed to screen
|
---|
901 | */
|
---|
902 | bool CheckListOfBaselines(const Tesselation * const TesselStruct)
|
---|
903 | {
|
---|
904 | Info FunctionInfo(__func__);
|
---|
905 | LineMap::const_iterator testline;
|
---|
906 | bool result = false;
|
---|
907 | int counter = 0;
|
---|
908 |
|
---|
909 | DoLog(1) && (Log() << Verbose(1) << "Check: List of Baselines with not two connected triangles:" << endl);
|
---|
910 | for (testline = TesselStruct->LinesOnBoundary.begin(); testline != TesselStruct->LinesOnBoundary.end(); testline++) {
|
---|
911 | if (testline->second->triangles.size() != 2) {
|
---|
912 | DoLog(2) && (Log() << Verbose(2) << *testline->second << "\t" << testline->second->triangles.size() << endl);
|
---|
913 | counter++;
|
---|
914 | }
|
---|
915 | }
|
---|
916 | if (counter == 0) {
|
---|
917 | DoLog(1) && (Log() << Verbose(1) << "None." << endl);
|
---|
918 | result = true;
|
---|
919 | }
|
---|
920 | return result;
|
---|
921 | }
|
---|
922 |
|
---|
923 | /** Counts the number of triangle pairs that contain the given polygon.
|
---|
924 | * \param *P polygon with endpoints to look for
|
---|
925 | * \param *T set of triangles to create pairs from containing \a *P
|
---|
926 | */
|
---|
927 | int CountTrianglePairContainingPolygon(const BoundaryPolygonSet * const P, const TriangleSet * const T)
|
---|
928 | {
|
---|
929 | Info FunctionInfo(__func__);
|
---|
930 | // check number of endpoints in *P
|
---|
931 | if (P->endpoints.size() != 4) {
|
---|
932 | DoeLog(1) && (eLog()<< Verbose(1) << "CountTrianglePairContainingPolygon works only on polygons with 4 nodes!" << endl);
|
---|
933 | return 0;
|
---|
934 | }
|
---|
935 |
|
---|
936 | // check number of triangles in *T
|
---|
937 | if (T->size() < 2) {
|
---|
938 | DoeLog(1) && (eLog()<< Verbose(1) << "Not enough triangles to have pairs!" << endl);
|
---|
939 | return 0;
|
---|
940 | }
|
---|
941 |
|
---|
942 | DoLog(0) && (Log() << Verbose(0) << "Polygon is " << *P << endl);
|
---|
943 | // create each pair, get the endpoints and check whether *P is contained.
|
---|
944 | int counter = 0;
|
---|
945 | PointSet Trianglenodes;
|
---|
946 | class BoundaryPolygonSet PairTrianglenodes;
|
---|
947 | for(TriangleSet::iterator Walker = T->begin(); Walker != T->end(); Walker++) {
|
---|
948 | for (int i=0;i<3;i++)
|
---|
949 | Trianglenodes.insert((*Walker)->endpoints[i]);
|
---|
950 |
|
---|
951 | for(TriangleSet::iterator PairWalker = Walker; PairWalker != T->end(); PairWalker++) {
|
---|
952 | if (Walker != PairWalker) { // skip first
|
---|
953 | PairTrianglenodes.endpoints = Trianglenodes;
|
---|
954 | for (int i=0;i<3;i++)
|
---|
955 | PairTrianglenodes.endpoints.insert((*PairWalker)->endpoints[i]);
|
---|
956 | const int size = PairTrianglenodes.endpoints.size();
|
---|
957 | if (size == 4) {
|
---|
958 | DoLog(0) && (Log() << Verbose(0) << " Current pair of triangles: " << **Walker << "," << **PairWalker << " with " << size << " distinct endpoints:" << PairTrianglenodes << endl);
|
---|
959 | // now check
|
---|
960 | if (PairTrianglenodes.ContainsPresentTupel(P)) {
|
---|
961 | counter++;
|
---|
962 | DoLog(0) && (Log() << Verbose(0) << " ACCEPT: Matches with " << *P << endl);
|
---|
963 | } else {
|
---|
964 | DoLog(0) && (Log() << Verbose(0) << " REJECT: No match with " << *P << endl);
|
---|
965 | }
|
---|
966 | } else {
|
---|
967 | DoLog(0) && (Log() << Verbose(0) << " REJECT: Less than four endpoints." << endl);
|
---|
968 | }
|
---|
969 | }
|
---|
970 | }
|
---|
971 | Trianglenodes.clear();
|
---|
972 | }
|
---|
973 | return counter;
|
---|
974 | };
|
---|
975 |
|
---|
976 | /** Checks whether two give polygons have two or more points in common.
|
---|
977 | * \param *P1 first polygon
|
---|
978 | * \param *P2 second polygon
|
---|
979 | * \return true - are connected, false = are note
|
---|
980 | */
|
---|
981 | bool ArePolygonsEdgeConnected(const BoundaryPolygonSet * const P1, const BoundaryPolygonSet * const P2)
|
---|
982 | {
|
---|
983 | Info FunctionInfo(__func__);
|
---|
984 | int counter = 0;
|
---|
985 | for(PointSet::const_iterator Runner = P1->endpoints.begin(); Runner != P1->endpoints.end(); Runner++) {
|
---|
986 | if (P2->ContainsBoundaryPoint((*Runner))) {
|
---|
987 | counter++;
|
---|
988 | DoLog(1) && (Log() << Verbose(1) << *(*Runner) << " of second polygon is found in the first one." << endl);
|
---|
989 | return true;
|
---|
990 | }
|
---|
991 | }
|
---|
992 | return false;
|
---|
993 | };
|
---|
994 |
|
---|
995 | /** Combines second into the first and deletes the second.
|
---|
996 | * \param *P1 first polygon, contains all nodes on return
|
---|
997 | * \param *&P2 second polygon, is deleted.
|
---|
998 | */
|
---|
999 | void CombinePolygons(BoundaryPolygonSet * const P1, BoundaryPolygonSet * &P2)
|
---|
1000 | {
|
---|
1001 | Info FunctionInfo(__func__);
|
---|
1002 | pair <PointSet::iterator, bool> Tester;
|
---|
1003 | for(PointSet::iterator Runner = P2->endpoints.begin(); Runner != P2->endpoints.end(); Runner++) {
|
---|
1004 | Tester = P1->endpoints.insert((*Runner));
|
---|
1005 | if (Tester.second)
|
---|
1006 | DoLog(0) && (Log() << Verbose(0) << "Inserting endpoint " << *(*Runner) << " into first polygon." << endl);
|
---|
1007 | }
|
---|
1008 | P2->endpoints.clear();
|
---|
1009 | delete(P2);
|
---|
1010 | };
|
---|
1011 |
|
---|