| 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 |
|
|---|
| 17 | /**
|
|---|
| 18 | * generates a plane from three given vectors defining three points in space
|
|---|
| 19 | */
|
|---|
| 20 | Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) throw(LinearDependenceException) :
|
|---|
| 21 | normalVector(new Vector())
|
|---|
| 22 | {
|
|---|
| 23 | Vector x1 = y1 -y2;
|
|---|
| 24 | Vector x2 = y3 -y2;
|
|---|
| 25 | if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
|
|---|
| 26 | throw LinearDependenceException(__FILE__,__LINE__);
|
|---|
| 27 | }
|
|---|
| 28 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
|---|
| 29 | // x1.Output((ofstream *)&cout);
|
|---|
| 30 | // Log() << Verbose(0) << endl;
|
|---|
| 31 | // Log() << Verbose(4) << "second plane coordinates:";
|
|---|
| 32 | // x2.Output((ofstream *)&cout);
|
|---|
| 33 | // Log() << Verbose(0) << endl;
|
|---|
| 34 |
|
|---|
| 35 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
|---|
| 36 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
|---|
| 37 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
|---|
| 38 | normalVector->Normalize();
|
|---|
| 39 |
|
|---|
| 40 | offset=normalVector->ScalarProduct(y1);
|
|---|
| 41 | }
|
|---|
| 42 | /**
|
|---|
| 43 | * Constructs a plane from two direction vectors and a offset.
|
|---|
| 44 | * If no offset is given a plane through origin is assumed
|
|---|
| 45 | */
|
|---|
| 46 | Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(LinearDependenceException) :
|
|---|
| 47 | normalVector(new Vector()),
|
|---|
| 48 | offset(_offset)
|
|---|
| 49 | {
|
|---|
| 50 | Vector x1 = y1;
|
|---|
| 51 | Vector x2 = y2;
|
|---|
| 52 | if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(x2)) < MYEPSILON)) {
|
|---|
| 53 | throw LinearDependenceException(__FILE__,__LINE__);
|
|---|
| 54 | }
|
|---|
| 55 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
|---|
| 56 | // x1.Output((ofstream *)&cout);
|
|---|
| 57 | // Log() << Verbose(0) << endl;
|
|---|
| 58 | // Log() << Verbose(4) << "second plane coordinates:";
|
|---|
| 59 | // x2.Output((ofstream *)&cout);
|
|---|
| 60 | // Log() << Verbose(0) << endl;
|
|---|
| 61 |
|
|---|
| 62 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
|---|
| 63 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
|---|
| 64 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
|---|
| 65 | normalVector->Normalize();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | Plane::Plane(const Vector &_normalVector, double _offset) throw(ZeroVectorException):
|
|---|
| 69 | normalVector(new Vector(_normalVector)),
|
|---|
| 70 | offset(_offset)
|
|---|
| 71 | {
|
|---|
| 72 | if(normalVector->IsZero())
|
|---|
| 73 | throw ZeroVectorException(__FILE__,__LINE__);
|
|---|
| 74 | double factor = 1/normalVector->Norm();
|
|---|
| 75 | // normalize the plane parameters
|
|---|
| 76 | (*normalVector)*=factor;
|
|---|
| 77 | offset*=factor;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) throw(ZeroVectorException):
|
|---|
| 81 | normalVector(new Vector(_normalVector))
|
|---|
| 82 | {
|
|---|
| 83 | if(normalVector->IsZero()){
|
|---|
| 84 | throw ZeroVectorException(__FILE__,__LINE__);
|
|---|
| 85 | }
|
|---|
| 86 | normalVector->Normalize();
|
|---|
| 87 | offset = normalVector->ScalarProduct(_offsetVector);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | Plane::~Plane()
|
|---|
| 91 | {}
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 | Vector Plane::getNormal(){
|
|---|
| 95 | return *normalVector;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | double Plane::getOffset(){
|
|---|
| 99 | return offset;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | Vector Plane::getOffsetVector() {
|
|---|
| 103 | return getOffset()*getNormal();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | vector<Vector> Plane::getPointsOnPlane(){
|
|---|
| 107 | std::vector<Vector> res;
|
|---|
| 108 | // first point on the plane
|
|---|
| 109 | res[0] = getOffsetVector();
|
|---|
| 110 | // first is orthogonal to the plane...
|
|---|
| 111 | // an orthogonal vector to this one lies on the plane
|
|---|
| 112 | Vector direction;
|
|---|
| 113 | direction.GetOneNormalVector(res[0]);
|
|---|
| 114 | res[1] = res[0]+direction;
|
|---|
| 115 | // get an orthogonal vector to direction and offset (lies on the plane)
|
|---|
| 116 | direction.VectorProduct(res[0]);
|
|---|
| 117 | direction.Normalize();
|
|---|
| 118 | res[2] = res[0] +direction;
|
|---|
| 119 | return res;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | /** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
|
|---|
| 124 | * According to [Bronstein] the vectorial plane equation is:
|
|---|
| 125 | * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
|
|---|
| 126 | * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
|
|---|
| 127 | * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
|
|---|
| 128 | * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
|
|---|
| 129 | * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
|
|---|
| 130 | * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
|
|---|
| 131 | * of the line yields the intersection point on the plane.
|
|---|
| 132 | * \param *Origin first vector of line
|
|---|
| 133 | * \param *LineVector second vector of line
|
|---|
| 134 | * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
|
|---|
| 135 | */
|
|---|
| 136 | Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
|
|---|
| 137 | {
|
|---|
| 138 | Info FunctionInfo(__func__);
|
|---|
| 139 | Vector res;
|
|---|
| 140 |
|
|---|
| 141 | // find intersection of a line defined by Offset and Direction with a plane defined by triangle
|
|---|
| 142 | Vector Direction = LineVector - Origin;
|
|---|
| 143 | Direction.Normalize();
|
|---|
| 144 | Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
|
|---|
| 145 | //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl;
|
|---|
| 146 | double factor1 = Direction.ScalarProduct(*normalVector.get());
|
|---|
| 147 | if (fabs(factor1) < MYEPSILON) { // Uniqueness: line parallel to plane?
|
|---|
| 148 | Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;
|
|---|
| 149 | throw LinearDependenceException(__FILE__,__LINE__);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | double factor2 = Origin.ScalarProduct(*normalVector.get());
|
|---|
| 153 | if (fabs(factor2-offset) < MYEPSILON) { // Origin is in-plane
|
|---|
| 154 | Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;
|
|---|
| 155 | res = Origin;
|
|---|
| 156 | return res;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | double scaleFactor = (offset-factor2)/factor1;
|
|---|
| 160 |
|
|---|
| 161 | //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
|
|---|
| 162 | Direction.Scale(scaleFactor);
|
|---|
| 163 | res = Origin + Direction;
|
|---|
| 164 | Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
|
|---|
| 165 |
|
|---|
| 166 | // test whether resulting vector really is on plane
|
|---|
| 167 | ASSERT(fabs(res.ScalarProduct(*normalVector) - offset) < MYEPSILON,
|
|---|
| 168 | "Calculated line-Plane intersection does not lie on plane.");
|
|---|
| 169 | return res;
|
|---|
| 170 | };
|
|---|
| 171 |
|
|---|
| 172 | /************ Methods inherited from Space ****************/
|
|---|
| 173 |
|
|---|
| 174 | double Plane::distance(const Vector &point) const{
|
|---|
| 175 | double res = point.ScalarProduct(*normalVector)-offset;
|
|---|
| 176 | return fabs(res);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | Vector Plane::getClosestPoint(const Vector &point) const{
|
|---|
| 180 | Vector difference = distance(point) * (*normalVector);
|
|---|
| 181 | if(difference.IsZero()){
|
|---|
| 182 | // the point itself lies on the plane
|
|---|
| 183 | return point;
|
|---|
| 184 | }
|
|---|
| 185 | // get the direction this vector is pointing
|
|---|
| 186 | double sign = difference.ScalarProduct(*normalVector);
|
|---|
| 187 | // sign cannot be zero, since normalVector and difference are both != zero
|
|---|
| 188 | sign = sign/fabs(sign);
|
|---|
| 189 | return (point - (sign * difference));
|
|---|
| 190 | }
|
|---|