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