Coverage Report

Created: 2025-06-22 07:30

/src/assimp/contrib/Open3DGC/o3dgcTriangleFans.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "o3dgcTriangleFans.h"
24
#include "o3dgcArithmeticCodec.h"
25
26
//#define DEBUG_VERBOSE
27
28
namespace o3dgc
29
{
30
#ifdef DEBUG_VERBOSE
31
        FILE* g_fileDebugTF = NULL;
32
#endif //DEBUG_VERBOSE
33
34
    O3DGCErrorCode    SaveUIntData(const Vector<long> & data,
35
                                   BinaryStream & bstream) 
36
0
    {
37
0
        unsigned long start = bstream.GetSize();
38
0
        bstream.WriteUInt32ASCII(0);
39
0
        const unsigned long size       = data.GetSize();
40
0
        bstream.WriteUInt32ASCII(size);
41
0
        for(unsigned long i = 0; i < size; ++i)
42
0
        {
43
0
            bstream.WriteUIntASCII(data[i]);
44
0
        }
45
0
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
46
0
        return O3DGC_OK;
47
0
    }
48
    O3DGCErrorCode    SaveIntData(const Vector<long> & data,
49
                                  BinaryStream & bstream) 
50
0
    {
51
0
        unsigned long start = bstream.GetSize();
52
0
        bstream.WriteUInt32ASCII(0);
53
0
        const unsigned long size       = data.GetSize();
54
0
        bstream.WriteUInt32ASCII(size);
55
0
        for(unsigned long i = 0; i < size; ++i)
56
0
        {
57
0
            bstream.WriteIntASCII(data[i]);
58
0
        }
59
0
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
60
0
        return O3DGC_OK;
61
0
    }
62
    O3DGCErrorCode    SaveBinData(const Vector<long> & data,
63
                                  BinaryStream & bstream) 
64
0
    {
65
0
        unsigned long start = bstream.GetSize();
66
0
        bstream.WriteUInt32ASCII(0);
67
0
        const unsigned long size = data.GetSize();
68
0
        long symbol;
69
0
        bstream.WriteUInt32ASCII(size);
70
0
        for(unsigned long i = 0; i < size; )
71
0
        {
72
0
            symbol = 0;
73
0
            for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 && i < size; ++h)
74
0
            {
75
0
                symbol += (data[i] << h);
76
0
                ++i;
77
0
            }
78
0
            bstream.WriteUCharASCII((unsigned char) symbol);
79
0
        }
80
0
        bstream.WriteUInt32ASCII(start, bstream.GetSize() - start);
81
0
        return O3DGC_OK;
82
0
    }
83
    O3DGCErrorCode    CompressedTriangleFans::SaveUIntAC(const Vector<long> & data,
84
                                                         const unsigned long M,
85
                                                         BinaryStream & bstream) 
86
0
    {
87
0
        unsigned long start = bstream.GetSize();     
88
0
        const unsigned int NMAX = data.GetSize() * 8 + 100;
89
0
        const unsigned long size       = data.GetSize();
90
0
        long minValue = O3DGC_MAX_LONG;
91
0
        bstream.WriteUInt32Bin(0);
92
0
        bstream.WriteUInt32Bin(size);
93
0
        if (size > 0)
94
0
        {
95
    #ifdef DEBUG_VERBOSE
96
            printf("-----------\nsize %i, start %i\n", size, start);
97
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
98
    #endif //DEBUG_VERBOSE
99
100
0
            for(unsigned long i = 0; i < size; ++i)
101
0
            {
102
0
                if (minValue > data[i]) 
103
0
                {
104
0
                    minValue = data[i];
105
0
                }
106
    #ifdef DEBUG_VERBOSE
107
                printf("%i\t%i\n", i, data[i]);
108
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
109
    #endif //DEBUG_VERBOSE
110
0
            }
111
0
            bstream.WriteUInt32Bin(minValue);
112
0
            if ( m_sizeBufferAC < NMAX )
113
0
            {
114
0
                delete [] m_bufferAC;
115
0
                m_sizeBufferAC = NMAX;
116
0
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
117
0
            }
118
0
            Arithmetic_Codec ace;
119
0
            ace.set_buffer(NMAX, m_bufferAC);
120
0
            ace.start_encoder();
121
0
            Adaptive_Data_Model mModelValues(M+1);
122
0
            for(unsigned long i = 0; i < size; ++i)
123
0
            {
124
0
                ace.encode(data[i]-minValue, mModelValues);
125
0
            }
126
0
            unsigned long encodedBytes = ace.stop_encoder();
127
0
            for(unsigned long i = 0; i < encodedBytes; ++i)
128
0
            {
129
0
                bstream.WriteUChar8Bin(m_bufferAC[i]);
130
0
            }
131
0
        }
132
0
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
133
0
        return O3DGC_OK;
134
0
    }
