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"
|
---|
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"
|
---|
19 |
|
---|
20 | using namespace std;
|
---|
21 |
|
---|
22 | Line::Line(const Vector &_origin, const Vector &_direction) :
|
---|
23 | direction(new Vector(_direction))
|
---|
24 | {
|
---|
25 | direction->Normalize();
|
---|
26 | origin.reset(new Vector(_origin.partition(*direction).second));
|
---|
27 | }
|
---|
28 |
|
---|
29 | Line::Line(const Line &src) :
|
---|
30 | origin(new Vector(*src.origin)),
|
---|
31 | direction(new Vector(*src.direction))
|
---|
32 | {}
|
---|
33 |
|
---|
34 | Line::~Line()
|
---|
35 | {}
|
---|
36 |
|
---|
37 |
|
---|
38 | double Line::distance(const Vector &point) const{
|
---|
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();
|
---|
44 | }
|
---|
45 |
|
---|
46 | Vector Line::getClosestPoint(const Vector &point) const{
|
---|
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 | /** Calculates the intersection of the two lines that are both on the same plane.
|
---|
71 | * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
|
---|
72 | * \param *out output stream for debugging
|
---|
73 | * \param *Line1a first vector of first line
|
---|
74 | * \param *Line1b second vector of first line
|
---|
75 | * \param *Line2a first vector of second line
|
---|
76 | * \param *Line2b second vector of second line
|
---|
77 | * \return true - \a this will contain the intersection on return, false - lines are parallel
|
---|
78 | */
|
---|
79 | Vector Line::getIntersection(const Line& otherLine) const{
|
---|
80 | Info FunctionInfo(__func__);
|
---|
81 |
|
---|
82 | pointset line1Points = getPointsOnLine();
|
---|
83 |
|
---|
84 | Vector Line1a = line1Points[0];
|
---|
85 | Vector Line1b = line1Points[1];
|
---|
86 |
|
---|
87 | pointset line2Points = otherLine.getPointsOnLine();
|
---|
88 |
|
---|
89 | Vector Line2a = line2Points[0];
|
---|
90 | Vector Line2b = line2Points[1];
|
---|
91 |
|
---|
92 | Vector res;
|
---|
93 |
|
---|
94 | auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
|
---|
95 |
|
---|
96 | M->SetAll(1.);
|
---|
97 | for (int i=0;i<3;i++) {
|
---|
98 | M->Set(0, i, Line1a[i]);
|
---|
99 | M->Set(1, i, Line1b[i]);
|
---|
100 | M->Set(2, i, Line2a[i]);
|
---|
101 | M->Set(3, i, Line2b[i]);
|
---|
102 | }
|
---|
103 |
|
---|
104 | //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
|
---|
105 | //for (int i=0;i<4;i++) {
|
---|
106 | // for (int j=0;j<4;j++)
|
---|
107 | // cout << "\t" << M->Get(i,j);
|
---|
108 | // cout << endl;
|
---|
109 | //}
|
---|
110 | if (fabs(M->Determinant()) > MYEPSILON) {
|
---|
111 | Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
|
---|
112 | throw SkewException(__FILE__,__LINE__);
|
---|
113 | }
|
---|
114 |
|
---|
115 | Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
|
---|
116 |
|
---|
117 |
|
---|
118 | // constuct a,b,c
|
---|
119 | Vector a = Line1b - Line1a;
|
---|
120 | Vector b = Line2b - Line2a;
|
---|
121 | Vector c = Line2a - Line1a;
|
---|
122 | Vector d = Line2b - Line1b;
|
---|
123 | Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
|
---|
124 | if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
|
---|
125 | res.Zero();
|
---|
126 | Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
|
---|
127 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
128 | }
|
---|
129 |
|
---|
130 | // check for parallelity
|
---|
131 | Vector parallel;
|
---|
132 | double factor = 0.;
|
---|
133 | if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
|
---|
134 | parallel = Line1a - Line2a;
|
---|
135 | factor = parallel.ScalarProduct(a)/a.Norm();
|
---|
136 | if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
|
---|
137 | res = Line2a;
|
---|
138 | Log() << Verbose(1) << "Lines conincide." << endl;
|
---|
139 | return res;
|
---|
140 | } else {
|
---|
141 | parallel = Line1a - Line2b;
|
---|
142 | factor = parallel.ScalarProduct(a)/a.Norm();
|
---|
143 | if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
|
---|
144 | res = Line2b;
|
---|
145 | Log() << Verbose(1) << "Lines conincide." << endl;
|
---|
146 | return res;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | Log() << Verbose(1) << "Lines are parallel." << endl;
|
---|
150 | res.Zero();
|
---|
151 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
152 | }
|
---|
153 |
|
---|
154 | // obtain s
|
---|
155 | double s;
|
---|
156 | Vector temp1, temp2;
|
---|
157 | temp1 = c;
|
---|
158 | temp1.VectorProduct(b);
|
---|
159 | temp2 = a;
|
---|
160 | temp2.VectorProduct(b);
|
---|
161 | Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
|
---|
162 | if (fabs(temp2.NormSquared()) > MYEPSILON)
|
---|
163 | s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
|
---|
164 | else
|
---|
165 | s = 0.;
|
---|
166 | Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
|
---|
167 |
|
---|
168 | // construct intersection
|
---|
169 | res = a;
|
---|
170 | res.Scale(s);
|
---|
171 | res += Line1a;
|
---|
172 | Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
|
---|
173 |
|
---|
174 | return res;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** Rotates the vector by an angle of \a alpha around this line.
|
---|
178 | * \param rhs Vector to rotate
|
---|
179 | * \param alpha rotation angle in radian
|
---|
180 | */
|
---|
181 | Vector Line::rotateVector(const Vector &rhs, double alpha) const{
|
---|
182 | Vector helper = rhs;
|
---|
183 |
|
---|
184 | // translate the coordinate system so that the line goes through (0,0,0)
|
---|
185 | helper -= *origin;
|
---|
186 |
|
---|
187 | // partition the vector into a part that gets rotated and a part that lies along the line
|
---|
188 | pair<Vector,Vector> parts = helper.partition(*direction);
|
---|
189 |
|
---|
190 | // we just keep anything that is along the axis
|
---|
191 | Vector res = parts.first;
|
---|
192 |
|
---|
193 | // the rest has to be rotated
|
---|
194 | Vector a = parts.second;
|
---|
195 | // we only have to do the rest, if we actually could partition the vector
|
---|
196 | if(!a.IsZero()){
|
---|
197 | // construct a vector that is orthogonal to a and direction and has length |a|
|
---|
198 | Vector y = a;
|
---|
199 | // direction is normalized, so the result has length |a|
|
---|
200 | y.VectorProduct(*direction);
|
---|
201 |
|
---|
202 | res += cos(alpha) * a + sin(alpha) * y;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // translate the coordinate system back
|
---|
206 | res += *origin;
|
---|
207 | return res;
|
---|
208 | }
|
---|
209 |
|
---|
210 | Line makeLineThrough(const Vector &x1, const Vector &x2){
|
---|
211 | if(x1==x2){
|
---|
212 | throw LinearDependenceException(__FILE__,__LINE__);
|
---|
213 | }
|
---|
214 | return Line(x1,x1-x2);
|
---|
215 | }
|
---|