summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/contrib/Open3DGC/o3dgcCommon.h
blob: ff6bf75587b6b907e1b52bad391f5d03cea04a5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


#pragma once
#ifndef O3DGC_COMMON_H
#define O3DGC_COMMON_H

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>

namespace o3dgc
{
    typedef float        Real;
    const double O3DGC_MAX_DOUBLE       = 1.79769e+308;
    const long O3DGC_MIN_LONG           = -2147483647;
    const long O3DGC_MAX_LONG           =  2147483647;
    const long O3DGC_MAX_UCHAR8         = 255;
    const long O3DGC_MAX_TFAN_SIZE      = 256;
    const unsigned long O3DGC_MAX_ULONG = 4294967295;

    const unsigned long O3DGC_SC3DMC_START_CODE               = 0x00001F1;
    const unsigned long O3DGC_DV_START_CODE                   = 0x00001F2;
    const unsigned long O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES = 256;
    const unsigned long O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES   = 256;
    const unsigned long O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES = 32;

    const unsigned long O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS = 2;
    const unsigned long O3DGC_SC3DMC_MAX_PREDICTION_SYMBOLS   = 257;

    enum O3DGCEndianness
    {
        O3DGC_BIG_ENDIAN     = 0,
        O3DGC_LITTLE_ENDIAN  = 1
    };
    enum O3DGCErrorCode
    {
        O3DGC_OK,
        O3DGC_ERROR_BUFFER_FULL,
        O3DGC_ERROR_CREATE_FILE,
        O3DGC_ERROR_OPEN_FILE,
        O3DGC_ERROR_READ_FILE,
        O3DGC_ERROR_CORRUPTED_STREAM,
        O3DGC_ERROR_NON_SUPPORTED_FEATURE
    };
    enum O3DGCSC3DMCBinarization
    {
        O3DGC_SC3DMC_BINARIZATION_FL     = 0,            // Fixed Length (not supported)
        O3DGC_SC3DMC_BINARIZATION_BP     = 1,            // BPC (not supported)
        O3DGC_SC3DMC_BINARIZATION_FC     = 2,            // 4 bits Coding (not supported)
        O3DGC_SC3DMC_BINARIZATION_AC     = 3,            // Arithmetic Coding (not supported)
        O3DGC_SC3DMC_BINARIZATION_AC_EGC = 4,            // Arithmetic Coding & EGCk
        O3DGC_SC3DMC_BINARIZATION_ASCII  = 5             // Arithmetic Coding & EGCk
    };
    enum O3DGCStreamType
    {
        O3DGC_STREAM_TYPE_UNKOWN = 0,
        O3DGC_STREAM_TYPE_ASCII  = 1,
        O3DGC_STREAM_TYPE_BINARY = 2
    };
    enum O3DGCSC3DMCQuantizationMode
    {
        O3DGC_SC3DMC_DIAG_BB             = 0, // supported
        O3DGC_SC3DMC_MAX_ALL_DIMS        = 1, // supported
        O3DGC_SC3DMC_MAX_SEP_DIM         = 2  // supported
    };
    enum O3DGCSC3DMCPredictionMode
    {
        O3DGC_SC3DMC_NO_PREDICTION                    = 0, // supported
        O3DGC_SC3DMC_DIFFERENTIAL_PREDICTION          = 1, // supported
        O3DGC_SC3DMC_XOR_PREDICTION                   = 2, // not supported
        O3DGC_SC3DMC_ADAPTIVE_DIFFERENTIAL_PREDICTION = 3, // not supported
        O3DGC_SC3DMC_CIRCULAR_DIFFERENTIAL_PREDICTION = 4, // not supported
        O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION         = 5,  // supported
        O3DGC_SC3DMC_SURF_NORMALS_PREDICTION          = 6   // supported
    };
    enum O3DGCSC3DMCEncodingMode
    {
        O3DGC_SC3DMC_ENCODE_MODE_QBCR       = 0,        // not supported
        O3DGC_SC3DMC_ENCODE_MODE_SVA        = 1,        // not supported
        O3DGC_SC3DMC_ENCODE_MODE_TFAN       = 2,        // supported
    };
    enum O3DGCDVEncodingMode
    {
        O3DGC_DYNAMIC_VECTOR_ENCODE_MODE_LIFT       = 0
    };
    enum O3DGCIFSFloatAttributeType
    {
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_UNKOWN   = 0,
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_POSITION = 1,
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_NORMAL   = 2,
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_COLOR    = 3,
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD = 4,
        O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_WEIGHT   = 5
        
    };
    enum O3DGCIFSIntAttributeType
    {
        O3DGC_IFS_INT_ATTRIBUTE_TYPE_UNKOWN   = 0,
        O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX    = 1,
        O3DGC_IFS_INT_ATTRIBUTE_TYPE_JOINT_ID = 2,
        O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX_BUFFER_ID = 3
    };

