KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gr_line_segment.h
Go to the documentation of this file.
1 /* $Id: gr_line_segment.h 18301 2014-11-26 19:17:13Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2014 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
21 
22 #ifndef GR_CPP_LINE_SEGMENT_H
23 #define GR_CPP_LINE_SEGMENT_H
24 
25 #include <utility>
26 #include <iosfwd>
27 #include <l_cpp/l_readable.h>
28 #include <l_cpp/l_writeable.h>
29 #include <m_cpp/m_vector.h>
30 #include <g_cpp/g_line.h>
31 
44 namespace kjb {
45 
46 class Image;
47 
62 class Line_segment : public Readable, public Writeable
63 {
64 public:
65 
68  : Readable(),
69  Writeable(),
70  centre(3,1.0),
71  start_point(3,1.0),
72  end_point(3,1.0),
73  line()
74  {}
75 
78  double icentre_x,
79  double icentre_y,
80  double iorientation,
81  double ilength
82  );
83 
85  Line_segment(const Vector& start, const Vector& end);
86 
88  Line_segment(std::istream & in);
89 
91  Line_segment(const Line_segment & ls);
92 
94  Line_segment & operator=(const Line_segment & ls);
95 
96  bool operator ==(const Line_segment& ls) const;
97 
98  inline bool operator <(const Line_segment& ls) const
99  {
100  return (this->get_start_x() < ls.get_start_x())
101  || ( this->get_start_x() == ls.get_start_x()
102  && this->get_start_y() < ls.get_start_y()
103  );
104  }
105 
112  double x_start,
113  double x_end,
114  double islope,
115  double iintercept
116  );
117 
119  void init_vertical_segment(double y_start, double y_end, double x);
120 
122  void init_from_end_points(double x_1, double y_1, double x_2, double y_2);
123 
125  double icentre_x,
126  double icentre_y,
127  double iorientation,
128  double ilength
129  );
130 
132  inline double get_centre_x() const
133  {
134  return centre(0);
135  }
136 
138  inline double get_centre_y() const
139  {
140  return centre(1);
141  }
142 
147  bool point_outside_segment(const Vector& point) const;
148 
150  const kjb::Vector & get_centre() const { return centre; }
151 
153  inline double get_start_x() const
154  {
155  return start_point(0);
156  }
157 
159  inline double get_start_y() const
160  {
161  return start_point(1);
162  }
163 
165  const kjb::Vector & get_start() const { return start_point; }
166 
168  inline double get_end_x() const
169  {
170  return end_point(0);
171  }
172 
174  inline double get_end_y() const
175  {
176  return end_point(1);
177  }
178 
180  const kjb::Vector & get_end() const { return end_point; }
181 
187  inline double get_slope() const
188  {
189  if(is_vertical())
190  {
191  return std::numeric_limits<double>::max() - DBL_EPSILON;
192  }
193  return -line.get_a();
194  }
195 
201  inline double get_y_intercept() const
202  {
203  if(is_vertical())
204  {
205  throw KJB_error("Vertical segment, there is no y intercept");
206  }
207  return -line.get_c();
208  }
209 
215  inline double get_x_intercept() const
216  {
217  if(is_horizontal())
218  {
219  throw KJB_error("Horizontal segment, there is no x intercept");
220  }
221  return -line.get_c()/line.get_a();
222  }
223 
224  inline const Vector & get_line_params() const
225  {
226  return line.get_params();
227  }
228 
229  inline const Line & get_line() const
230  {
231  return line;
232  }
233 
235  inline double get_orientation() const
236  {
237  return orientation;
238  }
239 
241  inline double get_length() const
242  {
243  return length;
244  }
245 
247  void read(std::istream& in);
248 
250  void write(std::ostream& out) const;
251 
253  void draw(
254  kjb::Image & img,
255  double ir,
256  double ig,
257  double ib,
258  double width = 1.0
259  ) const;
260 
262  void randomly_color(kjb::Image & img, double width = 1.0) const;
263 
270  bool get_intersection
271  (
272  const Line_segment& line,
274  ) const;
275 
277  double get_distance_from_point (const kjb::Vector& point) const;
278 
280  double get_angle_between_line (const Line_segment& line) const;
281 
290  double find_t(const kjb::Vector & point) const;
291 
293  double get_distance_from_point (
294  const kjb::Vector& point,
295  double * perp_dist
296  ) const;
297 
299  inline double get_dx() const
300  {
301  return fabs(end_point(0) - start_point(0) );
302  }
303 
305  inline double get_dy() const {return (end_point(1) - start_point(1) ); }
306 
307  friend std::ostream& operator<<(std::ostream&, const Line_segment&);
308 
310  inline bool is_horizontal() const
311  {
312  return (fabs(orientation) <= FLT_EPSILON );
313  }
314 
316  inline bool is_vertical() const
317  {
318  //This is a little hacky but I could not find a more elegant solution
319  //if(fabs(line.get_b()) <= FLT_EPSILON)
320  if(fabs(get_orientation()-M_PI_2) <= FLT_EPSILON)
321  {
322  return true;
323  }
324  return false;
325  }
326 
329  void get_direction(kjb::Vector & idirection) const;
330 
335 
340  static std::pair<Vector, Vector> project_line_segment_onto_line
341  (
342  const Line_segment& segment,
343  const Line& iline
344  );
345 
356  (
357  const Line_segment& segment_project,
358  const Line_segment& segment_target,
359  std::pair<Vector, Vector>& projected_points,
360  double& length_inside,
361  double& length_outside
362  );
363 
367  static bool less_than (const Vector& point1, const Vector& point2);
368 
369 
370  static bool collision_detection
371  (
372  double start_1,
373  double end_1,
374  double start_2,
375  double end_2,
376  int & direction,
377  double & delta
378  );
379 
381  (
382  double start_1,
383  double end_1,
384  double start_2,
385  double end_2,
386  int & direction,
387  double & delta,
388  int idirection
389  );
390 
391  static double get_overlap
392  (
393  double start_1,
394  double end_1,
395  double start_2,
396  double end_2
397  );
398 
399  bool is_collinear
400  (
401  const Line_segment & ls,
402  double collinear_threshold
403  ) const;
404 
405 protected:
406 
413  void compute_extrema();
414 
420 
421 
424 
428  double orientation;
429 
431  double length;
432 
440 
447 
449 
450 };
451 
452 std::ostream& operator<<(std::ostream& out, const Line_segment& ls);
453 
454 } // namespace kjb
455 #endif /* GR_CPP_LINE_SEGMENT_H */
Line_segment & operator=(const Line_segment &ls)
Assignment operator.
Definition: gr_line_segment.cpp:115
void get_direction(kjb::Vector &idirection) const
Returns a vector representing the normalized direction of this line_segment.
Definition: gr_line_segment.cpp:677
void write(std::ostream &out) const
Writes this Line segment to an output stream.
Definition: gr_line_segment.cpp:346
kjb::Vector start_point
Definition: gr_line_segment.h:438
Abstract class to write this object to an output stream.
Definition: l_writeable.h:41
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
bool is_collinear(const Line_segment &ls, double collinear_threshold) const
Definition: gr_line_segment.cpp:1074
Abstract class to read this object from an input stream.
Definition: l_readable.h:39
double get_end_y() const
Returns the y-coordinate of the rightmost point of the segment.
Definition: gr_line_segment.h:174
bool operator==(const Line_segment &ls) const
Definition: gr_line_segment.cpp:133
void randomly_color(kjb::Image &img, double width=1.0) const
Randomly colors this line segment on an image.
Definition: gr_line_segment.cpp:438
bool get_intersection(const Line_segment &line, kjb::Vector &point) const
Finds the intersection point of the this line_segment and the input Line_segment line. Return false if these two lines are parallel and therefore do not intersect.
Definition: gr_line_segment.cpp:717
static bool collision_detection_with_direction(double start_1, double end_1, double start_2, double end_2, int &direction, double &delta, int idirection)
Definition: gr_line_segment.cpp:986
double get_angle_between_line(const Line_segment &line) const
Returns the angle between this line segment and the input line.
Definition: gr_line_segment.cpp:650
bool is_vertical() const
Function object of testing whehter a line is horizontal.
Definition: gr_line_segment.h:316
double orientation
Orientation of this line segment, defined as the angle between the x axis and the segment...
Definition: gr_line_segment.h:428
void init_vertical_segment(double y_start, double y_end, double x)
Initialize a vertical Line_segment from y_start, y_end, x coord.
Definition: gr_line_segment.cpp:235
static Matrix _line_segment_rotation
Definition: gr_line_segment.h:448
const kjb::Vector & get_centre() const
Returns the mid-point.
Definition: gr_line_segment.h:150
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
void init_from_centre_and_orientation(double icentre_x, double icentre_y, double iorientation, double ilength)
Definition: gr_line_segment.cpp:158
Declarations for Line class.
double get_start_y() const
Returns the y-coordinate of the leftmost point of the segment.
Definition: gr_line_segment.h:159
double get_centre_x() const
Returns the x-coordinate of the mid-point.
Definition: gr_line_segment.h:132
const Line & get_line() const
Definition: gr_line_segment.h:229
double get_distance_from_point(const kjb::Vector &point) const
Returns distance between this line segment and the input point.
Definition: gr_line_segment.cpp:559
double get_orientation() const
Returns the orientation.
Definition: gr_line_segment.h:235
bool is_line_segment_consistent()
Definition: gr_line_segment.cpp:459
void draw(kjb::Image &img, double ir, double ig, double ib, double width=1.0) const
Draws this line segment.
Definition: gr_line_segment.cpp:423
REAL * point
Definition: triangle.c:537
void init_from_slope_and_intercept(double x_start, double x_end, double islope, double iintercept)
Initialize from x_start, x_end, slope, y_axis intercept.
Definition: gr_line_segment.cpp:183
double get_a() const
Returns the a parameter of the line as in ax + by + c = 0.
Definition: g_line.h:71
double get_slope() const
Returns the slope of the line segment.
Definition: gr_line_segment.h:187
double get_start_x() const
Returns the x-coordinate of the leftmost point of the segment.
Definition: gr_line_segment.h:153
static std::pair< Vector, Vector > project_line_segment_onto_line(const Line_segment &segment, const Line &iline)
Project a line segment onto a line.
Definition: gr_line_segment.cpp:731
const kjb::Vector & get_params() const
Returns the line parameters.
Definition: g_line.h:89
x
Definition: APPgetLargeConnectedEdges.m:100
void read(std::istream &in)
Reads this Line segment from an input stream.
Definition: gr_line_segment.cpp:298
double get_centre_y() const
Returns the y-coordinate of the mid-point.
Definition: gr_line_segment.h:138
static double get_overlap(double start_1, double end_1, double start_2, double end_2)
Definition: gr_line_segment.cpp:917
double get_dx() const
Returns the dx of this segment.
Definition: gr_line_segment.h:299
double find_t(const kjb::Vector &point) const
Definition: gr_line_segment.cpp:593
kjb::Vector centre
X coordinate of the centre of this line segment.
Definition: gr_line_segment.h:423
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
void init_from_end_points(double x_1, double y_1, double x_2, double y_2)
Initialize a new Line_segment from position of its end points.
Definition: gr_line_segment.cpp:271
std::ofstream & operator<<(std::ofstream &out, const Quaternion &q)
Definition: turntable_camera.cpp:77
const kjb::Vector & get_end() const
Returns the rightmost point of the segment.
Definition: gr_line_segment.h:180
Exception often thrown when wrapped C functions return error codes.
Definition: l_exception.h:262
const kjb::Vector & get_start() const
Returns the leftmost point of the segment.
Definition: gr_line_segment.h:165
static bool less_than(const Vector &point1, const Vector &point2)
Returns true if point1.x < point2.x || (point1.x == point2.x && point1.y < point2.y.
Definition: gr_line_segment.cpp:806
double get_length() const
Returns the length.
Definition: gr_line_segment.h:241
static bool project_line_segment_onto_line_segment(const Line_segment &segment_project, const Line_segment &segment_target, std::pair< Vector, Vector > &projected_points, double &length_inside, double &length_outside)
Project a line segment onto a line segment.
Definition: gr_line_segment.cpp:763
Line line
Definition: gr_line_segment.h:446
bool is_horizontal() const
Function object of testing whehter a line is horizontal.
Definition: gr_line_segment.h:310
double get_c() const
Returns the c parameter of the line as in ax + by + c = 0.
Definition: g_line.h:83
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Line_segment()
Constructor without initializations.
Definition: gr_line_segment.h:67
kjb::Vector end_point
Definition: gr_line_segment.h:439
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
bool operator<(const Line_segment &ls) const
Definition: gr_line_segment.h:98
bool point_outside_segment(const Vector &point) const
Check whether point is inside the ending points of this segment Note point is collinear with this lin...
Definition: gr_line_segment.cpp:544
double get_end_x() const
Returns the x-coordinate of the rightmost point of the segment.
Definition: gr_line_segment.h:168
const Vector & get_line_params() const
Definition: gr_line_segment.h:224
static bool collision_detection(double start_1, double end_1, double start_2, double end_2, int &direction, double &delta)
Definition: gr_line_segment.cpp:817
double get_x_intercept() const
Returns the x-intercept of the line segment.
Definition: gr_line_segment.h:215
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
double length
length of the line segment
Definition: gr_line_segment.h:431
double get_dy() const
Returns the dy of this segment.
Definition: gr_line_segment.h:305
Class to manipulate a line segment The class is parametrized in terms the position of the centre...
Definition: gr_line_segment.h:62
void compute_line_parameters()
Definition: gr_line_segment.cpp:397
friend std::ostream & operator<<(std::ostream &, const Line_segment &)
Definition: gr_line_segment.cpp:691
double get_y_intercept() const
Returns the y-intercept of the line segment.
Definition: gr_line_segment.h:201
void compute_extrema()
Computes the extrema of this line segments from centre, orientation, and length. This information is ...
Definition: gr_line_segment.cpp:358