1 // This is brl/bseg/boxm2/cpp/pro/processes/boxm2_cpp_ray_app_density_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 
25 //directory utility
26 #include <vcl_where_root_dir.h>
27 
28 namespace boxm2_cpp_ray_app_density_process_globals
29 {
30     constexpr unsigned n_inputs_ = 7;
31     constexpr unsigned n_outputs_ = 1;
32     std::size_t lthreads[2]={8,8};
33 }
34 
boxm2_cpp_ray_app_density_process_cons(bprb_func_process & pro)35 bool boxm2_cpp_ray_app_density_process_cons(bprb_func_process& pro)
36 {
37     using namespace boxm2_cpp_ray_app_density_process_globals;
38 
39     //process takes 1 input
40     std::vector<std::string> input_types_(n_inputs_);
41     input_types_[0] = "boxm2_scene_sptr";
42     input_types_[1] = "boxm2_cache_sptr";
43     input_types_[2] = "vpgl_camera_double_sptr";
44     input_types_[3] = "unsigned";
45     input_types_[4] = "unsigned";
46     input_types_[5] = "float";
47     input_types_[6] = "vcl_string";// if identifier string is empty, then only one appearance model
48 
49 
50     // process has 1 output:
51     // output[0]: scene sptr
52     std::vector<std::string>  output_types_(n_outputs_);
53     output_types_[0] = "bbas_1d_array_float_sptr"; //seg_len
54 
55     bool good = pro.set_input_types(input_types_) &&
56         pro.set_output_types(output_types_);
57     // in case the 6th input is not set
58     brdb_value_sptr idx = new brdb_value_t<std::string>("");
59     pro.set_input(5, idx);
60     return good;
61 }
62 
boxm2_cpp_ray_app_density_process(bprb_func_process & pro)63 bool boxm2_cpp_ray_app_density_process(bprb_func_process& pro)
64 {
65     using namespace boxm2_cpp_ray_app_density_process_globals;
66 
67     if ( pro.n_inputs() < n_inputs_ ) {
68         std::cout << pro.name() << ": The input number should be " << n_inputs_<< std::endl;
69         return false;
70     }
71     //get the inputs
72     unsigned i = 0;
73     boxm2_scene_sptr scene = pro.get_input<boxm2_scene_sptr>(i++);
74     boxm2_cache_sptr cache = pro.get_input<boxm2_cache_sptr>(i++);
75     vpgl_camera_double_sptr cam= pro.get_input<vpgl_camera_double_sptr>(i++);
76     auto pi=pro.get_input<unsigned>(i++);
77     auto pj=pro.get_input<unsigned>(i++);
78     auto intensity=pro.get_input<float>(i++);
79     std::string identifier = pro.get_input<std::string>(i);
80 
81     bool foundDataType = false;
82     std::string data_type;
83     std::vector<std::string> apps = scene->appearances();
84     for (const auto & app : apps) {
85         if ( app == boxm2_data_traits<BOXM2_MOG3_GREY>::prefix() )
86         {
87             data_type = app;
88             foundDataType = true;
89         }
90         else if ( app == boxm2_data_traits<BOXM2_MOG3_GREY_16>::prefix() )
91         {
92             data_type = app;
93             foundDataType = true;
94         }
95     }
96     if (!foundDataType) {
97         std::cout<<"BOXM2_CPP_RENDER_PROCESS ERROR: scene doesn't have BOXM2_MOG3_GREY or BOXM2_MOG3_GREY_16 data type"<<std::endl;
98         return false;
99     }
100 
101     if (identifier.size() > 0) {
102         data_type += "_" + identifier;
103     }
104 
105     std::vector<boxm2_block_id> vis_order=scene->get_vis_blocks((vpgl_generic_camera<double>*)(cam.ptr()));
106     std::vector<boxm2_block_id>::iterator id;
107 
108     std::vector<float> app_density;
109     for (id = vis_order.begin(); id != vis_order.end(); ++id)
110     {
111         std::cout<<"Block Id "<<(*id)<<std::endl;
112         boxm2_block *     blk = cache->get_block(scene,*id);
113         boxm2_data_base *  mog = cache->get_data_base(scene,*id,data_type);
114         std::vector<boxm2_data_base*> datas;
115         datas.push_back(mog);
116 
117         auto *scene_info_wrapper=new boxm2_scene_info_wrapper();
118         scene_info_wrapper->info=scene->get_blk_metadata(*id);
119         boxm2_ray_app_density_functor ray_app_density_functor;
120         ray_app_density_functor.init_data(datas,app_density,intensity);
121 
122         cast_ray_per_block<boxm2_ray_app_density_functor>(ray_app_density_functor,scene_info_wrapper->info,blk,cam,pi+1,pj+1,pi,pj);
123     }
124 
125     bbas_1d_array_float_sptr app_density_array  =new bbas_1d_array_float(app_density.size());
126 
127     for (unsigned i=0;i<app_density.size();i++)
128     {
129         app_density_array->data_array[i]=app_density[i];
130     }
131     // store scene smaprt pointer
132     pro.set_output_val<bbas_1d_array_float_sptr>(0, app_density_array);
133     return true;
134 }
135