    template<class T> 
    inline const T absolute(const T& a)
    {
        return (a < (T)(0)) ? -a : a;
    }
    template<class T> 
    inline const T min(const T& a, const T& b)
    {
        return (b < a) ? b : a;
    }
    template<class T> 
    inline const T max(const T& a, const T& b)
    {
        return (b > a) ? b : a;
    }
    template<class T> 
    inline void swap(T& a, T& b)
    {
        T tmp = a;
        a = b;
        b = tmp;
    }
    inline double log2( double n )  
    {  
        return log(n) / log(2.0);  
    }

    inline O3DGCEndianness SystemEndianness()
    {
        unsigned long num = 1;
        return ( *((char *)(&num)) == 1 )? O3DGC_LITTLE_ENDIAN : O3DGC_BIG_ENDIAN ;
    }
    class SC3DMCStats
    {
    public: 
                                    SC3DMCStats(void)
                                    {
                                        memset(this, 0, sizeof(SC3DMCStats));
                                    };
                                    ~SC3DMCStats(void){};
        
        double                      m_timeCoord;
        double                      m_timeNormal;
        double                      m_timeCoordIndex;
        double                      m_timeFloatAttribute[O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES];
        double                      m_timeIntAttribute  [O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES  ];
        double                      m_timeReorder;

        unsigned long               m_streamSizeCoord;
        unsigned long               m_streamSizeNormal;
        unsigned long               m_streamSizeCoordIndex;
        unsigned long               m_streamSizeFloatAttribute[O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES];
        unsigned long               m_streamSizeIntAttribute  [O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES  ];

    };
    typedef struct 
    {
        long          m_a;
        long          m_b;
        long          m_c;
    } SC3DMCTriplet;

    typedef struct 
    {
        SC3DMCTriplet m_id;
        long          m_pred[O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES];
    } SC3DMCPredictor;

    inline bool operator< (const SC3DMCTriplet& lhs, const SC3DMCTriplet& rhs)
    {
          if (lhs.m_c != rhs.m_c)
          {
              return (lhs.m_c < rhs.m_c);
          }
          else if (lhs.m_b != rhs.m_b)
          {
              return (lhs.m_b < rhs.m_b);
          }
          return (lhs.m_a < rhs.m_a);
    }
    inline bool operator== (const SC3DMCTriplet& lhs, const SC3DMCTriplet& rhs)
    {
          return (lhs.m_c == rhs.m_c && lhs.m_b == rhs.m_b && lhs.m_a == rhs.m_a);
    }


