File: bmt.cpp

package info (click to toggle)
meshlab 2022.02%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 47,348 kB
  • sloc: cpp: 536,635; ansic: 27,783; sh: 539; makefile: 36
file content (188 lines) | stat: -rw-r--r-- 6,952 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/****************************************************************************
* MeshLab                                                           o o     *
* An extendible mesh processor                                    o     o   *
*                                                                _   O  _   *
* Copyright(C) 2005, 2009                                          \/)\/    *
* 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.                                                         *
*                                                                           *
****************************************************************************/


#include <wrap/bmt/bmt.h>

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<Bmt::Cell *> &created = history[i].created;
    vector<Bmt::Cell *> &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<unsigned int> &created, std::vector<unsigned int> &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<unsigned int> &created = creation[i];
    vector<unsigned int> &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<Bmt::Cell>::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;
}