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