summaryrefslogtreecommitdiff
path: root/libs/assimp/code/AssetLib/FBX/FBXExportNode.cpp
blob: 21c61b257cec883825e64107976ae8f5a4153a85 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2022, 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 "FBXExportNode.h"
#include "FBXCommon.h"

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

#include <string>
#include <ostream>
#include <sstream> // ostringstream
#include <memory> // shared_ptr

namespace Assimp {
// AddP70<type> helpers... there's no usable pattern here,
// so all are defined as separate functions.
// Even "animatable" properties are often completely different
// from the standard (nonanimated) property definition,
// so they are specified with an 'A' suffix.

void FBX::Node::AddP70int(
    const std::string& cur_name, int32_t value
) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "int", "Integer", "", value);
    AddChild(n);
}

void FBX::Node::AddP70bool(
    const std::string& cur_name, bool value
) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "bool", "", "", int32_t(value));
    AddChild(n);
}

void FBX::Node::AddP70double(
        const std::string &cur_name, double value) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "double", "Number", "", value);
    AddChild(n);
}

void FBX::Node::AddP70numberA(
        const std::string &cur_name, double value) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "Number", "", "A", value);
    AddChild(n);
}

void FBX::Node::AddP70color(
        const std::string &cur_name, double r, double g, double b) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "ColorRGB", "Color", "", r, g, b);
    AddChild(n);
}

void FBX::Node::AddP70colorA(
        const std::string &cur_name, double r, double g, double b) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "Color", "", "A", r, g, b);
    AddChild(n);
}

void FBX::Node::AddP70vector(
        const std::string &cur_name, double x, double y, double z) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "Vector3D", "Vector", "", x, y, z);
    AddChild(n);
}

void FBX::Node::AddP70vectorA(
        const std::string &cur_name, double x, double y, double z) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "Vector", "", "A", x, y, z);
    AddChild(n);
}

void FBX::Node::AddP70string(
        const std::string &cur_name, const std::string &value) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "KString", "", "", value);
    AddChild(n);
}

void FBX::Node::AddP70enum(
        const std::string &cur_name, int32_t value) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "enum", "", "", value);
    AddChild(n);
}

void FBX::Node::AddP70time(
        const std::string &cur_name, int64_t value) {
    FBX::Node n("P");
    n.AddProperties(cur_name, "KTime", "Time", "", value);
    AddChild(n);
}


// public member functions for writing nodes to stream

void FBX::Node::Dump(
        const std::shared_ptr<Assimp::IOStream> &outfile,
        bool binary, int indent) {
    if (binary) {
        Assimp::StreamWriterLE outstream(outfile);
        DumpBinary(outstream);
    } else {
        std::ostringstream ss;
        DumpAscii(ss, indent);
        std::string s = ss.str();
        outfile->Write(s.c_str(), s.size(), 1);
    }
}

void FBX::Node::Dump(
    Assimp::StreamWriterLE &outstream,
    bool binary, int indent
) {
    if (binary) {
        DumpBinary(outstream);
    } else {
        std::ostringstream ss;
        DumpAscii(ss, indent);
        outstream.PutString(ss.str());
    }
}


// public member functions for low-level writing

void FBX::Node::Begin(
    Assimp::StreamWriterLE &s,
    bool binary, int indent
) {
    if (binary) {
        BeginBinary(s);
    } else {
        // assume we're at the correct place to start already
        (void)indent;
        std::ostringstream ss;
        BeginAscii(ss, indent);
        s.PutString(ss.str());
    }
}

void FBX::Node::DumpProperties(
    Assimp::StreamWriterLE& s,
    bool binary, int indent
) {
    if (binary) {
        DumpPropertiesBinary(s);
    } else {
        std::ostringstream ss;
        DumpPropertiesAscii(ss, indent);
        s.PutString(ss.str());
    }
}

void FBX::Node::EndProperties(
    Assimp::StreamWriterLE &s,
    bool binary, int indent
) {
    EndProperties(s, binary, indent, properties.size());
}

void FBX::Node::EndProperties(
    Assimp::StreamWriterLE &s,
    bool binary, int indent,
    size_t num_properties
) {
    if (binary) {
        EndPropertiesBinary(s, num_properties);
    } else {
        // nothing to do
        (void)indent;
    }
}

void FBX::Node::BeginChildren(
    Assimp::StreamWriterLE &s,
    bool binary, int indent
) {
    if (binary) {
        // nothing to do
    } else {
        std::ostringstream ss;
        BeginChildrenAscii(ss, indent);
        s.PutString(ss.str());
    }
}

void FBX::Node::DumpChildren(
    Assimp::StreamWriterLE& s,
    bool binary, int indent
) {
    if (binary) {
        DumpChildrenBinary(s);
    } else {
        std::ostringstream ss;
        DumpChildrenAscii(ss, indent);
        if (ss.tellp() > 0)
            s.PutString(ss.str());
    }
}