    // fix me: optimize this function (e.g., binary search)
    inline unsigned long Insert(SC3DMCTriplet e, unsigned long & nPred, SC3DMCPredictor * const list)
    {
        unsigned long pos = 0xFFFFFFFF;
        bool foundOrInserted = false;
        for (unsigned long j = 0; j < nPred; ++j)
        {
            if (e == list[j].m_id)
            {
                foundOrInserted = true;
                break;
            }
            else if (e < list[j].m_id)
            {
                if (nPred < O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS)
                {
                    ++nPred;
                }
                for (unsigned long h = nPred-1; h > j; --h)
                {
                    list[h] = list[h-1];
                }
                list[j].m_id = e;
                pos = j;
                foundOrInserted = true;
                break;
            }
        }
        if (!foundOrInserted && nPred < O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS)
        {
            pos = nPred;
            list[nPred++].m_id = e;
        }
        return pos;
    }
    template <class T> 
    inline void SphereToCube(const T x, const T y, const T z, 
                             T & a, T & b, char & index)
    {
        T ax = absolute(x);
        T ay = absolute(y);
        T az = absolute(z);
        if (az >= ax && az >= ay)
        {
            if (z >= (T)(0))
            {
                index = 0;
                a = x;
                b = y;
            }
            else
            {
                index = 1;
                a = -x;
                b = -y;
            }
        }
        else if (ay >= ax && ay >= az)
        {
            if (y >= (T)(0))
            {
                index = 2;
                a = z;
                b = x;
            }
            else
            {
                index = 3;
                a = -z;
                b = -x;
            }
        }
        else if (ax >= ay && ax >= az)
        {
            if (x >= (T)(0))
            {
                index = 4;
                a = y;
                b = z;
            }
            else
            {
                index = 5;
                a = -y;
                b = -z;
            }
        }
    }
    inline void CubeToSphere(const Real a, const Real b, const char index,
                             Real & x, Real & y, Real & z)
    {
        switch( index )
        {
        case 0:
            x = a;
            y = b;
            z =  (Real) sqrt(max(0.0, 1.0 - x*x-y*y));
            break;
        case 1:
            x = -a;
            y = -b;
            z = -(Real) sqrt(max(0.0, 1.0 - x*x-y*y));
            break;
        case 2:
            z = a;
            x = b;
            y =  (Real) sqrt(max(0.0, 1.0 - x*x-z*z));
            break;
        case 3:
            z = -a;
            x = -b;
            y = -(Real) sqrt(max(0.0, 1.0 - x*x-z*z));
            break;
        case 4:
            y = a;
            z = b;
            x =  (Real) sqrt(max(0.0, 1.0 - y*y-z*z));
            break;
        case 5:
            y = -a;
            z = -b;
            x = -(Real) sqrt(max(0.0, 1.0 - y*y-z*z));
            break;
        }
    }
    inline unsigned long IntToUInt(long value)
    {
        return (value < 0)?(unsigned long) (-1 - (2 * value)):(unsigned long) (2 * value);
    }
    inline long UIntToInt(unsigned long uiValue)
    {
        return (uiValue & 1)?-((long) ((uiValue+1) >> 1)):((long) (uiValue >> 1));
    }
    inline void ComputeVectorMinMax(const Real * const tab, 
                                    unsigned long size, 
                                    unsigned long dim,
                                    unsigned long stride,
                                    Real * minTab,
                                    Real * maxTab,
                                    O3DGCSC3DMCQuantizationMode quantMode)
    {
        if (size == 0 || dim == 0)
        {
            return;
        }
        unsigned long p = 0;
        for(unsigned long d = 0; d < dim; ++d)
        {
            maxTab[d] = minTab[d] = tab[p++];
        }
        p = stride;
        for(unsigned long i = 1; i < size; ++i)
        {
            for(unsigned long d = 0; d < dim; ++d)
            {
                if (maxTab[d] < tab[p+d]) maxTab[d] = tab[p+d];
                if (minTab[d] > tab[p+d]) minTab[d] = tab[p+d];
            }
            p += stride;
        }

        if (quantMode == O3DGC_SC3DMC_DIAG_BB)
        {
            Real diag = Real( 0.0 );
            Real r;
            for(unsigned long d = 0; d < dim; ++d)
            {
                r     = (maxTab[d] - minTab[d]);
                diag += r*r;
            } 
            diag = static_cast<Real>(sqrt(diag));
            for(unsigned long d = 0; d < dim; ++d)
            {
                 maxTab[d] = minTab[d] + diag;
            } 
        }
        else if (quantMode == O3DGC_SC3DMC_MAX_ALL_DIMS)
        {            
            Real maxr = (maxTab[0] - minTab[0]);
            Real r;
            for(unsigned long d = 1; d < dim; ++d)
            {
                r = (maxTab[d] - minTab[d]);
                if ( r > maxr)
                {
                    maxr = r;
                }
            } 
            for(unsigned long d = 0; d < dim; ++d)
            {
                 maxTab[d] = minTab[d] + maxr;
            } 
        }
    }
}
#endif // O3DGC_COMMON_H