o3dgcTriangleListDecoder.inl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_TRIANGLE_LIST_DECODER_INL
  21. #define O3DGC_TRIANGLE_LIST_DECODER_INL
  22. namespace o3dgc
  23. {
  24. template<class T>
  25. O3DGCErrorCode TriangleListDecoder<T>::Init(T * const triangles,
  26. const long numTriangles,
  27. const long numVertices,
  28. const long maxSizeV2T)
  29. {
  30. assert(numVertices > 0);
  31. assert(numTriangles > 0);
  32. m_numTriangles = numTriangles;
  33. m_numVertices = numVertices;
  34. m_triangles = triangles;
  35. m_vertexCount = 0;
  36. m_triangleCount = 0;
  37. m_itNumTFans = 0;
  38. m_itDegree = 0;
  39. m_itConfig = 0;
  40. m_itOperation = 0;
  41. m_itIndex = 0;
  42. if (m_numVertices > m_maxNumVertices)
  43. {
  44. m_maxNumVertices = m_numVertices;
  45. delete [] m_visitedVerticesValence;
  46. delete [] m_visitedVertices;
  47. m_visitedVerticesValence = new long [m_numVertices];
  48. m_visitedVertices = new long [m_numVertices];
  49. }
  50. if (m_decodeTrianglesOrder && m_tempTrianglesSize < m_numTriangles)
  51. {
  52. delete [] m_tempTriangles;
  53. m_tempTrianglesSize = m_numTriangles;
  54. m_tempTriangles = new T [3*m_tempTrianglesSize];
  55. }
  56. m_ctfans.SetStreamType(m_streamType);
  57. m_ctfans.Allocate(m_numVertices, m_numTriangles);
  58. m_tfans.Allocate(2 * m_numVertices, 8 * m_numVertices);
  59. // compute vertex-to-triangle adjacency information
  60. m_vertexToTriangle.AllocateNumNeighborsArray(numVertices);
  61. long * numNeighbors = m_vertexToTriangle.GetNumNeighborsBuffer();
  62. for(long i = 0; i < numVertices; ++i)
  63. {
  64. numNeighbors[i] = maxSizeV2T;
  65. }
  66. m_vertexToTriangle.AllocateNeighborsArray();
  67. m_vertexToTriangle.ClearNeighborsArray();
  68. return O3DGC_OK;
  69. }
  70. template<class T>
  71. O3DGCErrorCode TriangleListDecoder<T>::Decompress()
  72. {
  73. for(long focusVertex = 0; focusVertex < m_numVertices; ++focusVertex)
  74. {
  75. if (focusVertex == m_vertexCount)
  76. {
  77. m_vertexCount++; // insert focusVertex
  78. }
  79. CompueLocalConnectivityInfo(focusVertex);
  80. DecompressTFAN(focusVertex);
  81. }
  82. return O3DGC_OK;
  83. }
  84. template<class T>
  85. O3DGCErrorCode TriangleListDecoder<T>::Reorder()
  86. {
  87. if (m_decodeTrianglesOrder)
  88. {
  89. unsigned long itTriangleIndex = 0;
  90. long prevTriangleIndex = 0;
  91. long t;
  92. memcpy(m_tempTriangles, m_triangles, m_numTriangles * 3 * sizeof(T));
  93. for(long i = 0; i < m_numTriangles; ++i)
  94. {
  95. t = m_ctfans.ReadTriangleIndex(itTriangleIndex) + prevTriangleIndex;
  96. assert( t >= 0 && t < m_numTriangles);
  97. memcpy(m_triangles + 3 * t, m_tempTriangles + 3 * i, sizeof(T) * 3);
  98. prevTriangleIndex = t + 1;
  99. }
  100. }
  101. return O3DGC_OK;
  102. }
  103. template<class T>
  104. O3DGCErrorCode TriangleListDecoder<T>::CompueLocalConnectivityInfo(const long focusVertex)
  105. {
  106. long t = 0;
  107. long p, v;
  108. m_numConqueredTriangles = 0;
  109. m_numVisitedVertices = 0;
  110. for(long i = m_vertexToTriangle.Begin(focusVertex); (t >= 0) && (i < m_vertexToTriangle.End(focusVertex)); ++i)
  111. {
  112. t = m_vertexToTriangle.GetNeighbor(i);
  113. if ( t >= 0)
  114. {
  115. ++m_numConqueredTriangles;
  116. p = 3*t;
  117. // extract visited vertices
  118. for(long k = 0; k < 3; ++k)
  119. {
  120. v = m_triangles[p+k];
  121. if (v > focusVertex) // vertices are insertices by increasing traversal order
  122. {
  123. bool foundOrInserted = false;
  124. for (long j = 0; j < m_numVisitedVertices; ++j)
  125. {
  126. if (v == m_visitedVertices[j])
  127. {
  128. m_visitedVerticesValence[j]++;
  129. foundOrInserted = true;
  130. break;
  131. }
  132. else if (v < m_visitedVertices[j])
  133. {
  134. ++m_numVisitedVertices;
  135. for (long h = m_numVisitedVertices-1; h > j; --h)
  136. {
  137. m_visitedVertices[h] = m_visitedVertices[h-1];
  138. m_visitedVerticesValence[h] = m_visitedVerticesValence[h-1];
  139. }
  140. m_visitedVertices[j] = v;
  141. m_visitedVerticesValence[j] = 1;
  142. foundOrInserted = true;
  143. break;
  144. }
  145. }
  146. if (!foundOrInserted)
  147. {
  148. m_visitedVertices[m_numVisitedVertices] = v;
  149. m_visitedVerticesValence[m_numVisitedVertices] = 1;
  150. m_numVisitedVertices++;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. // re-order visited vertices by taking into account their valence (i.e., # of conquered triangles incident to each vertex)
  157. // in order to avoid config. 9
  158. if (m_numVisitedVertices > 2)
  159. {
  160. long y;
  161. for(long x = 1; x < m_numVisitedVertices; ++x)
  162. {
  163. if (m_visitedVerticesValence[x] == 1)
  164. {
  165. y = x;
  166. while( (y > 0) && (m_visitedVerticesValence[y] < m_visitedVerticesValence[y-1]) )
  167. {
  168. swap(m_visitedVerticesValence[y], m_visitedVerticesValence[y-1]);
  169. swap(m_visitedVertices[y], m_visitedVertices[y-1]);
  170. --y;
  171. }
  172. }
  173. }
  174. }
  175. return O3DGC_OK;
  176. }
  177. template<class T>
  178. O3DGCErrorCode TriangleListDecoder<T>::DecompressTFAN(const long focusVertex)
  179. {
  180. long ntfans;
  181. long degree, config;
  182. long op;
  183. long index;
  184. long k0, k1;
  185. long b, c, t;
  186. ntfans = m_ctfans.ReadNumTFans(m_itNumTFans);
  187. if (ntfans > 0)
  188. {
  189. for(long f = 0; f != ntfans; f++)
  190. {
  191. m_tfans.AddTFAN();
  192. degree = m_ctfans.ReadDegree(m_itDegree) +2 - m_numConqueredTriangles;
  193. config = m_ctfans.ReadConfig(m_itConfig);
  194. k0 = m_tfans.GetNumVertices();
  195. m_tfans.AddVertex(focusVertex);
  196. switch(config)
  197. {
  198. case 0:// ops: 1000001 vertices: -1 -2
  199. m_tfans.AddVertex(m_visitedVertices[0]);
  200. for(long u = 1; u < degree-1; u++)
  201. {
  202. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  203. m_tfans.AddVertex(m_vertexCount++);
  204. }
  205. m_tfans.AddVertex(m_visitedVertices[1]);
  206. break;
  207. case 1: // ops: 1xxxxxx1 vertices: -1 x x x x x -2
  208. m_tfans.AddVertex(m_visitedVertices[0]);
  209. for(long u = 1; u < degree-1; u++)
  210. {
  211. op = m_ctfans.ReadOperation(m_itOperation);
  212. if (op == 1)
  213. {
  214. index = m_ctfans.ReadIndex(m_itIndex);
  215. if ( index < 0)
  216. {
  217. m_tfans.AddVertex(m_visitedVertices[-index-1]);
  218. }
  219. else
  220. {
  221. m_tfans.AddVertex(index + focusVertex);
  222. }
  223. }
  224. else
  225. {
  226. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  227. m_tfans.AddVertex(m_vertexCount++);
  228. }
  229. }
  230. m_tfans.AddVertex(m_visitedVertices[1]);
  231. break;
  232. case 2: // ops: 00000001 vertices: -1
  233. for(long u = 0; u < degree-1; u++)
  234. {
  235. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  236. m_tfans.AddVertex(m_vertexCount++);
  237. }
  238. m_tfans.AddVertex(m_visitedVertices[0]);
  239. break;
  240. case 3: // ops: 00000001 vertices: -2
  241. for(long u=0; u < degree-1; u++)
  242. {
  243. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  244. m_tfans.AddVertex(m_vertexCount++);
  245. }
  246. m_tfans.AddVertex(m_visitedVertices[1]);
  247. break;
  248. case 4: // ops: 10000000 vertices: -1
  249. m_tfans.AddVertex(m_visitedVertices[0]);
  250. for(long u = 1; u < degree; u++)
  251. {
  252. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  253. m_tfans.AddVertex(m_vertexCount++);
  254. }
  255. break;
  256. case 5: // ops: 10000000 vertices: -2
  257. m_tfans.AddVertex(m_visitedVertices[1]);
  258. for(long u = 1; u < degree; u++)
  259. {
  260. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  261. m_tfans.AddVertex(m_vertexCount++);
  262. }
  263. break;
  264. case 6:// ops: 00000000 vertices:
  265. for(long u = 0; u < degree; u++)
  266. {
  267. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  268. m_tfans.AddVertex(m_vertexCount++);
  269. }
  270. break;
  271. case 7: // ops: 1000001 vertices: -2 -1
  272. m_tfans.AddVertex(m_visitedVertices[1]);
  273. for(long u = 1; u < degree-1; u++)
  274. {
  275. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  276. m_tfans.AddVertex(m_vertexCount++);
  277. }
  278. m_tfans.AddVertex(m_visitedVertices[0]);
  279. break;
  280. case 8: // ops: 1xxxxxx1 vertices: -2 x x x x x -1
  281. m_tfans.AddVertex(m_visitedVertices[1]);
  282. for(long u = 1; u < degree-1; u++)
  283. {
  284. op = m_ctfans.ReadOperation(m_itOperation);
  285. if (op == 1)
  286. {
  287. index = m_ctfans.ReadIndex(m_itIndex);
  288. if ( index < 0)
  289. {
  290. m_tfans.AddVertex(m_visitedVertices[-index-1]);
  291. }
  292. else
  293. {
  294. m_tfans.AddVertex(index + focusVertex);
  295. }
  296. }
  297. else
  298. {
  299. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  300. m_tfans.AddVertex(m_vertexCount++);
  301. }
  302. }
  303. m_tfans.AddVertex(m_visitedVertices[0]);
  304. break;
  305. case 9: // general case
  306. for(long u = 0; u < degree; u++)
  307. {
  308. op = m_ctfans.ReadOperation(m_itOperation);
  309. if (op == 1)
  310. {
  311. index = m_ctfans.ReadIndex(m_itIndex);
  312. if ( index < 0)
  313. {
  314. m_tfans.AddVertex(m_visitedVertices[-index-1]);
  315. }
  316. else
  317. {
  318. m_tfans.AddVertex(index + focusVertex);
  319. }
  320. }
  321. else
  322. {
  323. m_visitedVertices[m_numVisitedVertices++] = m_vertexCount;
  324. m_tfans.AddVertex(m_vertexCount++);
  325. }
  326. }
  327. break;
  328. }
  329. //logger.write_2_log("\t degree=%i \t cas = %i\n", degree, cas);
  330. k1 = m_tfans.GetNumVertices();
  331. b = m_tfans.GetVertex(k0+1);
  332. for (long k = k0+2; k < k1; k++)
  333. {
  334. c = m_tfans.GetVertex(k);
  335. t = m_triangleCount*3;
  336. m_triangles[t++] = (T) focusVertex;
  337. m_triangles[t++] = (T) b;
  338. m_triangles[t ] = (T) c;
  339. m_vertexToTriangle.AddNeighbor(focusVertex, m_triangleCount);
  340. m_vertexToTriangle.AddNeighbor(b , m_triangleCount);
  341. m_vertexToTriangle.AddNeighbor(c , m_triangleCount);
  342. b=c;
  343. m_triangleCount++;
  344. }
  345. }
  346. }
  347. return O3DGC_OK;
  348. }
  349. }
  350. #endif //O3DGC_TRIANGLE_LIST_DECODER_INL