source: molecuilder/src/Plane.cpp@ 0e9394

Last change on this file since 0e9394 was 0e9394, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added a class Space to represent general subspaces of R3 (planes and lines)

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