/src/assimp/contrib/Open3DGC/o3dgcCommon.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_COMMON_H |
26 | | #define O3DGC_COMMON_H |
27 | | |
28 | | #ifndef _CRT_SECURE_NO_WARNINGS |
29 | | #define _CRT_SECURE_NO_WARNINGS |
30 | | #endif |
31 | | |
32 | | #include <stdio.h> |
33 | | #include <string.h> |
34 | | #include <assert.h> |
35 | | #include <math.h> |
36 | | |
37 | | namespace o3dgc |
38 | | { |
39 | | typedef float Real; |
40 | | const double O3DGC_MAX_DOUBLE = 1.79769e+308; |
41 | | const long O3DGC_MIN_LONG = -2147483647; |
42 | | const long O3DGC_MAX_LONG = 2147483647; |
43 | | const long O3DGC_MAX_UCHAR8 = 255; |
44 | | const long O3DGC_MAX_TFAN_SIZE = 256; |
45 | | const unsigned long O3DGC_MAX_ULONG = 4294967295; |
46 | | |
47 | | const unsigned long O3DGC_SC3DMC_START_CODE = 0x00001F1; |
48 | | const unsigned long O3DGC_DV_START_CODE = 0x00001F2; |
49 | | const unsigned long O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES = 256; |
50 | | const unsigned long O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES = 256; |
51 | | const unsigned long O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES = 32; |
52 | | |
53 | | const unsigned long O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS = 2; |
54 | | const unsigned long O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS = 257; |
55 | | |
56 | | enum O3DGCEndianness |
57 | | { |
58 | | O3DGC_BIG_ENDIAN = 0, |
59 | | O3DGC_LITTLE_ENDIAN = 1 |
60 | | }; |
61 | | enum O3DGCErrorCode |
62 | | { |
63 | | O3DGC_OK, |
64 | | O3DGC_ERROR_BUFFER_FULL, |
65 | | O3DGC_ERROR_CREATE_FILE, |
66 | | O3DGC_ERROR_OPEN_FILE, |
67 | | O3DGC_ERROR_READ_FILE, |
68 | | O3DGC_ERROR_CORRUPTED_STREAM, |
69 | | O3DGC_ERROR_NON_SUPPORTED_FEATURE |
70 | | }; |
71 | | enum O3DGCSC3DMCBinarization |
72 | | { |
73 | | O3DGC_SC3DMC_BINARIZATION_FL = 0, // Fixed Length (not supported) |
74 | | O3DGC_SC3DMC_BINARIZATION_BP = 1, // BPC (not supported) |
75 | | O3DGC_SC3DMC_BINARIZATION_FC = 2, // 4 bits Coding (not supported) |
76 | | O3DGC_SC3DMC_BINARIZATION_AC = 3, // Arithmetic Coding (not supported) |
77 | | O3DGC_SC3DMC_BINARIZATION_AC_EGC = 4, // Arithmetic Coding & EGCk |
78 | | O3DGC_SC3DMC_BINARIZATION_ASCII = 5 // Arithmetic Coding & EGCk |
79 | | }; |
80 | | enum O3DGCStreamType |
81 | | { |
82 | | O3DGC_STREAM_TYPE_UNKOWN = 0, |
83 | | O3DGC_STREAM_TYPE_ASCII = 1, |
84 | | O3DGC_STREAM_TYPE_BINARY = 2 |
85 | | }; |
86 | | enum O3DGCSC3DMCQuantizationMode |
87 | | { |
88 | | O3DGC_SC3DMC_DIAG_BB = 0, // supported |
89 | | O3DGC_SC3DMC_MAX_ALL_DIMS = 1, // supported |
90 | | O3DGC_SC3DMC_MAX_SEP_DIM = 2 // supported |
91 | | }; |
92 | | enum O3DGCSC3DMCPredictionMode |
93 | | { |
94 | | O3DGC_SC3DMC_NO_PREDICTION = 0, // supported |
95 | | O3DGC_SC3DMC_DIFFERENTIAL_PREDICTION = 1, // supported |
96 | | O3DGC_SC3DMC_XOR_PREDICTION = 2, // not supported |
97 | | O3DGC_SC3DMC_ADAPTIVE_DIFFERENTIAL_PREDICTION = 3, // not supported |
98 | | O3DGC_SC3DMC_CIRCULAR_DIFFERENTIAL_PREDICTION = 4, // not supported |
99 | | O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION = 5, // supported |
100 | | O3DGC_SC3DMC_SURF_NORMALS_PREDICTION = 6 // supported |
101 | | }; |
102 | | enum O3DGCSC3DMCEncodingMode |
103 | | { |
104 | | O3DGC_SC3DMC_ENCODE_MODE_QBCR = 0, // not supported |
105 | | O3DGC_SC3DMC_ENCODE_MODE_SVA = 1, // not supported |
106 | | O3DGC_SC3DMC_ENCODE_MODE_TFAN = 2, // supported |
107 | | }; |
108 | | enum O3DGCDVEncodingMode |
109 | | { |
110 | | O3DGC_DYNAMIC_VECTOR_ENCODE_MODE_LIFT = 0 |
111 | | }; |
112 | | enum O3DGCIFSFloatAttributeType |
113 | | { |
114 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_UNKOWN = 0, |
115 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_POSITION = 1, |
116 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_NORMAL = 2, |
117 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_COLOR = 3, |
118 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD = 4, |
119 | | O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_WEIGHT = 5 |
120 | | |
121 | | }; |
122 | | enum O3DGCIFSIntAttributeType |
123 | | { |
124 | | O3DGC_IFS_INT_ATTRIBUTE_TYPE_UNKOWN = 0, |
125 | | O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX = 1, |
126 | | O3DGC_IFS_INT_ATTRIBUTE_TYPE_JOINT_ID = 2, |
127 | | O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX_BUFFER_ID = 3 |
128 | | }; |
129 | | |
130 | | template<class T> |
131 | | inline const T absolute(const T& a) |
132 | 0 | { |
133 | 0 | return (a < (T)(0)) ? -a : a; |
134 | 0 | } Unexecuted instantiation: long const o3dgc::absolute<long>(long const&) Unexecuted instantiation: float const o3dgc::absolute<float>(float const&) |
135 | | template<class T> |
136 | | inline const T min(const T& a, const T& b) |
137 | 0 | { |
138 | 0 | return (b < a) ? b : a; |
139 | 0 | } |
140 | | template<class T> |
141 | | inline const T max(const T& a, const T& b) |
142 | 0 | { |
143 | 0 | return (b > a) ? b : a; |
144 | 0 | } Unexecuted instantiation: long const o3dgc::max<long>(long const&, long const&) Unexecuted instantiation: double const o3dgc::max<double>(double const&, double const&) |
145 | | template<class T> |
146 | | inline void swap(T& a, T& b) |
147 | 0 | { |
148 | 0 | T tmp = a; |
149 | 0 | a = b; |
150 | 0 | b = tmp; |
151 | 0 | } |
152 | | inline double log2( double n ) |
153 | 0 | { |
154 | 0 | return log(n) / log(2.0); |
155 | 0 | } |
156 | | |
157 | | inline O3DGCEndianness SystemEndianness() |
158 | 0 | { |
159 | 0 | unsigned long num = 1; |
160 | 0 | return ( *((char *)(&num)) == 1 )? O3DGC_LITTLE_ENDIAN : O3DGC_BIG_ENDIAN ; |
161 | 0 | } |
162 | | class SC3DMCStats |
163 | | { |
164 | | public: |
165 | | SC3DMCStats(void) |
166 | 0 | { |
167 | 0 | memset(this, 0, sizeof(SC3DMCStats)); |
168 | 0 | }; |
169 | | |
170 | | double m_timeCoord; |
171 | | double m_timeNormal; |
172 | | double m_timeCoordIndex; |
173 | | double m_timeFloatAttribute[O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES]; |
174 | | double m_timeIntAttribute [O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES ]; |
175 | | double m_timeReorder; |
176 | | |
177 | | unsigned long m_streamSizeCoord; |
178 | | unsigned long m_streamSizeNormal; |
179 | | unsigned long m_streamSizeCoordIndex; |
180 | | unsigned long m_streamSizeFloatAttribute[O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES]; |
181 | | unsigned long m_streamSizeIntAttribute [O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES ]; |
182 | | |
183 | | }; |
184 | | typedef struct |
185 | | { |
186 | | long m_a; |
187 | | long m_b; |
188 | | long m_c; |
189 | | } SC3DMCTriplet; |
190 | | |
191 | | typedef struct |
192 | | { |
193 | | SC3DMCTriplet m_id; |
194 | | long m_pred[O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES]; |
195 | | } SC3DMCPredictor; |
196 | | |
197 | | inline bool operator< (const SC3DMCTriplet& lhs, const SC3DMCTriplet& rhs) |
198 | 0 | { |
199 | 0 | if (lhs.m_c != rhs.m_c) |
200 | 0 | { |
201 | 0 | return (lhs.m_c < rhs.m_c); |
202 | 0 | } |
203 | 0 | else if (lhs.m_b != rhs.m_b) |
204 | 0 | { |
205 | 0 | return (lhs.m_b < rhs.m_b); |
206 | 0 | } |
207 | 0 | return (lhs.m_a < rhs.m_a); |
208 | 0 | } |
209 | | inline bool operator== (const SC3DMCTriplet& lhs, const SC3DMCTriplet& rhs) |
210 | 0 | { |
211 | 0 | return (lhs.m_c == rhs.m_c && lhs.m_b == rhs.m_b && lhs.m_a == rhs.m_a); |
212 | 0 | } |
213 | | |
214 | | |
215 | | // fix me: optimize this function (e.g., binary search) |
216 | | inline unsigned long Insert(SC3DMCTriplet e, unsigned long & nPred, SC3DMCPredictor * const list) |
217 | 0 | { |
218 | 0 | unsigned long pos = 0xFFFFFFFF; |
219 | 0 | bool foundOrInserted = false; |
220 | 0 | for (unsigned long j = 0; j < nPred; ++j) |
221 | 0 | { |
222 | 0 | if (e == list[j].m_id) |
223 | 0 | { |
224 | 0 | foundOrInserted = true; |
225 | 0 | break; |
226 | 0 | } |
227 | 0 | else if (e < list[j].m_id) |
228 | 0 | { |
229 | 0 | if (nPred < O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS) |
230 | 0 | { |
231 | 0 | ++nPred; |
232 | 0 | } |
233 | 0 | for (unsigned long h = nPred-1; h > j; --h) |
234 | 0 | { |
235 | 0 | list[h] = list[h-1]; |
236 | 0 | } |
237 | 0 | list[j].m_id = e; |
238 | 0 | pos = j; |
239 | 0 | foundOrInserted = true; |
240 | 0 | break; |
241 | 0 | } |
242 | 0 | } |
243 | 0 | if (!foundOrInserted && nPred < O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS) |
244 | 0 | { |
245 | 0 | pos = nPred; |
246 | 0 | list[nPred++].m_id = e; |
247 | 0 | } |
248 | 0 | return pos; |
249 | 0 | } |
250 | | template <class T> |
251 | | inline void SphereToCube(const T x, const T y, const T z, |
252 | | T & a, T & b, char & index) |
253 | 0 | { |
254 | 0 | T ax = absolute(x); |
255 | 0 | T ay = absolute(y); |
256 | 0 | T az = absolute(z); |
257 | 0 | if (az >= ax && az >= ay) |
258 | 0 | { |
259 | 0 | if (z >= (T)(0)) |
260 | 0 | { |
261 | 0 | index = 0; |
262 | 0 | a = x; |
263 | 0 | b = y; |
264 | 0 | } |
265 | 0 | else |
266 | 0 | { |
267 | 0 | index = 1; |
268 | 0 | a = -x; |
269 | 0 | b = -y; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | else if (ay >= ax && ay >= az) |
273 | 0 | { |
274 | 0 | if (y >= (T)(0)) |
275 | 0 | { |
276 | 0 | index = 2; |
277 | 0 | a = z; |
278 | 0 | b = x; |
279 | 0 | } |
280 | 0 | else |
281 | 0 | { |
282 | 0 | index = 3; |
283 | 0 | a = -z; |
284 | 0 | b = -x; |
285 | 0 | } |
286 | 0 | } |
287 | 0 | else if (ax >= ay && ax >= az) |
288 | 0 | { |
289 | 0 | if (x >= (T)(0)) |
290 | 0 | { |
291 | 0 | index = 4; |
292 | 0 | a = y; |
293 | 0 | b = z; |
294 | 0 | } |
295 | 0 | else |
296 | 0 | { |
297 | 0 | index = 5; |
298 | 0 | a = -y; |
299 | 0 | b = -z; |
300 | 0 | } |
301 | 0 | } |
302 | 0 | } Unexecuted instantiation: void o3dgc::SphereToCube<long>(long, long, long, long&, long&, char&) Unexecuted instantiation: void o3dgc::SphereToCube<float>(float, float, float, float&, float&, char&) |
303 | | inline void CubeToSphere(const Real a, const Real b, const char index, |
304 | | Real & x, Real & y, Real & z) |
305 | 0 | { |
306 | 0 | switch( index ) |
307 | 0 | { |
308 | 0 | case 0: |
309 | 0 | x = a; |
310 | 0 | y = b; |
311 | 0 | z = (Real) sqrt(max(0.0, 1.0 - x*x-y*y)); |
312 | 0 | break; |
313 | 0 | case 1: |
314 | 0 | x = -a; |
315 | 0 | y = -b; |
316 | 0 | z = -(Real) sqrt(max(0.0, 1.0 - x*x-y*y)); |
317 | 0 | break; |
318 | 0 | case 2: |
319 | 0 | z = a; |
320 | 0 | x = b; |
321 | 0 | y = (Real) sqrt(max(0.0, 1.0 - x*x-z*z)); |
322 | 0 | break; |
323 | 0 | case 3: |
324 | 0 | z = -a; |
325 | 0 | x = -b; |
326 | 0 | y = -(Real) sqrt(max(0.0, 1.0 - x*x-z*z)); |
327 | 0 | break; |
328 | 0 | case 4: |
329 | 0 | y = a; |
330 | 0 | z = b; |
331 | 0 | x = (Real) sqrt(max(0.0, 1.0 - y*y-z*z)); |
332 | 0 | break; |
333 | 0 | case 5: |
334 | 0 | y = -a; |
335 | 0 | z = -b; |
336 | 0 | x = -(Real) sqrt(max(0.0, 1.0 - y*y-z*z)); |
337 | 0 | break; |
338 | 0 | } |
339 | 0 | } |
340 | | inline unsigned long IntToUInt(long value) |
341 | 0 | { |
342 | 0 | return (value < 0)?(unsigned long) (-1 - (2 * value)):(unsigned long) (2 * value); |
343 | 0 | } |
344 | | inline long UIntToInt(unsigned long uiValue) |
345 | 0 | { |
346 | 0 | return (uiValue & 1)?-((long) ((uiValue+1) >> 1)):((long) (uiValue >> 1)); |
347 | 0 | } |
348 | | inline void ComputeVectorMinMax(const Real * const tab, |
349 | | unsigned long size, |
350 | | unsigned long dim, |
351 | | unsigned long stride, |
352 | | Real * minTab, |
353 | | Real * maxTab, |
354 | | O3DGCSC3DMCQuantizationMode quantMode) |
355 | 0 | { |
356 | 0 | if (size == 0 || dim == 0) |
357 | 0 | { |
358 | 0 | return; |
359 | 0 | } |
360 | 0 | unsigned long p = 0; |
361 | 0 | for(unsigned long d = 0; d < dim; ++d) |
362 | 0 | { |
363 | 0 | maxTab[d] = minTab[d] = tab[p++]; |
364 | 0 | } |
365 | 0 | p = stride; |
366 | 0 | for(unsigned long i = 1; i < size; ++i) |
367 | 0 | { |
368 | 0 | for(unsigned long d = 0; d < dim; ++d) |
369 | 0 | { |
370 | 0 | if (maxTab[d] < tab[p+d]) maxTab[d] = tab[p+d]; |
371 | 0 | if (minTab[d] > tab[p+d]) minTab[d] = tab[p+d]; |
372 | 0 | } |
373 | 0 | p += stride; |
374 | 0 | } |
375 | 0 |
|
376 | 0 | if (quantMode == O3DGC_SC3DMC_DIAG_BB) |
377 | 0 | { |
378 | 0 | Real diag = Real( 0.0 ); |
379 | 0 | Real r; |
380 | 0 | for(unsigned long d = 0; d < dim; ++d) |
381 | 0 | { |
382 | 0 | r = (maxTab[d] - minTab[d]); |
383 | 0 | diag += r*r; |
384 | 0 | } |
385 | 0 | diag = static_cast<Real>(sqrt(diag)); |
386 | 0 | for(unsigned long d = 0; d < dim; ++d) |
387 | 0 | { |
388 | 0 | maxTab[d] = minTab[d] + diag; |
389 | 0 | } |
390 | 0 | } |
391 | 0 | else if (quantMode == O3DGC_SC3DMC_MAX_ALL_DIMS) |
392 | 0 | { |
393 | 0 | Real maxr = (maxTab[0] - minTab[0]); |
394 | 0 | Real r; |
395 | 0 | for(unsigned long d = 1; d < dim; ++d) |
396 | 0 | { |
397 | 0 | r = (maxTab[d] - minTab[d]); |
398 | 0 | if ( r > maxr) |
399 | 0 | { |
400 | 0 | maxr = r; |
401 | 0 | } |
402 | 0 | } |
403 | 0 | for(unsigned long d = 0; d < dim; ++d) |
404 | 0 | { |
405 | 0 | maxTab[d] = minTab[d] + maxr; |
406 | 0 | } |
407 | 0 | } |
408 | 0 | } |
409 | | } |
410 | | #endif // O3DGC_COMMON_H |