/opencascade-6.5.1/ros/src/Voxel/Voxel_Reader.cxx

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