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