void FBX::Node::End(
    Assimp::StreamWriterLE &s,
    bool binary, int indent,
    bool has_children
) {
    if (binary) {
        EndBinary(s, has_children);
    } else {
        std::ostringstream ss;
        EndAscii(ss, indent, has_children);
        if (ss.tellp() > 0)
            s.PutString(ss.str());
    }
}


// public member functions for writing to binary fbx

void FBX::Node::DumpBinary(Assimp::StreamWriterLE &s)
{
    // write header section (with placeholders for some things)
    BeginBinary(s);

    // write properties
    DumpPropertiesBinary(s);

    // go back and fill in property related placeholders
    EndPropertiesBinary(s, properties.size());

    // write children
    DumpChildrenBinary(s);

    // finish, filling in end offset placeholder
    EndBinary(s, force_has_children || !children.empty());
}


// public member functions for writing to ascii fbx

void FBX::Node::DumpAscii(std::ostream &s, int indent)
{
    // write name
    BeginAscii(s, indent);

    // write properties
    DumpPropertiesAscii(s, indent);

    if (force_has_children || !children.empty()) {
        // begin children (with a '{')
        BeginChildrenAscii(s, indent + 1);
        // write children
        DumpChildrenAscii(s, indent + 1);
    }

    // finish (also closing the children bracket '}')
    EndAscii(s, indent, force_has_children || !children.empty());
}


// private member functions for low-level writing to fbx

void FBX::Node::BeginBinary(Assimp::StreamWriterLE &s)
{
    // remember start pos so we can come back and write the end pos
    this->start_pos = s.Tell();

    // placeholders for end pos and property section info
    s.PutU8(0); // end pos
    s.PutU8(0); // number of properties
    s.PutU8(0); // total property section length

    // node name
    s.PutU1(uint8_t(name.size())); // length of node name
    s.PutString(name); // node name as raw bytes

    // property data comes after here
    this->property_start = s.Tell();
}

void FBX::Node::DumpPropertiesBinary(Assimp::StreamWriterLE& s)
{
    for (auto &p : properties) {
        p.DumpBinary(s);
    }
}

void FBX::Node::EndPropertiesBinary(
    Assimp::StreamWriterLE &s,
    size_t num_properties
) {
    if (num_properties == 0) { return; }
    size_t pos = s.Tell();
    ai_assert(pos > property_start);
    size_t property_section_size = pos - property_start;
    s.Seek(start_pos + 8); // 8 bytes of uint64_t of end_pos
    s.PutU8(num_properties);
    s.PutU8(property_section_size);
    s.Seek(pos);
}

void FBX::Node::DumpChildrenBinary(Assimp::StreamWriterLE& s)
{
    for (FBX::Node& child : children) {
        child.DumpBinary(s);
    }
}

void FBX::Node::EndBinary(
    Assimp::StreamWriterLE &s,
    bool has_children
) {
    // if there were children, add a null record
    if (has_children) { s.PutString(Assimp::FBX::NULL_RECORD); }

    // now go back and write initial pos
    this->end_pos = s.Tell();
    s.Seek(start_pos);
    s.PutU8(end_pos);
    s.Seek(end_pos);
}


void FBX::Node::BeginAscii(std::ostream& s, int indent)
{
    s << '\n';
    for (int i = 0; i < indent; ++i) { s << '\t'; }
    s << name << ": ";
}

void FBX::Node::DumpPropertiesAscii(std::ostream &s, int indent)
{
    for (size_t i = 0; i < properties.size(); ++i) {
        if (i > 0) { s << ", "; }
        properties[i].DumpAscii(s, indent);
    }
}

void FBX::Node::BeginChildrenAscii(std::ostream& s, int indent)
{
    // only call this if there are actually children
    s << " {";
    (void)indent;
}

void FBX::Node::DumpChildrenAscii(std::ostream& s, int indent)
{
    // children will need a lot of padding and corralling
    if (children.size() || force_has_children) {
        for (size_t i = 0; i < children.size(); ++i) {
            // no compression in ascii files, so skip this node if it exists
            if (children[i].name == "EncryptionType") { continue; }
            // the child can dump itself
            children[i].DumpAscii(s, indent);
        }
    }
}

void FBX::Node::EndAscii(std::ostream& s, int indent, bool has_children)
{
    if (!has_children) { return; } // nothing to do
    s << '\n';
    for (int i = 0; i < indent; ++i) { s << '\t'; }
    s << "}";
}

// private helpers for static member functions

