contrib/oxl/mvl/HMatrix1D.cxx
Go to the documentation of this file.
00001 // This is oxl/mvl/HMatrix1D.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 #include "HMatrix1D.h"
00008 
00009 #include <vgl/vgl_homg_point_1d.h>
00010 #include <vcl_fstream.h>
00011 #include <vnl/vnl_double_2.h>
00012 #include <vnl/vnl_inverse.h>
00013 
00014 //--------------------------------------------------------------
00015 //
00016 //: Default constructor
00017 HMatrix1D::HMatrix1D()
00018 {
00019 }
00020 
00021 //: Copy constructor
00022 HMatrix1D::HMatrix1D(const HMatrix1D& M)
00023   : t12_matrix_(M.t12_matrix_)
00024   , t21_matrix_(M.t21_matrix_)
00025 {
00026 }
00027 
00028 //--------------------------------------------------------------
00029 //
00030 //: Constructor
00031 HMatrix1D::HMatrix1D(vnl_double_2x2 const& M)
00032   : t12_matrix_ (M)
00033   , t21_matrix_(vnl_inverse(t12_matrix_))
00034 {
00035 }
00036 
00037 //--------------------------------------------------------------------------------
00038 
00039 HMatrix1D::HMatrix1D(const HMatrix1D&A,const HMatrix1D&B)
00040   : t12_matrix_(A.t12_matrix_ * B.t12_matrix_)
00041   , t21_matrix_(B.t21_matrix_ * A.t21_matrix_)
00042 {
00043 }
00044 
00045 //--------------------------------------------------------------
00046 //
00047 //: Constructor
00048 HMatrix1D::HMatrix1D (const double* H)
00049   : t12_matrix_ (H)
00050 {
00051   t21_matrix_ = vnl_inverse(t12_matrix_);
00052 }
00053 
00054 HMatrix1D::HMatrix1D (vcl_istream &is)
00055 {
00056   t12_matrix_.read_ascii(is);
00057   t21_matrix_ = vnl_inverse(t12_matrix_);
00058 }
00059 
00060 //: Destructor
00061 HMatrix1D::~HMatrix1D()
00062 {
00063 }
00064 
00065 // == OPERATIONS ==
00066 
00067 vgl_homg_point_1d<double> HMatrix1D::operator()(const vgl_homg_point_1d<double>& x1) const
00068 {
00069   vnl_double_2 v = t12_matrix_ * vnl_double_2(x1.x(),x1.w());
00070   return vgl_homg_point_1d<double>(v[0], v[1]);
00071 }
00072 
00073 //
00074 //: Return the transformed point given by $x_1 = {\tt H}^{-1} x_2$
00075 vgl_homg_point_1d<double> HMatrix1D::preimage(const vgl_homg_point_1d<double>& x2) const
00076 {
00077   vnl_double_2 v = t21_matrix_ * vnl_double_2(x2.x(),x2.w());
00078   return vgl_homg_point_1d<double>(v[0], v[1]);
00079 }
00080 
00081 //-----------------------------------------------------------------------------
00082 //: Print H on vcl_ostream
00083 vcl_ostream& operator<<(vcl_ostream& s, const HMatrix1D& h)
00084 {
00085   return s << h.get_matrix();
00086 }
00087 
00088 //: Read H from vcl_istream
00089 vcl_istream& operator >> (vcl_istream& s, HMatrix1D& H)
00090 {
00091   H = HMatrix1D(s);
00092   return s;
00093 }
00094 
00095 //: Read H from vcl_istream
00096 HMatrix1D HMatrix1D::read(vcl_istream& s)
00097 {
00098   return HMatrix1D(s);
00099 }
00100 
00101 
00102 //: Read H from file
00103 HMatrix1D HMatrix1D::read(char const* filename)
00104 {
00105   vcl_ifstream f(filename);
00106   if (!f.good())
00107     vcl_cerr << "HMatrix1D::read: Error opening " << filename << vcl_endl;
00108   return HMatrix1D(f);
00109 }
00110 
00111 // == DATA ACCESS ==
00112 
00113 //-----------------------------------------------------------------------------
00114 //: Get matrix element at (row_index, col_index)
00115 double HMatrix1D::get (unsigned int row_index, unsigned int col_index) const
00116 {
00117   return t12_matrix_. get (row_index, col_index);
00118 }
00119 
00120 //: Fill H with contents of this
00121 void HMatrix1D::get (double *H) const
00122 {
00123   for (int row_index = 0; row_index < 2; row_index++)
00124     for (int col_index = 0; col_index < 2; col_index++)
00125       *H++ = t12_matrix_. get (row_index, col_index);
00126 }
00127 
00128 //: Fill H with contents of this
00129 void HMatrix1D::get (vnl_matrix<double>* H) const
00130 {
00131   *H = t12_matrix_.as_ref(); // size 2x2
00132 }
00133 
00134 //: Set to 2x2 row-stored matrix, and cache inverse.
00135 void HMatrix1D::set (const double *H)
00136 {
00137   for (int row_index = 0; row_index < 2; row_index++)
00138     for (int col_index = 0; col_index < 2; col_index++)
00139       t12_matrix_. put (row_index, col_index, *H++);
00140 
00141   t21_matrix_ = vnl_inverse(t12_matrix_);
00142 }
00143 
00144 //: Set to given vnl_matrix, and cache inverse
00145 void HMatrix1D::set (vnl_double_2x2 const& H)
00146 {
00147   t12_matrix_ = H;
00148 
00149   t21_matrix_ = vnl_inverse(t12_matrix_);
00150 }
00151 
00152 //: Set to inverse of given vnl_matrix, and cache inverse.
00153 void HMatrix1D::set_inverse (vnl_double_2x2 const& H)
00154 {
00155   t21_matrix_ = H;
00156   t21_matrix_ = vnl_inverse(t12_matrix_);
00157 }
00158 
00159 //--------------------------------------------------------------------------------