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