135
    O3DGCErrorCode    CompressedTriangleFans::SaveBinAC(const Vector<long> & data,
136
                                                         BinaryStream & bstream) 
137
0
    {
138
0
        unsigned long start = bstream.GetSize();     
139
0
        const unsigned int NMAX = data.GetSize() * 8 + 100;
140
0
        const unsigned long size       = data.GetSize();
141
0
        bstream.WriteUInt32Bin(0);
142
0
        bstream.WriteUInt32Bin(size);
143
0
        if (size > 0)
144
0
        {
145
0
            if ( m_sizeBufferAC < NMAX )
146
0
            {
147
0
                delete [] m_bufferAC;
148
0
                m_sizeBufferAC = NMAX;
149
0
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
150
0
            }
151
0
            Arithmetic_Codec ace;
152
0
            ace.set_buffer(NMAX, m_bufferAC);
153
0
            ace.start_encoder();
154
0
            Adaptive_Bit_Model bModel;
155
    #ifdef DEBUG_VERBOSE
156
            printf("-----------\nsize %i, start %i\n", size, start);
157
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
158
    #endif //DEBUG_VERBOSE
159
0
            for(unsigned long i = 0; i < size; ++i)
160
0
            {
161
0
                ace.encode(data[i], bModel);
162
    #ifdef DEBUG_VERBOSE
163
                printf("%i\t%i\n", i, data[i]);
164
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
165
    #endif //DEBUG_VERBOSE
166
0
            }
167
0
            unsigned long encodedBytes = ace.stop_encoder();
168
0
            for(unsigned long i = 0; i < encodedBytes; ++i)
169
0
            {
170
0
                bstream.WriteUChar8Bin(m_bufferAC[i]);
171
0
            }
172
0
        }
173
0
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
174
0
        return O3DGC_OK;
175
0
    }
176
177
    O3DGCErrorCode    CompressedTriangleFans::SaveIntACEGC(const Vector<long> & data,
178
                                                            const unsigned long M,
179
                                                            BinaryStream & bstream) 
