[6f646d] | 1 | /*
|
---|
| 2 | * Line.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Apr 30, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include "Line.hpp"
|
---|
| 9 |
|
---|
| 10 | #include <cmath>
|
---|
| 11 |
|
---|
| 12 | #include "vector.hpp"
|
---|
[45ef76] | 13 | #include "log.hpp"
|
---|
| 14 | #include "verbose.hpp"
|
---|
| 15 | #include "gslmatrix.hpp"
|
---|
| 16 | #include "info.hpp"
|
---|
| 17 | #include "Exceptions/LinearDependenceException.hpp"
|
---|
| 18 | #include "Exceptions/SkewException.hpp"
|
---|
[6f646d] | 19 |
|
---|
[45ef76] | 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | Line::Line(const Vector &_origin, const Vector &_direction) :
|
---|
[6f646d] | 23 | direction(new Vector(_direction))
|
---|
| 24 | {
|
---|
| 25 | direction->Normalize();
|
---|
[45ef76] | 26 | origin.reset(new Vector(_origin.partition(*direction).second));
|
---|
[6f646d] | 27 | }
|
---|
| 28 |
|
---|
[45ef76] | 29 | Line::Line(const Line &src) :
|
---|
| 30 | origin(new Vector(*src.origin)),
|
---|
| 31 | direction(new Vector(*src.direction))
|
---|
| 32 | {}
|
---|
| 33 |
|
---|
[6f646d] | 34 | Line::~Line()
|
---|
| 35 | {}
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | double Line::distance(const Vector &point) const{
|
---|
[45ef76] | 39 | // get any vector from line to point
|
---|
| 40 | Vector helper = point - *origin;
|
---|
| 41 | // partition this vector along direction
|
---|
| 42 | // the residue points from the line to the point
|
---|
| 43 | return helper.partition(*direction).second.Norm();
|
---|
[6f646d] | 44 | }
|
---|
| 45 |
|
---|
| 46 | Vector Line::getClosestPoint(const Vector &point) const{
|
---|
[45ef76] | 47 | // get any vector from line to point
|
---|
| 48 | Vector helper = point - *origin;
|
---|
| 49 | // partition this vector along direction
|
---|
| 50 | // add only the part along the direction
|
---|
| 51 | return *origin + helper.partition(*direction).first;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | Vector Line::getDirection() const{
|
---|
| 55 | return *direction;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | Vector Line::getOrigin() const{
|
---|
| 59 | return *origin;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | vector<Vector> Line::getPointsOnLine() const{
|
---|
| 63 | vector<Vector> res;
|
---|
| 64 | res.reserve(2);
|
---|
| 65 | res.push_back(*origin);
|
---|
| 66 | res.push_back(*origin+*direction);
|
---|
| 67 | return res;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | Vector Line::getIntersection(const Line& otherLine) const{
|
---|
| 71 | Info FunctionInfo(__func__);
|
---|
| 72 |
|
---|
| 73 | pointset line1Points = getPointsOnLine();
|
---|
| 74 |
|
---|
| 75 | Vector Line1a = line1Points[0];
|
---|
| 76 | Vector Line1b = line1Points[1];
|
---|
| 77 |
|
---|
| 78 | pointset line2Points = otherLine.getPointsOnLine();
|
---|
| 79 |
|
---|
| 80 | Vector Line2a = line2Points[0];
|
---|
| 81 | Vector Line2b = line2Points[1];
|
---|
| 82 |
|
---|
| 83 | Vector res;
|
---|
| 84 |
|
---|
| 85 | auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
|
---|
| 86 |
|
---|
| 87 | M->SetAll(1.);
|
---|
| 88 | for (int i=0;i<3;i++) {
|
---|
| 89 | M->Set(0, i, Line1a[i]);
|
---|
| 90 | M->Set(1, i, Line1b[i]);
|
---|
| 91 | M->Set(2, i, Line2a[i]);
|
---|
| 92 | M->Set(3, i, Line2b[i]);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
|
---|
| 96 | //for (int i=0;i<4;i++) {
|
---|
| 97 | // for (int j=0;j<4;j++)
|
---|
| 98 | // cout << "\t" << M->Get(i,j);
|
---|
| 99 | // cout << endl;
|
---|
| 100 | //}
|
---|
| 101 | if (fabs(M->Determinant()) > MYEPSILON) {
|
---|
| 102 | Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
|
---|
| 103 | throw SkewException(__FILE__,__LINE__);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
|
---|
| 107 |
|
---|
| 108 |
|
---|
| 109 | // constuct a,b,c
|
---|
| 110 | Vector a = Line1b - Line1a;
|
---|
| 111 | Vector b = Line2b - Line2a;
|
---|
| 112 | Vector c = Line2a - Line1a;
|
---|
| 113 | Vector d = Line2b - Line1b;
|
---|
| 114 | Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
|
---|
| 115 | if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
|
---|
| 116 | res.Zero();
|
---|
| 117 | Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
|
---|
| 118 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // check for parallelity
|
---|
| 122 | Vector parallel;
|
---|
| 123 | double factor = 0.;
|
---|
| 124 | if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
|
---|
| 125 | parallel = Line1a - Line2a;
|
---|
| 126 | factor = parallel.ScalarProduct(a)/a.Norm();
|
---|
| 127 | if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
|
---|
| 128 | res = Line2a;
|
---|
| 129 | Log() << Verbose(1) << "Lines conincide." << endl;
|
---|
| 130 | return res;
|
---|
| 131 | } else {
|
---|
| 132 | parallel = Line1a - Line2b;
|
---|
| 133 | factor = parallel.ScalarProduct(a)/a.Norm();
|
---|
| 134 | if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
|
---|
| 135 | res = Line2b;
|
---|
| 136 | Log() << Verbose(1) << "Lines conincide." << endl;
|
---|
| 137 | return res;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | Log() << Verbose(1) << "Lines are parallel." << endl;
|
---|
| 141 | res.Zero();
|
---|
| 142 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | // obtain s
|
---|
| 146 | double s;
|
---|
| 147 | Vector temp1, temp2;
|
---|
| 148 | temp1 = c;
|
---|
| 149 | temp1.VectorProduct(b);
|
---|
| 150 | temp2 = a;
|
---|
| 151 | temp2.VectorProduct(b);
|
---|
| 152 | Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
|
---|
| 153 | if (fabs(temp2.NormSquared()) > MYEPSILON)
|
---|
| 154 | s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
|
---|
| 155 | else
|
---|
| 156 | s = 0.;
|
---|
| 157 | Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
|
---|
| 158 |
|
---|
| 159 | // construct intersection
|
---|
| 160 | res = a;
|
---|
| 161 | res.Scale(s);
|
---|
| 162 | res += Line1a;
|
---|
| 163 | Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
|
---|
| 164 |
|
---|
| 165 | return res;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | Line makeLineThrough(const Vector &x1, const Vector &x2){
|
---|
| 169 | if(x1==x2){
|
---|
| 170 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
| 171 | }
|
---|
| 172 | return Line(x1,x1-x2);
|
---|
[6f646d] | 173 | }
|
---|