| [6f646d] | 1 | /*
 | 
|---|
 | 2 |  * Line.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Apr 30, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [112b09] | 8 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 9 | 
 | 
|---|
| [6f646d] | 10 | #include "Line.hpp"
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | #include <cmath>
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | #include "vector.hpp"
 | 
|---|
| [45ef76] | 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"
 | 
|---|
| [5589858] | 21 | #include "Plane.hpp"
 | 
|---|
| [6f646d] | 22 | 
 | 
|---|
| [45ef76] | 23 | using namespace std;
 | 
|---|
 | 24 | 
 | 
|---|
 | 25 | Line::Line(const Vector &_origin, const Vector &_direction) :
 | 
|---|
| [6f646d] | 26 |   direction(new Vector(_direction))
 | 
|---|
 | 27 | {
 | 
|---|
 | 28 |   direction->Normalize();
 | 
|---|
| [45ef76] | 29 |   origin.reset(new Vector(_origin.partition(*direction).second));
 | 
|---|
| [6f646d] | 30 | }
 | 
|---|
 | 31 | 
 | 
|---|
| [45ef76] | 32 | Line::Line(const Line &src) :
 | 
|---|
 | 33 |   origin(new Vector(*src.origin)),
 | 
|---|
 | 34 |   direction(new Vector(*src.direction))
 | 
|---|
 | 35 | {}
 | 
|---|
 | 36 | 
 | 
|---|
| [6f646d] | 37 | Line::~Line()
 | 
|---|
 | 38 | {}
 | 
|---|
 | 39 | 
 | 
|---|
| [41da13] | 40 | Line &Line::operator=(const Line& rhs){
 | 
|---|
 | 41 |   if(this!=&rhs){
 | 
|---|
 | 42 |     origin.reset(new Vector(*rhs.origin));
 | 
|---|
 | 43 |     direction.reset(new Vector(*rhs.direction));
 | 
|---|
 | 44 |   }
 | 
|---|
 | 45 |   return *this;
 | 
|---|
 | 46 | }
 | 
|---|
 | 47 | 
 | 
|---|
| [6f646d] | 48 | 
 | 
|---|
 | 49 | double Line::distance(const Vector &point) const{
 | 
|---|
| [45ef76] | 50 |   // get any vector from line to point
 | 
|---|
 | 51 |   Vector helper = point - *origin;
 | 
|---|
 | 52 |   // partition this vector along direction
 | 
|---|
 | 53 |   // the residue points from the line to the point
 | 
|---|
 | 54 |   return helper.partition(*direction).second.Norm();
 | 
|---|
| [6f646d] | 55 | }
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | Vector Line::getClosestPoint(const Vector &point) const{
 | 
|---|
| [45ef76] | 58 |   // get any vector from line to point
 | 
|---|
 | 59 |   Vector helper = point - *origin;
 | 
|---|
 | 60 |   // partition this vector along direction
 | 
|---|
 | 61 |   // add only the part along the direction
 | 
|---|
 | 62 |   return *origin + helper.partition(*direction).first;
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | Vector Line::getDirection() const{
 | 
|---|
 | 66 |   return *direction;
 | 
|---|
 | 67 | }
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 | Vector Line::getOrigin() const{
 | 
|---|
 | 70 |   return *origin;
 | 
|---|
 | 71 | }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 | vector<Vector> Line::getPointsOnLine() const{
 | 
|---|
 | 74 |   vector<Vector> res;
 | 
|---|
 | 75 |   res.reserve(2);
 | 
|---|
 | 76 |   res.push_back(*origin);
 | 
|---|
 | 77 |   res.push_back(*origin+*direction);
 | 
|---|
 | 78 |   return res;
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
| [643e76] | 81 | /** Calculates the intersection of the two lines that are both on the same plane.
 | 
|---|
 | 82 |  * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
 | 
|---|
 | 83 |  * \param *out output stream for debugging
 | 
|---|
 | 84 |  * \param *Line1a first vector of first line
 | 
|---|
 | 85 |  * \param *Line1b second vector of first line
 | 
|---|
 | 86 |  * \param *Line2a first vector of second line
 | 
|---|
 | 87 |  * \param *Line2b second vector of second line
 | 
|---|
 | 88 |  * \return true - \a this will contain the intersection on return, false - lines are parallel
 | 
|---|
 | 89 |  */
 | 
