contrib/oxl/mvl/PMatrix.h
Go to the documentation of this file.
00001 // This is oxl/mvl/PMatrix.h
00002 #ifndef PMatrix_h_
00003 #define PMatrix_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief General 3x4 perspective projection matrix
00010 //
00011 // A class to hold a perspective projection matrix and use it to
00012 // perform common operations e.g. projecting point in 3d space to
00013 // its image on the image plane
00014 //
00015 // \verbatim
00016 //  Modifications
00017 //     010796 AWF Implemented get_focal_point() - awf, july 96
00018 //     011096 AWF Added caching vnl_svd<double>
00019 //     260297 AWF Converted to use vnl_double_3x4
00020 //     110397 PVr Added operator==
00021 //     221002 Peter Vanroose - added vgl_homg_point_2d interface
00022 //     231002 Peter Vanroose - using fixed 3x4 matrices throughout
00023 // \endverbatim
00024 
00025 #include <vcl_iosfwd.h>
00026 
00027 #include <vnl/algo/vnl_algo_fwd.h> // for vnl_svd
00028 #include <vnl/vnl_matrix.h>
00029 #include <vnl/vnl_vector.h>
00030 #include <vnl/vnl_double_3x3.h>
00031 #include <vnl/vnl_double_3x4.h>
00032 #include <vnl/vnl_double_4x4.h>
00033 #include <vnl/vnl_double_3.h>
00034 #include <vnl/vnl_double_4.h>
00035 #include <vgl/vgl_homg_point_2d.h>
00036 #include <vgl/vgl_homg_point_3d.h>
00037 #include <vgl/vgl_homg_line_2d.h>
00038 #include <vgl/vgl_homg_line_3d_2_points.h>
00039 #include <vgl/vgl_homg_plane_3d.h>
00040 #include <vgl/vgl_line_segment_2d.h>
00041 #include <vgl/vgl_line_segment_3d.h>
00042 #include <vgl/algo/vgl_homg_operators_3d.h> // for p_matrix_ * vgl_homg_point_3d
00043 #include <vbl/vbl_ref_count.h>
00044 
00045 class HomgPoint2D;
00046 class HomgLine2D;
00047 class HomgLineSeg2D;
00048 
00049 class HomgPoint3D;
00050 class HomgPlane3D;
00051 class HomgLine3D;
00052 class HomgLineSeg3D;
00053 class HMatrix3D;
00054 class HMatrix2D;
00055 
00056 class PMatrix : public vbl_ref_count
00057 {
00058  public:
00059 
00060   // Constructors/Initializers/Destructors-------------------------------------
00061 
00062   PMatrix();
00063   PMatrix(vcl_istream&);
00064   PMatrix(const double *c_matrix);
00065   explicit PMatrix(vnl_double_3x4 const&);
00066   PMatrix(const vnl_matrix<double>& A, const vnl_vector<double>& a);
00067   PMatrix(const PMatrix&);
00068  ~PMatrix();
00069 
00070   static PMatrix read(const char* filename);
00071   static PMatrix read(vcl_istream&);
00072 
00073   // Operations----------------------------------------------------------------
00074 
00075   HomgPoint2D   project(const HomgPoint3D& X) const;
00076   HomgLine2D    project(const HomgLine3D& L) const;
00077   HomgLineSeg2D project(const HomgLineSeg3D& L) const;
00078 
00079   HomgPoint3D backproject_pseudoinverse(const HomgPoint2D& x) const;
00080   HomgLine3D  backproject(const HomgPoint2D& x) const;
00081   HomgPlane3D backproject(const HomgLine2D& l) const;
00082 
00083   //: Return the image point which is the projection of the specified 3D point X
00084   vgl_homg_point_2d<double>   project(vgl_homg_point_3d<double> const& X) const { return p_matrix_ * X; }
00085   //: Return the image line which is the projection of the specified 3D line L
00086   vgl_homg_line_2d<double>    project(vgl_homg_line_3d_2_points<double> const& L) const;
00087   //: Return the image linesegment which is the projection of the specified 3D linesegment L
00088   vgl_line_segment_2d<double> project(vgl_line_segment_3d<double> const& L) const;
00089 
00090   //: Return the 3D point $\vec X$ which is $\vec X = P^+ \vec x$.
00091   vgl_homg_point_3d<double> backproject_pseudoinverse(vgl_homg_point_2d<double> const& x) const;
00092   //: Return the 3D line which is the backprojection of the specified image point, x.
00093   vgl_homg_line_3d_2_points<double>  backproject(vgl_homg_point_2d<double> const& x) const;
00094   //: Return the 3D plane which is the backprojection of the specified line l in the image
00095   vgl_homg_plane_3d<double> backproject(vgl_homg_line_2d<double> const& l) const;
00096 
00097   //: post-multiply this projection matrix with a HMatrix3D
00098   PMatrix postmultiply(vnl_double_4x4 const& H) const;
00099 
00100   //: pre-multiply this projection matrix with a HMatrix2D
00101   PMatrix premultiply(vnl_double_3x3 const& H) const;
00102 
00103   vnl_svd<double>* svd() const; // mutable const
00104   void clear_svd() const;
00105   HomgPoint3D get_focal_point() const;
00106   vgl_homg_point_3d<double> get_focal() const;
00107   HMatrix3D get_canonical_H() const;
00108   bool is_canonical(double tol = 0) const;
00109 
00110   bool is_behind_camera(const HomgPoint3D&);
00111   bool is_behind_camera(vgl_homg_point_3d<double> const&);
00112   void flip_sign();
00113   bool looks_conditioned();
00114   void fix_cheirality();
00115 
00116   // Data Access---------------------------------------------------------------
00117 
00118   PMatrix& operator=(const PMatrix&);
00119 
00120   bool operator==(PMatrix const& p) const { return p_matrix_ == p.get_matrix(); }
00121 
00122   void get(vnl_matrix<double>* A, vnl_vector<double>* a) const;
00123   void get(vnl_double_3x3* A, vnl_double_3* a) const;
00124   void set(vnl_double_3x3 const& A, vnl_double_3 const& a);
00125 
00126   void get_rows(vnl_vector<double>*, vnl_vector<double>*, vnl_vector<double>*) const;
00127   void get_rows(vnl_double_4*, vnl_double_4*, vnl_double_4*) const;
00128   void set_rows(const vnl_vector<double>&, const vnl_vector<double>&, const vnl_vector<double>&);
00129 
00130   double get(unsigned int row_index, unsigned int col_index) const;
00131   void get(double *c_matrix) const;
00132   void get(vnl_matrix<double>* p_matrix) const;
00133   void get(vnl_double_3x4* p_matrix) const;
00134 
00135   void set(const double* p_matrix);
00136   void set(const double p_matrix [3][4]);
00137   void set(const vnl_matrix<double>& p_matrix);
00138   void set(vnl_double_3x4 const& p_matrix);
00139 
00140   const vnl_double_3x4& get_matrix() const { return p_matrix_; }
00141 
00142   // Utility Methods-----------------------------------------------------------
00143   bool read_ascii(vcl_istream& f);
00144 
00145   // Data Members--------------------------------------------------------------
00146  protected:
00147   vnl_double_3x4 p_matrix_;
00148   mutable vnl_svd<double>* svd_;
00149 };
00150 
00151 vcl_ostream& operator<<(vcl_ostream& s, const PMatrix& p);
00152 vcl_istream& operator>>(vcl_istream& i, PMatrix& p);
00153 
00154 inline
00155 PMatrix operator*(vnl_double_3x3 const& C, const PMatrix& P)
00156 {
00157   return PMatrix(C * P.get_matrix());
00158 }
00159 
00160 #endif // PMatrix_h_