123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634 |
- /*
- Open Asset Import Library (assimp)
- ----------------------------------------------------------------------
- Copyright (c) 2006-2021, 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 "3MFXmlTags.h"
- #include "D3MFOpcPackage.h"
- #include <assimp/StringComparison.h>
- #include <assimp/StringUtils.h>
- #include <assimp/XmlParser.h>
- #include <assimp/ZipArchiveIOSystem.h>
- #include <assimp/importerdesc.h>
- #include <assimp/scene.h>
- #include <assimp/DefaultLogger.hpp>
- #include <assimp/IOSystem.hpp>
- #include <assimp/fast_atof.h>
- #include <cassert>
- #include <map>
- #include <memory>
- #include <string>
- #include <vector>
- #include <iomanip>
- #include <string.h>
- namespace Assimp {
- namespace D3MF {
- enum class ResourceType {
- RT_Object,
- RT_BaseMaterials,
- RT_Unknown
- }; // To be extended with other resource types (eg. material extension resources like Texture2d, Texture2dGroup...)
- class Resource {
- public:
- int mId;
- Resource(int id) :
- mId(id) {
- // empty
- }
- virtual ~Resource() {
- // empty
- }
- virtual ResourceType getType() const {
- return ResourceType::RT_Unknown;
- }
- };
- class BaseMaterials : public Resource {
- public:
- std::vector<aiMaterial *> mMaterials;
- std::vector<unsigned int> mMaterialIndex;
- BaseMaterials(int id) :
- Resource(id),
- mMaterials(),
- mMaterialIndex() {
- // empty
- }
- ~BaseMaterials() = default;
- ResourceType getType() const override {
- return ResourceType::RT_BaseMaterials;
- }
- };
- struct Component {
- int mObjectId;
- aiMatrix4x4 mTransformation;
- };
- class Object : public Resource {
- public:
- std::vector<aiMesh *> mMeshes;
- std::vector<unsigned int> mMeshIndex;
- std::vector<Component> mComponents;
- std::string mName;
- Object(int id) :
- Resource(id),
- mName(std::string("Object_") + ai_to_string(id)) {
- // empty
- }
- ~Object() = default;
- ResourceType getType() const override {
- return ResourceType::RT_Object;
- }
- };
- class XmlSerializer {
- public:
- XmlSerializer(XmlParser *xmlParser) :
- mResourcesDictionnary(),
- mMaterialCount(0),
- mMeshCount(0),
- mXmlParser(xmlParser) {
- // empty
- }
- ~XmlSerializer() {
- for (auto it = mResourcesDictionnary.begin(); it != mResourcesDictionnary.end(); ++it ) {
- delete it->second;
- }
- }
- void ImportXml(aiScene *scene) {
- if (nullptr == scene) {
- return;
- }
- scene->mRootNode = new aiNode(XmlTag::RootTag);
- XmlNode node = mXmlParser->getRootNode().child(XmlTag::model);
- if (node.empty()) {
- return;
- }
- XmlNode resNode = node.child(XmlTag::resources);
- for (auto ¤tNode : resNode.children()) {
- const std::string currentNodeName = currentNode.name();
- if (currentNodeName == XmlTag::object) {
- ReadObject(currentNode);
- } else if (currentNodeName == XmlTag::basematerials) {
- ReadBaseMaterials(currentNode);
- } else if (currentNodeName == XmlTag::meta) {
- ReadMetadata(currentNode);
- }
- }
- XmlNode buildNode = node.child(XmlTag::build);
- for (auto ¤tNode : buildNode.children()) {
- const std::string currentNodeName = currentNode.name();
- if (currentNodeName == XmlTag::item) {
- int objectId = -1;
- std::string transformationMatrixStr;
- aiMatrix4x4 transformationMatrix;
- getNodeAttribute(currentNode, D3MF::XmlTag::objectid, objectId);
- bool hasTransform = getNodeAttribute(currentNode, D3MF::XmlTag::transform, transformationMatrixStr);
- auto it = mResourcesDictionnary.find(objectId);
- if (it != mResourcesDictionnary.end() && it->second->getType() == ResourceType::RT_Object) {
- Object *obj = static_cast<Object *>(it->second);
- if (hasTransform) {
- transformationMatrix = parseTransformMatrix(transformationMatrixStr);
- }
- addObjectToNode(scene->mRootNode, obj, transformationMatrix);
- }
- }
- }
- // import the metadata
- if (!mMetaData.empty()) {
- const size_t numMeta = mMetaData.size();
- scene->mMetaData = aiMetadata::Alloc(static_cast<unsigned int>(numMeta));
- for (size_t i = 0; i < numMeta; ++i) {
- aiString val(mMetaData[i].value);
- scene->mMetaData->Set(static_cast<unsigned int>(i), mMetaData[i].name, val);
- }
- }
- // import the meshes
- scene->mNumMeshes = static_cast<unsigned int>(mMeshCount);
- if (scene->mNumMeshes != 0) {
- scene->mMeshes = new aiMesh *[scene->mNumMeshes]();
- for (auto it = mResourcesDictionnary.begin(); it != mResourcesDictionnary.end(); ++it) {
- if (it->second->getType() == ResourceType::RT_Object) {
- Object *obj = static_cast<Object *>(it->second);
- ai_assert(nullptr != obj);
- for (unsigned int i = 0; i < obj->mMeshes.size(); ++i) {
- scene->mMeshes[obj->mMeshIndex[i]] = obj->mMeshes[i];
- }
- }
- }
- }
- // import the materials
- scene->mNumMaterials = mMaterialCount;
- if (scene->mNumMaterials != 0) {
- scene->mMaterials = new aiMaterial *[scene->mNumMaterials];
- for (auto it = mResourcesDictionnary.begin(); it != mResourcesDictionnary.end(); ++it) {
- if (it->second->getType() == ResourceType::RT_BaseMaterials) {
- BaseMaterials *baseMaterials = static_cast<BaseMaterials *>(it->second);
- for (unsigned int i = 0; i < baseMaterials->mMaterials.size(); ++i) {
- scene->mMaterials[baseMaterials->mMaterialIndex[i]] = baseMaterials->mMaterials[i];
- }
- }
- }
- }
- }
- private:
- void addObjectToNode(aiNode *parent, Object *obj, aiMatrix4x4 nodeTransform) {
- ai_assert(nullptr != obj);
- aiNode *sceneNode = new aiNode(obj->mName);
- sceneNode->mNumMeshes = static_cast<unsigned int>(obj->mMeshes.size());
- sceneNode->mMeshes = new unsigned int[sceneNode->mNumMeshes];
- std::copy(obj->mMeshIndex.begin(), obj->mMeshIndex.end(), sceneNode->mMeshes);
- sceneNode->mTransformation = nodeTransform;
- if (nullptr != parent) {
- parent->addChildren(1, &sceneNode);
- }
- for (size_t i = 0; i < obj->mComponents.size(); ++i) {
- Component c = obj->mComponents[i];
- auto it = mResourcesDictionnary.find(c.mObjectId);
- if (it != mResourcesDictionnary.end() && it->second->getType() == ResourceType::RT_Object) {
- addObjectToNode(sceneNode, static_cast<Object *>(it->second), c.mTransformation);
- }
- }
- }
- bool getNodeAttribute(const XmlNode &node, const std::string &attribute, std::string &value) {
- pugi::xml_attribute objectAttribute = node.attribute(attribute.c_str());
- if (!objectAttribute.empty()) {
- value = objectAttribute.as_string();
- return true;
- }
- return false;
- }
- bool getNodeAttribute(const XmlNode &node, const std::string &attribute, int &value) {
- std::string strValue;
- bool ret = getNodeAttribute(node, attribute, strValue);
- if (ret) {
- value = std::atoi(strValue.c_str());
- return true;
- }
- return false;
- }
- aiMatrix4x4 parseTransformMatrix(std::string matrixStr) {
- // split the string
- std::vector<float> numbers;
- std::string currentNumber;
- for (size_t i = 0; i < matrixStr.size(); ++i) {
- const char c = matrixStr[i];
- if (c == ' ') {
- if (currentNumber.size() > 0) {
- float f = std::stof(currentNumber);
- numbers.push_back(f);
- currentNumber.clear();
- }
- } else {
- currentNumber.push_back(c);
- }
- }
- if (currentNumber.size() > 0) {
- const float f = std::stof(currentNumber);
- numbers.push_back(f);
- }
- aiMatrix4x4 transformMatrix;
- transformMatrix.a1 = numbers[0];
- transformMatrix.b1 = numbers[1];
- transformMatrix.c1 = numbers[2];
- transformMatrix.d1 = 0;
- transformMatrix.a2 = numbers[3];
- transformMatrix.b2 = numbers[4];
- transformMatrix.c2 = numbers[5];
- transformMatrix.d2 = 0;
- transformMatrix.a3 = numbers[6];
- transformMatrix.b3 = numbers[7];
- transformMatrix.c3 = numbers[8];
- transformMatrix.d3 = 0;
- transformMatrix.a4 = numbers[9];
- transformMatrix.b4 = numbers[10];
- transformMatrix.c4 = numbers[11];
- transformMatrix.d4 = 1;
- return transformMatrix;
- }
- void ReadObject(XmlNode &node) {
- int id = -1, pid = -1, pindex = -1;
- bool hasId = getNodeAttribute(node, XmlTag::id, id);
- bool hasPid = getNodeAttribute(node, XmlTag::pid, pid);
- bool hasPindex = getNodeAttribute(node, XmlTag::pindex, pindex);
- if (!hasId) {
- return;
- }
- Object *obj = new Object(id);
- for (XmlNode ¤tNode : node.children()) {
- const std::string ¤tName = currentNode.name();
- if (currentName == D3MF::XmlTag::mesh) {
- auto mesh = ReadMesh(currentNode);
- mesh->mName.Set(ai_to_string(id));
- if (hasPid) {
- auto it = mResourcesDictionnary.find(pid);
- if (hasPindex && it != mResourcesDictionnary.end() && it->second->getType() == ResourceType::RT_BaseMaterials) {
- BaseMaterials *materials = static_cast<BaseMaterials *>(it->second);
- mesh->mMaterialIndex = materials->mMaterialIndex[pindex];
- }
- }
- obj->mMeshes.push_back(mesh);
- obj->mMeshIndex.push_back(mMeshCount);
- mMeshCount++;
- } else if (currentName == D3MF::XmlTag::components) {
- for (XmlNode ¤tSubNode : currentNode.children()) {
- const std::string subNodeName = currentSubNode.name();
- if (subNodeName == D3MF::XmlTag::component) {
- int objectId = -1;
- std::string componentTransformStr;
- aiMatrix4x4 componentTransform;
- if (getNodeAttribute(currentSubNode, D3MF::XmlTag::transform, componentTransformStr)) {
- componentTransform = parseTransformMatrix(componentTransformStr);
- }
- if (getNodeAttribute(currentSubNode, D3MF::XmlTag::objectid, objectId)) {
- obj->mComponents.push_back({ objectId, componentTransform });
- }
- }
- }
- }
- }
- mResourcesDictionnary.insert(std::make_pair(id, obj));
- }
- aiMesh *ReadMesh(XmlNode &node) {
- aiMesh *mesh = new aiMesh();
- for (XmlNode ¤tNode : node.children()) {
- const std::string currentName = currentNode.name();
- if (currentName == XmlTag::vertices) {
- ImportVertices(currentNode, mesh);
- } else if (currentName == XmlTag::triangles) {
- ImportTriangles(currentNode, mesh);
- }
- }
- return mesh;
- }
- void ReadMetadata(XmlNode &node) {
- pugi::xml_attribute attribute = node.attribute(D3MF::XmlTag::meta_name);
- const std::string name = attribute.as_string();
- const std::string value = node.value();
- if (name.empty()) {
- return;
- }
- MetaEntry entry;
- entry.name = name;
- entry.value = value;
- mMetaData.push_back(entry);
- }
- void ImportVertices(XmlNode &node, aiMesh *mesh) {
- std::vector<aiVector3D> vertices;
- for (XmlNode ¤tNode : node.children()) {
- const std::string currentName = currentNode.name();
- if (currentName == XmlTag::vertex) {
- vertices.push_back(ReadVertex(currentNode));
- }
- }
- mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
- mesh->mVertices = new aiVector3D[mesh->mNumVertices];
- std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
- }
- aiVector3D ReadVertex(XmlNode &node) {
- aiVector3D vertex;
- vertex.x = ai_strtof(node.attribute(XmlTag::x).as_string(), nullptr);
- vertex.y = ai_strtof(node.attribute(XmlTag::y).as_string(), nullptr);
- vertex.z = ai_strtof(node.attribute(XmlTag::z).as_string(), nullptr);
- return vertex;
- }
- void ImportTriangles(XmlNode &node, aiMesh *mesh) {
- std::vector<aiFace> faces;
- for (XmlNode ¤tNode : node.children()) {
- const std::string currentName = currentNode.name();
- if (currentName == XmlTag::triangle) {
- aiFace face = ReadTriangle(currentNode);
- faces.push_back(face);
- int pid = 0, p1 = 0;
- bool hasPid = getNodeAttribute(currentNode, D3MF::XmlTag::pid, pid);
- bool hasP1 = getNodeAttribute(currentNode, D3MF::XmlTag::p1, p1);
- if (hasPid && hasP1) {
- auto it = mResourcesDictionnary.find(pid);
- if (it != mResourcesDictionnary.end()) {
- if (it->second->getType() == ResourceType::RT_BaseMaterials) {
- BaseMaterials *baseMaterials = static_cast<BaseMaterials *>(it->second);
- mesh->mMaterialIndex = baseMaterials->mMaterialIndex[p1];
- }
- // TODO: manage the separation into several meshes if the triangles of the mesh do not all refer to the same material
- }
- }
- }
- }
- 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(XmlNode &node) {
- aiFace face;
- face.mNumIndices = 3;
- face.mIndices = new unsigned int[face.mNumIndices];
- face.mIndices[0] = static_cast<unsigned int>(std::atoi(node.attribute(XmlTag::v1).as_string()));
- face.mIndices[1] = static_cast<unsigned int>(std::atoi(node.attribute(XmlTag::v2).as_string()));
- face.mIndices[2] = static_cast<unsigned int>(std::atoi(node.attribute(XmlTag::v3).as_string()));
- return face;
- }
- void ReadBaseMaterials(XmlNode &node) {
- int id = -1;
- if (getNodeAttribute(node, D3MF::XmlTag::basematerials_id, id)) {
- BaseMaterials *baseMaterials = new BaseMaterials(id);
- for (XmlNode ¤tNode : node.children()) {
- const std::string currentName = currentNode.name();
- if (currentName == XmlTag::basematerials_base) {
- baseMaterials->mMaterialIndex.push_back(mMaterialCount);
- baseMaterials->mMaterials.push_back(readMaterialDef(currentNode, id));
- ++mMaterialCount;
- }
- }
- mResourcesDictionnary.insert(std::make_pair(id, baseMaterials));
- }
- }
- bool parseColor(const char *color, aiColor4D &diffuse) {
- if (nullptr == color) {
- return false;
- }
- //format of the color string: #RRGGBBAA or #RRGGBB (3MF Core chapter 5.1.1)
- const size_t len = strlen(color);
- if (9 != len && 7 != len) {
- return false;
- }
- const char *buf(color);
- if ('#' != buf[0]) {
- return false;
- }
- char r[3] = { buf[1], buf[2], '\0' };
- diffuse.r = static_cast<ai_real>(strtol(r, nullptr, 16)) / ai_real(255.0);
- char g[3] = { buf[3], buf[4], '\0' };
- diffuse.g = static_cast<ai_real>(strtol(g, nullptr, 16)) / ai_real(255.0);
- char b[3] = { buf[5], buf[6], '\0' };
- diffuse.b = static_cast<ai_real>(strtol(b, nullptr, 16)) / ai_real(255.0);
- if (7 == len)
- return true;
- char a[3] = { buf[7], buf[8], '\0' };
- diffuse.a = static_cast<ai_real>(strtol(a, nullptr, 16)) / ai_real(255.0);
- return true;
- }
- void assignDiffuseColor(XmlNode &node, aiMaterial *mat) {
- const char *color = node.attribute(XmlTag::basematerials_displaycolor).as_string();
- aiColor4D diffuse;
- if (parseColor(color, diffuse)) {
- mat->AddProperty<aiColor4D>(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
- }
- }
- aiMaterial *readMaterialDef(XmlNode &node, unsigned int basematerialsId) {
- aiMaterial *material = new aiMaterial();
- material->mNumProperties = 0;
- std::string name;
- bool hasName = getNodeAttribute(node, D3MF::XmlTag::basematerials_name, name);
- std::string stdMaterialName;
- const std::string strId(ai_to_string(basematerialsId));
- stdMaterialName += "id";
- stdMaterialName += strId;
- stdMaterialName += "_";
- if (hasName) {
- stdMaterialName += std::string(name);
- } else {
- stdMaterialName += "basemat_";
- stdMaterialName += ai_to_string(mMaterialCount - basematerialsId);
- }
- aiString assimpMaterialName(stdMaterialName);
- material->AddProperty(&assimpMaterialName, AI_MATKEY_NAME);
- assignDiffuseColor(node, material);
- return material;
- }
- private:
- struct MetaEntry {
- std::string name;
- std::string value;
- };
- std::vector<MetaEntry> mMetaData;
- std::map<unsigned int, Resource *> mResourcesDictionnary;
- unsigned int mMaterialCount, mMeshCount;
- XmlParser *mXmlParser;
- };
- } //namespace D3MF
- using namespace D3MF;
- static const aiImporterDesc desc = {
- "3mf Importer",
- "",
- "",
- "http://3mf.io/",
- aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
- 0,
- 0,
- 0,
- 0,
- "3mf"
- };
- D3MFImporter::D3MFImporter() :
- BaseImporter() {
- // empty
- }
- D3MFImporter::~D3MFImporter() {
- // empty
- }
- bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig) const {
- const std::string extension(GetExtension(filename));
- if (extension == desc.mFileExtensions) {
- return true;
- } else if (!extension.length() || checkSig) {
- if (nullptr == pIOHandler) {
- return false;
- }
- if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) {
- return false;
- }
- D3MFOpcPackage opcPackage(pIOHandler, filename);
- return opcPackage.validate();
- }
- return false;
- }
- void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
- // empty
- }
- const aiImporterDesc *D3MFImporter::GetInfo() const {
- return &desc;
- }
- void D3MFImporter::InternReadFile(const std::string &filename, aiScene *pScene, IOSystem *pIOHandler) {
- D3MFOpcPackage opcPackage(pIOHandler, filename);
- XmlParser xmlParser;
- if (xmlParser.parse(opcPackage.RootStream())) {
- XmlSerializer xmlSerializer(&xmlParser);
- xmlSerializer.ImportXml(pScene);
- }
- }
- } // Namespace Assimp
- #endif // ASSIMP_BUILD_NO_3MF_IMPORTER
|