180
0
    {
181
0
        unsigned long start = bstream.GetSize();
182
0
        const unsigned int NMAX = data.GetSize() * 8 + 100;
183
0
        const unsigned long size       = data.GetSize();
184
0
        long minValue = 0;
185
0
        bstream.WriteUInt32Bin(0);
186
0
        bstream.WriteUInt32Bin(size);
187
0
        if (size > 0)
188
0
        {
189
#ifdef DEBUG_VERBOSE
190
            printf("-----------\nsize %i, start %i\n", size, start);
191
            fprintf(g_fileDebugTF, "-----------\nsize %i, start %i\n", size, start);
192
#endif //DEBUG_VERBOSE
193
0
            for(unsigned long i = 0; i < size; ++i)
194
0
            {
195
0
                if (minValue > data[i]) 
196
0
                {
197
0
                    minValue = data[i];
198
0
                }
199
#ifdef DEBUG_VERBOSE
200
                printf("%i\t%i\n", i, data[i]);
201
                fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
202
#endif //DEBUG_VERBOSE
203
0
            }
204
0
            bstream.WriteUInt32Bin(minValue + O3DGC_MAX_LONG);
205
0
            if ( m_sizeBufferAC < NMAX )
206
0
            {
207
0
                delete [] m_bufferAC;
208
0
                m_sizeBufferAC = NMAX;
209
0
                m_bufferAC     = new unsigned char [m_sizeBufferAC];
210
0
            }
211
0
            Arithmetic_Codec ace;
212
0
            ace.set_buffer(NMAX, m_bufferAC);
213
0
            ace.start_encoder();
214
0
            Adaptive_Data_Model mModelValues(M+2);
215
0
            Static_Bit_Model bModel0;
216
0
            Adaptive_Bit_Model bModel1;
217
0
            unsigned long value;
218
0
            for(unsigned long i = 0; i < size; ++i)
219
0
            {
220
0
                value = data[i]-minValue;
221
0
                if (value < M) 
222
0
                {
223
0
                    ace.encode(value, mModelValues);
224
0
                }
225
0
                else 
226
0
                {
227
0
                    ace.encode(M, mModelValues);
228
0
                    ace.ExpGolombEncode(value-M, 0, bModel0, bModel1);
229
0
                }
230
0
            }
231
0
            unsigned long encodedBytes = ace.stop_encoder();
232
0
            for(unsigned long i = 0; i < encodedBytes; ++i)
233
0
            {
234
0
                bstream.WriteUChar8Bin(m_bufferAC[i]);
235
0
            }
236
0
        }
237
0
        bstream.WriteUInt32Bin(start, bstream.GetSize() - start);
238
0
        return O3DGC_OK;
239
0
    }
240
    O3DGCErrorCode    CompressedTriangleFans::Save(BinaryStream & bstream, bool encodeTrianglesOrder, O3DGCStreamType streamType) 
241
0
    {
242
#ifdef DEBUG_VERBOSE
243
        g_fileDebugTF = fopen("SaveIntACEGC_new.txt", "w");
244
#endif //DEBUG_VERBOSE
245
246
0
        if (streamType == O3DGC_STREAM_TYPE_ASCII)
247
0
        {
248
0
            SaveUIntData(m_numTFANs  , bstream);
249
0
            SaveUIntData(m_degrees   , bstream);
250
0
            SaveUIntData(m_configs   , bstream);
251
0
            SaveBinData (m_operations, bstream);
252
0
            SaveIntData (m_indices   , bstream);
253
0
            if (encodeTrianglesOrder)
254
0
            {
255
0
                SaveUIntData(m_trianglesOrder, bstream);
256
0
            }
257
0
        }
258
0
        else
259
0
        {
260
0
            SaveIntACEGC(m_numTFANs  , 4 , bstream);
261
0
            SaveIntACEGC(m_degrees   , 16, bstream);
262
0
            SaveUIntAC  (m_configs   , 10, bstream);
263
0
            SaveBinAC   (m_operations,     bstream);
264
0
            SaveIntACEGC(m_indices   , 8 , bstream);
265
0
            if (encodeTrianglesOrder)
266
0
            {
267
0
                SaveIntACEGC(m_trianglesOrder , 16, bstream);
268
0
            }
269
0
        }
270
#ifdef DEBUG_VERBOSE
271
        fclose(g_fileDebugTF);
272
#endif //DEBUG_VERBOSE
273
0
        return O3DGC_OK;
274
0
    }
275
    O3DGCErrorCode    LoadUIntData(Vector<long> & data,
276
                                  const BinaryStream & bstream,
277
                                  unsigned long & iterator) 
278
0
    {
279
0
        bstream.ReadUInt32ASCII(iterator);
280
0
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
281
0
        data.Allocate(size);
282
0
        data.Clear();
283
0
        for(unsigned long i = 0; i < size; ++i)
284
0
        {
285
0
            data.PushBack(bstream.ReadUIntASCII(iterator));
286
0
        }
287
0
        return O3DGC_OK;
288
0
    }
289
    O3DGCErrorCode    LoadIntData(Vector<long> & data,
290
                                  const BinaryStream & bstream,
291
                                  unsigned long & iterator) 
