1 // This is brl/bseg/boxm2/pro/processes/boxm2_create_stream_cache_process.cxx
2 #include <iostream>
3 #include <fstream>
4 #include <bprb/bprb_func_process.h>
5 //:
6 // \file
7 // \brief  A process for creating stream cache.
8 //
9 // \author Ozge C. Ozcanli
10 // \date May 10, 2011
11 
12 #ifdef _MSC_VER
13 #  include "vcl_msvc_warnings.h"
14 #endif
15 #include <boxm2/boxm2_scene.h>
16 #include <boxm2/io/boxm2_stream_cache.h>
17 
18 namespace boxm2_create_stream_cache_process_globals
19 {
20   constexpr unsigned n_inputs_ = 4;
21   constexpr unsigned n_outputs_ = 1;
22 }
23 
boxm2_create_stream_cache_process_cons(bprb_func_process & pro)24 bool boxm2_create_stream_cache_process_cons(bprb_func_process& pro)
25 {
26   using namespace boxm2_create_stream_cache_process_globals;
27 
28   //process takes 1 input
29   std::vector<std::string> input_types_(n_inputs_);
30   input_types_[0] = "boxm2_scene_sptr";
31   input_types_[1] = "vcl_string";  // data type
32   input_types_[2] = "vcl_string";  // name of the identifier list file
33   input_types_[3] = "float"; // number of gigabytes available for stream cache, the buffer lengths will be based on this number
34 
35   // process has 1 output:
36   // output[0]: scene sptr
37   std::vector<std::string>  output_types_(n_outputs_);
38   output_types_[0] = "boxm2_stream_cache_sptr";
39 
40   return pro.set_input_types(input_types_) && pro.set_output_types(output_types_);
41 }
42 
boxm2_create_stream_cache_process(bprb_func_process & pro)43 bool boxm2_create_stream_cache_process(bprb_func_process& pro)
44 {
45   using namespace boxm2_create_stream_cache_process_globals;
46 
47   if ( pro.n_inputs() < n_inputs_ ){
48     std::cout << pro.name() << ": The input number should be " << n_inputs_<< std::endl;
49     return false;
50   }
51   //get the inputs
52   unsigned i = 0;
53   boxm2_scene_sptr scene= pro.get_input<boxm2_scene_sptr>(i++);
54   std::string data_type_fname = pro.get_input<std::string>(i++);   // open data streams of this type with each identifier
55   std::string identifier_fname= pro.get_input<std::string>(i++);
56   auto num_giga = pro.get_input<float>(i++);
57 
58   // extract list of image_ids from file
59   std::ifstream ifs(identifier_fname.c_str());
60   if (!ifs.good()) {
61     std::cerr << "error opening file " <<identifier_fname << '\n';
62     return false;
63   }
64   std::vector<std::string> image_ids;
65   unsigned int n_images = 0;
66   ifs >> n_images;
67   for (unsigned int i=0; i<n_images; ++i) {
68     std::string img_id;
69     ifs >> img_id;
70     image_ids.push_back(img_id);
71     //std::cout << "adding: " << img_id << std::endl;
72   }
73   ifs.close();
74 
75   // extract list of type names from file
76   std::ifstream ifst(data_type_fname.c_str());
77   if (!ifst.good()) {
78     std::cerr << "error opening file " <<data_type_fname << '\n';
79     return false;
80   }
81   std::vector<std::string> type_names;
82   unsigned int n_types = 0;
83   ifst >> n_types;
84   for (unsigned int i=0; i<n_types; ++i) {
85     std::string type_name;
86     ifst >> type_name;
87     type_names.push_back(type_name);
88     //std::cout << "adding type: " << type_name << std::endl;
89   }
90   ifst.close();
91 
92   boxm2_stream_cache * str_cache = new boxm2_stream_cache(scene, type_names, image_ids, num_giga);
93 
94   // store cache pointer
95   pro.set_output_val<boxm2_stream_cache_sptr>(0, str_cache);
96   return true;
97 }
98 
99 
100 namespace boxm2_stream_cache_close_files_process_globals
101 {
102   constexpr unsigned n_inputs_ = 1;
103   constexpr unsigned n_outputs_ = 0;
104 }
105 
boxm2_stream_cache_close_files_process_cons(bprb_func_process & pro)106 bool boxm2_stream_cache_close_files_process_cons(bprb_func_process& pro)
107 {
108   using namespace boxm2_stream_cache_close_files_process_globals;
109 
110   //process takes 1 input
111   std::vector<std::string> input_types_(n_inputs_);
112   input_types_[0] = "boxm2_stream_cache_sptr";
113 
114   // process has 0 output:
115   std::vector<std::string>  output_types_(n_outputs_);
116 
117   return pro.set_input_types(input_types_) && pro.set_output_types(output_types_);
118 }
119 
boxm2_stream_cache_close_files_process(bprb_func_process & pro)120 bool boxm2_stream_cache_close_files_process(bprb_func_process& pro)
121 {
122   using namespace boxm2_stream_cache_close_files_process_globals;
123 
124   if ( pro.n_inputs() < n_inputs_ ){
125     std::cout << pro.name() << ": The input number should be " << n_inputs_<< std::endl;
126     return false;
127   }
128   //get the inputs
129   unsigned i = 0;
130   boxm2_stream_cache_sptr cache = pro.get_input<boxm2_stream_cache_sptr>(i++);
131   cache->close_streams();
132   return true;
133 }
134