source: src/BoundaryTriangleSet.cpp@ 1d5a871

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 1d5a871 was ad011c, checked in by Frederik Heber <heber@…>, 14 years ago

CodePatterns places all includes now in subfolder CodePatterns/.

  • change all includes accordingly.
  • this was necessary as Helpers and Patterns are not very distinctive names for include folders. Already now, we had a conflict between Helpers from CodePatterns and Helpers from this project.
  • changed compilation test in ax_codepatterns.m4 when changing CodePatterns includes.
  • Property mode set to 100644
File size: 17.3 KB
Line 
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
8/*
9 * BoundaryTriangleSet.cpp
10 *
11 * Created on: Jul 29, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "BoundaryTriangleSet.hpp"
23
24#include <iostream>
25
26#include "BoundaryLineSet.hpp"
27#include "BoundaryPointSet.hpp"
28#include "TesselPoint.hpp"
29
30#include "CodePatterns/Assert.hpp"
31#include "CodePatterns/Info.hpp"
32#include "LinearAlgebra/Line.hpp"
33#include "CodePatterns/Log.hpp"
34#include "LinearAlgebra/Plane.hpp"
35#include "LinearAlgebra/Vector.hpp"
36#include "CodePatterns/Verbose.hpp"
37
38using namespace std;
39
40/** Constructor for BoundaryTriangleSet.
41 */
42BoundaryTriangleSet::BoundaryTriangleSet() :
43 Nr(-1)
44{
45 Info FunctionInfo(__func__);
46 for (int i = 0; i < 3; i++) {
47 endpoints[i] = NULL;
48 lines[i] = NULL;
49 }
50}
51;
52
53/** Constructor for BoundaryTriangleSet with three lines.
54 * \param *line[3] lines that make up the triangle
55 * \param number number of triangle
56 */
57BoundaryTriangleSet::BoundaryTriangleSet(class BoundaryLineSet * const line[3], const int number) :
58 Nr(number)
59{
60 Info FunctionInfo(__func__);
61 // set number
62 // set lines
63 for (int i = 0; i < 3; i++) {
64 lines[i] = line[i];
65 lines[i]->AddTriangle(this);
66 }
67 // get ascending order of endpoints
68 PointMap OrderMap;
69 for (int i = 0; i < 3; i++) {
70 // for all three lines
71 for (int j = 0; j < 2; j++) { // for both endpoints
72 OrderMap.insert(pair<int, class BoundaryPointSet *> (line[i]->endpoints[j]->Nr, line[i]->endpoints[j]));
73 // and we don't care whether insertion fails
74 }
75 }
76 // set endpoints
77 int Counter = 0;
78 DoLog(0) && (Log() << Verbose(0) << "New triangle " << Nr << " with end points: " << endl);
79 for (PointMap::iterator runner = OrderMap.begin(); runner != OrderMap.end(); runner++) {
80 endpoints[Counter] = runner->second;
81 DoLog(0) && (Log() << Verbose(0) << " " << *endpoints[Counter] << endl);
82 Counter++;
83 }
84 ASSERT(Counter >= 3,"We have a triangle with only two distinct endpoints!");
85};
86
87
88/** Destructor of BoundaryTriangleSet.
89 * Removes itself from each of its lines' LineMap and removes them if necessary.
90 * \note When removing triangles from a class Tesselation, use RemoveTesselationTriangle()
91 */
92BoundaryTriangleSet::~BoundaryTriangleSet()
93{
94 Info FunctionInfo(__func__);
95 for (int i = 0; i < 3; i++) {
96 if (lines[i] != NULL) {
97 if (lines[i]->triangles.erase(Nr)) {
98 //Log() << Verbose(0) << "Triangle Nr." << Nr << " erased in line " << *lines[i] << "." << endl;
99 }
100 if (lines[i]->triangles.empty()) {
101 //Log() << Verbose(0) << *lines[i] << " is no more attached to any triangle, erasing." << endl;
102 delete (lines[i]);
103 lines[i] = NULL;
104 }
105 }
106 }
107 //Log() << Verbose(0) << "Erasing triangle Nr." << Nr << " itself." << endl;
108}
109;
110
111/** Calculates the normal vector for this triangle.
112 * Is made unique by comparison with \a OtherVector to point in the other direction.
113 * \param &OtherVector direction vector to make normal vector unique.
114 */
115void BoundaryTriangleSet::GetNormalVector(const Vector &OtherVector)
116{
117 Info FunctionInfo(__func__);
118 // get normal vector
119 NormalVector = Plane((endpoints[0]->node->getPosition()),
120 (endpoints[1]->node->getPosition()),
121 (endpoints[2]->node->getPosition())).getNormal();
122
123 // make it always point inward (any offset vector onto plane projected onto normal vector suffices)
124 if (NormalVector.ScalarProduct(OtherVector) > 0.)
125 NormalVector.Scale(-1.);
126 DoLog(1) && (Log() << Verbose(1) << "Normal Vector is " << NormalVector << "." << endl);
127}
128;
129
130/** Finds the point on the triangle \a *BTS through which the line defined by \a *MolCenter and \a *x crosses.
131 * We call Vector::GetIntersectionWithPlane() to receive the intersection point with the plane
132 * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
133 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
134 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
135 * the first two basepoints) or not.
136 * \param *out output stream for debugging
137 * \param &MolCenter offset vector of line
138 * \param &x second endpoint of line, minus \a *MolCenter is directional vector of line
139 * \param &Intersection intersection on plane on return
140 * \return true - \a *Intersection contains intersection on plane defined by triangle, false - zero vector if outside of triangle.
141 */
142
143bool BoundaryTriangleSet::GetIntersectionInsideTriangle(const Vector & MolCenter, const Vector & x, Vector &Intersection) const
144{
145 Info FunctionInfo(__func__);
146 Vector CrossPoint;
147 Vector helper;
148
149 try {
150 Line centerLine = makeLineThrough(MolCenter, x);
151 Intersection = Plane(NormalVector, (endpoints[0]->node->getPosition())).GetIntersection(centerLine);
152
153 DoLog(1) && (Log() << Verbose(1) << "INFO: Triangle is " << *this << "." << endl);
154 DoLog(1) && (Log() << Verbose(1) << "INFO: Line is from " << MolCenter << " to " << x << "." << endl);
155 DoLog(1) && (Log() << Verbose(1) << "INFO: Intersection is " << Intersection << "." << endl);
156
157 if (Intersection.DistanceSquared(endpoints[0]->node->getPosition()) < MYEPSILON) {
158 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with first endpoint." << endl);
159 return true;
160 } else if (Intersection.DistanceSquared(endpoints[1]->node->getPosition()) < MYEPSILON) {
161 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with second endpoint." << endl);
162 return true;
163 } else if (Intersection.DistanceSquared(endpoints[2]->node->getPosition()) < MYEPSILON) {
164 DoLog(1) && (Log() << Verbose(1) << "Intersection coindices with third endpoint." << endl);
165 return true;
166 }
167 // Calculate cross point between one baseline and the line from the third endpoint to intersection
168 int i = 0;
169 do {
170 Line line1 = makeLineThrough((endpoints[i%3]->node->getPosition()),(endpoints[(i+1)%3]->node->getPosition()));
171 Line line2 = makeLineThrough((endpoints[(i+2)%3]->node->getPosition()),Intersection);
172 CrossPoint = line1.getIntersection(line2);
173 helper = (endpoints[(i+1)%3]->node->getPosition()) - (endpoints[i%3]->node->getPosition());
174 CrossPoint -= (endpoints[i%3]->node->getPosition()); // cross point was returned as absolute vector
175 const double s = CrossPoint.ScalarProduct(helper)/helper.NormSquared();
176 DoLog(1) && (Log() << Verbose(1) << "INFO: Factor s is " << s << "." << endl);
177 if ((s < -MYEPSILON) || ((s-1.) > MYEPSILON)) {
178 DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << "outside of triangle." << endl);
179 return false;
180 }
181 i++;
182 } while (i < 3);
183 DoLog(1) && (Log() << Verbose(1) << "INFO: Crosspoint " << CrossPoint << " inside of triangle." << endl);
184 return true;
185 }
186 catch (MathException &excp) {
187 Log() << Verbose(1) << excp;
188 DoeLog(1) && (eLog() << Verbose(1) << "Alas! Intersection with plane failed - at least numerically - the intersection is not on the plane!" << endl);
189 return false;
190 }
191}
192;
193
194/** Finds the point on the triangle to the point \a *x.
195 * We call Vector::GetIntersectionWithPlane() with \a * and the center of the triangle to receive an intersection point.
196 * Then we check the in-plane part (the part projected down onto plane). We check whether it crosses one of the
197 * boundary lines. If it does, we return this intersection as closest point, otherwise the projected point down.
198 * Thus we test if it's really on the plane and whether it's inside the triangle on the plane or not.
199 * The latter is done as follows: We calculate the cross point of one of the triangle's baseline with the line
200 * given by the intersection and the third basepoint. Then, we check whether it's on the baseline (i.e. between
201 * the first two basepoints) or not.
202 * \param *x point
203 * \param *ClosestPoint desired closest point inside triangle to \a *x, is absolute vector
204 * \return Distance squared between \a *x and closest point inside triangle
205 */
206double BoundaryTriangleSet::GetClosestPointInsideTriangle(const Vector &x, Vector &ClosestPoint) const
207{
208 Info FunctionInfo(__func__);
209 Vector Direction;
210
211 // 1. get intersection with plane
212 DoLog(1) && (Log() << Verbose(1) << "INFO: Looking for closest point of triangle " << *this << " to " << x << "." << endl);
213 GetCenter(Direction);
214 try {
215 Line l = makeLineThrough(x, Direction);
216 ClosestPoint = Plane(NormalVector, (endpoints[0]->node->getPosition())).GetIntersection(l);
217 }
218 catch (MathException &excp) {
219 (ClosestPoint) = (x);
220 }
221
222 // 2. Calculate in plane part of line (x, intersection)
223 Vector InPlane = (x) - (ClosestPoint); // points from plane intersection to straight-down point
224 InPlane.ProjectOntoPlane(NormalVector);
225 InPlane += ClosestPoint;
226
227 DoLog(2) && (Log() << Verbose(2) << "INFO: Triangle is " << *this << "." << endl);
228 DoLog(2) && (Log() << Verbose(2) << "INFO: Line is from " << Direction << " to " << x << "." << endl);
229 DoLog(2) && (Log() << Verbose(2) << "INFO: In-plane part is " << InPlane << "." << endl);
230
231 // Calculate cross point between one baseline and the desired point such that distance is shortest
232 double ShortestDistance = -1.;
233 bool InsideFlag = false;
234 Vector CrossDirection[3];
235 Vector CrossPoint[3];
236 Vector helper;
237 for (int i = 0; i < 3; i++) {
238 // treat direction of line as normal of a (cut)plane and the desired point x as the plane offset, the intersect line with point
239 Direction = (endpoints[(i+1)%3]->node->getPosition()) - (endpoints[i%3]->node->getPosition());
240 // calculate intersection, line can never be parallel to Direction (is the same vector as PlaneNormal);
241 Line l = makeLineThrough((endpoints[i%3]->node->getPosition()), (endpoints[(i+1)%3]->node->getPosition()));
242 CrossPoint[i] = Plane(Direction, InPlane).GetIntersection(l);
243 CrossDirection[i] = CrossPoint[i] - InPlane;
244 CrossPoint[i] -= (endpoints[i%3]->node->getPosition()); // cross point was returned as absolute vector
245 const double s = CrossPoint[i].ScalarProduct(Direction)/Direction.NormSquared();
246 DoLog(2) && (Log() << Verbose(2) << "INFO: Factor s is " << s << "." << endl);
247 if ((s >= -MYEPSILON) && ((s-1.) <= MYEPSILON)) {
248 CrossPoint[i] += (endpoints[i%3]->node->getPosition()); // make cross point absolute again
249 DoLog(2) && (Log() << Verbose(2) << "INFO: Crosspoint is " << CrossPoint[i] << ", intersecting BoundaryLine between " << endpoints[i % 3]->node->getPosition() << " and " << endpoints[(i + 1) % 3]->node->getPosition() << "." << endl);
250 const double distance = CrossPoint[i].DistanceSquared(x);
251 if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
252 ShortestDistance = distance;
253 (ClosestPoint) = CrossPoint[i];
254 }
255 } else
256 CrossPoint[i].Zero();
257 }
258 InsideFlag = true;
259 for (int i = 0; i < 3; i++) {
260 const double sign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 1) % 3]);
261 const double othersign = CrossDirection[i].ScalarProduct(CrossDirection[(i + 2) % 3]);
262
263 if ((sign > -MYEPSILON) && (othersign > -MYEPSILON)) // have different sign
264 InsideFlag = false;
265 }
266 if (InsideFlag) {
267 (ClosestPoint) = InPlane;
268 ShortestDistance = InPlane.DistanceSquared(x);
269 } else { // also check endnodes
270 for (int i = 0; i < 3; i++) {
271 const double distance = x.DistanceSquared(endpoints[i]->node->getPosition());
272 if ((ShortestDistance < 0.) || (ShortestDistance > distance)) {
273 ShortestDistance = distance;
274 (ClosestPoint) = (endpoints[i]->node->getPosition());
275 }
276 }
277 }
278 DoLog(1) && (Log() << Verbose(1) << "INFO: Closest Point is " << ClosestPoint << " with shortest squared distance is " << ShortestDistance << "." << endl);
279 return ShortestDistance;
280}
281;
282
283/** Checks whether lines is any of the three boundary lines this triangle contains.
284 * \param *line line to test
285 * \return true - line is of the triangle, false - is not
286 */
287bool BoundaryTriangleSet::ContainsBoundaryLine(const BoundaryLineSet * const line) const
288{
289 Info FunctionInfo(__func__);
290 for (int i = 0; i < 3; i++)
291 if (line == lines[i])
292 return true;
293 return false;
294}
295;
296
297/** Checks whether point is any of the three endpoints this triangle contains.
298 * \param *point point to test
299 * \return true - point is of the triangle, false - is not
300 */
301bool BoundaryTriangleSet::ContainsBoundaryPoint(const BoundaryPointSet * const point) const
302{
303 Info FunctionInfo(__func__);
304 for (int i = 0; i < 3; i++)
305 if (point == endpoints[i])
306 return true;
307 return false;
308}
309;
310
311/** Checks whether point is any of the three endpoints this triangle contains.
312 * \param *point TesselPoint to test
313 * \return true - point is of the triangle, false - is not
314 */
315bool BoundaryTriangleSet::ContainsBoundaryPoint(const TesselPoint * const point) const
316{
317 Info FunctionInfo(__func__);
318 for (int i = 0; i < 3; i++)
319 if (point == endpoints[i]->node)
320 return true;
321 return false;
322}
323;
324
325/** Checks whether three given \a *Points coincide with triangle's endpoints.
326 * \param *Points[3] pointer to BoundaryPointSet
327 * \return true - is the very triangle, false - is not
328 */
329bool BoundaryTriangleSet::IsPresentTupel(const BoundaryPointSet * const Points[3]) const
330{
331 Info FunctionInfo(__func__);
332 DoLog(1) && (Log() << Verbose(1) << "INFO: Checking " << Points[0] << "," << Points[1] << "," << Points[2] << " against " << endpoints[0] << "," << endpoints[1] << "," << endpoints[2] << "." << endl);
333 return (((endpoints[0] == Points[0]) || (endpoints[0] == Points[1]) || (endpoints[0] == Points[2])) && ((endpoints[1] == Points[0]) || (endpoints[1] == Points[1]) || (endpoints[1] == Points[2])) && ((endpoints[2] == Points[0]) || (endpoints[2] == Points[1]) || (endpoints[2] == Points[2])
334
335 ));
336}
337;
338
339/** Checks whether three given \a *Points coincide with triangle's endpoints.
340 * \param *Points[3] pointer to BoundaryPointSet
341 * \return true - is the very triangle, false - is not
342 */
343bool BoundaryTriangleSet::IsPresentTupel(const BoundaryTriangleSet * const T) const
344{
345 Info FunctionInfo(__func__);
346 return (((endpoints[0] == T->endpoints[0]) || (endpoints[0] == T->endpoints[1]) || (endpoints[0] == T->endpoints[2])) && ((endpoints[1] == T->endpoints[0]) || (endpoints[1] == T->endpoints[1]) || (endpoints[1] == T->endpoints[2])) && ((endpoints[2] == T->endpoints[0]) || (endpoints[2] == T->endpoints[1]) || (endpoints[2] == T->endpoints[2])
347
348 ));
349}
350;
351
352/** Returns the endpoint which is not contained in the given \a *line.
353 * \param *line baseline defining two endpoints
354 * \return pointer third endpoint or NULL if line does not belong to triangle.
355 */
356class BoundaryPointSet *BoundaryTriangleSet::GetThirdEndpoint(const BoundaryLineSet * const line) const
357{
358 Info FunctionInfo(__func__);
359 // sanity check
360 if (!ContainsBoundaryLine(line))
361 return NULL;
362 for (int i = 0; i < 3; i++)
363 if (!line->ContainsBoundaryPoint(endpoints[i]))
364 return endpoints[i];
365 // actually, that' impossible :)
366 return NULL;
367}
368;
369
370/** Returns the baseline which does not contain the given boundary point \a *point.
371 * \param *point endpoint which is neither endpoint of the desired line
372 * \return pointer to desired third baseline
373 */
374class BoundaryLineSet *BoundaryTriangleSet::GetThirdLine(const BoundaryPointSet * const point) const
375{
376 Info FunctionInfo(__func__);
377 // sanity check
378 if (!ContainsBoundaryPoint(point))
379 return NULL;
380 for (int i = 0; i < 3; i++)
381 if (!lines[i]->ContainsBoundaryPoint(point))
382 return lines[i];
383 // actually, that' impossible :)
384 return NULL;
385}
386;
387
388/** Calculates the center point of the triangle.
389 * Is third of the sum of all endpoints.
390 * \param *center central point on return.
391 */
392void BoundaryTriangleSet::GetCenter(Vector & center) const
393{
394 Info FunctionInfo(__func__);
395 center.Zero();
396 for (int i = 0; i < 3; i++)
397 (center) += (endpoints[i]->node->getPosition());
398 center.Scale(1. / 3.);
399 DoLog(1) && (Log() << Verbose(1) << "INFO: Center is at " << center << "." << endl);
400}
401
402/**
403 * gets the Plane defined by the three triangle Basepoints
404 */
405Plane BoundaryTriangleSet::getPlane() const{
406 ASSERT(endpoints[0] && endpoints[1] && endpoints[2], "Triangle not fully defined");
407
408 return Plane(endpoints[0]->node->getPosition(),
409 endpoints[1]->node->getPosition(),
410 endpoints[2]->node->getPosition());
411}
412
413Vector BoundaryTriangleSet::getEndpoint(int i) const{
414 ASSERT(i>=0 && i<3,"Index of Endpoint out of Range");
415
416 return endpoints[i]->node->getPosition();
417}
418
419string BoundaryTriangleSet::getEndpointName(int i) const{
420 ASSERT(i>=0 && i<3,"Index of Endpoint out of Range");
421
422 return endpoints[i]->node->getName();
423}
424
425/** output operator for BoundaryTriangleSet.
426 * \param &ost output stream
427 * \param &a boundary triangle
428 */
429ostream &operator <<(ostream &ost, const BoundaryTriangleSet &a)
430{
431 ost << "[" << a.Nr << "|" << a.getEndpointName(0) << "," << a.getEndpointName(1) << "," << a.getEndpointName(2) << "]";
432 // ost << "[" << a.Nr << "|" << a.endpoints[0]->node->Name << " at " << *a.endpoints[0]->node->node << ","
433 // << a.endpoints[1]->node->Name << " at " << *a.endpoints[1]->node->node << "," << a.endpoints[2]->node->Name << " at " << *a.endpoints[2]->node->node << "]";
434 return ost;
435}
436;
437
Note: See TracBrowser for help on using the repository browser.