292
0
    {
293
0
        bstream.ReadUInt32ASCII(iterator);
294
0
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
295
0
        data.Allocate(size);
296
0
        data.Clear();
297
0
        for(unsigned long i = 0; i < size; ++i)
298
0
        {
299
0
            data.PushBack(bstream.ReadIntASCII(iterator));
300
0
        }
301
0
        return O3DGC_OK;
302
0
    }
303
    O3DGCErrorCode    LoadBinData(Vector<long> & data,
304
                                  const BinaryStream & bstream,
305
                                  unsigned long & iterator) 
306
0
    {
307
0
        bstream.ReadUInt32ASCII(iterator);
308
0
        const unsigned long size = bstream.ReadUInt32ASCII(iterator);
309
0
        long symbol;
310
0
        data.Allocate(size * O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
311
0
        data.Clear();
312
0
        for(unsigned long i = 0; i < size;)
313
0
        {
314
0
            symbol = bstream.ReadUCharASCII(iterator);
315
0
            for(unsigned long h = 0; h < O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0; ++h)
316
0
            {
317
0
                data.PushBack(symbol & 1);
318
0
                symbol >>= 1;
319
0
                ++i;
320
0
            }
321
0
        }
322
0
        return O3DGC_OK;
323
0
    }
324
    O3DGCErrorCode    LoadUIntAC(Vector<long> & data,
325
                                 const unsigned long M,
326
                                 const BinaryStream & bstream,
327
                                 unsigned long & iterator) 
328
0
    {
329
0
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
330
0
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
331
0
        if (size == 0)
332
0
        {
333
0
            return O3DGC_OK;
334
0
        }
335
0
        long minValue   = bstream.ReadUInt32Bin(iterator);
336
0
        unsigned char * buffer = 0;
337
0
        bstream.GetBuffer(iterator, buffer);
338
0
        iterator += sizeSize;
339
0
        data.Allocate(size);
340
0
        Arithmetic_Codec acd;
341
0
        acd.set_buffer(sizeSize, buffer);
342
0
        acd.start_decoder();
343
0
        Adaptive_Data_Model mModelValues(M+1);
344
#ifdef DEBUG_VERBOSE
345
        printf("-----------\nsize %i\n", size);
346
        fprintf(g_fileDebugTF, "size %i\n", size);
347
#endif //DEBUG_VERBOSE
348
0
        for(unsigned long i = 0; i < size; ++i)
349
0
        {
350
0
            data.PushBack(acd.decode(mModelValues)+minValue);
351
#ifdef DEBUG_VERBOSE
352
            printf("%i\t%i\n", i, data[i]);
353
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
354
#endif //DEBUG_VERBOSE
355
0
        }
356
0
        return O3DGC_OK;
357
0
    }
358
    O3DGCErrorCode    LoadIntACEGC(Vector<long> & data,
359
                                   const unsigned long M,
360
                                   const BinaryStream & bstream,
361
                                   unsigned long & iterator) 
362
0
    {
363
0
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 12;
364
0
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
365
0
        if (size == 0)
366
0
        {
367
0
            return O3DGC_OK;
368
0
        }
369
0
        long minValue   = bstream.ReadUInt32Bin(iterator) - O3DGC_MAX_LONG;
370
0
        unsigned char * buffer = 0;
371
0
        bstream.GetBuffer(iterator, buffer);
372
0
        iterator += sizeSize;
373
0
        data.Allocate(size);
374
0
        Arithmetic_Codec acd;
375
0
        acd.set_buffer(sizeSize, buffer);
376
0
        acd.start_decoder();
377
0
        Adaptive_Data_Model mModelValues(M+2);
378
0
        Static_Bit_Model bModel0;
379
0
        Adaptive_Bit_Model bModel1;
380
0
        unsigned long value;
381
382
#ifdef DEBUG_VERBOSE
383
        printf("-----------\nsize %i\n", size);
384
        fprintf(g_fileDebugTF, "size %i\n", size);
385
#endif //DEBUG_VERBOSE
386
0
        for(unsigned long i = 0; i < size; ++i)
387
0
        {
388
0
            value = acd.decode(mModelValues);
389
0
            if ( value == M)
390
0
            {
391
0
                value += acd.ExpGolombDecode(0, bModel0, bModel1);
392
0
            }
393
0
            data.PushBack(value + minValue);
394
#ifdef DEBUG_VERBOSE
395
            printf("%i\t%i\n", i, data[i]);
396
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
397
#endif //DEBUG_VERBOSE
398
0
        }
399
#ifdef DEBUG_VERBOSE
400
        fflush(g_fileDebugTF);
401
#endif //DEBUG_VERBOSE
402
0
        return O3DGC_OK;
403
0
    }
404
    O3DGCErrorCode    LoadBinAC(Vector<long> & data,
405
                                const BinaryStream & bstream,
406
                                unsigned long & iterator) 
407
0
    {
408
0
        unsigned long sizeSize = bstream.ReadUInt32Bin(iterator) - 8;
409
0
        unsigned long size     = bstream.ReadUInt32Bin(iterator);
410
0
        if (size == 0)
411
0
        {
412
0
            return O3DGC_OK;
413
0
        }
414
0
        unsigned char * buffer = 0;
415
0
        bstream.GetBuffer(iterator, buffer);
416
0
        iterator += sizeSize;
417
0
        data.Allocate(size);
418
0
        Arithmetic_Codec acd;
419
0
        acd.set_buffer(sizeSize, buffer);
420
0
        acd.start_decoder();
421
0
        Adaptive_Bit_Model bModel;
422
#ifdef DEBUG_VERBOSE
423
        printf("-----------\nsize %i\n", size);
424
        fprintf(g_fileDebugTF, "size %i\n", size);
425
#endif //DEBUG_VERBOSE
426
0
        for(unsigned long i = 0; i < size; ++i)
427
0
        {
428
0
            data.PushBack(acd.decode(bModel));
429
#ifdef DEBUG_VERBOSE
430
            printf("%i\t%i\n", i, data[i]);
431
            fprintf(g_fileDebugTF, "%i\t%i\n", i, data[i]);
432
#endif //DEBUG_VERBOSE
433
0
        }
434
0
        return O3DGC_OK;
435
0
    }
436
    O3DGCErrorCode    CompressedTriangleFans::Load(const BinaryStream & bstream,
437
                                                   unsigned long & iterator, 
438
                                                   bool decodeTrianglesOrder,
439
                                                   O3DGCStreamType streamType) 
440
0
    {
441
#ifdef DEBUG_VERBOSE
442
        g_fileDebugTF = fopen("Load_new.txt", "w");
443
#endif //DEBUG_VERBOSE
444
0
        if (streamType == O3DGC_STREAM_TYPE_ASCII)
445
0
        {
446
0
            LoadUIntData(m_numTFANs  , bstream, iterator);
447
0
            LoadUIntData(m_degrees   , bstream, iterator);
448
0
            LoadUIntData(m_configs   , bstream, iterator);
449
0
            LoadBinData (m_operations, bstream, iterator);
450
0
            LoadIntData (m_indices   , bstream, iterator);
451
0
            if (decodeTrianglesOrder)
452
0
            {
453
0
                LoadUIntData(m_trianglesOrder , bstream, iterator);
454
0
            }
455
0
        }
456
0
        else
457
0
        {
458
0
            LoadIntACEGC(m_numTFANs  , 4 , bstream, iterator);
459
0
            LoadIntACEGC(m_degrees   , 16, bstream, iterator);
460
0
            LoadUIntAC  (m_configs   , 10, bstream, iterator);
461
0
            LoadBinAC   (m_operations,     bstream, iterator);
462
0
            LoadIntACEGC(m_indices   , 8 , bstream, iterator);
463
0
            if (decodeTrianglesOrder)
464
0
            {
465
0
                LoadIntACEGC(m_trianglesOrder , 16, bstream, iterator);
466
0
            }
467
0
        }
468
469
#ifdef DEBUG_VERBOSE
470
        fclose(g_fileDebugTF);
471
#endif //DEBUG_VERBOSE
472
0
        return O3DGC_OK;
473
0
    }
474
}
475