1 #ifndef rgrl_trans_rigid_h_
2 #define rgrl_trans_rigid_h_
3 //:
4 // \file
5 // \author Tomasz Malisiewicz
6 // \date   March 2004
7 
8 #include <iostream>
9 #include <iosfwd>
10 #include "rgrl_transformation.h"
11 #ifdef _MSC_VER
12 #  include <vcl_msvc_warnings.h>
13 #endif
14 
15 //: Represents a rigid transformation.
16 //
17 //  A rigid transform is q = R p + t where R
18 //  is an n-dimensional rotation matrix, and t is an n-dimensional
19 //  translation vector.  This is just like a similarity transformation with scale=1.
20 //  Note that R has {9,4} parameters, but only {3,1} degrees of freedom in {3D,2D}.
21 //  t has {3,2} degrees of freedom in {3D,2D}.
22 //  This can be most easily seen from the fact that rotation matrices are orthonormal.
23 //  The rigid transform has {6,3} total degrees of freedom (rotation + translation) in {3D,2D}.
24 //  The relationship between dimensionality(d) and dof(degrees of freedom) is: dof = 3(d-1)
25 class rgrl_trans_rigid
26   : public rgrl_transformation
27 {
28  public:
29   //: Initialize to the identity transformation.
30   //
31   rgrl_trans_rigid( unsigned int dimension = 0);
32 
33   //: Constructor based on an initial transformation and covar estimate
34   //
35   rgrl_trans_rigid(vnl_matrix<double> rot, vnl_vector<double> trans,
36                    vnl_matrix<double> const &covar);
37 
38   //: Constructor based on an initial transformation and unknown covar
39   //
40   //  The  covariance matrix is a zero matrix.
41   rgrl_trans_rigid(vnl_matrix<double> rot, vnl_vector<double> trans);
42 
43   //: Sets the translation component of this transformation
44   //  \note This method is valid for only the 3d version
45   void set_translation(double tx, double ty, double tz);
46 
47   //: Sets the translation component of this transformation
48   //  \note This method is valid for only the 2d version
49   void set_translation(double tx, double ty);
50 
51   //: Sets the rotation part of this transformation
52   //  \note This method is valid for only the 3d version
53   void set_rotation(double theta, double alpha, double phi);
54 
55   //: Sets the rotation part of this transformation
56   //  \note This method is valid for only the 2d version
57   void set_rotation(double theta);
58 
59   //: Given a rotation matrix which is the product of three independent rotations, extract the angles
60   //  \note This method is valid for only the 3d version
61   void determine_angles( double& phi, double& alpha, double& theta) const;
62 
63   //: Extract the angles
64   //  \note This method is valid for only the 2d version
65   void determine_angles( double& theta ) const;
66 
67 
68   vnl_matrix<double> transfer_error_covar( vnl_vector<double> const& p  ) const override;
69 
70   //: The rotation component of this transform
71   vnl_matrix<double> const& R() const;
72 
73   //: The translation component of the transform
74   vnl_vector<double> t() const;
75 
76   //:  Inverse map with an initial guess
77   void inv_map( const vnl_vector<double>& to,
78                 bool initialize_next,
79                 const vnl_vector<double>& to_delta,
80                 vnl_vector<double>& from,
81                 vnl_vector<double>& from_next_est) const override;
82 
83   //:  Inverse map based on the transformation.
84   //   The inverse mapping for R(p)+ t = q is p = A^-1(q-t)
85   void inv_map( const vnl_vector<double>& to,
86                 vnl_vector<double>& from ) const override;
87 
88   //: is this an invertible transformation?
is_invertible()89   bool is_invertible() const override { return true; }
90 
91   //: Return an inverse transformation
92   //  This function only exist for certain transformations.
93   rgrl_transformation_sptr inverse_transform() const override;
94 
95   //: Compute jacobian w.r.t. location
96   void jacobian_wrt_loc( vnl_matrix<double>& jac, vnl_vector<double> const& from_loc ) const override;
97 
98   //:  transform the transformation for images of different resolution
99   rgrl_transformation_sptr scale_by( double scale ) const override;
100 
101   // Defines type-related functions
102   rgrl_type_macro( rgrl_trans_rigid, rgrl_transformation )
103 
104   // for output
105   void write(std::ostream& os ) const override;
106 
107   // for input
108   bool read(std::istream& is ) override;
109 
110   //: make a clone copy
111   rgrl_transformation_sptr clone() const override;
112 
113  protected:
114   void map_loc( vnl_vector<double> const& from,
115                 vnl_vector<double>      & to ) const override;
116 
117   void map_dir( vnl_vector<double> const& from_loc,
118                 vnl_vector<double> const& from_dir,
119                 vnl_vector<double>      & to_dir    ) const override;
120 
121  private:
122   vnl_matrix<double> R_;
123   vnl_vector<double> trans_;
124 };
125 
126 
127 #endif
128