source: src/BoundaryTriangleSet.cpp@ 2d292d

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 2d292d was 8f4df1, checked in by Frederik Heber <heber@…>, 14 years ago

Merge branch 'AtomicPositionEncapsulation' into stable

Conflicts:

src/Actions/AtomAction/ChangeElementAction.cpp
src/Actions/WorldAction/RemoveSphereOfAtomsAction.cpp
src/Makefile.am
src/UIElements/TextUI/TextDialog.cpp
src/analysis_correlation.hpp
src/atom.cpp
src/atom_atominfo.hpp
src/bond.cpp
src/boundary.cpp
src/molecule_geometry.cpp
src/tesselation.cpp
src/tesselationhelpers.cpp
src/triangleintersectionlist.cpp
src/unittests/Makefile.am

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