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