1 #include "boxm2_vecf_ocl_ssd_func.h"
2 
boxm2_vecf_ocl_ssd_func(unsigned nx,unsigned ni,unsigned nj,const boxm2_vecf_ocl_transform_scene_sptr & tscn,const boxm2_scene_sptr & target_scene,const boxm2_opencl_cache_sptr & ocl_cache)3 boxm2_vecf_ocl_ssd_func::boxm2_vecf_ocl_ssd_func(unsigned nx, unsigned ni, unsigned nj, const boxm2_vecf_ocl_transform_scene_sptr& tscn,const boxm2_scene_sptr& target_scene,const boxm2_opencl_cache_sptr& ocl_cache):
4   ni_(ni), nj_(nj),
5   vnl_least_squares_function(nx, ni*nj, vnl_least_squares_function::no_gradient), tscn_(tscn),renderer_(target_scene,ocl_cache),depth_renderer_(target_scene , ocl_cache)
6 {
7 }
8 
f(vnl_vector<double> const & x,vnl_vector<double> & fx)9 void boxm2_vecf_ocl_ssd_func::f(vnl_vector<double> const& x, vnl_vector<double>& fx){
10   vil_image_view<float> diff = this->diff_img(x);
11   unsigned k = 0;
12   for(unsigned j = 0; j<nj_; ++j)
13     for(unsigned i = 0; i<ni_; ++i, ++k)
14       fx[k]=diff(i,j);
15 }
16 
diff_img(vnl_vector<double> const & x)17 vil_image_view<float> boxm2_vecf_ocl_ssd_func::diff_img(vnl_vector<double> const& x) {
18   vil_image_view<float> ret(ni_, nj_);
19   vgl_vector_3d<double> scale;
20   this->x_to_scale(x, scale);
21   tscn_->transform_1_blk_interp(rot_, trans_, scale, false);
22   vil_image_view<float> exp, vis;
23 
24   this->render_scene_appearance(ref_cam_, exp, vis, ni_, nj_);
25   for(unsigned j = 0; j<nj_; ++j)
26     for(unsigned i = 0; i<ni_; ++i){
27       float vi= vis(i,j), ex = exp(i,j), re = ref_img_(i,j);
28       float d = std::fabs((ex-re)*vi);
29       ret(i,j)=d;
30     }
31   return ret;
32 }
finish()33 void boxm2_vecf_ocl_ssd_func::finish(){
34   vil_image_view<float> exp, vis;
35   vgl_vector_3d<double> scale(1.0, 1.0, 1.0);
36   this->render_scene_appearance(ref_cam_, exp, vis, ni_, nj_);
37   tscn_->transform_1_blk_interp(rot_, trans_, scale, true);
38 }
39 
40   //:render the current state of the target scene leaving scene GPU buffers in place
41   // thus rendering can be faster since block buffer transfers are not needed
42   //:render the depth of the current state of the target scene leaving scene GPU buffers in place
43 
44 
render_scene_appearance(vpgl_camera_double_sptr const & cam,vil_image_view<float> & expected_img,vil_image_view<float> & vis_img,unsigned ni,unsigned nj)45 bool boxm2_vecf_ocl_ssd_func::render_scene_appearance(vpgl_camera_double_sptr const& cam,
46                         vil_image_view<float>& expected_img, vil_image_view<float>& vis_img,
47                         unsigned ni, unsigned nj) {
48 
49   bool status = renderer_.render(cam, ni, nj);
50   renderer_.get_last_vis(vis_img);
51   renderer_.get_last_rendered(expected_img);
52 
53   return status;
54 }
55 
render_scene_depth(vpgl_camera_double_sptr const & cam,vil_image_view<float> & expected_depth,vil_image_view<float> & vis_img,unsigned ni,unsigned nj)56 bool boxm2_vecf_ocl_ssd_func::render_scene_depth(vpgl_camera_double_sptr const & cam,
57                                                         vil_image_view<float>& expected_depth,
58                                                         vil_image_view<float>& vis_img,
59                                                         unsigned ni, unsigned nj)
60 {
61   bool status = depth_renderer_.render(cam, ni, nj);
62   depth_renderer_.get_last_vis(vis_img);
63   depth_renderer_.get_last_rendered(expected_depth);
64   return status;
65 }
66