source: src/tesselationhelpers.cpp@ 56fb09

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 56fb09 was 24c35a, checked in by Frederik Heber <heber@…>, 15 years ago

Removed unused variables.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 43.5 KB
RevLine 
[357fba]1/*
2 * TesselationHelpers.cpp
3 *
4 * Created on: Aug 3, 2009
5 * Author: heber
6 */
7
[112b09]8#include "Helpers/MemDebug.hpp"
9
[f66195]10#include <fstream>
11
[f67b6e]12#include "info.hpp"
[f66195]13#include "linkedcell.hpp"
[b32dbb]14#include "linearsystemofequations.hpp"
[e138de]15#include "log.hpp"
[f66195]16#include "tesselation.hpp"
[357fba]17#include "tesselationhelpers.hpp"
[f66195]18#include "vector.hpp"
[643e76]19#include "Line.hpp"
[0a4f7f]20#include "vector_ops.hpp"
[f66195]21#include "verbose.hpp"
[d4c9ae]22#include "Plane.hpp"
[357fba]23
[f67b6e]24double DetGet(gsl_matrix * const A, const int inPlace)
25{
26 Info FunctionInfo(__func__);
[357fba]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);
[24a5e0]35 gsl_matrix *tmpA=0;
[357fba]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
[c0f6c6]54void GetSphere(Vector * const center, const Vector &a, const Vector &b, const Vector &c, const double RADIUS)
[357fba]55{
[f67b6e]56 Info FunctionInfo(__func__);
[357fba]57 gsl_matrix *A = gsl_matrix_calloc(3,3);
58 double m11, m12, m13, m14;
59
60 for(int i=0;i<3;i++) {
[0a4f7f]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]);
[357fba]64 }
[f1cccd]65 m11 = DetGet(A, 1);
[357fba]66
67 for(int i=0;i<3;i++) {
[0a4f7f]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]);
[357fba]71 }
[f1cccd]72 m12 = DetGet(A, 1);
[357fba]73
74 for(int i=0;i<3;i++) {
[0a4f7f]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]);
[357fba]78 }
[f1cccd]79 m13 = DetGet(A, 1);
[357fba]80
81 for(int i=0;i<3;i++) {
[0a4f7f]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]);
[357fba]85 }
[f1cccd]86 m14 = DetGet(A, 1);
[357fba]87
88 if (fabs(m11) < MYEPSILON)
[58ed4a]89 DoeLog(1) && (eLog()<< Verbose(1) << "three points are colinear." << endl);
[357fba]90
[0a4f7f]91 center->at(0) = 0.5 * m12/ m11;
92 center->at(1) = -0.5 * m13/ m11;
93 center->at(2) = 0.5 * m14/ m11;
[357fba]94
[1513a74]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);
[357fba]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
[c0f6c6]109 * @param *Umkreismittelpunkt new center point of circumference
[357fba]110 * @param Direction vector indicates up/down
[c0f6c6]111 * @param AlternativeDirection Vector, needed in case the triangles have 90 deg angle
[357fba]112 * @param Halfplaneindicator double indicates whether Direction is up or down
[c0f6c6]113 * @param AlternativeIndicator double indicates in case of orthogonal triangles which direction of AlternativeDirection is suitable
[357fba]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 */
[c0f6c6]120void 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)
[357fba]122{
[f67b6e]123 Info FunctionInfo(__func__);
[357fba]124 Vector TempNormal, helper;
125 double Restradius;
126 Vector OtherCenter;
127 Center->Zero();
[273382]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;
[357fba]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)));
[273382]136 (*NewUmkreismittelpunkt) = (*Center);
[a67d19]137 DoLog(1) && (Log() << Verbose(1) << "Center of new circumference is " << *NewUmkreismittelpunkt << ".\n");
[357fba]138 // Here we calculated center of circumscribing circle, using barycentric coordinates
[a67d19]139 DoLog(1) && (Log() << Verbose(1) << "Center of circumference is " << *Center << " in direction " << *Direction << ".\n");
[357fba]140
[273382]141 TempNormal = a - b;
142 helper = a - c;
143 TempNormal.VectorProduct(helper);
[357fba]144 if (fabs(HalfplaneIndicator) < MYEPSILON)
145 {
[273382]146 if ((TempNormal.ScalarProduct(*AlternativeDirection) <0 && AlternativeIndicator >0) || (TempNormal.ScalarProduct(*AlternativeDirection) >0 && AlternativeIndicator <0))
[357fba]147 {
[273382]148 TempNormal *= -1;
[357fba]149 }
150 }
151 else
152 {
[273382]153 if (((TempNormal.ScalarProduct(*Direction)<0) && (HalfplaneIndicator >0)) || ((TempNormal.ScalarProduct(*Direction)>0) && (HalfplaneIndicator<0)))
[357fba]154 {
[273382]155 TempNormal *= -1;
[357fba]156 }
157 }
158
159 TempNormal.Normalize();
160 Restradius = sqrt(RADIUS*RADIUS - Umkreisradius*Umkreisradius);
[a67d19]161 DoLog(1) && (Log() << Verbose(1) << "Height of center of circumference to center of sphere is " << Restradius << ".\n");
[357fba]162 TempNormal.Scale(Restradius);
[a67d19]163 DoLog(1) && (Log() << Verbose(1) << "Shift vector to sphere of circumference is " << TempNormal << ".\n");
[273382]164 (*Center) += TempNormal;
[a67d19]165 DoLog(1) && (Log() << Verbose(1) << "Center of sphere of circumference is " << *Center << ".\n");
[f1cccd]166 GetSphere(&OtherCenter, a, b, c, RADIUS);
[a67d19]167 DoLog(1) && (Log() << Verbose(1) << "OtherCenter of sphere of circumference is " << OtherCenter << ".\n");
[357fba]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 */
[c0f6c6]177void GetCenterofCircumcircle(Vector * const Center, const Vector &a, const Vector &b, const Vector &c)
[357fba]178{
[f67b6e]179 Info FunctionInfo(__func__);
[357fba]180 Vector helper;
[273382]181 Vector SideA = b - c;
182 Vector SideB = c - a;
183 Vector SideC = a - b;
[357fba]184
[b32dbb]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;
[357fba]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 */
[c0f6c6]210double GetPathLengthonCircumCircle(const Vector &CircleCenter, const Vector &CirclePlaneNormal, const double CircleRadius, const Vector &NewSphereCenter, const Vector &OldSphereCenter, const Vector &NormalVector, const Vector &SearchDirection)
[357fba]211{
[f67b6e]212 Info FunctionInfo(__func__);
[357fba]213 Vector helper;
214 double radius, alpha;
[273382]215
216 Vector RelativeOldSphereCenter = OldSphereCenter - CircleCenter;
217 Vector RelativeNewSphereCenter = NewSphereCenter - CircleCenter;
218 helper = RelativeNewSphereCenter;
[357fba]219 // test whether new center is on the parameter circle's plane
[273382]220 if (fabs(helper.ScalarProduct(CirclePlaneNormal)) > HULLEPSILON) {
[8cbb97]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);
[273382]222 helper.ProjectOntoPlane(CirclePlaneNormal);
[357fba]223 }
[b998c3]224 radius = helper.NormSquared();
[357fba]225 // test whether the new center vector has length of CircleRadius
226 if (fabs(radius - CircleRadius) > HULLEPSILON)
[58ed4a]227 DoeLog(1) && (eLog()<< Verbose(1) << "The projected center of the new sphere has radius " << radius << " instead of " << CircleRadius << "." << endl);
[273382]228 alpha = helper.Angle(RelativeOldSphereCenter);
[357fba]229 // make the angle unique by checking the halfplanes/search direction
[273382]230 if (helper.ScalarProduct(SearchDirection) < -HULLEPSILON) // acos is not unique on [0, 2.*M_PI), hence extra check to decide between two half intervals
[357fba]231 alpha = 2.*M_PI - alpha;
[a67d19]232 DoLog(1) && (Log() << Verbose(1) << "INFO: RelativeNewSphereCenter is " << helper << ", RelativeOldSphereCenter is " << RelativeOldSphereCenter << " and resulting angle is " << alpha << "." << endl);
[1513a74]233 radius = helper.distance(RelativeOldSphereCenter);
[273382]234 helper.ProjectOntoPlane(NormalVector);
[357fba]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)) {
[a67d19]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);
[357fba]238 return alpha;
239 } else {
[a67d19]240 DoLog(1) && (Log() << Verbose(1) << "INFO: NewSphereCenter " << RelativeNewSphereCenter << " is too close to RelativeOldSphereCenter" << RelativeOldSphereCenter << "." << endl);
[357fba]241 return 2.*M_PI;
242 }
243};
244
245struct 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 */
258double MinIntersectDistance(const gsl_vector * x, void *params)
259{
[f67b6e]260 Info FunctionInfo(__func__);
[357fba]261 double retval = 0;
262 struct Intersection *I = (struct Intersection *)params;
263 Vector intersection;
264 for (int i=0;i<NDIM;i++)
[0a4f7f]265 intersection[i] = gsl_vector_get(x, i);
[357fba]266
[273382]267 Vector SideA = I->x1 -I->x2 ;
268 Vector HeightA = intersection - I->x1;
269 HeightA.ProjectOntoPlane(SideA);
[357fba]270
[273382]271 Vector SideB = I->x3 - I->x4;
272 Vector HeightB = intersection - I->x3;
273 HeightB.ProjectOntoPlane(SideB);
[357fba]274
[273382]275 retval = HeightA.ScalarProduct(HeightA) + HeightB.ScalarProduct(HeightB);
[f67b6e]276 //Log() << Verbose(1) << "MinIntersectDistance called, result: " << retval << endl;
[357fba]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 */
[c0f6c6]294bool existsIntersection(const Vector &point1, const Vector &point2, const Vector &point3, const Vector &point4)
[357fba]295{
[f67b6e]296 Info FunctionInfo(__func__);
[357fba]297 bool result;
298
299 struct Intersection par;
[273382]300 par.x1 = point1;
301 par.x2 = point2;
302 par.x3 = point3;
303 par.x4 = point4;
[357fba]304
305 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
306 gsl_multimin_fminimizer *s = NULL;
307 gsl_vector *ss, *x;
[f1cccd]308 gsl_multimin_function minexFunction;
[357fba]309
310 size_t iter = 0;
311 int status;
312 double size;
313
314 /* Starting point */
315 x = gsl_vector_alloc(NDIM);
[0a4f7f]316 gsl_vector_set(x, 0, point1[0]);
317 gsl_vector_set(x, 1, point1[1]);
318 gsl_vector_set(x, 2, point1[2]);
[357fba]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 */
[f1cccd]325 minexFunction.n = NDIM;
326 minexFunction.f = &MinIntersectDistance;
327 minexFunction.params = (void *)&par;
[357fba]328
329 s = gsl_multimin_fminimizer_alloc(T, NDIM);
[f1cccd]330 gsl_multimin_fminimizer_set(s, &minexFunction, x, ss);
[357fba]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) {
[a67d19]344 DoLog(1) && (Log() << Verbose(1) << "converged to minimum" << endl);
[357fba]345 }
346 } while (status == GSL_CONTINUE && iter < 100);
347
348 // check whether intersection is in between or not
[273382]349 Vector intersection;
[357fba]350 double t1, t2;
351 for (int i = 0; i < NDIM; i++) {
[0a4f7f]352 intersection[i] = gsl_vector_get(s->x, i);
[357fba]353 }
354
[273382]355 Vector SideA = par.x2 - par.x1;
356 Vector HeightA = intersection - par.x1;
[357fba]357
[273382]358 t1 = HeightA.ScalarProduct(SideA)/SideA.ScalarProduct(SideA);
[357fba]359
[273382]360 Vector SideB = par.x4 - par.x3;
361 Vector HeightB = intersection - par.x3;
[357fba]362
[273382]363 t2 = HeightB.ScalarProduct(SideB)/SideB.ScalarProduct(SideB);
[357fba]364
[f67b6e]365 Log() << Verbose(1) << "Intersection " << intersection << " is at "
[357fba]366 << t1 << " for (" << point1 << "," << point2 << ") and at "
367 << t2 << " for (" << point3 << "," << point4 << "): ";
368
369 if (((t1 >= 0) && (t1 <= 1)) && ((t2 >= 0) && (t2 <= 1))) {
[a67d19]370 DoLog(1) && (Log() << Verbose(1) << "true intersection." << endl);
[357fba]371 result = true;
372 } else {
[a67d19]373 DoLog(1) && (Log() << Verbose(1) << "intersection out of region of interest." << endl);
[357fba]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;
[91e7e4a]383};
384
[57066a]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 */
[c0f6c6]394double GetAngle(const Vector &point, const Vector &reference, const Vector &OrthogonalVector)
[57066a]395{
[f67b6e]396 Info FunctionInfo(__func__);
[57066a]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;
[273382]403 double phi = point.Angle(reference);
404 if (OrthogonalVector.ScalarProduct(point) > 0) {
[57066a]405 phi = 2.*M_PI - phi;
406 }
407
[a67d19]408 DoLog(1) && (Log() << Verbose(1) << "INFO: " << point << " has angle " << phi << " with respect to reference " << reference << "." << endl);
[57066a]409
410 return phi;
411}
412
[91e7e4a]413
414/** Calculates the volume of a general tetraeder.
415 * \param *a first vector
[b32dbb]416 * \param *b second vector
417 * \param *c third vector
418 * \param *d fourth vector
[91e7e4a]419 * \return \f$ \frac{1}{6} \cdot ((a-d) \times (a-c) \cdot (a-b)) \f$
420 */
[c0f6c6]421double CalculateVolumeofGeneralTetraeder(const Vector &a, const Vector &b, const Vector &c, const Vector &d)
[91e7e4a]422{
[f67b6e]423 Info FunctionInfo(__func__);
[91e7e4a]424 Vector Point, TetraederVector[3];
425 double volume;
426
[1bd79e]427 TetraederVector[0] = a;
428 TetraederVector[1] = b;
429 TetraederVector[2] = c;
[91e7e4a]430 for (int j=0;j<3;j++)
[273382]431 TetraederVector[j].SubtractVector(d);
[1bd79e]432 Point = TetraederVector[0];
[273382]433 Point.VectorProduct(TetraederVector[1]);
434 volume = 1./6. * fabs(Point.ScalarProduct(TetraederVector[2]));
[91e7e4a]435 return volume;
436};
[357fba]437
[b32dbb]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 */
445double 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
[57066a]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 */
[c0f6c6]465bool CheckLineCriteriaForDegeneratedTriangle(const BoundaryPointSet * const nodes[3])
[57066a]466{
[f67b6e]467 Info FunctionInfo(__func__);
[57066a]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++) {
[f1ef60a]474 if (nodes[i] == NULL) {
[a67d19]475 DoLog(1) && (Log() << Verbose(1) << "Node nr. " << i << " is not yet present." << endl);
[f1ef60a]476 result = true;
477 } else if (nodes[i]->lines.find(nodes[j]->node->nr) != nodes[i]->lines.end()) { // there already is a line
[776b64]478 LineMap::const_iterator FindLine;
479 pair<LineMap::const_iterator,LineMap::const_iterator> FindPair;
[57066a]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
[a67d19]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);
[57066a]490 result = true;
491 }
492 }
493 if ((!result) && (counter > 1)) {
[a67d19]494 DoLog(1) && (Log() << Verbose(1) << "INFO: Degenerate triangle is ok, at least two, here " << counter << ", existing lines are used." << endl);
[57066a]495 result = true;
496 }
497 return result;
498};
499
500
[f67b6e]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
[58ed4a]508// DoeLog(1) && (eLog()<< Verbose(1) << "sortCandidates was called for two different baselines: " << candidate1->BaseLine << " and " << candidate2->BaseLine << "." << endl);
[f67b6e]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//};
[57066a]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 */
[71b20e]554TesselPoint* FindSecondClosestTesselPoint(const Vector* Point, const LinkedCell* const LC)
[57066a]555{
[f67b6e]556 Info FunctionInfo(__func__);
[57066a]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];
[a67d19]567 DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
[57066a]568
569 LC->GetNeighbourBounds(Nlower, Nupper);
[f67b6e]570 //Log() << Verbose(1) << endl;
[57066a]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]++) {
[734816]574 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[f67b6e]575 //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
[57066a]576 if (List != NULL) {
[734816]577 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[273382]578 helper = (*Point) - (*(*Runner)->node);
[57066a]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);
[e138de]587 //Log() << Verbose(2) << "INFO: New Second Nearest Neighbour is " << *secondClosestPoint << "." << endl;
[57066a]588 }
589 }
590 } else {
[717e0c]591 eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
[57066a]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 */
[71b20e]608TesselPoint* FindClosestTesselPoint(const Vector* Point, TesselPoint *&SecondPoint, const LinkedCell* const LC)
[57066a]609{
[f67b6e]610 Info FunctionInfo(__func__);
[57066a]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];
[a67d19]621 DoLog(1) && (Log() << Verbose(1) << "INFO: Center cell is " << N[0] << ", " << N[1] << ", " << N[2] << " with No. " << LC->index << "." << endl);
[57066a]622
623 LC->GetNeighbourBounds(Nlower, Nupper);
[f67b6e]624 //Log() << Verbose(1) << endl;
[57066a]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]++) {
[734816]628 const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
[f67b6e]629 //Log() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << "," << LC->n[2] << endl;
[57066a]630 if (List != NULL) {
[734816]631 for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
[273382]632 helper = (*Point) - (*(*Runner)->node);
[71b20e]633 double currentNorm = helper.NormSquared();
[57066a]634 if (currentNorm < distance) {
635 secondDistance = distance;
636 SecondPoint = closestPoint;
637 distance = currentNorm;
638 closestPoint = (*Runner);
[f67b6e]639 //Log() << Verbose(1) << "INFO: New Nearest Neighbour is " << *closestPoint << "." << endl;
[57066a]640 } else if (currentNorm < secondDistance) {
641 secondDistance = currentNorm;
642 SecondPoint = (*Runner);
[f67b6e]643 //Log() << Verbose(1) << "INFO: New Second Nearest Neighbour is " << *SecondPoint << "." << endl;
[57066a]644 }
645 }
646 } else {
[717e0c]647 eLog() << Verbose(1) << "The current cell " << LC->n[0] << "," << LC->n[1] << ","
[57066a]648 << LC->n[2] << " is invalid!" << endl;
649 }
650 }
[a2028e]651 // output
652 if (closestPoint != NULL) {
[a67d19]653 DoLog(1) && (Log() << Verbose(1) << "Closest point is " << *closestPoint);
[a2028e]654 if (SecondPoint != NULL)
[a67d19]655 DoLog(0) && (Log() << Verbose(0) << " and second closest is " << *SecondPoint);
656 DoLog(0) && (Log() << Verbose(0) << "." << endl);
[a2028e]657 }
[57066a]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 */
[e138de]667Vector * GetClosestPointBetweenLine(const BoundaryLineSet * const Base, const BoundaryLineSet * const OtherBase)
[57066a]668{
[f67b6e]669 Info FunctionInfo(__func__);
[57066a]670 // construct the plane of the two baselines (i.e. take both their directional vectors)
[273382]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);
[57066a]675 Normal.Normalize();
[a67d19]676 DoLog(1) && (Log() << Verbose(1) << "First direction is " << Baseline << ", second direction is " << OtherBaseline << ", normal of intersection plane is " << Normal << "." << endl);
[57066a]677
678 // project one offset point of OtherBase onto this plane (and add plane offset vector)
[273382]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;
[57066a]683
684 // calculate the intersection between this projected baseline and Base
685 Vector *Intersection = new Vector;
[643e76]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);
[273382]689 Normal = (*Intersection) - (*Base->endpoints[0]->node->node);
[8cbb97]690 DoLog(1) && (Log() << Verbose(1) << "Found closest point on " << *Base << " at " << *Intersection << ", factor in line is " << fabs(Normal.ScalarProduct(Baseline)/Baseline.NormSquared()) << "." << endl);
[57066a]691
692 return Intersection;
693};
694
[c4d4df]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 */
[e138de]701double DistanceToTrianglePlane(const Vector *x, const BoundaryTriangleSet * const triangle)
[c4d4df]702{
[f67b6e]703 Info FunctionInfo(__func__);
[c4d4df]704 double distance = 0.;
705 if (x == NULL) {
706 return -1;
707 }
[d4c9ae]708 distance = x->DistanceToSpace(triangle->getPlane());
[c4d4df]709 return distance;
710};
[57066a]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 */
[e138de]718void WriteVrmlFile(ofstream * const vrmlfile, const Tesselation * const Tess, const PointCloud * const cloud)
[57066a]719{
[f67b6e]720 Info FunctionInfo(__func__);
[57066a]721 TesselPoint *Walker = NULL;
722 int i;
[e138de]723 Vector *center = cloud->GetCenter();
[57066a]724 if (vrmlfile != NULL) {
[e138de]725 //Log() << Verbose(1) << "Writing Raster3D file ... ";
[57066a]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++)
[0a4f7f]734 *vrmlfile << Walker->node->at(i)-center->at(i) << " ";
[57066a]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;
[776b64]740 for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
[57066a]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
[0a4f7f]744 *vrmlfile << TriangleRunner->second->endpoints[i]->node->node->at(j)-center->at(j) << " ";
[57066a]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 {
[58ed4a]751 DoeLog(1) && (eLog()<< Verbose(1) << "Given vrmlfile is " << vrmlfile << "." << endl);
[57066a]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 */
[e138de]762void IncludeSphereinRaster3D(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
[57066a]763{
[f67b6e]764 Info FunctionInfo(__func__);
[57066a]765 Vector helper;
[6a7f78c]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
[273382]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);
[6a7f78c]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";
[0a4f7f]778 *rasterfile << "2\n " << helper[0] << " " << helper[1] << " " << helper[2] << "\t" << 5. << "\t1 0 0\n";
[6a7f78c]779 *rasterfile << "9\n terminating special property\n";
780 delete(center);
781 }
[57066a]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 */
[e138de]790void WriteRaster3dFile(ofstream * const rasterfile, const Tesselation * const Tess, const PointCloud * const cloud)
[57066a]791{
[f67b6e]792 Info FunctionInfo(__func__);
[57066a]793 TesselPoint *Walker = NULL;
794 int i;
[fc9992]795 Vector *center = cloud->GetCenter();
[57066a]796 if (rasterfile != NULL) {
[e138de]797 //Log() << Verbose(1) << "Writing Raster3D file ... ";
[57066a]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
[15b670]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 }
[57066a]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";
[776b64]815 for (TriangleMap::const_iterator TriangleRunner = Tess->TrianglesOnBoundary.begin(); TriangleRunner != Tess->TrianglesOnBoundary.end(); TriangleRunner++) {
[57066a]816 *rasterfile << "1" << endl << " "; // 1 is triangle type
817 for (i=0;i<3;i++) { // print each node
[15b670]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 }
[57066a]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 {
[58ed4a]829 DoeLog(1) && (eLog()<< Verbose(1) << "Given rasterfile is " << rasterfile << "." << endl);
[57066a]830 }
[e138de]831 IncludeSphereinRaster3D(rasterfile, Tess, cloud);
[57066a]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 */
[e138de]840void WriteTecplotFile(ofstream * const tecplot, const Tesselation * const TesselStruct, const PointCloud * const cloud, const int N)
[57066a]841{
[f67b6e]842 Info FunctionInfo(__func__);
[57066a]843 if ((tecplot != NULL) && (TesselStruct != NULL)) {
844 // write header
845 *tecplot << "TITLE = \"3D CONVEX SHELL\"" << endl;
846 *tecplot << "VARIABLES = \"X\" \"Y\" \"Z\" \"U\"" << endl;
[6a7f78c]847 *tecplot << "ZONE T=\"";
848 if (N < 0) {
849 *tecplot << cloud->GetName();
850 } else {
851 *tecplot << N << "-";
[b60a29]852 if (TesselStruct->LastTriangle != NULL) {
853 for (int i=0;i<3;i++)
[68f03d]854 *tecplot << (i==0 ? "" : "_") << TesselStruct->LastTriangle->endpoints[i]->node->getName();
[b60a29]855 } else {
856 *tecplot << "none";
857 }
[6a7f78c]858 }
[57066a]859 *tecplot << "\", N=" << TesselStruct->PointsOnBoundary.size() << ", E=" << TesselStruct->TrianglesOnBoundary.size() << ", DATAPACKING=POINT, ZONETYPE=FETRIANGLE" << endl;
[15b670]860 const int MaxId=cloud->GetMaxId();
861 int *LookupList = new int[MaxId];
862 for (int i=0; i< MaxId ; i++){
[57066a]863 LookupList[i] = -1;
[c72112]864 }
[57066a]865
866 // print atom coordinates
867 int Counter = 1;
868 TesselPoint *Walker = NULL;
[c72112]869 for (PointMap::const_iterator target = TesselStruct->PointsOnBoundary.begin(); target != TesselStruct->PointsOnBoundary.end(); ++target) {
[57066a]870 Walker = target->second->node;
871 LookupList[Walker->nr] = Counter++;
[15b670]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;
[57066a]877 }
878 *tecplot << endl;
879 // print connectivity
[a67d19]880 DoLog(1) && (Log() << Verbose(1) << "The following triangles were created:" << endl);
[776b64]881 for (TriangleMap::const_iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++) {
[68f03d]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);
[57066a]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};
[7dea7c]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 */
[e138de]894void CalculateConcavityPerBoundaryPoint(const Tesselation * const TesselStruct)
[7dea7c]895{
[f67b6e]896 Info FunctionInfo(__func__);
[7dea7c]897 class BoundaryPointSet *point = NULL;
898 class BoundaryLineSet *line = NULL;
[b32dbb]899 class BoundaryTriangleSet *triangle = NULL;
900 double ConcavityPerLine = 0.;
901 double ConcavityPerTriangle = 0.;
902 double area = 0.;
903 double totalarea = 0.;
[7dea7c]904
[776b64]905 for (PointMap::const_iterator PointRunner = TesselStruct->PointsOnBoundary.begin(); PointRunner != TesselStruct->PointsOnBoundary.end(); PointRunner++) {
[7dea7c]906 point = PointRunner->second;
[a67d19]907 DoLog(1) && (Log() << Verbose(1) << "INFO: Current point is " << *point << "." << endl);
[b32dbb]908
909 // calculate mean concavity over all connected line
910 ConcavityPerLine = 0.;
[7dea7c]911 for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
912 line = LineRunner->second;
[f67b6e]913 //Log() << Verbose(1) << "INFO: Current line of point " << *point << " is " << *line << "." << endl;
[b32dbb]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;
[7dea7c]938 }
[b32dbb]939 ConcavityPerTriangle /= triangles->size()/totalarea;
940 delete(triangles);
941
942 // add up
943 point->value = ConcavityPerLine + ConcavityPerTriangle;
[7dea7c]944 }
945};
946
947
[b32dbb]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 */
955void 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
[7dea7c]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 */
[e138de]979bool CheckListOfBaselines(const Tesselation * const TesselStruct)
[7dea7c]980{
[f67b6e]981 Info FunctionInfo(__func__);
[776b64]982 LineMap::const_iterator testline;
[7dea7c]983 bool result = false;
984 int counter = 0;
985
[a67d19]986 DoLog(1) && (Log() << Verbose(1) << "Check: List of Baselines with not two connected triangles:" << endl);
[7dea7c]987 for (testline = TesselStruct->LinesOnBoundary.begin(); testline != TesselStruct->LinesOnBoundary.end(); testline++) {
988 if (testline->second->triangles.size() != 2) {
[a67d19]989 DoLog(2) && (Log() << Verbose(2) << *testline->second << "\t" << testline->second->triangles.size() << endl);
[7dea7c]990 counter++;
991 }
992 }
993 if (counter == 0) {
[a67d19]994 DoLog(1) && (Log() << Verbose(1) << "None." << endl);
[7dea7c]995 result = true;
996 }
997 return result;
998}
999
[262bae]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 */
1004int 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) {
[58ed4a]1009 DoeLog(1) && (eLog()<< Verbose(1) << "CountTrianglePairContainingPolygon works only on polygons with 4 nodes!" << endl);
[262bae]1010 return 0;
1011 }
1012
1013 // check number of triangles in *T
1014 if (T->size() < 2) {
[58ed4a]1015 DoeLog(1) && (eLog()<< Verbose(1) << "Not enough triangles to have pairs!" << endl);
[262bae]1016 return 0;
1017 }
1018
[a67d19]1019 DoLog(0) && (Log() << Verbose(0) << "Polygon is " << *P << endl);
[262bae]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]);
[856098]1033 const int size = PairTrianglenodes.endpoints.size();
1034 if (size == 4) {
[a67d19]1035 DoLog(0) && (Log() << Verbose(0) << " Current pair of triangles: " << **Walker << "," << **PairWalker << " with " << size << " distinct endpoints:" << PairTrianglenodes << endl);
[856098]1036 // now check
1037 if (PairTrianglenodes.ContainsPresentTupel(P)) {
1038 counter++;
[a67d19]1039 DoLog(0) && (Log() << Verbose(0) << " ACCEPT: Matches with " << *P << endl);
[856098]1040 } else {
[a67d19]1041 DoLog(0) && (Log() << Verbose(0) << " REJECT: No match with " << *P << endl);
[856098]1042 }
[262bae]1043 } else {
[a67d19]1044 DoLog(0) && (Log() << Verbose(0) << " REJECT: Less than four endpoints." << endl);
[262bae]1045 }
1046 }
1047 }
[856098]1048 Trianglenodes.clear();
[262bae]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 */
1058bool 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++;
[a67d19]1065 DoLog(1) && (Log() << Verbose(1) << *(*Runner) << " of second polygon is found in the first one." << endl);
[262bae]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 */
1076void CombinePolygons(BoundaryPolygonSet * const P1, BoundaryPolygonSet * &P2)
1077{
1078 Info FunctionInfo(__func__);
[856098]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)
[a67d19]1083 DoLog(0) && (Log() << Verbose(0) << "Inserting endpoint " << *(*Runner) << " into first polygon." << endl);
[262bae]1084 }
1085 P2->endpoints.clear();
1086 delete(P2);
1087};
1088
Note: See TracBrowser for help on using the repository browser.