1 // This is brl/bseg/boxm2/cpp/pro/processes/boxm2_cpp_refine_process2.cxx
2 #include <iostream>
3 #include <fstream>
4 #include <bprb/bprb_func_process.h>
5 //:
6 // \file
7 // \brief  A process for refining the scene.
8 //
9 // \author Vishal Jain
10 // \date Mar 10, 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_refine_block_function.h>
22 
23 //directory utility
24 #include <vcl_where_root_dir.h>
25 
26 namespace boxm2_cpp_refine_process2_globals
27 {
28   constexpr unsigned n_inputs_ = 4;
29   constexpr unsigned n_outputs_ = 0;
30 }
31 
boxm2_cpp_refine_process2_cons(bprb_func_process & pro)32 bool boxm2_cpp_refine_process2_cons(bprb_func_process& pro)
33 {
34   using namespace boxm2_cpp_refine_process2_globals;
35 
36   //process takes 1 input
37   std::vector<std::string> input_types_(n_inputs_);
38   input_types_[0] = "boxm2_scene_sptr";
39   input_types_[1] = "boxm2_cache_sptr";
40   input_types_[2] = "float";
41   input_types_[3] = "vcl_string";// if identifier is empty, then only one appearance model
42 
43   // process has 1 output:
44   // output[0]: scene sptr
45   std::vector<std::string>  output_types_(n_outputs_);
46 
47   bool good = pro.set_input_types(input_types_) && pro.set_output_types(output_types_);
48   // in case the 4th input is not set
49   brdb_value_sptr id = new brdb_value_t<std::string>("");
50   pro.set_input(3, id);
51   return good;
52 }
53 
boxm2_cpp_refine_process2(bprb_func_process & pro)54 bool boxm2_cpp_refine_process2(bprb_func_process& pro)
55 {
56   using namespace boxm2_cpp_refine_process2_globals;
57 
58   if ( pro.n_inputs() < n_inputs_ ) {
59     std::cout << pro.name() << ": The input number should be " << n_inputs_<< std::endl;
60     return false;
61   }
62   //get the inputs
63   unsigned i = 0;
64   boxm2_scene_sptr scene =pro.get_input<boxm2_scene_sptr>(i++);
65   boxm2_cache_sptr cache= pro.get_input<boxm2_cache_sptr>(i++);
66   auto  thresh=pro.get_input<float>(i++);
67   std::string identifier = pro.get_input<std::string>(i++);
68 
69   bool foundDataType = false;
70   std::string data_type;
71   std::vector<std::string> apps = scene->appearances();
72   for (const auto & app : apps) {
73     if ( app == boxm2_data_traits<BOXM2_MOG3_GREY>::prefix() )
74     {
75       data_type = app;
76       foundDataType = true;
77     }
78     else if ( app == boxm2_data_traits<BOXM2_MOG3_GREY_16>::prefix() )
79     {
80       data_type = app;
81       foundDataType = true;
82     }
83   }
84   if (!foundDataType) {
85     std::cout<<"BOXM2_CPP_REFINE_PROCESS2 ERROR: scene doesn't have BOXM2_MOG3_GREY or BOXM2_MOG3_GREY_16 data type"<<std::endl;
86     return false;
87   }
88   std::string num_obs_type = boxm2_data_traits<BOXM2_NUM_OBS>::prefix();
89   if (identifier.size() > 0) {
90     data_type += "_" + identifier;
91     num_obs_type += "_" + identifier;
92   }
93 
94   std::map<boxm2_block_id, boxm2_block_metadata> blocks = scene->blocks();
95   std::map<boxm2_block_id, boxm2_block_metadata>::iterator blk_iter;
96   for (blk_iter = blocks.begin(); blk_iter != blocks.end(); ++blk_iter)
97   {
98     boxm2_block_id id = blk_iter->first;
99     std::cout<<"Refining Block: "<<id<<std::endl;
100 
101     boxm2_block *     blk = cache->get_block(scene,id);
102     boxm2_data_base * alph = cache->get_data_base(scene,id,boxm2_data_traits<BOXM2_ALPHA>::prefix());
103     boxm2_data_base * mog = cache->get_data_base(scene,id,data_type);
104     boxm2_data_base * num_obs = cache->get_data_base(scene,id,num_obs_type);
105 
106     std::vector<boxm2_data_base*> datas;
107     datas.push_back(alph);
108     datas.push_back(mog);
109     datas.push_back(num_obs);
110 
111     //refine block and datas
112     boxm2_block_metadata data = blk_iter->second;
113     boxm2_refine_block(scene,blk,datas, thresh, false);
114     blk->enable_write(); // now cache will make sure that it is written to disc
115   }
116 
117   return true;
118 }
119