diff --git a/wrap/bmt/bmt.cpp b/wrap/bmt/bmt.cpp new file mode 100644 index 00000000..a5d48fba --- /dev/null +++ b/wrap/bmt/bmt.cpp @@ -0,0 +1,164 @@ +#include + +using namespace std; +using namespace vcg; + +Bmt::Bmt(): fp(NULL) {} +Bmt::~Bmt() {} + +bool Bmt::Load(const std::string &filename) { + fp = fopen(filename.c_str(), "rb"); + if(!fp) return false; + unsigned int magic; + fread(&magic, sizeof(unsigned int), 1, fp); + if(magic != 0x62647421) //bmt! + return false; + //signature + fread(&signature, sizeof(unsigned int), 1, fp); + //sphere + fread(&sphere, sizeof(Sphere3f), 1, fp); + //index and history offsets and size; + fread(&index_offset, sizeof(unsigned int), 1, fp); + fread(&index_size, sizeof(unsigned int), 1, fp); + fread(&history_offset, sizeof(unsigned int), 1, fp); + fread(&history_size, sizeof(unsigned int), 1, fp); + + //index + index.resize(index_size); + fread(&index[0], sizeof(Bmt::Cell), index.size(), fp); + + //history + history.resize(history_size); + for(unsigned int i = 0; i < history.size(); i++) { + vector &created = history[i].created; + vector &erased = history[i].erased; + + unsigned int created_size; + fread(&created_size, sizeof(unsigned int), 1, fp); + created.resize(created_size); + fread(&*created.begin(), sizeof(unsigned int), created.size(), fp); + for(unsigned int k = 0; k < created_size; k++) + created[k] = &(index[(unsigned int)created[k]]); + + unsigned int erased_size; + fread(&erased_size, sizeof(unsigned int), 1, fp); + erased.resize(erased_size); + fread(&*erased.begin(), sizeof(unsigned int), erased.size(), fp); + for(unsigned int k = 0; k < erased_size; k++) + erased[k] = &(index[(unsigned int)erased[k]]); + } + return true; +} + +char *Bmt::GetData(unsigned int &size, unsigned int offset) { + fseek(fp, offset, SEEK_SET); + fread(&size, sizeof(unsigned int), 1, fp); + assert(size < 64000); + char *data = new char[size]; + fread(data, 1, size, fp); + return data; +} + +/*unsigned int &Bmt::IndexOffset(); +unsigned int &Bmt::IndexSize(); +Bmt::Cell *Bmt::IndexBegin(); + +unsigned int &Bmt::HistoryOffset(); +unsigned int &Bmt::HistorySize(); +unsigned int *Bmt::HistoryBegin(); */ + +/**** BMT BUILDER ****/ + +BmtBuilder::BmtBuilder(): ftmp(NULL), fout(NULL) {} + +BmtBuilder::~BmtBuilder() {} + +bool BmtBuilder::Create(unsigned int sign) { + signature = sign; + ftmp = fopen("tmp.bmt", "wb+"); + if(!ftmp) + return false; + return true; +} + +unsigned int BmtBuilder::AddCell(Bmt::Cell cell, unsigned int size, char *data) { + sphere.Add(cell.sphere); + cell.offset = ftell(ftmp); + index.push_back(cell); + //TODO: add padding to 4 or 8 bytes. + fwrite(&size, sizeof(unsigned int), 1, ftmp); + fwrite(data, 1, size, ftmp); + return index.size()-1; +} + +void BmtBuilder::AddUpdate(std::vector &created, std::vector &erased) { + creation.push_back(created); + deletion.push_back(erased); +} + +bool BmtBuilder::Save(const std::string &filename) { + assert(ftmp); + + //TODO: reorganize data to be spatially coherent (both index and related data. + + fout = fopen(filename.c_str(), "wb+"); + if(!fout) { + fclose(ftmp); + return false; + } + //first thing we write a magic constant "bmt!" + unsigned int magic = 0x62647421; //bmt! + fwrite(&magic, sizeof(unsigned int), 1, fout); + + //signature: + fwrite(&signature, sizeof(unsigned int), 1, fout); + //sphere + fwrite(&sphere, sizeof(Sphere3f), 1, fout); + + //next we write index and history offset and size + unsigned int index_offset = 5 * sizeof(unsigned int) + sizeof(Sphere3f); + unsigned int index_size = index.size(); + fwrite(&index_offset, sizeof(unsigned int), 1, fout); + fwrite(&index_size, sizeof(unsigned int), 1, fout); + + unsigned int history_offset = index_offset + index_size * sizeof(Bmt::Cell); + unsigned int history_size = creation.size(); + fwrite(&history_offset, sizeof(unsigned int), 1, fout); + fwrite(&history_size, sizeof(unsigned int), 1, fout); + + unsigned int index_start = ftell(fout); + //writing index (but its a fake... it will need to be rewritten late5r with correct offsets + fwrite(&index[0], sizeof(Bmt::Cell), index.size(), fout); + + //writing history + for(unsigned int i = 0; i < creation.size(); i++) { + vector &created = creation[i]; + vector &erased = deletion[i]; + unsigned int created_size = created.size(); + fwrite(&created_size, sizeof(unsigned int), 1, fout); + fwrite(&*created.begin(), sizeof(unsigned int), created.size(), fout); + unsigned int erased_size = erased.size(); + fwrite(&erased_size, sizeof(unsigned int), 1, fout); + fwrite(&*created.begin(), sizeof(unsigned int), erased.size(), fout); + } + //writing data + vector::iterator k; + for(k = index.begin(); k != index.end(); k++) { + Bmt::Cell &cell = *k; + fseek(ftmp, cell.offset, SEEK_SET); + unsigned int size; + fread(&size, sizeof(unsigned int), 1, ftmp); + char *data = new char[size]; + fread(data, 1, size, ftmp); + cell.offset = ftell(fout); + fwrite(&size, sizeof(unsigned int), 1, fout); + fwrite(data, 1, size, fout); + delete []data; + } + //writing index again + fseek(fout, index_start, SEEK_SET); + fwrite(&index[0], sizeof(Bmt::Cell), index.size(), fout); + + fclose(fout); + return true; +} diff --git a/wrap/bmt/bmt.h b/wrap/bmt/bmt.h new file mode 100644 index 00000000..03939813 --- /dev/null +++ b/wrap/bmt/bmt.h @@ -0,0 +1,111 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +#ifndef VCG_BMT_H +#define VCG_BMT_H + +#include +#include + +#include +#include + +namespace vcg { + +/** \addtogroup bmt */ +/*@{*/ + /** + The class for representing a batched mt structure. + See bmt.cpp for details on the structure of the file. + */ + +class Bmt { +public: + ///Cell structure for the mt representation + class Cell { + public: + //this effectively limits databases to 4Gb in size. + unsigned int offset; + float error; + Sphere3f sphere; + float Error() { return error; } + }; + + ///tells what is inside each cell. + unsigned int signature; + ///Bounding sphere + Sphere3f sphere; + + std::vector index; + std::vector< MT::Update > history; + + + + Bmt(); + ~Bmt(); + bool Load(const std::string &filename); + char *GetData(unsigned int &size, unsigned int offset); + +private: + FILE *fp; + unsigned int index_offset; + unsigned int index_size; + unsigned int history_offset; + unsigned int history_size; +}; + +class BmtBuilder { +public: + BmtBuilder(); + ~BmtBuilder(); + + ///tells what is inside each cell. + unsigned int signature; + ///Bounding sphere + Sphere3f sphere; + std::vector index; + std::vector > creation; + std::vector > deletion; + + bool Create(unsigned int signature); + + unsigned int AddCell(Bmt::Cell cell, unsigned int size, char *data); + void AddUpdate(std::vector &created, std::vector &erased); + + bool Save(const std::string &filename); +private: + FILE *ftmp; + FILE *fout; +}; + +float Distance(Bmt::Cell &cell, Point3f &p); + +}//end namespace + +#endif \ No newline at end of file diff --git a/wrap/bmt/strip_mesh.h b/wrap/bmt/strip_mesh.h new file mode 100644 index 00000000..93713b6b --- /dev/null +++ b/wrap/bmt/strip_mesh.h @@ -0,0 +1,70 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +#ifndef VCG_STRIP_MESH_H +#define VCG_STRIP_MESH_H + +#include + +namespace vcg { + + +class StripMesh { +public: + enum Signature { NORMAL = 1, COLOR = 2, STRIP = 4 }; + + StripMesh(char *s); + +private: + unsigned short _vert_size; + unsigned short _norm_size; + unsigned short _color_size; + unsigned short _strip_size; + + Point3f *_vert_start; + short *_norm_start; + unsigned char *_color_start; + unsigned short *_strip_start; +}; + +class StripMeshBuilder { +public: + std::vector vert; + std::vector norm; + std::vector color; + std::vector strip; + + unsigned int Signature(); + ///required size; + unsigned int Size(); + void Write(char *buffer, unsigned int size); +}; + +}//namespace +#endif