contrib/oxl/mvl/FMatrix.h
Go to the documentation of this file.
00001 // This is oxl/mvl/FMatrix.h
00002 #ifndef _FMatrix_h
00003 #define _FMatrix_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 //  \file
00009 // \brief General fundamental matrix
00010 //
00011 // A class to hold a Fundamental Matrix of the general form and to
00012 // perform common operations e.g. generate epipolar lines
00013 //
00014 // \verbatim
00015 // Modifications
00016 //    22 Oct 2002 - Peter Vanroose - added vgl_homg_point_2d interface
00017 //    23 Oct 2002 - Peter Vanroose - using fixed 3x3 matrices throughout
00018 // \endverbatim
00019 
00020 #include <mvl/HomgLine2D.h>
00021 #include <mvl/HomgPoint2D.h>
00022 #include <vgl/vgl_homg_line_2d.h>
00023 #include <mvl/PMatrix.h>
00024 #include <vgl/vgl_homg_point_2d.h>
00025 #include <vcl_iosfwd.h>
00026 #include <vgl/algo/vgl_p_matrix.h>
00027 
00028 #include <vnl/vnl_double_3x3.h>
00029 
00030 class PMatrix;
00031 
00032 //:
00033 // A class to hold a Fundamental Matrix of the general form and to
00034 // perform common operations e.g. generate epipolar lines
00035 class FMatrix
00036 {
00037  public:
00038 
00039   // Constructors/Initializers/Destructors----------------------------------
00040 
00041   FMatrix();
00042   FMatrix(vcl_istream& f);
00043   FMatrix(const double *f_matrix);
00044   FMatrix(const vnl_double_3x3& f_matrix);
00045   FMatrix(const PMatrix& P1, const PMatrix& P2);
00046   FMatrix(const PMatrix& P2);
00047   FMatrix(const FMatrix& that) { *this = that; }
00048   virtual ~FMatrix();
00049 
00050   static FMatrix read(char const* filename);
00051   static FMatrix read(vcl_istream& s);
00052 
00053   // Operations------------------------------------------------------------
00054 
00055   HomgLine2D       image1_epipolar_line(const HomgPoint2D& x2) const;
00056   vgl_homg_line_2d<double> image1_epipolar_line(vgl_homg_point_2d<double> const& x2) const;
00057   HomgLine2D       image2_epipolar_line(const HomgPoint2D& x1) const;
00058   vgl_homg_line_2d<double> image2_epipolar_line(vgl_homg_point_2d<double> const& x1) const;
00059 
00060   double image1_epipolar_distance_squared(HomgPoint2D *point1_ptr, HomgPoint2D *point2_ptr) const;
00061   double image1_epipolar_distance_squared(vgl_homg_point_2d<double> const& p1,
00062                                           vgl_homg_point_2d<double> const& p2) const;
00063   double image2_epipolar_distance_squared(HomgPoint2D *point1_ptr, HomgPoint2D *point2_ptr) const;
00064   double image2_epipolar_distance_squared(vgl_homg_point_2d<double> const& p1,
00065                                           vgl_homg_point_2d<double> const& p2) const;
00066 
00067   // Computations------------------------------------------------------------
00068 
00069   void set_rank2_using_svd();
00070   FMatrix get_rank2_truncated();
00071   FMatrix transpose() const;
00072   bool get_epipoles (HomgPoint2D* e1_out, HomgPoint2D* e2_out) const;
00073   bool get_epipoles (vgl_homg_point_2d<double>& e1_out,
00074                      vgl_homg_point_2d<double>& e2_out) const;
00075   void decompose_to_skew_rank3 (vnl_matrix<double> *skew,
00076                                 vnl_matrix<double> *rank3) const;
00077 
00078   void find_nearest_perfect_match(const HomgPoint2D& in1, const HomgPoint2D& in2,
00079                                   HomgPoint2D *out1, HomgPoint2D *out2) const;
00080 
00081   void find_nearest_perfect_match(vgl_homg_point_2d<double> const& in1,
00082                                   vgl_homg_point_2d<double> const& in2,
00083                                   vgl_homg_point_2d<double>& out1,
00084                                   vgl_homg_point_2d<double>& out2) const;
00085 
00086   void find_nearest_perfect_match(const HomgPoint2D& in1, const HomgPoint2D& in2,
00087                                   const HomgPoint2D&  e1, const HomgPoint2D& e2,
00088                                   HomgPoint2D *out1, HomgPoint2D *out2) const;
00089 
00090   void find_nearest_perfect_match(vgl_homg_point_2d<double> const& in1,
00091                                   vgl_homg_point_2d<double> const& in2,
00092                                   vgl_homg_point_2d<double> const&  e1,
00093                                   vgl_homg_point_2d<double> const& e2,
00094                                   vgl_homg_point_2d<double>& out1,
00095                                   vgl_homg_point_2d<double>& out2) const;
00096 
00097   // Data Access------------------------------------------------------------
00098   void compute_P_matrix(vnl_matrix<double> &P2) const;
00099   void compute_P_matrix(PMatrix &P2) const {
00100     vnl_matrix<double> temp(3,4);
00101     compute_P_matrix(temp);
00102     P2.set(temp);
00103   }
00104   void compute_P_matrix(vgl_p_matrix<double> &P2) const {
00105     vnl_matrix<double> temp(3,4);
00106     compute_P_matrix(temp);
00107     P2.set(temp);
00108   }
00109 
00110 
00111   double get (unsigned int row_index, unsigned int col_index) const;
00112 
00113   virtual bool set (const double* f_matrix);
00114   void get (double *f_matrix) const;
00115 
00116   virtual bool set (const vnl_double_3x3& f_matrix);
00117   void get (vnl_matrix<double>* f_matrix) const;
00118 
00119   void set(const PMatrix& P1, const PMatrix& P2);
00120   void set(const PMatrix& P2);
00121   void set(const FMatrix&);
00122 
00123   //: Return a const reference to the internal 3x3 matrix.
00124   const vnl_double_3x3& get_matrix () const { return f_matrix_; }
00125 
00126   //: Return a const reference to the transpose of the internal 3x3 matrix.
00127   const vnl_double_3x3& get_transpose_matrix () const { return ft_matrix_; }
00128 
00129   bool get_rank2_flag (void) const;
00130   void set_rank2_flag (bool rank2_flag);
00131 
00132 friend vcl_ostream& operator<<(vcl_ostream& s, const FMatrix& F);
00133 friend vcl_istream& operator>>(vcl_istream& s, FMatrix& F);
00134 
00135   bool read_ascii(vcl_istream& f);
00136 
00137   // INTERNALS---------------------------------------------------------------
00138 
00139     // Data Members------------------------------------------------------------
00140 
00141  protected:
00142 
00143   // Fundamental matrix
00144   vnl_double_3x3 f_matrix_;
00145 
00146   // transpose of Fundamental matrix
00147   vnl_double_3x3 ft_matrix_;
00148 
00149   // True if the Fundamental matrix is rank 2
00150   // (the result of a linear computation is not normally a rank 2 matrix)
00151   bool rank2_flag_;
00152 };
00153 
00154 #endif // _FMatrix_h