core/vpgl/vpgl_local_rational_camera.txx
Go to the documentation of this file.
00001 // This is core/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(vpgl_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_(vpgl_lvcs(latitude, longitude, elevation,
00039                   vpgl_lvcs::wgs84, vpgl_lvcs::DEG, vpgl_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   vpgl_lvcs& non_const_lvcs = const_cast<vpgl_lvcs&>(lvcs_);
00058   non_const_lvcs.local_to_global(x, y, z, vpgl_lvcs::wgs84, lon, lat, gz);
00059   vpgl_rational_camera<T>::project((T)lon, (T)lat, (T)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 = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::V_INDX) << '\n'
00130            << "  sampOffset = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::U_INDX) << '\n'
00131            << "  latOffset = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::Y_INDX) << '\n'
00132            << "  longOffset = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::X_INDX) << '\n'
00133            << "  heightOffset = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::Z_INDX) << '\n'
00134            << "  lineScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::V_INDX) << '\n'
00135            << "  sampScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::U_INDX) << '\n'
00136            << "  latScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::Y_INDX) << '\n'
00137            << "  longScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::X_INDX) << '\n'
00138            << "  heightScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::Z_INDX) << '\n';
00139   vnl_matrix_fixed<T,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 
00177 // read from a file
00178 template <class T>
00179 vpgl_local_rational_camera<T>* read_local_rational_camera(vcl_string cam_path)
00180 {
00181   vcl_ifstream file_inp;
00182   file_inp.open(cam_path.c_str());
00183   if (!file_inp.good()) {
00184     vcl_cout << "error: bad filename: " << cam_path << vcl_endl;
00185     return 0;
00186   }
00187   return read_local_rational_camera<T>(file_inp);
00188 }
00189 //: read from an open istream
00190 template <class T>
00191 vpgl_local_rational_camera<T>* read_local_rational_camera(vcl_istream& istr)
00192 {
00193   vpgl_rational_camera<T>* rcam = read_rational_camera<T>(istr);
00194   if (!rcam)
00195     return 0;
00196   vcl_string input;
00197   bool good = false;
00198   vpgl_lvcs lvcs;
00199   while (!istr.eof()&&!good) {
00200     istr >> input;
00201     if (input=="lvcs")
00202     {
00203       double longitude, latitude, elevation;
00204       istr >> longitude >> latitude >> elevation;
00205       lvcs = vpgl_lvcs(latitude, longitude, elevation,
00206                        vpgl_lvcs::wgs84, vpgl_lvcs::DEG, vpgl_lvcs::METERS);
00207       good = true;
00208     }
00209   }
00210   if  (!good)
00211   {
00212     vcl_cout << "error: not a composite rational camera file\n";
00213     return 0;
00214   }
00215   return new vpgl_local_rational_camera<T>(lvcs, *rcam);
00216 }
00217 
00218 
00219 //: Write to stream
00220 template <class T>
00221 vcl_ostream&  operator<<(vcl_ostream& s, const vpgl_local_rational_camera<T>& c )
00222 {
00223   c.print(s);
00224   return s;
00225 }
00226 
00227 
00228 // Code for easy instantiation.
00229 #undef vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE
00230 #define vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE(T) \
00231 template class vpgl_local_rational_camera<T >; \
00232 template vcl_ostream& operator<<(vcl_ostream&, const vpgl_local_rational_camera<T >&); \
00233 template vpgl_local_rational_camera<T >* read_local_rational_camera(vcl_string); \
00234 template vpgl_local_rational_camera<T >* read_local_rational_camera(vcl_istream&)
00235 
00236 #endif // vpgl_local_rational_camera_txx_