o3dgcSC3DMCEncoder.inl 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. #pragma once
  20. #ifndef O3DGC_SC3DMC_ENCODER_INL
  21. #define O3DGC_SC3DMC_ENCODER_INL
  22. #include "o3dgcArithmeticCodec.h"
  23. #include "o3dgcTimer.h"
  24. #include "o3dgcVector.h"
  25. #include "o3dgcBinaryStream.h"
  26. #include "o3dgcCommon.h"
  27. //#define DEBUG_VERBOSE
  28. namespace o3dgc
  29. {
  30. #ifdef DEBUG_VERBOSE
  31. FILE * g_fileDebugSC3DMCEnc = NULL;
  32. #endif //DEBUG_VERBOSE
  33. template <class T>
  34. O3DGCErrorCode SC3DMCEncoder<T>::Encode(const SC3DMCEncodeParams & params,
  35. const IndexedFaceSet<T> & ifs,
  36. BinaryStream & bstream)
  37. {
  38. // Encode header
  39. unsigned long start = bstream.GetSize();
  40. EncodeHeader(params, ifs, bstream);
  41. // Encode payload
  42. EncodePayload(params, ifs, bstream);
  43. bstream.WriteUInt32(m_posSize, bstream.GetSize() - start, m_streamType);
  44. return O3DGC_OK;
  45. }
  46. template <class T>
  47. O3DGCErrorCode SC3DMCEncoder<T>::EncodeHeader(const SC3DMCEncodeParams & params,
  48. const IndexedFaceSet<T> & ifs,
  49. BinaryStream & bstream)
  50. {
  51. m_streamType = params.GetStreamType();
  52. bstream.WriteUInt32(O3DGC_SC3DMC_START_CODE, m_streamType);
  53. m_posSize = bstream.GetSize();
  54. bstream.WriteUInt32(0, m_streamType); // to be filled later
  55. bstream.WriteUChar(O3DGC_SC3DMC_ENCODE_MODE_TFAN, m_streamType);
  56. bstream.WriteFloat32((float)ifs.GetCreaseAngle(), m_streamType);
  57. unsigned char mask = 0;
  58. bool markerBit0 = false;
  59. bool markerBit1 = false;
  60. bool markerBit2 = false;
  61. bool markerBit3 = false;
  62. mask += (ifs.GetCCW() );
  63. mask += (ifs.GetSolid() << 1);
  64. mask += (ifs.GetConvex() << 2);
  65. mask += (ifs.GetIsTriangularMesh() << 3);
  66. mask += (markerBit0 << 4);
  67. mask += (markerBit1 << 5);
  68. mask += (markerBit2 << 6);
  69. mask += (markerBit3 << 7);
  70. bstream.WriteUChar(mask, m_streamType);
  71. bstream.WriteUInt32(ifs.GetNCoord(), m_streamType);
  72. bstream.WriteUInt32(ifs.GetNNormal(), m_streamType);
  73. bstream.WriteUInt32(ifs.GetNumFloatAttributes(), m_streamType);
  74. bstream.WriteUInt32(ifs.GetNumIntAttributes(), m_streamType);
  75. if (ifs.GetNCoord() > 0)
  76. {
  77. bstream.WriteUInt32(ifs.GetNCoordIndex(), m_streamType);
  78. for(int j=0 ; j<3 ; ++j)
  79. {
  80. bstream.WriteFloat32((float) ifs.GetCoordMin(j), m_streamType);
  81. bstream.WriteFloat32((float) ifs.GetCoordMax(j), m_streamType);
  82. }
  83. bstream.WriteUChar((unsigned char) params.GetCoordQuantBits(), m_streamType);
  84. }
  85. if (ifs.GetNNormal() > 0)
  86. {
  87. bstream.WriteUInt32(0, m_streamType);
  88. for(int j=0 ; j<3 ; ++j)
  89. {
  90. bstream.WriteFloat32((float) ifs.GetNormalMin(j), m_streamType);
  91. bstream.WriteFloat32((float) ifs.GetNormalMax(j), m_streamType);
  92. }
  93. bstream.WriteUChar(true, m_streamType); //(unsigned char) ifs.GetNormalPerVertex()
  94. bstream.WriteUChar((unsigned char) params.GetNormalQuantBits(), m_streamType);
  95. }
  96. for(unsigned long a = 0; a < ifs.GetNumFloatAttributes(); ++a)
  97. {
  98. bstream.WriteUInt32(ifs.GetNFloatAttribute(a), m_streamType);
  99. if (ifs.GetNFloatAttribute(a) > 0)
  100. {
  101. assert(ifs.GetFloatAttributeDim(a) < (unsigned long) O3DGC_MAX_UCHAR8);
  102. bstream.WriteUInt32(0, m_streamType);
  103. unsigned char d = (unsigned char) ifs.GetFloatAttributeDim(a);
  104. bstream.WriteUChar(d, m_streamType);
  105. for(unsigned char j = 0 ; j < d ; ++j)
  106. {
  107. bstream.WriteFloat32((float) ifs.GetFloatAttributeMin(a, j), m_streamType);
  108. bstream.WriteFloat32((float) ifs.GetFloatAttributeMax(a, j), m_streamType);
  109. }
  110. bstream.WriteUChar(true, m_streamType); //(unsigned char) ifs.GetFloatAttributePerVertex(a)
  111. bstream.WriteUChar((unsigned char) ifs.GetFloatAttributeType(a), m_streamType);
  112. bstream.WriteUChar((unsigned char) params.GetFloatAttributeQuantBits(a), m_streamType);
  113. }
  114. }
  115. for(unsigned long a = 0; a < ifs.GetNumIntAttributes(); ++a)
  116. {
  117. bstream.WriteUInt32(ifs.GetNIntAttribute(a), m_streamType);
  118. if (ifs.GetNIntAttribute(a) > 0)
  119. {
  120. assert(ifs.GetFloatAttributeDim(a) < (unsigned long) O3DGC_MAX_UCHAR8);
  121. bstream.WriteUInt32(0, m_streamType);
  122. bstream.WriteUChar((unsigned char) ifs.GetIntAttributeDim(a), m_streamType);
  123. bstream.WriteUChar(true, m_streamType); // (unsigned char) ifs.GetIntAttributePerVertex(a)
  124. bstream.WriteUChar((unsigned char) ifs.GetIntAttributeType(a), m_streamType);
  125. }
  126. }
  127. return O3DGC_OK;
  128. }
  129. template <class T>
  130. O3DGCErrorCode SC3DMCEncoder<T>::QuantizeFloatArray(const Real * const floatArray,
  131. unsigned long numFloatArray,
  132. unsigned long dimFloatArray,
  133. unsigned long stride,
  134. const Real * const minFloatArray,
  135. const Real * const maxFloatArray,
  136. unsigned long nQBits)
  137. {
  138. const unsigned long size = numFloatArray * dimFloatArray;
  139. Real delta[O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES];
  140. Real r;
  141. for(unsigned long d = 0; d < dimFloatArray; d++)
  142. {
  143. r = maxFloatArray[d] - minFloatArray[d];
  144. if (r > 0.0f)
  145. {
  146. delta[d] = (float)((1 << nQBits) - 1) / r;
  147. }
  148. else
  149. {
  150. delta[d] = 1.0f;
  151. }
  152. }
  153. if (m_quantFloatArraySize < size)
  154. {
  155. delete [] m_quantFloatArray;
  156. m_quantFloatArraySize = size;
  157. m_quantFloatArray = new long [size];
  158. }
  159. for(unsigned long v = 0; v < numFloatArray; ++v)
  160. {
  161. for(unsigned long d = 0; d < dimFloatArray; ++d)
  162. {
  163. m_quantFloatArray[v * stride + d] = (long)((floatArray[v * stride + d]-minFloatArray[d]) * delta[d] + 0.5f);
  164. }
  165. }
  166. return O3DGC_OK;
  167. }
  168. template <class T>
  169. O3DGCErrorCode SC3DMCEncoder<T>::EncodeFloatArray(const Real * const floatArray,
  170. unsigned long numFloatArray,
  171. unsigned long dimFloatArray,
  172. unsigned long stride,
  173. const Real * const minFloatArray,
  174. const Real * const maxFloatArray,
  175. unsigned long nQBits,
  176. const IndexedFaceSet<T> & ifs,
  177. O3DGCSC3DMCPredictionMode predMode,
  178. BinaryStream & bstream)
  179. {
  180. assert(dimFloatArray < O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  181. long predResidual, v, uPredResidual;
  182. unsigned long nPred;
  183. Arithmetic_Codec ace;
  184. Static_Bit_Model bModel0;
  185. Adaptive_Bit_Model bModel1;
  186. const AdjacencyInfo & v2T = m_triangleListEncoder.GetVertexToTriangle();
  187. const long * const vmap = m_triangleListEncoder.GetVMap();
  188. const long * const invVMap = m_triangleListEncoder.GetInvVMap();
  189. const T * const triangles = ifs.GetCoordIndex();
  190. const long nvert = (long) numFloatArray;
  191. unsigned long start = bstream.GetSize();
  192. unsigned char mask = predMode & 7;
  193. const unsigned long M = O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS - 1;
  194. unsigned long nSymbols = O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS;
  195. unsigned long nPredictors = O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS;
  196. Adaptive_Data_Model mModelValues(M+2);
  197. Adaptive_Data_Model mModelPreds(O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS+1);
  198. memset(m_freqSymbols, 0, sizeof(unsigned long) * O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS);
  199. memset(m_freqPreds , 0, sizeof(unsigned long) * O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS);
  200. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  201. {
  202. mask += (O3DGC_SC3DMC_BINARIZATION_ASCII & 7)<<4;
  203. m_predictors.Allocate(nvert);
  204. m_predictors.Clear();
  205. }
  206. else
  207. {
  208. mask += (O3DGC_SC3DMC_BINARIZATION_AC_EGC & 7)<<4;
  209. const unsigned int NMAX = numFloatArray * dimFloatArray * 8 + 100;
  210. if ( m_sizeBufferAC < NMAX )
  211. {
  212. delete [] m_bufferAC;
  213. m_sizeBufferAC = NMAX;
  214. m_bufferAC = new unsigned char [m_sizeBufferAC];
  215. }
  216. ace.set_buffer(NMAX, m_bufferAC);
  217. ace.start_encoder();
  218. ace.ExpGolombEncode(0, 0, bModel0, bModel1);
  219. ace.ExpGolombEncode(M, 0, bModel0, bModel1);
  220. }
  221. bstream.WriteUInt32(0, m_streamType);
  222. bstream.WriteUChar(mask, m_streamType);
  223. #ifdef DEBUG_VERBOSE
  224. printf("FloatArray (%i, %i)\n", numFloatArray, dimFloatArray);
  225. fprintf(g_fileDebugSC3DMCEnc, "FloatArray (%i, %i)\n", numFloatArray, dimFloatArray);
  226. #endif //DEBUG_VERBOSE
  227. if (predMode == O3DGC_SC3DMC_SURF_NORMALS_PREDICTION)
  228. {
  229. const Real minFloatArray[2] = {(Real)(-2.0),(Real)(-2.0)};
  230. const Real maxFloatArray[2] = {(Real)(2.0),(Real)(2.0)};
  231. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  232. {
  233. for(unsigned long i = 0; i < numFloatArray; ++i)
  234. {
  235. bstream.WriteIntASCII(m_predictors[i]);
  236. }
  237. }
  238. else
  239. {
  240. Adaptive_Data_Model dModel(12);
  241. for(unsigned long i = 0; i < numFloatArray; ++i)
  242. {
  243. ace.encode(IntToUInt(m_predictors[i]), dModel);
  244. }
  245. }
  246. QuantizeFloatArray(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits+1);
  247. }
  248. else
  249. {
  250. QuantizeFloatArray(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits);
  251. }
  252. for (long vm=0; vm < nvert; ++vm)
  253. {
  254. nPred = 0;
  255. v = invVMap[vm];
  256. assert( v >= 0 && v < nvert);
  257. if ( v2T.GetNumNeighbors(v) > 0 &&
  258. predMode != O3DGC_SC3DMC_NO_PREDICTION)
  259. {
  260. int u0 = v2T.Begin(v);
  261. int u1 = v2T.End(v);
  262. for (long u = u0; u < u1; u++)
  263. {
  264. long ta = v2T.GetNeighbor(u);
  265. if ( predMode == O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION )
  266. {
  267. long a,b;
  268. if ((long) triangles[ta*3] == v)
  269. {
  270. a = triangles[ta*3 + 1];
  271. b = triangles[ta*3 + 2];
  272. }
  273. else if ((long) triangles[ta*3 + 1] == v)
  274. {
  275. a = triangles[ta*3 + 0];
  276. b = triangles[ta*3 + 2];
  277. }
  278. else
  279. {
  280. a = triangles[ta*3 + 0];
  281. b = triangles[ta*3 + 1];
  282. }
  283. if ( vmap[a] < vm && vmap[b] < vm)
  284. {
  285. int u0 = v2T.Begin(a);
  286. int u1 = v2T.End(a);
  287. for (long u = u0; u < u1; u++)
  288. {
  289. long tb = v2T.GetNeighbor(u);
  290. long c = -1;
  291. bool foundB = false;
  292. for(long k = 0; k < 3; ++k)
  293. {
  294. long x = triangles[tb*3 + k];
  295. if (x == b)
  296. {
  297. foundB = true;
  298. }
  299. if (vmap[x] < vm && x != a && x != b)
  300. {
  301. c = x;
  302. }
  303. }
  304. if (c != -1 && foundB)
  305. {
  306. SC3DMCTriplet id = {min(vmap[a], vmap[b]), max(vmap[a], vmap[b]), -vmap[c]-1};
  307. unsigned long p = Insert(id, nPred, m_neighbors);
  308. if (p != 0xFFFFFFFF)
  309. {
  310. for (unsigned long i = 0; i < dimFloatArray; i++)
  311. {
  312. m_neighbors[p].m_pred[i] = m_quantFloatArray[a*stride+i] +
  313. m_quantFloatArray[b*stride+i] -
  314. m_quantFloatArray[c*stride+i];
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. if ( predMode == O3DGC_SC3DMC_SURF_NORMALS_PREDICTION ||
  322. predMode == O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION ||
  323. predMode == O3DGC_SC3DMC_DIFFERENTIAL_PREDICTION )
  324. {
  325. for(long k = 0; k < 3; ++k)
  326. {
  327. long w = triangles[ta*3 + k];
  328. if ( vmap[w] < vm )
  329. {
  330. SC3DMCTriplet id = {-1, -1, vmap[w]};
  331. unsigned long p = Insert(id, nPred, m_neighbors);
  332. if (p != 0xFFFFFFFF)
  333. {
  334. for (unsigned long i = 0; i < dimFloatArray; i++)
  335. {
  336. m_neighbors[p].m_pred[i] = m_quantFloatArray[w*stride+i];
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. if (nPred > 1)
  345. {
  346. // find best predictor
  347. unsigned long bestPred = 0xFFFFFFFF;
  348. double bestCost = O3DGC_MAX_DOUBLE;
  349. double cost;
  350. #ifdef DEBUG_VERBOSE1
  351. printf("\t\t vm %i\n", vm);
  352. fprintf(g_fileDebugSC3DMCEnc, "\t\t vm %i\n", vm);
  353. #endif //DEBUG_VERBOSE
  354. for (unsigned long p = 0; p < nPred; ++p)
  355. {
  356. #ifdef DEBUG_VERBOSE1
  357. printf("\t\t pred a = %i b = %i c = %i \n", m_neighbors[p].m_id.m_a, m_neighbors[p].m_id.m_b, m_neighbors[p].m_id.m_c);
  358. fprintf(g_fileDebugSC3DMCEnc, "\t\t pred a = %i b = %i c = %i \n", m_neighbors[p].m_id.m_a, m_neighbors[p].m_id.m_b, m_neighbors[p].m_id.m_c);
  359. #endif //DEBUG_VERBOSE
  360. cost = -log2((m_freqPreds[p]+1.0) / nPredictors );
  361. for (unsigned long i = 0; i < dimFloatArray; ++i)
  362. {
  363. #ifdef DEBUG_VERBOSE1
  364. printf("\t\t\t %i\n", m_neighbors[p].m_pred[i]);
  365. fprintf(g_fileDebugSC3DMCEnc, "\t\t\t %i\n", m_neighbors[p].m_pred[i]);
  366. #endif //DEBUG_VERBOSE
  367. predResidual = (long) IntToUInt(m_quantFloatArray[v*stride+i] - m_neighbors[p].m_pred[i]);
  368. if (predResidual < (long) M)
  369. {
  370. cost += -log2((m_freqSymbols[predResidual]+1.0) / nSymbols );
  371. }
  372. else
  373. {
  374. cost += -log2((m_freqSymbols[M] + 1.0) / nSymbols ) + log2((double) (predResidual-M));
  375. }
  376. }
  377. if (cost < bestCost)
  378. {
  379. bestCost = cost;
  380. bestPred = p;
  381. }
  382. }
  383. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  384. {
  385. m_predictors.PushBack((unsigned char) bestPred);
  386. }
  387. else
  388. {
  389. ace.encode(bestPred, mModelPreds);
  390. }
  391. #ifdef DEBUG_VERBOSE1
  392. printf("best (%i, %i, %i) \t pos %i\n", m_neighbors[bestPred].m_id.m_a, m_neighbors[bestPred].m_id.m_b, m_neighbors[bestPred].m_id.m_c, bestPred);
  393. fprintf(g_fileDebugSC3DMCEnc, "best (%i, %i, %i) \t pos %i\n", m_neighbors[bestPred].m_id.m_a, m_neighbors[bestPred].m_id.m_b, m_neighbors[bestPred].m_id.m_c, bestPred);
  394. #endif //DEBUG_VERBOSE
  395. // use best predictor
  396. for (unsigned long i = 0; i < dimFloatArray; ++i)
  397. {
  398. predResidual = m_quantFloatArray[v*stride+i] - m_neighbors[bestPred].m_pred[i];
  399. uPredResidual = IntToUInt(predResidual);
  400. ++m_freqSymbols[(uPredResidual < (long) M)? uPredResidual : M];
  401. #ifdef DEBUG_VERBOSE
  402. printf("%i \t %i \t [%i]\n", vm*dimFloatArray+i, predResidual, m_neighbors[bestPred].m_pred[i]);
  403. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i \t [%i]\n", vm*dimFloatArray+i, predResidual, m_neighbors[bestPred].m_pred[i]);
  404. #endif //DEBUG_VERBOSE
  405. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  406. {
  407. bstream.WriteIntASCII(predResidual);
  408. }
  409. else
  410. {
  411. EncodeIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  412. }
  413. }
  414. ++m_freqPreds[bestPred];
  415. nSymbols += dimFloatArray;
  416. ++nPredictors;
  417. }
  418. else if ( vm > 0 && predMode != O3DGC_SC3DMC_NO_PREDICTION)
  419. {
  420. long prev = invVMap[vm-1];
  421. for (unsigned long i = 0; i < dimFloatArray; i++)
  422. {
  423. predResidual = m_quantFloatArray[v*stride+i] - m_quantFloatArray[prev*stride+i];
  424. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  425. {
  426. bstream.WriteIntASCII(predResidual);
  427. }
  428. else
  429. {
  430. EncodeIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  431. }
  432. #ifdef DEBUG_VERBOSE
  433. printf("%i \t %i\n", vm*dimFloatArray+i, predResidual);
  434. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i\n", vm*dimFloatArray+i, predResidual);
  435. #endif //DEBUG_VERBOSE
  436. }
  437. }
  438. else
  439. {
  440. for (unsigned long i = 0; i < dimFloatArray; i++)
  441. {
  442. predResidual = m_quantFloatArray[v*stride+i];
  443. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  444. {
  445. bstream.WriteUIntASCII(predResidual);
  446. }
  447. else
  448. {
  449. EncodeUIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  450. }
  451. #ifdef DEBUG_VERBOSE
  452. printf("%i \t %i\n", vm*dimFloatArray+i, predResidual);
  453. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i\n", vm*dimFloatArray+i, predResidual);
  454. #endif //DEBUG_VERBOSE
  455. }
  456. }
  457. }
  458. if (m_streamType != O3DGC_STREAM_TYPE_ASCII)
  459. {
  460. unsigned long encodedBytes = ace.stop_encoder();
  461. for(unsigned long i = 0; i < encodedBytes; ++i)
  462. {
  463. bstream.WriteUChar8Bin(m_bufferAC[i]);
  464. }
  465. }
  466. bstream.WriteUInt32(start, bstream.GetSize() - start, m_streamType);
  467. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  468. {
  469. unsigned long start = bstream.GetSize();
  470. bstream.WriteUInt32ASCII(0);
  471. const unsigned long size = m_predictors.GetSize();
  472. for(unsigned long i = 0; i < size; ++i)
  473. {
  474. bstream.WriteUCharASCII((unsigned char) m_predictors[i]);
  475. }
  476. bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
  477. }
  478. #ifdef DEBUG_VERBOSE
  479. fflush(g_fileDebugSC3DMCEnc);
  480. #endif //DEBUG_VERBOSE
  481. return O3DGC_OK;
  482. }
  483. template <class T>
  484. O3DGCErrorCode SC3DMCEncoder<T>::EncodeIntArray(const long * const intArray,
  485. unsigned long numIntArray,
  486. unsigned long dimIntArray,
  487. unsigned long stride,
  488. const IndexedFaceSet<T> & ifs,
  489. O3DGCSC3DMCPredictionMode predMode,
  490. BinaryStream & bstream)
  491. {
  492. assert(dimIntArray < O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  493. long predResidual, v, uPredResidual;
  494. unsigned long nPred;
  495. Arithmetic_Codec ace;
  496. Static_Bit_Model bModel0;
  497. Adaptive_Bit_Model bModel1;
  498. const AdjacencyInfo & v2T = m_triangleListEncoder.GetVertexToTriangle();
  499. const long * const vmap = m_triangleListEncoder.GetVMap();
  500. const long * const invVMap = m_triangleListEncoder.GetInvVMap();
  501. const T * const triangles = ifs.GetCoordIndex();
  502. const long nvert = (long) numIntArray;
  503. unsigned long start = bstream.GetSize();
  504. unsigned char mask = predMode & 7;
  505. const unsigned long M = O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS - 1;
  506. unsigned long nSymbols = O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS;
  507. unsigned long nPredictors = O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS;
  508. Adaptive_Data_Model mModelValues(M+2);
  509. Adaptive_Data_Model mModelPreds(O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS+1);
  510. memset(m_freqSymbols, 0, sizeof(unsigned long) * O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS);
  511. memset(m_freqPreds , 0, sizeof(unsigned long) * O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS);
  512. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  513. {
  514. mask += (O3DGC_SC3DMC_BINARIZATION_ASCII & 7)<<4;
  515. m_predictors.Allocate(nvert);
  516. m_predictors.Clear();
  517. }
  518. else
  519. {
  520. mask += (O3DGC_SC3DMC_BINARIZATION_AC_EGC & 7)<<4;
  521. const unsigned int NMAX = numIntArray * dimIntArray * 8 + 100;
  522. if ( m_sizeBufferAC < NMAX )
  523. {
  524. delete [] m_bufferAC;
  525. m_sizeBufferAC = NMAX;
  526. m_bufferAC = new unsigned char [m_sizeBufferAC];
  527. }
  528. ace.set_buffer(NMAX, m_bufferAC);
  529. ace.start_encoder();
  530. ace.ExpGolombEncode(0, 0, bModel0, bModel1);
  531. ace.ExpGolombEncode(M, 0, bModel0, bModel1);
  532. }
  533. bstream.WriteUInt32(0, m_streamType);
  534. bstream.WriteUChar(mask, m_streamType);
  535. #ifdef DEBUG_VERBOSE
  536. printf("IntArray (%i, %i)\n", numIntArray, dimIntArray);
  537. fprintf(g_fileDebugSC3DMCEnc, "IntArray (%i, %i)\n", numIntArray, dimIntArray);
  538. #endif //DEBUG_VERBOSE
  539. for (long vm=0; vm < nvert; ++vm)
  540. {
  541. nPred = 0;
  542. v = invVMap[vm];
  543. assert( v >= 0 && v < nvert);
  544. if ( v2T.GetNumNeighbors(v) > 0 &&
  545. predMode != O3DGC_SC3DMC_NO_PREDICTION)
  546. {
  547. int u0 = v2T.Begin(v);
  548. int u1 = v2T.End(v);
  549. for (long u = u0; u < u1; u++)
  550. {
  551. long ta = v2T.GetNeighbor(u);
  552. for(long k = 0; k < 3; ++k)
  553. {
  554. long w = triangles[ta*3 + k];
  555. if ( vmap[w] < vm )
  556. {
  557. SC3DMCTriplet id = {-1, -1, vmap[w]};
  558. unsigned long p = Insert(id, nPred, m_neighbors);
  559. if (p != 0xFFFFFFFF)
  560. {
  561. for (unsigned long i = 0; i < dimIntArray; i++)
  562. {
  563. m_neighbors[p].m_pred[i] = intArray[w*stride+i];
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. if (nPred > 1)
  571. {
  572. // find best predictor
  573. unsigned long bestPred = 0xFFFFFFFF;
  574. double bestCost = O3DGC_MAX_DOUBLE;
  575. double cost;
  576. #ifdef DEBUG_VERBOSE1
  577. printf("\t\t vm %i\n", vm);
  578. fprintf(g_fileDebugSC3DMCEnc, "\t\t vm %i\n", vm);
  579. #endif //DEBUG_VERBOSE
  580. for (unsigned long p = 0; p < nPred; ++p)
  581. {
  582. #ifdef DEBUG_VERBOSE1
  583. printf("\t\t pred a = %i b = %i c = %i \n", m_neighbors[p].m_id.m_a, m_neighbors[p].m_id.m_b, m_neighbors[p].m_id.m_c);
  584. fprintf(g_fileDebugSC3DMCEnc, "\t\t pred a = %i b = %i c = %i \n", m_neighbors[p].m_id.m_a, m_neighbors[p].m_id.m_b, m_neighbors[p].m_id.m_c);
  585. #endif //DEBUG_VERBOSE
  586. cost = -log2((m_freqPreds[p]+1.0) / nPredictors );
  587. for (unsigned long i = 0; i < dimIntArray; ++i)
  588. {
  589. #ifdef DEBUG_VERBOSE1
  590. printf("\t\t\t %i\n", m_neighbors[p].m_pred[i]);
  591. fprintf(g_fileDebugSC3DMCEnc, "\t\t\t %i\n", m_neighbors[p].m_pred[i]);
  592. #endif //DEBUG_VERBOSE
  593. predResidual = (long) IntToUInt(intArray[v*stride+i] - m_neighbors[p].m_pred[i]);
  594. if (predResidual < (long) M)
  595. {
  596. cost += -log2((m_freqSymbols[predResidual]+1.0) / nSymbols );
  597. }
  598. else
  599. {
  600. cost += -log2((m_freqSymbols[M] + 1.0) / nSymbols ) + log2((double) (predResidual-M));
  601. }
  602. }
  603. if (cost < bestCost)
  604. {
  605. bestCost = cost;
  606. bestPred = p;
  607. }
  608. }
  609. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  610. {
  611. m_predictors.PushBack((unsigned char) bestPred);
  612. }
  613. else
  614. {
  615. ace.encode(bestPred, mModelPreds);
  616. }
  617. #ifdef DEBUG_VERBOSE1
  618. printf("best (%i, %i, %i) \t pos %i\n", m_neighbors[bestPred].m_id.m_a, m_neighbors[bestPred].m_id.m_b, m_neighbors[bestPred].m_id.m_c, bestPred);
  619. fprintf(g_fileDebugSC3DMCEnc, "best (%i, %i, %i) \t pos %i\n", m_neighbors[bestPred].m_id.m_a, m_neighbors[bestPred].m_id.m_b, m_neighbors[bestPred].m_id.m_c, bestPred);
  620. #endif //DEBUG_VERBOSE
  621. // use best predictor
  622. for (unsigned long i = 0; i < dimIntArray; ++i)
  623. {
  624. predResidual = intArray[v*stride+i] - m_neighbors[bestPred].m_pred[i];
  625. uPredResidual = IntToUInt(predResidual);
  626. ++m_freqSymbols[(uPredResidual < (long) M)? uPredResidual : M];
  627. #ifdef DEBUG_VERBOSE
  628. printf("%i \t %i \t [%i]\n", vm*dimIntArray+i, predResidual, m_neighbors[bestPred].m_pred[i]);
  629. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i \t [%i]\n", vm*dimIntArray+i, predResidual, m_neighbors[bestPred].m_pred[i]);
  630. #endif //DEBUG_VERBOSE
  631. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  632. {
  633. bstream.WriteIntASCII(predResidual);
  634. }
  635. else
  636. {
  637. EncodeIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  638. }
  639. }
  640. ++m_freqPreds[bestPred];
  641. nSymbols += dimIntArray;
  642. ++nPredictors;
  643. }
  644. else if ( vm > 0 && predMode != O3DGC_SC3DMC_NO_PREDICTION)
  645. {
  646. long prev = invVMap[vm-1];
  647. for (unsigned long i = 0; i < dimIntArray; i++)
  648. {
  649. predResidual = intArray[v*stride+i] - intArray[prev*stride+i];
  650. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  651. {
  652. bstream.WriteIntASCII(predResidual);
  653. }
  654. else
  655. {
  656. EncodeIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  657. }
  658. #ifdef DEBUG_VERBOSE
  659. printf("%i \t %i\n", vm*dimIntArray+i, predResidual);
  660. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i\n", vm*dimIntArray+i, predResidual);
  661. #endif //DEBUG_VERBOSE
  662. }
  663. }
  664. else
  665. {
  666. for (unsigned long i = 0; i < dimIntArray; i++)
  667. {
  668. predResidual = intArray[v*stride+i];
  669. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  670. {
  671. bstream.WriteUIntASCII(predResidual);
  672. }
  673. else
  674. {
  675. EncodeUIntACEGC(predResidual, ace, mModelValues, bModel0, bModel1, M);
  676. }
  677. #ifdef DEBUG_VERBOSE
  678. printf("%i \t %i\n", vm*dimIntArray+i, predResidual);
  679. fprintf(g_fileDebugSC3DMCEnc, "%i \t %i\n", vm*dimIntArray+i, predResidual);
  680. #endif //DEBUG_VERBOSE
  681. }
  682. }
  683. }
  684. if (m_streamType != O3DGC_STREAM_TYPE_ASCII)
  685. {
  686. unsigned long encodedBytes = ace.stop_encoder();
  687. for(unsigned long i = 0; i < encodedBytes; ++i)
  688. {
  689. bstream.WriteUChar8Bin(m_bufferAC[i]);
  690. }
  691. }
  692. bstream.WriteUInt32(start, bstream.GetSize() - start, m_streamType);
  693. if (m_streamType == O3DGC_STREAM_TYPE_ASCII)
  694. {
  695. unsigned long start = bstream.GetSize();
  696. bstream.WriteUInt32ASCII(0);
  697. const unsigned long size = m_predictors.GetSize();
  698. for(unsigned long i = 0; i < size; ++i)
  699. {
  700. bstream.WriteUCharASCII((unsigned char) m_predictors[i]);
  701. }
  702. bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
  703. }
  704. #ifdef DEBUG_VERBOSE
  705. fflush(g_fileDebugSC3DMCEnc);
  706. #endif //DEBUG_VERBOSE
  707. return O3DGC_OK;
  708. }
  709. template <class T>
  710. O3DGCErrorCode SC3DMCEncoder<T>::ProcessNormals(const IndexedFaceSet<T> & ifs)
  711. {
  712. const long nvert = (long) ifs.GetNNormal();
  713. const unsigned long normalSize = ifs.GetNNormal() * 2;
  714. if (m_normalsSize < normalSize)
  715. {
  716. delete [] m_normals;
  717. m_normalsSize = normalSize;
  718. m_normals = new Real [normalSize];
  719. }
  720. const AdjacencyInfo & v2T = m_triangleListEncoder.GetVertexToTriangle();
  721. const long * const invVMap = m_triangleListEncoder.GetInvVMap();
  722. const T * const triangles = ifs.GetCoordIndex();
  723. const Real * const originalNormals = ifs.GetNormal();
  724. Vec3<long> p1, p2, p3, n0, nt;
  725. Vec3<Real> n1;
  726. long na0 = 0, nb0 = 0;
  727. Real rna0, rnb0, na1 = 0, nb1 = 0, norm0, norm1;
  728. char ni0 = 0, ni1 = 0;
  729. long a, b, c, v;
  730. m_predictors.Clear();
  731. for (long i=0; i < nvert; ++i)
  732. {
  733. v = invVMap[i];
  734. n0.X() = 0;
  735. n0.Y() = 0;
  736. n0.Z() = 0;
  737. int u0 = v2T.Begin(v);
  738. int u1 = v2T.End(v);
  739. for (long u = u0; u < u1; u++)
  740. {
  741. long ta = v2T.GetNeighbor(u);
  742. a = triangles[ta*3 + 0];
  743. b = triangles[ta*3 + 1];
  744. c = triangles[ta*3 + 2];
  745. p1.X() = m_quantFloatArray[3*a];
  746. p1.Y() = m_quantFloatArray[3*a+1];
  747. p1.Z() = m_quantFloatArray[3*a+2];
  748. p2.X() = m_quantFloatArray[3*b];
  749. p2.Y() = m_quantFloatArray[3*b+1];
  750. p2.Z() = m_quantFloatArray[3*b+2];
  751. p3.X() = m_quantFloatArray[3*c];
  752. p3.Y() = m_quantFloatArray[3*c+1];
  753. p3.Z() = m_quantFloatArray[3*c+2];
  754. nt = (p2-p1)^(p3-p1);
  755. n0 += nt;
  756. }
  757. norm0 = (Real) n0.GetNorm();
  758. if (norm0 == 0.0)
  759. {
  760. norm0 = 1.0;
  761. }
  762. SphereToCube(n0.X(), n0.Y(), n0.Z(), na0, nb0, ni0);
  763. rna0 = na0 / norm0;
  764. rnb0 = nb0 / norm0;
  765. n1.X() = originalNormals[3*v];
  766. n1.Y() = originalNormals[3*v+1];
  767. n1.Z() = originalNormals[3*v+2];
  768. norm1 = (Real) n1.GetNorm();
  769. if (norm1 != 0.0)
  770. {
  771. n1.X() /= norm1;
  772. n1.Y() /= norm1;
  773. n1.Z() /= norm1;
  774. }
  775. SphereToCube(n1.X(), n1.Y(), n1.Z(), na1, nb1, ni1);
  776. m_predictors.PushBack(ni1 - ni0);
  777. if ( (ni1 >> 1) != (ni0 >> 1) )
  778. {
  779. rna0 = (Real)0.0;
  780. rnb0 = (Real)0.0;
  781. }
  782. m_normals[2*v] = na1 - rna0;
  783. m_normals[2*v+1] = nb1 - rnb0;
  784. #ifdef DEBUG_VERBOSE1
  785. printf("n0 \t %i \t %i \t %i \t %i (%f, %f)\n", i, n0.X(), n0.Y(), n0.Z(), rna0, rnb0);
  786. fprintf(g_fileDebugSC3DMCEnc,"n0 \t %i \t %i \t %i \t %i (%f, %f)\n", i, n0.X(), n0.Y(), n0.Z(), rna0, rnb0);
  787. #endif //DEBUG_VERBOSE
  788. #ifdef DEBUG_VERBOSE1
  789. printf("normal \t %i \t %f \t %f \t %f \t (%i, %f, %f) \t (%f, %f)\n", i, n1.X(), n1.Y(), n1.Z(), ni1, na1, nb1, rna0, rnb0);
  790. fprintf(g_fileDebugSC3DMCEnc, "normal \t %i \t %f \t %f \t %f \t (%i, %f, %f) \t (%f, %f)\n", i, n1.X(), n1.Y(), n1.Z(), ni1, na1, nb1, rna0, rnb0);
  791. #endif //DEBUG_VERBOSE
  792. }
  793. return O3DGC_OK;
  794. }
  795. template <class T>
  796. O3DGCErrorCode SC3DMCEncoder<T>::EncodePayload(const SC3DMCEncodeParams & params,
  797. const IndexedFaceSet<T> & ifs,
  798. BinaryStream & bstream)
  799. {
  800. #ifdef DEBUG_VERBOSE
  801. g_fileDebugSC3DMCEnc = fopen("tfans_enc_main.txt", "w");
  802. #endif //DEBUG_VERBOSE
  803. // encode triangle list
  804. m_triangleListEncoder.SetStreamType(params.GetStreamType());
  805. m_stats.m_streamSizeCoordIndex = bstream.GetSize();
  806. Timer timer;
  807. timer.Tic();
  808. m_triangleListEncoder.Encode(ifs.GetCoordIndex(), ifs.GetIndexBufferID(), ifs.GetNCoordIndex(), ifs.GetNCoord(), bstream);
  809. timer.Toc();
  810. m_stats.m_timeCoordIndex = timer.GetElapsedTime();
  811. m_stats.m_streamSizeCoordIndex = bstream.GetSize() - m_stats.m_streamSizeCoordIndex;
  812. // encode coord
  813. m_stats.m_streamSizeCoord = bstream.GetSize();
  814. timer.Tic();
  815. if (ifs.GetNCoord() > 0)
  816. {
  817. EncodeFloatArray(ifs.GetCoord(), ifs.GetNCoord(), 3, 3, ifs.GetCoordMin(), ifs.GetCoordMax(),
  818. params.GetCoordQuantBits(), ifs, params.GetCoordPredMode(), bstream);
  819. }
  820. timer.Toc();
  821. m_stats.m_timeCoord = timer.GetElapsedTime();
  822. m_stats.m_streamSizeCoord = bstream.GetSize() - m_stats.m_streamSizeCoord;
  823. // encode Normal
  824. m_stats.m_streamSizeNormal = bstream.GetSize();
  825. timer.Tic();
  826. if (ifs.GetNNormal() > 0)
  827. {
  828. if (params.GetNormalPredMode() == O3DGC_SC3DMC_SURF_NORMALS_PREDICTION)
  829. {
  830. ProcessNormals(ifs);
  831. EncodeFloatArray(m_normals, ifs.GetNNormal(), 2, 2, ifs.GetNormalMin(), ifs.GetNormalMax(),
  832. params.GetNormalQuantBits(), ifs, params.GetNormalPredMode(), bstream);
  833. }
  834. else
  835. {
  836. EncodeFloatArray(ifs.GetNormal(), ifs.GetNNormal(), 3, 3, ifs.GetNormalMin(), ifs.GetNormalMax(),
  837. params.GetNormalQuantBits(), ifs, params.GetNormalPredMode(), bstream);
  838. }
  839. }
  840. timer.Toc();
  841. m_stats.m_timeNormal = timer.GetElapsedTime();
  842. m_stats.m_streamSizeNormal = bstream.GetSize() - m_stats.m_streamSizeNormal;
  843. // encode FloatAttribute
  844. for(unsigned long a = 0; a < ifs.GetNumFloatAttributes(); ++a)
  845. {
  846. m_stats.m_streamSizeFloatAttribute[a] = bstream.GetSize();
  847. timer.Tic();
  848. EncodeFloatArray(ifs.GetFloatAttribute(a), ifs.GetNFloatAttribute(a),
  849. ifs.GetFloatAttributeDim(a), ifs.GetFloatAttributeDim(a),
  850. ifs.GetFloatAttributeMin(a), ifs.GetFloatAttributeMax(a),
  851. params.GetFloatAttributeQuantBits(a), ifs,
  852. params.GetFloatAttributePredMode(a), bstream);
  853. timer.Toc();
  854. m_stats.m_timeFloatAttribute[a] = timer.GetElapsedTime();
  855. m_stats.m_streamSizeFloatAttribute[a] = bstream.GetSize() - m_stats.m_streamSizeFloatAttribute[a];
  856. }
  857. // encode IntAttribute
  858. for(unsigned long a = 0; a < ifs.GetNumIntAttributes(); ++a)
  859. {
  860. m_stats.m_streamSizeIntAttribute[a] = bstream.GetSize();
  861. timer.Tic();
  862. EncodeIntArray(ifs.GetIntAttribute(a), ifs.GetNIntAttribute(a), ifs.GetIntAttributeDim(a),
  863. ifs.GetIntAttributeDim(a), ifs, params.GetIntAttributePredMode(a), bstream);
  864. timer.Toc();
  865. m_stats.m_timeIntAttribute[a] = timer.GetElapsedTime();
  866. m_stats.m_streamSizeIntAttribute[a] = bstream.GetSize() - m_stats.m_streamSizeIntAttribute[a];
  867. }
  868. #ifdef DEBUG_VERBOSE
  869. fclose(g_fileDebugSC3DMCEnc);
  870. #endif //DEBUG_VERBOSE
  871. return O3DGC_OK;
  872. }
  873. }
  874. #endif // O3DGC_SC3DMC_ENCODER_INL