1 // Created on: 2008-08-28
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 2008-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <Voxel_Reader.ixx>
22 #include <Voxel_BoolDS.hxx>
23 #include <Voxel_ColorDS.hxx>
24 #include <Voxel_FloatDS.hxx>
25 #include <Voxel_VoxelFileFormat.hxx>
26 #include <Voxel_TypeDef.hxx>
28 #include <TCollection_AsciiString.hxx>
30 Voxel_Reader::Voxel_Reader():myBoolVoxels(0),myColorVoxels(0),myFloatVoxels(0)
35 Standard_Boolean Voxel_Reader::Read(const TCollection_ExtendedString& file)
37 // Open file in ASCII mode to read header
38 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
40 return Standard_False;
43 Standard_Byte type; // 0 - bool, 1 - color, 2 - float
44 Voxel_VoxelFileFormat format;
45 Standard_Character svoxels[8], sformat[8], stype[8];
46 fscanf(f, "%s %s %s\n", svoxels, sformat, stype);
49 // Take format, type of voxels.
51 if (strcmp(svoxels, VOXELS))
52 return Standard_False;
54 if (strcmp(sformat, ASCII) == 0)
55 format = Voxel_VFF_ASCII;
56 else if (strcmp(sformat, BINARY) == 0)
57 format = Voxel_VFF_BINARY;
59 return Standard_False;
61 if (strcmp(stype, BOOL) == 0)
63 else if (strcmp(stype, COLOR) == 0)
65 else if (strcmp(stype, FLOAT) == 0)
68 return Standard_False;
78 return ReadBoolAsciiVoxels(file);
80 return ReadColorAsciiVoxels(file);
82 return ReadFloatAsciiVoxels(file);
85 case Voxel_VFF_BINARY:
90 return ReadBoolBinaryVoxels(file);
92 return ReadColorBinaryVoxels(file);
94 return ReadFloatBinaryVoxels(file);
99 // No voxels or no format description is found:
100 return Standard_False;
103 Standard_Boolean Voxel_Reader::IsBoolVoxels() const
105 return (myBoolVoxels != 0);
108 Standard_Boolean Voxel_Reader::IsColorVoxels() const
110 return (myColorVoxels != 0);
113 Standard_Boolean Voxel_Reader::IsFloatVoxels() const
115 return (myFloatVoxels != 0);
118 Standard_Address Voxel_Reader::GetBoolVoxels() const
123 Standard_Address Voxel_Reader::GetColorVoxels() const
125 return myColorVoxels;
128 Standard_Address Voxel_Reader::GetFloatVoxels() const
130 return myFloatVoxels;
133 static Standard_Boolean has_slice(const Standard_CString line)
135 Standard_Integer i = 0, nb_spaces = 0;
136 while (line[i] != '\0')
142 return (nb_spaces == 2);
145 Standard_Boolean Voxel_Reader::ReadBoolAsciiVoxels(const TCollection_ExtendedString& file)
147 // Open file for reading
148 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
150 return Standard_False;
151 Standard_Character line[64], sx[32], sy[32], sz[32];
156 // Location, size, number of splits
157 Standard_Integer nbx = 0, nby = 0, nbz = 0;
158 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
159 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
162 return Standard_False;
164 x = Atof(sx); y = Atof(sy); z = Atof(sz);
165 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
168 return Standard_False;
170 xlen = Atof(sx); ylen = Atof(sy); zlen = Atof(sz);
171 if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
174 return Standard_False;
177 // Allocate the voxels
178 myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
181 // Copied from Voxel_BoolDS.cxx:
182 Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
183 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
184 // myData[0 .. nb_slices - 1][0 .. 7]
187 Standard_Integer i1 = 0, i2 = 0, value = 0;
193 if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
196 return Standard_False;
201 if (sscanf(line, "%d %d\n", &i2, &value) != 2)
204 return Standard_False;
209 if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
211 ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] =
212 (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
214 (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = value;
219 return Standard_True;
222 Standard_Boolean Voxel_Reader::ReadColorAsciiVoxels(const TCollection_ExtendedString& file)
224 // Open file for reading
225 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
227 return Standard_False;
228 Standard_Character line[64], sx[32], sy[32], sz[32];
233 // Location, size, number of splits
234 Standard_Integer nbx = 0, nby = 0, nbz = 0;
235 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
236 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
239 return Standard_False;
241 x = Atof(sx); y = Atof(sy); z = Atof(sz);
242 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
245 return Standard_False;
247 xlen = Atof(sx); ylen = Atof(sy); zlen = Atof(sz);
248 if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
251 return Standard_False;
254 // Allocate the voxels
255 myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
258 // Copied from Voxel_ColorDS.cxx:
259 Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
260 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
261 // myData[0 .. nb_slices - 1][0 .. 31]
264 Standard_Integer i1 = 0, i2 = 0, value = 0;
270 if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
273 return Standard_False;
278 if (sscanf(line, "%d %d\n", &i2, &value) != 2)
281 return Standard_False;
286 if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
288 ((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] =
289 (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
291 (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = value;
296 return Standard_True;
299 Standard_Boolean Voxel_Reader::ReadFloatAsciiVoxels(const TCollection_ExtendedString& file)
301 // Open file for reading
302 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
304 return Standard_False;
305 Standard_Character line[64], sx[32], sy[32], sz[32];
310 // Location, size, number of splits
311 Standard_Integer nbx = 0, nby = 0, nbz = 0;
312 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
313 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
316 return Standard_False;
318 x = Atof(sx); y = Atof(sy); z = Atof(sz);
319 if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
322 return Standard_False;
324 xlen = Atof(sx); ylen = Atof(sy); zlen = Atof(sz);
325 if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
328 return Standard_False;
331 // Allocate the voxels
332 myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
335 // Copied from Voxel_FloatDS.cxx:
336 Standard_Integer nb_floats = nbx * nby * nbz;
337 Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
338 // myData[0 .. nb_slices - 1][0 .. 31]
341 Standard_Integer i1 = 0, i2 = 0;
342 Standard_ShortReal value = 0.0;
348 if (sscanf(line, "%d %d %s\n", &i1, &i2, line) != 3)
351 return Standard_False;
356 if (sscanf(line, "%d %s\n", &i2, line) != 2)
359 return Standard_False;
362 value = (Standard_ShortReal)Atof(line);
365 if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
367 ((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] =
368 (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
370 (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
375 return Standard_True;
378 Standard_Boolean Voxel_Reader::ReadBoolBinaryVoxels(const TCollection_ExtendedString& file)
380 // Open file for reading
381 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
383 return Standard_False;
386 Standard_Character line[64];
389 // Location, size, number of splits
390 Standard_Integer nbx = 0, nby = 0, nbz = 0;
391 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
392 fread(&x, sizeof(Standard_Real), 1, f);
393 fread(&y, sizeof(Standard_Real), 1, f);
394 fread(&z, sizeof(Standard_Real), 1, f);
395 fread(&xlen, sizeof(Standard_Real), 1, f);
396 fread(&ylen, sizeof(Standard_Real), 1, f);
397 fread(&zlen, sizeof(Standard_Real), 1, f);
398 fread(&nbx, sizeof(Standard_Integer), 1, f);
399 fread(&nby, sizeof(Standard_Integer), 1, f);
400 fread(&nbz, sizeof(Standard_Integer), 1, f);
402 // Allocate the voxels
403 myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
406 // Copied from Voxel_BoolDS.cxx:
407 Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
408 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
409 // myData[0 .. nb_slices - 1][0 .. 7]
412 Standard_Integer i1 = 0, i2 = 0, value = 0;
415 fread(&i1, sizeof(Standard_Integer), 1, f);
416 fread(&i2, sizeof(Standard_Integer), 1, f);
417 fread(&value, sizeof(Standard_Byte), 1, f);
420 if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
422 ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] =
423 (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
425 (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = value;
430 return Standard_True;
433 Standard_Boolean Voxel_Reader::ReadColorBinaryVoxels(const TCollection_ExtendedString& file)
435 // Open file for reading
436 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
438 return Standard_False;
441 Standard_Character line[64];
444 // Location, size, number of splits
445 Standard_Integer nbx = 0, nby = 0, nbz = 0;
446 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
447 fread(&x, sizeof(Standard_Real), 1, f);
448 fread(&y, sizeof(Standard_Real), 1, f);
449 fread(&z, sizeof(Standard_Real), 1, f);
450 fread(&xlen, sizeof(Standard_Real), 1, f);
451 fread(&ylen, sizeof(Standard_Real), 1, f);
452 fread(&zlen, sizeof(Standard_Real), 1, f);
453 fread(&nbx, sizeof(Standard_Integer), 1, f);
454 fread(&nby, sizeof(Standard_Integer), 1, f);
455 fread(&nbz, sizeof(Standard_Integer), 1, f);
457 // Allocate the voxels
458 myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
461 // Copied from Voxel_ColorDS.cxx:
462 Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
463 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
464 // myData[0 .. nb_slices - 1][0 .. 31]
467 Standard_Integer i1 = 0, i2 = 0, value = 0;
470 fread(&i1, sizeof(Standard_Integer), 1, f);
471 fread(&i2, sizeof(Standard_Integer), 1, f);
472 fread(&value, sizeof(Standard_Byte), 1, f);
475 if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
477 ((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] =
478 (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
480 (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = value;
485 return Standard_True;
488 Standard_Boolean Voxel_Reader::ReadFloatBinaryVoxels(const TCollection_ExtendedString& file)
490 // Open file for reading
491 FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
493 return Standard_False;
496 Standard_Character line[64];
499 // Location, size, number of splits
500 Standard_Integer nbx = 0, nby = 0, nbz = 0;
501 Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
502 fread(&x, sizeof(Standard_Real), 1, f);
503 fread(&y, sizeof(Standard_Real), 1, f);
504 fread(&z, sizeof(Standard_Real), 1, f);
505 fread(&xlen, sizeof(Standard_Real), 1, f);
506 fread(&ylen, sizeof(Standard_Real), 1, f);
507 fread(&zlen, sizeof(Standard_Real), 1, f);
508 fread(&nbx, sizeof(Standard_Integer), 1, f);
509 fread(&nby, sizeof(Standard_Integer), 1, f);
510 fread(&nbz, sizeof(Standard_Integer), 1, f);
512 // Allocate the voxels
513 myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
516 // Copied from Voxel_FloatDS.cxx:
517 Standard_Integer nb_floats = nbx * nby * nbz;
518 Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
519 // myData[0 .. nb_slices - 1][0 .. 31]
522 Standard_Integer i1 = 0, i2 = 0;
523 Standard_ShortReal value = 0.0;
526 fread(&i1, sizeof(Standard_Integer), 1, f);
527 fread(&i2, sizeof(Standard_Integer), 1, f);
528 fread(&value, sizeof(Standard_ShortReal), 1, f);
531 if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
533 ((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] =
534 (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
536 (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
541 return Standard_True;