Coverage Report

Created: 2026-06-15 06:20

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
539
: type('C')
62
539
, 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
11.8k
: type('I')
73
11.8k
, data(4) {
74
11.8k
    uint8_t* d = data.data();
75
11.8k
    (reinterpret_cast<int32_t*>(d))[0] = v;
76
11.8k
}
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
16.7k
: type('D')
87
16.7k
, data(8) {
88
16.7k
    uint8_t* d = data.data();
89
16.7k
    (reinterpret_cast<double*>(d))[0] = v;
90
16.7k
}
91
92
FBXExportProperty::FBXExportProperty(int64_t v)
93
18.9k
: type('L')
94
18.9k
, data(8) {
95
18.9k
    uint8_t* d = data.data();
96
18.9k
    (reinterpret_cast<int64_t*>(d))[0] = v;
97
18.9k
}
98
99
// constructors for array-type properties
100
101
FBXExportProperty::FBXExportProperty(const char* c, bool raw)
102
50.1k
: FBXExportProperty(std::string(c), raw) {
103
    // empty
104
50.1k
}
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
77.3k
: type(raw ? 'R' : 'S')
109
77.3k
, data(s.size()) {
110
477k
    for (size_t i = 0; i < s.size(); ++i) {
111
400k
        data[i] = uint8_t(s[i]);
112
400k
    }
113
77.3k
}
114
115
FBXExportProperty::FBXExportProperty(const std::vector<uint8_t>& r)
116
50
: type('R')
117
50
, data(r) {
118
    // empty
119
50
}
120
121
FBXExportProperty::FBXExportProperty(const std::vector<int32_t>& va)
122
6.59k
: type('i')
123
6.59k
, data(4 * va.size() ) {
124
6.59k
    int32_t* d = reinterpret_cast<int32_t*>(data.data());
125
173k
    for (size_t i = 0; i < va.size(); ++i) {
126
167k
        d[i] = va[i];
127
167k
    }
128
6.59k
}
129
130
FBXExportProperty::FBXExportProperty(const std::vector<int64_t>& va)
131
3.13k
: type('l')
132
3.13k
, data(8 * va.size()) {
133
3.13k
    int64_t* d = reinterpret_cast<int64_t*>(data.data());
134
27.6k
    for (size_t i = 0; i < va.size(); ++i) {
135
24.4k
        d[i] = va[i];
136
24.4k
    }
137
3.13k
}
138
139
FBXExportProperty::FBXExportProperty(const std::vector<float>& va)
140
6.26k
: type('f')
141
6.26k
, data(4 * va.size()) {
142
6.26k
    float* d = reinterpret_cast<float*>(data.data());
143
43.2k
    for (size_t i = 0; i < va.size(); ++i) {
144
37.0k
        d[i] = va[i];
145
37.0k
    }
146
6.26k
}
147
148
FBXExportProperty::FBXExportProperty(const std::vector<double>& va)
149
132
: type('d')
150
132
, data(8 * va.size()) {
151
132
    double* d = reinterpret_cast<double*>(data.data());
152
15.6k
    for (size_t i = 0; i < va.size(); ++i) {
153
15.4k
        d[i] = va[i];
154
15.4k
    }
155
132
}
156
157
FBXExportProperty::FBXExportProperty(const aiMatrix4x4& vm)
158
264
: type('d')
159
264
, data(8 * 16) {
160
264
    double* d = reinterpret_cast<double*>(data.data());
161
1.32k
    for (unsigned int c = 0; c < 4; ++c) {
162
5.28k
        for (unsigned int r = 0; r < 4; ++r) {
163
4.22k
            d[4 * c + r] = vm[r][c];
164
4.22k
        }
165
1.05k
    }
166
264
}
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
141k
void FBXExportProperty::DumpBinary(Assimp::StreamWriterLE& s) {
191
141k
    s.PutU1(type);
192
141k
    uint8_t* d = data.data();
193
141k
    size_t N;
194
141k
    switch (type) {
195
539
        case 'C': s.PutU1(*(reinterpret_cast<uint8_t*>(d))); return;
196
0
        case 'Y': s.PutI2(*(reinterpret_cast<int16_t*>(d))); return;
197
11.8k
        case 'I': s.PutI4(*(reinterpret_cast<int32_t*>(d))); return;
198
0
        case 'F': s.PutF4(*(reinterpret_cast<float*>(d))); return;
199
16.7k
        case 'D': s.PutF8(*(reinterpret_cast<double*>(d))); return;
200
18.9k
        case 'L': s.PutI8(*(reinterpret_cast<int64_t*>(d))); return;
201
77.3k
        case 'S':
202
77.3k
        case 'R':
203
77.3k
            s.PutU4(uint32_t(data.size()));
204
1.17M
            for (size_t i = 0; i < data.size(); ++i) { s.PutU1(data[i]); }
205
77.3k
            return;
206
6.59k
        case 'i':
207
6.59k
            N = data.size() / 4;
208
6.59k
            s.PutU4(uint32_t(N)); // number of elements
209
6.59k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
210
            // TODO: compress if large?
211
6.59k
            s.PutU4(uint32_t(data.size())); // data size
212
173k
            for (size_t i = 0; i < N; ++i) {
213
167k
                s.PutI4((reinterpret_cast<int32_t*>(d))[i]);
214
167k
            }
215
6.59k
            return;
216
3.13k
        case 'l':
217
3.13k
            N = data.size() / 8;
218
3.13k
            s.PutU4(uint32_t(N)); // number of elements
219
3.13k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
220
            // TODO: compress if large?
221
3.13k
            s.PutU4(uint32_t(data.size())); // data size
222
27.6k
            for (size_t i = 0; i < N; ++i) {
223
24.4k
                s.PutI8((reinterpret_cast<int64_t*>(d))[i]);
224
24.4k
            }
225
3.13k
            return;
226
6.26k
        case 'f':
227
6.26k
            N = data.size() / 4;
228
6.26k
            s.PutU4(uint32_t(N)); // number of elements
229
6.26k
            s.PutU4(0); // no encoding (1 would be zip-compressed)
230
            // TODO: compress if large?
231
6.26k
            s.PutU4(uint32_t(data.size())); // data size
232
43.2k
            for (size_t i = 0; i < N; ++i) {
233
37.0k
                s.PutF4((reinterpret_cast<float*>(d))[i]);
234
37.0k
            }
235
6.26k
            return;
236
396
        case 'd':
237
396
            N = data.size() / 8;
238
396
            s.PutU4(uint32_t(N)); // number of elements
239
396
            s.PutU4(0); // no encoding (1 would be zip-compressed)
240
            // TODO: compress if large?
241
396
            s.PutU4(uint32_t(data.size())); // data size
242
20.1k
            for (size_t i = 0; i < N; ++i) {
243
19.7k
                s.PutF8((reinterpret_cast<double*>(d))[i]);
244
19.7k
            }
245
396
            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
141k
    }
252
141k
}
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