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