Coverage Report

Created: 2026-01-25 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/FBX/FBXExportProperty.cpp
Line
Count
Source
1
/*
2
Open Asset Import Library (assimp)
3
----------------------------------------------------------------------
4
5
Copyright (c) 2006-2026, assimp team
6
7
All rights reserved.
8
9
Redistribution and use of this software in source and binary forms,
10
with or without modification, are permitted provided that the
11
following conditions are met:
12
13
* Redistributions of source code must retain the above
14
copyright notice, this list of conditions and the
15
following disclaimer.
16
17
* Redistributions in binary form must reproduce the above
18
copyright notice, this list of conditions and the
19
following disclaimer in the documentation and/or other
20
materials provided with the distribution.
21
22
* Neither the name of the assimp team, nor the names of its
23
contributors may be used to endorse or promote products
24
derived from this software without specific prior
25
written permission of the assimp team.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39
----------------------------------------------------------------------
40
*/
41
#ifndef ASSIMP_BUILD_NO_EXPORT
42
#ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
43
44
#include "FBXExportProperty.h"
45
46
#include <assimp/StreamWriter.h> // StreamWriterLE
47
#include <assimp/Exceptional.h> // DeadlyExportError
48
49
#include <string>
50
#include <vector>
51
#include <ostream>
52
#include <locale>
53
#include <sstream> // ostringstream
54
55
namespace Assimp {
56
namespace FBX {
57
58
// constructors for single element properties
59
60
FBXExportProperty::FBXExportProperty(bool v)
61
6.83k
: type('C')
62
6.83k
, data(1, uint8_t(v)) {}
63
64
FBXExportProperty::FBXExportProperty(int16_t v)
65
0
: type('Y')
66
0
, data(2) {
67
0
    uint8_t* d = data.data();
68
0
    (reinterpret_cast<int16_t*>(d))[0] = v;
69
0
}
70
71
FBXExportProperty::FBXExportProperty(int32_t v)
72
50.0k
: type('I')
73
50.0k
, data(4) {
74
50.0k
    uint8_t* d = data.data();
75
50.0k
    (reinterpret_cast<int32_t*>(d))[0] = v;
76
50.0k
}
77
78
FBXExportProperty::FBXExportProperty(float v)
79
0
: type('F')
80
0
, data(4) {
81
0
    uint8_t* d = data.data();
82
0
    (reinterpret_cast<float*>(d))[0] = v;
83
0
}
84
85
FBXExportProperty::FBXExportProperty(double v)
86
32.8k
: type('D')
87
32.8k
, data(8) {
88
32.8k
    uint8_t* d = data.data();
89
32.8k
    (reinterpret_cast<double*>(d))[0] = v;
90
32.8k
}
91
92
FBXExportProperty::FBXExportProperty(int64_t v)
93
48.1k
: type('L')
94
48.1k
, data(8) {
95
48.1k
    uint8_t* d = data.data();
96
48.1k
    (reinterpret_cast<int64_t*>(d))[0] = v;
97
48.1k
}
98
99
// constructors for array-type properties
100
101
FBXExportProperty::FBXExportProperty(const char* c, bool raw)
102
175k
: FBXExportProperty(std::string(c), raw) {
103
    // empty
104
175k
}
105
106
// strings can either be saved as "raw" (R) data, or "string" (S) data
107
FBXExportProperty::FBXExportProperty(const std::string& s, bool raw)
108
254k
: type(raw ? 'R' : 'S')
109
254k
, data(s.size()) {
110
1.79M
    for (size_t i = 0; i < s.size(); ++i) {
111
1.53M
        data[i] = uint8_t(s[i]);
112
1.53M
    }
113
254k
}
114
115
FBXExportProperty::FBXExportProperty(const std::vector<uint8_t>& r)
116
153
: type('R')
117
153
, data(r) {
118
    // empty
119
153
}
120
121
FBXExportProperty::FBXExportProperty(const std::vector<int32_t>& va)
122
6.77k
: type('i')
123
6.77k
, data(4 * va.size() ) {
124
6.77k
    int32_t* d = reinterpret_cast<int32_t*>(data.data());
125
104k
    for (size_t i = 0; i < va.size(); ++i) {
126
98.1k
        d[i] = va[i];
127
98.1k
    }
128
6.77k
}
129
130
FBXExportProperty::FBXExportProperty(const std::vector<int64_t>& va)
131
2.61k
: type('l')
132
2.61k
, data(8 * va.size()) {
133
2.61k
    int64_t* d = reinterpret_cast<int64_t*>(data.data());
134
34.4k
    for (size_t i = 0; i < va.size(); ++i) {
135
31.8k
        d[i] = va[i];
136
31.8k
    }
137
2.61k
}
138
139
FBXExportProperty::FBXExportProperty(const std::vector<float>& va)
140
5.22k
: type('f')
141
5.22k
, data(4 * va.size()) {
142
5.22k
    float* d = reinterpret_cast<float*>(data.data());
143
47.5k
    for (size_t i = 0; i < va.size(); ++i) {
144
42.2k
        d[i] = va[i];
145
42.2k
    }
146
5.22k
}
147
148
FBXExportProperty::FBXExportProperty(const std::vector<double>& va)
149
950
: type('d')
150
950
, data(8 * va.size()) {
151
950
    double* d = reinterpret_cast<double*>(data.data());
152
15.3k
    for (size_t i = 0; i < va.size(); ++i) {
153
14.3k
        d[i] = va[i];
154
14.3k
    }
155
950
}
156
157
FBXExportProperty::FBXExportProperty(const aiMatrix4x4& vm)
158
1.90k
: type('d')
159
1.90k
, data(8 * 16) {
160
1.90k
    double* d = reinterpret_cast<double*>(data.data());
161
9.50k
    for (unsigned int c = 0; c < 4; ++c) {
162
38.0k
        for (unsigned int r = 0; r < 4; ++r) {
163
30.4k
            d[4 * c + r] = vm[r][c];
164
30.4k
        }
165
7.60k
    }
166
1.90k
}
167
168
// public member functions
169
170
0
size_t FBXExportProperty::size() {
171
0
    switch (type) {
172
0
        case 'C':
173
0
        case 'Y':
174
0
        case 'I':
175
0
        case 'F':
176
0
        case 'D':
177
0
        case 'L':
178
0
            return data.size() + 1;
179
0
        case 'S':
180
0
        case 'R':
181
0
            return data.size() + 5;
182
0
        case 'i':
183
0
        case 'd':
184
0
            return data.size() + 13;
185
0
        default:
186
0
            throw DeadlyExportError("Requested size on property of unknown type");
187
0
    }
188
0
}
189
190
409k
void FBXExportProperty::DumpBinary(Assimp::StreamWriterLE& s) {
191
409k
    s.PutU1(type);
192
409k
    uint8_t* d = data.data();
193
409k
    size_t N;
194
409k
    switch (type) {
195
6.83k
        case 'C': s.PutU1(*(reinterpret_cast<uint8_t*>(d))); return;
196
0
        case 'Y': s.PutI2(*(reinterpret_cast<int16_t*>(d))); return;
197
50.0k
        case 'I': s.PutI4(*(reinterpret_cast<int32_t*>(d))); return;
198
0
        case 'F': s.PutF4(*(reinterpret_cast<float*>(d))); return;
199
32.8k
        case 'D': s.PutF8(*(reinterpret_cast<double*>(d))); return;
200
48.1k
        case 'L': s.PutI8(*(reinterpret_cast<int64_t*>(d))); return;
201
254k
        case 'S':
202
254k
        case 'R':
203
254k
            s.PutU4(uint32_t(data.size()));
204
2.15M
            for (size_t i = 0; i < data.size(); ++i) { s.PutU1(data[i]); }
205
254k
            return;
206
6.77k
        case 'i':
207
6.77k
            N = data.size() / 4;
208
6.77k
            s.PutU4(uint32_t(N)); // number of elements
209
6.77k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
210
            // TODO: compress if large?
211
6.77k
            s.PutU4(uint32_t(data.size())); // data size
212
104k
            for (size_t i = 0; i < N; ++i) {
213
98.1k
                s.PutI4((reinterpret_cast<int32_t*>(d))[i]);
214
98.1k
            }
215
6.77k
            return;
216
2.61k
        case 'l':
217
2.61k
            N = data.size() / 8;
218
2.61k
            s.PutU4(uint32_t(N)); // number of elements
219
2.61k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
220
            // TODO: compress if large?
221
2.61k
            s.PutU4(uint32_t(data.size())); // data size
222
34.4k
            for (size_t i = 0; i < N; ++i) {
223
31.8k
                s.PutI8((reinterpret_cast<int64_t*>(d))[i]);
224
31.8k
            }
225
2.61k
            return;
226
5.22k
        case 'f':
227
5.22k
            N = data.size() / 4;
228
5.22k
            s.PutU4(uint32_t(N)); // number of elements
229
5.22k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
230
            // TODO: compress if large?
231
5.22k
            s.PutU4(uint32_t(data.size())); // data size
232
47.5k
            for (size_t i = 0; i < N; ++i) {
233
42.2k
                s.PutF4((reinterpret_cast<float*>(d))[i]);
234
42.2k
            }
235
5.22k
            return;
236
2.85k
        case 'd':
237
2.85k
            N = data.size() / 8;
238
2.85k
            s.PutU4(uint32_t(N)); // number of elements
239
2.85k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
240
            // TODO: compress if large?
241
2.85k
            s.PutU4(uint32_t(data.size())); // data size
242
47.6k
            for (size_t i = 0; i < N; ++i) {
243
44.7k
                s.PutF8((reinterpret_cast<double*>(d))[i]);
244
44.7k
            }
245
2.85k
            return;
246
0
        default:
247
0
            std::ostringstream err;
248
0
            err << "Tried to dump property with invalid type '";
249
0
            err << type << "'!";
250
0
            throw DeadlyExportError(err.str());
251
409k
    }
252
409k
}
253
254
0
void FBXExportProperty::DumpAscii(Assimp::StreamWriterLE& outstream, int indent) {
255
0
    std::ostringstream ss;
256
0
    ss.imbue(std::locale::classic());
257
0
    ss.precision(15); // this seems to match official FBX SDK exports
258
0
    DumpAscii(ss, indent);
259
0
    outstream.PutString(ss.str());
260
0
}
261
262
0
void FBXExportProperty::DumpAscii(std::ostream& s, int indent) {
263
    // no writing type... or anything. just shove it into the stream.
264
0
    uint8_t* d = data.data();
265
0
    size_t N;
266
0
    size_t swap = data.size();
267
0
    size_t count = 0;
268
0
    switch (type) {
269
0
    case 'C':
270
0
        if (*(reinterpret_cast<uint8_t*>(d))) { s << 'T'; }
271
0
        else { s << 'F'; }
272
0
        return;
273
0
    case 'Y': s << *(reinterpret_cast<int16_t*>(d)); return;
274
0
    case 'I': s << *(reinterpret_cast<int32_t*>(d)); return;
275
0
    case 'F': s << *(reinterpret_cast<float*>(d)); return;
276
0
    case 'D': s << *(reinterpret_cast<double*>(d)); return;
277
0
    case 'L': s << *(reinterpret_cast<int64_t*>(d)); return;
278
0
    case 'S':
279
        // first search to see if it has "\x00\x01" in it -
280
        // which separates fields which are reversed in the ascii version.
281
        // yeah.
282
        // FBX, yeah.
283
0
        for (size_t i = 0; i < data.size(); ++i) {
284
0
            if (data[i] == '\0') {
285
0
                swap = i;
286
0
                break;
287
0
            }
288
0
        }
289
        // assimp issue #6112; fallthrough confirmed by @mesilliac
290
0
        [[fallthrough]];
291
0
    case 'R':
292
0
        s << '"';
293
        // we might as well check this now,
294
        // probably it will never happen
295
0
        for (size_t i = 0; i < data.size(); ++i) {
296
0
            char c = data[i];
297
0
            if (c == '"') {
298
0
                throw runtime_error("can't handle quotes in property string");
299
0
            }
300
0
        }
301
        // first write the SWAPPED member (if any)
302
0
        for (size_t i = swap + 2; i < data.size(); ++i) {
303
0
            char c = data[i];
304
0
            s << c;
305
0
        }
306
        // then a separator
307
0
        if (swap != data.size()) {
308
0
            s << "::";
309
0
        }
310
        // then the initial member
311
0
        for (size_t i = 0; i < swap; ++i) {
312
0
            char c = data[i];
313
0
            s << c;
314
0
        }
315
0
        s << '"';
316
0
        return;
317
0
    case 'i':
318
0
        N = data.size() / 4; // number of elements
319
0
        s << '*' << N << " {\n";
320
0
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
321
0
        s << "a: ";
322
0
        for (size_t i = 0; i < N; ++i) {
323
0
            if (i > 0) { s << ','; }
324
0
            if (count++ > 120) { s << '\n'; count = 0; }
325
0
            s << (reinterpret_cast<int32_t*>(d))[i];
326
0
        }
327
0
        s << '\n';
328
0
        for (int i = 0; i < indent; ++i) { s << '\t'; }
329
0
        s << "} ";
330
0
        return;
331
0
    case 'l':
332
0
        N = data.size() / 8;
333
0
        s << '*' << N << " {\n";
334
0
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
335
0
        s << "a: ";
336
0
        for (size_t i = 0; i < N; ++i) {
337
0
            if (i > 0) { s << ','; }
338
0
            if (count++ > 120) { s << '\n'; count = 0; }
339
0
            s << (reinterpret_cast<int64_t*>(d))[i];
340
0
        }
341
0
        s << '\n';
342
0
        for (int i = 0; i < indent; ++i) { s << '\t'; }
343
0
        s << "} ";
344
0
        return;
345
0
    case 'f':
346
0
        N = data.size() / 4;
347
0
        s << '*' << N << " {\n";
348
0
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
349
0
        s << "a: ";
350
0
        for (size_t i = 0; i < N; ++i) {
351
0
            if (i > 0) { s << ','; }
352
0
            if (count++ > 120) { s << '\n'; count = 0; }
353
0
            s << (reinterpret_cast<float*>(d))[i];
354
0
        }
355
0
        s << '\n';
356
0
        for (int i = 0; i < indent; ++i) { s << '\t'; }
357
0
        s << "} ";
358
0
        return;
359
0
    case 'd':
360
0
        N = data.size() / 8;
361
0
        s << '*' << N << " {\n";
362
0
        for (int i = 0; i < indent + 1; ++i) { s << '\t'; }
363
0
        s << "a: ";
364
        // set precision to something that can handle doubles
365
0
        s.precision(15);
366
0
        for (size_t i = 0; i < N; ++i) {
367
0
            if (i > 0) { s << ','; }
368
0
            if (count++ > 120) { s << '\n'; count = 0; }
369
0
            s << (reinterpret_cast<double*>(d))[i];
370
0
        }
371
0
        s << '\n';
372
0
        for (int i = 0; i < indent; ++i) { s << '\t'; }
373
0
        s << "} ";
374
0
        return;
375
0
    default:
376
0
        std::ostringstream err;
377
0
        err << "Tried to dump property with invalid type '";
378
0
        err << type << "'!";
379
0
        throw runtime_error(err.str());
380
0
    }
381
0
}
382
383
} // Namespace FBX
384
} // Namespace Assimp
385
386
#endif // ASSIMP_BUILD_NO_FBX_EXPORTER
387
#endif // ASSIMP_BUILD_NO_EXPORT