1 // Created on: 2008-08-28
2 // Created by: Vladislav ROMASHKO
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Voxel_Writer.ixx>
17 #include <Voxel_TypeDef.hxx>
19 #include <Precision.hxx>
20 #include <TCollection_AsciiString.hxx>
21 #include <OSD_OpenFile.hxx>
23 Voxel_Writer::Voxel_Writer():myFormat(Voxel_VFF_ASCII),myBoolVoxels(0),myColorVoxels(0),myFloatVoxels(0)
28 void Voxel_Writer::SetFormat(const Voxel_VoxelFileFormat format)
33 void Voxel_Writer::SetVoxels(const Voxel_BoolDS& voxels)
35 myBoolVoxels = (Standard_Address) &voxels;
40 void Voxel_Writer::SetVoxels(const Voxel_ColorDS& voxels)
43 myColorVoxels = (Standard_Address) &voxels;
47 void Voxel_Writer::SetVoxels(const Voxel_FloatDS& voxels)
51 myFloatVoxels = (Standard_Address) &voxels;
54 Standard_Boolean Voxel_Writer::Write(const TCollection_ExtendedString& file) const
61 return WriteBoolAsciiVoxels(file);
62 else if (myColorVoxels)
63 return WriteColorAsciiVoxels(file);
64 else if (myFloatVoxels)
65 return WriteFloatAsciiVoxels(file);
67 case Voxel_VFF_BINARY:
70 return WriteBoolBinaryVoxels(file);
71 else if (myColorVoxels)
72 return WriteColorBinaryVoxels(file);
73 else if (myFloatVoxels)
74 return WriteFloatBinaryVoxels(file);
78 // No voxels or no format description is found:
79 return Standard_False;
82 Standard_Boolean Voxel_Writer::WriteBoolAsciiVoxels(const TCollection_ExtendedString& file) const
84 Voxel_BoolDS* ds = (Voxel_BoolDS*) myBoolVoxels;
86 return Standard_False;
88 // Open file for writing
89 FILE* f = OSD_OpenFile(file, "w+");
91 return Standard_False;
93 // Header: file format, type of voxels
101 // Location, size, number of splits
102 fprintf(f, "%g %g %g\n", ds->GetX(), ds->GetY(), ds->GetZ());
103 fprintf(f, "%g %g %g\n", ds->GetXLen(), ds->GetYLen(), ds->GetZLen());
104 fprintf(f, "%d %d %d\n", ds->GetNbX(), ds->GetNbY(), ds->GetNbZ());
107 // Copied from Voxel_BoolDS.cxx:
108 Standard_Integer nb_bytes = RealToInt(ceil(ds->GetNbX() * ds->GetNbY() * ds->GetNbZ() / 8.0));
109 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
110 // myData[0 .. nb_slices - 1][0 .. 7]
113 Standard_Integer i1 = 0, i2 = 0;
114 for (i1 = 0; i1 < nb_slices; i1++)
116 if (((Standard_Byte**)ds->myData)[i1])
118 Standard_Boolean has_value = Standard_False;
119 fprintf(f, "%d ", i1); // index of slice
120 for (i2 = 0; i2 < 8; i2++)
122 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)ds->myData)[i1])[i2];
125 has_value = Standard_True;
126 fprintf(f, "%d %d\n", i2, value);
138 return Standard_True;
141 Standard_Boolean Voxel_Writer::WriteColorAsciiVoxels(const TCollection_ExtendedString& file) const
143 Voxel_ColorDS* ds = (Voxel_ColorDS*) myColorVoxels;
145 return Standard_False;
147 // Open file for writing
148 FILE* f = OSD_OpenFile(file, "w+");
150 return Standard_False;
152 // Header: file format, type of voxels
160 // Location, size, number of splits
161 fprintf(f, "%g %g %g\n", ds->GetX(), ds->GetY(), ds->GetZ());
162 fprintf(f, "%g %g %g\n", ds->GetXLen(), ds->GetYLen(), ds->GetZLen());
163 fprintf(f, "%d %d %d\n", ds->GetNbX(), ds->GetNbY(), ds->GetNbZ());
166 // Copied from Voxel_ColorDS.cxx:
167 Standard_Integer nb_bytes = RealToInt(ceil(ds->GetNbX() * ds->GetNbY() * ds->GetNbZ() / 2.0));
168 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
169 // myData[0 .. nb_slices - 1][0 .. 31]
172 Standard_Integer i1 = 0, i2 = 0;
173 for (i1 = 0; i1 < nb_slices; i1++)
175 if (((Standard_Byte**)ds->myData)[i1])
177 Standard_Boolean has_value = Standard_False;
178 fprintf(f, "%d ", i1); // index of slice
179 for (i2 = 0; i2 < 32; i2++)
181 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)ds->myData)[i1])[i2];
184 has_value = Standard_True;
185 fprintf(f, "%d %d\n", i2, value);
197 return Standard_True;
200 Standard_Boolean Voxel_Writer::WriteFloatAsciiVoxels(const TCollection_ExtendedString& file) const
202 Voxel_FloatDS* ds = (Voxel_FloatDS*) myFloatVoxels;
204 return Standard_False;
206 // Open file for writing
207 FILE* f = OSD_OpenFile(file, "w+");
209 return Standard_False;
211 // Header: file format, type of voxels
219 // Location, size, number of splits
220 fprintf(f, "%g %g %g\n", ds->GetX(), ds->GetY(), ds->GetZ());
221 fprintf(f, "%g %g %g\n", ds->GetXLen(), ds->GetYLen(), ds->GetZLen());
222 fprintf(f, "%d %d %d\n", ds->GetNbX(), ds->GetNbY(), ds->GetNbZ());
225 // Copied from Voxel_FloatDS.cxx:
226 Standard_Integer nb_floats = ds->GetNbX() * ds->GetNbY() * ds->GetNbZ();
227 Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
228 // myData[0 .. nb_slices - 1][0 .. 31]
231 Standard_Integer i1 = 0, i2 = 0;
232 for (i1 = 0; i1 < nb_slices; i1++)
234 if (((Standard_ShortReal**)ds->myData)[i1])
236 Standard_Boolean has_value = Standard_False;
237 fprintf(f, "%d ", i1); // index of slice
238 for (i2 = 0; i2 < 32; i2++)
240 Standard_ShortReal value = ((Standard_ShortReal*)((Standard_ShortReal**)ds->myData)[i1])[i2];
243 has_value = Standard_True;
244 fprintf(f, "%d %g\n", i2, value);
256 return Standard_True;
259 Standard_Boolean Voxel_Writer::WriteBoolBinaryVoxels(const TCollection_ExtendedString& file) const
261 Voxel_BoolDS* ds = (Voxel_BoolDS*) myBoolVoxels;
263 return Standard_False;
265 // Open file for writing
266 FILE* f = OSD_OpenFile(file, "wb");
268 return Standard_False;
270 // Header: file format, type of voxels
278 // Location, size, number of splits
279 fwrite(&(ds->myX), sizeof(Standard_Real), 1, f);
280 fwrite(&(ds->myY), sizeof(Standard_Real), 1, f);
281 fwrite(&(ds->myZ), sizeof(Standard_Real), 1, f);
282 fwrite(&(ds->myXLen), sizeof(Standard_Real), 1, f);
283 fwrite(&(ds->myYLen), sizeof(Standard_Real), 1, f);
284 fwrite(&(ds->myZLen), sizeof(Standard_Real), 1, f);
285 fwrite(&(ds->myNbX), sizeof(Standard_Integer), 1, f);
286 fwrite(&(ds->myNbY), sizeof(Standard_Integer), 1, f);
287 fwrite(&(ds->myNbZ), sizeof(Standard_Integer), 1, f);
290 // Copied from Voxel_BoolDS.cxx:
291 Standard_Integer nb_bytes = RealToInt(ceil(ds->GetNbX() * ds->GetNbY() * ds->GetNbZ() / 8.0));
292 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
293 // myData[0 .. nb_slices - 1][0 .. 7]
296 Standard_Integer i1 = 0, i2 = 0;
297 for (i1 = 0; i1 < nb_slices; i1++)
299 if (((Standard_Byte**)ds->myData)[i1])
301 for (i2 = 0; i2 < 8; i2++)
303 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)ds->myData)[i1])[i2];
306 fwrite(&i1, sizeof(Standard_Integer), 1, f);
307 fwrite(&i2, sizeof(Standard_Integer), 1, f);
308 fwrite(&value, sizeof(Standard_Byte), 1, f);
316 return Standard_True;
319 Standard_Boolean Voxel_Writer::WriteColorBinaryVoxels(const TCollection_ExtendedString& file) const
321 Voxel_ColorDS* ds = (Voxel_ColorDS*) myColorVoxels;
323 return Standard_False;
325 // Open file for writing
326 FILE* f = OSD_OpenFile(file, "wb");
328 return Standard_False;
330 // Header: file format, type of voxels
338 // Location, size, number of splits
339 fwrite(&(ds->myX), sizeof(Standard_Real), 1, f);
340 fwrite(&(ds->myY), sizeof(Standard_Real), 1, f);
341 fwrite(&(ds->myZ), sizeof(Standard_Real), 1, f);
342 fwrite(&(ds->myXLen), sizeof(Standard_Real), 1, f);
343 fwrite(&(ds->myYLen), sizeof(Standard_Real), 1, f);
344 fwrite(&(ds->myZLen), sizeof(Standard_Real), 1, f);
345 fwrite(&(ds->myNbX), sizeof(Standard_Integer), 1, f);
346 fwrite(&(ds->myNbY), sizeof(Standard_Integer), 1, f);
347 fwrite(&(ds->myNbZ), sizeof(Standard_Integer), 1, f);
350 // Copied from Voxel_ColorDS.cxx:
351 Standard_Integer nb_bytes = RealToInt(ceil(ds->myNbX * ds->myNbY * ds->myNbZ / 2.0));
352 Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
353 // myData[0 .. nb_slices - 1][0 .. 31]
356 Standard_Integer i1 = 0, i2 = 0;
357 for (i1 = 0; i1 < nb_slices; i1++)
359 if (((Standard_Byte**)ds->myData)[i1])
361 for (i2 = 0; i2 < 32; i2++)
363 Standard_Byte value = ((Standard_Byte*)((Standard_Byte**)ds->myData)[i1])[i2];
366 fwrite(&i1, sizeof(Standard_Integer), 1, f);
367 fwrite(&i2, sizeof(Standard_Integer), 1, f);
368 fwrite(&value, sizeof(Standard_Byte), 1, f);
376 return Standard_True;
379 Standard_Boolean Voxel_Writer::WriteFloatBinaryVoxels(const TCollection_ExtendedString& file) const
381 Voxel_FloatDS* ds = (Voxel_FloatDS*) myFloatVoxels;
383 return Standard_False;
385 // Open file for writing
386 FILE* f = OSD_OpenFile(file, "wb");
388 return Standard_False;
390 // Header: file format, type of voxels
398 // Location, size, number of splits
399 fwrite(&(ds->myX), sizeof(Standard_Real), 1, f);
400 fwrite(&(ds->myY), sizeof(Standard_Real), 1, f);
401 fwrite(&(ds->myZ), sizeof(Standard_Real), 1, f);
402 fwrite(&(ds->myXLen), sizeof(Standard_Real), 1, f);
403 fwrite(&(ds->myYLen), sizeof(Standard_Real), 1, f);
404 fwrite(&(ds->myZLen), sizeof(Standard_Real), 1, f);
405 fwrite(&(ds->myNbX), sizeof(Standard_Integer), 1, f);
406 fwrite(&(ds->myNbY), sizeof(Standard_Integer), 1, f);
407 fwrite(&(ds->myNbZ), sizeof(Standard_Integer), 1, f);
410 // Copied from Voxel_FloatDS.cxx:
411 Standard_Integer nb_floats = ds->myNbX * ds->myNbY * ds->myNbZ;
412 Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
413 // myData[0 .. nb_slices - 1][0 .. 31]
416 Standard_Integer i1 = 0, i2 = 0;
417 Standard_Real small = Precision::SquareConfusion();
418 for (i1 = 0; i1 < nb_slices; i1++)
420 if (((Standard_ShortReal**)ds->myData)[i1])
422 for (i2 = 0; i2 < 32; i2++)
424 Standard_ShortReal value = ((Standard_ShortReal*)((Standard_ShortReal**)ds->myData)[i1])[i2];
425 if (fabs(value) > small)
427 fwrite(&i1, sizeof(Standard_Integer), 1, f);
428 fwrite(&i2, sizeof(Standard_Integer), 1, f);
429 fwrite(&value, sizeof(Standard_ShortReal), 1, f);
437 return Standard_True;