bmt.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 * MeshLab                                                           o o     *
00003 * An extendible mesh processor                                    o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2005, 2009                                          \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 
00025 #include <wrap/bmt/bmt.h>
00026 
00027 using namespace std;
00028 using namespace vcg;
00029 
00030 Bmt::Bmt(): fp(NULL) {}
00031 Bmt::~Bmt() {}
00032 
00033 bool Bmt::Load(const std::string &filename) {
00034   fp = fopen(filename.c_str(), "rb");
00035   if(!fp) return false;
00036   unsigned int magic;
00037   fread(&magic, sizeof(unsigned int), 1, fp);
00038   if(magic != 0x62647421) //bmt!
00039     return false;
00040   //signature
00041   fread(&signature, sizeof(unsigned int), 1, fp);
00042   //sphere
00043   fread(&sphere, sizeof(Sphere3f), 1, fp);
00044   //index and history offsets and size;  
00045   fread(&index_offset, sizeof(unsigned int), 1, fp);
00046   fread(&index_size, sizeof(unsigned int), 1, fp);
00047   fread(&history_offset, sizeof(unsigned int), 1, fp);
00048   fread(&history_size, sizeof(unsigned int), 1, fp);
00049 
00050   //index
00051   index.resize(index_size);
00052   fread(&index[0], sizeof(Bmt::Cell), index.size(), fp);
00053 
00054   //history
00055   history.resize(history_size);  
00056   for(unsigned int i = 0; i < history.size(); i++) {
00057     vector<Bmt::Cell *> &created = history[i].created;
00058     vector<Bmt::Cell *> &erased = history[i].erased;    
00059 
00060     unsigned int created_size;
00061     fread(&created_size, sizeof(unsigned int), 1, fp);
00062     created.resize(created_size);
00063     fread(&*created.begin(), sizeof(unsigned int), created.size(), fp);
00064     for(unsigned int k = 0; k < created_size; k++) 
00065       created[k] = &(index[(unsigned int)created[k]]);
00066     
00067     unsigned int erased_size;
00068     fread(&erased_size, sizeof(unsigned int), 1, fp);
00069     erased.resize(erased_size);
00070     fread(&*erased.begin(), sizeof(unsigned int), erased.size(), fp);
00071     for(unsigned int k = 0; k < erased_size; k++) 
00072       erased[k] = &(index[(unsigned int)erased[k]]);        
00073   }  
00074   return true;
00075 }
00076       
00077 char *Bmt::GetData(unsigned int &size, unsigned int offset) {
00078   fseek(fp, offset, SEEK_SET);
00079   fread(&size, sizeof(unsigned int), 1, fp);
00080   assert(size < 64000);
00081   char *data = new char[size];
00082   fread(data, 1, size, fp);
00083   return data;
00084 }
00085 
00086 /*unsigned int &Bmt::IndexOffset();
00087 unsigned int &Bmt::IndexSize();
00088 Bmt::Cell *Bmt::IndexBegin();
00089 
00090 unsigned int &Bmt::HistoryOffset();  
00091 unsigned int &Bmt::HistorySize();
00092 unsigned int *Bmt::HistoryBegin();        */
00093 
00094 /**** BMT BUILDER ****/
00095 
00096 BmtBuilder::BmtBuilder(): ftmp(NULL), fout(NULL) {}
00097 
00098 BmtBuilder::~BmtBuilder() {}
00099   
00100 bool BmtBuilder::Create(unsigned int sign) {
00101   signature = sign;
00102   ftmp = fopen("tmp.bmt", "wb+");
00103   if(!ftmp)
00104     return false;
00105   return true;
00106 }
00107 
00108 unsigned int BmtBuilder::AddCell(Bmt::Cell cell, unsigned int size, char *data) {  
00109   sphere.Add(cell.sphere);
00110   cell.offset = ftell(ftmp);
00111   index.push_back(cell);
00112   //TODO: add padding to 4 or 8 bytes.
00113   fwrite(&size, sizeof(unsigned int), 1, ftmp);
00114   fwrite(data, 1, size, ftmp);    
00115   return index.size()-1;
00116 }
00117 
00118 void BmtBuilder::AddUpdate(std::vector<unsigned int> &created, std::vector<unsigned int> &erased) {
00119   creation.push_back(created);
00120   deletion.push_back(erased);
00121 }
00122 
00123 bool BmtBuilder::Save(const std::string &filename) {  
00124   assert(ftmp);
00125   
00126   //TODO: reorganize data to be spatially coherent (both index and related data.
00127 
00128   fout = fopen(filename.c_str(), "wb+");
00129   if(!fout) {
00130     fclose(ftmp);
00131     return false;
00132   }
00133   //first thing we write a magic constant "bmt!"
00134   unsigned int magic = 0x62647421; //bmt!  
00135   fwrite(&magic, sizeof(unsigned int), 1, fout);
00136 
00137   //signature:
00138   fwrite(&signature, sizeof(unsigned int), 1, fout);
00139   //sphere
00140   fwrite(&sphere, sizeof(Sphere3f), 1, fout);
00141 
00142   //next we write index and history offset and size
00143   unsigned int index_offset = 5 * sizeof(unsigned int) + sizeof(Sphere3f);
00144   unsigned int index_size = index.size();
00145   fwrite(&index_offset, sizeof(unsigned int), 1, fout);
00146   fwrite(&index_size, sizeof(unsigned int), 1, fout);
00147 
00148   unsigned int history_offset = index_offset + index_size * sizeof(Bmt::Cell);
00149   unsigned int history_size = creation.size();
00150   fwrite(&history_offset, sizeof(unsigned int), 1, fout);
00151   fwrite(&history_size, sizeof(unsigned int), 1, fout);
00152 
00153   unsigned int index_start = ftell(fout);
00154   //writing index (but its a fake... it will need to be rewritten late5r with correct offsets
00155   fwrite(&index[0], sizeof(Bmt::Cell), index.size(), fout);
00156 
00157   //writing history
00158   for(unsigned int i = 0; i < creation.size(); i++) {
00159     vector<unsigned int> &created = creation[i];
00160     vector<unsigned int> &erased = deletion[i];
00161     unsigned int created_size = created.size();
00162     fwrite(&created_size, sizeof(unsigned int), 1, fout);
00163     fwrite(&*created.begin(), sizeof(unsigned int), created.size(), fout);
00164     unsigned int erased_size = erased.size();
00165     fwrite(&erased_size, sizeof(unsigned int), 1, fout);
00166     fwrite(&*created.begin(), sizeof(unsigned int), erased.size(), fout);
00167   }
00168   //writing data
00169   vector<Bmt::Cell>::iterator k;
00170   for(k = index.begin(); k != index.end(); k++) {
00171     Bmt::Cell &cell = *k;    
00172     fseek(ftmp, cell.offset, SEEK_SET);
00173     unsigned int size;
00174     fread(&size, sizeof(unsigned int), 1, ftmp);
00175     char *data = new char[size];
00176     fread(data, 1, size, ftmp);
00177     cell.offset = ftell(fout);
00178     fwrite(&size, sizeof(unsigned int), 1, fout);
00179     fwrite(data, 1, size, fout);
00180     delete []data;
00181   }
00182   //writing index again
00183   fseek(fout, index_start, SEEK_SET);
00184   fwrite(&index[0], sizeof(Bmt::Cell), index.size(), fout);
00185 
00186   fclose(fout);
00187   return true;
00188 }


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:29:20