vgl_line_2d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_line_2d.h
2 #ifndef vgl_line_2d_h_
3 #define vgl_line_2d_h_
4 //:
5 // \file
6 // \author Don Hamilton, Peter Tu, Peter Vanroose, Francois BERTEL, Franck Bettinger
7 // \date 2000-02-16 Don HAMILTON, Peter TU - Creation
8 //
9 // \verbatim
10 // Modifications
11 // 2000-02-29 Peter Vanroose Several minor fixes
12 // 2000-05-05 Francois BERTEL Several minor bugs fixed
13 // 2000-05-09 Peter Vanroose dist_origin() re-implemented
14 // 2000-12-01 Peter Vanroose moved dist_origin() to vgl_distance.h
15 // 2001-03-19 Franck Bettinger added Manchester binary IO code
16 // 2001-06-27 Peter Vanroose Added operator==
17 // 2001-07-05 Peter Vanroose direction, normal in terms of vgl_vector_2d
18 // 2001-07-06 Peter Vanroose Added concurrent(), added assertions
19 // 2009-05-21 Peter Vanroose istream operator>> re-implemented
20 // \endverbatim
21 
22 #include <iosfwd>
23 #ifdef _MSC_VER
24 # include <vcl_msvc_warnings.h>
25 #endif
26 #include <cassert>
27 #include <vgl/vgl_fwd.h> // forward declare vgl_point_2d and vgl_homg_line_2d
28 #include <vgl/vgl_vector_2d.h>
29 
30 //: Represents a Euclidean 2D line
31 // An interface for the line coefficients, [a,b,c], is provided in terms of the
32 // standard implicit line equation: a*x + b*y + c = 0
33 template <class Type>
34 class vgl_line_2d
35 {
36  // the data associated with this point
37  Type a_;
38  Type b_;
39  Type c_;
40 
41  public:
42  //: Default constructor (Line 1.y==0, the X axis)
43  inline vgl_line_2d() : a_(0), b_(1), c_(0) {}
44 
45  //: Construct a vgl_line_2d from its equation, three Types.
46  // The values of a and b should not be both zero.
47  inline vgl_line_2d(Type ta, Type tb, Type tc) :a_(ta),b_(tb),c_(tc) { assert(ta||tb); }
48 
49  //: Construct from its equation, a 3-vector.
50  // The values v[0] and v[1] should not be both zero.
51  inline vgl_line_2d(const Type v[3]):a_(v[0]),b_(v[1]),c_(v[2]) { assert(a_||b_); }
52 
53  //: Construct from homogeneous description of line
54  // The line l should not be the line at infinity.
56 
57  //: Construct from two distinct points (join)
58  // The two points must be distinct!
59  vgl_line_2d (vgl_point_2d<Type> const& p1, vgl_point_2d<Type> const& p2);
60 
61  //: Construct from one point and one vector
63 
64 #if 0 // use compiler defaults for these
65  // Default destructor
66  inline ~vgl_line_2d () {}
67 
68  // Default assignment operator
69  inline vgl_line_2d<Type>& operator=(const vgl_line_2d<Type>& l)
70  { set(l.a(),l.b(),l.c()); return *this; }
71 #endif
72 
73  //: the comparison operator
74  inline bool operator==(vgl_line_2d<Type> const& l) const
75  {
76  return (this==&l) ||
77  (a()*l.c()==c()*l.a() && b()*l.c()==c()*l.b() && b()*l.a()==a()*l.b());
78  }
79 
80  inline bool operator!=(vgl_line_2d<Type>const& other) const { return !operator==(other); }
81 
82  //: angle with the horizontal line y=0, measured in radians.
83  // Returns values between -pi and pi, i.e., the lines x-y=0 and y-x=0
84  // return different values (pi/4 and -3pi/4 respectively) although these
85  // lines are identical.
86  double slope_radians() const;
87 
88  //: angle with the horizontal line y=0, measured in 360-degrees.
89  // Returns values between -180 and 180, i.e., the lines x-y=0 and y-x=0
90  // return different values (45 and -135 respectively) although these
91  // lines are identical.
92  double slope_degrees() const;
93 
94  // Data Access-------------------------------------------------------------
95 
96  //: Parameter a of line a*x + b*y + c = 0
97  inline Type a() const {return a_;}
98  //: Parameter b of line a*x + b*y + c = 0
99  inline Type b() const {return b_;}
100  //: Parameter c of line a*x + b*y + c = 0
101  inline Type c() const {return c_;}
102 
103  //: unit vector describing line direction
105  { return normalized(vgl_vector_2d<Type>(b_,-a_)); }
106 
107  //: unit vector orthogonal to line
109  { return normalized(vgl_vector_2d<Type>(a_,b_)); }
110 
111  //: normalize the line coefficients s.t. a^2 + b^2 = 1
112  bool normalize();
113 
114  //: Set a b c.
115  // The values of a and b should not be both zero.
116  // Note that it does not make sense to set a, b or c separately
117  inline void set(Type ta, Type tb, Type tc) { assert(ta||tb); a_=ta; b_=tb; c_=tc; }
118 
119  //: Return true iff this line is the line at infinity
120  // This always returns "false"
121  inline bool ideal(Type = (Type)0) const { return false; }
122 
123  //: Get two points on the line; normally the intersection with X and Y axes.
124  // When the line is parallel to one of these,
125  // the point with \a y=1 or \a x=1, resp. are taken. When the line goes
126  // through the origin, the second point is (b, -a).
128 };
129 
130 #define l vgl_line_2d<Type>
131 
132 //: Return true iff line is the line at infinity
133 // \relatesalso vgl_line_2d
134 template <class Type> inline
135 bool is_ideal(l const&, Type = (Type)0) { return false; }
136 
137 //: Are three lines concurrent, i.e., do they pass through a common point?
138 // \relatesalso vgl_line_2d
139 template <class Type> inline
140 bool concurrent(l const& l1, l const& l2, l const& l3)
141 {
142  return l1.a()*(l2.b()*l3.c()-l3.b()*l2.c())
143  +l2.a()*(l3.b()*l1.c()-l1.b()*l3.c())
144  +l3.a()*(l1.b()*l2.c()-l2.b()*l1.c())==0;
145 }
146 
147 //: Write line description to stream: "<vgl_line_2d ax+by+c>"
148 // \relatesalso vgl_line_2d
149 template <class Type>
150 std::ostream& operator<<(std::ostream& s, l const& line);
151 
152 //: Read in three line parameters from stream
153 // Either just reads three blank-separated numbers,
154 // or reads three comma-separated numbers,
155 // or reads three numbers in parenthesized form "(123, 321, -456)"
156 // or reads a formatted line equation "123x+321y-456=0"
157 // \relatesalso vgl_line_2d
158 template <class Type>
159 std::istream& operator>>(std::istream& s, l& line);
160 
161 #undef l
162 
163 #define VGL_LINE_2D_INSTANTIATE(T) extern "please include vgl/vgl_line_2d.hxx first"
164 
165 #endif // vgl_line_2d_h_
bool operator!=(vgl_line_2d< Type >const &other) const
Definition: vgl_line_2d.h:80
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
vgl_line_2d()
Default constructor (Line 1.y==0, the X axis).
Definition: vgl_line_2d.h:43
void set(Type ta, Type tb, Type tc)
Set a b c.
Definition: vgl_line_2d.h:117
bool concurrent(l const &l1, l const &l2, l const &l3)
Are three lines concurrent, i.e., do they pass through a common point?.
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
bool operator==(vgl_line_2d< Type > const &l) const
the comparison operator.
Definition: vgl_line_2d.h:74
#define l
Definition: vgl_line_2d.h:130
bool normalize()
normalize the line coefficients s.t. a^2 + b^2 = 1.
Definition: vgl_line_2d.hxx:79
bool ideal(Type=(Type) 0) const
Return true iff this line is the line at infinity.
Definition: vgl_line_2d.h:121
Represents a Euclidean 2D line.
Definition: vgl_fwd.h:16
void get_two_points(vgl_point_2d< Type > &p1, vgl_point_2d< Type > &p2) const
Get two points on the line; normally the intersection with X and Y axes.
Definition: vgl_line_2d.hxx:50
vgl_vector_2d< Type > normal() const
unit vector orthogonal to line.
Definition: vgl_line_2d.h:108
vgl_line_2d(const Type v[3])
Construct from its equation, a 3-vector.
Definition: vgl_line_2d.h:51
Type a() const
Parameter a of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:97
bool is_ideal(l const &line, T tol=(T) 0)
Return true iff line is the line at infinity.
vgl_line_2d(Type ta, Type tb, Type tc)
Construct a vgl_line_2d from its equation, three Types.
Definition: vgl_line_2d.h:47
direction vector in Euclidean 2D space
double slope_radians() const
angle with the horizontal line y=0, measured in radians.
Definition: vgl_line_2d.hxx:73
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
Type b() const
Parameter b of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:99
vgl_vector_2d< Type > direction() const
unit vector describing line direction.
Definition: vgl_line_2d.h:104
Represents a cartesian 2D point.
Definition: vgl_area.h:7
Type c() const
Parameter c of line a*x + b*y + c = 0.
Definition: vgl_line_2d.h:101
double slope_degrees() const
angle with the horizontal line y=0, measured in 360-degrees.
Definition: vgl_line_2d.hxx:60
v normalized(v const &a)
Return a normalised version of a.