summaryrefslogtreecommitdiff
path: root/thirdparty/assimp/code/FBXExportProperty.cpp
blob: 9981d6b1c6d2d4fcae50775b5d55a14535fd292a (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
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2019, assimp team

All rights reserved.

Redistribution and use of this software 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 the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.

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.

----------------------------------------------------------------------
*/
#ifndef ASSIMP_BUILD_NO_EXPORT
#ifndef ASSIMP_BUILD_NO_FBX_EXPORTER

#include "FBXExportProperty.h"

#include <assimp/StreamWriter.h> // StreamWriterLE
#include <assimp/Exceptional.h> // DeadlyExportError

#include <string>
#include <vector>
#include <ostream>
#include <locale>
#include <sstream> // ostringstream


// constructors for single element properties

FBX::Property::Property(bool v)
    : type('C'), data(1)
{
    data = {uint8_t(v)};
}

FBX::Property::Property(int16_t v) : type('Y'), data(2)
{
    uint8_t* d = data.data();
    (reinterpret_cast<int16_t*>(d))[0] = v;
}

FBX::Property::Property(int32_t v) : type('I'), data(4)
{
    uint8_t* d = data.data();
    (reinterpret_cast<int32_t*>(d))[0] = v;
}

FBX::Property::Property(float v) : type('F'), data(4)
{
    uint8_t* d = data.data();
    (reinterpret_cast<float*>(d))[0] = v;
}

FBX::Property::Property(double v) : type('D'), data(8)
{
    uint8_t* d = data.data();
    (reinterpret_cast<double*>(d))[0] = v;
}

FBX::Property::Property(int64_t v) : type('L'), data(8)
{
    uint8_t* d = data.data();
    (reinterpret_cast<int64_t*>(d))[0] = v;
}


// constructors for array-type properties

FBX::Property::Property(const char* c, bool raw)
    : Property(std::string(c), raw)
{}

// strings can either be saved as "raw" (R) data, or "string" (S) data
FBX::Property::Property(const std::string& s, bool raw)
    : type(raw ? 'R' : 'S'), data(s.size())
{
    for (size_t i = 0; i < s.size(); ++i) {
        data[i] = uint8_t(s[i]);
    }
}

FBX::Property::Property(const std::vector<uint8_t>& r)
    : type('R'), data(r)
{}

FBX::Property::Property(const std::vector<int32_t>& va)
    : type('i'), data(4*va.size())
{
    int32_t* d = reinterpret_cast<int32_t*>(data.data());
    for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
}

FBX::Property::Property(const std::vector<int64_t>& va)
    : type('l'), data(8*va.size())
{
    int64_t* d = reinterpret_cast<int64_t*>(data.data());
    for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
}

FBX::Property::Property(const std::vector<float>& va)
    : type('f'), data(4*va.size())
{
    float* d = reinterpret_cast<float*>(data.data());
    for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
}

FBX::Property::Property(const std::vector<double>& va)
    : type('d'), data(8*va.size())
{
    double* d = reinterpret_cast<double*>(data.data());
    for (size_t i = 0; i < va.size(); ++i) { d[i] = va[i]; }
}

FBX::Property::Property(const aiMatrix4x4& vm)
    : type('d'), data(8*16)
{
    double* d = reinterpret_cast<double*>(data.data());
    for (unsigned int c = 0; c < 4; ++c) {
        for (unsigned int r = 0; r < 4; ++r) {
            d[4*c+r] = vm[r][c];
        }
    }
}

// public member functions

size_t FBX::Property::size()
{
    switch (type) {
    case 'C': case 'Y': case 'I': case 'F': case 'D': case 'L':
        return data.size() + 1;
    case 'S': case 'R':
        return data.size() + 5;
    case 'i': case 'd':
        return data.size() + 13;
    default:
        throw DeadlyExportError("Requested size on property of unknown type");
    }
}

void FBX::Property::DumpBinary(Assimp::StreamWriterLE &s)
{
    s.PutU1(type);
    uint8_t* d = data.data();
    size_t N;
    switch (type) {
    case 'C': s.PutU1(*(reinterpret_cast<uint8_t*>(d))); return;
    case 'Y': s.PutI2(*(reinterpret_cast<int16_t*>(d))); return;
    case 'I': s.PutI4(*(reinterpret_cast<int32_t*>(d))); return;
    case 'F': s.PutF4(*(reinterpret_cast<float*>(d))); return;
    case 'D': s.PutF8(*(reinterpret_cast<double*>(d))); return;
    case 'L': s.PutI8(*(reinterpret_cast<int64_t*>(d))); return;
    case 'S':
    case 'R':
        s.PutU4(uint32_t(data.size()));
        for (size_t i = 0; i < data.size(); ++i) { s.PutU1(data[i]); }
        return;
    case 'i':
        N = data.size() / 4;
        s.PutU4(uint32_t(N)); // number of elements
        s.PutU4(0); // no encoding (1 would be zip-compressed)
        // TODO: compress if large?
        s.PutU4(uint32_t(data.size())); // data size
        for (size_t i = 0; i < N; ++i) {
            s.PutI4((reinterpret_cast<int32_t*>(d))[i]);
        }
        return;
    case 'l':
        N = data.size() / 8;
        s.PutU4(uint32_t(N)); // number of elements
        s.PutU4(0); // no encoding (1 would be zip-compressed)
        // TODO: compress if large?
        s.PutU4(uint32_t(data.size())); // data size
        for (size_t i = 0; i < N; ++i) {
            s.PutI8((reinterpret_cast<int64_t*>(d))[i]);
        }
        return;
    case 'f':
        N = data.size() / 4;
        s.PutU4(uint32_t(N)); // number of elements
        s.PutU4(0); // no encoding (1 would be zip-compressed)
        // TODO: compress if large?
        s.PutU4(uint32_t(data.size())); // data size
        for (size_t i = 0; i < N; ++i) {
            s.PutF4((reinterpret_cast<float*>(d))[i]);
        }
        return;
    case 'd':
        N = data.size() / 8;
        s.PutU4(uint32_t(N)); // number of elements
        s.PutU4(0); // no encoding (1 would be zip-compressed)
        // TODO: compress if large?
        s.PutU4(uint32_t(data.size())); // data size
        for (size_t i = 0; i < N; ++i) {
            s.PutF8((reinterpret_cast<double*>(d))[i]);
        }
        return;
    default:
        std::ostringstream err;
        err << "Tried to dump property with invalid type '";
        err << type << "'!";
        throw DeadlyExportError(err.str());
    }
}

void FBX::Property::DumpAscii(Assimp::StreamWriterLE &outstream, int indent)
{
    std::ostringstream ss;
    ss.imbue(std::locale::classic());
    ss.precision(15); // this seems to match official FBX SDK exports
    DumpAscii(ss, indent);
    outstream.PutString(ss.str());
}

void FBX::Property::DumpAscii(std::ostream& s, int indent)
{
    // no writing type... or anything. just shove it into the stream.
    uint8_t* d = data.data();
    size_t N;
    size_t swap = data.size();
    size_t count = 0;
    switch (type) {
    case 'C':
        if (*(reinterpret_cast<uint8_t*>(d))) { s << 'T'; }
        else { s << 'F'; }
        return;
    case 'Y': s << *(reinterpret_cast<int16_t*>(d)); return;
    case 'I': s << *(reinterpret_cast<int32_t*>(d)); return;
    case 'F': s << *(reinterpret_cast<float*>(d)); return;
    case 'D': s << *(reinterpret_cast<double*>(d)); return;
    case 'L': s << *(reinterpret_cast<int64_t*>(d)); return;
    case 'S':
        // first search to see if it has "\x00\x01" in it -
        // which separates fields which are reversed in the ascii version.
        // yeah.
        // FBX, yeah.
        for (size_t i = 0; i < data.size(); ++i) {
            if (data[i] == '\0') {
                swap = i;
                break;
            }
        }
    case 'R':
        s << '"';
        // we might as well check this now,
        // probably it will never happen
        for (size_t i = 0; i < data.size(); ++i) {
            char c = data[i];
            if (c == '"') {
                throw runtime_error("can't handle quotes in property string");
            }
        }
        // first write the SWAPPED member (if any)
        for (size_t i = swap + 2; i < data.size(); ++i) {
            char c = data[i];
            s << c;
        }
        // then a separator
        if (swap != data.size()) {
            s << "::";
        }
        // then the initial member
        for (size_t i = 0; i < swap; ++i) {
            char c = data[i];
            s << c;
        }
        s << '"';
        return;
    case 'i':
        N = data.size() / 4; // number of elements
        s << '*' << N << " {\n";
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
        s << "a: ";
        for (size_t i = 0; i < N; ++i) {
            if (i > 0) { s << ','; }
            if (count++ > 120) { s << '\n'; count = 0; }
            s << (reinterpret_cast<int32_t*>(d))[i];
        }
        s << '\n';
        for (int i = 0; i < indent; ++i) { s << '\t'; }
        s << "} ";
        return;
    case 'l':
        N = data.size() / 8;
        s << '*' << N << " {\n";
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
        s << "a: ";
        for (size_t i = 0; i < N; ++i) {
            if (i > 0) { s << ','; }
            if (count++ > 120) { s << '\n'; count = 0; }
            s << (reinterpret_cast<int64_t*>(d))[i];
        }
        s << '\n';
        for (int i = 0; i < indent; ++i) { s << '\t'; }
        s << "} ";
        return;
    case 'f':
        N = data.size() / 4;
        s << '*' << N << " {\n";
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
        s << "a: ";
        for (size_t i = 0; i < N; ++i) {
            if (i > 0) { s << ','; }
            if (count++ > 120) { s << '\n'; count = 0; }
            s << (reinterpret_cast<float*>(d))[i];
        }
        s << '\n';
        for (int i = 0; i < indent; ++i) { s << '\t'; }
        s << "} ";
        return;
    case 'd':
        N = data.size() / 8;
        s << '*' << N << " {\n";
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
        s << "a: ";
        // set precision to something that can handle doubles
        s.precision(15);
        for (size_t i = 0; i < N; ++i) {
            if (i > 0) { s << ','; }
            if (count++ > 120) { s << '\n'; count = 0; }
            s << (reinterpret_cast<double*>(d))[i];
        }
        s << '\n';
        for (int i = 0; i < indent; ++i) { s << '\t'; }
        s << "} ";
        return;
    default:
        std::ostringstream err;
        err << "Tried to dump property with invalid type '";
        err << type << "'!";
        throw runtime_error(err.str());
    }
}

#endif // ASSIMP_BUILD_NO_FBX_EXPORTER
#endif // ASSIMP_BUILD_NO_EXPORT