[0a4f7f] | 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 "Exceptions/LinearDependenceException.hpp"
|
---|
| 11 | #include "info.hpp"
|
---|
| 12 | #include "log.hpp"
|
---|
| 13 | #include "verbose.hpp"
|
---|
| 14 | #include "Helpers/Assert.hpp"
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * generates a plane from three given vectors defining three points in space
|
---|
| 18 | */
|
---|
| 19 | Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) :
|
---|
| 20 | normalVector(new Vector())
|
---|
| 21 | {
|
---|
| 22 | Vector x1, x2;
|
---|
| 23 |
|
---|
| 24 | x1.CopyVector(&y1);
|
---|
| 25 | x1.SubtractVector(&y2);
|
---|
| 26 | x2.CopyVector(&y3);
|
---|
| 27 | x2.SubtractVector(&y2);
|
---|
| 28 | if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
|
---|
| 29 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 30 | }
|
---|
| 31 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
---|
| 32 | // x1.Output((ofstream *)&cout);
|
---|
| 33 | // Log() << Verbose(0) << endl;
|
---|
| 34 | // Log() << Verbose(4) << "second plane coordinates:";
|
---|
| 35 | // x2.Output((ofstream *)&cout);
|
---|
| 36 | // Log() << Verbose(0) << endl;
|
---|
| 37 |
|
---|
| 38 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
---|
| 39 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
---|
| 40 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
---|
| 41 | normalVector->Normalize();
|
---|
| 42 |
|
---|
| 43 | offset=normalVector->ScalarProduct(&y1);
|
---|
| 44 | }
|
---|
| 45 | /**
|
---|
| 46 | * Constructs a plane from two vectors and a offset.
|
---|
| 47 | * If no offset is given a plane through origin is assumed
|
---|
| 48 | */
|
---|
| 49 | Plane::Plane(const Vector &y1, const Vector &y2, double _offset):
|
---|
| 50 | normalVector(new Vector()),
|
---|
| 51 | offset(_offset)
|
---|
| 52 | {
|
---|
| 53 | Vector x1,x2;
|
---|
| 54 | x1.CopyVector(&y1);
|
---|
| 55 | x2.CopyVector(&y2);
|
---|
| 56 | if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
|
---|
| 57 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 58 | }
|
---|
| 59 | // Log() << Verbose(4) << "relative, first plane coordinates:";
|
---|
| 60 | // x1.Output((ofstream *)&cout);
|
---|
| 61 | // Log() << Verbose(0) << endl;
|
---|
| 62 | // Log() << Verbose(4) << "second plane coordinates:";
|
---|
| 63 | // x2.Output((ofstream *)&cout);
|
---|
| 64 | // Log() << Verbose(0) << endl;
|
---|
| 65 |
|
---|
| 66 | normalVector->at(0) = (x1[1]*x2[2] - x1[2]*x2[1]);
|
---|
| 67 | normalVector->at(1) = (x1[2]*x2[0] - x1[0]*x2[2]);
|
---|
| 68 | normalVector->at(2) = (x1[0]*x2[1] - x1[1]*x2[0]);
|
---|
| 69 | normalVector->Normalize();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | Plane::Plane(const Vector &_normalVector, double _offset) :
|
---|
| 73 | normalVector(new Vector(_normalVector)),
|
---|
| 74 | offset(_offset)
|
---|
| 75 | {}
|
---|
| 76 |
|
---|
| 77 | Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) :
|
---|
| 78 | normalVector(new Vector(_normalVector))
|
---|
| 79 | {
|
---|
| 80 | offset = normalVector->ScalarProduct(&_offsetVector);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | Plane::~Plane()
|
---|
| 84 | {}
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | Vector Plane::getNormal(){
|
---|
| 88 | return *normalVector;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | double Plane::getOffset(){
|
---|
| 92 | return offset;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
|
---|
| 96 | * According to [Bronstein] the vectorial plane equation is:
|
---|
| 97 | * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
|
---|
| 98 | * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
|
---|
| 99 | * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
|
---|
| 100 | * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
|
---|
| 101 | * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
|
---|
| 102 | * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
|
---|
| 103 | * of the line yields the intersection point on the plane.
|
---|
| 104 | * \param *Origin first vector of line
|
---|
| 105 | * \param *LineVector second vector of line
|
---|
| 106 | * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
|
---|
| 107 | */
|
---|
| 108 | Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
|
---|
| 109 | {
|
---|
| 110 | Info FunctionInfo(__func__);
|
---|
| 111 | Vector res;
|
---|
| 112 |
|
---|
| 113 | // find intersection of a line defined by Offset and Direction with a plane defined by triangle
|
---|
| 114 | Vector Direction = LineVector - Origin;
|
---|
| 115 | Direction.Normalize();
|
---|
| 116 | Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
|
---|
| 117 | //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl;
|
---|
| 118 | double factor1 = Direction.ScalarProduct(normalVector.get());
|
---|
| 119 | if (fabs(factor1) < MYEPSILON) { // Uniqueness: line parallel to plane?
|
---|
| 120 | Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;
|
---|
| 121 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | double factor2 = Origin.ScalarProduct(normalVector.get());
|
---|
| 125 | if (fabs(factor2-offset) < MYEPSILON) { // Origin is in-plane
|
---|
| 126 | Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;
|
---|
| 127 | res = Origin;
|
---|
| 128 | return res;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | double scaleFactor = (offset-factor2)/factor1;
|
---|
| 132 |
|
---|
| 133 | //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
|
---|
| 134 | Direction.Scale(scaleFactor);
|
---|
| 135 | res = Origin + Direction;
|
---|
| 136 | Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
|
---|
| 137 |
|
---|
| 138 | // test whether resulting vector really is on plane
|
---|
| 139 | ASSERT(fabs(res.ScalarProduct(normalVector.get()) - offset) < MYEPSILON,
|
---|
| 140 | "Calculated line-Plane intersection does not lie on plane.");
|
---|
| 141 | return res;
|
---|
| 142 | };
|
---|