2016-05-07 12:16:33 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2019-01-30 08:41:39 +00:00
|
|
|
Copyright (c) 2006-2019, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +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"
|
|
|
|
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringComparison.h>
|
|
|
|
#include <assimp/StringUtils.h>
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
|
2016-10-10 15:58:06 +00:00
|
|
|
#include "D3MFOpcPackage.h"
|
2019-05-24 20:37:09 +00:00
|
|
|
#ifdef ASSIMP_USE_HUNTER
|
|
|
|
# include <minizip/unzip.h>
|
|
|
|
#else
|
|
|
|
# include <unzip.h>
|
|
|
|
#endif
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/irrXMLWrapper.h>
|
2017-11-20 21:36:17 +00:00
|
|
|
#include "3MFXmlTags.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 {
|
|
|
|
|
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
|
|
|
using MatArray = std::vector<aiMaterial*>;
|
|
|
|
using MatId2MatArray = std::map<unsigned int, std::vector<unsigned int>>;
|
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
XmlSerializer(XmlReader* xmlReader)
|
2018-02-23 19:34:34 +00:00
|
|
|
: mMeshes()
|
2018-03-09 10:40:45 +00:00
|
|
|
, mMatArray()
|
|
|
|
, mActiveMatGroup( 99999999 )
|
|
|
|
, mMatId2MatArray()
|
2018-02-23 19:34:34 +00:00
|
|
|
, xmlReader(xmlReader){
|
2017-08-31 09:04:13 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-08-31 09:04:13 +00:00
|
|
|
void ImportXml(aiScene* scene) {
|
2018-02-18 20:30:23 +00:00
|
|
|
if ( nullptr == scene ) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
scene->mRootNode = new aiNode();
|
|
|
|
std::vector<aiNode*> children;
|
|
|
|
|
2018-03-20 17:43:54 +00:00
|
|
|
std::string nodeName;
|
2017-11-21 20:34:25 +00:00
|
|
|
while(ReadToEndElement(D3MF::XmlTag::model)) {
|
2018-03-20 17:43:54 +00:00
|
|
|
nodeName = xmlReader->getNodeName();
|
2018-02-18 20:30:23 +00:00
|
|
|
if( nodeName == D3MF::XmlTag::object) {
|
2016-05-07 12:16:33 +00:00
|
|
|
children.push_back(ReadObject(scene));
|
2018-02-18 20:30:23 +00:00
|
|
|
} else if( nodeName == D3MF::XmlTag::build) {
|
2018-02-12 21:07:12 +00:00
|
|
|
//
|
2018-02-18 20:30:23 +00:00
|
|
|
} else if ( nodeName == D3MF::XmlTag::basematerials ) {
|
2018-02-12 21:07:12 +00:00
|
|
|
ReadBaseMaterials();
|
2018-03-20 09:09:47 +00:00
|
|
|
} else if ( nodeName == D3MF::XmlTag::meta ) {
|
|
|
|
ReadMetadata();
|
2017-05-06 09:14:04 +00:00
|
|
|
}
|
2017-03-27 22:35:56 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-20 21:36:17 +00:00
|
|
|
if ( scene->mRootNode->mName.length == 0 ) {
|
|
|
|
scene->mRootNode->mName.Set( "3MF" );
|
|
|
|
}
|
2017-05-06 09:14:04 +00:00
|
|
|
|
2018-03-20 13:10:08 +00:00
|
|
|
// import the metadata
|
2018-03-20 09:09:47 +00:00
|
|
|
if ( !mMetaData.empty() ) {
|
|
|
|
const size_t numMeta( mMetaData.size() );
|
2018-05-03 18:59:18 +00:00
|
|
|
scene->mMetaData = aiMetadata::Alloc(static_cast<unsigned int>( numMeta ) );
|
2018-03-20 09:09:47 +00:00
|
|
|
for ( size_t i = 0; i < numMeta; ++i ) {
|
|
|
|
aiString val( mMetaData[ i ].value );
|
2018-05-03 18:59:18 +00:00
|
|
|
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
|
2018-02-12 21:07:12 +00:00
|
|
|
scene->mNumMeshes = static_cast<unsigned int>( mMeshes.size());
|
2016-05-07 12:16:33 +00:00
|
|
|
scene->mMeshes = new aiMesh*[scene->mNumMeshes]();
|
2018-02-12 21:07:12 +00:00
|
|
|
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
|
2018-03-13 17:19:10 +00:00
|
|
|
scene->mNumMaterials = static_cast<unsigned int>( mMatArray.size() );
|
2018-02-18 20:30:23 +00:00
|
|
|
if ( 0 != scene->mNumMaterials ) {
|
|
|
|
scene->mMaterials = new aiMaterial*[ scene->mNumMaterials ];
|
2018-03-09 10:40:45 +00:00
|
|
|
std::copy( mMatArray.begin(), mMatArray.end(), scene->mMaterials );
|
2018-02-18 20:30:23 +00:00
|
|
|
}
|
2018-03-20 13:10:08 +00:00
|
|
|
|
|
|
|
// create the scenegraph
|
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]();
|
|
|
|
std::copy(children.begin(), children.end(), scene->mRootNode->mChildren);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-02-12 21:07:12 +00:00
|
|
|
aiNode* ReadObject(aiScene* scene) {
|
2017-12-02 15:15:57 +00:00
|
|
|
std::unique_ptr<aiNode> node(new aiNode());
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
std::vector<unsigned long> meshIds;
|
|
|
|
|
2016-10-18 19:16:44 +00:00
|
|
|
const char *attrib( nullptr );
|
|
|
|
std::string name, type;
|
2018-03-12 21:28:00 +00:00
|
|
|
attrib = xmlReader->getAttributeValue( D3MF::XmlTag::id.c_str() );
|
2016-10-18 19:16:44 +00:00
|
|
|
if ( nullptr != attrib ) {
|
|
|
|
name = attrib;
|
|
|
|
}
|
2018-03-12 21:28:00 +00:00
|
|
|
attrib = xmlReader->getAttributeValue( D3MF::XmlTag::type.c_str() );
|
2016-10-18 19:16:44 +00:00
|
|
|
if ( nullptr != attrib ) {
|
|
|
|
type = attrib;
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
node->mParent = scene->mRootNode;
|
2016-10-16 15:14:22 +00:00
|
|
|
node->mName.Set(name);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
size_t meshIdx = mMeshes.size();
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
while(ReadToEndElement(D3MF::XmlTag::object)) {
|
|
|
|
if(xmlReader->getNodeName() == D3MF::XmlTag::mesh) {
|
2016-05-07 12:16:33 +00:00
|
|
|
auto mesh = ReadMesh();
|
|
|
|
|
|
|
|
mesh->mName.Set(name);
|
2018-02-12 21:07:12 +00:00
|
|
|
mMeshes.push_back(mesh);
|
2016-11-18 16:34:39 +00:00
|
|
|
meshIds.push_back(static_cast<unsigned long>(meshIdx));
|
2018-02-12 21:07:12 +00:00
|
|
|
++meshIdx;
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node->mNumMeshes = static_cast<unsigned int>(meshIds.size());
|
|
|
|
|
|
|
|
node->mMeshes = new unsigned int[node->mNumMeshes];
|
|
|
|
|
|
|
|
std::copy(meshIds.begin(), meshIds.end(), node->mMeshes);
|
|
|
|
|
2017-12-02 15:15:57 +00:00
|
|
|
return node.release();
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
aiMesh *ReadMesh() {
|
2016-05-07 12:16:33 +00:00
|
|
|
aiMesh* mesh = new aiMesh();
|
2018-02-12 21:07:12 +00:00
|
|
|
while(ReadToEndElement(D3MF::XmlTag::mesh)) {
|
|
|
|
if(xmlReader->getNodeName() == D3MF::XmlTag::vertices) {
|
2016-05-07 12:16:33 +00:00
|
|
|
ImportVertices(mesh);
|
2018-02-12 21:07:12 +00:00
|
|
|
} else if(xmlReader->getNodeName() == D3MF::XmlTag::triangles) {
|
2016-05-07 12:16:33 +00:00
|
|
|
ImportTriangles(mesh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mesh;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:09:47 +00:00
|
|
|
void ReadMetadata() {
|
|
|
|
const std::string name = xmlReader->getAttributeValue( D3MF::XmlTag::meta_name.c_str() );
|
|
|
|
xmlReader->read();
|
|
|
|
const std::string value = xmlReader->getNodeData();
|
|
|
|
|
|
|
|
if ( name.empty() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaEntry entry;
|
|
|
|
entry.name = name;
|
|
|
|
entry.value = value;
|
|
|
|
mMetaData.push_back( entry );
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
void ImportVertices(aiMesh* mesh) {
|
2016-10-16 15:14:22 +00:00
|
|
|
std::vector<aiVector3D> vertices;
|
2018-02-23 16:49:29 +00:00
|
|
|
while(ReadToEndElement(D3MF::XmlTag::vertices)) {
|
|
|
|
if(xmlReader->getNodeName() == D3MF::XmlTag::vertex) {
|
2016-05-07 12:16:33 +00:00
|
|
|
vertices.push_back(ReadVertex());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mesh->mNumVertices = static_cast<unsigned int>(vertices.size());
|
|
|
|
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
2017-05-06 09:14:04 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
std::copy(vertices.begin(), vertices.end(), mesh->mVertices);
|
2017-05-06 09:14:04 +00:00
|
|
|
}
|
2017-11-20 21:36:17 +00:00
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
aiVector3D ReadVertex() {
|
2016-05-07 12:16:33 +00:00
|
|
|
aiVector3D vertex;
|
2017-01-17 21:05:45 +00:00
|
|
|
|
2016-06-20 18:30:29 +00:00
|
|
|
vertex.x = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::x.c_str()), nullptr);
|
|
|
|
vertex.y = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::y.c_str()), nullptr);
|
2017-01-17 21:05:45 +00:00
|
|
|
vertex.z = ai_strtof(xmlReader->getAttributeValue(D3MF::XmlTag::z.c_str()), nullptr);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
void ImportTriangles(aiMesh* mesh) {
|
2016-10-16 15:14:22 +00:00
|
|
|
std::vector<aiFace> faces;
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
while(ReadToEndElement(D3MF::XmlTag::triangles)) {
|
2018-02-18 20:30:23 +00:00
|
|
|
const std::string nodeName( xmlReader->getNodeName() );
|
2018-02-12 21:07:12 +00:00
|
|
|
if(xmlReader->getNodeName() == D3MF::XmlTag::triangle) {
|
2016-05-07 12:16:33 +00:00
|
|
|
faces.push_back(ReadTriangle());
|
2018-02-23 16:49:29 +00:00
|
|
|
const char *pidToken( xmlReader->getAttributeValue( D3MF::XmlTag::p1.c_str() ) );
|
2018-02-20 21:08:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
aiFace ReadTriangle() {
|
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];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
void ReadBaseMaterials() {
|
2018-03-09 10:40:45 +00:00
|
|
|
std::vector<unsigned int> MatIdArray;
|
|
|
|
const char *baseMaterialId( xmlReader->getAttributeValue( D3MF::XmlTag::basematerials_id.c_str() ) );
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 19:38:03 +00:00
|
|
|
MatIdArray.push_back( static_cast<unsigned int>( newMatIdx ) );
|
2018-03-09 10:40:45 +00:00
|
|
|
mMatId2MatArray[ mActiveMatGroup ] = MatIdArray;
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
while ( ReadToEndElement( D3MF::XmlTag::basematerials ) ) {
|
2018-03-09 10:40:45 +00:00
|
|
|
mMatArray.push_back( readMaterialDef() );
|
2018-02-12 21:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2018-02-12 21:07:12 +00:00
|
|
|
const size_t len( strlen( color ) );
|
2018-09-05 13:49:15 +00:00
|
|
|
if ( 9 != len && 7 != len) {
|
2018-02-12 21:07:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *buf( color );
|
|
|
|
if ( '#' != *buf ) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-23 16:49:29 +00:00
|
|
|
++buf;
|
|
|
|
char comp[ 3 ] = { 0,0,'\0' };
|
2018-02-12 21:07:12 +00:00
|
|
|
|
|
|
|
comp[ 0 ] = *buf;
|
|
|
|
++buf;
|
|
|
|
comp[ 1 ] = *buf;
|
|
|
|
++buf;
|
2018-10-06 14:28:10 +00:00
|
|
|
diffuse.r = static_cast<ai_real>( strtol( comp, NULL, 16 ) ) / ai_real(255.0);
|
2018-02-23 16:49:29 +00:00
|
|
|
|
2018-02-12 21:07:12 +00:00
|
|
|
|
|
|
|
comp[ 0 ] = *buf;
|
|
|
|
++buf;
|
|
|
|
comp[ 1 ] = *buf;
|
|
|
|
++buf;
|
2018-10-06 14:28:10 +00:00
|
|
|
diffuse.g = static_cast< ai_real >( strtol( comp, NULL, 16 ) ) / ai_real(255.0);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
|
|
|
comp[ 0 ] = *buf;
|
|
|
|
++buf;
|
|
|
|
comp[ 1 ] = *buf;
|
|
|
|
++buf;
|
2018-10-06 14:28:10 +00:00
|
|
|
diffuse.b = static_cast< ai_real >( strtol( comp, NULL, 16 ) ) / ai_real(255.0);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
2018-09-05 13:49:15 +00:00
|
|
|
if(7 == len)
|
|
|
|
return true;
|
2018-02-12 21:07:12 +00:00
|
|
|
comp[ 0 ] = *buf;
|
|
|
|
++buf;
|
|
|
|
comp[ 1 ] = *buf;
|
|
|
|
++buf;
|
2018-10-06 14:28:10 +00:00
|
|
|
diffuse.a = static_cast< ai_real >( strtol( comp, NULL, 16 ) ) / ai_real(255.0);
|
2018-02-12 21:07:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
void assignDiffuseColor( aiMaterial *mat ) {
|
|
|
|
const char *color = xmlReader->getAttributeValue( D3MF::XmlTag::basematerials_displaycolor.c_str() );
|
|
|
|
aiColor4D diffuse;
|
|
|
|
if ( parseColor( color, diffuse ) ) {
|
|
|
|
mat->AddProperty<aiColor4D>( &diffuse, 1, AI_MATKEY_COLOR_DIFFUSE );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-02-12 21:07:12 +00:00
|
|
|
aiMaterial *readMaterialDef() {
|
2018-02-18 20:30:23 +00:00
|
|
|
aiMaterial *mat( nullptr );
|
2018-02-23 16:49:29 +00:00
|
|
|
const char *name( nullptr );
|
|
|
|
const std::string nodeName( xmlReader->getNodeName() );
|
|
|
|
if ( nodeName == D3MF::XmlTag::basematerials_base ) {
|
|
|
|
name = xmlReader->getAttributeValue( D3MF::XmlTag::basematerials_name.c_str() );
|
2018-03-09 10:40:45 +00:00
|
|
|
std::string stdMatName;
|
2018-02-23 16:49:29 +00:00
|
|
|
aiString matName;
|
2018-03-09 10:40:45 +00:00
|
|
|
std::string strId( to_string( mActiveMatGroup ) );
|
|
|
|
stdMatName += "id";
|
|
|
|
stdMatName += strId;
|
|
|
|
stdMatName += "_";
|
|
|
|
if ( nullptr != name ) {
|
|
|
|
stdMatName += std::string( name );
|
|
|
|
} else {
|
|
|
|
stdMatName += "basemat";
|
|
|
|
}
|
|
|
|
matName.Set( stdMatName );
|
|
|
|
|
2018-02-23 16:49:29 +00:00
|
|
|
mat = new aiMaterial;
|
|
|
|
mat->AddProperty( &matName, AI_MATKEY_NAME );
|
|
|
|
|
2018-03-09 10:40:45 +00:00
|
|
|
assignDiffuseColor( mat );
|
2018-02-23 16:49:29 +00:00
|
|
|
}
|
2018-02-12 21:07:12 +00:00
|
|
|
|
2018-02-18 20:30:23 +00:00
|
|
|
return mat;
|
2018-02-12 21:07:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
private:
|
2018-02-23 16:49:29 +00:00
|
|
|
bool ReadToStartElement(const std::string& startTag) {
|
|
|
|
while(xmlReader->read()) {
|
|
|
|
const std::string &nodeName( xmlReader->getNodeName() );
|
|
|
|
if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT && nodeName == startTag) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return true;
|
2018-02-23 16:49:29 +00:00
|
|
|
} else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END && nodeName == startTag) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 16:49:29 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:08:40 +00:00
|
|
|
bool ReadToEndElement(const std::string& closeTag) {
|
2018-02-23 16:49:29 +00:00
|
|
|
while(xmlReader->read()) {
|
|
|
|
const std::string &nodeName( xmlReader->getNodeName() );
|
2016-05-07 12:16:33 +00:00
|
|
|
if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT) {
|
|
|
|
return true;
|
2018-02-23 16:49:29 +00:00
|
|
|
} else if (xmlReader->getNodeType() == irr::io::EXN_ELEMENT_END && nodeName == closeTag) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 14:48:43 +00:00
|
|
|
ASSIMP_LOG_ERROR("unexpected EOF, expected closing <" + closeTag + "> tag");
|
2018-02-23 16:49:29 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-03-20 09:09:47 +00:00
|
|
|
struct MetaEntry {
|
|
|
|
std::string name;
|
|
|
|
std::string value;
|
|
|
|
};
|
|
|
|
std::vector<MetaEntry> mMetaData;
|
2018-02-12 21:07:12 +00:00
|
|
|
std::vector<aiMesh*> mMeshes;
|
2018-03-09 10:40:45 +00:00
|
|
|
MatArray mMatArray;
|
|
|
|
unsigned int mActiveMatGroup;
|
|
|
|
MatId2MatArray mMatId2MatArray;
|
2016-05-07 12:16:33 +00:00
|
|
|
XmlReader* xmlReader;
|
|
|
|
};
|
|
|
|
|
|
|
|
} //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
|
|
|
};
|
|
|
|
|
2017-05-06 09:14:04 +00:00
|
|
|
D3MFImporter::D3MFImporter()
|
2017-11-20 21:36:17 +00:00
|
|
|
: BaseImporter() {
|
|
|
|
// 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 {
|
|
|
|
const std::string extension( GetExtension( filename ) );
|
2018-11-23 19:21:58 +00:00
|
|
|
if(extension == desc.mFileExtensions ) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return true;
|
2016-09-10 07:27:29 +00:00
|
|
|
} else if ( !extension.length() || checkSig ) {
|
2018-03-09 10:40:45 +00:00
|
|
|
if ( nullptr == pIOHandler ) {
|
|
|
|
return false;
|
2016-09-10 07:27:29 +00:00
|
|
|
}
|
2018-03-13 16:50:34 +00:00
|
|
|
if ( !D3MF::D3MFOpcPackage::isZipArchive( pIOHandler, filename ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-09 10:40:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-11 19:15:49 +00:00
|
|
|
void D3MFImporter::InternReadFile( const std::string &filename, aiScene *pScene, IOSystem *pIOHandler ) {
|
|
|
|
D3MF::D3MFOpcPackage opcPackage(pIOHandler, filename);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-10 07:27:29 +00:00
|
|
|
} // Namespace Assimp
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_3MF_IMPORTER
|