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