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