[go: up one dir, main page]

File: OGroup.cpp

package info (click to toggle)
field3d 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,500 kB
  • ctags: 3,213
  • sloc: cpp: 25,268; ansic: 2,577; python: 222; sh: 72; makefile: 25
file content (329 lines) | stat: -rw-r--r-- 8,820 bytes parent folder | download | duplicates (3)
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
//-*****************************************************************************
//
// Copyright (c) 2013,
//  Sony Pictures Imageworks Inc. and
//  Industrial Light & Magic, a division of Lucasfilm Entertainment Company Ltd.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//-*****************************************************************************

#include "OGroup.h"
#include "OArchive.h"
#include "OData.h"
#include "OStream.h"

namespace Alembic {
namespace Ogawa {
namespace ALEMBIC_VERSION_NS {

typedef std::pair< OGroupPtr, Alembic::Util::uint64_t > ParentPair;
typedef std::vector< ParentPair > ParentPairVec;

class OGroup::PrivateData
{
public:
    PrivateData() {};
    ~PrivateData() {};

    OStreamPtr stream;

    // used before freeze
    ParentPairVec parents;

    // used before and after freeze
    std::vector<Alembic::Util::uint64_t> childVec;

    // set after freeze
    Alembic::Util::uint64_t pos;
};

OGroup::OGroup(OGroupPtr iParent, Alembic::Util::uint64_t iIndex)
    : mData(new OGroup::PrivateData())
{
    mData->stream = iParent->mData->stream;
    mData->parents.push_back( ParentPair(iParent, iIndex) );
    mData->pos = INVALID_GROUP;
}

OGroup::OGroup(OStreamPtr iStream)
    : mData(new OGroup::PrivateData())
{
    mData->stream = iStream;
    mData->parents.push_back(ParentPair(OGroupPtr(), 0));
    mData->pos = INVALID_GROUP;
}

OGroup::~OGroup()
{
    freeze();
}

OGroupPtr OGroup::addGroup()
{
    OGroupPtr child;
    if (!isFrozen())
    {
        mData->childVec.push_back(0);
        child.reset(new OGroup(shared_from_this(), mData->childVec.size() - 1));
    }
    return child;
}

ODataPtr OGroup::createData(Alembic::Util::uint64_t iSize, const void * iData)
{
    ODataPtr child;
    if (isFrozen())
    {
        return child;
    }

    if (iSize == 0)
    {
        mData->childVec.push_back(EMPTY_DATA);
        child.reset(new OData());
        return child;
    }

    Alembic::Util::uint64_t pos = mData->stream->getAndSeekEndPos();

    Alembic::Util::uint64_t size = iSize;
    mData->stream->write(&size, 8);
    mData->stream->write(iData, iSize);

    child.reset(new OData(mData->stream, pos, iSize));

    return child;
}

ODataPtr OGroup::addData(Alembic::Util::uint64_t iSize, const void * iData)
{
    ODataPtr child = OGroup::createData(iSize, iData);
    if (child)
    {
        // flip top bit for data so we can easily distinguish between it and
        // a group
        mData->childVec.push_back(child->getPos() | 0x8000000000000000ULL);
    }
    return child;
}

ODataPtr OGroup::createData(Alembic::Util::uint64_t iNumData,
                            const Alembic::Util::uint64_t * iSizes,
                            const void ** iDatas)
{
    ODataPtr child;
    if (isFrozen())
    {
        return child;
    }

    Alembic::Util::uint64_t totalSize = 0;
    for (Alembic::Util::uint64_t i = 0; i < iNumData; ++i)
    {
        totalSize += iSizes[i];
    }

    if (totalSize == 0)
    {
        mData->childVec.push_back(EMPTY_DATA);
        child.reset(new OData());
        return child;
    }

    Alembic::Util::uint64_t pos = mData->stream->getAndSeekEndPos();

    mData->stream->write(&totalSize, 8);
    for (Alembic::Util::uint64_t i = 0; i < iNumData; ++i)
    {
        Alembic::Util::uint64_t size = iSizes[i];
        if (size != 0)
        {
            mData->stream->write(iDatas[i], size);
        }
    }

    child.reset(new OData(mData->stream, pos, totalSize));

    return child;
}

ODataPtr OGroup::addData(Alembic::Util::uint64_t iNumData,
                         const Alembic::Util::uint64_t * iSizes,
                         const void ** iDatas)
{
    ODataPtr child = createData(iNumData, iSizes, iDatas);
    if (child)
    {
        // flip top bit for data so we can easily distinguish between it and
        // a group
        mData->childVec.push_back(child->getPos() | 0x8000000000000000ULL);
    }
    return child;
}

void OGroup::addData(ODataPtr iData)
{
    if (!isFrozen())
    {
        mData->childVec.push_back(iData->getPos() | 0x8000000000000000ULL);
    }
}

void OGroup::addGroup(OGroupPtr iGroup)
{
    if (!isFrozen())
    {
        if (iGroup->isFrozen())
        {
            mData->childVec.push_back(iGroup->mData->pos);
        }
        else
        {
            mData->childVec.push_back(EMPTY_GROUP);
            iGroup->mData->parents.push_back(
                ParentPair(shared_from_this(), mData->childVec.size() - 1));
        }
    }
}

void OGroup::addEmptyGroup()
{
    if (!isFrozen())
    {
        mData->childVec.push_back(EMPTY_GROUP);
    }
}

void OGroup::addEmptyData()
{
    if (!isFrozen())
    {
        mData->childVec.push_back(EMPTY_DATA);
    }
}

// no more children can be added, commit to the stream
void OGroup::freeze()
{
    // bail if we've already done this work
    if (isFrozen())
    {
        return;
    }

    // we ended up not adding any children, so no need to commit this group
    // to disk, use empty group instead
    if (mData->childVec.empty())
    {
        mData->pos = 0;
    }
    else
    {
        mData->pos = mData->stream->getAndSeekEndPos();
        Alembic::Util::uint64_t size = mData->childVec.size();
        mData->stream->write(&size, 8);
        mData->stream->write(&mData->childVec.front(), size*8);
    }

    // go through and update each of the parents
    ParentPairVec::iterator it;
    for(it = mData->parents.begin(); it != mData->parents.end(); ++it)
    {
        // special group owned by the archive
        if (!it->first && it->second == 0)
        {
            mData->stream->seek(8);
            mData->stream->write(&mData->pos, 8);
            continue;
        }
        else if (it->first->isFrozen())
        {
            mData->stream->seek(it->first->mData->pos + (it->second + 1) * 8);
            mData->stream->write(&mData->pos, 8);
        }
        it->first->mData->childVec[it->second] = mData->pos;
    }

    mData->parents.clear();

}

bool OGroup::isFrozen()
{
    return mData->pos != INVALID_GROUP;
}

Alembic::Util::uint64_t OGroup::getNumChildren() const
{
    return mData->childVec.size();
}

bool OGroup::isChildGroup(Alembic::Util::uint64_t iIndex) const
{
    return (iIndex < mData->childVec.size() &&
            (mData->childVec[iIndex] & EMPTY_DATA) == 0);
}

bool OGroup::isChildData(Alembic::Util::uint64_t iIndex) const
{
    return (iIndex < mData->childVec.size() &&
            (mData->childVec[iIndex] & EMPTY_DATA) != 0);
}

bool OGroup::isChildEmptyGroup(Alembic::Util::uint64_t iIndex) const
{
    return (iIndex < mData->childVec.size() &&
            mData->childVec[iIndex] == EMPTY_GROUP);
}

bool OGroup::isChildEmptyData(Alembic::Util::uint64_t iIndex) const
{
    return (iIndex < mData->childVec.size() &&
        mData->childVec[iIndex] == EMPTY_DATA);
}

void OGroup::replaceData(Alembic::Util::uint64_t iIndex, ODataPtr iData)
{
    if (!isChildData(iIndex))
    {
        return;
    }

    Alembic::Util::uint64_t pos = iData->getPos() | 0x8000000000000000ULL;
    if (isFrozen())
    {
        mData->stream->seek(mData->pos + (iIndex + 1) * 8);
        mData->stream->write(&pos, 8);
    }
    mData->childVec[iIndex] = pos;
}

} // End namespace ALEMBIC_VERSION_NS
} // End namespace Ogawa
} // End namespace Alembic