contrib/gel/mrc/vpgl/vpgl_local_rational_camera.txx
Go to the documentation of this file.
00001 // This is gel/mrc/vpgl/vpgl_local_rational_camera.txx
00002 #ifndef vpgl_local_rational_camera_txx_
00003 #define vpgl_local_rational_camera_txx_
00004 //:
00005 // \file
00006 #include "vpgl_local_rational_camera.h"
00007 #include <vcl_vector.txx>
00008 #include <vcl_fstream.h>
00009 #include <vgl/vgl_point_2d.h>
00010 #include <vgl/vgl_point_3d.h>
00011 //--------------------------------------
00012 // Constructors
00013 //
00014 
00015 // Create an identity projection, i.e. (x,y,z) identically maps to (x,y)
00016 template <class T>
00017 vpgl_local_rational_camera<T>::vpgl_local_rational_camera():
00018   vpgl_rational_camera<T>()
00019 {
00020 }
00021 
00022 //: Constructor from a rational camera and an affine matrix
00023 template <class T>
00024 vpgl_local_rational_camera<T>::
00025 vpgl_local_rational_camera(bgeo_lvcs const& lvcs,
00026                            vpgl_rational_camera<T> const& rcam):
00027 
00028   vpgl_rational_camera<T>(rcam), lvcs_(lvcs)
00029 {
00030 }
00031 
00032 //: Constructor from a rational camera and a geographic origin
00033 template <class T>
00034 vpgl_local_rational_camera<T>::
00035 vpgl_local_rational_camera(T longitude, T latitude, T elevation,
00036                            vpgl_rational_camera<T> const& rcam) :
00037   vpgl_rational_camera<T>(rcam),
00038   lvcs_(bgeo_lvcs(latitude, longitude, elevation,
00039                   bgeo_lvcs::wgs84, bgeo_lvcs::DEG, bgeo_lvcs::METERS))
00040   {
00041   }
00042 
00043 
00044 template <class T>
00045 vpgl_local_rational_camera<T>* vpgl_local_rational_camera<T>::clone(void) const
00046 {
00047   return new vpgl_local_rational_camera<T>(*this);
00048 }
00049 
00050 // Base projection method, x, y, z are in local Cartesian coordinates
00051 template <class T>
00052 void vpgl_local_rational_camera<T>::project(const T x, const T y, const T z,
00053                                            T& u, T& v) const
00054 {
00055   //first convert to global geographic  coordinates
00056   double lon, lat, gz;
00057   bgeo_lvcs& non_const_lvcs = const_cast<bgeo_lvcs&>(lvcs_);
00058   non_const_lvcs.local_to_global(x, y, z, bgeo_lvcs::wgs84, lon, lat, gz);
00059   vpgl_rational_camera<T>::project(lon, lat, gz, u, v);
00060 }
00061 
00062 //vnl interface methods
00063 template <class T>
00064 vnl_vector_fixed<T, 2>
00065 vpgl_local_rational_camera<T>::project(vnl_vector_fixed<T, 3> const& world_point)const
00066 {
00067   vnl_vector_fixed<T, 2> image_point;
00068   this->project(world_point[0], world_point[1], world_point[2],
00069                 image_point[0], image_point[1]);
00070   return image_point;
00071 }
00072 
00073 //vgl interface methods
00074 template <class T>
00075 vgl_point_2d<T> vpgl_local_rational_camera<T>::project(vgl_point_3d<T> world_point)const
00076 {
00077   T u = 0, v = 0;
00078   this->project(world_point.x(), world_point.y(), world_point.z(), u, v);
00079   return vgl_point_2d<T>(u, v);
00080 }
00081 
00082 //: print the camera parameters
00083 template <class T>
00084 void vpgl_local_rational_camera<T>::print(vcl_ostream& s) const
00085 {
00086   vpgl_rational_camera<T>::print(s);
00087   s << lvcs_ <<'\n'
00088     <<"------------------------------------------------\n\n";
00089 }
00090 
00091 template <class T>
00092 bool vpgl_local_rational_camera<T>::save(vcl_string cam_path)
00093 {
00094   vcl_ofstream file_out;
00095   file_out.open(cam_path.c_str());
00096   if (!file_out.good()) {
00097     vcl_cerr << "error: bad filename: " << cam_path << vcl_endl;
00098     return false;
00099   }
00100   file_out.precision(12);
00101 
00102   int map[20];
00103   map[0]=19;
00104   map[1]=9;
00105   map[2]=15;
00106   map[3]=18;
00107   map[4]=6;
00108   map[5]=8;
00109   map[6]=14;
00110   map[7]=3;
00111   map[8]=12;
00112   map[9]=17;
00113   map[10]=5;
00114   map[11]=0;
00115   map[12]=4;
00116   map[13]=7;
00117   map[14]=1;
00118   map[15]=10;
00119   map[16]=13;
00120   map[17]=2;
00121   map[18]=11;
00122   map[19]=16;
00123 
00124   file_out << "satId = \"????\";\n"
00125            << "bandId = \"RGB\";\n"
00126            << "SpecId = \"RPC00B\";\n"
00127            << "BEGIN_GROUP = IMAGE\n"
00128            << "\n\n"  // skip errBias and errRand fields
00129            << "  lineOffset = " << offset(vpgl_rational_camera<T>::V_INDX) << '\n'
00130            << "  sampOffset = " << offset(vpgl_rational_camera<T>::U_INDX) << '\n'
00131            << "  latOffset = " << offset(vpgl_rational_camera<T>::Y_INDX) << '\n'
00132            << "  longOffset = " << offset(vpgl_rational_camera<T>::X_INDX) << '\n'
00133            << "  heightOffset = " << offset(vpgl_rational_camera<T>::Z_INDX) << '\n'
00134            << "  lineScale = " << scale(vpgl_rational_camera<T>::V_INDX) << '\n'
00135            << "  sampScale = " << scale(vpgl_rational_camera<T>::U_INDX) << '\n'
00136            << "  latScale = " << scale(vpgl_rational_camera<T>::Y_INDX) << '\n'
00137            << "  longScale = " << scale(vpgl_rational_camera<T>::X_INDX) << '\n'
00138            << "  heightScale = " << scale(vpgl_rational_camera<T>::Z_INDX) << '\n';
00139   vnl_matrix_fixed<double,4,20> coeffs = this->coefficient_matrix();
00140   file_out << "  lineNumCoef = (";
00141   for (int i=0; i<20; i++) {
00142     file_out << "\n    " << coeffs[vpgl_rational_camera<T>::NEU_V][map[i]];
00143     if (i < 19)
00144       file_out << ',';
00145   }
00146   file_out << ");\n  lineDenCoef = (";
00147   for (int i=0; i<20; i++) {
00148     file_out << "\n    " << coeffs[vpgl_rational_camera<T>::DEN_V][map[i]];
00149     if (i < 19)
00150       file_out << ',';
00151   }
00152   file_out << ");\n  sampNumCoef = (";
00153   for (int i=0; i<20; i++) {
00154     file_out << "\n    " << coeffs[vpgl_rational_camera<T>::NEU_U][map[i]];
00155     if (i < 19)
00156       file_out << ',';
00157   }
00158   file_out << ");\n  sampDenCoef = (";
00159   for (int i=0; i<20; i++) {
00160     file_out << "\n    " << coeffs[vpgl_rational_camera<T>::DEN_U][map[i]];
00161     if (i < 19)
00162       file_out << ',';
00163   }
00164   file_out << ");\n"
00165            << "END_GROUP = IMAGE\n"
00166            << "END;\n"
00167 
00168            << "lvcs\n";
00169   double longitude, latitude, elevation;
00170   lvcs_.get_origin(latitude, longitude, elevation);
00171   file_out << longitude << '\n'
00172            << latitude << '\n'
00173            << elevation << '\n';
00174   return true;
00175 }
00176 // Binary I/O
00177 
00178 //: Binary read self from stream
00179 template <class T> void vpgl_local_rational_camera<T>::
00180 b_read(vsl_b_istream &is)
00181 {
00182   vpgl_rational_camera<T>::b_read(is);
00183   lvcs_.b_read(is);
00184   return;
00185 }
00186 
00187 
00188 //: Binary save self to stream.
00189 template <class T> void vpgl_local_rational_camera<T>::
00190 b_write(vsl_b_ostream &os) const
00191 {
00192   vpgl_rational_camera<T>::b_write(os);
00193   lvcs_.b_write(os);
00194   return;
00195 }
00196 
00197 // read from a file
00198 template <class T>
00199 vpgl_local_rational_camera<T>* read_local_rational_camera(vcl_string cam_path)
00200 {
00201   vcl_ifstream file_inp;
00202   file_inp.open(cam_path.c_str());
00203   if (!file_inp.good()) {
00204     vcl_cout << "error: bad filename: " << cam_path << vcl_endl;
00205     return 0;
00206   }
00207   return read_local_rational_camera<T>(file_inp);
00208 }
00209 //: read from an open istream
00210 template <class T>
00211 vpgl_local_rational_camera<T>* read_local_rational_camera(vcl_istream& istr)
00212 {
00213   vpgl_rational_camera<T>* rcam = read_rational_camera<T>(istr);
00214   if (!rcam)
00215     return 0;
00216   vcl_string input;
00217   bool good = false;
00218   bgeo_lvcs lvcs;
00219   while (!istr.eof()&&!good) {
00220     istr >> input;
00221     if (input=="lvcs")
00222     {
00223       double longitude, latitude, elevation;
00224       istr >> longitude >> latitude >> elevation;
00225       lvcs = bgeo_lvcs(latitude, longitude, elevation,
00226                      bgeo_lvcs::wgs84, bgeo_lvcs::DEG, bgeo_lvcs::METERS);
00227       good = true;
00228     }
00229   }
00230   if  (!good)
00231   {
00232     vcl_cout << "error: not a composite rational camera file\n";
00233     return 0;
00234   }
00235   return new vpgl_local_rational_camera<T>(lvcs, *rcam);
00236 }
00237 
00238 
00239 //: Write to stream
00240 template <class T>
00241 vcl_ostream&  operator<<(vcl_ostream& s, const vpgl_local_rational_camera<T>& c )
00242 {
00243   c.print(s);
00244   return s;
00245 }
00246 
00247 
00248 // Code for easy instantiation.
00249 #undef vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE
00250 #define vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE(T) \
00251 template class vpgl_local_rational_camera<T >; \
00252 template vcl_ostream& operator<<(vcl_ostream&, const vpgl_local_rational_camera<T >&); \
00253 template vpgl_local_rational_camera<T >* read_local_rational_camera(vcl_string); \
00254 template vpgl_local_rational_camera<T >* read_local_rational_camera(vcl_istream&)
00255 
00256 #endif // vpgl_local_rational_camera_txx_