summaryrefslogtreecommitdiff
path: root/src/Voxel/Voxel_Reader.cxx
blob: 1701c99f48cd84f704c9d16ca4e5db4725040aa6 (plain)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// File:	Voxel_Reader.cxx
// Created:	Wed Aug 28 19:34:33 2008
// Author:	Vladislav ROMASHKO
//		<vladislav.romashko@opencascade.com>

#include <Voxel_Reader.ixx>
#include <Voxel_BoolDS.hxx>
#include <Voxel_ColorDS.hxx>
#include <Voxel_FloatDS.hxx>
#include <Voxel_VoxelFileFormat.hxx>
#include <Voxel_TypeDef.hxx>

#include <TCollection_AsciiString.hxx>

#include <stdio.h>

Voxel_Reader::Voxel_Reader():myBoolVoxels(0),myColorVoxels(0),myFloatVoxels(0)
{

}

Standard_Boolean Voxel_Reader::Read(const TCollection_ExtendedString& file)
{
  // Open file in ASCII mode to read header
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  if (!f)
    return Standard_False;

  // Read the header
  Standard_Byte type; // 0 - bool, 1 - color, 2 - float
  Voxel_VoxelFileFormat format;
  Standard_Character svoxels[8], sformat[8], stype[8];
  fscanf(f, "%s %s %s\n", svoxels, sformat, stype);
  fclose(f);

  // Take format, type of voxels.
  // Voxels
  if (strcmp(svoxels, VOXELS))
    return Standard_False;
  // Format
  if (strcmp(sformat, ASCII) == 0)
    format = Voxel_VFF_ASCII;
  else if (strcmp(sformat, BINARY) == 0)
    format = Voxel_VFF_BINARY;
  else
    return Standard_False;
  // Type of voxels
  if (strcmp(stype, BOOL) == 0)
    type = 0;
  else if (strcmp(stype, COLOR) == 0)
    type = 1;
  else if (strcmp(stype, FLOAT) == 0)
    type = 2;
  else
    return Standard_False;

  // Read the rest
  switch (format)
  {
    case Voxel_VFF_ASCII:
    {
      switch (type)
      {
	case 0:
	  return ReadBoolAsciiVoxels(file);
	case 1:
	  return ReadColorAsciiVoxels(file);
	case 2:
	  return ReadFloatAsciiVoxels(file);
      }
    }
    case Voxel_VFF_BINARY:
    {
      switch (type)
      {
	case 0:
	  return ReadBoolBinaryVoxels(file);
	case 1:
	  return ReadColorBinaryVoxels(file);
	case 2:
	  return ReadFloatBinaryVoxels(file);
      }
    }
  }

  // No voxels or no format description is found:
  return Standard_False;
}

Standard_Boolean Voxel_Reader::IsBoolVoxels() const
{
  return (myBoolVoxels != 0);
}

Standard_Boolean Voxel_Reader::IsColorVoxels() const
{
  return (myColorVoxels != 0);
}

Standard_Boolean Voxel_Reader::IsFloatVoxels() const
{
  return (myFloatVoxels != 0);
}

Standard_Address Voxel_Reader::GetBoolVoxels() const
{
  return myBoolVoxels;
}

Standard_Address Voxel_Reader::GetColorVoxels() const
{
  return myColorVoxels;
}

Standard_Address Voxel_Reader::GetFloatVoxels() const
{
  return myFloatVoxels;
}

static Standard_Boolean has_slice(const Standard_CString line)
{
  Standard_Integer i = 0, nb_spaces = 0;
  while (line[i] != '\0')
  {
    if (line[i] == ' ')
      nb_spaces++;
    i++;
  }
  return (nb_spaces == 2);
}