|---|
| [45ef76] | 90 | Vector Line::getIntersection(const Line& otherLine) const{
 | 
|---|
 | 91 |   Info FunctionInfo(__func__);
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 |   pointset line1Points = getPointsOnLine();
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |   Vector Line1a = line1Points[0];
 | 
|---|
 | 96 |   Vector Line1b = line1Points[1];
 | 
|---|
 | 97 | 
 | 
|---|
 | 98 |   pointset line2Points = otherLine.getPointsOnLine();
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |   Vector Line2a = line2Points[0];
 | 
|---|
 | 101 |   Vector Line2b = line2Points[1];
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 |   Vector res;
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |   auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |   M->SetAll(1.);
 | 
|---|
 | 108 |   for (int i=0;i<3;i++) {
 | 
|---|
 | 109 |     M->Set(0, i, Line1a[i]);
 | 
|---|
 | 110 |     M->Set(1, i, Line1b[i]);
 | 
|---|
 | 111 |     M->Set(2, i, Line2a[i]);
 | 
|---|
 | 112 |     M->Set(3, i, Line2b[i]);
 | 
|---|
 | 113 |   }
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 |   //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
 | 
|---|
 | 116 |   //for (int i=0;i<4;i++) {
 | 
|---|
 | 117 |   //  for (int j=0;j<4;j++)
 | 
|---|
 | 118 |   //    cout << "\t" << M->Get(i,j);
 | 
|---|
 | 119 |   //  cout << endl;
 | 
|---|
 | 120 |   //}
 | 
|---|
 | 121 |   if (fabs(M->Determinant()) > MYEPSILON) {
 | 
|---|
 | 122 |     Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
 | 
|---|
 | 123 |     throw SkewException(__FILE__,__LINE__);
 | 
|---|
 | 124 |   }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 |   Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |   // constuct a,b,c
 | 
|---|
 | 130 |   Vector a = Line1b - Line1a;
 | 
|---|
 | 131 |   Vector b = Line2b - Line2a;
 | 
|---|
 | 132 |   Vector c = Line2a - Line1a;
 | 
|---|
 | 133 |   Vector d = Line2b - Line1b;
 | 
|---|
 | 134 |   Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
 | 
|---|
 | 135 |   if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
 | 
|---|
 | 136 |    res.Zero();
 | 
|---|
 | 137 |    Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
 | 
|---|
 | 138 |    throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 | 
 | 
|---|
 | 141 |   // check for parallelity
 | 
|---|
 | 142 |   Vector parallel;
 | 
|---|
 | 143 |   double factor = 0.;
 | 
|---|
 | 144 |   if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
 | 
|---|
 | 145 |     parallel = Line1a - Line2a;
 | 
|---|
 | 146 |     factor = parallel.ScalarProduct(a)/a.Norm();
 | 
|---|
 | 147 |     if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
 | 
|---|
 | 148 |       res = Line2a;
 | 
|---|
 | 149 |       Log() << Verbose(1) << "Lines conincide." << endl;
 | 
|---|
 | 150 |       return res;
 | 
|---|
 | 151 |     } else {
 | 
|---|
 | 152 |       parallel = Line1a - Line2b;
 | 
|---|
 | 153 |       factor = parallel.ScalarProduct(a)/a.Norm();
 | 
|---|
 | 154 |       if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
 | 
|---|
 | 155 |         res = Line2b;
 | 
|---|
 | 156 |         Log() << Verbose(1) << "Lines conincide." << endl;
 | 
|---|
 | 157 |         return res;
 | 
|---|
 | 158 |       }
 | 
|---|
 | 159 |     }
 | 
|---|
 | 160 |     Log() << Verbose(1) << "Lines are parallel." << endl;
 | 
|---|
 | 161 |     res.Zero();
 | 
|---|
 | 162 |     throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
 | 163 |   }
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 |   // obtain s
 | 
|---|
 | 166 |   double s;
 | 
|---|
 | 167 |   Vector temp1, temp2;
 | 
|---|
 | 168 |   temp1 = c;
 | 
|---|
 | 169 |   temp1.VectorProduct(b);
 | 
|---|
 | 170 |   temp2 = a;
 | 
|---|
 | 171 |   temp2.VectorProduct(b);
 | 
