source: src/tesselationhelpers.cpp@ e4b2f6

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 e4b2f6 was 735b1c, checked in by Frederik Heber <heber@…>, 14 years ago

ParticleInfo::ParticleInfo_nr is protected and accessed via getter/setter.

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