Line.hh
Go to the documentation of this file.
1 
4 #pragma once
5 #include <boost/units/operators.hpp>
6 
7 namespace boca
8 {
9 
14 template <template<typename> class Vector_, typename Value_>
15 class Line
16 {
17 
18  using Value_Square = typename boost::units::multiply_typeof_helper<Value_, Value_>::type;
19 
20 public:
21 
30  Line() {}
31 
35  Line(Vector_<Value_> const &point_1, Vector_<Value_> const &point_2) :
36  point_1_(point_1),
37  point_2_(point_2)
38  {}
39 
41 
50  Vector_<Value_> const &Point_1() const
51  {
52  return point_1_;
53  }
54 
58  Vector_<Value_> Point_1()
59  {
60  return point_1_;
61  }
62 
66  Vector_<Value_> const &Point_2() const
67  {
68  return point_2_;
69  }
70 
74  Vector_<Value_> Point_2()
75  {
76  return point_2_;
77  }
78 
80 
89  Value_ DistanceToSegment(Vector_<Value_> const &point) const
90  {
91  if (Mag2() == Value_Square(0)) return Distance_1(point); // v == w case
92  auto parameter = Paramter(point);
93  if (parameter < 0) return Distance_1(point); // Beyond the 'v' end of the segment
94  if (parameter > 1) return Distance_2(point); // Beyond the 'w' end of the segment
95  return DistanceToLine(point); // Projection falls on the segment
96  }
97 
101  Value_ DistanceToLine(Vector_<Value_> const &point) const
102  {
103  if (Mag2() == Value_Square(0)) return Distance_1(point); // v == w case
104  return (point - Projection(point)).Mag();
105  }
106 
108 
117  Vector_<Value_> Vector() const
118  {
119  return point_2_ - point_1_;
120  }
121 
125  Value_Square Mag2() const
126  {
127  return Vector().Mag2();
128  }
129 
133  Value_ Mag() const
134  {
135  return Vector().Mag();
136  }
137 
139 
148  Vector_<Value_> Vector_1(Vector_<Value_> const &point) const
149  {
150  return point - point_1_;
151  }
152 
156  Vector_<Value_> Vector_2(Vector_<Value_> const &point) const
157  {
158  return point - point_2_;
159  }
160 
164  Value_ Distance_1(Vector_<Value_> const &point) const
165  {
166  return Vector_1(point).Mag();
167  }
168 
172  Value_ Distance_2(Vector_<Value_> const &point) const
173  {
174  return Vector_2(point).Mag();
175  }
176 
180  double Paramter(Vector_<Value_> const &point) const
181  {
182  // Consider the line extending the segment, parameterized as v + t (w - v).
183  // We find projection of point p onto the line.
184  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
185  return point * Vector() / Mag2();
186  }
187 
191  Vector_<Value_> Projection(Vector_<Value_> const &point) const
192  {
193  return point_1_ + Vector() * Paramter(point);
194  }
195 
197 
201  friend auto &operator<<(std::ostream &stream, Line const &line)
202  {
203  stream << line.point_1_ << line.point_2_;
204  return stream;
205  }
206 
207 private:
208 
209  Vector_<Value_> point_1_; // v
210 
211  Vector_<Value_> point_2_; // w
212 
213 };
214 
215 }
Line(Vector_< Value_ > const &point_1, Vector_< Value_ > const &point_2)
Constructor accepting two vectors.
Definition: Line.hh:35
Value_ DistanceToSegment(Vector_< Value_ > const &point) const
Distance of point to line segment spanned between the two vectors.
Definition: Line.hh:89
Vector_< Value_ > Vector() const
Vector between the two points.
Definition: Line.hh:117
Value_ Mag() const
Magnitude of the vector.
Definition: Line.hh:133
double Paramter(Vector_< Value_ > const &point) const
Parameter along the line segment between the two points.
Definition: Line.hh:180
Vector_< Value_ > Projection(Vector_< Value_ > const &point) const
Projection of the point onto the line.
Definition: Line.hh:191
friend auto & operator<<(std::ostream &stream, Line const &line)
Output stream operator.
Definition: Line.hh:201
Vector_< Value_ > Vector_1(Vector_< Value_ > const &point) const
vector between a point and the first point
Definition: Line.hh:148
Boosted Collider Analysis.
Definition: Analysis.hh:15
Vector_< Value_ > const & Point_1() const
Vector of first point.
Definition: Line.hh:50
Vector_< Value_ > Point_1()
Vector of first point.
Definition: Line.hh:58
Vector_< Value_ > Point_2()
Vector of second point.
Definition: Line.hh:74
Value_ Distance_1(Vector_< Value_ > const &point) const
Distance between the point and the first point.
Definition: Line.hh:164
Vector_< Value_ > Vector_2(Vector_< Value_ > const &point) const
vector between a point and the second point
Definition: Line.hh:156
Value_ Distance_2(Vector_< Value_ > const &point) const
Distance between the point and the second point.
Definition: Line.hh:172
Vector_< Value_ > const & Point_2() const
Vector of second point.
Definition: Line.hh:66
Value_Square Mag2() const
Square of the magnitude of the vector.
Definition: Line.hh:125
Value_ DistanceToLine(Vector_< Value_ > const &point) const
Distance of point to the line.
Definition: Line.hh:101
Line between two points.
Definition: Line.hh:15
Line()
Default constructor.
Definition: Line.hh:30