vgl_line_segment_2d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_line_segment_2d.hxx
2 #ifndef vgl_line_segment_2d_hxx_
3 #define vgl_line_segment_2d_hxx_
4 
5 #include <iostream>
6 #include <cmath>
7 #include <string>
8 #include "vgl_line_segment_2d.h"
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 // stream operators
13 template <class Type>
14 std::ostream& operator<<(std::ostream& s, vgl_line_segment_2d<Type> const & p)
15 {
16  return s << "<vgl_line_segment_2d " << p.point1() << " to " << p.point2() << " >";
17 }
18 
19 template <class Type>
20 std::istream& operator>>(std::istream& s, vgl_line_segment_2d<Type>& p)
21 {
22  std::string temp;
23  s >> std::skipws >> std::ws;
24  char c = s.peek();
25  if(c=='<')
26  s >> temp;
27  vgl_point_2d<Type> p1, p2;
28  s >> p1>>std::ws;
29  c = s.peek();
30  if(c=='t')
31  s >> temp;
32  s >> std::ws >> p2 >> temp;
33  p.set(p1, p2);
34  return s;
35 }
36 
37 template <class Type>
39 {
40  return point1_.y()-point2_.y();
41 }
42 
43 template <class Type>
45 {
46  return point2_.x()-point1_.x();
47 }
48 
49 template <class Type>
51 {
52  return point1_.x()*point2_.y()-point2_.x()*point1_.y();
53 }
54 
55 template <class Type>
57 {
58  vgl_vector_2d<Type> v(point2_.x()-point1_.x(),point2_.y()-point1_.y());
59  return normalized(v);
60 }
61 
62 template <class Type>
64 {
65  vgl_vector_2d<Type> v(point1_.y()-point2_.y(),point2_.x()-point1_.x());
66  return normalized(v);
67 }
68 
69 template <class Type>
71 {
72  static const double deg_per_rad = 45.0/std::atan2(1.0,1.0);
73  double dy = point2_.y()-point1_.y();
74  double dx = point2_.x()-point1_.x();
75  // do special cases separately, to avoid rounding errors:
76  if (dx == 0) return dy<0 ? -90.0 : 90.0;
77  if (dy == 0) return dx<0 ? 180.0 : 0.0;
78  if (dy == dx) return dy<0 ? -135.0 : 45.0;
79  if (dy+dx == 0) return dy<0 ? -45.0 : 135.0;
80  // general case:
81  return deg_per_rad * std::atan2(dy,dx);
82 }
83 
84 template <class Type>
86 {
87  double dy = point2_.y()-point1_.y();
88  double dx = point2_.x()-point1_.x();
89  return std::atan2(dy,dx);
90 }
91 
92 #undef VGL_LINE_SEGMENT_2D_INSTANTIATE
93 #define VGL_LINE_SEGMENT_2D_INSTANTIATE(Type) \
94 template class vgl_line_segment_2d<Type >;\
95 template std::istream& operator>>(std::istream&, vgl_line_segment_2d<Type >&);\
96 template std::ostream& operator<<(std::ostream&, vgl_line_segment_2d<Type > const&)
97 
98 #endif // vgl_line_segment_2d_hxx_
Represents a 2D line segment using two points.
Definition: vgl_fwd.h:18
v normalized(v const &a)
Return a normalised version of a.
Type b() const
Parameter b of line a*x + b*y + c = 0.
vgl_vector_2d< Type > normal() const
unit vector orthogonal to line.
Type a() const
Parameter a of line a*x + b*y + c = 0.
double slope_degrees() const
angle with the oriented horizontal line y=0, measured in 360-degrees.
Type c() const
Parameter c of line a*x + b*y + c = 0.
vgl_point_2d< Type > point2() const
The other end-point of the line segment.
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
#define v
Definition: vgl_vector_2d.h:74
vgl_vector_2d< Type > direction() const
unit vector describing line direction.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
double slope_radians() const
angle with the oriented horizontal line y=0, measured in radians.
Represents a cartesian 2D point.
Definition: vgl_area.h:7
void set(vgl_point_2d< Type > const &p1, vgl_point_2d< Type > const &p2)
Assignment.
vgl_point_2d< Type > point1() const
One end-point of the line segment.