1 #include "testlib/testlib_test.h"
2 #include <boxm2/volm/boxm2_volm_wr3db_index.h>
3 #include <boxm2/volm/boxm2_volm_wr3db_index_sptr.h>
4 #include <volm/volm_spherical_container.h>
5 #include <volm/volm_spherical_shell_container.h>
6 #include <volm/volm_spherical_shell_container_sptr.h>
7 #include "vnl/vnl_random.h"
8 
test_volm_wr3db_ind()9 static void test_volm_wr3db_ind()
10 {
11   // create a much smaller index for testing purposes
12   float vmin = 2.0f;  // min voxel resolution
13   float dmax = 3000.0f;
14   float solid_angle = 2.0f;
15   volm_spherical_container_sptr sph2 = new volm_spherical_container(solid_angle,vmin,dmax);
16 #if 0
17   boxm2_volm_wr3db_index_sptr ind = new boxm2_volm_wr3db_index(sph2);
18   std::cout << "number of voxels in container: " << sph2->get_voxels().size() << std::endl;
19   sph2->draw_template("./container.vrml",0.0);
20 
21    fill the layer after vmin with some depth interval values:
22   unsigned int offset, end_offset; double depth;
23   sph2->first_res(sph2->min_voxel_res()*2, offset, end_offset, depth);
24   std::vector<unsigned char> values(end_offset-offset);
25   vnl_random rng;
26   for (unsigned i = 0; i+offset < end_offset; ++i)
27     values[i] = (unsigned char)rng.drand32(1.0,(double)(sph2->get_depth_offset_map().size()-1));
28 
29   std::vector<char> vis_prob;
30   vis_prob.resize(sph2->get_voxels().size());
31   boxm2_volm_wr3db_index::inflate_index_vis_and_prob(values, sph2, vis_prob);
32   sph2->draw_template_vis_prob("./container2.vrml", 0.0, vis_prob);
33 #endif
34   volm_spherical_shell_container_sptr sph_shell = new volm_spherical_shell_container(1.0, 90.0, 30.0, 0.0, 0.0);
35 
36   // test io
37   int layer_size = (int)sph_shell->get_container_size();
38   boxm2_volm_wr3db_index_sptr ind = new boxm2_volm_wr3db_index(layer_size, 1.0f);
39 
40   std::vector<unsigned char> vals(layer_size, 0);
41   vals[0] = 'a'; vals[3] = 'c';
42 
43   vul_file::delete_file_glob("./test_ind.bin");
44   TEST("initialize write", ind->initialize_write("./test_ind.bin"), true);
45   // now fill up the active cache twice! before the second fill the first batch needs to be written to disc
46   for (unsigned i = 0; i < ind->buffer_size(); i++)
47     ind->add_to_index(vals);
48   // the one below should trigger a write to disc
49   vals[0] = 'b';  vals[layer_size-1] = 'd';
50   TEST("add to index", ind->add_to_index(vals), true);
51   auto *vals_buf = new unsigned char[layer_size];
52   vals_buf[0] = 'e';  vals_buf[layer_size-1] = 'f';
53   TEST("add to index", ind->add_to_index(vals_buf), true);
54   std::cout << "global id: " << ind->current_global_id() << " current active cache id: " << ind->current_id() << std::endl;
55 
56   TEST("finalize write", ind->finalize(), true);
57 
58   const boxm2_volm_wr3db_index_sptr& ind2 = ind;
59 
60   // initialize_read() finalizes write operations in case there is any active write operation
61   TEST("initialize read", ind2->initialize_read("./test_ind.bin"), true);
62   TEST("global id", ind2->current_global_id(), 0);
63 
64   std::vector<unsigned char> vals2(layer_size);
65   TEST("getting the first index", ind2->get_next(vals2), true);
66   TEST("test index 0", vals2[0] == 'a', true);
67   TEST("test index 0", vals2[3] == 'c', true);
68   for (unsigned i = 0; i < ind->buffer_size()-1; i++)
69     ind2->get_next(vals2);
70   ind2->get_next(vals2);
71 
72   std::vector<unsigned char> vals_buf2(layer_size);
73   ind2->get_next(vals_buf2);
74   TEST("test index end", vals_buf2[0] == 'e', true);
75   TEST("test index end", vals_buf2[layer_size-1] == 'f', true);
76   TEST("finalize read", ind2->finalize(), true);
77 }
78 
79 TESTMAIN(test_volm_wr3db_ind);
80 
81 #if 0
82   float vmin = 10.0f;  // min voxel resolution
83   float dmax = 60000.0f;
84   float solid_angle = 4.0f;
85 
86   volm_spherical_container_sptr sph = new volm_spherical_container(solid_angle,vmin,dmax);
87   std::vector<volm_voxel>& voxels = sph->get_voxels();
88   int data_size = 1; // 1 byte if only vis values will be indexed, otherwise its 2 bytes
89   double ind_size = voxels.size()*data_size/1048576.0;  // in MBs
90   std::cout << "number of voxels in container: " << voxels.size() << " size of voxel array for " << data_size << " bytes: " << ind_size << " MB" << std::endl;
91   double inc = 10.0;
92   double num = (100000/inc)*(100000/inc);
93   std::cout << " for 100 km size WR (in each size) for every " << inc << "m there are: " << num << " location hypotheses\n"
94            << " then the index size on file is: " << num*ind_size << " MBs = " << num*ind_size/1024 << " GBs = " << num*ind_size/1048576 << " TBs.\n";
95 
96   unsigned int offset, end_offset; double d;
97   //sph->first_res(sph->min_voxel_res()*2, offset, end_offset, d);
98   sph->last_res(vmin, offset, end_offset, d);
99 
100   std::cout << " last layer with vmin res is at depth: " << d << " and has offset: " << offset << " end_offset: " << end_offset << '\n'
101            << "first voxel: " << sph->get_voxels()[offset].center_ << ' ' << sph->get_voxels()[offset].resolution_ << '\n'
102            << "last voxel: " << sph->get_voxels()[end_offset-1].center_ << ' ' << sph->get_voxels()[end_offset-1].resolution_ << '\n'
103            << "after last voxel: " << sph->get_voxels()[end_offset].center_ << ' ' << sph->get_voxels()[end_offset].resolution_ << std::endl;
104   unsigned int size = end_offset-offset;
105   std::cout << "size is: " << size << std::endl;
106   ind_size = size*sizeof(float)*2*num/1048576.0;  // in MBs
107   std::cout << "if we index 2 floats for this layer each location index is of size: "<< size*sizeof(float)*2/1048576.0 << " MBs, for all locs, size is: "<< ind_size << " MB = " << ind_size/1024 << " GB\n"
108            << "if we index 1 floats for this layer each location index is of size: "<< size*sizeof(float)/1048576.0 << " MBs, for all locs, size is: "<< ind_size/2 << " MB = " << ind_size/2048 << " GB\n"
109            << "if we index 1 unsigned short for this layer, each location index is of size: "<< size*sizeof(unsigned short)/1048576.0 << " MBs, for all locs, size is: "<< size*num*sizeof(unsigned short)/1048576.0 << " MBs = " << size*num*sizeof(unsigned short)/1048576.0/1024 << " GBs\n";
110 #endif
111 
112 #if 0
113   // use hypotheses to generate index
114   boxm2_volm_wr3db_index_sptr ind = new boxm2_volm_wr3db_index(sph);
115   ind->index_locations(scene, h);
116   std::string out_name = out_file() + "_volm_index_" + tiles[i].get_string() + ".bin";
117   if (!ind->write_index(out_name))
118     std::cerr << "Problems writing index: " << out_name << std::endl;
119 
120   for (unsigned i = 0; i < tiles.size(); i++) {
121     std::string name = out_file() + "_volm_index_" + tiles[i].get_string() + ".bin";
122     boxm2_volm_wr3db_index_sptr ind = new boxm2_volm_wr3db_index(sph);
123     ind->read_index(name);
124   }
125 #endif
126