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