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