// ascii property node from vector of doubles
void FBX::Node::WritePropertyNodeAscii(
    const std::string& name,
    const std::vector<double>& v,
    Assimp::StreamWriterLE& s,
    int indent
){
    char buffer[32];
    FBX::Node node(name);
    node.Begin(s, false, indent);
    std::string vsize = ai_to_string(v.size());
    // *<size> {
    s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
    // indent + 1
    for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
    // a: value,value,value,...
    s.PutString("a: ");
    int count = 0;
    for (size_t i = 0; i < v.size(); ++i) {
        if (i > 0) { s.PutChar(','); }
        int len = ai_snprintf(buffer, sizeof(buffer), "%f", v[i]);
        count += len;
        if (count > 2048) { s.PutChar('\n'); count = 0; }
        if (len < 0 || len > 31) {
            // this should never happen
            throw DeadlyExportError("failed to convert double to string");
        }
        for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
    }
    // }
    s.PutChar('\n');
    for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
    s.PutChar('}'); s.PutChar(' ');
    node.End(s, false, indent, false);
}

// ascii property node from vector of int32_t
void FBX::Node::WritePropertyNodeAscii(
    const std::string& name,
    const std::vector<int32_t>& v,
    Assimp::StreamWriterLE& s,
    int indent
){
    char buffer[32];
    FBX::Node node(name);
    node.Begin(s, false, indent);
    std::string vsize = ai_to_string(v.size());
    // *<size> {
    s.PutChar('*'); s.PutString(vsize); s.PutString(" {\n");
    // indent + 1
    for (int i = 0; i < indent + 1; ++i) { s.PutChar('\t'); }
    // a: value,value,value,...
    s.PutString("a: ");
    int count = 0;
    for (size_t i = 0; i < v.size(); ++i) {
        if (i > 0) { s.PutChar(','); }
        int len = ai_snprintf(buffer, sizeof(buffer), "%d", v[i]);
        count += len;
        if (count > 2048) { s.PutChar('\n'); count = 0; }
        if (len < 0 || len > 31) {
            // this should never happen
            throw DeadlyExportError("failed to convert double to string");
        }
        for (int j = 0; j < len; ++j) { s.PutChar(buffer[j]); }
    }
    // }
    s.PutChar('\n');
    for (int i = 0; i < indent; ++i) { s.PutChar('\t'); }
    s.PutChar('}'); s.PutChar(' ');
    node.End(s, false, indent, false);
}

// binary property node from vector of doubles
// TODO: optional zip compression!
void FBX::Node::WritePropertyNodeBinary(
    const std::string& name,
    const std::vector<double>& v,
    Assimp::StreamWriterLE& s
){
    FBX::Node node(name);
    node.BeginBinary(s);
    s.PutU1('d');
    s.PutU4(uint32_t(v.size())); // number of elements
    s.PutU4(0); // no encoding (1 would be zip-compressed)
    s.PutU4(uint32_t(v.size()) * 8); // data size
    for (auto it = v.begin(); it != v.end(); ++it) { s.PutF8(*it); }
    node.EndPropertiesBinary(s, 1);
    node.EndBinary(s, false);
}

// binary property node from vector of int32_t
// TODO: optional zip compression!
void FBX::Node::WritePropertyNodeBinary(
    const std::string& name,
    const std::vector<int32_t>& v,
    Assimp::StreamWriterLE& s
){
    FBX::Node node(name);
    node.BeginBinary(s);
    s.PutU1('i');
    s.PutU4(uint32_t(v.size())); // number of elements
    s.PutU4(0); // no encoding (1 would be zip-compressed)
    s.PutU4(uint32_t(v.size()) * 4); // data size
    for (auto it = v.begin(); it != v.end(); ++it) { s.PutI4(*it); }
    node.EndPropertiesBinary(s, 1);
    node.EndBinary(s, false);
}

// public static member functions

// convenience function to create and write a property node,
// holding a single property which is an array of values.
// does not copy the data, so is efficient for large arrays.
void FBX::Node::WritePropertyNode(
    const std::string& name,
    const std::vector<double>& v,
    Assimp::StreamWriterLE& s,
    bool binary, int indent
){
    if (binary) {
        FBX::Node::WritePropertyNodeBinary(name, v, s);
    } else {
        FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
    }
}

// convenience function to create and write a property node,
// holding a single property which is an array of values.
// does not copy the data, so is efficient for large arrays.
void FBX::Node::WritePropertyNode(
    const std::string& name,
    const std::vector<int32_t>& v,
    Assimp::StreamWriterLE& s,
    bool binary, int indent
){
    if (binary) {
        FBX::Node::WritePropertyNodeBinary(name, v, s);
    } else {
        FBX::Node::WritePropertyNodeAscii(name, v, s, indent);
    }
}
}
#endif // ASSIMP_BUILD_NO_FBX_EXPORTER
#endif // ASSIMP_BUILD_NO_EXPORT