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