/src/Voxel/Voxel_Reader.cxx

https://github.com/hmeyer/oce · C++ · 529 lines · 410 code · 59 blank · 60 comment · 83 complexity · f33f66b0e76bd47ec027c201f761d3fe MD5 · raw file

  1. // File: Voxel_Reader.cxx
  2. // Created: Wed Aug 28 19:34:33 2008
  3. // Author: Vladislav ROMASHKO
  4. // <vladislav.romashko@opencascade.com>
  5. #include <Voxel_Reader.ixx>
  6. #include <Voxel_BoolDS.hxx>
  7. #include <Voxel_ColorDS.hxx>
  8. #include <Voxel_FloatDS.hxx>
  9. #include <Voxel_VoxelFileFormat.hxx>
  10. #include <Voxel_TypeDef.hxx>
  11. #include <TCollection_AsciiString.hxx>
  12. #include <stdio.h>
  13. Voxel_Reader::Voxel_Reader():myBoolVoxels(0),myColorVoxels(0),myFloatVoxels(0)
  14. {
  15. }
  16. Standard_Boolean Voxel_Reader::Read(const TCollection_ExtendedString& file)
  17. {
  18. // Open file in ASCII mode to read header
  19. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  20. if (!f)
  21. return Standard_False;
  22. // Read the header
  23. Standard_Byte type; // 0 - bool, 1 - color, 2 - float
  24. Voxel_VoxelFileFormat format;
  25. Standard_Character svoxels[8], sformat[8], stype[8];
  26. fscanf(f, "%s %s %s\n", svoxels, sformat, stype);
  27. fclose(f);
  28. // Take format, type of voxels.
  29. // Voxels
  30. if (strcmp(svoxels, VOXELS))
  31. return Standard_False;
  32. // Format
  33. if (strcmp(sformat, ASCII) == 0)
  34. format = Voxel_VFF_ASCII;
  35. else if (strcmp(sformat, BINARY) == 0)
  36. format = Voxel_VFF_BINARY;
  37. else
  38. return Standard_False;
  39. // Type of voxels
  40. if (strcmp(stype, BOOL) == 0)
  41. type = 0;
  42. else if (strcmp(stype, COLOR) == 0)
  43. type = 1;
  44. else if (strcmp(stype, FLOAT) == 0)
  45. type = 2;
  46. else
  47. return Standard_False;
  48. // Read the rest
  49. switch (format)
  50. {
  51. case Voxel_VFF_ASCII:
  52. {
  53. switch (type)
  54. {
  55. case 0:
  56. return ReadBoolAsciiVoxels(file);
  57. case 1:
  58. return ReadColorAsciiVoxels(file);
  59. case 2:
  60. return ReadFloatAsciiVoxels(file);
  61. }
  62. }
  63. case Voxel_VFF_BINARY:
  64. {
  65. switch (type)
  66. {
  67. case 0:
  68. return ReadBoolBinaryVoxels(file);
  69. case 1:
  70. return ReadColorBinaryVoxels(file);
  71. case 2:
  72. return ReadFloatBinaryVoxels(file);
  73. }
  74. }
  75. }
  76. // No voxels or no format description is found:
  77. return Standard_False;
  78. }
  79. Standard_Boolean Voxel_Reader::IsBoolVoxels() const
  80. {
  81. return (myBoolVoxels != 0);
  82. }
  83. Standard_Boolean Voxel_Reader::IsColorVoxels() const
  84. {
  85. return (myColorVoxels != 0);
  86. }
  87. Standard_Boolean Voxel_Reader::IsFloatVoxels() const
  88. {
  89. return (myFloatVoxels != 0);
  90. }
  91. Standard_Address Voxel_Reader::GetBoolVoxels() const
  92. {
  93. return myBoolVoxels;
  94. }
  95. Standard_Address Voxel_Reader::GetColorVoxels() const
  96. {
  97. return myColorVoxels;
  98. }
  99. Standard_Address Voxel_Reader::GetFloatVoxels() const
  100. {
  101. return myFloatVoxels;
  102. }
  103. static Standard_Boolean has_slice(const Standard_CString line)
  104. {
  105. Standard_Integer i = 0, nb_spaces = 0;
  106. while (line[i] != '\0')
  107. {
  108. if (line[i] == ' ')
  109. nb_spaces++;
  110. i++;
  111. }
  112. return (nb_spaces == 2);
  113. }
  114. Standard_Boolean Voxel_Reader::ReadBoolAsciiVoxels(const TCollection_ExtendedString& file)
  115. {
  116. // Open file for reading
  117. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  118. if (!f)
  119. return Standard_False;
  120. Standard_Character line[64], sx[32], sy[32], sz[32];
  121. // Header: skip it
  122. fgets(line, 64, f);
  123. // Location, size, number of splits
  124. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  125. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  126. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  127. {
  128. fclose(f);
  129. return Standard_False;
  130. }
  131. x = atof(sx); y = atof(sy); z = atof(sz);
  132. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  133. {
  134. fclose(f);
  135. return Standard_False;
  136. }
  137. xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  138. if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  139. {
  140. fclose(f);
  141. return Standard_False;
  142. }
  143. // Allocate the voxels
  144. myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  145. // Data
  146. // Copied from Voxel_BoolDS.cxx:
  147. Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
  148. Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
  149. // myData[0 .. nb_slices - 1][0 .. 7]
  150. if (nb_slices)
  151. {
  152. Standard_Integer i1 = 0, i2 = 0, value = 0;
  153. while (!feof(f))
  154. {
  155. fgets(line, 64, f);
  156. if (has_slice(line))
  157. {
  158. if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
  159. {
  160. fclose(f);
  161. return Standard_False;
  162. }
  163. }
  164. else
  165. {
  166. if (sscanf(line, "%d %d\n", &i2, &value) != 2)
  167. {
  168. fclose(f);
  169. return Standard_False;
  170. }
  171. }
  172. // Set value
  173. if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
  174. {
  175. ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] =
  176. (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
  177. }
  178. (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
  179. }
  180. }
  181. fclose(f);
  182. return Standard_True;
  183. }
  184. Standard_Boolean Voxel_Reader::ReadColorAsciiVoxels(const TCollection_ExtendedString& file)
  185. {
  186. // Open file for reading
  187. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  188. if (!f)
  189. return Standard_False;
  190. Standard_Character line[64], sx[32], sy[32], sz[32];
  191. // Header: skip it
  192. fgets(line, 64, f);
  193. // Location, size, number of splits
  194. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  195. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  196. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  197. {
  198. fclose(f);
  199. return Standard_False;
  200. }
  201. x = atof(sx); y = atof(sy); z = atof(sz);
  202. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  203. {
  204. fclose(f);
  205. return Standard_False;
  206. }
  207. xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  208. if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  209. {
  210. fclose(f);
  211. return Standard_False;
  212. }
  213. // Allocate the voxels
  214. myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  215. // Data
  216. // Copied from Voxel_ColorDS.cxx:
  217. Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
  218. Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
  219. // myData[0 .. nb_slices - 1][0 .. 31]
  220. if (nb_slices)
  221. {
  222. Standard_Integer i1 = 0, i2 = 0, value = 0;
  223. while (!feof(f))
  224. {
  225. fgets(line, 64, f);
  226. if (has_slice(line))
  227. {
  228. if (sscanf(line, "%d %d %d\n", &i1, &i2, &value) != 3)
  229. {
  230. fclose(f);
  231. return Standard_False;
  232. }
  233. }
  234. else
  235. {
  236. if (sscanf(line, "%d %d\n", &i2, &value) != 2)
  237. {
  238. fclose(f);
  239. return Standard_False;
  240. }
  241. }
  242. // Set value
  243. if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
  244. {
  245. ((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] =
  246. (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
  247. }
  248. (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
  249. }
  250. }
  251. fclose(f);
  252. return Standard_True;
  253. }
  254. Standard_Boolean Voxel_Reader::ReadFloatAsciiVoxels(const TCollection_ExtendedString& file)
  255. {
  256. // Open file for reading
  257. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "r");
  258. if (!f)
  259. return Standard_False;
  260. Standard_Character line[64], sx[32], sy[32], sz[32];
  261. // Header: skip it
  262. fgets(line, 64, f);
  263. // Location, size, number of splits
  264. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  265. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  266. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  267. {
  268. fclose(f);
  269. return Standard_False;
  270. }
  271. x = atof(sx); y = atof(sy); z = atof(sz);
  272. if (fscanf(f, "%s %s %s\n", sx, sy, sz) != 3)
  273. {
  274. fclose(f);
  275. return Standard_False;
  276. }
  277. xlen = atof(sx); ylen = atof(sy); zlen = atof(sz);
  278. if (fscanf(f, "%d %d %d\n", &nbx, &nby, &nbz) != 3)
  279. {
  280. fclose(f);
  281. return Standard_False;
  282. }
  283. // Allocate the voxels
  284. myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  285. // Data
  286. // Copied from Voxel_FloatDS.cxx:
  287. Standard_Integer nb_floats = nbx * nby * nbz;
  288. Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
  289. // myData[0 .. nb_slices - 1][0 .. 31]
  290. if (nb_slices)
  291. {
  292. Standard_Integer i1 = 0, i2 = 0;
  293. Standard_ShortReal value = 0.0;
  294. while (!feof(f))
  295. {
  296. fgets(line, 64, f);
  297. if (has_slice(line))
  298. {
  299. if (sscanf(line, "%d %d %s\n", &i1, &i2, line) != 3)
  300. {
  301. fclose(f);
  302. return Standard_False;
  303. }
  304. }
  305. else
  306. {
  307. if (sscanf(line, "%d %s\n", &i2, line) != 2)
  308. {
  309. fclose(f);
  310. return Standard_False;
  311. }
  312. }
  313. value = (Standard_ShortReal)atof(line);
  314. // Set value
  315. if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
  316. {
  317. ((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] =
  318. (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
  319. }
  320. (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
  321. }
  322. }
  323. fclose(f);
  324. return Standard_True;
  325. }
  326. Standard_Boolean Voxel_Reader::ReadBoolBinaryVoxels(const TCollection_ExtendedString& file)
  327. {
  328. // Open file for reading
  329. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  330. if (!f)
  331. return Standard_False;
  332. // Header: skip it
  333. Standard_Character line[64];
  334. fgets(line, 64, f);
  335. // Location, size, number of splits
  336. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  337. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  338. fread(&x, sizeof(Standard_Real), 1, f);
  339. fread(&y, sizeof(Standard_Real), 1, f);
  340. fread(&z, sizeof(Standard_Real), 1, f);
  341. fread(&xlen, sizeof(Standard_Real), 1, f);
  342. fread(&ylen, sizeof(Standard_Real), 1, f);
  343. fread(&zlen, sizeof(Standard_Real), 1, f);
  344. fread(&nbx, sizeof(Standard_Integer), 1, f);
  345. fread(&nby, sizeof(Standard_Integer), 1, f);
  346. fread(&nbz, sizeof(Standard_Integer), 1, f);
  347. // Allocate the voxels
  348. myBoolVoxels = (Standard_Address) new Voxel_BoolDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  349. // Data
  350. // Copied from Voxel_BoolDS.cxx:
  351. Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 8.0));
  352. Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 8.0));
  353. // myData[0 .. nb_slices - 1][0 .. 7]
  354. if (nb_slices)
  355. {
  356. Standard_Integer i1 = 0, i2 = 0, value = 0;
  357. while (!feof(f))
  358. {
  359. fread(&i1, sizeof(Standard_Integer), 1, f);
  360. fread(&i2, sizeof(Standard_Integer), 1, f);
  361. fread(&value, sizeof(Standard_Byte), 1, f);
  362. // Set value
  363. if (!((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])
  364. {
  365. ((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1] =
  366. (Standard_Byte*) calloc(8/*number of bytes in slice*/, sizeof(Standard_Byte));
  367. }
  368. (((Standard_Byte**)((Voxel_DS*)myBoolVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
  369. }
  370. }
  371. fclose(f);
  372. return Standard_True;
  373. }
  374. Standard_Boolean Voxel_Reader::ReadColorBinaryVoxels(const TCollection_ExtendedString& file)
  375. {
  376. // Open file for reading
  377. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  378. if (!f)
  379. return Standard_False;
  380. // Header: skip it
  381. Standard_Character line[64];
  382. fgets(line, 64, f);
  383. // Location, size, number of splits
  384. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  385. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  386. fread(&x, sizeof(Standard_Real), 1, f);
  387. fread(&y, sizeof(Standard_Real), 1, f);
  388. fread(&z, sizeof(Standard_Real), 1, f);
  389. fread(&xlen, sizeof(Standard_Real), 1, f);
  390. fread(&ylen, sizeof(Standard_Real), 1, f);
  391. fread(&zlen, sizeof(Standard_Real), 1, f);
  392. fread(&nbx, sizeof(Standard_Integer), 1, f);
  393. fread(&nby, sizeof(Standard_Integer), 1, f);
  394. fread(&nbz, sizeof(Standard_Integer), 1, f);
  395. // Allocate the voxels
  396. myColorVoxels = (Standard_Address) new Voxel_ColorDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  397. // Data
  398. // Copied from Voxel_ColorDS.cxx:
  399. Standard_Integer nb_bytes = RealToInt(ceil(nbx * nby * nbz / 2.0));
  400. Standard_Integer nb_slices = RealToInt(ceil(nb_bytes / 32.0));
  401. // myData[0 .. nb_slices - 1][0 .. 31]
  402. if (nb_slices)
  403. {
  404. Standard_Integer i1 = 0, i2 = 0, value = 0;
  405. while (!feof(f))
  406. {
  407. fread(&i1, sizeof(Standard_Integer), 1, f);
  408. fread(&i2, sizeof(Standard_Integer), 1, f);
  409. fread(&value, sizeof(Standard_Byte), 1, f);
  410. // Set value
  411. if (!((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])
  412. {
  413. ((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1] =
  414. (Standard_Byte*) calloc(32/*number of bytes in slice*/, sizeof(Standard_Byte));
  415. }
  416. (((Standard_Byte**)((Voxel_DS*)myColorVoxels)->myData)[i1])[i2] = (Standard_Byte)value;
  417. }
  418. }
  419. fclose(f);
  420. return Standard_True;
  421. }
  422. Standard_Boolean Voxel_Reader::ReadFloatBinaryVoxels(const TCollection_ExtendedString& file)
  423. {
  424. // Open file for reading
  425. FILE* f = fopen(TCollection_AsciiString(file, '?').ToCString(), "rb");
  426. if (!f)
  427. return Standard_False;
  428. // Header: skip it
  429. Standard_Character line[64];
  430. fgets(line, 64, f);
  431. // Location, size, number of splits
  432. Standard_Integer nbx = 0, nby = 0, nbz = 0;
  433. Standard_Real x = 0.0, y = 0.0, z = 0.0, xlen = 0.0, ylen = 0.0, zlen = 0.0;
  434. fread(&x, sizeof(Standard_Real), 1, f);
  435. fread(&y, sizeof(Standard_Real), 1, f);
  436. fread(&z, sizeof(Standard_Real), 1, f);
  437. fread(&xlen, sizeof(Standard_Real), 1, f);
  438. fread(&ylen, sizeof(Standard_Real), 1, f);
  439. fread(&zlen, sizeof(Standard_Real), 1, f);
  440. fread(&nbx, sizeof(Standard_Integer), 1, f);
  441. fread(&nby, sizeof(Standard_Integer), 1, f);
  442. fread(&nbz, sizeof(Standard_Integer), 1, f);
  443. // Allocate the voxels
  444. myFloatVoxels = (Standard_Address) new Voxel_FloatDS(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
  445. // Data
  446. // Copied from Voxel_FloatDS.cxx:
  447. Standard_Integer nb_floats = nbx * nby * nbz;
  448. Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
  449. // myData[0 .. nb_slices - 1][0 .. 31]
  450. if (nb_slices)
  451. {
  452. Standard_Integer i1 = 0, i2 = 0;
  453. Standard_ShortReal value = 0.0;
  454. while (!feof(f))
  455. {
  456. fread(&i1, sizeof(Standard_Integer), 1, f);
  457. fread(&i2, sizeof(Standard_Integer), 1, f);
  458. fread(&value, sizeof(Standard_ShortReal), 1, f);
  459. // Set value
  460. if (!((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])
  461. {
  462. ((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1] =
  463. (Standard_ShortReal*) calloc(32/*number of floats in slice*/, sizeof(Standard_ShortReal));
  464. }
  465. (((Standard_ShortReal**)((Voxel_DS*)myFloatVoxels)->myData)[i1])[i2] = value;
  466. }
  467. }
  468. fclose(f);
  469. return Standard_True;
  470. }