-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryLoader.cpp
411 lines (351 loc) · 12.7 KB
/
BinaryLoader.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "BinaryLoader.h"
///////////////////////////////////////////////////////////////////////////////
template<typename T>
void BinaryLoader::readXYZ(
const String& filename,
size_t readStart, size_t readLength, int decimation,
Vector<Vector3f>* points, Vector<Vector4f>* colors,
size_t* numPoints,
Vector3f* pointmin,
Vector3f* pointmax,
Vector4f* rgbamin,
Vector4f* rgbamax) const
{
Vector3f point;
Vector4f color(1.0f, 1.0f, 1.0f, 1.0f);
// Default record size = 7 doubles (X,Y,Z,R,G,B,A)
int numFields = 7;
size_t recordSize = sizeof(T)* numFields;
FILE* fin = fopen(filename.c_str(), "rb");
// How many records are in the file?
fseek(fin, 0, SEEK_END);
size_t endpos = ftell(fin);
fseek(fin, 0, SEEK_SET);
size_t numRecords = endpos / recordSize;
//size_t readStart = numRecords * readStartP / BINARY_POINTS_MAX_BATCHES;
//size_t readLength = numRecords * readLengthP / BINARY_POINTS_MAX_BATCHES;
if(decimation <= 0) decimation = 1;
if(readStart != 0)
{
fseek(fin, (long)(readStart * recordSize), SEEK_SET);
}
// Adjust read length.
if(readLength == 0 || readStart + readLength > numRecords)
{
readLength = numRecords - readStart;
}
//ofmsg("BinaryPointsLoader: reading records %1% - %2% of %3% (decimation %4%) of %5%",
// %readStart % (readStart + readLength) % numRecords %decimation %filename);
// Read in data
T* buffer = (T*)malloc(recordSize * readLength / decimation);
if(buffer == NULL)
{
oferror("BinaryPointsLoader::readXYZ: could not allocate %1% bytes",
% (recordSize * readLength / decimation));
return;
}
size_t ne = readLength / decimation;
srand(100);
// Read data
// If data is not decimated, read it in one go.
if(decimation == 1)
{
size_t size = fread(buffer, recordSize, readLength, fin);
}
else
{
int j = 0;
for(int i = 0; i < ne; i++)
{
// // Read one record
// size_t size = fread(&buffer[j], recordSize, 1, fin);
// // Skip ahead decimation - 1 records.
// fseek(fin, recordSize * (decimation - 1), SEEK_CUR);
// RANDOM DECIMATED READ
size_t recordoffset = rand() / (RAND_MAX / decimation + 1);
size_t offs = ((size_t)recordSize) * (i * (decimation)+recordoffset);
fseek(fin, (long)((readStart * recordSize) + offs), SEEK_SET);
size_t size = fread(&buffer[j], recordSize, 1, fin);
j += numFields;
}
}
points->reserve(ne);
colors->reserve(ne);
size_t j = 0;
for(size_t i = 0; i < ne; i++)
{
point[0] = buffer[i * numFields];
point[1] = buffer[i * numFields + 1];
point[2] = buffer[i * numFields + 2];
color[0] = buffer[i * numFields + 3];
color[1] = buffer[i * numFields + 4];
color[2] = buffer[i * numFields + 5];
color[3] = buffer[i * numFields + 6];
points->push_back(point);
colors->push_back(color);
// Update data bounds and number of points
*numPoints = *numPoints + 1;
for(size_t j = 0; j < 4; j++)
{
if(color[j] < (*rgbamin)[j]) (*rgbamin)[j] = color[j];
if(color[j] > (*rgbamax)[j]) (*rgbamax)[j] = color[j];
}
for(size_t j = 0; j < 3; j++)
{
if(point[j] < (*pointmin)[j]) (*pointmin)[j] = point[j];
if(point[j] > (*pointmax)[j]) (*pointmax)[j] = point[j];
}
}
fclose(fin);
free(buffer);
}
///////////////////////////////////////////////////////////////////////////////
template<typename T>
T* readField(
const String& filename,
uint fieldIndex,
size_t readStart, size_t readLength, int decimation,
double* fmin,
double* fmax)
{
Vector3f point;
Vector4f color(1.0f, 1.0f, 1.0f, 1.0f);
// Default record size = 7 doubles (X,Y,Z,R,G,B,A)
int numFields = 7;
size_t recordSize = sizeof(T)* numFields;
FILE* fin = fopen(filename.c_str(), "rb");
// How many records are in the file?
fseek(fin, 0, SEEK_END);
size_t endpos = ftell(fin);
fseek(fin, 0, SEEK_SET);
size_t numRecords = endpos / recordSize;
if(decimation <= 0) decimation = 1;
if(readStart != 0)
{
fseek(fin, (long)(readStart * recordSize), SEEK_SET);
}
// Adjust read length.
if(readLength == 0 || readStart + readLength > numRecords)
{
readLength = numRecords - readStart;
}
//ofmsg("BinaryPointsLoader: reading records %1% - %2% of %3% (decimation %4%) of %5%",
// %readStart % (readStart + readLength) % numRecords %decimation %filename);
// Read in data
T* buffer = (T*)malloc(recordSize * readLength / decimation);
if(buffer == NULL)
{
oferror("BinaryPointsLoader::readXYZ: could not allocate %1% bytes",
% (recordSize * readLength / decimation));
return NULL;
}
size_t ne = readLength / decimation;
srand(100);
// Read data
// If data is not decimated, read it in one go.
if(decimation == 1)
{
size_t size = fread(buffer, recordSize, readLength, fin);
}
else
{
int j = 0;
for(int i = 0; i < ne; i++)
{
// // Read one record
// size_t size = fread(&buffer[j], recordSize, 1, fin);
// // Skip ahead decimation - 1 records.
// fseek(fin, recordSize * (decimation - 1), SEEK_CUR);
// RANDOM DECIMATED READ
size_t recordoffset = rand() / (RAND_MAX / decimation + 1);
size_t offs = ((size_t)recordSize) * (i * (decimation)+recordoffset);
fseek(fin, (long)((readStart * recordSize) + offs), SEEK_SET);
size_t size = fread(&buffer[j], recordSize, 1, fin);
j += numFields;
}
}
size_t j = 0;
for(size_t i = 0; i < ne; i++)
{
buffer[i] = buffer[i * numFields + fieldIndex];
// Update data bounds and number of points
*fmin = *fmin < buffer[i] ? *fmin : buffer[i];
*fmax = *fmax > buffer[i] ? *fmax : buffer[i];
}
fclose(fin);
return buffer;
}
///////////////////////////////////////////////////////////////////////////////
class LoadTask : public WorkerTask
{
public:
Ref<Field> field;
String path;
void execute(WorkerTask::TaskInfo* ti)
{
String fullpath;
if(DataManager::findFile(path, fullpath))
{
// Parse csv data column into a float array.
int nrows = 0;
double fmin = field->getDimension()->floatRangeMin;
double fmax = field->getDimension()->floatRangeMax;
int index = field->getDimension()->index;
void* data = NULL;
if(Dataset::useDoublePrecision())
{
data = readField<double>(fullpath, index, field->domain.start, field->domain.length, field->domain.decimation, &fmin, &fmax);
}
else
{
data = readField<float>(fullpath, index, field->domain.start, field->domain.length, field->domain.decimation, &fmin, &fmax);
}
field->getDimension()->floatRangeMin = fmin;
field->getDimension()->floatRangeMax = fmax;
field->lock.lock();
// Extend the field data memory and copy the new data into it.
size_t elemSize = field->getDimension()->getElementSize();
if(field->data == NULL)
{
field->data = (char*)data;
}
else
{
free(field->data);
field->data = (char*)data;
}
// Update field length
field->loaded = true;
field->stamp = otimestamp();
field->lock.unlock();
//ofmsg("Loading %1% finished", %field->getName());
}
}
};
///////////////////////////////////////////////////////////////////////////////
BinaryLoader::BinaryLoader()
{
myNumRecords = 0;
myLoaderPool.start(4);
}
///////////////////////////////////////////////////////////////////////////////
BinaryLoader::~BinaryLoader()
{
myLoaderPool.stop();
}
///////////////////////////////////////////////////////////////////////////////
void BinaryLoader::open(const String& source)
{
myFilename = source;
}
///////////////////////////////////////////////////////////////////////////////
void BinaryLoader::load(Field* f)
{
LoadTask* task = new LoadTask();
task->field = f;
task->path = myFilename;
myLoaderPool.queue(task);
}
///////////////////////////////////////////////////////////////////////////////
size_t BinaryLoader::getNumRecords(Dataset* d)
{
if(myNumRecords != 0) return myNumRecords;
// Compute max records (points) from source file
String path;
if(!DataManager::findFile(myFilename, path))
{
ofwarn("BinaryLoader::getNumRecords: could not find %1%", %myFilename);
return 0;
}
// Default record size = 7 doubles (X,Y,Z,R,G,B,A)
size_t fs = Dataset::useDoublePrecision() ? sizeof(double) : sizeof(float);
int numFields = 7;
size_t recordSize = fs * numFields;
FILE* fin = fopen(path.c_str(), "rb");
// How many records are in the file?
fseek(fin, 0, SEEK_END);
size_t endpos = ftell(fin);
size_t numRecords = endpos / recordSize;
fclose(fin);
myNumRecords = numRecords;
return numRecords;
}
///////////////////////////////////////////////////////////////////////////////
bool BinaryLoader::getBounds(const Domain& d, float* bounds)
{
String boundsBasename;
String boundsExtension;
String boundsPath;
StringUtils::splitFullFilename(myFilename, boundsBasename, boundsExtension, boundsPath);
String boundsFileName = ostr("%1%bounds/%2%.%3%-%4%-%5%.bounds", %boundsPath %boundsBasename %d.start %d.length %d.decimation);
size_t length = d.length;
size_t start = d.start;
int decimation = d.decimation;
// Try to read bounds from bounds cache
if(readBoundsFile(boundsFileName, bounds)) return true;
String path;
if(DataManager::findFile(myFilename, path)) {
Vector<Vector3f> points;
Vector<Vector4f> colors;
size_t numPoints = 0;
float maxf = numeric_limits<float>::max();
float minf = -numeric_limits<float>::max();
Vector4f rgbamin = Vector4f(maxf, maxf, maxf, maxf);
Vector4f rgbamax = Vector4f(minf, minf, minf, minf);
Vector3f pointmin = Vector3f(maxf, maxf, maxf);
Vector3f pointmax = Vector3f(minf, minf, minf);
if(Dataset::useDoublePrecision()) readXYZ<double>(path, start, length, decimation, &points, &colors, &numPoints, &pointmin, &pointmax, &rgbamin, &rgbamax);
else readXYZ<float>(path, start, length, decimation, &points, &colors, &numPoints, &pointmin, &pointmax, &rgbamin, &rgbamax);
// make sure path exists
String p = boundsPath + "bounds";
//ofmsg("Creating path %1%", %p);
DataManager::createPath(p);
FILE* bf = fopen(boundsFileName.c_str(), "w");
if(bf == NULL)
{
ofwarn("[BinaryLoader::getBounds] failed opening <%1%> for writing", %boundsFileName);
return false;
}
fprintf(bf, "%f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f",
pointmin[0], pointmax[0],
pointmin[1], pointmax[1],
pointmin[2], pointmax[2],
rgbamin[0], rgbamax[0],
rgbamin[1], rgbamax[1],
rgbamin[2], rgbamax[2],
rgbamin[3], rgbamax[3]);
fclose(bf);
bounds[0] = pointmin[0]; bounds[1] = pointmax[0];
bounds[2] = pointmin[1]; bounds[3] = pointmax[1];
bounds[4] = pointmin[2]; bounds[5] = pointmax[2];
bounds[6] = rgbamin[0]; bounds[7] = rgbamax[0];
bounds[8] = rgbamin[1]; bounds[9] = rgbamax[1];
bounds[10] = rgbamin[2]; bounds[11] = rgbamax[2];
bounds[12] = rgbamin[3]; bounds[13] = rgbamax[3];
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////
bool BinaryLoader::readBoundsFile(const String& filename, float* bounds)
{
String path;
if(DataManager::findFile(filename, path))
{
String boundsText = DataManager::readTextFile(path);
// Bounds text format:
// xmin, xmax, ymin, ymax, zmin, zmax, rmin, rmax, gmin, gmax, bmin, bmax, amin, amax
Vector<String> vals = StringUtils::split(boundsText, ",");
for(int i = 0; i < 14; i++)
{
StringUtils::trim(vals[i]);
bounds[i] = boost::lexical_cast<float>(vals[i]);
}
return true;
}
else
{
ofmsg("BinaryPointsReader::readBoundsFile bounds file not found %1% ", %filename);
}
return false;
}