00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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)
00039 return false;
00040
00041 fread(&signature, sizeof(unsigned int), 1, fp);
00042
00043 fread(&sphere, sizeof(Sphere3f), 1, fp);
00044
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
00051 index.resize(index_size);
00052 fread(&index[0], sizeof(Bmt::Cell), index.size(), fp);
00053
00054
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
00087
00088
00089
00090
00091
00092
00093
00094
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
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
00127
00128 fout = fopen(filename.c_str(), "wb+");
00129 if(!fout) {
00130 fclose(ftmp);
00131 return false;
00132 }
00133
00134 unsigned int magic = 0x62647421;
00135 fwrite(&magic, sizeof(unsigned int), 1, fout);
00136
00137
00138 fwrite(&signature, sizeof(unsigned int), 1, fout);
00139
00140 fwrite(&sphere, sizeof(Sphere3f), 1, fout);
00141
00142
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
00155 fwrite(&index[0], sizeof(Bmt::Cell), index.size(), fout);
00156
00157
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
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
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 }