| 1 | /*
 | 
|---|
| 2 |  * Line.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Apr 30, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Line.hpp"
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include <cmath>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include "vector.hpp"
 | 
|---|
| 15 | #include "log.hpp"
 | 
|---|
| 16 | #include "verbose.hpp"
 | 
|---|
| 17 | #include "gslmatrix.hpp"
 | 
|---|
| 18 | #include "info.hpp"
 | 
|---|
| 19 | #include "Exceptions/LinearDependenceException.hpp"
 | 
|---|
| 20 | #include "Exceptions/SkewException.hpp"
 | 
|---|
| 21 | #include "Plane.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | using namespace std;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | Line::Line(const Vector &_origin, const Vector &_direction) :
 | 
|---|
| 26 |   direction(new Vector(_direction))
 | 
|---|
| 27 | {
 | 
|---|
| 28 |   direction->Normalize();
 | 
|---|
| 29 |   origin.reset(new Vector(_origin.partition(*direction).second));
 | 
|---|
| 30 | }
 | 
|---|
| 31 | 
 | 
|---|
| 32 | Line::Line(const Line &src) :
 | 
|---|
| 33 |   origin(new Vector(*src.origin)),
 | 
|---|
| 34 |   direction(new Vector(*src.direction))
 | 
|---|
| 35 | {}
 | 
|---|
| 36 | 
 | 
|---|
| 37 | Line::~Line()
 | 
|---|
| 38 | {}
 | 
|---|
| 39 | 
 | 
|---|
| 40 | 
 | 
|---|
| 41 | double Line::distance(const Vector &point) const{
 | 
|---|
| 42 |   // get any vector from line to point
 | 
|---|
| 43 |   Vector helper = point - *origin;
 | 
|---|
| 44 |   // partition this vector along direction
 | 
|---|
| 45 |   // the residue points from the line to the point
 | 
|---|
| 46 |   return helper.partition(*direction).second.Norm();
 | 
|---|
| 47 | }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | Vector Line::getClosestPoint(const Vector &point) const{
 | 
|---|
| 50 |   // get any vector from line to point
 | 
|---|
| 51 |   Vector helper = point - *origin;
 | 
|---|
| 52 |   // partition this vector along direction
 | 
|---|
| 53 |   // add only the part along the direction
 | 
|---|
| 54 |   return *origin + helper.partition(*direction).first;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | Vector Line::getDirection() const{
 | 
|---|
| 58 |   return *direction;
 | 
|---|
| 59 | }
 | 
|---|
| 60 | 
 | 
|---|
| 61 | Vector Line::getOrigin() const{
 | 
|---|
| 62 |   return *origin;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | vector<Vector> Line::getPointsOnLine() const{
 | 
|---|
| 66 |   vector<Vector> res;
 | 
|---|
| 67 |   res.reserve(2);
 | 
|---|
| 68 |   res.push_back(*origin);
 | 
|---|
| 69 |   res.push_back(*origin+*direction);
 | 
|---|
| 70 |   return res;
 | 
|---|
| 71 | }
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /** Calculates the intersection of the two lines that are both on the same plane.
 | 
|---|
| 74 |  * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
 | 
|---|
| 75 |  * \param *out output stream for debugging
 | 
|---|
| 76 |  * \param *Line1a first vector of first line
 | 
|---|
| 77 |  * \param *Line1b second vector of first line
 | 
|---|
| 78 |  * \param *Line2a first vector of second line
 | 
|---|
| 79 |  * \param *Line2b second vector of second line
 | 
|---|
| 80 |  * \return true - \a this will contain the intersection on return, false - lines are parallel
 | 
|---|
| 81 |  */
 | 
|---|
| 82 | Vector Line::getIntersection(const Line& otherLine) const{
 | 
|---|
| 83 |   Info FunctionInfo(__func__);
 | 
|---|
| 84 | 
 | 
|---|
| 85 |   pointset line1Points = getPointsOnLine();
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   Vector Line1a = line1Points[0];
 | 
|---|
| 88 |   Vector Line1b = line1Points[1];
 | 
|---|
| 89 | 
 | 
|---|
| 90 |   pointset line2Points = otherLine.getPointsOnLine();
 | 
|---|
| 91 | 
 | 
|---|
| 92 |   Vector Line2a = line2Points[0];
 | 
|---|
| 93 |   Vector Line2b = line2Points[1];
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   Vector res;
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   M->SetAll(1.);
 | 
|---|
| 100 |   for (int i=0;i<3;i++) {
 | 
|---|
| 101 |     M->Set(0, i, Line1a[i]);
 | 
|---|
| 102 |     M->Set(1, i, Line1b[i]);
 | 
|---|
| 103 |     M->Set(2, i, Line2a[i]);
 | 
|---|
| 104 |     M->Set(3, i, Line2b[i]);
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
 | 
|---|
| 108 |   //for (int i=0;i<4;i++) {
 | 
|---|
| 109 |   //  for (int j=0;j<4;j++)
 | 
|---|
| 110 |   //    cout << "\t" << M->Get(i,j);
 | 
|---|
| 111 |   //  cout << endl;
 | 
|---|
| 112 |   //}
 | 
|---|
| 113 |   if (fabs(M->Determinant()) > MYEPSILON) {
 | 
|---|
| 114 |     Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
 | 
|---|
| 115 |     throw SkewException(__FILE__,__LINE__);
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
 | 
|---|
| 119 | 
 | 
|---|
| 120 | 
 | 
|---|
| 121 |   // constuct a,b,c
 | 
|---|
| 122 |   Vector a = Line1b - Line1a;
 | 
|---|
| 123 |   Vector b = Line2b - Line2a;
 | 
|---|
| 124 |   Vector c = Line2a - Line1a;
 | 
|---|
| 125 |   Vector d = Line2b - Line1b;
 | 
|---|
| 126 |   Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
 | 
|---|
| 127 |   if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
 | 
|---|
| 128 |    res.Zero();
 | 
|---|
| 129 |    Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
 | 
|---|
| 130 |    throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
| 131 |   }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   // check for parallelity
 | 
|---|
| 134 |   Vector parallel;
 | 
|---|
| 135 |   double factor = 0.;
 | 
|---|
| 136 |   if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
 | 
|---|
| 137 |     parallel = Line1a - Line2a;
 | 
|---|
| 138 |     factor = parallel.ScalarProduct(a)/a.Norm();
 | 
|---|
| 139 |     if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
 | 
|---|
| 140 |       res = Line2a;
 | 
|---|
| 141 |       Log() << Verbose(1) << "Lines conincide." << endl;
 | 
|---|
| 142 |       return res;
 | 
|---|
| 143 |     } else {
 | 
|---|
| 144 |       parallel = Line1a - Line2b;
 | 
|---|
| 145 |       factor = parallel.ScalarProduct(a)/a.Norm();
 | 
|---|
| 146 |       if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
 | 
|---|
| 147 |         res = Line2b;
 | 
|---|
| 148 |         Log() << Verbose(1) << "Lines conincide." << endl;
 | 
|---|
| 149 |         return res;
 | 
|---|
| 150 |       }
 | 
|---|
| 151 |     }
 | 
