source: src/Plane.cpp@ 986ed3

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 986ed3 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

  • 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 "Helpers/MemDebug.hpp"
9
10#include "Plane.hpp"
11#include "vector.hpp"
12#include "defs.hpp"
13#include "info.hpp"
14#include "log.hpp"
15#include "verbose.hpp"
16#include "Helpers/Assert.hpp"
17#include <cmath>
18#include "Line.hpp"
19#include "Exceptions/MultipleSolutionsException.hpp"
20
21/**
22 * generates a plane from three given vectors defining three points in space
23 */
24Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) throw(LinearDependenceException) :
25 normalVector(new Vector())
26{
27 Vector x1 = y1 -y2;
28 Vector x2 = y3 -y2;
29 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
30 throw LinearDependenceException(__FILE__,__LINE__);
31 }
32// Log() << Verbose(4) << "relative, first plane coordinates:";
33// x1.Output((ofstream *)&cout);
34// Log() << Verbose(0) << endl;
35// Log() << Verbose(4) << "second plane coordinates:";
36// x2.Output((ofstream *)&cout);
37// Log() << Verbose(0) << endl;
38
39 normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
40 normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
41 normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
42 normalVector->Normalize();
43
44 offset=normalVector->ScalarProduct(y1);
45}
46/**
47 * Constructs a plane from two direction vectors and a offset.
48 */
49Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) :
50 normalVector(new Vector()),
51 offset(_offset)
52{
53 Vector x1 = y1;
54 Vector x2 = y2;
55 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON)) {
56 throw ZeroVectorException(__FILE__,__LINE__);
57 }
58
59 if((fabs(x1.Angle(x2)) < MYEPSILON)) {
60 throw LinearDependenceException(__FILE__,__LINE__);
61 }
62// Log() << Verbose(4) << "relative, first plane coordinates:";
63// x1.Output((ofstream *)&cout);
64// Log() << Verbose(0) << endl;
65// Log() << Verbose(4) << "second plane coordinates:";
66// x2.Output((ofstream *)&cout);
67// Log() << Verbose(0) << endl;
68
69 normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
70 normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
71 normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
72 normalVector->Normalize();
73}
74
75Plane::Plane(const Vector &_normalVector, double _offset) throw(ZeroVectorException):
76 normalVector(new Vector(_normalVector)),
77 offset(_offset)
78{
79 if(normalVector->IsZero())
80 throw ZeroVectorException(__FILE__,__LINE__);
81 double factor = 1/normalVector->Norm();
82 // normalize the plane parameters
83 (*normalVector)*=factor;
84 offset*=factor;
85}
86
87Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) throw(ZeroVectorException):
88 normalVector(new Vector(_normalVector))
89{
90 if(normalVector->IsZero()){
91 throw ZeroVectorException(__FILE__,__LINE__);
92 }
93 normalVector->Normalize();
94 offset = normalVector->ScalarProduct(_offsetVector);
95}
96
97/**
98 * copy constructor
99 */
100Plane::Plane(const Plane& plane) :
101 normalVector(new Vector(*plane.normalVector)),
102 offset(plane.offset)
103{}
104
105
106Plane::~Plane()
107{}
108
109
110Vector Plane::getNormal() const{
111 return *normalVector;
112}
113
114double Plane::getOffset() const{
115 return offset;
116}
117
118Vector Plane::getOffsetVector() const {
119 return getOffset()*getNormal();
120}
121
122vector<Vector> Plane::getPointsOnPlane() const{
123 std::vector<Vector> res;
124 res.reserve(3);
125 // first point on the plane
126 res.push_back(getOffsetVector());
127 // get a vector that has direction of plane
128 Vector direction;
129 direction.GetOneNormalVector(getNormal());
130 res.push_back(res[0]+direction);
131 // get an orthogonal vector to direction and normal (has direction of plane)
132 direction.VectorProduct(getNormal());
133 direction.Normalize();
134 res.push_back(res[0] +direction);
135 return res;
136}
137
138
139/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
140 * According to [Bronstein] the vectorial plane equation is:
141 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
142 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
143 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
144 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
145 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
146 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
147 * of the line yields the intersection point on the plane.
148 * \param *Origin first vector of line
149 * \param *LineVector second vector of line
150 * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
151 */
152Vector Plane::GetIntersection(const Line& line) const
153{
154 Info FunctionInfo(__func__);
155 Vector res;
156
157 double factor1 = getNormal().ScalarProduct(line.getDirection());
158 if(fabs(factor1)<MYEPSILON){
159 // the plane is parallel... under all circumstances this is bad luck
160 // we no have either no or infinite solutions
161 if(isContained(line.getOrigin())){
162 throw MultipleSolutionsException<Vector>(__FILE__,__LINE__,line.getOrigin());
163 }
164 else{
165 throw LinearDependenceException(__FILE__,__LINE__);
166 }
167 }
168
169 double factor2 = getNormal().ScalarProduct(line.getOrigin());
170 double scaleFactor = (offset-factor2)/factor1;
171
172 res = line.getOrigin() + scaleFactor * line.getDirection();
173
174 // tests to make sure the resulting vector really is on plane and line
175 ASSERT(isContained(res),"Calculated line-Plane intersection does not lie on plane.");
176 ASSERT(line.isContained(res),"Calculated line-Plane intersection does not lie on line.");
177 return res;
178};
179
180Vector Plane::mirrorVector(const Vector &rhs) const {
181 Vector helper = getVectorToPoint(rhs);
182 // substract twice the Vector to the plane
183 return rhs+2*helper;
184}
185
186Line Plane::getOrthogonalLine(const Vector &origin) const{
187 return Line(origin,getNormal());
188}
189
190/************ Methods inherited from Space ****************/
191
192double Plane::distance(const Vector &point) const{
193 double res = point.ScalarProduct(*normalVector)-offset;
194 return fabs(res);
195}
196
197Vector Plane::getClosestPoint(const Vector &point) const{
198 double factor = point.ScalarProduct(*normalVector)-offset;
199 if(fabs(factor) < MYEPSILON){
200 // the point itself lies on the plane
201 return point;
202 }
203 Vector difference = factor * (*normalVector);
204 return (point - difference);
205}
206
207// Operators
208
209ostream &operator << (ostream &ost,const Plane &p){
210 ost << "<" << p.getNormal() << ";x> - " << p.getOffset() << "=0";
211 return ost;
212}
Note: See TracBrowser for help on using the repository browser.