Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/vector_ops.cpp

    rfa5a6a r42a101  
    1515#include "Helpers/fast_functions.hpp"
    1616#include "Exceptions/LinearDependenceException.hpp"
     17#include "Exceptions/SkewException.hpp"
    1718
    1819#include <gsl/gsl_linalg.h>
     
    110111  return true;
    111112};
    112 
    113 /** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
    114  * \param *axis rotation axis
    115  * \param alpha rotation angle in radian
    116  */
    117 Vector RotateVector(const Vector &vec,const Vector &axis, const double alpha)
    118 {
    119   Vector a,y;
    120   Vector res;
    121   // normalise this vector with respect to axis
    122   a = vec;
    123   a.ProjectOntoPlane(axis);
    124   // construct normal vector
    125   try {
    126     y = Plane(axis,a,0).getNormal();
    127   }
    128   catch (MathException &excp) {
    129     // The normal vector cannot be created if there is linar dependency.
    130     // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
    131     return vec;
    132   }
    133   y.Scale(vec.Norm());
    134   // scale normal vector by sine and this vector by cosine
    135   y.Scale(sin(alpha));
    136   a.Scale(cos(alpha));
    137   res = vec.Projection(axis);
    138   // add scaled normal vector onto this vector
    139   res += y;
    140   // add part in axis direction
    141   res += a;
    142   return res;
    143 };
    144 
    145 /** Calculates the intersection of the two lines that are both on the same plane.
    146  * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
    147  * \param *out output stream for debugging
    148  * \param *Line1a first vector of first line
    149  * \param *Line1b second vector of first line
    150  * \param *Line2a first vector of second line
    151  * \param *Line2b second vector of second line
    152  * \return true - \a this will contain the intersection on return, false - lines are parallel
    153  */
    154 Vector GetIntersectionOfTwoLinesOnPlane(const Vector &Line1a, const Vector &Line1b, const Vector &Line2a, const Vector &Line2b)
    155 {
    156   Info FunctionInfo(__func__);
    157 
    158   Vector res;
    159 
    160   auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
    161 
    162   M->SetAll(1.);
    163   for (int i=0;i<3;i++) {
    164     M->Set(0, i, Line1a[i]);
    165     M->Set(1, i, Line1b[i]);
    166     M->Set(2, i, Line2a[i]);
    167     M->Set(3, i, Line2b[i]);
    168   }
    169 
    170   //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
    171   //for (int i=0;i<4;i++) {
    172   //  for (int j=0;j<4;j++)
    173   //    cout << "\t" << M->Get(i,j);
    174   //  cout << endl;
    175   //}
    176   if (fabs(M->Determinant()) > MYEPSILON) {
    177     Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
    178     throw LinearDependenceException(__FILE__,__LINE__);
    179   }
    180 
    181   Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
    182 
    183 
    184   // constuct a,b,c
    185   Vector a = Line1b - Line1a;
    186   Vector b = Line2b - Line2a;
    187   Vector c = Line2a - Line1a;
    188   Vector d = Line2b - Line1b;
    189   Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
    190   if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
    191    res.Zero();
    192    Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
    193    throw LinearDependenceException(__FILE__,__LINE__);
    194   }
    195 
    196   // check for parallelity
    197   Vector parallel;
    198   double factor = 0.;
    199   if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
    200     parallel = Line1a - Line2a;
    201     factor = parallel.ScalarProduct(a)/a.Norm();
    202     if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
    203       res = Line2a;
    204       Log() << Verbose(1) << "Lines conincide." << endl;
    205       return res;
    206     } else {
    207       parallel = Line1a - Line2b;
    208       factor = parallel.ScalarProduct(a)/a.Norm();
    209       if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
    210         res = Line2b;
    211         Log() << Verbose(1) << "Lines conincide." << endl;
    212         return res;
    213       }
    214     }
    215     Log() << Verbose(1) << "Lines are parallel." << endl;
    216     res.Zero();
    217     throw LinearDependenceException(__FILE__,__LINE__);
    218   }
    219 
    220   // obtain s
    221   double s;
    222   Vector temp1, temp2;
    223   temp1 = c;
    224   temp1.VectorProduct(b);
    225   temp2 = a;
    226   temp2.VectorProduct(b);
    227   Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
    228   if (fabs(temp2.NormSquared()) > MYEPSILON)
    229     s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
    230   else
    231     s = 0.;
    232   Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
    233 
    234   // construct intersection
    235   res = a;
    236   res.Scale(s);
    237   res += Line1a;
    238   Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
    239 
    240   return res;
    241 };
Note: See TracChangeset for help on using the changeset viewer.