source: src/tesselationhelpers.cpp@ 42a101

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 42a101 was 643e76, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed getIntersectionOfTwoLinesOnPlane() in favor of the Line::getIntersection() method.

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