| 1 | /* | 
|---|
| 2 | * Plane.cpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Apr 7, 2010 | 
|---|
| 5 | *      Author: crueger | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include "Helpers/MemDebug.hpp" | 
|---|
| 9 |  | 
|---|
| 10 | #include "Plane.hpp" | 
|---|
| 11 | #include "vector.hpp" | 
|---|
| 12 | #include "defs.hpp" | 
|---|
| 13 | #include "info.hpp" | 
|---|
| 14 | #include "log.hpp" | 
|---|
| 15 | #include "verbose.hpp" | 
|---|
| 16 | #include "Helpers/Assert.hpp" | 
|---|
| 17 | #include <cmath> | 
|---|
| 18 | #include "Line.hpp" | 
|---|
| 19 | #include "Exceptions/MultipleSolutionsException.hpp" | 
|---|
| 20 |  | 
|---|
| 21 | /** | 
|---|
| 22 | * generates a plane from three given vectors defining three points in space | 
|---|
| 23 | */ | 
|---|
| 24 | Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) throw(LinearDependenceException) : | 
|---|
| 25 | normalVector(new Vector()) | 
|---|
| 26 | { | 
|---|
| 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)) { | 
|---|
| 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 |  | 
|---|
| 44 | offset=normalVector->ScalarProduct(y1); | 
|---|
| 45 | } | 
|---|
| 46 | /** | 
|---|
| 47 | * Constructs a plane from two direction vectors and a offset. | 
|---|
| 48 | */ | 
|---|
| 49 | Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) : | 
|---|
| 50 | normalVector(new Vector()), | 
|---|
| 51 | offset(_offset) | 
|---|
| 52 | { | 
|---|
| 53 | Vector x1 = y1; | 
|---|
| 54 | Vector x2 = y2; | 
|---|
| 55 | if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON)) { | 
|---|
| 56 | throw ZeroVectorException(__FILE__,__LINE__); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | if((fabs(x1.Angle(x2)) < MYEPSILON)) { | 
|---|
| 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 |  | 
|---|
| 75 | Plane::Plane(const Vector &_normalVector, double _offset) throw(ZeroVectorException): | 
|---|
| 76 | normalVector(new Vector(_normalVector)), | 
|---|
| 77 | offset(_offset) | 
|---|
| 78 | { | 
|---|
| 79 | if(normalVector->IsZero()) | 
|---|
| 80 | throw ZeroVectorException(__FILE__,__LINE__); | 
|---|
| 81 | double factor = 1/normalVector->Norm(); | 
|---|
| 82 | // normalize the plane parameters | 
|---|
| 83 | (*normalVector)*=factor; | 
|---|
| 84 | offset*=factor; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) throw(ZeroVectorException): | 
|---|
| 88 | normalVector(new Vector(_normalVector)) | 
|---|
| 89 | { | 
|---|
| 90 | if(normalVector->IsZero()){ | 
|---|
| 91 | throw ZeroVectorException(__FILE__,__LINE__); | 
|---|
| 92 | } | 
|---|
| 93 | normalVector->Normalize(); | 
|---|
| 94 | offset = normalVector->ScalarProduct(_offsetVector); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /** | 
|---|
| 98 | * copy constructor | 
|---|
| 99 | */ | 
|---|
| 100 | Plane::Plane(const Plane& plane) : | 
|---|
| 101 | normalVector(new Vector(*plane.normalVector)), | 
|---|
| 102 | offset(plane.offset) | 
|---|
| 103 | {} | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 | Plane::~Plane() | 
|---|
| 107 | {} | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | Vector Plane::getNormal() const{ | 
|---|
| 111 | return *normalVector; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | double Plane::getOffset() const{ | 
|---|
| 115 | return offset; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | Vector Plane::getOffsetVector() const { | 
|---|
| 119 | return getOffset()*getNormal(); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | vector<Vector> Plane::getPointsOnPlane() const{ | 
|---|
| 123 | std::vector<Vector> res; | 
|---|
| 124 | res.reserve(3); | 
|---|
| 125 | // first point on the plane | 
|---|
| 126 | res.push_back(getOffsetVector()); | 
|---|
| 127 | // get a vector that has direction of plane | 
|---|
| 128 | Vector direction; | 
|---|
| 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()); | 
|---|
| 133 | direction.Normalize(); | 
|---|
| 134 | res.push_back(res[0] +direction); | 
|---|
| 135 | return res; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 |  | 
|---|
| 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 | */ | 
|---|
| 152 | Vector Plane::GetIntersection(const Line& line) const | 
|---|
| 153 | { | 
|---|
| 154 | Info FunctionInfo(__func__); | 
|---|
| 155 | Vector res; | 
|---|
| 156 |  | 
|---|
| 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 | } | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | double factor2 = getNormal().ScalarProduct(line.getOrigin()); | 
|---|
| 170 | double scaleFactor = (offset-factor2)/factor1; | 
|---|
| 171 |  | 
|---|
| 172 | res = line.getOrigin() + scaleFactor * line.getDirection(); | 
|---|
| 173 |  | 
|---|
| 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."); | 
|---|
| 177 | return res; | 
|---|
| 178 | }; | 
|---|
| 179 |  | 
|---|
| 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 |  | 
|---|
| 186 | Line Plane::getOrthogonalLine(const Vector &origin) const{ | 
|---|
| 187 | return Line(origin,getNormal()); | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | /************ Methods inherited from Space ****************/ | 
|---|
| 191 |  | 
|---|
| 192 | double Plane::distance(const Vector &point) const{ | 
|---|
| 193 | double res = point.ScalarProduct(*normalVector)-offset; | 
|---|
| 194 | return fabs(res); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | Vector Plane::getClosestPoint(const Vector &point) const{ | 
|---|
| 198 | double factor = point.ScalarProduct(*normalVector)-offset; | 
|---|
| 199 | if(fabs(factor) < MYEPSILON){ | 
|---|
| 200 | // the point itself lies on the plane | 
|---|
| 201 | return point; | 
|---|
| 202 | } | 
|---|
| 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; | 
|---|
| 212 | } | 
|---|