core/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/vpgl_lvcs_sptr.h>
00019 #include <vpgl/vpgl_lvcs.h>
00020 #include <vnl/vnl_matrix.h>
00021 
00022 #include <vpgl/vpgl_camera.h>
00023 
00024 #include <vil/vil_image_resource_sptr.h>
00025 
00026 class vpgl_geo_camera : public vpgl_camera<double>
00027 {
00028  public:
00029   //: creates identity matrix and all zero tiepoints
00030   vpgl_geo_camera();
00031 
00032   //: if scale tag is false be sure that trans_matrix[0][0] and trans_matrix[1][1] is 1.0 otherwise set it to true
00033   vpgl_geo_camera(vnl_matrix<double> trans_matrix,
00034                   vpgl_lvcs_sptr lvcs)
00035     : trans_matrix_(trans_matrix), is_utm(false), scale_tag_(false) { this->set_lvcs(lvcs); }
00036 
00037   // copy constructor
00038   vpgl_geo_camera(vpgl_geo_camera const& rhs);
00039 
00040   vpgl_geo_camera(vpgl_camera<double> const& rhs);
00041 
00042   static bool init_geo_camera(vil_image_resource_sptr const geotiff_img,
00043                               vpgl_lvcs_sptr lvcs,
00044                               vpgl_geo_camera*& camera);
00045 
00046   ~vpgl_geo_camera() {}
00047 
00048   virtual vcl_string type_name() const { return "vpgl_geo_camera"; }
00049 
00050   //northing=0 means North, 1 is east
00051   void set_utm(int utm_zone, unsigned northing) { is_utm=true, utm_zone_=utm_zone; northing_=northing; }
00052 
00053   void set_lvcs(vpgl_lvcs_sptr lvcs) {lvcs_ = new vpgl_lvcs(*lvcs); }
00054 
00055   void set_scale_format(bool scale_tag) { scale_tag_=scale_tag; }
00056 
00057   vpgl_lvcs_sptr const lvcs() {return lvcs_;}
00058 
00059   //: Implementing the generic camera interface of vpgl_camera.
00060   //  x,y,z are in local coordinates, u represents image column, v image row
00061   void project(const double x, const double y, const double z, double& u, double& v) const;
00062 
00063   //: backprojects an image point into local coordinates (based on lvcs_)
00064   void backproject(const double u, const double v, double& x, double& y, double& z);
00065 
00066   // adds translation to the trans matrix
00067   void translate(double tx, double ty, double z);
00068 
00069   //: the lidar pixel size in meters assumes square pixels
00070   double pixel_spacing() { if (scale_tag_) return trans_matrix_[0][0];
00071                            else return 1.0; }
00072 
00073   bool operator ==(vpgl_geo_camera const& rhs) const;
00074 
00075   static bool comp_trans_matrix(double sx1, double sy1, double sz1,
00076                                 vcl_vector<vcl_vector<double> > tiepoints,
00077                                 vnl_matrix<double>& trans_matrix,
00078                                 bool scale_tag = false);
00079 
00080   //: Return a platform independent string identifying the class
00081   virtual vcl_string is_a() const { return vcl_string("vpgl_geo_camera"); }
00082 
00083   //: Return true if the argument matches the string identifying the class or any parent class
00084   virtual bool is_class(vcl_string const& cls) const
00085   { return cls==is_a() || cls==vcl_string("vpgl_geo_camera"); }
00086 
00087   //: Write camera to stream
00088   friend vcl_ostream&  operator<<(vcl_ostream& s, vpgl_geo_camera const& p);
00089 
00090   //: Read camera  from stream
00091   friend vcl_istream&  operator>>(vcl_istream& s, vpgl_geo_camera& p);
00092 
00093   //: returns the corresponding geographical coordinates for a given pixel position (i,j)
00094   //  the output global coord is wgs84
00095   void img_to_global(const double i, const double j,
00096                      double& lon, double& lat) const;
00097 
00098   //: returns the corresponding pixel position for given geographical coordinates
00099   //  the input global coord is wgs84
00100   void global_to_img(const double lon, const double lat, const double elev,
00101                      double& u, double& v) const;
00102 
00103   //: returns the corresponding geographical coordinates for a given pixel position (i,j)
00104   //  the output global coord is UTM: x east, y north 
00105   void img_to_global_utm(const double i, const double j,
00106                          double& x, double& y) const;
00107 
00108   //: returns the corresponding pixel position for given geographical coordinates
00109   //  the input global coord is UTM: x east, for y north
00110   void global_utm_to_img(const double x, const double y, int zone, double elev, 
00111                          double& u, double& v) const;
00112 
00113   //: returns the corresponding utm location for the given local position
00114   void local_to_utm(const double x, const double y, const double z, double& e, double& n, int& utm_zone); 
00115 
00116   int utm_zone() { return utm_zone_; }
00117 
00118   bool img_four_corners_in_utm(const unsigned ni, const unsigned nj, double elev, double& e1, double& n1, double& e2, double& n2);
00119 
00120   //: returns the corresponding geographical coordinate (lon, lat, elev) for a given pixel position (i,j,k)
00121   //  Note: not yet implemented -- PVr, 16 aug 2012
00122   void img_to_wgs(unsigned i, unsigned j, unsigned k, double& lon, double& lat, double& elev);
00123 
00124 #if 0
00125   //: returns the corresponding pixel position (i,j) for a given geographical coordinate (lon, lat)
00126   void wgs_to_img(double lon, double lat,
00127                   unsigned& i, unsigned& j);
00128 #endif // 0
00129 
00130  private:
00131 
00132   vnl_matrix<double> trans_matrix_;           // 4x4 matrix
00133   //: lvcs of world parameters
00134   vpgl_lvcs_sptr lvcs_;
00135   bool is_utm;
00136   int utm_zone_;
00137   int northing_; //0 North, 1 South
00138   bool scale_tag_;
00139 };
00140 
00141 #endif // vpgl_geo_camera_h_