Coverage Report

Created: 2026-11-12 07:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/assimp/code/AssetLib/Assxml/AssxmlFileWriter.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
/** @file AssxmlFileWriter.cpp
42
 *  @brief Implementation of Assxml file writer.
43
 */
44
45
#include "AssxmlFileWriter.h"
46
47
#include "PostProcessing/ProcessHelper.h"
48
49
#include <assimp/version.h>
50
#include <assimp/Exporter.hpp>
51
#include <assimp/IOStream.hpp>
52
#include <assimp/IOSystem.hpp>
53
54
#include <stdarg.h>
55
56
#ifdef ASSIMP_BUILD_NO_OWN_ZLIB
57
#include <zlib.h>
58
#else
59
#include <contrib/zlib/zlib.h>
60
#endif
61
62
#include <stdio.h>
63
#include <time.h>
64
#include <memory>
65
66
using namespace Assimp;
67
68
namespace Assimp {
69
70
namespace AssxmlFileWriter {
71
72
// -----------------------------------------------------------------------------------
73
8.80M
static int ioprintf(IOStream *io, const char *format, ...) {
74
8.80M
    using namespace std;
75
8.80M
    if (nullptr == io) {
76
0
        return -1;
77
0
    }
78
79
8.80M
    static const int Size = 4096;
80
8.80M
    char sz[Size] = {};
81
8.80M
    va_list va;
82
8.80M
    va_start(va, format);
83
8.80M
    const unsigned int nSize = vsnprintf(sz, Size - 1, format, va);
84
8.80M
    ai_assert(nSize < Size);
85
8.80M
    va_end(va);
86
87
8.80M
    io->Write(sz, sizeof(char), nSize);
88
89
8.80M
    return nSize;
90
8.80M
}
91
92
// -----------------------------------------------------------------------------------
93
// Convert a name to standard XML format
94
125k
static void ConvertName(aiString &out, const aiString &in) {
95
125k
    out.length = 0;
96
2.31M
    for (unsigned int i = 0; i < in.length; ++i) {
97
2.18M
        switch (in.data[i]) {
98
7.04k
        case '<':
99
7.04k
            out.Append("&lt;");
100
7.04k
            break;
101
4.66k
        case '>':
102
4.66k
            out.Append("&gt;");
103
4.66k
            break;
104
147
        case '&':
105
147
            out.Append("&amp;");
106
147
            break;
107
14.4k
        case '\"':
108
14.4k
            out.Append("&quot;");
109
14.4k
            break;
110
920
        case '\'':
111
920
            out.Append("&apos;");
112
920
            break;
113
2.16M
        default:
114
2.16M
            out.data[out.length++] = in.data[i];
115
2.18M
        }
116
2.18M
    }
117
125k
    out.data[out.length] = 0;
118
125k
}
119
120
// -----------------------------------------------------------------------------------
121
// Write a single node as text dump
122
119k
static void WriteNode(const aiNode *node, IOStream *io, unsigned int depth) {
123
119k
    char prefix[512];
124
457k
    for (unsigned int i = 0; i < depth; ++i)
125
338k
        prefix[i] = '\t';
126
119k
    prefix[depth] = '\0';
127
128
119k
    const aiMatrix4x4 &m = node->mTransformation;
129
130
119k
    aiString name;
131
119k
    ConvertName(name, node->mName);
132
119k
    ioprintf(io, "%s<Node name=\"%s\"> \n"
133
119k
                 "%s\t<Matrix4> \n"
134
119k
                 "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
135
119k
                 "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
136
119k
                 "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
137
119k
                 "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
138
119k
                 "%s\t</Matrix4> \n",
139
119k
            prefix, name.data, prefix,
140
119k
            prefix, m.a1, m.a2, m.a3, m.a4,
141
119k
            prefix, m.b1, m.b2, m.b3, m.b4,
142
119k
            prefix, m.c1, m.c2, m.c3, m.c4,
143
119k
            prefix, m.d1, m.d2, m.d3, m.d4, prefix);
144
145
119k
    if (node->mNumMeshes) {
146
12.9k
        ioprintf(io, "%s\t<MeshRefs num=\"%u\">\n%s\t",
147
12.9k
                prefix, node->mNumMeshes, prefix);
148
149
33.9k
        for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
150
20.9k
            ioprintf(io, "%u ", node->mMeshes[i]);
151
20.9k
        }
152
12.9k
        ioprintf(io, "\n%s\t</MeshRefs>\n", prefix);
153
12.9k
    }
154
155
119k
    if (node->mNumChildren) {
156
14.6k
        ioprintf(io, "%s\t<NodeList num=\"%u\">\n",
157
14.6k
                prefix, node->mNumChildren);
158
159
128k
        for (unsigned int i = 0; i < node->mNumChildren; ++i) {
160
114k
            WriteNode(node->mChildren[i], io, depth + 2);
161
114k
        }
162
14.6k
        ioprintf(io, "%s\t</NodeList>\n", prefix);
163
14.6k
    }
164
119k
    ioprintf(io, "%s</Node>\n", prefix);
165
119k
}
166
167
// -----------------------------------------------------------------------------------
168
// Some chunks of text will need to be encoded for XML
169
// http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string#5665377
170
7.03k
static std::string encodeXML(const std::string &data) {
171
7.03k
    std::string buffer;
172
7.03k
    buffer.reserve(data.size());
173
149k
    for (size_t pos = 0; pos != data.size(); ++pos) {
174
142k
        switch (data[pos]) {
175
15
        case '&': buffer.append("&amp;"); break;
176
475
        case '\"': buffer.append("&quot;"); break;
177
64
        case '\'': buffer.append("&apos;"); break;
178
366
        case '<': buffer.append("&lt;"); break;
179
329
        case '>': buffer.append("&gt;"); break;
180
140k
        default: buffer.append(&data[pos], 1); break;
181
142k
        }
182
142k
    }
183
7.03k
    return buffer;
184
7.03k
}
185
186
// -----------------------------------------------------------------------------------
187
// Write a text model dump
188
4.96k
static void WriteDump(const char *pFile, const char *cmd, const aiScene *scene, IOStream *io, bool shortened) {
189
4.96k
    time_t tt = ::time(nullptr);
190
#if _WIN32
191
    tm *p = gmtime(&tt);
192
#else
193
4.96k
    struct tm now;
194
4.96k
    tm *p = gmtime_r(&tt, &now);
195
4.96k
#endif
196
4.96k
    ai_assert(nullptr != p);
197
198
4.96k
    std::string c = cmd;
199
4.96k
    std::string::size_type s;
200
201
    // https://sourceforge.net/tracker/?func=detail&aid=3167364&group_id=226462&atid=1067632
202
    // -- not allowed in XML comments
203
4.96k
    while ((s = c.find("--")) != std::string::npos) {
204
0
        c[s] = '?';
205
0
    }
206
207
    // write header
208
4.96k
    std::string header(
209
4.96k
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
210
4.96k
            "<ASSIMP format_id=\"1\">\n\n"
211
4.96k
            "<!-- XML Model dump produced by assimp dump\n"
212
4.96k
            "  Library version: %u.%u.%u\n"
213
4.96k
            "  Source: %s\n"
214
4.96k
            "  Command line: %s\n"
215
4.96k
            "  %s\n"
216
4.96k
            "-->"
217
4.96k
            " \n\n"
218
4.96k
            "<Scene flags=\"%u\" postprocessing=\"%u\">\n");
219
220
4.96k
    const unsigned int majorVersion(aiGetVersionMajor());
221
4.96k
    const unsigned int minorVersion(aiGetVersionMinor());
222
4.96k
    const unsigned int rev(aiGetVersionRevision());
223
4.96k
    const char *curtime = asctime(p);
224
4.96k
    ioprintf(io, header.c_str(), majorVersion, minorVersion, rev, pFile, c.c_str(), curtime, scene->mFlags, 0u);
225
226
    // write the node graph
227
4.96k
    WriteNode(scene->mRootNode, io, 0);
228
229
#if 0
230
    // write cameras
231
    for (unsigned int i = 0; i < scene->mNumCameras;++i) {
232
        aiCamera* cam  = scene->mCameras[i];
233
        ConvertName(name,cam->mName);
234
235
        // camera header
236
        ioprintf(io,"\t<Camera parent=\"%s\">\n"
237
            "\t\t<Vector3 name=\"up\"        > %0 8f %0 8f %0 8f </Vector3>\n"
238
            "\t\t<Vector3 name=\"lookat\"    > %0 8f %0 8f %0 8f </Vector3>\n"
239
            "\t\t<Vector3 name=\"pos\"       > %0 8f %0 8f %0 8f </Vector3>\n"
240
            "\t\t<Float   name=\"fov\"       > %f </Float>\n"
241
            "\t\t<Float   name=\"aspect\"    > %f </Float>\n"
242
            "\t\t<Float   name=\"near_clip\" > %f </Float>\n"
243
            "\t\t<Float   name=\"far_clip\"  > %f </Float>\n"
244
            "\t</Camera>\n",
245
            name.data,
246
            cam->mUp.x,cam->mUp.y,cam->mUp.z,
247
            cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
248
            cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
249
            cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
250
    }
251
252
    // write lights
253
    for (unsigned int i = 0; i < scene->mNumLights;++i) {
254
        aiLight* l  = scene->mLights[i];
255
        ConvertName(name,l->mName);
256
257
        // light header
258
        ioprintf(io,"\t<Light parent=\"%s\"> type=\"%s\"\n"
259
            "\t\t<Vector3 name=\"diffuse\"   > %0 8f %0 8f %0 8f </Vector3>\n"
260
            "\t\t<Vector3 name=\"specular\"  > %0 8f %0 8f %0 8f </Vector3>\n"
261
            "\t\t<Vector3 name=\"ambient\"   > %0 8f %0 8f %0 8f </Vector3>\n",
262
            name.data,
263
            (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
264
            (l->mType == aiLightSource_POINT ? "point" : "spot" )),
265
            l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
266
            l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
267
            l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
268
269
        if (l->mType != aiLightSource_DIRECTIONAL) {
270
            ioprintf(io,
271
                "\t\t<Vector3 name=\"pos\"       > %0 8f %0 8f %0 8f </Vector3>\n"
272
                "\t\t<Float   name=\"atten_cst\" > %f </Float>\n"
273
                "\t\t<Float   name=\"atten_lin\" > %f </Float>\n"
274
                "\t\t<Float   name=\"atten_sqr\" > %f </Float>\n",
275
                l->mPosition.x,l->mPosition.y,l->mPosition.z,
276
                l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
277
        }
278
279
        if (l->mType != aiLightSource_POINT) {
280
            ioprintf(io,
281
                "\t\t<Vector3 name=\"lookat\"    > %0 8f %0 8f %0 8f </Vector3>\n",
282
                l->mDirection.x,l->mDirection.y,l->mDirection.z);
283
        }
284
285
        if (l->mType == aiLightSource_SPOT) {
286
            ioprintf(io,
287
                "\t\t<Float   name=\"cone_out\" > %f </Float>\n"
288
                "\t\t<Float   name=\"cone_inn\" > %f </Float>\n",
289
                l->mAngleOuterCone,l->mAngleInnerCone);
290
        }
291
        ioprintf(io,"\t</Light>\n");
292
    }
293
#endif
294
4.96k
    aiString name;
295
296
    // write textures
297
4.96k
    if (scene->mNumTextures) {
298
368
        ioprintf(io, "<TextureList num=\"%u\">\n", scene->mNumTextures);
299
846
        for (unsigned int i = 0; i < scene->mNumTextures; ++i) {
300
478
            aiTexture *tex = scene->mTextures[i];
301
478
            bool compressed = (tex->mHeight == 0);
302
303
            // mesh header
304
478
            std::string texName = "unknown";
305
478
            if (tex->mFilename.length != 0u) {
306
447
                texName = tex->mFilename.data;
307
447
            }
308
478
            ioprintf(io, "\t<Texture name=\"%s\" width=\"%u\" height=\"%u\" compressed=\"%s\"> \n", texName.c_str(),
309
478
                    (compressed ? -1 : tex->mWidth), (compressed ? -1 : tex->mHeight),
310
478
                    (compressed ? "true" : "false"));
311
312
478
            if (compressed) {
313
96
                ioprintf(io, "\t\t<Data length=\"%u\"> \n", tex->mWidth);
314
315
96
                if (!shortened) {
316
1.51M
                    for (unsigned int n = 0; n < tex->mWidth; ++n) {
317
1.51M
                        ioprintf(io, "\t\t\t%2x", reinterpret_cast<uint8_t *>(tex->pcData)[n]);
318
1.51M
                        if (n && !(n % 50)) {
319
30.2k
                            ioprintf(io, "\n");
320
30.2k
                        }
321
1.51M
                    }
322
96
                }
323
382
            } else if (!shortened) {
324
382
                ioprintf(io, "\t\t<Data length=\"%u\"> \n", tex->mWidth * tex->mHeight * 4);
325
326
                // const unsigned int width = (unsigned int)std::log10((double)std::max(tex->mHeight,tex->mWidth))+1;
327
14.7k
                for (unsigned int y = 0; y < tex->mHeight; ++y) {
328
962k
                    for (unsigned int x = 0; x < tex->mWidth; ++x) {
329
948k
                        aiTexel *tx = tex->pcData + y * tex->mWidth + x;
330
948k
                        unsigned int r = tx->r, g = tx->g, b = tx->b, a = tx->a;
331
948k
                        ioprintf(io, "\t\t\t%2x %2x %2x %2x", r, g, b, a);
332
333
                        // group by four for readability
334
948k
                        if (0 == (x + y * tex->mWidth) % 4) {
335
237k
                            ioprintf(io, "\n");
336
237k
                        }
337
948k
                    }
338
14.3k
                }
339
382
            }
340
478
            ioprintf(io, "\t\t</Data>\n\t</Texture>\n");
341
478
        }
342
368
        ioprintf(io, "</TextureList>\n");
343
368
    }
344
345
    // write materials
346
4.96k
    if (scene->mNumMaterials) {
347
4.27k
        ioprintf(io, "<MaterialList num=\"%u\">\n", scene->mNumMaterials);
348
9.65k
        for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
349
5.37k
            const aiMaterial *mat = scene->mMaterials[i];
350
351
5.37k
            ioprintf(io, "\t<Material>\n");
352
5.37k
            ioprintf(io, "\t\t<MatPropertyList  num=\"%u\">\n", mat->mNumProperties);
353
61.0k
            for (unsigned int n = 0; n < mat->mNumProperties; ++n) {
354
355
55.6k
                const aiMaterialProperty *prop = mat->mProperties[n];
356
55.6k
                auto sz = "";
357
55.6k
                if (prop->mType == aiPTI_Float) {
358
35.7k
                    sz = "float";
359
35.7k
                } else if (prop->mType == aiPTI_Integer) {
360
11.8k
                    sz = "integer";
361
11.8k
                } else if (prop->mType == aiPTI_String) {
362
7.03k
                    sz = "string";
363
7.03k
                } else if (prop->mType == aiPTI_Buffer) {
364
1.01k
                    sz = "binary_buffer";
365
1.01k
                }
366
367
55.6k
                ioprintf(io, "\t\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%u\"",
368
55.6k
                        prop->mKey.data, sz,
369
55.6k
                        ::aiTextureTypeToString((aiTextureType)prop->mSemantic), prop->mIndex);
370
371
55.6k
                if (prop->mType == aiPTI_Float) {
372
35.7k
                    ioprintf(io, " size=\"%i\">\n\t\t\t\t",
373
35.7k
                            static_cast<int>(prop->mDataLength / sizeof(float)));
374
375
119k
                    for (unsigned int pp = 0; pp < prop->mDataLength / sizeof(float); ++pp) {
376
83.3k
                        ioprintf(io, "%f ", *((float *)(prop->mData + pp * sizeof(float))));
377
83.3k
                    }
378
35.7k
                } else if (prop->mType == aiPTI_Integer) {
379
11.8k
                    ioprintf(io, " size=\"%i\">\n\t\t\t\t",
380
11.8k
                            static_cast<int>(prop->mDataLength / sizeof(int)));
381
382
23.7k
                    for (unsigned int pp = 0; pp < prop->mDataLength / sizeof(int); ++pp) {
383
11.8k
                        ioprintf(io, "%i ", *((int *)(prop->mData + pp * sizeof(int))));
384
11.8k
                    }
385
11.8k
                } else if (prop->mType == aiPTI_Buffer) {
386
1.01k
                    ioprintf(io, " size=\"%i\">\n\t\t\t\t",
387
1.01k
                            static_cast<int>(prop->mDataLength));
388
389
4.58k
                    for (unsigned int pp = 0; pp < prop->mDataLength; ++pp) {
390
3.56k
                        ioprintf(io, "%2x ", prop->mData[pp]);
391
3.56k
                        if (pp && 0 == pp % 30) {
392
0
                            ioprintf(io, "\n\t\t\t\t");
393
0
                        }
394
3.56k
                    }
395
7.03k
                } else if (prop->mType == aiPTI_String) {
396
7.03k
                    ioprintf(io, ">\n\t\t\t\t\"%s\"", encodeXML(prop->mData + 4).c_str() /* skip length */);
397
7.03k
                }
398
55.6k
                ioprintf(io, "\n\t\t\t</MatProperty>\n");
399
55.6k
            }
400
5.37k
            ioprintf(io, "\t\t</MatPropertyList>\n");
401
5.37k
            ioprintf(io, "\t</Material>\n");
402
5.37k
        }
403
4.27k
        ioprintf(io, "</MaterialList>\n");
404
4.27k
    }
405
406
    // write animations
407
4.96k
    if (scene->mNumAnimations) {
408
490
        ioprintf(io, "<AnimationList num=\"%u\">\n", scene->mNumAnimations);
409
1.37k
        for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
410
889
            aiAnimation *anim = scene->mAnimations[i];
411
412
            // anim header
413
889
            ConvertName(name, anim->mName);
414
889
            ioprintf(io, "\t<Animation name=\"%s\" duration=\"%e\" tick_cnt=\"%e\">\n",
415
889
                    name.data, anim->mDuration, anim->mTicksPerSecond);
416
417
            // write bone animation channels
418
889
            if (anim->mNumChannels) {
419
889
                ioprintf(io, "\t\t<NodeAnimList num=\"%u\">\n", anim->mNumChannels);
420
3.48k
                for (unsigned int n = 0; n < anim->mNumChannels; ++n) {
421
2.59k
                    aiNodeAnim *nd = anim->mChannels[n];
422
423
                    // node anim header
424
2.59k
                    ConvertName(name, nd->mNodeName);
425
2.59k
                    ioprintf(io, "\t\t\t<NodeAnim node=\"%s\">\n", name.data);
426
427
2.59k
                    if (!shortened) {
428
                        // write position keys
429
2.59k
                        if (nd->mNumPositionKeys) {
430
2.59k
                            ioprintf(io, "\t\t\t\t<PositionKeyList num=\"%u\">\n", nd->mNumPositionKeys);
431
16.1k
                            for (unsigned int a = 0; a < nd->mNumPositionKeys; ++a) {
432
13.5k
                                aiVectorKey *vc = nd->mPositionKeys + a;
433
13.5k
                                ioprintf(io, "\t\t\t\t\t<PositionKey time=\"%e\">\n"
434
13.5k
                                             "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</PositionKey>\n",
435
13.5k
                                        vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z);
436
13.5k
                            }
437
2.59k
                            ioprintf(io, "\t\t\t\t</PositionKeyList>\n");
438
2.59k
                        }
439
440
                        // write scaling keys
441
2.59k
                        if (nd->mNumScalingKeys) {
442
2.59k
                            ioprintf(io, "\t\t\t\t<ScalingKeyList num=\"%u\">\n", nd->mNumScalingKeys);
443
5.77k
                            for (unsigned int a = 0; a < nd->mNumScalingKeys; ++a) {
444
3.17k
                                aiVectorKey *vc = nd->mScalingKeys + a;
445
3.17k
                                ioprintf(io, "\t\t\t\t\t<ScalingKey time=\"%e\">\n"
446
3.17k
                                             "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</ScalingKey>\n",
447
3.17k
                                        vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z);
448
3.17k
                            }
449
2.59k
                            ioprintf(io, "\t\t\t\t</ScalingKeyList>\n");
450
2.59k
                        }
451
452
                        // write rotation keys
453
2.59k
                        if (nd->mNumRotationKeys) {
454
2.59k
                            ioprintf(io, "\t\t\t\t<RotationKeyList num=\"%u\">\n", nd->mNumRotationKeys);
455
61.3k
                            for (unsigned int a = 0; a < nd->mNumRotationKeys; ++a) {
456
58.7k
                                aiQuatKey *vc = nd->mRotationKeys + a;
457
58.7k
                                ioprintf(io, "\t\t\t\t\t<RotationKey time=\"%e\">\n"
458
58.7k
                                             "\t\t\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t\t\t</RotationKey>\n",
459
58.7k
                                        vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z, vc->mValue.w);
460
58.7k
                            }
461
2.59k
                            ioprintf(io, "\t\t\t\t</RotationKeyList>\n");
462
2.59k
                        }
463
2.59k
                    }
464
2.59k
                    ioprintf(io, "\t\t\t</NodeAnim>\n");
465
2.59k
                }
466
889
                ioprintf(io, "\t\t</NodeAnimList>\n");
467
889
            }
468
889
            ioprintf(io, "\t</Animation>\n");
469
889
        }
470
490
        ioprintf(io, "</AnimationList>\n");
471
490
    }
472
473
    // write meshes
474
4.96k
    if (scene->mNumMeshes) {
475
4.27k
        ioprintf(io, "<MeshList num=\"%u\">\n", scene->mNumMeshes);
476
25.5k
        for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
477
21.2k
            aiMesh *mesh = scene->mMeshes[i];
478
            // const unsigned int width = (unsigned int)std::log10((double)mesh->mNumVertices)+1;
479
480
            // mesh header
481
21.2k
            ioprintf(io, "\t<Mesh types=\"%s %s %s %s\" material_index=\"%u\">\n",
482
21.2k
                    (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
483
21.2k
                    (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
484
21.2k
                    (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
485
21.2k
                    (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
486
21.2k
                    mesh->mMaterialIndex);
487
488
            // bones
489
21.2k
            if (mesh->mNumBones) {
490
1.20k
                ioprintf(io, "\t\t<BoneList num=\"%u\">\n", mesh->mNumBones);
491
492
4.16k
                for (unsigned int n = 0; n < mesh->mNumBones; ++n) {
493
2.96k
                    aiBone *bone = mesh->mBones[n];
494
495
2.96k
                    ConvertName(name, bone->mName);
496
                    // bone header
497
2.96k
                    ioprintf(io, "\t\t\t<Bone name=\"%s\">\n"
498
2.96k
                                 "\t\t\t\t<Matrix4> \n"
499
2.96k
                                 "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
500
2.96k
                                 "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
501
2.96k
                                 "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
502
2.96k
                                 "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
503
2.96k
                                 "\t\t\t\t</Matrix4> \n",
504
2.96k
                            name.data,
505
2.96k
                            bone->mOffsetMatrix.a1, bone->mOffsetMatrix.a2, bone->mOffsetMatrix.a3, bone->mOffsetMatrix.a4,
506
2.96k
                            bone->mOffsetMatrix.b1, bone->mOffsetMatrix.b2, bone->mOffsetMatrix.b3, bone->mOffsetMatrix.b4,
507
2.96k
                            bone->mOffsetMatrix.c1, bone->mOffsetMatrix.c2, bone->mOffsetMatrix.c3, bone->mOffsetMatrix.c4,
508
2.96k
                            bone->mOffsetMatrix.d1, bone->mOffsetMatrix.d2, bone->mOffsetMatrix.d3, bone->mOffsetMatrix.d4);
509
510
2.96k
                    if (!shortened && bone->mNumWeights) {
511
2.96k
                        ioprintf(io, "\t\t\t\t<WeightList num=\"%u\">\n", bone->mNumWeights);
512
513
                        // bone weights
514
71.4k
                        for (unsigned int a = 0; a < bone->mNumWeights; ++a) {
515
68.5k
                            aiVertexWeight *wght = bone->mWeights + a;
516
517
68.5k
                            ioprintf(io, "\t\t\t\t\t<Weight index=\"%u\">\n\t\t\t\t\t\t%f\n\t\t\t\t\t</Weight>\n",
518
68.5k
                                    wght->mVertexId, wght->mWeight);
519
68.5k
                        }
520
2.96k
                        ioprintf(io, "\t\t\t\t</WeightList>\n");
521
2.96k
                    }
522
2.96k
                    ioprintf(io, "\t\t\t</Bone>\n");
523
2.96k
                }
524
1.20k
                ioprintf(io, "\t\t</BoneList>\n");
525
1.20k
            }
526
527
            // faces
528
21.2k
            if (!shortened && mesh->mNumFaces) {
529
21.2k
                ioprintf(io, "\t\t<FaceList num=\"%u\">\n", mesh->mNumFaces);
530
683k
                for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
531
661k
                    aiFace &f = mesh->mFaces[n];
532
661k
                    ioprintf(io, "\t\t\t<Face num=\"%u\">\n"
533
661k
                                 "\t\t\t\t",
534
661k
                            f.mNumIndices);
535
536
2.52M
                    for (unsigned int j = 0; j < f.mNumIndices; ++j)
537
1.86M
                        ioprintf(io, "%u ", f.mIndices[j]);
538
539
661k
                    ioprintf(io, "\n\t\t\t</Face>\n");
540
661k
                }
541
21.2k
                ioprintf(io, "\t\t</FaceList>\n");
542
21.2k
            }
543
544
            // vertex positions
545
21.2k
            if (mesh->HasPositions()) {
546
21.2k
                ioprintf(io, "\t\t<Positions num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
547
21.2k
                if (!shortened) {
548
578k
                    for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
549
557k
                        ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
550
557k
                                mesh->mVertices[n].x,
551
557k
                                mesh->mVertices[n].y,
552
557k
                                mesh->mVertices[n].z);
553
557k
                    }
554
21.2k
                }
555
21.2k
                ioprintf(io, "\t\t</Positions>\n");
556
21.2k
            }
557
558
            // vertex normals
559
21.2k
            if (mesh->HasNormals()) {
560
18.2k
                ioprintf(io, "\t\t<Normals num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
561
18.2k
                if (!shortened) {
562
565k
                    for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
563
546k
                        ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
564
546k
                                mesh->mNormals[n].x,
565
546k
                                mesh->mNormals[n].y,
566
546k
                                mesh->mNormals[n].z);
567
546k
                    }
568
18.2k
                }
569
18.2k
                ioprintf(io, "\t\t</Normals>\n");
570
18.2k
            }
571
572
            // vertex tangents and bitangents
573
21.2k
            if (mesh->HasTangentsAndBitangents()) {
574
4.47k
                ioprintf(io, "\t\t<Tangents num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
575
4.47k
                if (!shortened) {
576
245k
                    for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
577
240k
                        ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
578
240k
                                mesh->mTangents[n].x,
579
240k
                                mesh->mTangents[n].y,
580
240k
                                mesh->mTangents[n].z);
581
240k
                    }
582
4.47k
                }
583
4.47k
                ioprintf(io, "\t\t</Tangents>\n");
584
585
4.47k
                ioprintf(io, "\t\t<Bitangents num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
586
4.47k
                if (!shortened) {
587
245k
                    for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
588
240k
                        ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
589
240k
                                mesh->mBitangents[n].x,
590
240k
                                mesh->mBitangents[n].y,
591
240k
                                mesh->mBitangents[n].z);
592
240k
                    }
593
4.47k
                }
594
4.47k
                ioprintf(io, "\t\t</Bitangents>\n");
595
4.47k
            }
596
597
            // texture coordinates
598
26.0k
            for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
599
26.0k
                if (!mesh->mTextureCoords[a])
600
21.2k
                    break;
601
602
4.85k
                ioprintf(io, "\t\t<TextureCoords num=\"%u\" set=\"%u\" name=\"%s\" num_components=\"%u\"> \n",
603
4.85k
                         mesh->mNumVertices,
604
4.85k
                         a,
605
4.85k
                         (mesh->HasTextureCoordsName(a) ? mesh->GetTextureCoordsName(a)->C_Str() : ""),
606
4.85k
                         mesh->mNumUVComponents[a]);
607
608
4.85k
                if (!shortened) {
609
4.85k
                    if (mesh->mNumUVComponents[a] == 3) {
610
1.13k
                        for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
611
1.09k
                            ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
612
1.09k
                                    mesh->mTextureCoords[a][n].x,
613
1.09k
                                    mesh->mTextureCoords[a][n].y,
614
1.09k
                                    mesh->mTextureCoords[a][n].z);
615
1.09k
                        }
616
4.81k
                    } else {
617
257k
                        for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
618
252k
                            ioprintf(io, "\t\t%0 8f %0 8f\n",
619
252k
                                    mesh->mTextureCoords[a][n].x,
620
252k
                                    mesh->mTextureCoords[a][n].y);
621
252k
                        }
622
4.81k
                    }
623
4.85k
                }
