summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/assimp/code/D3MFImporter.cpp
blob: 372416dbd7ae4183a08238a081ac45d276188cc2 (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
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------

Copyright (c) 2006-2017, 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_3MF_IMPORTER

#include "D3MFImporter.h"

#include <assimp/scene.h>
#include <assimp/IOSystem.hpp>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h>
#include "StringComparison.h"
#include "StringUtils.h"

#include <string>
#include <vector>
#include <map>
#include <cassert>
#include <memory>

#include "D3MFOpcPackage.h"
#include <contrib/unzip/unzip.h>
#include "irrXMLWrapper.h"
#include "3MFXmlTags.h"

namespace Assimp {
namespace D3MF {

class XmlSerializer {
public:
    XmlSerializer(XmlReader* xmlReader)
    : xmlReader(xmlReader) {
		// empty
    }

    ~XmlSerializer() {
        // empty
    }

    void ImportXml(aiScene* scene) {
        scene->mRootNode = new aiNode();
        std::vector<aiNode*> children;

        while(ReadToEndElement(D3MF::XmlTag::model)) {
            if(xmlReader->getNodeName() == D3MF::XmlTag::object) {
                children.push_back(ReadObject(scene));
            } else if(xmlReader->getNodeName() == D3MF::XmlTag::build) {

            }
        }

        if ( scene->mRootNode->mName.length == 0 ) {
            scene->mRootNode->mName.Set( "3MF" );
        }

        scene->mNumMeshes = static_cast<unsigned int>(meshes.size());
        scene->mMeshes = new aiMesh*[scene->mNumMeshes]();

        std::copy(meshes.begin(), meshes.end(), scene->mMeshes);

        scene->mRootNode->mNumChildren = static_cast<unsigned int>(children.size());
        scene->mRootNode->mChildren = new aiNode*[scene->mRootNode->mNumChildren]();

        std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
    }

private:
    aiNode* ReadObject(aiScene* scene)
    {
        std::unique_ptr<aiNode> node(new aiNode());

        std::vector<unsigned long> meshIds;

        const char *attrib( nullptr );
        std::string name, type;
        attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
        if ( nullptr != attrib ) {
            name = attrib;
        }
        attrib = xmlReader->getAttributeValue( D3MF::XmlTag::name.c_str() );
        if ( nullptr != attrib ) {
            type = attrib;
        }

        node->mParent = scene->mRootNode;
        node->mName.Set(name);

        size_t meshIdx = meshes.size();

        while(ReadToEndElement(D3MF::XmlTag::object))
        {
            if(xmlReader->getNodeName() == D3MF::XmlTag::mesh)
            {
                auto mesh = ReadMesh();

                mesh->mName.Set(name);
                meshes.push_back(mesh);
                meshIds.push_back(static_cast<unsigned long>(meshIdx));
                meshIdx++;

            }
        }

        node->mNumMeshes = static_cast<unsigned int>(meshIds.size());

        node->mMeshes = new unsigned int[node->mNumMeshes];

        std::copy(meshIds.begin(), meshIds.end(), node->mMeshes);

        return node.release();

    }

    aiMesh* ReadMesh() {
        aiMesh* mesh = new aiMesh();
        while(ReadToEndElement(D3MF::XmlTag::mesh))
        {
            if(xmlReader->getNodeName() == D3MF::XmlTag::vertices)
            {
                ImportVertices(mesh);
            }
            else if(xmlReader->getNodeName() == D3MF::XmlTag::triangles)
            {
                ImportTriangles(mesh);
            }
        }

        return mesh;
    }

    void ImportVertices(aiMesh* mesh)
    {
        std::vector<aiVector3D> vertices;

        while(ReadToEndElement(D3MF::XmlTag::vertices))
        {
            if(xmlReader->getNodeName() == D3MF::XmlTag::vertex)
            {
                vertices.push_back(ReadVertex());
            }
        }
        mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
        mesh->mVertices = new aiVector3D[mesh->mNumVertices];

        std::copy(vertices.begin(), vertices.end(), mesh->mVertices);

    }

    aiVector3D ReadVertex()
    {
        aiVector3D vertex;

        vertex.x = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::x.c_str()), nullptr);
        vertex.y = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::y.c_str()), nullptr);
        vertex.z = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::z.c_str()), nullptr);

        return vertex;
    }

    void ImportTriangles(aiMesh* mesh)
    {
         std::vector<aiFace> faces;


         while(ReadToEndElement(D3MF::XmlTag::triangles))
         {
             if(xmlReader->getNodeName() == D3MF::XmlTag::triangle)
             {
                 faces.push_back(ReadTriangle());
             }
         }

        mesh->mNumFaces = static_cast<unsigned int>(faces.size());
        mesh->mFaces = new aiFace[mesh->mNumFaces];
        mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;

        std::copy(faces.begin(), faces.end(), mesh->mFaces);
    }

    aiFace ReadTriangle()
    {
        aiFace face;

        face.mNumIndices = 3;
        face.mIndices = new unsigned int[face.mNumIndices];
        face.mIndices[0] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v1.c_str())));
        face.mIndices[1] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v2.c_str())));
        face.mIndices[2] = static_cast<unsigned int>(std::atoi(xmlReader->getAttributeValue(D3MF::XmlTag::v3.c_str())));

        return face;
    }

private:
    bool ReadToStartElement(const std::string& startTag)
    {
        while(xmlReader->read())
        {
            if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT && xmlReader->getNodeName() == startTag)
            {
                return true;
            }
            else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END &&
                     xmlReader->getNodeName() == startTag)
            {
                return false;
            }
        }
        //DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
        return false;
    }

    bool ReadToEndElement(const std::string& closeTag)
    {
        while(xmlReader->read())
        {
            if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT) {
                return true;
            }
            else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END
                     && xmlReader->getNodeName() == closeTag)
            {
                return false;
            }
        }
        DefaultLogger::get()->error("unexpected EOF, expected closing <" + closeTag + "> tag");
        return false;
    }


private:
    std::vector<aiMesh*> meshes;
    XmlReader* xmlReader;
};

} //namespace D3MF

static const std::string Extension = "3mf";

static const aiImporterDesc desc = {
    "3mf Importer",
    "",
    "",
    "http://3mf.io/",
    aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
    0,
    0,
    0,
    0,
    Extension.c_str()
};


D3MFImporter::D3MFImporter()
: BaseImporter() {
    // empty
}

D3MFImporter::~D3MFImporter() {
    // empty
}

bool D3MFImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
    const std::string extension = GetExtension(pFile);
    if(extension == Extension ) {
        return true;
    } else if ( !extension.length() || checkSig ) {
        if (nullptr == pIOHandler ) {
            return true;
        }
    }

    return false;
}

void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
    // empty
}

const aiImporterDesc *D3MFImporter::GetInfo() const {
    return &desc;
}

void D3MFImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
    D3MF::D3MFOpcPackage opcPackage(pIOHandler, pFile);

    std::unique_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(opcPackage.RootStream()));
    std::unique_ptr<D3MF::XmlReader> xmlReader(irr::io::createIrrXMLReader(xmlStream.get()));

    D3MF::XmlSerializer xmlSerializer(xmlReader.get());

    xmlSerializer.ImportXml(pScene);
}

} // Namespace Assimp

#endif // ASSIMP_BUILD_NO_3MF_IMPORTER