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