source: src/BoundaryTriangleSet.cpp@ bf3817

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

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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