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