source: src/LinearAlgebra/Plane.cpp@ 8b9c43

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 Candidate_v1.7.0 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 8b9c43 was 8b9c43, checked in by Frederik Heber <heber@…>, 15 years ago

Removed Exceptions/ZeroVectorException.

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