1 #include "bstm_block.h"
2 //:
3 // \file
4 
bstm_block(const bstm_block_id & id,bstm_block_metadata data,char * buffer)5 bstm_block::bstm_block(const bstm_block_id& id, bstm_block_metadata data, char* buffer)
6 {
7   init_level_ = data.init_level_;
8   max_level_  = data.max_level_;
9   max_mb_     = int(data.max_mb_);
10   sub_block_dim_ = data.sub_block_dim_;
11   sub_block_num_ = data.sub_block_num_;
12 
13   block_id_ = id;
14   buffer_ = buffer;
15   this->b_read(buffer_);
16   read_only_ = false; // make sure that it is written back to disc
17 }
18 
bstm_block(bstm_block_metadata data)19 bstm_block::bstm_block(bstm_block_metadata data)
20 {
21   block_id_ = data.id_;
22   this->init_empty_block(data);
23   read_only_ = false;  // make sure that it is written back to disc
24 }
25 
b_read(char * buff)26 bool bstm_block::b_read(char* buff)
27 {
28   auto* treesBuff = (uchar16*) (buff);
29   byte_count_ = sizeof(uchar16)* sub_block_num_.x()*sub_block_num_.y()*sub_block_num_.z();
30   trees_     = boxm2_array_3d<uchar16>( sub_block_num_.x(),
31                                         sub_block_num_.y(),
32                                         sub_block_num_.z(),
33                                         treesBuff);
34   return true;
35 }
36 
37 //:
38 //  This type of writing is sort of counter intuitive, as the buffer
39 //  just needs to be returned and written to disk. The first few calls
40 //  ensure the meta data is lined up correctly.  To use this, just pass in
41 //  the bstm_block buffer.
b_write(char *)42 bool bstm_block::b_write(char*  /*buff*/)
43 {
44   return true;
45 }
46 
47 
48 //: initializes empty scene given
49 // This method uses the max_mb parameter to determine how many data cells to
50 // allocate.  MAX_MB is assumed to include blocks, alpha, mog3, num_obs and 16 byte aux data
init_empty_block(bstm_block_metadata data)51 bool bstm_block::init_empty_block(bstm_block_metadata data)
52 {
53   //total number of (sub) blocks in the scene
54   int total_blocks =  data.sub_block_num_.x()
55                     * data.sub_block_num_.y()
56                     * data.sub_block_num_.z();
57 
58   //to initialize
59   int num_buffers, blocks_per_buffer;
60 
61   //only 1 buffer, blocks per buffer is all blocks
62   num_buffers = 1;
63   blocks_per_buffer = total_blocks;
64   std::cout<<"Num buffers: "<<num_buffers
65           <<" .. num_trees: "<<blocks_per_buffer<<std::endl;
66 
67   //now construct a byte stream, and read in with b_read
68   byte_count_ = calc_byte_count(num_buffers, blocks_per_buffer, total_blocks);
69   init_level_ = data.init_level_;
70   max_level_  = data.max_level_;
71   max_mb_     = int(data.max_mb_);
72   buffer_ = new char[byte_count_];
73 
74   //get member variable metadata straight, then write to the buffer
75   long bytes_read = 0;
76 
77   sub_block_dim_ = data.sub_block_dim_;
78   sub_block_num_ = data.sub_block_num_;
79 
80   //4. setup big arrays (3d block of trees)
81   auto* treesBuff = (uchar16*) (buffer_+bytes_read);
82   trees_     = boxm2_array_3d<uchar16>( sub_block_num_.x(),
83                                         sub_block_num_.y(),
84                                         sub_block_num_.z(),
85                                         treesBuff);
86 
87   //--- Now initialize blocks and their pointers --------- ---------------------
88   //6. initialize blocks in order
89   int tree_index = 0;
90   boxm2_array_3d<uchar16>::iterator iter;
91   for (iter = trees_.begin(); iter != trees_.end(); ++iter)
92   {
93     //initialize empty tree
94     uchar16 treeBlk( (unsigned char) 0 );
95 
96     //the tree_index must be scaled with the number of time trees
97     //int tree_index_scaled = tree_index * sub_block_num_t_;
98 
99     //store root data index in bits [10, 11, 12, 13] ;
100     treeBlk[10] = (tree_index) & 0xff;
101     treeBlk[11] = (tree_index>>8)  & 0xff;
102     treeBlk[12] = (tree_index>>16) & 0xff;
103     treeBlk[13] = (tree_index>>24) & 0xff;
104 
105     //Set Init_Level, 1=just root, 2=2 generations, 3=3 generations, 4=all four
106     if (init_level_== 1) {
107       treeBlk[0] = 0;
108       tree_index += 1;
109     }
110     else if (init_level_ == 2){
111       treeBlk[0] = 1;
112       tree_index += 9;                //root + 1st
113     }
114     else if (init_level_ == 3) {
115       treeBlk[0] = 1;
116       treeBlk[1] = 0xff;
117       tree_index += 1 + 8 + 64;       //root + 1st + 2nd
118     }
119     else if (init_level_ == 4) {
120       treeBlk[0] = 1;
121       for (int i=1; i<1+8; ++i)
122         treeBlk[i] = 0xff;
123       tree_index += 1 + 8 + 64 + 512; // root + 1st + 2nd + 3rd...
124     }
125 
126     //store this tree in block bytes
127     for (int i=0; i<16; i++)
128       (*iter)[i] = treeBlk[i];
129   }
130   return true;
131 }
132 
133 
134 //: Given number of buffers, number of trees in each buffer, and number of total trees (x*y*z number)
135 // \return size of byte stream
calc_byte_count(int,int,int num_trees)136 long bstm_block::calc_byte_count(int  /*num_buffers*/, int  /*trees_per_buffer*/, int num_trees)
137 {
138   long toReturn = num_trees * sizeof(uchar16) ;
139   return toReturn;
140 }
141 
142 
143 //------------ I/O -------------------------------------------------------------
operator <<(std::ostream & s,bstm_block & block)144 std::ostream& operator <<(std::ostream &s, bstm_block& block)
145 {
146   return
147   s << "Block ID=" << block.block_id() << '\n'
148     << "Byte Count=" << block.byte_count() << '\n'
149     << "Init level=" << block.init_level() << '\n'
150     << "Max level=" << block.max_level() << '\n'
151     << "Max MB=" << block.max_mb() << '\n'
152     << "Read only=" << block.read_only() << '\n'
153     << "Sub Block Dim=" << block.sub_block_dim() << '\n'
154     << "Sub Block Num=" << block.sub_block_num() << std::endl;
155 }
156 
157 //: Binary write bstm_block to stream.
158 // DUMMY IMPLEMENTATION: does nothing!
vsl_b_write(vsl_b_ostream &,bstm_block_sptr const &)159 void vsl_b_write(vsl_b_ostream&, bstm_block_sptr const&) {}
160 
161 //: Binary load bstm_block from stream.
162 // DUMMY IMPLEMENTATION: does nothing!
vsl_b_read(vsl_b_istream &,bstm_block_sptr &)163 void vsl_b_read(vsl_b_istream&, bstm_block_sptr&) {}
164 //: Binary load bstm_block from stream.
165 // DUMMY IMPLEMENTATION: does nothing!
vsl_b_read(vsl_b_istream &,bstm_block_sptr const &)166 void vsl_b_read(vsl_b_istream&, bstm_block_sptr const&) {}
167