summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/assimp/contrib/Open3DGC/o3dgcTriangleFans.h
blob: 861836428d124390bb788a6a0d8a0b223ce7ae9b (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
/*
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_TRIANGLE_FANS_H
#define O3DGC_TRIANGLE_FANS_H

#include "o3dgcCommon.h"
#include "o3dgcVector.h"
#include "o3dgcBinaryStream.h"


namespace o3dgc
{
    const long O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER = 128;
    const long O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER          = 8;

    class CompressedTriangleFans
    {
    public:    
        //! Constructor.
                                    CompressedTriangleFans(void)
                                    {
                                        m_streamType   = O3DGC_STREAM_TYPE_UNKOWN;
                                        m_bufferAC     = 0;
                                        m_sizeBufferAC = 0;
                                    };
        //! Destructor.
                                    ~CompressedTriangleFans(void) 
                                    {
                                        delete [] m_bufferAC;
                                    };
        O3DGCStreamType       GetStreamType() const { return m_streamType; }
        void                        SetStreamType(O3DGCStreamType streamType) { m_streamType = streamType; }

        O3DGCErrorCode              Allocate(long numVertices, long numTriangles)
                                    {
                                        assert(numVertices > 0);
                                        m_numTFANs.Allocate(numVertices);
                                        m_degrees.Allocate(2*numVertices);
                                        m_configs.Allocate(2*numVertices);
                                        m_operations.Allocate(2*numVertices);
                                        m_indices.Allocate(2*numVertices);
                                        m_trianglesOrder.Allocate(numTriangles);
                                        Clear();
                                        return O3DGC_OK;
                                    }
        O3DGCErrorCode              PushNumTFans(long numTFans)
                                    {
                                         m_numTFANs.PushBack(numTFans);
                                        return O3DGC_OK;
                                    }
        long                        ReadNumTFans(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_numTFANs.GetSize());
                                        return m_numTFANs[iterator++];
                                    }
        O3DGCErrorCode              PushDegree(long degree)
                                    {
                                        m_degrees.PushBack(degree);
                                        return O3DGC_OK;
                                    }
        long                        ReadDegree(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_degrees.GetSize());
                                        return m_degrees[iterator++];
                                    }
        O3DGCErrorCode              PushConfig(long config)
                                    {
                                        m_configs.PushBack(config);
                                        return O3DGC_OK;
                                    }
        long                        ReadConfig(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_configs.GetSize());
                                        return m_configs[iterator++];
                                    }
        O3DGCErrorCode              PushOperation(long op)
                                    {
                                        m_operations.PushBack(op);
                                        return O3DGC_OK;
                                    }
        long                        ReadOperation(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_operations.GetSize());
                                        return m_operations[iterator++];
                                    }
        O3DGCErrorCode              PushIndex(long index)
                                    {
                                        m_indices.PushBack(index);
                                        return O3DGC_OK;
                                    }
        long                        ReadIndex(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_indices.GetSize());
                                        return m_indices[iterator++];
                                    }
        O3DGCErrorCode              PushTriangleIndex(long index)
                                    {
                                        m_trianglesOrder.PushBack(IntToUInt(index));
                                        return O3DGC_OK;
                                    }
        long                        ReadTriangleIndex(unsigned long & iterator) const
                                    {
                                        assert(iterator < m_trianglesOrder.GetSize());
                                        return UIntToInt(m_trianglesOrder[iterator++]);
                                    }
        O3DGCErrorCode              Clear()
                                    {
                                        m_numTFANs.Clear();
                                        m_degrees.Clear();
                                        m_configs.Clear();
                                        m_operations.Clear();
                                        m_indices.Clear();
                                        return O3DGC_OK;
                                    }
        O3DGCErrorCode              Save(BinaryStream & bstream,
                                         bool encodeTrianglesOrder,
                                         O3DGCStreamType streamType);
        O3DGCErrorCode              Load(const BinaryStream & bstream, 
                                         unsigned long & iterator, 
                                         bool decodeTrianglesOrder,
                                         O3DGCStreamType streamType);

    private:
        O3DGCErrorCode              SaveBinAC(const Vector<long> & data,
                                            BinaryStream & bstream);
        O3DGCErrorCode              SaveUIntAC(const Vector<long> & data,
                                             const unsigned long M,
                                               BinaryStream & bstream);
        O3DGCErrorCode              SaveIntACEGC(const Vector<long> & data,
                                                 const unsigned long M,
                                                 BinaryStream & bstream);

        Vector<long>                m_numTFANs;
        Vector<long>                m_degrees;
        Vector<long>                m_configs;
        Vector<long>                m_operations;
        Vector<long>                m_indices;
        Vector<long>                m_trianglesOrder;
        unsigned char *             m_bufferAC;
        unsigned long               m_sizeBufferAC;
        O3DGCStreamType       m_streamType;
    };

    //! 
    class TriangleFans
    {
    public:    
        //! Constructor.
                                    TriangleFans(long sizeTFAN     = O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER, 
                                                 long verticesSize = O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER)
                                    {
                                        assert(sizeTFAN     > 0);
                                        assert(verticesSize > 0);
                                        m_numTFANs              = 0;
                                        m_numVertices           = 0;
                                        m_verticesAllocatedSize = verticesSize;
                                        m_sizeTFANAllocatedSize = sizeTFAN;
                                        m_sizeTFAN              = new long [m_sizeTFANAllocatedSize];
                                        m_vertices              = new long [m_verticesAllocatedSize];
                                    };
        //! Destructor.
                                    ~TriangleFans(void)
                                    {
                                        delete [] m_vertices;
                                        delete [] m_sizeTFAN;
                                    };

        O3DGCErrorCode                Allocate(long sizeTFAN, long verticesSize)
                                    {
                                        assert(sizeTFAN     > 0);
                                        assert(verticesSize > 0);
                                        m_numTFANs    = 0;
                                        m_numVertices = 0;
                                        if (m_verticesAllocatedSize < verticesSize)
                                        {
                                            delete [] m_vertices;
                                            m_verticesAllocatedSize = verticesSize;
                                            m_vertices              = new long [m_verticesAllocatedSize];
                                        }
                                        if (m_sizeTFANAllocatedSize < sizeTFAN)
                                        {
                                            delete [] m_sizeTFAN;
                                            m_sizeTFANAllocatedSize = sizeTFAN;
                                            m_sizeTFAN              = new long [m_sizeTFANAllocatedSize];
                                        }
                                        return O3DGC_OK;
                                    };
        O3DGCErrorCode                Clear()
                                    {
                                        m_numTFANs    = 0;
                                        m_numVertices = 0;
                                        return O3DGC_OK;
                                    }
        O3DGCErrorCode                AddVertex(long vertex) 
                                    {
                                        assert(m_numTFANs    >= 0);
                                        assert(m_numTFANs    <  m_sizeTFANAllocatedSize);
                                        assert(m_numVertices >= 0);
                                        ++m_numVertices;
                                        if (m_numVertices == m_verticesAllocatedSize)
                                        {
                                            m_verticesAllocatedSize *= 2;
                                            long * tmp = m_vertices;
                                            m_vertices = new long [m_verticesAllocatedSize];
                                            memcpy(m_vertices, tmp, sizeof(long) * m_numVertices);
                                            delete [] tmp;
                                        }
                                        m_vertices[m_numVertices-1] = vertex;
                                        ++m_sizeTFAN[m_numTFANs-1];
                                        return O3DGC_OK;
                                    }
        O3DGCErrorCode                AddTFAN()
                                    {
                                        assert(m_numTFANs >= 0);
                                        ++m_numTFANs;
                                        if (m_numTFANs == m_sizeTFANAllocatedSize)
                                        {
                                            m_sizeTFANAllocatedSize *= 2;
                                            long * tmp = m_sizeTFAN;
                                            m_sizeTFAN = new long [m_sizeTFANAllocatedSize];
                                            memcpy(m_sizeTFAN, tmp, sizeof(long) * m_numTFANs);
                                            delete [] tmp;
                                        }
                                        m_sizeTFAN[m_numTFANs-1] = (m_numTFANs > 1) ? m_sizeTFAN[m_numTFANs-2] : 0;
                                        return O3DGC_OK;
                                    }
        long                        Begin(long tfan) const 
                                    {
                                        assert(tfan < m_numTFANs);
                                        assert(tfan >= 0);
                                        return (tfan>0)?m_sizeTFAN[tfan-1]:0;
                                    }
        long                        End(long tfan) const
                                    {
                                        assert(tfan < m_numTFANs);
                                        assert(tfan >= 0);
                                        return m_sizeTFAN[tfan];
                                    }
        long                        GetVertex(long vertex) const
                                    {
                                        assert(vertex < m_numVertices);
                                        assert(vertex >= 0);
                                        return m_vertices[vertex];
                                    }
        long                        GetTFANSize(long tfan)  const 
                                    { 
                                        return End(tfan) - Begin(tfan);
                                    }
        long                        GetNumTFANs()  const 
                                    { 
                                        return m_numTFANs;
                                    }
        long                        GetNumVertices()  const 
                                    { 
                                        return m_numVertices;
                                    }

    private:
        long                        m_verticesAllocatedSize;
        long                        m_sizeTFANAllocatedSize;
        long                        m_numTFANs;
        long                        m_numVertices;
        long *                      m_vertices;
        long *                      m_sizeTFAN;
    
    };
}
#endif // O3DGC_TRIANGLE_FANS_H