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