vgl_h_matrix_1d.h
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_h_matrix_1d.h
2 #ifndef vgl_h_matrix_1d_h_
3 #define vgl_h_matrix_1d_h_
4 //:
5 // \file
6 // \brief 2x2 line-to-line projectivity
7 //
8 // A class to hold a line-to-line projective transformation matrix
9 // and to perform common operations using it e.g. transfer point.
10 //
11 // \verbatim
12 // Modifications
13 // 22 Oct 2002 - Peter Vanroose - added vgl_homg_point_2d interface
14 // 23 Oct 2002 - Peter Vanroose - using fixed 3x3 matrices throughout
15 // 22 Mar 2003 - J.L. Mundy - preparing for upgrade to vgl
16 // 13 Jun 2004 - Peter Vanroose - added set_identity() and projective_basis()
17 // 31 Jul 2010 - Peter Vanroose - made more similar to 2d and 3d variants
18 // 24 Oct 2010 - Peter Vanroose - mutators and setters now return *this
19 // 27 Oct 2010 - Peter Vanroose - moved Doxygen docs from .hxx to .h
20 // 26 Jul 2011 - Peter Vanroose - added is_identity(),is_rotation(),is_euclidean(),
21 // correlation(),operator*(h_matrix),set(row,col,val),
22 // set_translation(),set_affine(), cf vgl_h_matrix_2d
23 // \endverbatim
24 
25 #include <vector>
26 #include <iosfwd>
27 #include <vnl/vnl_matrix_fixed.h>
28 #include <vgl/vgl_homg_point_1d.h>
29 #ifdef _MSC_VER
30 # include <vcl_msvc_warnings.h>
31 #endif
32 
33 //:
34 // A class to hold a line-to-line projective transformation matrix
35 // and to perform common operations using it e.g. transfer point.
36 template <class T>
37 class vgl_h_matrix_1d
38 {
39  // Data Members--------------------------------------------------------------
40  protected:
41  vnl_matrix_fixed<T,2,2> t12_matrix_;
42 
43  public:
44 
45  // Constructors/Initializers/Destructors-------------------------------------
46 
47  vgl_h_matrix_1d() = default;
48  ~vgl_h_matrix_1d() = default;
49 
50  //: Copy constructor
52 
53  //: Constructor from a 2x2 matrix, and implicit cast from vnl_matrix_fixed<T,2,2>
54  vgl_h_matrix_1d(vnl_matrix_fixed<T,2,2> const& M) : t12_matrix_(M) {}
55 
56  //: Constructor from 2x2 C-array
57  explicit vgl_h_matrix_1d(T const* M) : t12_matrix_(M) {}
58  //: Constructor from istream
59  explicit vgl_h_matrix_1d(std::istream& s);
60  //: Constructor from file
61  explicit vgl_h_matrix_1d(char const* filename);
62  //: Constructor - calculate homography between two sets of 1D points (minimum 3)
63  vgl_h_matrix_1d(std::vector<vgl_homg_point_1d<T> > const& points1,
64  std::vector<vgl_homg_point_1d<T> > const& points2);
65 
66  // Operations----------------------------------------------------------------
67 
68  //: Return the transformed point given by $q = {\tt H} p$
70  //: Return the transformed point given by $q = {\tt H} p$
71  vgl_homg_point_1d<T> operator*(vgl_homg_point_1d<T> const& p) const { return (*this)(p); }
72  vgl_homg_point_1d<T> correlation(vgl_homg_point_1d<T> const& p) const { return (*this)(p); }
73 
74  //: Return the transformed point given by $p = {\tt H}^{-1} q$
76 
77  bool operator==(vgl_h_matrix_1d<T> const& M) const { return t12_matrix_ == M.get_matrix(); }
78 
79  //: product of two vgl_h_matrix_1ds
82 
83  // Data Access---------------------------------------------------------------
84 
85  //: Return the 2x2 homography matrix
86  vnl_matrix_fixed<T,2,2> const& get_matrix() const { return t12_matrix_; }
87  //: Fill M with contents of the 2x2 homography matrix
88  void get (vnl_matrix_fixed<T,2,2>* M) const;
89  //:
90  // \deprecated use the vnl_matrix_fixed variant instead
91  void get (vnl_matrix<T>* M) const;
92  //: Fill M with contents of the 2x2 homography matrix
93  void get (T* M) const;
94  //: Return an element from the 2x2 homography matrix
95  T get (unsigned int row_index, unsigned int col_index) const;
96  //: Return the inverse homography
98 
99  bool is_rotation() const;
100  bool is_euclidean() const;
101  bool is_identity() const;
102 
103  //: transformation to projective basis (canonical frame)
104  // Compute the homography that takes the input set of points to the
105  // canonical frame. The points act as the projective basis for
106  // the canonical coordinate system. In the canonical frame the points
107  // have coordinates:
108  // \verbatim
109  // p[0]p[1]p[2]
110  // 1 0 1
111  // 0 1 1
112  // \endverbatim
113  bool projective_basis(std::vector<vgl_homg_point_1d<T> > const& three_points);
114 
115  //: initialize the transformation to identity
117  //: Set to 2x2 row-stored matrix
118  vgl_h_matrix_1d& set(T const* M);
119  //: Set to given 2x2 matrix
120  vgl_h_matrix_1d& set(vnl_matrix_fixed<T,2,2> const& M);
121 
122  //: Set an element of the 2x2 homography matrix
123  vgl_h_matrix_1d& set (unsigned int row_index, unsigned int col_index, T value)
124  { t12_matrix_[row_index][col_index]=value; return *this; }
125 
126  //: compose the current transform with a uniform scaling transformation, S.
127  // $S = \left[ \begin{array}{cc}
128  // s & 0 \\%
129  // 0 & 1
130  // \end{array}\right]$ , Ts = S*T.
131  vgl_h_matrix_1d& set_scale(T scale);
132 
133  //: set H[0][1] = tx, other elements unaltered
135 
136  //: set the transform to a general affine transform matrix
137  // $A = \left[ \begin{array}{ccc}
138  // a00 & a01 \\%
139  // 0 & 1
140  // \end{array}\right]$
141  vgl_h_matrix_1d& set_affine(vnl_matrix_fixed<T,1,2> const& M12);
142 
143  //: Read H from file
144  bool read(char const* filename);
145  //: Read H from std::istream
146  bool read(std::istream& s);
147 };
148 
149 //: Print H on std::ostream
150 template <class T> std::ostream& operator<<(std::ostream& s, vgl_h_matrix_1d<T> const& H);
151 //: Read H from std::istream
152 template <class T> std::istream& operator>>(std::istream& s, vgl_h_matrix_1d<T>& H)
153 { H.read(s); return s; }
154 
155 
156 #define VGL_H_MATRIX_1D_INSTANTIATE(T) extern "please include vgl/algo/vgl_h_matrix_1d.hxx first"
157 
158 #endif // vgl_h_matrix_1d_h_
vgl_h_matrix_1d & set(unsigned int row_index, unsigned int col_index, T value)
Set an element of the 2x2 homography matrix.
A class to hold a line-to-line projective transformation matrix and to perform common operations usin...
Definition: vgl_algo_fwd.h:10
a point in homogeneous 1-D space, i.e., a homogeneous pair (x,w)
bool operator==(vgl_h_matrix_1d< T > const &M) const
vgl_h_matrix_1d & set_translation(T tx)
set H[0][1] = tx, other elements unaltered.
vgl_homg_point_1d< T > correlation(vgl_homg_point_1d< T > const &p) const
vgl_h_matrix_1d & set_scale(T scale)
compose the current transform with a uniform scaling transformation, S.
bool is_euclidean() const
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
void get(vnl_matrix_fixed< T, 2, 2 > *M) const
Fill M with contents of the 2x2 homography matrix.
bool projective_basis(std::vector< vgl_homg_point_1d< T > > const &three_points)
transformation to projective basis (canonical frame).
vgl_h_matrix_1d & set_affine(vnl_matrix_fixed< T, 1, 2 > const &M12)
set the transform to a general affine transform matrix.
vgl_h_matrix_1d(vgl_h_matrix_1d< T > const &M)
Copy constructor.
vgl_homg_point_1d< T > operator()(vgl_homg_point_1d< T > const &p) const
Return the transformed point given by $q = {\tt H} p$.
vgl_h_matrix_1d & set(T const *M)
Set to 2x2 row-stored matrix.
vnl_matrix_fixed< T, 2, 2 > const & get_matrix() const
Return the 2x2 homography matrix.
bool read(char const *filename)
Read H from file.
vgl_h_matrix_1d(vnl_matrix_fixed< T, 2, 2 > const &M)
Constructor from a 2x2 matrix, and implicit cast from vnl_matrix_fixed<T,2,2>.
vgl_h_matrix_1d(T const *M)
Constructor from 2x2 C-array.
vgl_h_matrix_1d & set_identity()
initialize the transformation to identity.
bool is_rotation() const
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
bool is_identity() const
vnl_matrix_fixed< T, 2, 2 > t12_matrix_
vgl_h_matrix_1d get_inverse() const
Return the inverse homography.
vgl_homg_point_1d< T > preimage(vgl_homg_point_1d< T > const &q) const
Return the transformed point given by $p = {\tt H}^{-1} q$.
Represents a homogeneous 1-D point, i.e., a homogeneous pair (x,w).
Definition: vgl_fwd.h:7
vgl_homg_point_1d< T > operator *(vgl_homg_point_1d< T > const &p) const
Return the transformed point given by $q = {\tt H} p$.
~vgl_h_matrix_1d()=default
vgl_h_matrix_1d()=default