vgl_homg_line_2d.h
Go to the documentation of this file.
1 // This is core/vgl/vgl_homg_line_2d.h
2 #ifndef vgl_homg_line_2d_h
3 #define vgl_homg_line_2d_h
4 //:
5 // \file
6 // \brief line in projective 2D space
7 // \author Don Hamilton, Peter Tu
8 //
9 // \verbatim
10 // Modifications
11 // Peter Vanroose - 6 July 2001 - Added normal(), direction() and concurrent()
12 // Peter Vanroose - 4 July 2001 - Added assertions and cstr from non-homg line
13 // Peter Vanroose - 27 June 2001 - Added operator==
14 // \endverbatim
15 
16 #include <iosfwd>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 #include <vgl/vgl_fwd.h> // forward declare vgl_homg_point_2d and vgl_line_2d
21 #include <cassert>
22 #include <vgl/vgl_vector_2d.h>
23 
24 //: Represents a homogeneous 2D line.
25 template <class T>
26 class vgl_homg_line_2d
27 {
28  //: the data associated with this line
29  T a_;
30  T b_;
31  T c_;
32 
33  public:
34 
35  // Constructors/Initializers/Destructor------------------------------------
36 
37  //: Default constructor (Line 1.y==0, the X axis)
38  inline vgl_homg_line_2d() : a_(0), b_(1), c_(0) {}
39 
40  //: Construct from three Types.
41  // The three given numbers should not be all 0
42  inline vgl_homg_line_2d(T va, T vb, T vc) : a_(va), b_(vb), c_(vc) {assert(va||vb||vc);}
43 
44  //: Construct from 3-vector.
45  // The three given numbers should not be all 0
46  inline vgl_homg_line_2d(const T v[3]) : a_(v[0]), b_(v[1]), c_(v[2]) {assert(a_||b_||c_);}
47 
48  //: Construct from non-homogeneous line
50 
51  //: Construct from two distinct points (join)
52  // The two points must be distinct!
54 
55 #if 0 // The defaults for these, as provided by the compiler, are all right:
56  // Default copy constructor
57  inline vgl_homg_line_2d(const vgl_homg_line_2d<T>& l) : a_(l.a()), b_(l.b()), c_(l.c()) {}
58 
59  // Default destructor
60  inline ~vgl_homg_line_2d() {}
61 
62  // Default assignment operator
63  inline vgl_homg_line_2d<T>& operator=(const vgl_homg_line_2d<T>& l) {
64  set(l.a(),l.b(),l.c()); return *this;
65  }
66 #endif
67 
68  //: the comparison operator
69  inline bool operator==(vgl_homg_line_2d<T> const& l) const
70  {
71  return (this==&l) ||
72  (a()*l.c()==c()*l.a() && b()*l.c()==c()*l.b() && b()*l.a()==a()*l.b());
73  }
74 
75  inline bool operator!=(vgl_homg_line_2d<T> const& other)const{return !operator==(other);}
76 
77  // Data Access-------------------------------------------------------------
78 
79  //: Parameter a of line a*x + b*y + c*w = 0
80  inline T a() const {return a_;}
81  //: Parameter b of line a*x + b*y + c*w = 0
82  inline T b() const {return b_;}
83  //: Parameter c of line a*x + b*y + c*w = 0
84  inline T c() const {return c_;}
85 
86  //: unit vector describing line direction, or (0,0) if line at infinity
88 
89  //: unit vector orthogonal to line, or (0,0) if line at infinity
91 
92  //: divide all coefficients by sqrt(a^2 + b^2)
93  void normalize();
94 
95  //: Set a b c.
96  // The three given numbers should not be all 0
97  // Note that it does not make sense to set a, b or c separately
98  inline void set(T va, T vb, T vc) {assert(va||vb||vc); a_=va; b_=vb; c_=vc;}
99 
100  //: Return true iff this line is the line at infinity
101  // This version checks (max(|a|,|b|) <= tol * |c|
102  inline bool ideal(T tol = (T)0) const
103  {
104 #define vgl_Abs(x) ((x)<0?-(x):(x)) // avoid #include of vcl_cmath.h AND vcl_cstdlib.h
105  return vgl_Abs(a()) <= tol*vgl_Abs(c()) && vgl_Abs(b()) <= tol*vgl_Abs(c());
106 #undef vgl_Abs
107  }
108 
109  //:get two points on the line
110  // These two points are normally the intersections
111  // with the Y axis and X axis, respectively. When the line is parallel to one
112  // of these, the point with y=1 or x=1, resp. are taken. When the line goes
113  // through the origin, the second point is (b, -a, 1). Finally, when the line
114  // is the line at infinity, the returned points are (1,0,0) and (0,1,0).
115  // Thus, whenever possible, the returned points are not at infinity.
117 };
118 
119 #define l vgl_homg_line_2d<T>
120 
121 //: Return true iff line is the line at infinity
122 // This version checks (max(|a|,|b|) <= tol * |c|
123 // \relatesalso vgl_homg_line_2d
124 template <class T>
125 inline bool is_ideal(l const& line, T tol = (T)0) { return line.ideal(tol); }
126 
127 //: Are three lines concurrent, i.e., do they pass through a common point?
128 // \relatesalso vgl_homg_line_2d
129 template <class T>
130 inline bool concurrent(l const& l1, l const& l2, l const& l3)
131 {
132  return l1.a()*(l2.b()*l3.c()-l3.b()*l2.c())
133  +l2.a()*(l3.b()*l1.c()-l1.b()*l3.c())
134  +l3.a()*(l1.b()*l2.c()-l2.b()*l1.c())==0;
135 }
136 
137 //: Print line equation to stream
138 // \relatesalso vgl_homg_line_2d
139 template <class T>
140 std::ostream& operator<<(std::ostream& s, l const& line);
141 
142 //: Load in line parameters from stream
143 // \relatesalso vgl_homg_line_2d
144 template <class T>
145 std::istream& operator>>(std::istream& s, l& line);
146 
147 #undef l
148 
149 #define VGL_HOMG_LINE_2D_INSTANTIATE(T) extern "please include vgl/vgl_homg_line_2d.hxx first"
150 
151 #endif // vgl_homg_line_2d_h
vgl_homg_line_2d(const T v[3])
Construct from 3-vector.
vgl_homg_line_2d(T va, T vb, T vc)
Construct from three Types.
T a() const
Parameter a of line a*x + b*y + c*w = 0.
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
void get_two_points(vgl_homg_point_2d< T > &p1, vgl_homg_point_2d< T > &p2) const
get two points on the line.
Direction vector in Euclidean 2D space, templated by type of element.
Definition: vgl_fwd.h:12
T b() const
Parameter b of line a*x + b*y + c*w = 0.
vgl_homg_line_2d()
Default constructor (Line 1.y==0, the X axis).
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.
bool operator!=(vgl_homg_line_2d< T > const &other) const
#define v
Definition: vgl_vector_2d.h:74
bool ideal(T tol=(T) 0) const
Return true iff this line is the line at infinity.
T a_
the data associated with this line.
vgl_vector_2d< double > direction() const
unit vector describing line direction, or (0,0) if line at infinity.
bool operator==(vgl_homg_line_2d< T > const &l) const
the comparison operator.
bool is_ideal(l const &line, T tol=(T) 0)
Return true iff line is the line at infinity.
direction vector in Euclidean 2D space
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
void normalize()
divide all coefficients by sqrt(a^2 + b^2).
#define l
T c() const
Parameter c of line a*x + b*y + c*w = 0.
vgl_vector_2d< double > normal() const
unit vector orthogonal to line, or (0,0) if line at infinity.
void set(T va, T vb, T vc)
Set a b c.
Represents a homogeneous 2D point.
Definition: vgl_fwd.h:8
v normalized(v const &a)
Return a normalised version of a.
#define vgl_Abs(x)