summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/contrib/Open3DGC/o3dgcTriangleFans.cpp
blob: 078ed16e62f388e2c00ac76bfadfdd80a7a75d0a (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
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.
*/

#include "o3dgcTriangleFans.h"
#include "o3dgcArithmeticCodec.h"

//#define DEBUG_VERBOSE

namespace o3dgc
{
#ifdef DEBUG_VERBOSE
        FILE* g_fileDebugTF = NULL;
#endif //DEBUG_VERBOSE

    O3DGCErrorCode    SaveUIntData(const Vector<long> & data,
                                   BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();
        bstream.WriteUInt32ASCII(0);
        const unsigned long size       = data.GetSize();
        bstream.WriteUInt32ASCII(size);
        for(unsigned long i = 0; i < size; ++i)
        {
            bstream.WriteUIntASCII(data[i]);
        }
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
    O3DGCErrorCode    SaveIntData(const Vector<long> & data,
                                  BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();
        bstream.WriteUInt32ASCII(0);
        const unsigned long size       = data.GetSize();
        bstream.WriteUInt32ASCII(size);
        for(unsigned long i = 0; i < size; ++i)
        {
            bstream.WriteIntASCII(data[i]);
        }
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
    O3DGCErrorCode    SaveBinData(const Vector<long> & data,
                                  BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();
        bstream.WriteUInt32ASCII(0);
        const unsigned long size = data.GetSize();
        long symbol;
        bstream.WriteUInt32ASCII(size);
        for(unsigned long i = 0; i < size; )
        {
            symbol = 0;
            for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 && i < size; ++h)
            {
                symbol += (data[i] << h);
                ++i;
            }
            bstream.WriteUCharASCII((unsigned char) symbol);
        }
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
    O3DGCErrorCode    CompressedTriangleFans::SaveUIntAC(const Vector<long> & data,
                                                         const unsigned long M,
                                                         BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();     
        const unsigned int NMAX = data.GetSize() * 8 + 100;
        const unsigned long size       = data.GetSize();
        long minValue = O3DGC_MAX_LONG;
        bstream.WriteUInt32Bin(0);
        bstream.WriteUInt32Bin(size);
        if (size > 0)
        {
    #ifdef DEBUG_VERBOSE
            printf("-----------\nsize %i, start %i\n", size, start);
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
    #endif //DEBUG_VERBOSE

            for(unsigned long i = 0; i < size; ++i)
            {
                if (minValue > data[i]) 
                {
                    minValue = data[i];
                }
    #ifdef DEBUG_VERBOSE
                printf("%i\t%i\n", i, data[i]);
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
    #endif //DEBUG_VERBOSE
            }
            bstream.WriteUInt32Bin(minValue);
            if ( m_sizeBufferAC < NMAX )
            {
                delete [] m_bufferAC;
                m_sizeBufferAC = NMAX;
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
            }
            Arithmetic_Codec ace;
            ace.set_buffer(NMAX, m_bufferAC);
            ace.start_encoder();
            Adaptive_Data_Model mModelValues(M+1);
            for(unsigned long i = 0; i < size; ++i)
            {
                ace.encode(data[i]-minValue, mModelValues);
            }
            unsigned long encodedBytes = ace.stop_encoder();
            for(unsigned long i = 0; i < encodedBytes; ++i)
            {
                bstream.WriteUChar8Bin(m_bufferAC[i]);
            }
        }
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
    O3DGCErrorCode    CompressedTriangleFans::SaveBinAC(const Vector<long> & data,
                                                         BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();     
        const unsigned int NMAX = data.GetSize() * 8 + 100;
        const unsigned long size       = data.GetSize();
        bstream.WriteUInt32Bin(0);
        bstream.WriteUInt32Bin(size);
        if (size > 0)
        {
            if ( m_sizeBufferAC < NMAX )
            {
                delete [] m_bufferAC;
                m_sizeBufferAC = NMAX;
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
            }
            Arithmetic_Codec ace;
            ace.set_buffer(NMAX, m_bufferAC);
            ace.start_encoder();
            Adaptive_Bit_Model bModel;
    #ifdef DEBUG_VERBOSE
            printf("-----------\nsize %i, start %i\n", size, start);
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
    #endif //DEBUG_VERBOSE
            for(unsigned long i = 0; i < size; ++i)
            {
                ace.encode(data[i], bModel);
    #ifdef DEBUG_VERBOSE
                printf("%i\t%i\n", i, data[i]);
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
    #endif //DEBUG_VERBOSE
            }
            unsigned long encodedBytes = ace.stop_encoder();
            for(unsigned long i = 0; i < encodedBytes; ++i)
            {
                bstream.WriteUChar8Bin(m_bufferAC[i]);
            }
        }
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }

    O3DGCErrorCode    CompressedTriangleFans::SaveIntACEGC(const Vector<long> & data,
                                                            const unsigned long M,
                                                            BinaryStream & bstream) 
    {
        unsigned long start = bstream.GetSize();
        const unsigned int NMAX = data.GetSize() * 8 + 100;
        const unsigned long size       = data.GetSize();
        long minValue = 0;
        bstream.WriteUInt32Bin(0);
        bstream.WriteUInt32Bin(size);
        if (size > 0)
        {
#ifdef DEBUG_VERBOSE
            printf("-----------\nsize %i, start %i\n", size, start);
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
#endif //DEBUG_VERBOSE
            for(unsigned long i = 0; i < size; ++i)
            {
                if (minValue > data[i]) 
                {
                    minValue = data[i];
                }
#ifdef DEBUG_VERBOSE
                printf("%i\t%i\n", i, data[i]);
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
#endif //DEBUG_VERBOSE
            }
            bstream.WriteUInt32Bin(minValue + O3DGC_MAX_LONG);
            if ( m_sizeBufferAC < NMAX )
            {
                delete [] m_bufferAC;
                m_sizeBufferAC = NMAX;
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
            }
            Arithmetic_Codec ace;
            ace.set_buffer(NMAX, m_bufferAC);
            ace.start_encoder();
            Adaptive_Data_Model mModelValues(M+2);
            Static_Bit_Model bModel0;
            Adaptive_Bit_Model bModel1;
            unsigned long value;
            for(unsigned long i = 0; i < size; ++i)
            {
                value = data[i]-minValue;
                if (value < M) 
                {
                    ace.encode(value, mModelValues);
                }
                else 
                {
                    ace.encode(M, mModelValues);
                    ace.ExpGolombEncode(value-M, 0, bModel0, bModel1);
                }
            }
            unsigned long encodedBytes = ace.stop_encoder();
            for(unsigned long i = 0; i < encodedBytes; ++i)
            {
                bstream.WriteUChar8Bin(m_bufferAC[i]);
            }
        }
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
        return O3DGC_OK;
    }
    O3DGCErrorCode    CompressedTriangleFans::Save(BinaryStream & bstream, bool encodeTrianglesOrder, O3DGCStreamType streamType) 
    {
#ifdef DEBUG_VERBOSE
        g_fileDebugTF = fopen("SaveIntACEGC_new.txt", "w");
#endif //DEBUG_VERBOSE

        if (streamType == O3DGC_STREAM_TYPE_ASCII)
        {
            SaveUIntData(m_numTFANs  , bstream);
            SaveUIntData(m_degrees   , bstream);
            SaveUIntData(m_configs   , bstream);
            SaveBinData (m_operations, bstream);
            SaveIntData (m_indices   , bstream);
            if (encodeTrianglesOrder)
            {
                SaveUIntData(m_trianglesOrder, bstream);
            }
        }
        else
        {
            SaveIntACEGC(m_numTFANs  , 4 , bstream);
            SaveIntACEGC(m_degrees   , 16, bstream);
            SaveUIntAC  (m_configs   , 10, bstream);
            SaveBinAC   (m_operations,     bstream);
            SaveIntACEGC(m_indices   , 8 , bstream);
            if (encodeTrianglesOrder)
            {
                SaveIntACEGC(m_trianglesOrder , 16, bstream);
            }
        }
#ifdef DEBUG_VERBOSE
        fclose(g_fileDebugTF);
#endif //DEBUG_VERBOSE
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadUIntData(Vector<long> & data,
                                  const BinaryStream & bstream,
                                  unsigned long & iterator) 
    {
        bstream.ReadUInt32ASCII(iterator);
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
        data.Allocate(size);
        data.Clear();
        for(unsigned long i = 0; i < size; ++i)
        {
            data.PushBack(bstream.ReadUIntASCII(iterator));
        }
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadIntData(Vector<long> & data,
                                  const BinaryStream & bstream,
                                  unsigned long & iterator) 
    {
        bstream.ReadUInt32ASCII(iterator);
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
        data.Allocate(size);
        data.Clear();
        for(unsigned long i = 0; i < size; ++i)
        {
            data.PushBack(bstream.ReadIntASCII(iterator));
        }
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadBinData(Vector<long> & data,
                                  const BinaryStream & bstream,
                                  unsigned long & iterator) 
    {
        bstream.ReadUInt32ASCII(iterator);
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
        long symbol;
        data.Allocate(size * O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
        data.Clear();
        for(unsigned long i = 0; i < size;)
        {
            symbol = bstream.ReadUCharASCII(iterator);
            for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0; ++h)
            {
                data.PushBack(symbol & 1);
                symbol >>= 1;
                ++i;
            }
        }
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadUIntAC(Vector<long> & data,
                                 const unsigned long M,
                                 const BinaryStream & bstream,
                                 unsigned long & iterator) 
    {
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
        if (size == 0)
        {
            return O3DGC_OK;
        }
        long minValue   = bstream.ReadUInt32Bin(iterator);
        unsigned char * buffer = 0;
        bstream.GetBuffer(iterator, buffer);
        iterator += sizeSize;
        data.Allocate(size);
        Arithmetic_Codec acd;
        acd.set_buffer(sizeSize, buffer);
        acd.start_decoder();
        Adaptive_Data_Model mModelValues(M+1);
#ifdef DEBUG_VERBOSE
        printf("-----------\nsize %i\n", size);
        fprintf(g_fileDebugTF, "size %i\n", size);
#endif //DEBUG_VERBOSE
        for(unsigned long i = 0; i < size; ++i)
        {
            data.PushBack(acd.decode(mModelValues)+minValue);
#ifdef DEBUG_VERBOSE
            printf("%i\t%i\n", i, data[i]);
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
#endif //DEBUG_VERBOSE
        }
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadIntACEGC(Vector<long> & data,
                                   const unsigned long M,
                                   const BinaryStream & bstream,
                                   unsigned long & iterator) 
    {
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
        if (size == 0)
        {
            return O3DGC_OK;
        }
        long minValue   = bstream.ReadUInt32Bin(iterator) - O3DGC_MAX_LONG;
        unsigned char * buffer = 0;
        bstream.GetBuffer(iterator, buffer);
        iterator += sizeSize;
        data.Allocate(size);
        Arithmetic_Codec acd;
        acd.set_buffer(sizeSize, buffer);
        acd.start_decoder();
        Adaptive_Data_Model mModelValues(M+2);
        Static_Bit_Model bModel0;
        Adaptive_Bit_Model bModel1;
        unsigned long value;

#ifdef DEBUG_VERBOSE
        printf("-----------\nsize %i\n", size);
        fprintf(g_fileDebugTF, "size %i\n", size);
#endif //DEBUG_VERBOSE
        for(unsigned long i = 0; i < size; ++i)
        {
            value = acd.decode(mModelValues);
            if ( value == M)
            {
                value += acd.ExpGolombDecode(0, bModel0, bModel1);
            }
            data.PushBack(value + minValue);
#ifdef DEBUG_VERBOSE
            printf("%i\t%i\n", i, data[i]);
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
#endif //DEBUG_VERBOSE
        }
#ifdef DEBUG_VERBOSE
        fflush(g_fileDebugTF);
#endif //DEBUG_VERBOSE
        return O3DGC_OK;
    }
    O3DGCErrorCode    LoadBinAC(Vector<long> & data,
                                const BinaryStream & bstream,
                                unsigned long & iterator) 
    {
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 8;
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
        if (size == 0)
        {
            return O3DGC_OK;
        }
        unsigned char * buffer = 0;
        bstream.GetBuffer(iterator, buffer);
        iterator += sizeSize;
        data.Allocate(size);
        Arithmetic_Codec acd;
        acd.set_buffer(sizeSize, buffer);
        acd.start_decoder();
        Adaptive_Bit_Model bModel;
#ifdef DEBUG_VERBOSE
        printf("-----------\nsize %i\n", size);
        fprintf(g_fileDebugTF, "size %i\n", size);
#endif //DEBUG_VERBOSE
        for(unsigned long i = 0; i < size; ++i)
        {
            data.PushBack(acd.decode(bModel));
#ifdef DEBUG_VERBOSE
            printf("%i\t%i\n", i, data[i]);
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
#endif //DEBUG_VERBOSE
        }
        return O3DGC_OK;
    }
    O3DGCErrorCode    CompressedTriangleFans::Load(const BinaryStream & bstream,
                                                   unsigned long & iterator, 
                                                   bool decodeTrianglesOrder,
                                                   O3DGCStreamType streamType) 
    {
#ifdef DEBUG_VERBOSE
        g_fileDebugTF = fopen("Load_new.txt", "w");
#endif //DEBUG_VERBOSE
        if (streamType == O3DGC_STREAM_TYPE_ASCII)
        {
            LoadUIntData(m_numTFANs  , bstream, iterator);
            LoadUIntData(m_degrees   , bstream, iterator);
            LoadUIntData(m_configs   , bstream, iterator);
            LoadBinData (m_operations, bstream, iterator);
            LoadIntData (m_indices   , bstream, iterator);
            if (decodeTrianglesOrder)
            {
                LoadUIntData(m_trianglesOrder , bstream, iterator);
            }
        }
        else
        {
            LoadIntACEGC(m_numTFANs  , 4 , bstream, iterator);
            LoadIntACEGC(m_degrees   , 16, bstream, iterator);
            LoadUIntAC  (m_configs   , 10, bstream, iterator);
            LoadBinAC   (m_operations,     bstream, iterator);
            LoadIntACEGC(m_indices   , 8 , bstream, iterator);
            if (decodeTrianglesOrder)
            {
                LoadIntACEGC(m_trianglesOrder , 16, bstream, iterator);
            }
        }

#ifdef DEBUG_VERBOSE
        fclose(g_fileDebugTF);
#endif //DEBUG_VERBOSE
        return O3DGC_OK;
    }
}