|---|
| 152 |     Log() << Verbose(1) << "Lines are parallel." << endl;
 | 
|---|
| 153 |     res.Zero();
 | 
|---|
| 154 |     throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
| 155 |   }
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   // obtain s
 | 
|---|
| 158 |   double s;
 | 
|---|
| 159 |   Vector temp1, temp2;
 | 
|---|
| 160 |   temp1 = c;
 | 
|---|
| 161 |   temp1.VectorProduct(b);
 | 
|---|
| 162 |   temp2 = a;
 | 
|---|
| 163 |   temp2.VectorProduct(b);
 | 
|---|
| 164 |   Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
 | 
|---|
| 165 |   if (fabs(temp2.NormSquared()) > MYEPSILON)
 | 
|---|
| 166 |     s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
 | 
|---|
| 167 |   else
 | 
|---|
| 168 |     s = 0.;
 | 
|---|
| 169 |   Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
 | 
|---|
| 170 | 
 | 
|---|
| 171 |   // construct intersection
 | 
|---|
| 172 |   res = a;
 | 
|---|
| 173 |   res.Scale(s);
 | 
|---|
| 174 |   res += Line1a;
 | 
|---|
| 175 |   Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
 | 
|---|
| 176 | 
 | 
|---|
| 177 |   return res;
 | 
|---|
| 178 | }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | /** Rotates the vector by an angle of \a alpha around this line.
 | 
|---|
| 181 |  * \param rhs Vector to rotate
 | 
|---|
| 182 |  * \param alpha rotation angle in radian
 | 
|---|
| 183 |  */
 | 
|---|
| 184 | Vector Line::rotateVector(const Vector &rhs, double alpha) const{
 | 
|---|
| 185 |   Vector helper = rhs;
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   // translate the coordinate system so that the line goes through (0,0,0)
 | 
|---|
| 188 |   helper -= *origin;
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   // partition the vector into a part that gets rotated and a part that lies along the line
 | 
|---|
| 191 |   pair<Vector,Vector> parts = helper.partition(*direction);
 | 
|---|
| 192 | 
 | 
|---|
| 193 |   // we just keep anything that is along the axis
 | 
|---|
| 194 |   Vector res = parts.first;
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   // the rest has to be rotated
 | 
|---|
| 197 |   Vector a = parts.second;
 | 
|---|
| 198 |   // we only have to do the rest, if we actually could partition the vector
 | 
|---|
| 199 |   if(!a.IsZero()){
 | 
|---|
| 200 |     // construct a vector that is orthogonal to a and direction and has length |a|
 | 
|---|
| 201 |     Vector y = a;
 | 
|---|
| 202 |     // direction is normalized, so the result has length |a|
 | 
|---|
| 203 |     y.VectorProduct(*direction);
 | 
|---|
| 204 | 
 | 
|---|
| 205 |     res += cos(alpha) * a + sin(alpha) * y;
 | 
|---|
| 206 |   }
 | 
|---|
| 207 | 
 | 
|---|
| 208 |   // translate the coordinate system back
 | 
|---|
| 209 |   res += *origin;
 | 
|---|
| 210 |   return res;
 | 
|---|
| 211 | }
 | 
|---|
| 212 | 
 | 
|---|
| 213 | Plane Line::getOrthogonalPlane(const Vector &origin) const{
 | 
|---|
| 214 |   return Plane(getDirection(),origin);
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | Line makeLineThrough(const Vector &x1, const Vector &x2){
 | 
|---|
| 218 |   if(x1==x2){
 | 
|---|
| 219 |     throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
| 220 |   }
 | 
|---|
| 221 |   return Line(x1,x1-x2);
 | 
|---|
| 222 | }
 | 
|---|