source: src/Plane.cpp@ 79dd0e

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

Added a method to check whether two points lie on the same side of a plane

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