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