|---|
 | 172 |   Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
 | 
|---|
 | 173 |   if (fabs(temp2.NormSquared()) > MYEPSILON)
 | 
|---|
 | 174 |     s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
 | 
|---|
 | 175 |   else
 | 
|---|
 | 176 |     s = 0.;
 | 
|---|
 | 177 |   Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 |   // construct intersection
 | 
|---|
 | 180 |   res = a;
 | 
|---|
 | 181 |   res.Scale(s);
 | 
|---|
 | 182 |   res += Line1a;
 | 
|---|
 | 183 |   Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |   return res;
 | 
|---|
 | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
| [42a101] | 188 | /** Rotates the vector by an angle of \a alpha around this line.
 | 
|---|
 | 189 |  * \param rhs Vector to rotate
 | 
|---|
 | 190 |  * \param alpha rotation angle in radian
 | 
|---|
 | 191 |  */
 | 
|---|
 | 192 | Vector Line::rotateVector(const Vector &rhs, double alpha) const{
 | 
|---|
 | 193 |   Vector helper = rhs;
 | 
|---|
 | 194 | 
 | 
|---|
 | 195 |   // translate the coordinate system so that the line goes through (0,0,0)
 | 
|---|
 | 196 |   helper -= *origin;
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 |   // partition the vector into a part that gets rotated and a part that lies along the line
 | 
|---|
 | 199 |   pair<Vector,Vector> parts = helper.partition(*direction);
 | 
|---|
 | 200 | 
 | 
|---|
 | 201 |   // we just keep anything that is along the axis
 | 
|---|
 | 202 |   Vector res = parts.first;
 | 
|---|
 | 203 | 
 | 
|---|
 | 204 |   // the rest has to be rotated
 | 
|---|
 | 205 |   Vector a = parts.second;
 | 
|---|
 | 206 |   // we only have to do the rest, if we actually could partition the vector
 | 
|---|
 | 207 |   if(!a.IsZero()){
 | 
|---|
 | 208 |     // construct a vector that is orthogonal to a and direction and has length |a|
 | 
|---|
 | 209 |     Vector y = a;
 | 
|---|
 | 210 |     // direction is normalized, so the result has length |a|
 | 
|---|
 | 211 |     y.VectorProduct(*direction);
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 |     res += cos(alpha) * a + sin(alpha) * y;
 | 
|---|
 | 214 |   }
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 |   // translate the coordinate system back
 | 
|---|
 | 217 |   res += *origin;
 | 
|---|
 | 218 |   return res;
 | 
|---|
 | 219 | }
 | 
|---|
 | 220 | 
 | 
