viz_storage.cpp
Go to the documentation of this file.
00001 #include <megatree/viz_storage.h>
00002 #include <megatree/storage_factory.h>
00003 #include <megatree/metadata.h>
00004 
00005 
00006 namespace megatree
00007 {
00008   VizStorage::VizStorage(const boost::filesystem::path &path): tree(path)
00009   {
00010     storage = openStorage(path, NORMAL_FORMAT);
00011     
00012     // get subtree width
00013     ByteVec data;
00014     storage->get("metadata.ini", data);
00015     MetaData metadata;
00016     metadata.deserialize(data);
00017     subtree_width = metadata.subtree_width;
00018   }
00019 
00020   void VizStorage::get(const boost::filesystem::path &path, ByteVec &result)
00021   {
00022     ByteVec tmp;
00023     storage->get(path, tmp);
00024     convert(tmp, result);
00025   }
00026 
00027   void VizStorage::getBatch(const std::vector<boost::filesystem::path> &paths, std::vector<ByteVec> &results)
00028   {
00029     std::vector<ByteVec> tmp;
00030     storage->getBatch(paths, tmp);
00031 
00032     results.resize(tmp.size());
00033     for (unsigned i=0; i<results.size(); i++)
00034       convert(tmp[i], results[i]);
00035   }
00036 
00037   void VizStorage::getAsync(const boost::filesystem::path &path, GetCallback callback)
00038   {
00039     if (path == "metadata.ini" || path == "views.ini")
00040       storage->getAsync(path, callback); 
00041     else
00042       storage->getAsync(path, boost::bind(&VizStorage::convertCb, this, path, callback, _1));
00043   }
00044 
00045   
00046 void VizStorage::convertCb(const boost::filesystem::path &path, GetCallback cb, const ByteVec& data)
00047   {
00048     printf("Response for tree %s and file %s\n", tree.string().c_str(), path.string().c_str());
00049 
00050     ByteVec data_converted;
00051     convert(data, data_converted);
00052     cb(data_converted);
00053   }
00054 
00055 
00056   void VizStorage::convert(const ByteVec& data, ByteVec& res)
00057   {
00058     const static size_t STRIDE = 3 + 3;
00059     
00060     uint64_t num_nodes = (data.size()-1)/NODE_SIZE;
00061     res.resize(1+num_nodes*STRIDE);  // max possible size
00062 
00063     unsigned res_offset = 0;
00064     unsigned data_offset = 0;
00065 
00066     // children of file
00067     memcpy(&res[res_offset], &data[data_offset], 1);
00068     res_offset += 1;
00069     data_offset += 1;
00070 
00071     // node data
00072     while (data_offset < data.size())
00073     {
00074       // pre-read short id
00075       uint32_t short_id;
00076       memcpy(&short_id, &data[data_offset + POINT_SIZE + COLOR_SIZE + COUNT_SIZE + CHILDREN_SIZE], SHORT_ID_SIZE);
00077 
00078       // read point
00079       Point pnt[3];
00080       for (unsigned i=0; i<3; i++)
00081       {      
00082         //TODO: We're checking out how rendering from the center of a cell looks
00083         pnt[i] = 32768;
00084         //pnt[i] = 0;
00085         //memcpy(&pnt[i], &data[data_offset], POINT_SIZE / 3);
00086         data_offset += POINT_SIZE / 3;
00087       }
00088 
00089       // convert point from local node frame to frame of node file
00090       for (unsigned i=0; i<subtree_width; i++)
00091       {
00092         int which = (short_id >> (i*3)) & 07;
00093         pnt[0] = (pnt[0] >> 1) | ((which & (1<<X_BIT)) ? 1<<(8*POINT_SIZE/3-1) : 0);
00094         pnt[1] = (pnt[1] >> 1) | ((which & (1<<Y_BIT)) ? 1<<(8*POINT_SIZE/3-1) : 0);
00095         pnt[2] = (pnt[2] >> 1) | ((which & (1<<Z_BIT)) ? 1<<(8*POINT_SIZE/3-1) : 0);
00096       }
00097 
00098       // one byte per point component
00099       for (unsigned i=0; i<3; i++)
00100       {      
00101         res[res_offset] = pnt[i] >> 8*(POINT_SIZE / 3 - 1);
00102         res_offset  += 1;
00103       }
00104     
00105       // one byte per color component
00106       for (unsigned i=0; i<3; i++)
00107       {      
00108         memcpy(&res[res_offset], &data[data_offset], 1);
00109         data_offset += COLOR_SIZE / 3;
00110         res_offset += 1;
00111       }
00112 
00113       // Extracts the count
00114       Count count;
00115       memcpy(&count, &data[data_offset], COUNT_SIZE);
00116       data_offset += COUNT_SIZE;
00117 
00118       // skip children of node
00119       uint8_t children;
00120       memcpy(&children, &data[data_offset], 1);
00121       data_offset += CHILDREN_SIZE;
00122     
00123       // Stashes whether the node is a leaf or not into the color
00124       if (children == 0) {
00125         res[res_offset-1] &= (~1);
00126       }
00127       else {
00128         res[res_offset-1] |= 1;
00129       }
00130 
00131       // skip short id
00132       data_offset += SHORT_ID_SIZE;
00133     }    
00134   }
00135 }


megatree_storage
Author(s): Wim Meeussen
autogenerated on Thu Nov 28 2013 11:30:26