1 #ifndef boxm2_stream_cache_h_ 2 #define boxm2_stream_cache_h_ 3 //: 4 // \file 5 #include <iostream> 6 #include <fstream> 7 #include <boxm2/boxm2_scene.h> 8 #include <boxm2/boxm2_block.h> 9 #include <boxm2/boxm2_data.h> 10 #include <boxm2/basic/boxm2_block_id.h> 11 12 #ifdef _MSC_VER 13 # include <vcl_msvc_warnings.h> 14 #endif 15 16 #include <vbl/vbl_ref_count.h> 17 #include <vbl/vbl_smart_ptr.h> 18 19 class boxm2_stream_cache_helper : public vbl_ref_count 20 { 21 public: boxm2_stream_cache_helper()22 boxm2_stream_cache_helper() : index_(-1), buf_(nullptr) {} 23 ~boxm2_stream_cache_helper() override; 24 25 bool open_file(const std::string& filename); 26 void read(unsigned long size, const boxm2_block_id& id); 27 void close_file(); 28 29 //: return num cells on the buf 30 int num_cells(std::size_t cell_size); 31 32 //: return the byte buffer that contains ith cell, i is with respect to the global file 33 char *get_cell(int i, std::size_t cell_size, const boxm2_block_id& id); 34 35 int index_; // index of the data point at the beginning of buf_ 36 std::ifstream ifs_; 37 boxm2_data_base *buf_; 38 }; 39 40 typedef vbl_smart_ptr<boxm2_stream_cache_helper> boxm2_stream_cache_helper_sptr; 41 42 class boxm2_stream_cache_datatype_helper : public vbl_ref_count 43 { 44 public: boxm2_stream_cache_datatype_helper()45 boxm2_stream_cache_datatype_helper() : current_index_(-1), cell_cnt_(-1) {} 46 47 int current_index_; // global index in the file 48 boxm2_block_id current_block_; 49 50 //: the size of datatype that is stored in each cell 51 std::size_t cell_size_; 52 53 //: the size of chunks that will be read from the file, computed based on cell size and available memory 54 unsigned long buf_size_; 55 56 //: number of cells in the data files, computed based on file size and cell size 57 int cell_cnt_; 58 }; 59 60 typedef vbl_smart_ptr<boxm2_stream_cache_datatype_helper> boxm2_stream_cache_datatype_helper_sptr; 61 62 class boxm2_stream_cache_destroyer; 63 64 class boxm2_stream_cache: public vbl_ref_count 65 { 66 public: 67 //: hidden constructor (singleton class) 68 boxm2_stream_cache(const boxm2_scene_sptr& scene, 69 const std::vector<std::string>& data_types, 70 const std::vector<std::string>& identifier_list, 71 float num_giga = 1.0f); 72 73 //: return the next cells in the streams of each data block given by the identifier list for this datatype, pass index if available to check synchronization 74 template <boxm2_data_type T> std::vector<typename boxm2_data_traits<T>::datatype> get_next(boxm2_block_id id, int index = -1); 75 76 //: random access in the stream 77 template <boxm2_data_type T> std::vector<typename boxm2_data_traits<T>::datatype> get_random_i(boxm2_block_id id, unsigned int index); 78 //: in iterative mode, the files need to be closed and re-opened 79 void close_streams(); 80 81 template <boxm2_data_type T> int exists(boxm2_block_id bid); 82 83 protected: 84 85 //: hidden destructor (singleton class) 86 ~boxm2_stream_cache() override; 87 88 boxm2_stream_cache_datatype_helper_sptr get_helper(std::string& data_type); 89 90 template <boxm2_data_type T> bool open_streams( boxm2_stream_cache_datatype_helper_sptr h); 91 #if 0 92 //: singleton instance of boxm2_stream_cache 93 static boxm2_stream_cache* instance_; 94 #endif 95 //: boxm2_scene needs to be around to get path of the files 96 boxm2_scene_sptr scene_; 97 98 std::vector<std::string> identifier_list_; 99 100 //: available RAM size in bytes 101 unsigned long mem_size_; 102 103 //: map to store various info about each datatype 104 std::map<std::string, boxm2_stream_cache_datatype_helper_sptr > data_types_; 105 106 //: for each data type, there is a list for each identifier 107 std::map<std::string, std::vector<boxm2_stream_cache_helper_sptr> > data_streams_; 108 }; 109 110 typedef vbl_smart_ptr<boxm2_stream_cache> boxm2_stream_cache_sptr; 111 112 113 //: Binary write boxm2_cache to stream 114 void vsl_b_write(vsl_b_ostream& os, boxm2_stream_cache const& scene); 115 void vsl_b_write(vsl_b_ostream& os, const boxm2_stream_cache* &p); 116 void vsl_b_write(vsl_b_ostream& os, boxm2_stream_cache_sptr& sptr); 117 void vsl_b_write(vsl_b_ostream& os, boxm2_stream_cache_sptr const& sptr); 118 119 //: Binary load boxm2_cache from stream. 120 void vsl_b_read(vsl_b_istream& is, boxm2_stream_cache &scene); 121 void vsl_b_read(vsl_b_istream& is, boxm2_stream_cache* p); 122 void vsl_b_read(vsl_b_istream& is, boxm2_stream_cache_sptr& sptr); 123 void vsl_b_read(vsl_b_istream& is, boxm2_stream_cache_sptr const& sptr); 124 125 126 #endif //boxm2_stream_cache_h_ 127