source: molecuilder/src/Plane.cpp@ 536ce2

Last change on this file since 536ce2 was 59e7832, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added unittests for planes

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[71910a]1/*
2 * Plane.cpp
3 *
4 * Created on: Apr 7, 2010
5 * Author: crueger
6 */
7
8#include "Plane.hpp"
9#include "vector.hpp"
[0e9394]10#include "defs.hpp"
[71910a]11#include "info.hpp"
12#include "log.hpp"
13#include "verbose.hpp"
14#include "Helpers/Assert.hpp"
[0e9394]15#include <cmath>
[71910a]16
17/**
18 * generates a plane from three given vectors defining three points in space
19 */
[aab470]20Plane::Plane(const Vector &y1, const Vector &y2, const Vector &y3) throw(LinearDependenceException) :
[71910a]21 normalVector(new Vector())
22{
[1f591b]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)) {
[71910a]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
[1f591b]40 offset=normalVector->ScalarProduct(y1);
[71910a]41}
42/**
[aab470]43 * Constructs a plane from two direction vectors and a offset.
[71910a]44 * If no offset is given a plane through origin is assumed
45 */
[59e7832]46Plane::Plane(const Vector &y1, const Vector &y2, double _offset) throw(ZeroVectorException,LinearDependenceException) :
[71910a]47 normalVector(new Vector()),
48 offset(_offset)
49{
[1f591b]50 Vector x1 = y1;
51 Vector x2 = y2;
[59e7832]52 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON)) {
53 throw ZeroVectorException(__FILE__,__LINE__);
54 }
55
56 if((fabs(x1.Angle(x2)) < MYEPSILON)) {
[71910a]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
[aab470]72Plane::Plane(const Vector &_normalVector, double _offset) throw(ZeroVectorException):
[71910a]73 normalVector(new Vector(_normalVector)),
74 offset(_offset)
[0f55b2]75{
[aab470]76 if(normalVector->IsZero())
77 throw ZeroVectorException(__FILE__,__LINE__);
[0f55b2]78 double factor = 1/normalVector->Norm();
79 // normalize the plane parameters
80 (*normalVector)*=factor;
81 offset*=factor;
82}
[71910a]83
[aab470]84Plane::Plane(const Vector &_normalVector, const Vector &_offsetVector) throw(ZeroVectorException):
[71910a]85 normalVector(new Vector(_normalVector))
86{
[aab470]87 if(normalVector->IsZero()){
88 throw ZeroVectorException(__FILE__,__LINE__);
89 }
[7a8319]90 normalVector->Normalize();
[1f591b]91 offset = normalVector->ScalarProduct(_offsetVector);
[71910a]92}
93
94Plane::~Plane()
95{}
96
97
[59e7832]98Vector Plane::getNormal() const{
[71910a]99 return *normalVector;
100}
101
[59e7832]102double Plane::getOffset() const{
[71910a]103 return offset;
104}
105
[0f55b2]106Vector Plane::getOffsetVector() {
107 return getOffset()*getNormal();
108}
[465abf]109
110vector<Vector> Plane::getPointsOnPlane(){
[6a314f]111 std::vector<Vector> res;
[59e7832]112 res.reserve(3);
[6a314f]113 // first point on the plane
[59e7832]114 res.push_back(getOffsetVector());
115 // get a vector that has direction of plane
[465abf]116 Vector direction;
[59e7832]117 direction.GetOneNormalVector(getNormal());
118 res.push_back(res[0]+direction);
119 // get an orthogonal vector to direction and normal (has direction of plane)
120 direction.VectorProduct(getNormal());
[465abf]121 direction.Normalize();
[59e7832]122 res.push_back(res[0] +direction);
[465abf]123 return res;
[6a314f]124}
[465abf]125
[0f55b2]126
[71910a]127/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
128 * According to [Bronstein] the vectorial plane equation is:
129 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
130 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
131 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
132 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
133 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
134 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
135 * of the line yields the intersection point on the plane.
136 * \param *Origin first vector of line
137 * \param *LineVector second vector of line
138 * \return true - \a this contains intersection point on return, false - line is parallel to plane (even if in-plane)
139 */
140Vector Plane::GetIntersection(const Vector &Origin, const Vector &LineVector)
141{
142 Info FunctionInfo(__func__);
143 Vector res;
144
145 // find intersection of a line defined by Offset and Direction with a plane defined by triangle
146 Vector Direction = LineVector - Origin;
147 Direction.Normalize();
148 Log() << Verbose(1) << "INFO: Direction is " << Direction << "." << endl;
149 //Log() << Verbose(1) << "INFO: PlaneNormal is " << *PlaneNormal << " and PlaneOffset is " << *PlaneOffset << "." << endl;
[1f591b]150 double factor1 = Direction.ScalarProduct(*normalVector.get());
[71910a]151 if (fabs(factor1) < MYEPSILON) { // Uniqueness: line parallel to plane?
152 Log() << Verbose(1) << "BAD: Line is parallel to plane, no intersection." << endl;
153 throw LinearDependenceException(__FILE__,__LINE__);
154 }
155
[1f591b]156 double factor2 = Origin.ScalarProduct(*normalVector.get());
[71910a]157 if (fabs(factor2-offset) < MYEPSILON) { // Origin is in-plane
158 Log() << Verbose(1) << "GOOD: Origin of line is in-plane." << endl;
159 res = Origin;
160 return res;
161 }
162
163 double scaleFactor = (offset-factor2)/factor1;
164
165 //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
166 Direction.Scale(scaleFactor);
167 res = Origin + Direction;
168 Log() << Verbose(1) << "INFO: Scaled direction is " << Direction << "." << endl;
169
170 // test whether resulting vector really is on plane
[cb2b9a]171 ASSERT(fabs(res.ScalarProduct(*normalVector) - offset) < MYEPSILON,
[71910a]172 "Calculated line-Plane intersection does not lie on plane.");
173 return res;
174};
[0e9394]175
176/************ Methods inherited from Space ****************/
177
[f70c2a]178double Plane::distance(const Vector &point) const{
[0e9394]179 double res = point.ScalarProduct(*normalVector)-offset;
180 return fabs(res);
181}
182
[f70c2a]183Vector Plane::getClosestPoint(const Vector &point) const{
[59e7832]184 double factor = point.ScalarProduct(*normalVector)-offset;
185 if(fabs(factor) < MYEPSILON){
[0e9394]186 // the point itself lies on the plane
187 return point;
188 }
[59e7832]189 Vector difference = factor * (*normalVector);
190 return (point - difference);
191}
192
193// Operators
194
195ostream &operator << (ostream &ost,const Plane &p){
196 ost << "<" << p.getNormal() << ";x> - " << p.getOffset() << "=0";
197 return ost;
[0e9394]198}
Note: See TracBrowser for help on using the repository browser.