KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
g_line.h
Go to the documentation of this file.
1 /* $Id $ */
2 
15 #ifndef EDGE_LINE_H
16 #define EDGE_LINE_H
17 
18 #include "m_cpp/m_vector.h"
19 #include "m_cpp/m_vector_d.h"
20 #include "l_cpp/l_exception.h"
21 #include <limits>
22 
23 namespace kjb {
30 class Line
31 {
32  public:
33 
35  Line() : line_params(3, 1.0) {}
36 
38  Line(const kjb::Vector & point_1, const kjb::Vector & point_2);
39 
42  Line(double a, double b, double c) : line_params(3, 1.0)
43  {
44  line_params(0) = a;
45  line_params(1) = b;
46  line_params(2) = c;
47  }
48 
51  Line(const kjb::Vector & iparams)
52  {
53  if(iparams.size() != 3)
54  {
55  throw kjb::Illegal_argument("Line constructor, input parameter vector must have size 3");
56  }
57  line_params = iparams;
58  }
59 
61  Line(const Line & l) : line_params(l.line_params) { }
62 
64  Line & operator=(const Line & l)
65  {
66  line_params = l.line_params;
67  return (*this);
68  }
69 
71  inline double get_a() const
72  {
73  return line_params(0);
74  }
75 
77  inline double get_b() const
78  {
79  return line_params(1);
80  }
81 
83  inline double get_c() const
84  {
85  return line_params(2);
86  }
87 
89  inline const kjb::Vector & get_params() const
90  {
91  return line_params;
92  }
93 
95  double compute_y_coordinate(double ix)
96  {
97  if( fabs(line_params(1)) < FLT_EPSILON )
98  {
99  throw kjb::KJB_error("Cannot compute y coordinate for a vertical line");
100  }
101  return (-line_params(0)*ix - line_params(2))/line_params(1);
102  }
103 
105  inline void set_a(double ia)
106  {
107  line_params(0) = ia;
108  }
109 
111  inline void set_b(double ib)
112  {
113  line_params(1) = ib;
114  }
115 
117  inline void set_c(double ic)
118  {
119  line_params(2) = ic;
120  }
121 
123  inline void set_line_params(const kjb::Vector & iparams)
124  {
125  if(iparams.size() != 3)
126  {
127  throw kjb::Illegal_argument("Line constructor, input parameter vector must have size 3");
128  }
129  line_params = iparams;
130  }
131 
132  inline bool point_is_on_line(const Vector & point) const
133  {
134  if(point.size() < 2)
135  {
136  throw kjb::Illegal_argument("point_is_on_line(), point's size < 2");
137  }
138 
139  return fabs(get_a()*point(0)+get_b()*point(1)+get_c()) < FLT_EPSILON;
140  }
141 
145  static bool find_line_intersection(const Line & l1, const Line & l2, kjb::Vector & ints);
146 
148  double find_distance_to_point(const kjb::Vector & point) const;
149 
154 
155  private:
156 
158  kjb::Vector line_params;
159 
160 };
161 
162 
164 (
165  kjb::Vector & intersection,
166  double &t,
167  const kjb::Vector & point,
168  const kjb::Vector & direction,
169  const kjb::Vector & plane
170 );
171 
172 
176 template <class VectorType>
178 (
179  const VectorType& line_point,
180  const VectorType& line_direction,
181  const VectorType& plane_point,
182  const VectorType& plane_normal
183 )
184 {
185  double numerator = kjb::dot((plane_point - line_point), plane_normal);
186  double denominator = kjb::dot(line_direction, plane_normal);
187 
188  if(fabs(denominator) < FLT_EPSILON)
189  {
190  if(fabs(numerator) > FLT_EPSILON)
191  return std::numeric_limits<double>::infinity();
192  else
193  return std::numeric_limits<double>::quiet_NaN();
194  }
195  return numerator/denominator;
196 }
197 
198 
221 inline double intersect_line_with_plane
222 (
223  const kjb::Vector& line_point,
224  const kjb::Vector& line_direction,
225  const kjb::Vector& plane_point,
226  const kjb::Vector& plane_normal
227 )
228 {
230  line_point,
231  line_direction,
232  plane_point,
233  plane_normal);
234 }
235 
239 template <std::size_t D>
240 inline double intersect_line_with_plane
241 (
242  const kjb::Vector_d<D>& line_point,
243  const kjb::Vector_d<D>& line_direction,
244  const kjb::Vector_d<D>& plane_point,
245  const kjb::Vector_d<D>& plane_normal
246 )
247 {
249  line_point,
250  line_direction,
251  plane_point,
252  plane_normal);
253 }
254 
262 inline
263 Vector project_point_onto_line(const Vector& A, const Vector& B, const Vector& P)
264 {
265  IFT(A.size() == B.size() && A.size() == P.size(), Illegal_argument,
266  "Cannot project point onto line: dimensions are wrong.");
267 
268  Vector AP = P - A;
269  Vector AB = B - A;
270 
271  double s = dot(AP, AB) / dot(AB, AB);
272  return A + s*AB;
273 }
274 
275 } // namespace kjb
276 
277 #endif
double compute_y_coordinate(double ix)
computes the y coordinate for the input x coordinate
Definition: g_line.h:95
double intersect_line_with_plane_dispatch_(const VectorType &line_point, const VectorType &line_direction, const VectorType &plane_point, const VectorType &plane_normal)
Definition: g_line.h:178
size_type size() const
Alias to get_length(). Required to comply with stl Container concept.
Definition: m_vector.h:510
Line & operator=(const Line &l)
Assignment operator.
Definition: g_line.h:64
Line(double a, double b, double c)
Constructs a new Line_segment from line parameters a, b, c as in ax + by + c = 0. ...
Definition: g_line.h:42
Line(const Line &l)
Copy constructor.
Definition: g_line.h:61
bool intersect_3D_line_with_plane(kjb::Vector &intersection, double &t, const kjb::Vector &point, const kjb::Vector &direction, const kjb::Vector &plane)
Definition: g_line.cpp:181
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void set_b(double ib)
Sets the b parameter of the line as in ax + by + c = 0.
Definition: g_line.h:111
#define IFT(a, ex, msg)
Definition: l_exception.h:101
Line()
Constructor without initializations.
Definition: g_line.h:35
REAL * point
Definition: triangle.c:537
double get_a() const
Returns the a parameter of the line as in ax + by + c = 0.
Definition: g_line.h:71
bool point_is_on_line(const Vector &point) const
Definition: g_line.h:132
const kjb::Vector & get_params() const
Returns the line parameters.
Definition: g_line.h:89
void set_a(double ia)
Sets the a parameter of the line as in ax + by + c = 0.
Definition: g_line.h:105
Vector project_point_onto_line(const Vector &A, const Vector &B, const Vector &P)
Project a point onto a line, in any dimension.
Definition: g_line.h:263
Vector project_point_onto_line(const Vector &point) const
Project a point onto a line.
Definition: g_line.cpp:153
double find_distance_to_point(const kjb::Vector &point) const
Finds the perpendicular distance between a line and a point.
Definition: g_line.cpp:137
Parametric representation of a 2D line in terms of three parameters (a,b,c) (as in ax+by+c = 0)...
Definition: g_line.h:30
double get_b() const
Returns the b parameter of the line as in ax + by + c = 0.
Definition: g_line.h:77
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
Line(const kjb::Vector &iparams)
Constructs a new Line_segment from line parameters vector [a, b, c] as in ax + by + c = 0...
Definition: g_line.h:51
Object thrown when an argument to a function is not acceptable.
Definition: l_exception.h:377
double intersect_line_with_plane(const kjb::Vector &line_point, const kjb::Vector &line_direction, const kjb::Vector &plane_point, const kjb::Vector &plane_normal)
Definition: g_line.h:222
double get_c() const
Returns the c parameter of the line as in ax + by + c = 0.
Definition: g_line.h:83
void set_c(double ic)
Sets the c parameter of the line as in ax + by + c = 0.
Definition: g_line.h:117
Definition: g_quaternion.h:37
long int dot(const Int_vector &op1, const Int_vector &op2)
Returns dot product of this and op2.
Definition: l_int_vector.h:1532
Support for error handling exception classes in libKJB.
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
void set_line_params(const kjb::Vector &iparams)
Sets the line parameters [a,b,c] of the line as in ax + by + c = 0.
Definition: g_line.h:123
static bool find_line_intersection(const Line &l1, const Line &l2, kjb::Vector &ints)
Find the intersection between two lines, Returns false if the lines are parallel. ...
Definition: g_line.cpp:99