vpgl_local_rational_camera.hxx
Go to the documentation of this file.
1 // This is core/vpgl/vpgl_local_rational_camera.hxx
2 #ifndef vpgl_local_rational_camera_hxx_
3 #define vpgl_local_rational_camera_hxx_
4 //:
5 // \file
6 #include <vector>
7 #include <fstream>
9 #ifdef _MSC_VER
10 # include <vcl_msvc_warnings.h>
11 #endif
12 #include <vgl/vgl_point_2d.h>
13 #include <vgl/vgl_point_3d.h>
14 //--------------------------------------
15 // Constructors
16 //
17 
18 // Create an identity projection, i.e. (x,y,z) identically maps to (x,y)
19 template <class T>
22 {
23 }
24 
25 //: Constructor from a rational camera and an affine matrix
26 template <class T>
29  vpgl_rational_camera<T> const& rcam):
30 
31  vpgl_rational_camera<T>(rcam), lvcs_(lvcs)
32 {
33 }
34 
35 //: Constructor from a rational camera and a geographic origin
36 template <class T>
38 vpgl_local_rational_camera(T longitude, T latitude, T elevation,
39  vpgl_rational_camera<T> const& rcam) :
40  vpgl_rational_camera<T>(rcam),
41  lvcs_(vpgl_lvcs(latitude, longitude, elevation,
42  vpgl_lvcs::wgs84, vpgl_lvcs::DEG, vpgl_lvcs::METERS))
43  {
44  }
45 
46 
47 template <class T>
49 {
50  return new vpgl_local_rational_camera<T>(*this);
51 }
52 
53 // Base projection method, x, y, z are in local Cartesian coordinates
54 template <class T>
55 void vpgl_local_rational_camera<T>::project(const T x, const T y, const T z,
56  T& u, T& v) const
57 {
58  //first convert to global geographic coordinates
59  double lon, lat, gz;
60  vpgl_lvcs& non_const_lvcs = const_cast<vpgl_lvcs&>(lvcs_);
61  non_const_lvcs.local_to_global(x, y, z, vpgl_lvcs::wgs84, lon, lat, gz);
62  vpgl_rational_camera<T>::project((T)lon, (T)lat, (T)gz, u, v);
63 }
64 
65 //vnl interface methods
66 template <class T>
69 {
70  vnl_vector_fixed<T, 2> image_point;
71  this->project(world_point[0], world_point[1], world_point[2],
72  image_point[0], image_point[1]);
73  return image_point;
74 }
75 
76 //vgl interface methods
77 template <class T>
79 {
80  T u = 0, v = 0;
81  this->project(world_point.x(), world_point.y(), world_point.z(), u, v);
82  return vgl_point_2d<T>(u, v);
83 }
84 
85 //: print the camera parameters
86 template <class T>
87 void vpgl_local_rational_camera<T>::print(std::ostream& s) const
88 {
90  s << lvcs_ <<'\n'
91  <<"------------------------------------------------\n\n";
92 }
93 
94 template <class T>
95 bool vpgl_local_rational_camera<T>::save(std::string cam_path)
96 {
97  std::ofstream file_out;
98  file_out.open(cam_path.c_str());
99  if (!file_out.good()) {
100  std::cerr << "error: bad filename: " << cam_path << std::endl;
101  return false;
102  }
103  file_out.precision(12);
104 
105  int map[20];
106  map[0]=19;
107  map[1]=9;
108  map[2]=15;
109  map[3]=18;
110  map[4]=6;
111  map[5]=8;
112  map[6]=14;
113  map[7]=3;
114  map[8]=12;
115  map[9]=17;
116  map[10]=5;
117  map[11]=0;
118  map[12]=4;
119  map[13]=7;
120  map[14]=1;
121  map[15]=10;
122  map[16]=13;
123  map[17]=2;
124  map[18]=11;
125  map[19]=16;
126 
127  file_out << "satId = \"????\";\n"
128  << "bandId = \"RGB\";\n"
129  << "SpecId = \"RPC00B\";\n"
130  << "BEGIN_GROUP = IMAGE\n"
131  << "\n\n" // skip errBias and errRand fields
136  << " heightOffset = " << vpgl_rational_camera<T>::offset(vpgl_rational_camera<T>::Z_INDX) << '\n'
141  << " heightScale = " << vpgl_rational_camera<T>::scale(vpgl_rational_camera<T>::Z_INDX) << '\n';
142  vnl_matrix_fixed<T,4,20> coeffs = this->coefficient_matrix();
143  file_out << " lineNumCoef = (";
144  for (int i=0; i<20; i++) {
145  file_out << "\n " << coeffs[vpgl_rational_camera<T>::NEU_V][map[i]];
146  if (i < 19)
147  file_out << ',';
148  }
149  file_out << ");\n lineDenCoef = (";
150  for (int i=0; i<20; i++) {
151  file_out << "\n " << coeffs[vpgl_rational_camera<T>::DEN_V][map[i]];
152  if (i < 19)
153  file_out << ',';
154  }
155  file_out << ");\n sampNumCoef = (";
156  for (int i=0; i<20; i++) {
157  file_out << "\n " << coeffs[vpgl_rational_camera<T>::NEU_U][map[i]];
158  if (i < 19)
159  file_out << ',';
160  }
161  file_out << ");\n sampDenCoef = (";
162  for (int i=0; i<20; i++) {
163  file_out << "\n " << coeffs[vpgl_rational_camera<T>::DEN_U][map[i]];
164  if (i < 19)
165  file_out << ',';
166  }
167  file_out << ");\n"
168  << "END_GROUP = IMAGE\n"
169  << "END;\n"
170 
171  << "lvcs\n";
172  double longitude, latitude, elevation;
173  lvcs_.get_origin(latitude, longitude, elevation);
174  file_out << longitude << '\n'
175  << latitude << '\n'
176  << elevation << '\n';
177  return true;
178 }
179 
180 // read from a file
181 template <class T>
183 {
184  std::ifstream file_inp;
185  file_inp.open(cam_path.c_str());
186  if (!file_inp.good()) {
187  std::cout << "error: bad filename: " << cam_path << std::endl;
188  return nullptr;
189  }
190  return read_local_rational_camera<T>(file_inp);
191 }
192 //: read from an open istream
193 template <class T>
195 {
196  vpgl_rational_camera<T>* rcam = read_rational_camera<T>(istr);
197  if (!rcam)
198  return nullptr;
199  std::string input;
200  bool good = false;
201  vpgl_lvcs lvcs;
202  while (!istr.eof()&&!good) {
203  istr >> input;
204  if (input=="lvcs")
205  {
206  double longitude, latitude, elevation;
207  istr >> longitude >> latitude >> elevation;
208  lvcs = vpgl_lvcs(latitude, longitude, elevation,
210  good = true;
211  }
212  }
213  if (!good)
214  {
215  //std::cout << "error: not a composite rational camera file\n";
216  return nullptr;
217  }
218  return new vpgl_local_rational_camera<T>(lvcs, *rcam);
219 }
220 template <class T>
222 
223  vpgl_rational_camera<T>* rcam = read_rational_camera_from_txt<T>(cam_path);
224 
225  if(! rcam){
226  std::cout << "Failed to read rational camera part of " << cam_path << '\n';
227  return nullptr;
228  }
229  std::ifstream file_inp;
230  file_inp.open(cam_path.c_str());
231  if (!file_inp.good()) {
232  std::cout << "error: bad filename: " << cam_path << std::endl;
233  return nullptr;
234  }
235  bool good = false;
236  vpgl_lvcs lvcs;
237  std::string input;
238  while (!file_inp.eof()&&!good) {
239  file_inp >> input;
240  if (input=="lvcs")
241  {
242  double longitude, latitude, elevation;
243  file_inp >> longitude >> latitude >> elevation;
244  lvcs = vpgl_lvcs(latitude, longitude, elevation,
246  good = true;
247  }
248  }
249  if (!good)
250  {
251  //std::cout << "error: not a composite rational camera file\n";
252  return nullptr;
253  }
254  return new vpgl_local_rational_camera<T>(lvcs, *rcam);
255 }
256 
257 //: Write to stream
258 template <class T>
259 std::ostream& operator<<(std::ostream& s, const vpgl_local_rational_camera<T>& c )
260 {
261  c.print(s);
262  return s;
263 }
264 
265 
266 // Code for easy instantiation.
267 #undef vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE
268 #define vpgl_LOCAL_RATIONAL_CAMERA_INSTANTIATE(T) \
269 template class vpgl_local_rational_camera<T >; \
270 template std::ostream& operator<<(std::ostream&, const vpgl_local_rational_camera<T >&); \
271 template vpgl_local_rational_camera<T >* read_local_rational_camera(std::string); \
272  template vpgl_local_rational_camera<T >* read_local_rational_camera_from_txt(std::string); \
273 template vpgl_local_rational_camera<T >* read_local_rational_camera(std::istream&)
274 
275 #endif // vpgl_local_rational_camera_hxx_
void project(const T x, const T y, const T z, T &u, T &v) const override
The generic camera interface. u represents image column, v image row.
void local_to_global(const double lx, const double ly, const double lz, cs_names cs_name, double &lon, double &lat, double &gz, AngUnits output_ang_unit=DEG, LenUnits output_len_unit=METERS)
Converts pointin, given in local vertical coord system, to pointout in the global coord system given ...
Definition: vpgl_lvcs.cxx:350
vpgl_local_rational_camera< T > * clone(void) const override
Clone ‘this’: creation of a new object and initialization.
void print(std::ostream &s=std::cout) const override
print the camera parameters.
vpgl_local_rational_camera< T > * read_local_rational_camera(std::string cam_path)
Creates a local rational camera from a file.
#define v
vnl_vector_fixed< T, 2 > project(vnl_vector_fixed< T, 3 > const &world_point) const override
Project a world point onto the image.
std::ostream & operator<<(std::ostream &s, const vpgl_local_rational_camera< T > &p)
Write to stream.
A local rational camera model.
T scale(const coor_index coor_index) const
get a specific scale value.
vpgl_local_rational_camera()
default constructor.
virtual void print(std::ostream &s=std::cout) const
print the camera parameters.
T offset(const coor_index coor_index) const
get a specific offset value.
vpgl_local_rational_camera< T > * read_local_rational_camera_from_txt(std::string cam_path)
read camera from txt file (small sat format).
void project(const T x, const T y, const T z, T &u, T &v) const override
The generic camera interface. u represents image column, v image row.
bool save(std::string cam_path) override
save to file (the lvcs is after the global rational camera parameters).