1 #include "boxm2_convert_nvm_txt.h"
2 #include "boxm2_point_util.h"
3 //:
4 // \file
5 #include <cassert>
6 #include <utility>
7 #include <vgl/algo/vgl_rotation_3d.h>
8 #include "vgl/vgl_box_3d.h"
9 #include "vnl/vnl_double_3.h"
10 #include "vnl/vnl_matrix_fixed.h"
11 #include "vnl/vnl_quaternion.h"
12 #include <vsph/vsph_camera_bounds.h>
13 #ifdef _MSC_VER
14 #  include "vcl_msvc_warnings.h"
15 #endif
16 #include "vul/vul_file.h"
17 
18 //: Main boxm2_convert_nvm_txt function
19 //  Takes in bundle.out file and image directory that created img_dir
boxm2_util_convert_nvm_txt(std::string nvm_file,std::string img_dir,std::map<std::string,vpgl_perspective_camera<double> * > & cams,std::map<std::string,std::string> & img_name_mapping)20 void boxm2_util_convert_nvm_txt(std::string nvm_file,
21                                     std::string img_dir,
22                                     std::map<std::string, vpgl_perspective_camera<double>* >& cams,
23                                     std::map<std::string, std::string >& img_name_mapping)
24 {
25 
26   boxm2_convert_nvm_txt b2s(std::move(nvm_file), std::move(img_dir));
27   cams        = b2s.get_cams();
28   img_name_mapping = b2s.get_img_name_mapping();
29 }
30 
31 // reads bundler file and populates list of cameras, and a scene bounding box
boxm2_convert_nvm_txt(const std::string & nvm_file,const std::string & img_dir)32 boxm2_convert_nvm_txt::boxm2_convert_nvm_txt(const std::string& nvm_file, const std::string& img_dir)
33 {
34   img_dir_ = img_dir;
35   nvm_file_ = nvm_file;
36 
37   // verify image dir
38   if (!vul_file::is_directory(img_dir.c_str()))
39   {
40     std::cout<<"boxm2_convert_nvm_txt::Image directory does not exist"<<std::endl;
41     return;
42   }
43 
44   // open the bundler file
45   std::ifstream bfile( nvm_file.c_str() );
46   if (!bfile)
47   {
48     std::cout<<"boxm2_convert_nvm_txt::Error Opening Bundler output file: " << nvm_file <<std::endl;
49     return;
50   }
51 
52   this->read_cameras(bfile);
53 
54   //write final mapping
55   for (unsigned i = 0; i < cams_.size(); ++i) {
56     std::string old_file_name = vul_file::strip_directory(old_names_[i]) ;
57     std::cout << "Old file name " <<  old_file_name << std::endl;
58     auto* cam = new CamType(cams_[i]);
59     final_cams_[old_file_name] = cam;
60     img_name_map_[old_file_name] = names_[i];
61   }
62 
63 }
64 
65 //------------------------------------------------------------------------
66 // reading the cameras from nvm file
67 //------------------------------------------------------------------------
read_cameras(std::ifstream & in)68 bool boxm2_convert_nvm_txt::read_cameras(std::ifstream& in)
69 {
70 
71   std::string token;
72   //read the header
73   for (unsigned int i = 0; i < 19; ++i)
74     {
75     std::getline(in, token); // was: in >> token; //file header
76     std::cout << token << std::endl;
77     }
78 
79 
80   // read # of cameras
81   int ncam = 0;
82   in >> ncam;
83   if (ncam <= 1) {
84     std::cout<<"Found fewer than 1 camera in NVM file (" << ncam<<')' <<std::endl;
85     return false;
86   }
87   std::cout<<"Found "<<ncam<<" cameras in nvm file"<<std::endl;
88 
89 
90   //read the camera parameters
91   cams_.resize(ncam); // allocate the camera data
92   names_.resize(ncam); // allocate token data
93   old_names_.resize(ncam); // allocate token data
94 
95   for (int i = 0; i < ncam; ++i)
96   {
97     std::getline(in, token); //empty line
98     std::getline(in, token); //empty line
99 
100     std::string img_name,old_file_name;
101     std::getline(in, img_name);
102     std::getline(in, old_file_name);
103 
104     //internal param
105     double f, q[9], c[3], p_x,p_y;
106     in >> f;
107     in >>  p_x >> p_y;
108 
109 
110     vgl_point_2d<double> ppoint(p_x,p_y);
111     vpgl_calibration_matrix<double> K(f,ppoint);
112 
113     //external
114     std::getline(in, token);
115     in >> c[0] >> c[1] >> c[2];
116     for (int j = 0; j < 3; ++j) in >> q[j]; //camera center....
117     std::getline(in, token);
118     for (int j = 0; j < 3; ++j) in >> q[j]; //axis angle rot
119     std::getline(in, token);
120     for (int j = 0; j < 4; ++j) in >> q[j]; //quaternion rot
121     std::getline(in, token);
122     for (double & j : q) in >> j; //rot matrix
123     std::getline(in, token);
124     std::getline(in, token);
125 
126     vnl_matrix_fixed<double,3,3> r;
127     r(0,0) = q[0];  r(0,1) = q[1];  r(0,2) = q[2];
128     r(1,0) = q[3];  r(1,1) = q[4];  r(1,2) = q[5];
129     r(2,0) = q[6];  r(2,1) = q[7];  r(2,2) = q[8];
130 
131     vgl_rotation_3d<double> rot(r);
132     vgl_vector_3d<double> t(c[0],c[1],c[2]);
133 
134     vpgl_perspective_camera<double> cam(K,rot,t);
135     cams_[i] = cam;
136     std::cout << cam << std::endl;
137 
138     names_[i] = img_name;
139     old_names_[i] = old_file_name;
140   }
141   return true;
142 }
143