1 // This is brl/bseg/bvpl/util/bvpl_bundler_features_2d.cxx
2 #include <iostream>
3 #include <cstdlib>
4 #include <iterator>
5 #include "bvpl_bundler_features_2d.h"
6 //
7 #include <vgl/io/vgl_io_point_3d.h>
8 #include <vnl/io/vnl_io_vector.h>
9 
10 #ifdef _MSC_VER
11 #  include "vcl_msvc_warnings.h"
12 #endif
13 
write_feature_txt(std::string const & filename) const14 void bvpl_bundler_features_2d::write_feature_txt( std::string const& filename ) const
15 {
16     std::ofstream of(filename.c_str());
17 
18     if (!of)
19     {
20         std::cerr << "----ERROR---- bvpl_bundler_features_2d::write_txt\n"
21                  << "\tCOULD NOT OPEN FILE: " << filename
22                  << " for writing.\n"
23                  << __FILE__ << '\n'
24                  << __LINE__ << '\n' << std::flush;
25         std::exit(-1);
26     }
27 
28     point_view_feature_map_type::const_iterator
29         p_itr, p_end = this->pt_view_feature_map.end();
30 
31     of << "points_3d = [ ";
32     for ( p_itr = this->pt_view_feature_map.begin();
33           p_itr != p_end; ++p_itr )
34     {
35         std::map<unsigned, vnl_vector<double> >::const_iterator
36             v_itr, v_end = p_itr->second.end();
37 
38         for ( v_itr = p_itr->second.begin();
39               v_itr != v_end; ++v_itr )
40         {
41             of << v_itr->second << '\n';
42         } //end view iteration
43     } //end 3d point iteration
44 
45 }//end bvpl_bundler_features_2d::write_mfile
46 
write_txt(std::string const & filename) const47 void bvpl_bundler_features_2d::write_txt( std::string const& filename ) const
48 {
49   std::ofstream feature_file(filename.c_str());
50 
51   if (!feature_file)
52   {
53     std::cerr << "----ERROR---- bvpl_bundler_features_2d::write_txt\n"
54              << "\tCOULD NOT OPEN FILE: " << filename
55              << " for writing.\n"
56              << __FILE__ << '\n'
57              << __LINE__ << '\n' << std::flush;
58     std::exit(-1);
59   }
60 
61   point_view_feature_map_type::const_iterator
62     p_itr, p_end = this->pt_view_feature_map.end();
63 
64   for ( p_itr = this->pt_view_feature_map.begin();
65         p_itr != p_end; ++p_itr )
66   {
67     //output the 3d point
68     feature_file << p_itr->first << '\n';
69 
70     std::map<unsigned, vnl_vector<double> >::const_iterator
71       v_itr, v_end = p_itr->second.end();
72 
73     //output the number of views
74     feature_file << p_itr->second.size() << '\n';
75 
76     for ( v_itr = p_itr->second.begin();
77           v_itr != v_end; ++v_itr )
78     {
79       //output the view number
80       feature_file << v_itr->first << '\n';
81 
82       //output the vector
83       feature_file << v_itr->second << '\n';
84     }//end view iteration
85   }//end point iteration
86 
87   return;
88 }//end write_txt
89 
b_write(vsl_b_ostream & os) const90 void bvpl_bundler_features_2d::b_write( vsl_b_ostream& os ) const
91 {
92   constexpr short version_no = 1;
93   vsl_b_write(os, version_no);
94 
95   //write the number of points
96   vsl_b_write(os, this->pt_view_feature_map.size());
97 
98   point_view_feature_map_type::const_iterator
99   pt_itr, pt_end = this->pt_view_feature_map.end();
100 
101   for ( pt_itr = this->pt_view_feature_map.begin();
102         pt_itr != pt_end; ++pt_itr )
103   {
104     //write the point
105     vsl_b_write(os, pt_itr->first);
106 
107     //write the number of views
108     vsl_b_write(os, pt_itr->second.size());
109 
110     std::map<unsigned, vnl_vector<double> >::const_iterator
111       v_itr, v_end = pt_itr->second.end();
112 
113     for ( v_itr = pt_itr->second.begin();
114           v_itr != v_end; ++v_itr )
115     {
116       //write the view number
117       vsl_b_write(os, v_itr->first);
118 
119       //write the feature
120       vsl_b_write(os, v_itr->second);
121     }//end view iteration
122   }//end point iteration
123 
124   return;
125 }//end b_write
126 
b_read(vsl_b_istream & is)127 void bvpl_bundler_features_2d::b_read( vsl_b_istream& is )
128 {
129   if ( !is ) return;
130 
131   short v;
132   vsl_b_read(is,v);
133 
134   switch (v)
135   {
136    case 1:
137     {
138       //read the number of points
139       std::size_t npoints;
140       vsl_b_read(is,npoints);
141 
142       for ( std::size_t i = 0; i < npoints; ++i )
143       {
144         //read the point
145         vgl_point_3d<double> pt;
146         vsl_b_read(is,pt);
147 
148         //read the number of views
149         std::size_t nviews;
150         vsl_b_read(is,nviews);
151 
152         std::map<unsigned,vnl_vector<double> > view_feature_map;
153 
154         for ( std::size_t j = 0; j < nviews; ++j )
155         {
156           //read the view number
157           unsigned view_number;
158           vsl_b_read(is,view_number);
159 
160           //read the feature
161           vnl_vector<double> v;
162           vsl_b_read(is,v);
163 
164           view_feature_map.insert(std::make_pair(view_number,v));
165         }//end view iteration
166 
167         this->pt_view_feature_map.insert(std::make_pair(pt,view_feature_map));
168       }//end point iteration
169       break;
170     }//end case 1
171    default:
172     {
173       std::cerr << "----ERROR---- bof_bundler_features_2d::b_read\n"
174                << "\tUNKNOWN I/O VERSION\n"
175                << __FILE__ << '\n'
176                << __LINE__ << '\n'
177                << std::flush;
178       std::exit(-1);
179     }//end default
180   }//end switch
181 
182   return;
183 }//end b_read
184