1 // This is brl/bseg/boxm2/volm/boxm2_volm_matcher_p1.h
2 #ifndef boxm2_volm_matcher_p1_h_
3 #define boxm2_volm_matcher_p1_h_
4 //:
5 // \file
6 // \brief Match a voxelized query volume to an indexed reference volume.
7 //  A class to match a voxelized query volume to an indexed reference volume
8 //  based on the depth value and relative order of voxels.  The index and hypotheses
9 //  are now referred by volm_geo_index
10 //
11 // \author Yi Dong
12 // \date January 21, 2012
13 // \verbatim
14 //  Modifications
15 //   <none yet>
16 // \endverbatim
17 //
18 
19 #include <utility>
20 #include "boxm2_volm_wr3db_index_sptr.h"
21 #include "boxm2_volm_wr3db_index.h"
22 #include <volm/volm_camera_space.h>
23 #include <volm/volm_camera_space_sptr.h>
24 #include <volm/volm_query_sptr.h>
25 #include <volm/volm_query.h>
26 #include <volm/volm_io.h>
27 #include <volm/volm_geo_index_sptr.h>
28 #include <volm/volm_geo_index.h>
29 #include <volm/volm_loc_hyp.h>
30 #include <volm/volm_loc_hyp_sptr.h>
31 #include <bocl/bocl_manager.h>
32 #include <bocl/bocl_device.h>
33 #include <bocl/bocl_kernel.h>
34 #include <bocl/bocl_mem.h>
35 
36 
37 class boxm2_volm_score_out;
38 
39 class boxm2_volm_matcher_p1
40 {
41  public:
42   //: default constructor
43   boxm2_volm_matcher_p1() = default;
44   //: constructor
45   boxm2_volm_matcher_p1(volm_camera_space_sptr const& cam_space,
46                         volm_query_sptr const& query,
47                         std::vector<volm_geo_index_node_sptr>  leaves,
48                         float const& buffer_capacity,
49                         std::string const& geo_index_folder,
50                         unsigned const& tile_id,
51                         std::vector<float>  depth_interval,
52                         vgl_polygon<double> const& cand_poly,
53                         const bocl_device_sptr& gpu,
54                         bool const& is_candidate,
55                         bool const& is_last_pass,
56                         std::string  out_folder,
57                         float const& threshold,
58                         unsigned const& max_cam_per_loc,
59                         std::vector<volm_weight> weights);
60 
61   //: destructor
62   ~boxm2_volm_matcher_p1();
63   //: matcher function
64   bool volm_matcher_p1(int const& num_locs_to_kernel = -1);
65   //: generate output -- probability map, binary score file, etc
66   bool write_matcher_result(std::string const& tile_fname_bin, std::string const& tile_fname_txt);
67   bool write_matcher_result(std::string const& tile_fname_bin);
68   //: for testing purpose -- output score for all camera (should only be used for ground truth location)
69   bool write_gt_cam_score(unsigned const& leaf_id, unsigned const& hypo_id, std::string const& out_fname);
70 
71 
72  private:
73   //: query, indices, device
74   volm_camera_space_sptr                        cam_space_;
75   std::vector<unsigned>                  valid_cam_indices_;
76   volm_query_sptr                                   query_;
77   std::vector<volm_geo_index_node_sptr>             leaves_;
78   boxm2_volm_wr3db_index_sptr                         ind_;
79   boxm2_volm_wr3db_index_sptr                  ind_orient_;
80   boxm2_volm_wr3db_index_sptr                   ind_label_;
81   float                                        ind_buffer_;
82   std::stringstream                          file_name_pre_;
83   std::vector<volm_weight>                         weights_;
84 
85   //: land fallback category table size
86   unsigned char                             fallback_size_;
87   unsigned char*                       fallback_size_buff_;
88   bocl_mem*                          fallback_size_cl_mem_;
89   //: shell container size
90   unsigned                                     layer_size_;
91   unsigned*                               layer_size_buff_;
92   bocl_mem*                             layer_size_cl_mem_;
93   //: candidate list
94   bool                                       is_candidate_;
95   vgl_polygon<double>                           cand_poly_;
96   //: score profile from last matcher
97   bool                                       is_last_pass_;
98   std::string                                   out_folder_;
99   //: depth interval table
100   std::vector<float>                        depth_interval_;
101   //: kernel related
102   bocl_device_sptr                                    gpu_;
103   std::size_t                             local_threads_[2];
104   std::size_t                            global_threads_[2];
105   cl_uint                                        work_dim_;
106   cl_command_queue                                  queue_;
107   cl_ulong                               query_global_mem_;
108   cl_ulong                                query_local_mem_;
109   cl_ulong                              device_global_mem_;
110   cl_ulong                               device_local_mem_;
111   std::map<std::string, std::vector<bocl_kernel*> >  kernels_;
112 
113   //: query related
114   bool                       is_grd_reg_;
115   bool                       is_sky_reg_;
116   bool                       is_obj_reg_;
117   unsigned*                       n_cam_;
118   bocl_mem*                n_cam_cl_mem_;
119   unsigned*                       n_obj_;
120   bocl_mem*                n_obj_cl_mem_;
121 
122   unsigned*                 grd_id_buff_;
123   bocl_mem*               grd_id_cl_mem_;
124   unsigned char*          grd_dist_buff_;
125   bocl_mem*             grd_dist_cl_mem_;
126   unsigned char*          grd_land_buff_;
127   bocl_mem*             grd_land_cl_mem_;
128   float*              grd_land_wgt_buff_;
129   bocl_mem*         grd_land_wgt_cl_mem_;
130   unsigned*          grd_id_offset_buff_;
131   bocl_mem*        grd_id_offset_cl_mem_;
132   float*                grd_weight_buff_;
133   bocl_mem*           grd_weight_cl_mem_;
134   float*             grd_wgt_attri_buff_;
135   bocl_mem*        grd_wgt_attri_cl_mem_;
136 
137   unsigned*                 sky_id_buff_;
138   bocl_mem*               sky_id_cl_mem_;
139   unsigned*          sky_id_offset_buff_;
140   bocl_mem*        sky_id_offset_cl_mem_;
141   float*                sky_weight_buff_;
142   bocl_mem*           sky_weight_cl_mem_;
143 
144   unsigned*                 obj_id_buff_;
145   bocl_mem*               obj_id_cl_mem_;
146   unsigned*          obj_id_offset_buff_;
147   bocl_mem*        obj_id_offset_cl_mem_;
148 
149   unsigned char*      obj_min_dist_buff_;
150   bocl_mem*         obj_min_dist_cl_mem_;
151   unsigned char*         obj_order_buff_;
152   bocl_mem*            obj_order_cl_mem_;
153   float*                obj_weight_buff_;
154   bocl_mem*           obj_weight_cl_mem_;
155   float*             obj_wgt_attri_buff_;
156   bocl_mem*        obj_wgt_attri_cl_mem_;
157   unsigned char*        obj_orient_buff_;
158   bocl_mem*           obj_orient_cl_mem_;
159   unsigned char*          obj_land_buff_;
160   bocl_mem*             obj_land_cl_mem_;
161   float*              obj_land_wgt_buff_;
162   bocl_mem*         obj_land_wgt_cl_mem_;
163 
164   //: depth interval
165   float*            depth_interval_buff_;
166   bocl_mem*       depth_interval_cl_mem_;
167   unsigned*           depth_length_buff_;
168   bocl_mem*         depth_length_cl_mem_;
169 
170   //: indices related
171   unsigned*                       n_ind_;
172 
173   //: output related
174   // threshold that only the camera with score higher than threshold will be considered to put into output
175   float                            threshold_;
176   // maximum number of cameras for each location
177   unsigned                   max_cam_per_loc_;
178   std::vector<volm_score_sptr>      score_all_;
179   std::vector<boxm2_volm_score_out> score_cam_;
180 
181   //: transfer volm_query to 1D array for kernel
182   bool transfer_query();
183   //: transfer volm_query orientation information to 1D array for kernel
184   bool transfer_orient();
185   //: transfer volm_weight parameters to 1D array for kernel
186   bool transfer_weight();
187   //: read given number of indices from volm_geo_index, with two index files, index depth and index orientation
188   bool fill_index(unsigned const& n_ind,
189                   unsigned const& layer_size,
190                   unsigned& leaf_id,
191                   unsigned char* index_buff,
192                   unsigned char* index_orient_buff,
193                   unsigned char* index_land_buff,
194                   std::vector<unsigned>& l_id,
195                   std::vector<unsigned>& h_id,
196                   unsigned& actual_n_ind);
197   //: check the given leaf has un-read hypothesis or not
198   bool is_leaf_finish(unsigned const& leaf_id);
199   //: clear all query cl_mem pointer
200   bool clean_query_cl_mem();
201   //: clear all weight cl_mem pointer
202   bool clean_weight_cl_mem();
203   //: compile kernel
204   bool compile_kernel(std::vector<bocl_kernel*>& vec_kernels);
205   //: create queue
206   bool create_queue();
207   //: check whether a given point is inside the candidate polygon
208   bool inside_candidate(vgl_polygon<double> const& poly, double const& x, double const& y);
209 
210 #if 0
211   //: kernel execution function
212   bool execute_matcher_kernel(bocl_device_sptr                         device,
213                               cl_command_queue&                         queue,
214                               std::vector<bocl_kernel*>                   kern,
215                               bocl_mem*                         n_ind_cl_mem_,
216                               bocl_mem*                         index_cl_mem_,
217                               bocl_mem*                         score_cl_mem_,
218                               bocl_mem*                            mu_cl_mem_);
219 #endif
220 
221   // kernel execution function with orientation
222   bool execute_matcher_kernel_orient(const bocl_device_sptr&                  device,
223                                      cl_command_queue&                  queue,
224                                      std::vector<bocl_kernel*>        kern_vec,
225                                      bocl_mem*                  n_ind_cl_mem_,
226                                      bocl_mem*                  index_cl_mem_,
227                                      bocl_mem*           index_orient_cl_mem_,
228                                      bocl_mem*             index_land_cl_mem_,
229                                      bocl_mem*                  score_cl_mem_,
230                                      bocl_mem*                     mu_cl_mem_);
231 
232   //: a test function to check the kernel implementation
233   bool volm_matcher_p1_test_ori(unsigned n_ind,
234                                 unsigned char* index,
235                                 const unsigned char* index_ori,
236                                 const unsigned char* index_lnd,
237                                 float* score_buff,
238                                 float* mu_buff);
239 };
240 
241 class boxm2_volm_score_out
242 {
243  public:
244   boxm2_volm_score_out() = default;
boxm2_volm_score_out(unsigned const & leaf_id,unsigned const & hypo_id,std::vector<unsigned> cam_id,std::vector<float> cam_score)245   boxm2_volm_score_out(unsigned const& leaf_id, unsigned const& hypo_id,
246                        std::vector<unsigned>  cam_id,
247                        std::vector<float>  cam_score)
248   : l_id_(leaf_id), h_id_(hypo_id), cam_id_(std::move(cam_id)), cam_score_(std::move(cam_score)) {}
249   ~boxm2_volm_score_out() = default;
250 
251   unsigned l_id_;
252   unsigned h_id_;
253   std::vector<unsigned> cam_id_;
254   std::vector<float> cam_score_;
255 };
256 
257 
258 #endif // boxm2_volm_matcher_p1_h_
259