|---|
| [5589858] | 221 | Plane Line::getOrthogonalPlane(const Vector &origin) const{
 | 
|---|
 | 222 |   return Plane(getDirection(),origin);
 | 
|---|
 | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
| [f932b7] | 225 | std::vector<Vector> Line::getSphereIntersections() const{
 | 
|---|
 | 226 |   std::vector<Vector> res;
 | 
|---|
 | 227 | 
 | 
|---|
 | 228 |   // line is kept in normalized form, so we can skip a lot of calculations
 | 
|---|
 | 229 |   double discriminant = 1-origin->NormSquared();
 | 
|---|
 | 230 |   // we might have 2, 1 or 0 solutions, depending on discriminant
 | 
|---|
 | 231 |   if(discriminant>=0){
 | 
|---|
 | 232 |     if(discriminant==0){
 | 
|---|
 | 233 |       res.push_back(*origin);
 | 
|---|
 | 234 |     }
 | 
|---|
 | 235 |     else{
 | 
|---|
 | 236 |       Vector helper = sqrt(discriminant)*(*direction);
 | 
|---|
 | 237 |       res.push_back(*origin+helper);
 | 
|---|
 | 238 |       res.push_back(*origin-helper);
 | 
|---|
 | 239 |     }
 | 
|---|
 | 240 |   }
 | 
|---|
 | 241 |   return res;
 | 
|---|
 | 242 | }
 | 
|---|
 | 243 | 
 | 
|---|
| [6256f5] | 244 | LinePoint Line::getLinePoint(const Vector &point) const{
 | 
|---|
 | 245 |   ASSERT(isContained(point),"Line point queried for point not on line");
 | 
|---|
 | 246 |   Vector helper = point - (*origin);
 | 
|---|
 | 247 |   double param = helper.ScalarProduct(*direction);
 | 
|---|
 | 248 |   return LinePoint(*this,param);
 | 
|---|
 | 249 | }
 | 
|---|
 | 250 | 
 | 
|---|
 | 251 | LinePoint Line::posEndpoint() const{
 | 
|---|
 | 252 |   return LinePoint(*this, numeric_limits<double>::infinity());
 | 
|---|
 | 253 | }
 | 
|---|
 | 254 | LinePoint Line::negEndpoint() const{
 | 
|---|
 | 255 |   return LinePoint(*this,-numeric_limits<double>::infinity());
 | 
|---|
 | 256 | }
 | 
|---|
 | 257 | 
 | 
|---|
| [82cf79] | 258 | bool operator==(const Line &x,const Line &y){
 | 
|---|
 | 259 |   return *x.origin == *y.origin && *x.direction == *y.direction;
 | 
|---|
 | 260 | }
 | 
|---|
 | 261 | 
 | 
|---|
| [45ef76] | 262 | Line makeLineThrough(const Vector &x1, const Vector &x2){
 | 
|---|
 | 263 |   if(x1==x2){
 | 
|---|
 | 264 |     throw LinearDependenceException(__FILE__,__LINE__);
 | 
|---|
 | 265 |   }
 | 
|---|
 | 266 |   return Line(x1,x1-x2);
 | 
|---|
| [6f646d] | 267 | }
 | 
|---|
| [6256f5] | 268 | 
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 | /******************************** Points on the line ********************/
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 | LinePoint::LinePoint(const LinePoint &src) :
 | 
|---|
 | 273 |   line(src.line),param(src.param)
 | 
|---|
 | 274 | {}
 | 
|---|
 | 275 | 
 | 
|---|
 | 276 | LinePoint::LinePoint(const Line &_line, double _param) :
 | 
|---|
 | 277 |   line(_line),param(_param)
 | 
|---|
 | 278 | {}
 | 
|---|
 | 279 | 
 | 
|---|
 | 280 | LinePoint& LinePoint::operator=(const LinePoint &src){
 | 
|---|
 | 281 |   ASSERT(src.line==line,"Operation on two points of different lines");
 | 
|---|
 | 282 |   param=src.param;
 | 
|---|
 | 283 |   return *this;
 | 
|---|
 | 284 | }
 | 
|---|
 | 285 | 
 | 
|---|
 | 286 | Vector LinePoint::getPoint() const{
 | 
|---|
 | 287 |   ASSERT(!isInfinite(),"getPoint() on infinite LinePoint called");
 | 
|---|
 | 288 |   return (*line.origin)+param*(*line.direction);
 | 
|---|
 | 289 | }
 | 
|---|
 | 290 | 
 | 
|---|
 | 291 | Line LinePoint::getLine() const{
 | 
|---|
 | 292 |   return line;
 | 
|---|
 | 293 | }
 | 
|---|
 | 294 | 
 | 
|---|
 | 295 | bool LinePoint::isInfinite() const{
 | 
|---|
 | 296 |   return isPosInfinity() || isNegInfinity();
 | 
|---|
 | 297 | }
 | 
|---|
 | 298 | bool LinePoint::isPosInfinity() const{
 | 
|---|
 | 299 |   return param == numeric_limits<double>::infinity();
 | 
|---|
 | 300 | }
 | 
|---|
 | 301 | bool LinePoint::isNegInfinity() const{
 | 
|---|
 | 302 |   return param ==-numeric_limits<double>::infinity();
 | 
|---|
 | 303 | }
 | 
|---|
 | 304 | 
 | 
|---|
 | 305 | bool operator==(const LinePoint &x, const LinePoint &y){
 | 
|---|
 | 306 |   ASSERT(x.line==y.line,"Operation on two points of different lines");
 | 
|---|
 | 307 |   return x.param == y.param;
 | 
|---|
 | 308 | 
 | 
|---|
 | 309 | }
 | 
|---|
 | 310 | bool operator<(const LinePoint &x, const LinePoint &y){
 | 
|---|
 | 311 |   ASSERT(x.line==y.line,"Operation on two points of different lines");
 | 
|---|
 | 312 |   return x.param<y.param;
 | 
|---|
 | 313 | }
 | 
|---|