2016-05-07 12:16:33 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
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"
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringComparison.h>
|
|
|
|
#include <assimp/StringUtils.h>
|
2020-08-18 14:54:29 +00:00
|
|
|
#include <assimp/XmlParser.h>
|
2019-07-11 11:22:43 +00:00
|
|
|
#include <assimp/ZipArchiveIOSystem.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#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>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <map>
|
2016-05-07 12:16:33 +00:00
|
|
|
#include <memory>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-20 21:36:17 +00:00
|
|
|
#include "3MFXmlTags.h"
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "D3MFOpcPackage.h"
|
2018-02-23 16:49:29 +00:00
|
|
|
#include <assimp/fast_atof.h>
|
|
|
|
|
|
|
|
#include <iomanip>
|
2016-10-10 15:58:06 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
namespace Assimp {
|
|
|
|
namespace D3MF {
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
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:
|
|
|
|
Resource(int id) :
|
|
|
|
mId(id) {}
|
|
|
|
|
|
|
|
virtual ~Resource() {}
|
|
|
|
|
|
|
|
int mId;
|
|
|
|
|
|
|
|
virtual ResourceType getType() {
|
|
|
|
return ResourceType::RT_Unknown;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BaseMaterials : public Resource {
|
|
|
|
public:
|
|
|
|
BaseMaterials(int id) :
|
|
|
|
Resource(id),
|
|
|
|
mMaterials(),
|
|
|
|
mMaterialIndex() {}
|
|
|
|
|
|
|
|
std::vector<aiMaterial *> mMaterials;
|
|
|
|
std::vector<unsigned int> mMaterialIndex;
|
|
|
|
|
|
|
|
virtual ResourceType getType() {
|
|
|
|
return ResourceType::RT_BaseMaterials;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-28 14:47:20 +00:00
|
|
|
struct Component {
|
|
|
|
int mObjectId;
|
|
|
|
aiMatrix4x4 mTransformation;
|
|
|
|
};
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
class Object : public Resource {
|
|
|
|
public:
|
|
|
|
std::vector<aiMesh*> mMeshes;
|
|
|
|
std::vector<unsigned int> mMeshIndex;
|
2020-12-28 14:47:20 +00:00
|
|
|
std::vector<Component> mComponents;
|
2020-12-29 14:46:28 +00:00
|
|
|
std::string mName;
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
Object(int id) :
|
2020-12-29 14:46:28 +00:00
|
|
|
Resource(id),
|
|
|
|
mName (std::string("Object_") + to_string(id)){}
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
virtual ResourceType getType() {
|
|
|
|
return ResourceType::RT_Object;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-11-20 21:36:17 +00:00
|
|
|
class XmlSerializer {
|
2016-05-07 12:16:33 +00:00
|
|
|
public:
|
2018-03-09 10:40:45 +00:00
|
|
|
|
2020-08-18 14:54:29 +00:00
|
|
|
XmlSerializer(XmlParser *xmlParser) :
|
2020-12-28 11:31:54 +00:00
|
|
|
mResourcesDictionnary(),
|
2020-12-26 01:36:24 +00:00
|
|
|
mMaterialCount(0),
|
2020-12-28 11:31:54 +00:00
|
|
|
mMeshCount(0),
|
2020-08-18 14:54:29 +00:00
|
|
|
mXmlParser(xmlParser) {
|
2020-05-02 13:14:38 +00:00
|
|
|
// empty
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:36:17 +00:00
|
|
|
~XmlSerializer() {
|
2020-12-28 11:31:54 +00:00
|
|
|
for (auto it = mResourcesDictionnary.begin(); it != mResourcesDictionnary.end(); it++) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
2017-11-20 21:36:17 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
void ImportXml(aiScene *scene) {
|
|
|
|
if (nullptr == scene) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
scene->mRootNode = new aiNode("3MF");
|
2016-05-07 12:16:33 +00:00
|
|
|
|
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 ¤tNodeName = currentNode.name();
|
|
|
|
if (currentNodeName == D3MF::XmlTag::object) {
|
2020-12-28 11:31:54 +00:00
|
|
|
ReadObject(currentNode);;
|
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-05-06 09:14:04 +00:00
|
|
|
}
|
2017-03-27 22:35:56 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
XmlNode buildNode = node.child("build");
|
|
|
|
for (XmlNode currentNode = buildNode.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
|
|
|
|
const std::string ¤tNodeName = currentNode.name();
|
|
|
|
if (currentNodeName == D3MF::XmlTag::item) {
|
|
|
|
int objectId = -1;
|
2020-12-28 14:47:20 +00:00
|
|
|
std::string transformationMatrixStr;
|
|
|
|
aiMatrix4x4 transformationMatrix;
|
2020-12-28 11:31:54 +00:00
|
|
|
getNodeAttribute(currentNode, D3MF::XmlTag::objectid, objectId);
|
2020-12-28 14:47:20 +00:00
|
|
|
bool hasTransform = getNodeAttribute(currentNode, D3MF::XmlTag::transform, transformationMatrixStr);
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
auto it = mResourcesDictionnary.find(objectId);
|
|
|
|
if (it != mResourcesDictionnary.end() && it->second->getType() == ResourceType::RT_Object) {
|
|
|
|
Object *obj = static_cast<Object *>(it->second);
|
|
|
|
if (hasTransform) {
|
2020-12-28 14:47:20 +00:00
|
|
|
transformationMatrix = parseTransformMatrix(transformationMatrixStr);
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 14:47:20 +00:00
|
|
|
addObjectToNode(scene->mRootNode, obj, transformationMatrix);
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-20 21:36:17 +00:00
|
|
|
}
|
2017-05-06 09:14:04 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
|
2018-03-20 13:10:08 +00:00
|
|
|
// import the metadata
|
2020-05-02 13:14:38 +00:00
|
|
|
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
|
2020-12-29 14:46:28 +00:00
|
|
|
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);
|
|
|
|
for (unsigned int i = 0; i < obj->mMeshes.size(); ++i) {
|
|
|
|
scene->mMeshes[obj->mMeshIndex[i]] = obj->mMeshes[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2018-03-20 13:10:08 +00:00
|
|
|
// import the materials
|
2020-12-26 01:36:24 +00:00
|
|
|
scene->mNumMaterials = static_cast<unsigned int>(mMaterialCount);
|
2020-12-29 14:46:28 +00:00
|
|
|
if (scene->mNumMaterials != 0) {
|
2020-05-02 13:14:38 +00:00
|
|
|
scene->mMaterials = new aiMaterial *[scene->mNumMaterials];
|
2020-12-28 11:31:54 +00:00
|
|
|
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];
|
|
|
|
}
|
2020-12-26 01:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 20:30:23 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-12-26 13:04:45 +00:00
|
|
|
|
2020-12-28 14:47:20 +00:00
|
|
|
void addObjectToNode(aiNode* parent, Object* obj, aiMatrix4x4 nodeTransform) {
|
2020-12-29 14:46:28 +00:00
|
|
|
aiNode *sceneNode = new aiNode(obj->mName);
|
2020-12-28 14:47:20 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:04:45 +00:00
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-12-28 14:47:20 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
2020-12-28 14:47:20 +00:00
|
|
|
if (currentNumber.size() > 0) {
|
|
|
|
float f = std::stof(currentNumber);
|
|
|
|
numbers.push_back(f);
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
2020-12-28 14:47:20 +00:00
|
|
|
|
|
|
|
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;
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
void ReadObject(XmlNode &node) {
|
|
|
|
int id = -1, pid = -1, pindex = -1;
|
2020-12-26 13:04:45 +00:00
|
|
|
bool hasId = getNodeAttribute(node, D3MF::XmlTag::id, id);
|
|
|
|
//bool hasType = getNodeAttribute(node, D3MF::XmlTag::type, type); not used currently
|
|
|
|
bool hasPid = getNodeAttribute(node, D3MF::XmlTag::pid, pid);
|
|
|
|
bool hasPindex = getNodeAttribute(node, D3MF::XmlTag::pindex, pindex);
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
std::string idStr = to_string(id);
|
|
|
|
|
2020-12-26 13:04:45 +00:00
|
|
|
if (!hasId) {
|
2020-12-28 11:31:54 +00:00
|
|
|
return;
|
2016-10-18 19:16:44 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
Object *obj = new Object(id);
|
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 ¤tName = currentNode.name();
|
|
|
|
if (currentName == D3MF::XmlTag::mesh) {
|
|
|
|
auto mesh = ReadMesh(currentNode);
|
2020-12-28 11:31:54 +00:00
|
|
|
mesh->mName.Set(idStr);
|
|
|
|
|
|
|
|
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];
|
|
|
|
}
|
2020-12-26 13:04:45 +00:00
|
|
|
}
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
obj->mMeshes.push_back(mesh);
|
|
|
|
obj->mMeshIndex.push_back(mMeshCount);
|
|
|
|
mMeshCount++;
|
|
|
|
} else if (currentName == D3MF::XmlTag::components) {
|
|
|
|
for (XmlNode currentSubNode = currentNode.first_child(); currentSubNode; currentSubNode = currentSubNode.next_sibling()) {
|
|
|
|
if (currentSubNode.name() == D3MF::XmlTag::component) {
|
|
|
|
int objectId = -1;
|
2020-12-28 14:47:20 +00:00
|
|
|
std::string componentTransformStr;
|
|
|
|
aiMatrix4x4 componentTransform;
|
|
|
|
if (getNodeAttribute(currentSubNode, D3MF::XmlTag::transform, componentTransformStr)) {
|
|
|
|
componentTransform = parseTransformMatrix(componentTransformStr);
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
if (getNodeAttribute(currentSubNode, D3MF::XmlTag::objectid, objectId))
|
2020-12-28 14:47:20 +00:00
|
|
|
obj->mComponents.push_back({ objectId, componentTransform });
|
2020-12-28 11:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
mResourcesDictionnary.insert(std::make_pair(id, obj));
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 14:54:29 +00:00
|
|
|
aiMesh *ReadMesh(XmlNode &node) {
|
2020-05-02 13:14:38 +00:00
|
|
|
aiMesh *mesh = new aiMesh();
|
2020-12-28 11:31:54 +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 ¤tName = 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();
|
2020-05-02 13:14:38 +00:00
|
|
|
if (name.empty()) {
|
2018-03-20 09:09:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaEntry entry;
|
|
|
|
entry.name = name;
|
|
|
|
entry.value = value;
|
2020-05-02 13:14:38 +00:00
|
|
|
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 ¤tName = 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-05-06 09:14:04 +00:00
|
|
|
}
|
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) {
|
2020-05-02 13:14:38 +00:00
|
|
|
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 ¤tName = currentNode.name();
|
|
|
|
if (currentName == D3MF::XmlTag::triangle) {
|
2020-12-28 11:31:54 +00:00
|
|
|
aiFace face = ReadTriangle(currentNode);
|
|
|
|
faces.push_back(face);
|
|
|
|
|
|
|
|
int pid, p1;
|
|
|
|
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
|
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
2017-05-06 09:14:04 +00:00
|
|
|
|
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) {
|
2020-12-28 11:31:54 +00:00
|
|
|
int id = -1;
|
|
|
|
if (getNodeAttribute(node, D3MF::XmlTag::basematerials_id, id)) {
|
|
|
|
BaseMaterials* baseMaterials = new BaseMaterials(id);
|
2020-12-26 01:36:24 +00:00
|
|
|
|
|
|
|
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling())
|
|
|
|
{
|
|
|
|
if (currentNode.name() == D3MF::XmlTag::basematerials_base) {
|
2020-12-28 11:31:54 +00:00
|
|
|
baseMaterials->mMaterialIndex.push_back(mMaterialCount);
|
|
|
|
baseMaterials->mMaterials.push_back(readMaterialDef(currentNode, id));
|
2020-12-26 01:36:24 +00:00
|
|
|
mMaterialCount++;
|
2018-03-09 10:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
mResourcesDictionnary.insert(std::make_pair(id, baseMaterials));
|
2020-12-26 01:36:24 +00:00
|
|
|
}
|
2018-02-12 21:07:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
bool parseColor(const char *color, aiColor4D &diffuse) {
|
|
|
|
if (nullptr == color) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-05 13:49:15 +00:00
|
|
|
//format of the color string: #RRGGBBAA or #RRGGBB (3MF Core chapter 5.1.1)
|
2020-05-02 13:14:38 +00:00
|
|
|
const size_t len(strlen(color));
|
|
|
|
if (9 != len && 7 != len) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
const char *buf(color);
|
2020-12-28 11:31:54 +00:00
|
|
|
if ('#' != buf[0]) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
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);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
if (7 == len)
|
2018-09-05 13:49:15 +00:00
|
|
|
return true;
|
2020-12-28 11:31:54 +00:00
|
|
|
|
|
|
|
char a[3] = { buf[7], buf[8], '\0' };
|
|
|
|
diffuse.a = static_cast<ai_real>(strtol(a, nullptr, 16)) / ai_real(255.0);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
|
|
|
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();
|
2018-03-09 10:40:45 +00:00
|
|
|
aiColor4D diffuse;
|
2020-05-02 13:14:38 +00:00
|
|
|
if (parseColor(color, diffuse)) {
|
|
|
|
mat->AddProperty<aiColor4D>(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2018-03-09 10:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-18 14:54:29 +00:00
|
|
|
|
2020-12-26 01:36:24 +00:00
|
|
|
aiMaterial *readMaterialDef(XmlNode &node, unsigned int basematerialsId) {
|
2020-12-28 11:31:54 +00:00
|
|
|
aiMaterial *material = new aiMaterial();
|
|
|
|
material->mNumProperties = 0;
|
|
|
|
std::string name;
|
|
|
|
bool hasName = getNodeAttribute(node, D3MF::XmlTag::basematerials_name, name);
|
|
|
|
|
|
|
|
std::string stdMaterialName;
|
|
|
|
std::string strId(to_string(basematerialsId));
|
|
|
|
stdMaterialName += "id";
|
|
|
|
stdMaterialName += strId;
|
|
|
|
stdMaterialName += "_";
|
|
|
|
if (hasName) {
|
|
|
|
stdMaterialName += std::string(name);
|
|
|
|
} else {
|
|
|
|
stdMaterialName += "basemat_";
|
|
|
|
stdMaterialName += to_string(mMaterialCount - basematerialsId);
|
|
|
|
}
|
2018-03-09 10:40:45 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
aiString assimpMaterialName(stdMaterialName);
|
|
|
|
material->AddProperty(&assimpMaterialName, AI_MATKEY_NAME);
|
2018-02-23 16:49:29 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
assignDiffuseColor(node, material);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
2020-12-28 11:31:54 +00:00
|
|
|
return material;
|
2018-02-12 21:07:12 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2020-12-28 11:31:54 +00:00
|
|
|
std::map<unsigned int, Resource*> mResourcesDictionnary;
|
|
|
|
unsigned int mMaterialCount, mMeshCount;
|
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,
|
2018-11-23 19:04:16 +00:00
|
|
|
"3mf"
|
2016-05-07 12:16:33 +00:00
|
|
|
};
|
|
|
|
|
2020-05-02 13:14:38 +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
|
|
|
}
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
bool D3MFImporter::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig) const {
|
2020-05-02 13:14:38 +00:00
|
|
|
const std::string extension(GetExtension(filename));
|
|
|
|
if (extension == desc.mFileExtensions) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return true;
|
2020-05-02 13:14:38 +00:00
|
|
|
} else if (!extension.length() || checkSig) {
|
|
|
|
if (nullptr == pIOHandler) {
|
2018-03-09 10:40:45 +00:00
|
|
|
return false;
|
2016-09-10 07:27:29 +00:00
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
if (!ZipArchiveIOSystem::isZipArchive(pIOHandler, filename)) {
|
2018-03-13 16:50:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-05-02 13:14:38 +00:00
|
|
|
D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
|
2018-03-09 10:40:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-10 07:27:29 +00:00
|
|
|
} // Namespace Assimp
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_3MF_IMPORTER
|