source: src/tesselationhelpers.cpp@ d74077

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 d74077 was d74077, checked in by Frederik Heber <heber@…>, 15 years ago

Member variable Vector and element of class atom are now private.

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