o3dgcTriangleFans.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "o3dgcTriangleFans.h"
  20. #include "o3dgcArithmeticCodec.h"
  21. //#define DEBUG_VERBOSE
  22. namespace o3dgc
  23. {
  24. #ifdef DEBUG_VERBOSE
  25. FILE* g_fileDebugTF = NULL;
  26. #endif //DEBUG_VERBOSE
  27. O3DGCErrorCode SaveUIntData(const Vector<long> & data,
  28. BinaryStream & bstream)
  29. {
  30. unsigned long start = bstream.GetSize();
  31. bstream.WriteUInt32ASCII(0);
  32. const unsigned long size = data.GetSize();
  33. bstream.WriteUInt32ASCII(size);
  34. for(unsigned long i = 0; i < size; ++i)
  35. {
  36. bstream.WriteUIntASCII(data[i]);
  37. }
  38. bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
  39. return O3DGC_OK;
  40. }
  41. O3DGCErrorCode SaveIntData(const Vector<long> & data,
  42. BinaryStream & bstream)
  43. {
  44. unsigned long start = bstream.GetSize();
  45. bstream.WriteUInt32ASCII(0);
  46. const unsigned long size = data.GetSize();
  47. bstream.WriteUInt32ASCII(size);
  48. for(unsigned long i = 0; i < size; ++i)
  49. {
  50. bstream.WriteIntASCII(data[i]);
  51. }
  52. bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
  53. return O3DGC_OK;
  54. }
  55. O3DGCErrorCode SaveBinData(const Vector<long> & data,
  56. BinaryStream & bstream)
  57. {
  58. unsigned long start = bstream.GetSize();
  59. bstream.WriteUInt32ASCII(0);
  60. const unsigned long size = data.GetSize();
  61. long symbol;
  62. bstream.WriteUInt32ASCII(size);
  63. for(unsigned long i = 0; i < size; )
  64. {
  65. symbol = 0;
  66. for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 && i < size; ++h)
  67. {
  68. symbol += (data[i] << h);
  69. ++i;
  70. }
  71. bstream.WriteUCharASCII((unsigned char) symbol);
  72. }
  73. bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
  74. return O3DGC_OK;
  75. }
  76. O3DGCErrorCode CompressedTriangleFans::SaveUIntAC(const Vector<long> & data,
  77. const unsigned long M,
  78. BinaryStream & bstream)
  79. {
  80. unsigned long start = bstream.GetSize();
  81. const unsigned int NMAX = data.GetSize() * 8 + 100;
  82. const unsigned long size = data.GetSize();
  83. long minValue = O3DGC_MAX_LONG;
  84. bstream.WriteUInt32Bin(0);
  85. bstream.WriteUInt32Bin(size);
  86. if (size > 0)
  87. {
  88. #ifdef DEBUG_VERBOSE
  89. printf("-----------\nsize %i, start %i\n", size, start);
  90. fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
  91. #endif //DEBUG_VERBOSE
  92. for(unsigned long i = 0; i < size; ++i)
  93. {
  94. if (minValue > data[i])
  95. {
  96. minValue = data[i];
  97. }
  98. #ifdef DEBUG_VERBOSE
  99. printf("%i\t%i\n", i, data[i]);
  100. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  101. #endif //DEBUG_VERBOSE
  102. }
  103. bstream.WriteUInt32Bin(minValue);
  104. if ( m_sizeBufferAC < NMAX )
  105. {
  106. delete [] m_bufferAC;
  107. m_sizeBufferAC = NMAX;
  108. m_bufferAC = new unsigned char [m_sizeBufferAC];
  109. }
  110. Arithmetic_Codec ace;
  111. ace.set_buffer(NMAX, m_bufferAC);
  112. ace.start_encoder();
  113. Adaptive_Data_Model mModelValues(M+1);
  114. for(unsigned long i = 0; i < size; ++i)
  115. {
  116. ace.encode(data[i]-minValue, mModelValues);
  117. }
  118. unsigned long encodedBytes = ace.stop_encoder();
  119. for(unsigned long i = 0; i < encodedBytes; ++i)
  120. {
  121. bstream.WriteUChar8Bin(m_bufferAC[i]);
  122. }
  123. }
  124. bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
  125. return O3DGC_OK;
  126. }
  127. O3DGCErrorCode CompressedTriangleFans::SaveBinAC(const Vector<long> & data,
  128. BinaryStream & bstream)
  129. {
  130. unsigned long start = bstream.GetSize();
  131. const unsigned int NMAX = data.GetSize() * 8 + 100;
  132. const unsigned long size = data.GetSize();
  133. bstream.WriteUInt32Bin(0);
  134. bstream.WriteUInt32Bin(size);
  135. if (size > 0)
  136. {
  137. if ( m_sizeBufferAC < NMAX )
  138. {
  139. delete [] m_bufferAC;
  140. m_sizeBufferAC = NMAX;
  141. m_bufferAC = new unsigned char [m_sizeBufferAC];
  142. }
  143. Arithmetic_Codec ace;
  144. ace.set_buffer(NMAX, m_bufferAC);
  145. ace.start_encoder();
  146. Adaptive_Bit_Model bModel;
  147. #ifdef DEBUG_VERBOSE
  148. printf("-----------\nsize %i, start %i\n", size, start);
  149. fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
  150. #endif //DEBUG_VERBOSE
  151. for(unsigned long i = 0; i < size; ++i)
  152. {
  153. ace.encode(data[i], bModel);
  154. #ifdef DEBUG_VERBOSE
  155. printf("%i\t%i\n", i, data[i]);
  156. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  157. #endif //DEBUG_VERBOSE
  158. }
  159. unsigned long encodedBytes = ace.stop_encoder();
  160. for(unsigned long i = 0; i < encodedBytes; ++i)
  161. {
  162. bstream.WriteUChar8Bin(m_bufferAC[i]);
  163. }
  164. }
  165. bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
  166. return O3DGC_OK;
  167. }
  168. O3DGCErrorCode CompressedTriangleFans::SaveIntACEGC(const Vector<long> & data,
  169. const unsigned long M,
  170. BinaryStream & bstream)
  171. {
  172. unsigned long start = bstream.GetSize();
  173. const unsigned int NMAX = data.GetSize() * 8 + 100;
  174. const unsigned long size = data.GetSize();
  175. long minValue = 0;
  176. bstream.WriteUInt32Bin(0);
  177. bstream.WriteUInt32Bin(size);
  178. if (size > 0)
  179. {
  180. #ifdef DEBUG_VERBOSE
  181. printf("-----------\nsize %i, start %i\n", size, start);
  182. fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
  183. #endif //DEBUG_VERBOSE
  184. for(unsigned long i = 0; i < size; ++i)
  185. {
  186. if (minValue > data[i])
  187. {
  188. minValue = data[i];
  189. }
  190. #ifdef DEBUG_VERBOSE
  191. printf("%i\t%i\n", i, data[i]);
  192. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  193. #endif //DEBUG_VERBOSE
  194. }
  195. bstream.WriteUInt32Bin(minValue + O3DGC_MAX_LONG);
  196. if ( m_sizeBufferAC < NMAX )
  197. {
  198. delete [] m_bufferAC;
  199. m_sizeBufferAC = NMAX;
  200. m_bufferAC = new unsigned char [m_sizeBufferAC];
  201. }
  202. Arithmetic_Codec ace;
  203. ace.set_buffer(NMAX, m_bufferAC);
  204. ace.start_encoder();
  205. Adaptive_Data_Model mModelValues(M+2);
  206. Static_Bit_Model bModel0;
  207. Adaptive_Bit_Model bModel1;
  208. unsigned long value;
  209. for(unsigned long i = 0; i < size; ++i)
  210. {
  211. value = data[i]-minValue;
  212. if (value < M)
  213. {
  214. ace.encode(value, mModelValues);
  215. }
  216. else
  217. {
  218. ace.encode(M, mModelValues);
  219. ace.ExpGolombEncode(value-M, 0, bModel0, bModel1);
  220. }
  221. }
  222. unsigned long encodedBytes = ace.stop_encoder();
  223. for(unsigned long i = 0; i < encodedBytes; ++i)
  224. {
  225. bstream.WriteUChar8Bin(m_bufferAC[i]);
  226. }
  227. }
  228. bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
  229. return O3DGC_OK;
  230. }
  231. O3DGCErrorCode CompressedTriangleFans::Save(BinaryStream & bstream, bool encodeTrianglesOrder, O3DGCStreamType streamType)
  232. {
  233. #ifdef DEBUG_VERBOSE
  234. g_fileDebugTF = fopen("SaveIntACEGC_new.txt", "w");
  235. #endif //DEBUG_VERBOSE
  236. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  237. {
  238. SaveUIntData(m_numTFANs , bstream);
  239. SaveUIntData(m_degrees , bstream);
  240. SaveUIntData(m_configs , bstream);
  241. SaveBinData (m_operations, bstream);
  242. SaveIntData (m_indices , bstream);
  243. if (encodeTrianglesOrder)
  244. {
  245. SaveUIntData(m_trianglesOrder, bstream);
  246. }
  247. }
  248. else
  249. {
  250. SaveIntACEGC(m_numTFANs , 4 , bstream);
  251. SaveIntACEGC(m_degrees , 16, bstream);
  252. SaveUIntAC (m_configs , 10, bstream);
  253. SaveBinAC (m_operations, bstream);
  254. SaveIntACEGC(m_indices , 8 , bstream);
  255. if (encodeTrianglesOrder)
  256. {
  257. SaveIntACEGC(m_trianglesOrder , 16, bstream);
  258. }
  259. }
  260. #ifdef DEBUG_VERBOSE
  261. fclose(g_fileDebugTF);
  262. #endif //DEBUG_VERBOSE
  263. return O3DGC_OK;
  264. }
  265. O3DGCErrorCode LoadUIntData(Vector<long> & data,
  266. const BinaryStream & bstream,
  267. unsigned long & iterator)
  268. {
  269. bstream.ReadUInt32ASCII(iterator);
  270. const unsigned long size = bstream.ReadUInt32ASCII(iterator);
  271. data.Allocate(size);
  272. data.Clear();
  273. for(unsigned long i = 0; i < size; ++i)
  274. {
  275. data.PushBack(bstream.ReadUIntASCII(iterator));
  276. }
  277. return O3DGC_OK;
  278. }
  279. O3DGCErrorCode LoadIntData(Vector<long> & data,
  280. const BinaryStream & bstream,
  281. unsigned long & iterator)
  282. {
  283. bstream.ReadUInt32ASCII(iterator);
  284. const unsigned long size = bstream.ReadUInt32ASCII(iterator);
  285. data.Allocate(size);
  286. data.Clear();
  287. for(unsigned long i = 0; i < size; ++i)
  288. {
  289. data.PushBack(bstream.ReadIntASCII(iterator));
  290. }
  291. return O3DGC_OK;
  292. }
  293. O3DGCErrorCode LoadBinData(Vector<long> & data,
  294. const BinaryStream & bstream,
  295. unsigned long & iterator)
  296. {
  297. bstream.ReadUInt32ASCII(iterator);
  298. const unsigned long size = bstream.ReadUInt32ASCII(iterator);
  299. long symbol;
  300. data.Allocate(size * O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
  301. data.Clear();
  302. for(unsigned long i = 0; i < size;)
  303. {
  304. symbol = bstream.ReadUCharASCII(iterator);
  305. for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0; ++h)
  306. {
  307. data.PushBack(symbol & 1);
  308. symbol >>= 1;
  309. ++i;
  310. }
  311. }
  312. return O3DGC_OK;
  313. }
  314. O3DGCErrorCode LoadUIntAC(Vector<long> & data,
  315. const unsigned long M,
  316. const BinaryStream & bstream,
  317. unsigned long & iterator)
  318. {
  319. unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
  320. unsigned long size = bstream.ReadUInt32Bin(iterator);
  321. if (size == 0)
  322. {
  323. return O3DGC_OK;
  324. }
  325. long minValue = bstream.ReadUInt32Bin(iterator);
  326. unsigned char * buffer = 0;
  327. bstream.GetBuffer(iterator, buffer);
  328. iterator += sizeSize;
  329. data.Allocate(size);
  330. Arithmetic_Codec acd;
  331. acd.set_buffer(sizeSize, buffer);
  332. acd.start_decoder();
  333. Adaptive_Data_Model mModelValues(M+1);
  334. #ifdef DEBUG_VERBOSE
  335. printf("-----------\nsize %i\n", size);
  336. fprintf(g_fileDebugTF, "size %i\n", size);
  337. #endif //DEBUG_VERBOSE
  338. for(unsigned long i = 0; i < size; ++i)
  339. {
  340. data.PushBack(acd.decode(mModelValues)+minValue);
  341. #ifdef DEBUG_VERBOSE
  342. printf("%i\t%i\n", i, data[i]);
  343. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  344. #endif //DEBUG_VERBOSE
  345. }
  346. return O3DGC_OK;
  347. }
  348. O3DGCErrorCode LoadIntACEGC(Vector<long> & data,
  349. const unsigned long M,
  350. const BinaryStream & bstream,
  351. unsigned long & iterator)
  352. {
  353. unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
  354. unsigned long size = bstream.ReadUInt32Bin(iterator);
  355. if (size == 0)
  356. {
  357. return O3DGC_OK;
  358. }
  359. long minValue = bstream.ReadUInt32Bin(iterator) - O3DGC_MAX_LONG;
  360. unsigned char * buffer = 0;
  361. bstream.GetBuffer(iterator, buffer);
  362. iterator += sizeSize;
  363. data.Allocate(size);
  364. Arithmetic_Codec acd;
  365. acd.set_buffer(sizeSize, buffer);
  366. acd.start_decoder();
  367. Adaptive_Data_Model mModelValues(M+2);
  368. Static_Bit_Model bModel0;
  369. Adaptive_Bit_Model bModel1;
  370. unsigned long value;
  371. #ifdef DEBUG_VERBOSE
  372. printf("-----------\nsize %i\n", size);
  373. fprintf(g_fileDebugTF, "size %i\n", size);
  374. #endif //DEBUG_VERBOSE
  375. for(unsigned long i = 0; i < size; ++i)
  376. {
  377. value = acd.decode(mModelValues);
  378. if ( value == M)
  379. {
  380. value += acd.ExpGolombDecode(0, bModel0, bModel1);
  381. }
  382. data.PushBack(value + minValue);
  383. #ifdef DEBUG_VERBOSE
  384. printf("%i\t%i\n", i, data[i]);
  385. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  386. #endif //DEBUG_VERBOSE
  387. }
  388. #ifdef DEBUG_VERBOSE
  389. fflush(g_fileDebugTF);
  390. #endif //DEBUG_VERBOSE
  391. return O3DGC_OK;
  392. }
  393. O3DGCErrorCode LoadBinAC(Vector<long> & data,
  394. const BinaryStream & bstream,
  395. unsigned long & iterator)
  396. {
  397. unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 8;
  398. unsigned long size = bstream.ReadUInt32Bin(iterator);
  399. if (size == 0)
  400. {
  401. return O3DGC_OK;
  402. }
  403. unsigned char * buffer = 0;
  404. bstream.GetBuffer(iterator, buffer);
  405. iterator += sizeSize;
  406. data.Allocate(size);
  407. Arithmetic_Codec acd;
  408. acd.set_buffer(sizeSize, buffer);
  409. acd.start_decoder();
  410. Adaptive_Bit_Model bModel;
  411. #ifdef DEBUG_VERBOSE
  412. printf("-----------\nsize %i\n", size);
  413. fprintf(g_fileDebugTF, "size %i\n", size);
  414. #endif //DEBUG_VERBOSE
  415. for(unsigned long i = 0; i < size; ++i)
  416. {
  417. data.PushBack(acd.decode(bModel));
  418. #ifdef DEBUG_VERBOSE
  419. printf("%i\t%i\n", i, data[i]);
  420. fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
  421. #endif //DEBUG_VERBOSE
  422. }
  423. return O3DGC_OK;
  424. }
  425. O3DGCErrorCode CompressedTriangleFans::Load(const BinaryStream & bstream,
  426. unsigned long & iterator,
  427. bool decodeTrianglesOrder,
  428. O3DGCStreamType streamType)
  429. {
  430. #ifdef DEBUG_VERBOSE
  431. g_fileDebugTF = fopen("Load_new.txt", "w");
  432. #endif //DEBUG_VERBOSE
  433. if (streamType == O3DGC_STREAM_TYPE_ASCII)
  434. {
  435. LoadUIntData(m_numTFANs , bstream, iterator);
  436. LoadUIntData(m_degrees , bstream, iterator);
  437. LoadUIntData(m_configs , bstream, iterator);
  438. LoadBinData (m_operations, bstream, iterator);
  439. LoadIntData (m_indices , bstream, iterator);
  440. if (decodeTrianglesOrder)
  441. {
  442. LoadUIntData(m_trianglesOrder , bstream, iterator);
  443. }
  444. }
  445. else
  446. {
  447. LoadIntACEGC(m_numTFANs , 4 , bstream, iterator);
  448. LoadIntACEGC(m_degrees , 16, bstream, iterator);
  449. LoadUIntAC (m_configs , 10, bstream, iterator);
  450. LoadBinAC (m_operations, bstream, iterator);
  451. LoadIntACEGC(m_indices , 8 , bstream, iterator);
  452. if (decodeTrianglesOrder)
  453. {
  454. LoadIntACEGC(m_trianglesOrder , 16, bstream, iterator);
  455. }
  456. }
  457. #ifdef DEBUG_VERBOSE
  458. fclose(g_fileDebugTF);
  459. #endif //DEBUG_VERBOSE
  460. return O3DGC_OK;
  461. }
  462. }