assimp/code/AssetLib/3MF/D3MFImporter.cpp

454 lines
15 KiB
C++
Raw Normal View History

2016-05-07 12:16:33 +00:00
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2020, assimp team
2018-01-28 18:42:05 +00:00
2016-05-07 12:16:33 +00:00
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.
----------------------------------------------------------------------
*/
2017-01-10 14:53:11 +00:00
#ifndef ASSIMP_BUILD_NO_3MF_IMPORTER
2016-05-07 12:16:33 +00:00
#include "D3MFImporter.h"
#include <assimp/StringComparison.h>
#include <assimp/StringUtils.h>
2020-08-18 14:54:29 +00:00
#include <assimp/XmlParser.h>
#include <assimp/ZipArchiveIOSystem.h>
#include <assimp/importerdesc.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/IOSystem.hpp>
2016-05-07 12:16:33 +00:00
#include <cassert>
#include <map>
2016-05-07 12:16:33 +00:00
#include <memory>
#include <string>
#include <vector>
2016-05-07 12:16:33 +00:00
2017-11-20 21:36:17 +00:00
#include "3MFXmlTags.h"
#include "D3MFOpcPackage.h"
#include <assimp/fast_atof.h>
#include <iomanip>
2016-05-07 12:16:33 +00:00
namespace Assimp {
namespace D3MF {
2017-11-20 21:36:17 +00:00
class XmlSerializer {
2016-05-07 12:16:33 +00:00
public:
using MatArray = std::vector<aiMaterial *>;
using MatId2MatArray = std::map<unsigned int, std::vector<unsigned int>>;
2020-08-18 14:54:29 +00:00
XmlSerializer(XmlParser *xmlParser) :
mMeshes(),
mMatArray(),
mActiveMatGroup(99999999),
mMatId2MatArray(),
2020-08-18 14:54:29 +00:00
mXmlParser(xmlParser) {
// empty
2016-05-07 12:16:33 +00:00
}
2017-11-20 21:36:17 +00:00
~XmlSerializer() {
2017-12-07 16:50:18 +00:00
// empty
2017-11-20 21:36:17 +00:00
}
void ImportXml(aiScene *scene) {
if (nullptr == scene) {
return;
}
2016-05-07 12:16:33 +00:00
scene->mRootNode = new aiNode();
std::vector<aiNode *> children;
2016-05-07 12:16:33 +00:00
2018-03-20 17:43:54 +00:00
std::string nodeName;
2020-09-02 19:49:20 +00:00
XmlNode node = mXmlParser->getRootNode().child("model");
if (node.empty()) {
return;
}
XmlNode resNode = node.child("resources");
for (XmlNode currentNode = resNode.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-18 14:54:29 +00:00
const std::string &currentNodeName = currentNode.name();
if (currentNodeName == D3MF::XmlTag::object) {
children.push_back(ReadObject(currentNode, scene));
} else if (currentNodeName == D3MF::XmlTag::build) {
//
2020-08-18 14:54:29 +00:00
} else if (currentNodeName == D3MF::XmlTag::basematerials) {
ReadBaseMaterials(currentNode);
} else if (currentNodeName == D3MF::XmlTag::meta) {
ReadMetadata(currentNode);
}
2017-03-27 22:35:56 +00:00
}
2016-05-07 12:16:33 +00:00
if (scene->mRootNode->mName.length == 0) {
scene->mRootNode->mName.Set("3MF");
2017-11-20 21:36:17 +00:00
}
2018-03-20 13:10:08 +00:00
// 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);
2018-03-20 09:09:47 +00:00
}
}
2018-03-20 13:10:08 +00:00
// import the meshes
scene->mNumMeshes = static_cast<unsigned int>(mMeshes.size());
scene->mMeshes = new aiMesh *[scene->mNumMeshes]();
std::copy(mMeshes.begin(), mMeshes.end(), scene->mMeshes);
2016-05-07 12:16:33 +00:00
2018-03-20 13:10:08 +00:00
// import the materials
scene->mNumMaterials = static_cast<unsigned int>(mMatArray.size());
if (0 != scene->mNumMaterials) {
scene->mMaterials = new aiMaterial *[scene->mNumMaterials];
std::copy(mMatArray.begin(), mMatArray.end(), scene->mMaterials);
2018-02-18 20:30:23 +00:00
}
2018-03-20 13:10:08 +00:00
2020-08-31 14:10:38 +00:00
// create the scene-graph
2016-05-07 12:16:33 +00:00
scene->mRootNode->mNumChildren = static_cast<unsigned int>(children.size());
scene->mRootNode->mChildren = new aiNode *[scene->mRootNode->mNumChildren]();
2016-05-07 12:16:33 +00:00
std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
}
private:
2020-08-18 14:54:29 +00:00
aiNode *ReadObject(XmlNode &node, aiScene *scene) {
std::unique_ptr<aiNode> nodePtr(new aiNode());
2016-05-07 12:16:33 +00:00
std::vector<unsigned long> meshIds;
2016-10-18 19:16:44 +00:00
std::string name, type;
2020-08-18 14:54:29 +00:00
pugi::xml_attribute attr = node.attribute(D3MF::XmlTag::id.c_str());
if (!attr.empty()) {
name = attr.as_string();
2016-10-18 19:16:44 +00:00
}
2020-09-02 19:49:20 +00:00
attr = node.attribute(D3MF::XmlTag::type.c_str());
2020-08-18 14:54:29 +00:00
if (!attr.empty()) {
type = attr.as_string();
2016-10-18 19:16:44 +00:00
}
2016-05-07 12:16:33 +00:00
2020-08-18 14:54:29 +00:00
nodePtr->mParent = scene->mRootNode;
nodePtr->mName.Set(name);
2016-05-07 12:16:33 +00:00
size_t meshIdx = mMeshes.size();
2016-05-07 12:16:33 +00:00
2020-08-28 14:17:56 +00:00
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-18 14:54:29 +00:00
const std::string &currentName = currentNode.name();
if (currentName == D3MF::XmlTag::mesh) {
auto mesh = ReadMesh(currentNode);
2016-05-07 12:16:33 +00:00
mesh->mName.Set(name);
mMeshes.push_back(mesh);
meshIds.push_back(static_cast<unsigned long>(meshIdx));
++meshIdx;
2016-05-07 12:16:33 +00:00
}
}
2020-08-18 14:54:29 +00:00
nodePtr->mNumMeshes = static_cast<unsigned int>(meshIds.size());
2016-05-07 12:16:33 +00:00
2020-08-18 14:54:29 +00:00
nodePtr->mMeshes = new unsigned int[nodePtr->mNumMeshes];
2016-05-07 12:16:33 +00:00
2020-08-18 14:54:29 +00:00
std::copy(meshIds.begin(), meshIds.end(), nodePtr->mMeshes);
2016-05-07 12:16:33 +00:00
2020-08-18 14:54:29 +00:00
return nodePtr.release();
2016-05-07 12:16:33 +00:00
}
2020-08-18 14:54:29 +00:00
aiMesh *ReadMesh(XmlNode &node) {
aiMesh *mesh = new aiMesh();
2020-08-28 14:17:56 +00:00
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-18 14:54:29 +00:00
const std::string &currentName = currentNode.name();
if (currentName == D3MF::XmlTag::vertices) {
ImportVertices(currentNode, mesh);
} else if (currentName == D3MF::XmlTag::triangles) {
ImportTriangles(currentNode, mesh);
2016-05-07 12:16:33 +00:00
}
2020-08-18 14:54:29 +00:00
2016-05-07 12:16:33 +00:00
}
return mesh;
}
2020-08-18 14:54:29 +00:00
void ReadMetadata(XmlNode &node) {
pugi::xml_attribute attribute = node.attribute(D3MF::XmlTag::meta_name.c_str());
const std::string name = attribute.as_string();
const std::string value = node.value();
if (name.empty()) {
2018-03-20 09:09:47 +00:00
return;
}
MetaEntry entry;
entry.name = name;
entry.value = value;
mMetaData.push_back(entry);
2018-03-20 09:09:47 +00:00
}
2020-08-18 14:54:29 +00:00
void ImportVertices(XmlNode &node, aiMesh *mesh) {
2016-10-16 15:14:22 +00:00
std::vector<aiVector3D> vertices;
2020-08-28 14:17:56 +00:00
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-18 14:54:29 +00:00
const std::string &currentName = currentNode.name();
if (currentName == D3MF::XmlTag::vertex) {
vertices.push_back(ReadVertex(currentNode));
2016-05-07 12:16:33 +00:00
}
}
2020-08-18 14:54:29 +00:00
2016-05-07 12:16:33 +00:00
mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
}
2017-11-20 21:36:17 +00:00
2020-08-18 14:54:29 +00:00
aiVector3D ReadVertex(XmlNode &node) {
2016-05-07 12:16:33 +00:00
aiVector3D vertex;
2020-08-18 14:54:29 +00:00
vertex.x = ai_strtof(node.attribute(D3MF::XmlTag::x.c_str()).as_string(), nullptr);
2020-09-02 19:49:20 +00:00
vertex.y = ai_strtof(node.attribute(D3MF::XmlTag::y.c_str()).as_string(), nullptr);
2020-08-18 14:54:29 +00:00
vertex.z = ai_strtof(node.attribute(D3MF::XmlTag::z.c_str()).as_string(), nullptr);
2016-05-07 12:16:33 +00:00
return vertex;
}
2020-08-18 14:54:29 +00:00
void ImportTriangles(XmlNode &node, aiMesh *mesh) {
std::vector<aiFace> faces;
2020-08-28 14:17:56 +00:00
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
2020-08-18 14:54:29 +00:00
const std::string &currentName = currentNode.name();
if (currentName == D3MF::XmlTag::triangle) {
faces.push_back(ReadTriangle(currentNode));
const char *pidToken = currentNode.attribute(D3MF::XmlTag::p1.c_str()).as_string();
if (nullptr != pidToken) {
int matIdx(std::atoi(pidToken));
mesh->mMaterialIndex = matIdx;
}
}
}
2016-05-07 12:16:33 +00:00
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);
}
2020-08-18 14:54:29 +00:00
aiFace ReadTriangle(XmlNode &node) {
2016-05-07 12:16:33 +00:00
aiFace face;
2016-05-07 12:16:33 +00:00
face.mNumIndices = 3;
face.mIndices = new unsigned int[face.mNumIndices];
2020-08-18 14:54:29 +00:00
face.mIndices[0] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v1.c_str()).as_string()));
face.mIndices[1] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v2.c_str()).as_string()));
face.mIndices[2] = static_cast<unsigned int>(std::atoi(node.attribute(D3MF::XmlTag::v3.c_str()).as_string()));
2016-05-07 12:16:33 +00:00
return face;
}
2020-08-18 14:54:29 +00:00
void ReadBaseMaterials(XmlNode &node) {
std::vector<unsigned int> MatIdArray;
2020-08-18 14:54:29 +00:00
const char *baseMaterialId = node.attribute(D3MF::XmlTag::basematerials_id.c_str()).as_string();
if (nullptr != baseMaterialId) {
unsigned int id = std::atoi(baseMaterialId);
const size_t newMatIdx(mMatArray.size());
if (id != mActiveMatGroup) {
mActiveMatGroup = id;
MatId2MatArray::const_iterator it(mMatId2MatArray.find(id));
if (mMatId2MatArray.end() == it) {
MatIdArray.clear();
mMatId2MatArray[id] = MatIdArray;
} else {
MatIdArray = it->second;
}
}
MatIdArray.push_back(static_cast<unsigned int>(newMatIdx));
mMatId2MatArray[mActiveMatGroup] = MatIdArray;
}
2020-08-18 14:54:29 +00:00
mMatArray.push_back(readMaterialDef(node));
}
bool parseColor(const char *color, aiColor4D &diffuse) {
if (nullptr == color) {
return false;
}
2018-09-05 13:49:15 +00:00
//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) {
return false;
}
++buf;
char comp[3] = { 0, 0, '\0' };
comp[0] = *buf;
++buf;
comp[1] = *buf;
++buf;
diffuse.r = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
comp[0] = *buf;
++buf;
comp[1] = *buf;
++buf;
diffuse.g = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
comp[0] = *buf;
++buf;
comp[1] = *buf;
++buf;
diffuse.b = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
if (7 == len)
2018-09-05 13:49:15 +00:00
return true;
comp[0] = *buf;
++buf;
comp[1] = *buf;
++buf;
diffuse.a = static_cast<ai_real>(strtol(comp, nullptr, 16)) / ai_real(255.0);
return true;
}
2020-08-18 14:54:29 +00:00
void assignDiffuseColor(XmlNode &node, aiMaterial *mat) {
const char *color = node.attribute(D3MF::XmlTag::basematerials_displaycolor.c_str()).as_string();
aiColor4D diffuse;
if (parseColor(color, diffuse)) {
mat->AddProperty<aiColor4D>(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
}
}
2020-08-18 14:54:29 +00:00
aiMaterial *readMaterialDef(XmlNode &node) {
aiMaterial *mat(nullptr);
const char *name(nullptr);
2020-08-18 14:54:29 +00:00
const std::string nodeName = node.name();
if (nodeName == D3MF::XmlTag::basematerials_base) {
2020-08-18 14:54:29 +00:00
name = node.attribute(D3MF::XmlTag::basematerials_name.c_str()).as_string();
std::string stdMatName;
aiString matName;
std::string strId(to_string(mActiveMatGroup));
stdMatName += "id";
stdMatName += strId;
stdMatName += "_";
if (nullptr != name) {
stdMatName += std::string(name);
} else {
stdMatName += "basemat";
}
matName.Set(stdMatName);
mat = new aiMaterial;
mat->AddProperty(&matName, AI_MATKEY_NAME);
2020-08-18 14:54:29 +00:00
assignDiffuseColor(node, mat);
}
2018-02-18 20:30:23 +00:00
return mat;
}
2016-05-07 12:16:33 +00:00
private:
2018-03-20 09:09:47 +00:00
struct MetaEntry {
std::string name;
std::string value;
};
std::vector<MetaEntry> mMetaData;
std::vector<aiMesh *> mMeshes;
MatArray mMatArray;
unsigned int mActiveMatGroup;
MatId2MatArray mMatId2MatArray;
2020-08-18 14:54:29 +00:00
XmlParser *mXmlParser;
2016-05-07 12:16:33 +00:00
};
} //namespace D3MF
static const aiImporterDesc desc = {
"3mf Importer",
"",
"",
"http://3mf.io/",
aiImporterFlags_SupportBinaryFlavour | aiImporterFlags_SupportCompressedFlavour,
0,
0,
0,
0,
"3mf"
2016-05-07 12:16:33 +00:00
};
D3MFImporter::D3MFImporter() :
BaseImporter() {
2017-11-20 21:36:17 +00:00
// empty
2016-05-07 12:16:33 +00:00
}
2017-11-20 21:36:17 +00:00
D3MFImporter::~D3MFImporter() {
// empty
2016-05-07 12:16:33 +00:00
}
bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig) const {
const std::string extension(GetExtension(filename));
if (extension == desc.mFileExtensions) {
2016-05-07 12:16:33 +00:00
return true;
} else if (!extension.length() || checkSig) {
if (nullptr == pIOHandler) {
return false;
}
if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) {
return false;
}
D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
return opcPackage.validate();
2016-05-07 12:16:33 +00:00
}
return false;
}
2017-11-20 21:36:17 +00:00
void D3MFImporter::SetupProperties(const Importer * /*pImp*/) {
// empty
2016-05-07 12:16:33 +00:00
}
2017-11-20 21:36:17 +00:00
const aiImporterDesc *D3MFImporter::GetInfo() const {
2016-05-07 12:16:33 +00:00
return &desc;
}
void D3MFImporter::InternReadFile(const std::string &filename, aiScene *pScene, IOSystem *pIOHandler) {
2018-03-11 19:15:49 +00:00
D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
2016-05-07 12:16:33 +00:00
2020-08-18 14:54:29 +00:00
XmlParser xmlParser;
if (xmlParser.parse(opcPackage.RootStream())) {
D3MF::XmlSerializer xmlSerializer(&xmlParser);
xmlSerializer.ImportXml(pScene);
}
2016-05-07 12:16:33 +00:00
}
} // Namespace Assimp
2016-05-07 12:16:33 +00:00
#endif // ASSIMP_BUILD_NO_3MF_IMPORTER