o3dgcTriangleFans.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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_FANS_H
  21. #define O3DGC_TRIANGLE_FANS_H
  22. #include "o3dgcCommon.h"
  23. #include "o3dgcVector.h"
  24. #include "o3dgcBinaryStream.h"
  25. namespace o3dgc
  26. {
  27. const long O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER = 128;
  28. const long O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER = 8;
  29. class CompressedTriangleFans
  30. {
  31. public:
  32. //! Constructor.
  33. CompressedTriangleFans(void)
  34. {
  35. m_streamType = O3DGC_STREAM_TYPE_UNKOWN;
  36. m_bufferAC = 0;
  37. m_sizeBufferAC = 0;
  38. };
  39. //! Destructor.
  40. ~CompressedTriangleFans(void)
  41. {
  42. delete [] m_bufferAC;
  43. };
  44. O3DGCStreamType GetStreamType() const { return m_streamType; }
  45. void SetStreamType(O3DGCStreamType streamType) { m_streamType = streamType; }
  46. O3DGCErrorCode Allocate(long numVertices, long numTriangles)
  47. {
  48. assert(numVertices > 0);
  49. m_numTFANs.Allocate(numVertices);
  50. m_degrees.Allocate(2*numVertices);
  51. m_configs.Allocate(2*numVertices);
  52. m_operations.Allocate(2*numVertices);
  53. m_indices.Allocate(2*numVertices);
  54. m_trianglesOrder.Allocate(numTriangles);
  55. Clear();
  56. return O3DGC_OK;
  57. }
  58. O3DGCErrorCode PushNumTFans(long numTFans)
  59. {
  60. m_numTFANs.PushBack(numTFans);
  61. return O3DGC_OK;
  62. }
  63. long ReadNumTFans(unsigned long & iterator) const
  64. {
  65. assert(iterator < m_numTFANs.GetSize());
  66. return m_numTFANs[iterator++];
  67. }
  68. O3DGCErrorCode PushDegree(long degree)
  69. {
  70. m_degrees.PushBack(degree);
  71. return O3DGC_OK;
  72. }
  73. long ReadDegree(unsigned long & iterator) const
  74. {
  75. assert(iterator < m_degrees.GetSize());
  76. return m_degrees[iterator++];
  77. }
  78. O3DGCErrorCode PushConfig(long config)
  79. {
  80. m_configs.PushBack(config);
  81. return O3DGC_OK;
  82. }
  83. long ReadConfig(unsigned long & iterator) const
  84. {
  85. assert(iterator < m_configs.GetSize());
  86. return m_configs[iterator++];
  87. }
  88. O3DGCErrorCode PushOperation(long op)
  89. {
  90. m_operations.PushBack(op);
  91. return O3DGC_OK;
  92. }
  93. long ReadOperation(unsigned long & iterator) const
  94. {
  95. assert(iterator < m_operations.GetSize());
  96. return m_operations[iterator++];
  97. }
  98. O3DGCErrorCode PushIndex(long index)
  99. {
  100. m_indices.PushBack(index);
  101. return O3DGC_OK;
  102. }
  103. long ReadIndex(unsigned long & iterator) const
  104. {
  105. assert(iterator < m_indices.GetSize());
  106. return m_indices[iterator++];
  107. }
  108. O3DGCErrorCode PushTriangleIndex(long index)
  109. {
  110. m_trianglesOrder.PushBack(IntToUInt(index));
  111. return O3DGC_OK;
  112. }
  113. long ReadTriangleIndex(unsigned long & iterator) const
  114. {
  115. assert(iterator < m_trianglesOrder.GetSize());
  116. return UIntToInt(m_trianglesOrder[iterator++]);
  117. }
  118. O3DGCErrorCode Clear()
  119. {
  120. m_numTFANs.Clear();
  121. m_degrees.Clear();
  122. m_configs.Clear();
  123. m_operations.Clear();
  124. m_indices.Clear();
  125. return O3DGC_OK;
  126. }
  127. O3DGCErrorCode Save(BinaryStream & bstream,
  128. bool encodeTrianglesOrder,
  129. O3DGCStreamType streamType);
  130. O3DGCErrorCode Load(const BinaryStream & bstream,
  131. unsigned long & iterator,
  132. bool decodeTrianglesOrder,
  133. O3DGCStreamType streamType);
  134. private:
  135. O3DGCErrorCode SaveBinAC(const Vector<long> & data,
  136. BinaryStream & bstream);
  137. O3DGCErrorCode SaveUIntAC(const Vector<long> & data,
  138. const unsigned long M,
  139. BinaryStream & bstream);
  140. O3DGCErrorCode SaveIntACEGC(const Vector<long> & data,
  141. const unsigned long M,
  142. BinaryStream & bstream);
  143. Vector<long> m_numTFANs;
  144. Vector<long> m_degrees;
  145. Vector<long> m_configs;
  146. Vector<long> m_operations;
  147. Vector<long> m_indices;
  148. Vector<long> m_trianglesOrder;
  149. unsigned char * m_bufferAC;
  150. unsigned long m_sizeBufferAC;
  151. O3DGCStreamType m_streamType;
  152. };
  153. //!
  154. class TriangleFans
  155. {
  156. public:
  157. //! Constructor.
  158. TriangleFans(long sizeTFAN = O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER,
  159. long verticesSize = O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER)
  160. {
  161. assert(sizeTFAN > 0);
  162. assert(verticesSize > 0);
  163. m_numTFANs = 0;
  164. m_numVertices = 0;
  165. m_verticesAllocatedSize = verticesSize;
  166. m_sizeTFANAllocatedSize = sizeTFAN;
  167. m_sizeTFAN = new long [m_sizeTFANAllocatedSize];
  168. m_vertices = new long [m_verticesAllocatedSize];
  169. };
  170. //! Destructor.
  171. ~TriangleFans(void)
  172. {
  173. delete [] m_vertices;
  174. delete [] m_sizeTFAN;
  175. };
  176. O3DGCErrorCode Allocate(long sizeTFAN, long verticesSize)
  177. {
  178. assert(sizeTFAN > 0);
  179. assert(verticesSize > 0);
  180. m_numTFANs = 0;
  181. m_numVertices = 0;
  182. if (m_verticesAllocatedSize < verticesSize)
  183. {
  184. delete [] m_vertices;
  185. m_verticesAllocatedSize = verticesSize;
  186. m_vertices = new long [m_verticesAllocatedSize];
  187. }
  188. if (m_sizeTFANAllocatedSize < sizeTFAN)
  189. {
  190. delete [] m_sizeTFAN;
  191. m_sizeTFANAllocatedSize = sizeTFAN;
  192. m_sizeTFAN = new long [m_sizeTFANAllocatedSize];
  193. }
  194. return O3DGC_OK;
  195. };
  196. O3DGCErrorCode Clear()
  197. {
  198. m_numTFANs = 0;
  199. m_numVertices = 0;
  200. return O3DGC_OK;
  201. }
  202. O3DGCErrorCode AddVertex(long vertex)
  203. {
  204. assert(m_numTFANs >= 0);
  205. assert(m_numTFANs < m_sizeTFANAllocatedSize);
  206. assert(m_numVertices >= 0);
  207. ++m_numVertices;
  208. if (m_numVertices == m_verticesAllocatedSize)
  209. {
  210. m_verticesAllocatedSize *= 2;
  211. long * tmp = m_vertices;
  212. m_vertices = new long [m_verticesAllocatedSize];
  213. memcpy(m_vertices, tmp, sizeof(long) * m_numVertices);
  214. delete [] tmp;
  215. }
  216. m_vertices[m_numVertices-1] = vertex;
  217. ++m_sizeTFAN[m_numTFANs-1];
  218. return O3DGC_OK;
  219. }
  220. O3DGCErrorCode AddTFAN()
  221. {
  222. assert(m_numTFANs >= 0);
  223. ++m_numTFANs;
  224. if (m_numTFANs == m_sizeTFANAllocatedSize)
  225. {
  226. m_sizeTFANAllocatedSize *= 2;
  227. long * tmp = m_sizeTFAN;
  228. m_sizeTFAN = new long [m_sizeTFANAllocatedSize];
  229. memcpy(m_sizeTFAN, tmp, sizeof(long) * m_numTFANs);
  230. delete [] tmp;
  231. }
  232. m_sizeTFAN[m_numTFANs-1] = (m_numTFANs > 1) ? m_sizeTFAN[m_numTFANs-2] : 0;
  233. return O3DGC_OK;
  234. }
  235. long Begin(long tfan) const
  236. {
  237. assert(tfan < m_numTFANs);
  238. assert(tfan >= 0);
  239. return (tfan>0)?m_sizeTFAN[tfan-1]:0;
  240. }
  241. long End(long tfan) const
  242. {
  243. assert(tfan < m_numTFANs);
  244. assert(tfan >= 0);
  245. return m_sizeTFAN[tfan];
  246. }
  247. long GetVertex(long vertex) const
  248. {
  249. assert(vertex < m_numVertices);
  250. assert(vertex >= 0);
  251. return m_vertices[vertex];
  252. }
  253. long GetTFANSize(long tfan) const
  254. {
  255. return End(tfan) - Begin(tfan);
  256. }
  257. long GetNumTFANs() const
  258. {
  259. return m_numTFANs;
  260. }
  261. long GetNumVertices() const
  262. {
  263. return m_numVertices;
  264. }
  265. private:
  266. long m_verticesAllocatedSize;
  267. long m_sizeTFANAllocatedSize;
  268. long m_numTFANs;
  269. long m_numVertices;
  270. long * m_vertices;
  271. long * m_sizeTFAN;
  272. };
  273. }
  274. #endif // O3DGC_TRIANGLE_FANS_H