/src/assimp/contrib/Open3DGC/o3dgcTriangleFans.h
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc. |
3 | | |
4 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | of this software and associated documentation files (the "Software"), to deal |
6 | | in the Software without restriction, including without limitation the rights |
7 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
8 | | copies of the Software, and to permit persons to whom the Software is |
9 | | furnished to do so, subject to the following conditions: |
10 | | |
11 | | The above copyright notice and this permission notice shall be included in |
12 | | all copies or substantial portions of the Software. |
13 | | |
14 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
19 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
20 | | THE SOFTWARE. |
21 | | */ |
22 | | |
23 | | |
24 | | #pragma once |
25 | | #ifndef O3DGC_TRIANGLE_FANS_H |
26 | | #define O3DGC_TRIANGLE_FANS_H |
27 | | |
28 | | #include "o3dgcCommon.h" |
29 | | #include "o3dgcVector.h" |
30 | | #include "o3dgcBinaryStream.h" |
31 | | |
32 | | |
33 | | namespace o3dgc |
34 | | { |
35 | | const long O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER = 128; |
36 | | const long O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER = 8; |
37 | | |
38 | | class CompressedTriangleFans |
39 | | { |
40 | | public: |
41 | | //! Constructor. |
42 | | CompressedTriangleFans(void) |
43 | 0 | { |
44 | 0 | m_streamType = O3DGC_STREAM_TYPE_UNKOWN; |
45 | 0 | m_bufferAC = 0; |
46 | 0 | m_sizeBufferAC = 0; |
47 | 0 | }; |
48 | | //! Destructor. |
49 | | ~CompressedTriangleFans(void) |
50 | 0 | { |
51 | 0 | delete [] m_bufferAC; |
52 | 0 | }; |
53 | 0 | O3DGCStreamType GetStreamType() const { return m_streamType; } |
54 | 0 | void SetStreamType(O3DGCStreamType streamType) { m_streamType = streamType; } |
55 | | |
56 | | O3DGCErrorCode Allocate(long numVertices, long numTriangles) |
57 | 0 | { |
58 | 0 | assert(numVertices > 0); |
59 | 0 | m_numTFANs.Allocate(numVertices); |
60 | 0 | m_degrees.Allocate(2*numVertices); |
61 | 0 | m_configs.Allocate(2*numVertices); |
62 | 0 | m_operations.Allocate(2*numVertices); |
63 | 0 | m_indices.Allocate(2*numVertices); |
64 | 0 | m_trianglesOrder.Allocate(numTriangles); |
65 | 0 | Clear(); |
66 | 0 | return O3DGC_OK; |
67 | 0 | } |
68 | | O3DGCErrorCode PushNumTFans(long numTFans) |
69 | 0 | { |
70 | 0 | m_numTFANs.PushBack(numTFans); |
71 | 0 | return O3DGC_OK; |
72 | 0 | } |
73 | | long ReadNumTFans(unsigned long & iterator) const |
74 | 0 | { |
75 | 0 | assert(iterator < m_numTFANs.GetSize()); |
76 | 0 | return m_numTFANs[iterator++]; |
77 | 0 | } |
78 | | O3DGCErrorCode PushDegree(long degree) |
79 | 0 | { |
80 | 0 | m_degrees.PushBack(degree); |
81 | 0 | return O3DGC_OK; |
82 | 0 | } |
83 | | long ReadDegree(unsigned long & iterator) const |
84 | 0 | { |
85 | 0 | assert(iterator < m_degrees.GetSize()); |
86 | 0 | return m_degrees[iterator++]; |
87 | 0 | } |
88 | | O3DGCErrorCode PushConfig(long config) |
89 | 0 | { |
90 | 0 | m_configs.PushBack(config); |
91 | 0 | return O3DGC_OK; |
92 | 0 | } |
93 | | long ReadConfig(unsigned long & iterator) const |
94 | 0 | { |
95 | 0 | assert(iterator < m_configs.GetSize()); |
96 | 0 | return m_configs[iterator++]; |
97 | 0 | } |
98 | | O3DGCErrorCode PushOperation(long op) |
99 | 0 | { |
100 | 0 | m_operations.PushBack(op); |
101 | 0 | return O3DGC_OK; |
102 | 0 | } |
103 | | long ReadOperation(unsigned long & iterator) const |
104 | 0 | { |
105 | 0 | assert(iterator < m_operations.GetSize()); |
106 | 0 | return m_operations[iterator++]; |
107 | 0 | } |
108 | | O3DGCErrorCode PushIndex(long index) |
109 | 0 | { |
110 | 0 | m_indices.PushBack(index); |
111 | 0 | return O3DGC_OK; |
112 | 0 | } |
113 | | long ReadIndex(unsigned long & iterator) const |
114 | 0 | { |
115 | 0 | assert(iterator < m_indices.GetSize()); |
116 | 0 | return m_indices[iterator++]; |
117 | 0 | } |
118 | | O3DGCErrorCode PushTriangleIndex(long index) |
119 | 0 | { |
120 | 0 | m_trianglesOrder.PushBack(IntToUInt(index)); |
121 | 0 | return O3DGC_OK; |
122 | 0 | } |
123 | | long ReadTriangleIndex(unsigned long & iterator) const |
124 | 0 | { |
125 | 0 | assert(iterator < m_trianglesOrder.GetSize()); |
126 | 0 | return UIntToInt(m_trianglesOrder[iterator++]); |
127 | 0 | } |
128 | | O3DGCErrorCode Clear() |
129 | 0 | { |
130 | 0 | m_numTFANs.Clear(); |
131 | 0 | m_degrees.Clear(); |
132 | 0 | m_configs.Clear(); |
133 | 0 | m_operations.Clear(); |
134 | 0 | m_indices.Clear(); |
135 | 0 | return O3DGC_OK; |
136 | 0 | } |
137 | | O3DGCErrorCode Save(BinaryStream & bstream, |
138 | | bool encodeTrianglesOrder, |
139 | | O3DGCStreamType streamType); |
140 | | O3DGCErrorCode Load(const BinaryStream & bstream, |
141 | | unsigned long & iterator, |
142 | | bool decodeTrianglesOrder, |
143 | | O3DGCStreamType streamType); |
144 | | |
145 | | private: |
146 | | O3DGCErrorCode SaveBinAC(const Vector<long> & data, |
147 | | BinaryStream & bstream); |
148 | | O3DGCErrorCode SaveUIntAC(const Vector<long> & data, |
149 | | const unsigned long M, |
150 | | BinaryStream & bstream); |
151 | | O3DGCErrorCode SaveIntACEGC(const Vector<long> & data, |
152 | | const unsigned long M, |
153 | | BinaryStream & bstream); |
154 | | |
155 | | Vector<long> m_numTFANs; |
156 | | Vector<long> m_degrees; |
157 | | Vector<long> m_configs; |
158 | | Vector<long> m_operations; |
159 | | Vector<long> m_indices; |
160 | | Vector<long> m_trianglesOrder; |
161 | | unsigned char * m_bufferAC; |
162 | | unsigned long m_sizeBufferAC; |
163 | | O3DGCStreamType m_streamType; |
164 | | }; |
165 | | |
166 | | //! |
167 | | class TriangleFans |
168 | | { |
169 | | public: |
170 | | //! Constructor. |
171 | | TriangleFans(long sizeTFAN = O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER, |
172 | | long verticesSize = O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER) |
173 | 0 | { |
174 | 0 | assert(sizeTFAN > 0); |
175 | 0 | assert(verticesSize > 0); |
176 | 0 | m_numTFANs = 0; |
177 | 0 | m_numVertices = 0; |
178 | 0 | m_verticesAllocatedSize = verticesSize; |
179 | 0 | m_sizeTFANAllocatedSize = sizeTFAN; |
180 | 0 | m_sizeTFAN = new long [m_sizeTFANAllocatedSize]; |
181 | 0 | m_vertices = new long [m_verticesAllocatedSize]; |
182 | 0 | }; |
183 | | //! Destructor. |
184 | | ~TriangleFans(void) |
185 | 0 | { |
186 | 0 | delete [] m_vertices; |
187 | 0 | delete [] m_sizeTFAN; |
188 | 0 | }; |
189 | | |
190 | | O3DGCErrorCode Allocate(long sizeTFAN, long verticesSize) |
191 | 0 | { |
192 | 0 | assert(sizeTFAN > 0); |
193 | 0 | assert(verticesSize > 0); |
194 | 0 | m_numTFANs = 0; |
195 | 0 | m_numVertices = 0; |
196 | 0 | if (m_verticesAllocatedSize < verticesSize) |
197 | 0 | { |
198 | 0 | delete [] m_vertices; |
199 | 0 | m_verticesAllocatedSize = verticesSize; |
200 | 0 | m_vertices = new long [m_verticesAllocatedSize]; |
201 | 0 | } |
202 | 0 | if (m_sizeTFANAllocatedSize < sizeTFAN) |
203 | 0 | { |
204 | 0 | delete [] m_sizeTFAN; |
205 | 0 | m_sizeTFANAllocatedSize = sizeTFAN; |
206 | 0 | m_sizeTFAN = new long [m_sizeTFANAllocatedSize]; |
207 | 0 | } |
208 | 0 | return O3DGC_OK; |
209 | 0 | }; |
210 | | O3DGCErrorCode Clear() |
211 | 0 | { |
212 | 0 | m_numTFANs = 0; |
213 | 0 | m_numVertices = 0; |
214 | 0 | return O3DGC_OK; |
215 | 0 | } |
216 | | O3DGCErrorCode AddVertex(long vertex) |
217 | 0 | { |
218 | 0 | assert(m_numTFANs >= 0); |
219 | 0 | assert(m_numTFANs < m_sizeTFANAllocatedSize); |
220 | 0 | assert(m_numVertices >= 0); |
221 | 0 | ++m_numVertices; |
222 | 0 | if (m_numVertices == m_verticesAllocatedSize) |
223 | 0 | { |
224 | 0 | m_verticesAllocatedSize *= 2; |
225 | 0 | long * tmp = m_vertices; |
226 | 0 | m_vertices = new long [m_verticesAllocatedSize]; |
227 | 0 | memcpy(m_vertices, tmp, sizeof(long) * m_numVertices); |
228 | 0 | delete [] tmp; |
229 | 0 | } |
230 | 0 | m_vertices[m_numVertices-1] = vertex; |
231 | 0 | ++m_sizeTFAN[m_numTFANs-1]; |
232 | 0 | return O3DGC_OK; |
233 | 0 | } |
234 | | O3DGCErrorCode AddTFAN() |
235 | 0 | { |
236 | 0 | assert(m_numTFANs >= 0); |
237 | 0 | ++m_numTFANs; |
238 | 0 | if (m_numTFANs == m_sizeTFANAllocatedSize) |
239 | 0 | { |
240 | 0 | m_sizeTFANAllocatedSize *= 2; |
241 | 0 | long * tmp = m_sizeTFAN; |
242 | 0 | m_sizeTFAN = new long [m_sizeTFANAllocatedSize]; |
243 | 0 | memcpy(m_sizeTFAN, tmp, sizeof(long) * m_numTFANs); |
244 | 0 | delete [] tmp; |
245 | 0 | } |
246 | 0 | m_sizeTFAN[m_numTFANs-1] = (m_numTFANs > 1) ? m_sizeTFAN[m_numTFANs-2] : 0; |
247 | 0 | return O3DGC_OK; |
248 | 0 | } |
249 | | long Begin(long tfan) const |
250 | 0 | { |
251 | 0 | assert(tfan < m_numTFANs); |
252 | 0 | assert(tfan >= 0); |
253 | 0 | return (tfan>0)?m_sizeTFAN[tfan-1]:0; |
254 | 0 | } |
255 | | long End(long tfan) const |
256 | 0 | { |
257 | 0 | assert(tfan < m_numTFANs); |
258 | 0 | assert(tfan >= 0); |
259 | 0 | return m_sizeTFAN[tfan]; |
260 | 0 | } |
261 | | long GetVertex(long vertex) const |
262 | 0 | { |
263 | 0 | assert(vertex < m_numVertices); |
264 | 0 | assert(vertex >= 0); |
265 | 0 | return m_vertices[vertex]; |
266 | 0 | } |
267 | | long GetTFANSize(long tfan) const |
268 | 0 | { |
269 | 0 | return End(tfan) - Begin(tfan); |
270 | 0 | } |
271 | | long GetNumTFANs() const |
272 | 0 | { |
273 | 0 | return m_numTFANs; |
274 | 0 | } |
275 | | long GetNumVertices() const |
276 | 0 | { |
277 | 0 | return m_numVertices; |
278 | 0 | } |
279 | | |
280 | | private: |
281 | | long m_verticesAllocatedSize; |
282 | | long m_sizeTFANAllocatedSize; |
283 | | long m_numTFANs; |
284 | | long m_numVertices; |
285 | | long * m_vertices; |
286 | | long * m_sizeTFAN; |
287 | | |
288 | | }; |
289 | | } |
290 | | #endif // O3DGC_TRIANGLE_FANS_H |
291 | | |