1 // This is brl/bseg/boxm2/cpp/pro/processes/boxm2_cpp_vis_of_point_process.cxx
2 #include <iostream>
3 #include <fstream>
4 #include <bprb/bprb_func_process.h>
5 //:
6 // \file
7 // \brief  A process for probing along a ray in the scene.
8 //
9 // \author Vishal Jain
10 // \date June 3, 2011
11 
12 #ifdef _MSC_VER
13 #  include "vcl_msvc_warnings.h"
14 #endif
15 #include <boxm2/io/boxm2_cache.h>
16 #include <boxm2/boxm2_scene.h>
17 #include <boxm2/boxm2_block.h>
18 #include <boxm2/boxm2_data_base.h>
19 //brdb stuff
20 #include <brdb/brdb_value.h>
21 #include <boxm2/cpp/algo/boxm2_ray_probe_functor.h>
22 #include <bpro/core/bbas_pro/bbas_1d_array_float.h>
23 #include <boxm2/cpp/algo/boxm2_cast_ray_function.h>
24 #include <boxm2/cpp/algo/boxm2_render_exp_image_functor.h>
25 
26 //directory utility
27 #include <vcl_where_root_dir.h>
28 
29 namespace boxm2_cpp_vis_of_point_process_globals
30 {
31     constexpr unsigned n_inputs_ = 6;
32     constexpr unsigned n_outputs_ = 1;
33     std::size_t lthreads[2]={8,8};
34 }
35 
boxm2_cpp_vis_of_point_process_cons(bprb_func_process & pro)36 bool boxm2_cpp_vis_of_point_process_cons(bprb_func_process& pro)
37 {
38     using namespace boxm2_cpp_vis_of_point_process_globals;
39 
40     //process takes 1 input
41     std::vector<std::string> input_types_(n_inputs_);
42     input_types_[0] = "boxm2_scene_sptr";
43     input_types_[1] = "boxm2_cache_sptr";
44     input_types_[2] = "vpgl_camera_double_sptr";
45     input_types_[3] = "float";  //x
46     input_types_[4] = "float";  //y
47     input_types_[5] = "float";  //z
48 
49     // process has 1 output:
50     // output[0]: scene sptr
51     std::vector<std::string>  output_types_(n_outputs_);
52     output_types_[0] = "float"; //seg_len
53 
54     bool good = pro.set_input_types(input_types_) &&
55         pro.set_output_types(output_types_);
56     // in case the 6th input is not set
57     brdb_value_sptr idx = new brdb_value_t<std::string>("");
58     pro.set_input(5, idx);
59     return good;
60 }
61 
boxm2_cpp_vis_of_point_process(bprb_func_process & pro)62 bool boxm2_cpp_vis_of_point_process(bprb_func_process& pro)
63 {
64     using namespace boxm2_cpp_vis_of_point_process_globals;
65 
66     if ( pro.n_inputs() < n_inputs_ ) {
67         std::cout << pro.name() << ": The input number should be " << n_inputs_<< std::endl;
68         return false;
69     }
70     //get the inputs
71     unsigned i = 0;
72     boxm2_scene_sptr scene = pro.get_input<boxm2_scene_sptr>(i++);
73     boxm2_cache_sptr cache = pro.get_input<boxm2_cache_sptr>(i++);
74     vpgl_camera_double_sptr cam= pro.get_input<vpgl_camera_double_sptr>(i++);
75     auto px=pro.get_input<float>(i++);
76     auto py=pro.get_input<float>(i++);
77     auto pz=pro.get_input<float>(i++);
78 
79     bool foundDataType = false;
80     std::string data_type;
81     std::vector<std::string> apps = scene->appearances();
82     for (const auto & app : apps) {
83         if ( app == boxm2_data_traits<BOXM2_MOG3_GREY>::prefix() )
84         {
85             data_type = app;
86             foundDataType = true;
87         }
88         else if ( app == boxm2_data_traits<BOXM2_MOG3_GREY_16>::prefix() )
89         {
90             data_type = app;
91             foundDataType = true;
92         }
93     }
94     if (!foundDataType) {
95         std::cout<<"BOXM2_CPP_RENDER_PROCESS ERROR: scene doesn't have BOXM2_MOG3_GREY or BOXM2_MOG3_GREY_16 data type"<<std::endl;
96         return false;
97     }
98 
99 
100     std::vector<boxm2_block_id> vis_order=scene->get_vis_order_from_pt(vgl_point_3d<double>(px,py,pz));
101 
102     vgl_ray_3d<double> ray;
103     if(auto * pcam=dynamic_cast<vpgl_perspective_camera<double> * > (cam.ptr()))
104     {
105         vgl_point_3d<double>  qpoint(px,py,pz);
106         vgl_point_3d<double>  cam_center=pcam->camera_center();
107         vgl_vector_3d<double> dir=cam_center-qpoint;
108         ray.set(qpoint,dir);
109     }
110     std::vector<boxm2_block_id>::iterator id;
111     vil_image_view<float> vis_img(1,1);
112     vis_img.fill(1.0);
113     for (id = vis_order.begin(); id != vis_order.end(); ++id)
114     {
115         std::cout<<"Block Id "<<(*id)<<std::endl;
116         boxm2_block *     blk = cache->get_block(scene,*id);
117         boxm2_data_base *  alph = cache->get_data_base(scene,*id,boxm2_data_traits<BOXM2_ALPHA>::prefix());
118         std::vector<boxm2_data_base*> datas;
119         datas.push_back(alph);
120         auto *scene_info_wrapper=new boxm2_scene_info_wrapper();
121         scene_info_wrapper->info=scene->get_blk_metadata(*id);
122         boxm2_render_vis_image_functor point_vis_functor;
123         point_vis_functor.init_data(datas,&vis_img);
124         boxm2_cast_ray_function<boxm2_render_vis_image_functor>(ray,scene_info_wrapper->info,blk,0,0,point_vis_functor);
125     }
126 
127     // store scene smaprt pointer
128     pro.set_output_val<float>(0, vis_img(0,0));
129     return true;
130 }
131