624
4.85k
                ioprintf(io, "\t\t</TextureCoords>\n");
625
4.85k
            }
626
627
            // vertex colors
628
25.3k
            for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
629
25.3k
                if (!mesh->mColors[a])
630
21.2k
                    break;
631
4.09k
                ioprintf(io, "\t\t<Colors num=\"%u\" set=\"%u\" num_components=\"4\"> \n", mesh->mNumVertices, a);
632
4.09k
                if (!shortened) {
633
40.3k
                    for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
634
36.2k
                        ioprintf(io, "\t\t%0 8f %0 8f %0 8f %0 8f\n",
635
36.2k
                                mesh->mColors[a][n].r,
636
36.2k
                                mesh->mColors[a][n].g,
637
36.2k
                                mesh->mColors[a][n].b,
638
36.2k
                                mesh->mColors[a][n].a);
639
36.2k
                    }
640
4.09k
                }
641
4.09k
                ioprintf(io, "\t\t</Colors>\n");
642
4.09k
            }
643
21.2k
            ioprintf(io, "\t</Mesh>\n");
644
21.2k
        }
645
4.27k
        ioprintf(io, "</MeshList>\n");
646
4.27k
    }
647
4.96k
    ioprintf(io, "</Scene>\n</ASSIMP>");
648
4.96k
}
649
650
} // end of namespace AssxmlFileWriter
651
652
void DumpSceneToAssxml(
653
        const char *pFile, const char *cmd, IOSystem *pIOSystem,
654
4.96k
        const aiScene *pScene, bool shortened) {
655
4.96k
    std::unique_ptr<IOStream> file(pIOSystem->Open(pFile, "wt"));
656
4.96k
    if (!file) {
657
0
        throw std::runtime_error("Unable to open output file " + std::string(pFile) + '\n');
658
0
    }
659
660
4.96k
    AssxmlFileWriter::WriteDump(pFile, cmd, pScene, file.get(), shortened);
661
4.96k
}
662
663
} // end of namespace Assimp