contrib/gel/mrc/vpgl/file_formats/vpgl_geo_camera.h
Go to the documentation of this file.
00001 #ifndef vpgl_geo_camera_h_
00002 #define vpgl_geo_camera_h_
00003 //:
00004 // \file
00005 // \brief A geotiff image deduced camera class.
00006 // \author Gamze Tunali
00007 // \date October 24, 2008
00008 // \author Gamze Tunali
00009 //
00010 // Geotiff images contain information about the corresponding world coordinate
00011 // values of the pixel positions. This class is a wrapper of that information
00012 // to project and backproject the 3D points in local coordinates to pixel positions
00013 
00014 
00015 #include <vcl_iosfwd.h>
00016 #include <vcl_vector.h>
00017 
00018 #include <vpgl/bgeo/bgeo_lvcs_sptr.h>
00019 #include <vpgl/bgeo/bgeo_lvcs.h>
00020 #include <vnl/vnl_matrix.h>
00021 
00022 #include <vpgl/vpgl_camera.h>
00023 #include <vsl/vsl_binary_io.h>
00024 
00025 #include <vil/file_formats/vil_tiff.h>
00026 
00027 class vpgl_geo_camera : public vpgl_camera<double>
00028 {
00029  public:
00030   //: creates identity matrix and all zero tiepoints
00031   vpgl_geo_camera();
00032 
00033   vpgl_geo_camera(vnl_matrix<double> trans_matrix,
00034                   bgeo_lvcs_sptr lvcs,
00035                   vcl_vector<vcl_vector<double> > tiepoints) // FIXME - unused parameter
00036     : trans_matrix_(trans_matrix), is_utm(false), scale_tag_(false) { this->set_lvcs(lvcs); }
00037 
00038   // copy constructor
00039   vpgl_geo_camera(vpgl_geo_camera const& rhs);
00040 
00041   vpgl_geo_camera(vpgl_camera<double> const& rhs);
00042 
00043   static bool init_geo_camera(vil_tiff_image* const& geotiff_img,
00044                               bgeo_lvcs_sptr lvcs,
00045                               vpgl_geo_camera*& camera);
00046 
00047   ~vpgl_geo_camera() {}
00048 
00049   virtual vcl_string type_name() const { return "vpgl_geo_camera"; }
00050 
00051   //northing=0 means North, 1 is east
00052   void set_utm(int utm_zone, unsigned northing) { is_utm=true, utm_zone_=utm_zone; northing_=northing; }
00053 
00054   void set_lvcs(bgeo_lvcs_sptr lvcs) {lvcs_ = new bgeo_lvcs(*lvcs); }
00055 
00056   void set_scale_format(bool scale_tag) { scale_tag_=scale_tag; }
00057 
00058   bgeo_lvcs_sptr const lvcs() {return lvcs_;}
00059 
00060   //: Implementing the generic camera interface of vpgl_camera.
00061   //  x,y,z are in local coordinates, u represents image column, v image row
00062   void project(const double x, const double y, const double z, double& u, double& v) const;
00063 
00064   //: backprojects an image point into local coordinates (based on lvcs_)
00065   void backproject(const double u, const double v, double& x, double& y, double& z);
00066 
00067   // adds translation to the trans matrix
00068   void translate(double tx, double ty, double z);
00069 
00070   //: the lidar pixel size in meters assumes square pixels
00071   double pixel_spacing() { if (scale_tag_) return trans_matrix_[0][0];
00072                            else return 1.0; }
00073 
00074   bool operator ==(vpgl_geo_camera const& rhs) const;
00075 
00076   static bool comp_trans_matrix(double sx1, double sy1, double sz1,
00077                                 vcl_vector<vcl_vector<double> > tiepoints,
00078                                 vnl_matrix<double>& trans_matrix,
00079                                 bool scale_tag = false);
00080 
00081   //: Return a platform independent string identifying the class
00082   virtual vcl_string is_a() const { return vcl_string("vpgl_geo_camera"); }
00083 
00084   //: Return true if the argument matches the string identifying the class or any parent class
00085   virtual bool is_class(vcl_string const& cls) const
00086   { return cls==is_a() || cls==vcl_string("vpgl_geo_camera"); }
00087 
00088   // *** binary IO ***
00089 
00090   //: Binary save self to stream.
00091   virtual void b_write(vsl_b_ostream &os) const;
00092 
00093   //: Binary load self from stream.
00094   virtual void b_read(vsl_b_istream &is);
00095 
00096   //: Write camera to stream
00097   friend vcl_ostream&  operator<<(vcl_ostream& s, vpgl_geo_camera const& p);
00098 
00099   //: Read camera  from stream
00100   friend vcl_istream&  operator>>(vcl_istream& s, vpgl_geo_camera& p);
00101 
00102   //: returns the corresponding geographical coordinates for a given pixel position (i,j)
00103   void img_to_wgs(const unsigned i, const unsigned j, const unsigned z,
00104                   double& lon, double& lat, double& elev);
00105 
00106 #if 0
00107   //: returns the corresponding pixel position (i,j) for a given geographical coordinates (lon, lat)
00108   void wgs_to_img(double lon, double lat,
00109                   unsigned& i, unsigned& j);
00110 #endif // 0
00111 
00112  private:
00113 
00114   vnl_matrix<double> trans_matrix_;           // 4x4 matrix
00115   //: lvcs of world parameters
00116   bgeo_lvcs_sptr lvcs_;
00117   bool is_utm;
00118   int utm_zone_;
00119   int northing_; //0 North, 1 South
00120   bool scale_tag_;
00121 };
00122 
00123 #endif // vpgl_geo_camera_h_