source: src/BoundaryTriangleSet.cpp@ bbbad5

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

Added MemDebug.hpp to each and every .cpp file (were it was still missing).

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