vgl_homg_line_2d.hxx
Go to the documentation of this file.
1 // This is core/vgl/vgl_homg_line_2d.hxx
2 #ifndef vgl_homg_line_2d_hxx_
3 #define vgl_homg_line_2d_hxx_
4 //:
5 // \file
6 
7 #include <cmath>
8 #include <iostream>
9 #include "vgl_homg_line_2d.h"
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 #include <cassert>
14 #include <vgl/vgl_homg_point_2d.h>
15 #include <vgl/vgl_line_2d.h>
16 
17 template <class Type>
19  : a_(l.a()) , b_(l.b()) , c_(l.c())
20 {
21 }
22 
23 //: get two points on the line.
24 // These two points are normally the intersections with the Y axis and X axis,
25 // respectively. When the line is parallel to one of these, the point with
26 // \a y/w=1 or \a x/w=1, resp. are taken. When the line goes through the origin,
27 // the second point is (b, -a, 1). Finally, when the line is the line at
28 // infinity, the returned points are (1,0,0) and (0,1,0).
29 //
30 // Thus, whenever possible, the returned points are not at infinity.
31 //
32 template <class Type>
34 {
35  if ( b() == 0) p1.set(-c(), a(), a());
36  else p1.set(0, -c(), b());
37  if ( a() == 0) p2.set(b(), -c(), b());
38  else if ( c() == 0) p2.set(b(), -a(), 1);
39  else p2.set(-c(), 0, a());
40 }
41 
42 template <class Type>
44  vgl_homg_point_2d<Type> const& p2)
45 {
46  set(p1.y()*p2.w()-p1.w()*p2.y(),
47  p1.w()*p2.x()-p1.x()*p2.w(),
48  p1.x()*p2.y()-p1.y()*p2.x());
49  assert(a_||b_||c_); // given points should be different
50 }
51 
52 #define vp(os,v,s) { (os)<<' '; if ((v)>0) (os)<<'+';\
53  if ((v)&&!(s)[0]) (os)<<(v); else { \
54  if ((v)==-1) (os)<<'-';\
55  else if ((v)!=0&&(v)!=1) (os)<<(v);\
56  if ((v)!=0) (os)<<' '<<(s); } }
57 
58 //: Print line equation to stream
59 template <class Type>
60 std::ostream& operator<<(std::ostream& os, vgl_homg_line_2d<Type>const& l)
61 {
62  os << "<vgl_homg_line_2d"; vp(os,l.a(),"x"); vp(os,l.b(),"y"); vp(os,l.c(),"w");
63  return os << " = 0 >";
64 }
65 
66 #undef vp
67 
68 //: Load in line parameters from stream
69 template <class Type>
70 std::istream& operator>>(std::istream& is, vgl_homg_line_2d<Type>& p)
71 {
72  Type a,b,c;
73  is >> a >> b >> c;
74  p.set(a,b,c);
75  return is;
76 }
77 
78 template <class Type>
80 {
81  double sum = a_*a_ + b_*b_;
82  double den = std::sqrt(sum);
83  if (den<1.0e-8)//don't normalize ideal line
84  return;
85  double an= (double)a()/den;
86  double bn= (double)b()/den;
87  double cn= (double)c()/den;
88  //standardize so that a is positive unless a is smaller than b, then
89  //standardize the sign of b
90  if (std::fabs(an)>std::fabs(bn))
91  if (an>0)
92  {
93  a_ = (Type)an;
94  b_ = (Type)bn;
95  c_ = (Type)cn;
96  }
97  else
98  {
99  a_ = -(Type)an;
100  b_ = -(Type)bn;
101  c_ = -(Type)cn;
102  }
103  else
104  if (bn>0)
105  {
106  a_ = (Type)an;
107  b_ = (Type)bn;
108  c_ = (Type)cn;
109  }
110  else
111  {
112  a_ = -(Type)an;
113  b_ = -(Type)bn;
114  c_ = -(Type)cn;
115  }
116 
117  return;
118 }
119 
120 #undef VGL_HOMG_LINE_2D_INSTANTIATE
121 #define VGL_HOMG_LINE_2D_INSTANTIATE(T) \
122 template class vgl_homg_line_2d<T >; \
123 template std::ostream& operator<<(std::ostream&, vgl_homg_line_2d<T >const&); \
124 template std::istream& operator>>(std::istream&, vgl_homg_line_2d<T >&)
125 
126 #endif // vgl_homg_line_2d_hxx_
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.
vgl_homg_line_2d()
Default constructor (Line 1.y==0, the X axis).
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
line in projective 2D space
#define vp(os, v, s)
Represents a Euclidean 2D line.
Definition: vgl_fwd.h:16
point in projective 2D space
void set(T px, T py, T pw=(T) 1)
Set x,y,w.
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
void set(T va, T vb, T vc)
Set a b c.