source: src/tesselationhelpers.cpp@ 0e01b4

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 0e01b4 was 8cbb97, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'VectorRefactoring' into StructureRefactoring

Conflicts:

molecuilder/src/Legacy/oldmenu.cpp
molecuilder/src/Makefile.am
molecuilder/src/analysis_correlation.cpp
molecuilder/src/boundary.cpp
molecuilder/src/builder.cpp
molecuilder/src/config.cpp
molecuilder/src/ellipsoid.cpp
molecuilder/src/linkedcell.cpp
molecuilder/src/molecule.cpp
molecuilder/src/molecule_fragmentation.cpp
molecuilder/src/molecule_geometry.cpp
molecuilder/src/molecule_graph.cpp
molecuilder/src/moleculelist.cpp
molecuilder/src/tesselation.cpp
molecuilder/src/tesselationhelpers.cpp
molecuilder/src/unittests/AnalysisCorrelationToSurfaceUnitTest.cpp
molecuilder/src/unittests/bondgraphunittest.cpp
molecuilder/src/vector.cpp
molecuilder/src/vector.hpp

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