o3dgcSC3DMCEncoder.inl 40 KB

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