Standard_Boolean Voxel_Reader::ReadBoolAsciiVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  if (!f)
    return Standard_False;
  Standard_Character line[64], sx[32], sy[32], sz[32];

  // Header: skip it
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  x = atof(sx); y = atof(sy); z = atof(sz);
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  {
    fclose(f);
    return Standard_False;
  }

  // Allocate the voxels
  myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_BoolDS.cxx:
  Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
  Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
  // myData[0 .. nb_slices - 1][0 .. 7]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0, value = 0;
    while (!feof(f))
    {
      fgets(line, 64, f);
      if (has_slice(line))
      {
	if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
        {
	  fclose(f);
	  return Standard_False;
	}
      }
      else
      {
	if (sscanf(line, "%d %d\n", &i2, &value) != 2)
        {
	  fclose(f);
	  return Standard_False;
	}
      }

      // Set value
      if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
      {
	((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] = 
	  (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
      }
      (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
    }
  }

  fclose(f);
  return Standard_True;
}

Standard_Boolean Voxel_Reader::ReadColorAsciiVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  if (!f)
    return Standard_False;
  Standard_Character line[64], sx[32], sy[32], sz[32];

  // Header: skip it
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  x = atof(sx); y = atof(sy); z = atof(sz);
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  {
    fclose(f);
    return Standard_False;
  }

  // Allocate the voxels
  myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_ColorDS.cxx:
  Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
  Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
  // myData[0 .. nb_slices - 1][0 .. 31]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0, value = 0;
    while (!feof(f))
    {
      fgets(line, 64, f);
      if (has_slice(line))
      {
	if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
        {
	  fclose(f);
	  return Standard_False;
	}
      }
      else
      {
	if (sscanf(line, "%d %d\n", &i2, &value) != 2)
        {
	  fclose(f);
	  return Standard_False;
	}
      }

      // Set value
      if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
      {
	((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] = 
	  (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
      }
      (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
    }
  }

  fclose(f);
  return Standard_True;
}

Standard_Boolean Voxel_Reader::ReadFloatAsciiVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  if (!f)
    return Standard_False;
  Standard_Character line[64], sx[32], sy[32], sz[32];

  // Header: skip it
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  x = atof(sx); y = atof(sy); z = atof(sz);
  if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  {
    fclose(f);
    return Standard_False;
  }
  xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  {
    fclose(f);
    return Standard_False;
  }

  // Allocate the voxels
  myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_FloatDS.cxx:
  Standard_Integer nb_floats = nbx * nby * nbz;
  Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
  // myData[0 .. nb_slices - 1][0 .. 31]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0;
    Standard_ShortReal value = 0.0;
    while (!feof(f))
    {
      fgets(line, 64, f);
      if (has_slice(line))
      {
	if (sscanf(line, "%d %d %s\n", &i1, &i2, line) != 3)
        {
	  fclose(f);
	  return Standard_False;
	}
      }
      else
      {
	if (sscanf(line, "%d %s\n", &i2, line) != 2)
        {
	  fclose(f);
	  return Standard_False;
	}
      }
      value = (Standard_ShortReal)atof(line);

      // Set value
      if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
      {
	((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] = 
	  (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
      }
      (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
    }
  }

  fclose(f);
  return Standard_True;
}

Standard_Boolean Voxel_Reader::ReadBoolBinaryVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  if (!f)
    return Standard_False;

  // Header: skip it
  Standard_Character line[64];
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  fread(&x, sizeof(Standard_Real), 1, f);
  fread(&y, sizeof(Standard_Real), 1, f);
  fread(&z, sizeof(Standard_Real), 1, f);
  fread(&xlen, sizeof(Standard_Real), 1, f);
  fread(&ylen, sizeof(Standard_Real), 1, f);
  fread(&zlen, sizeof(Standard_Real), 1, f);
  fread(&nbx, sizeof(Standard_Integer), 1, f);
  fread(&nby, sizeof(Standard_Integer), 1, f);
  fread(&nbz, sizeof(Standard_Integer), 1, f);

  // Allocate the voxels
  myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_BoolDS.cxx:
  Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
  Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
  // myData[0 .. nb_slices - 1][0 .. 7]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0, value = 0;
    while (!feof(f))
    {
      fread(&i1, sizeof(Standard_Integer), 1, f);
      fread(&i2, sizeof(Standard_Integer), 1, f);
      fread(&value, sizeof(Standard_Byte), 1, f);

      // Set value
      if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
      {
	((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] = 
	  (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
      }
      (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
    }
  }

  fclose(f);
  return Standard_True;
}

Standard_Boolean Voxel_Reader::ReadColorBinaryVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  if (!f)
    return Standard_False;

  // Header: skip it
  Standard_Character line[64];
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  fread(&x, sizeof(Standard_Real), 1, f);
  fread(&y, sizeof(Standard_Real), 1, f);
  fread(&z, sizeof(Standard_Real), 1, f);
  fread(&xlen, sizeof(Standard_Real), 1, f);
  fread(&ylen, sizeof(Standard_Real), 1, f);
  fread(&zlen, sizeof(Standard_Real), 1, f);
  fread(&nbx, sizeof(Standard_Integer), 1, f);
  fread(&nby, sizeof(Standard_Integer), 1, f);
  fread(&nbz, sizeof(Standard_Integer), 1, f);

  // Allocate the voxels
  myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_ColorDS.cxx:
  Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
  Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
  // myData[0 .. nb_slices - 1][0 .. 31]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0, value = 0;
    while (!feof(f))
    {
      fread(&i1, sizeof(Standard_Integer), 1, f);
      fread(&i2, sizeof(Standard_Integer), 1, f);
      fread(&value, sizeof(Standard_Byte), 1, f);

      // Set value
      if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
      {
	((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] = 
	  (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
      }
      (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
    }
  }

  fclose(f);
  return Standard_True;
}

Standard_Boolean Voxel_Reader::ReadFloatBinaryVoxels(const TCollection_ExtendedString& file)
{
  // Open file for reading
  FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  if (!f)
    return Standard_False;

  // Header: skip it
  Standard_Character line[64];
  fgets(line, 64, f);
  
  // Location, size, number of splits
  Standard_Integer nbx = 0, nby = 0, nbz = 0;
  Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  fread(&x, sizeof(Standard_Real), 1, f);
  fread(&y, sizeof(Standard_Real), 1, f);
  fread(&z, sizeof(Standard_Real), 1, f);
  fread(&xlen, sizeof(Standard_Real), 1, f);
  fread(&ylen, sizeof(Standard_Real), 1, f);
  fread(&zlen, sizeof(Standard_Real), 1, f);
  fread(&nbx, sizeof(Standard_Integer), 1, f);
  fread(&nby, sizeof(Standard_Integer), 1, f);
  fread(&nbz, sizeof(Standard_Integer), 1, f);

  // Allocate the voxels
  myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  // Data
  // Copied from Voxel_FloatDS.cxx:
  Standard_Integer nb_floats = nbx * nby * nbz;
  Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
  // myData[0 .. nb_slices - 1][0 .. 31]
  if (nb_slices)
  {
    Standard_Integer i1 = 0, i2 = 0;
    Standard_ShortReal value = 0.0;
    while (!feof(f))
    {
      fread(&i1, sizeof(Standard_Integer), 1, f);
      fread(&i2, sizeof(Standard_Integer), 1, f);
      fread(&value, sizeof(Standard_ShortReal), 1, f);

      // Set value
      if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
      {
	((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] = 
	  (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
      }
      (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
    }
  }

  fclose(f);
  return Standard_True;
}