source: src/Plane.cpp@ 3cdd16

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 3cdd16 was 3cdd16, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added more Exceptions to the project to signal differen Math erorrs

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Plane.cpp
3 *
4 * Created on: Apr 7, 2010
5 * Author: crueger
6 */
7
8#include "Plane.hpp"
9#include "vector.hpp"
10#include "defs.hpp"
11#include "Exceptions/LinearDependenceException.hpp"
12#include "info.hpp"
13#include "log.hpp"
14#include "verbose.hpp"
15#include "Helpers/Assert.hpp"
16#include <cmath>
17
18/**
19 * generates a plane from three given vectors defining three points in space
20 */
21Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) :
22 normalVector(new Vector())
23{
24 Vector x1 = y1 -y2;
25 Vector x2 = y3 -y2;
26 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
27 throw LinearDependenceException(__FILE__,__LINE__);
28 }
29// Log() << Verbose(4) << "relative, first plane coordinates:";
30// x1.Output((ofstream *)&cout);
31// Log() << Verbose(0) << endl;
32// Log() << Verbose(4) << "second plane coordinates:";
33// x2.Output((ofstream *)&cout);
34// Log() << Verbose(0) << endl;
35
36 normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
37 normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
38 normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
39 normalVector->Normalize();
40
41 offset=normalVector->ScalarProduct(y1);
42}
43/**
44 * Constructs a plane from two vectors and a offset.
45 * If no offset is given a plane through origin is assumed
46 */
47Plane::Plane(const Vector &y1, const Vector &y2, double _offset):
48 normalVector(new Vector()),
49 offset(_offset)
50{
51 Vector x1 = y1;
52 Vector x2 = y2;
53 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
54 throw LinearDependenceException(__FILE__,__LINE__);
55 }
56// Log() << Verbose(4) << "relative, first plane coordinates:";
57// x1.Output((ofstream *)&cout);
58// Log() << Verbose(0) << endl;
59// Log() << Verbose(4) << "second plane coordinates:";
60// x2.Output((ofstream *)&cout);
61// Log() << Verbose(0) << endl;
62
63 normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
64 normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
65 normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
66 normalVector->Normalize();
67}
68
69Plane::Plane(const Vector &_normalVector, double _offset) :
70 normalVector(new Vector(_normalVector)),
71 offset(_offset)
72{
73 ASSERT(normalVector->Norm()>MYEPSILON,"Normalvector was zero when constructing a plane.");
74 double factor = 1/normalVector->Norm();
75 // normalize the plane parameters
76 (*normalVector)*=factor;
77 offset*=factor;
78}
79
80Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) :
81 normalVector(new Vector(_normalVector))
82{
83 normalVector->Normalize();
84 offset = normalVector->ScalarProduct(_offsetVector);
85}
86
87Plane::~Plane()
88{}
89
90
91Vector Plane::getNormal(){
92 return *normalVector;
93}
94
95double Plane::getOffset(){
96 return offset;
97}
98
99Vector Plane::getOffsetVector() {
100 return getOffset()*getNormal();
101}
102
103vector<Vector> Plane::getPointsOnPlane(){
104 std::vector<Vector> res;
105 // first point on the plane
106 res[0] = getOffsetVector();
107 // first is orthogonal to the plane...
108 // an orthogonal vector to this one lies on the plane
109 Vector direction;
110 direction.GetOneNormalVector(res[0]);
111 res[1] = res[0]+direction;
112 // get an orthogonal vector to direction and offset (lies on the plane)
113 direction.VectorProduct(res[0]);
114 direction.Normalize();
115 res[2] = res[0] +direction;
116 return res;
117}
118
119
120/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
121 * According to [Bronstein] the vectorial plane equation is:
122 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
123 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
124 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
125 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
126 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
127 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
128 * of the line yields the intersection point on the plane.
129 * \param *Origin first vector of line
130 * \param *LineVector second vector of line
131 * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
132 */
133Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
134{
135 Info FunctionInfo(__func__);
136 Vector res;
137
138 // find intersection of a line defined by Offset and Direction with a plane defined by triangle
139 Vector Direction = LineVector - Origin;
140 Direction.Normalize();
141 Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
142 //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl;
143 double factor1 = Direction.ScalarProduct(*normalVector.get());
144 if (fabs(factor1) < MYEPSILON) { // Uniqueness: line parallel to plane?
145 Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;
146 throw LinearDependenceException(__FILE__,__LINE__);
147 }
148
149 double factor2 = Origin.ScalarProduct(*normalVector.get());
150 if (fabs(factor2-offset) < MYEPSILON) { // Origin is in-plane
151 Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;
152 res = Origin;
153 return res;
154 }
155
156 double scaleFactor = (offset-factor2)/factor1;
157
158 //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
159 Direction.Scale(scaleFactor);
160 res = Origin + Direction;
161 Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
162
163 // test whether resulting vector really is on plane
164 ASSERT(fabs(res.ScalarProduct((*normalVector.get())) - offset) < MYEPSILON,
165 "Calculated line-Plane intersection does not lie on plane.");
166 return res;
167};
168
169/************ Methods inherited from Space ****************/
170
171double Plane::distance(Vector &point){
172 double res = point.ScalarProduct(*normalVector)-offset;
173 return fabs(res);
174}
175
176Vector Plane::getClosestPoint(Vector &point){
177 Vector difference = distance(point) * (*normalVector);
178 if(difference.IsZero()){
179 // the point itself lies on the plane
180 return point;
181 }
182 // get the direction this vector is pointing
183 double sign = difference.ScalarProduct(*normalVector);
184 // sign cannot be zero, since normalVector and difference are both != zero
185 sign = sign/fabs(sign);
186 return (point - (sign * difference));
187}
188
189bool Plane::isContained(Vector &point){
190 return (point.ScalarProduct(*normalVector) - offset) < MYEPSILON;
191}
Note: See TracBrowser for help on using the repository browser.