2016-05-07 12:16:33 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, 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 "D3MFOpcPackage.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2020-08-18 14:54:29 +00:00
|
|
|
#include <assimp/XmlParser.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/ZipArchiveIOSystem.h>
|
|
|
|
#include <assimp/ai_assert.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/IOStream.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2021-08-12 19:13:07 +00:00
|
|
|
#include <assimp/texture.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include "3MFXmlTags.h"
|
2021-09-13 20:38:20 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
2017-08-02 11:15:07 +00:00
|
|
|
#include <cstdlib>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <map>
|
2016-05-07 12:16:33 +00:00
|
|
|
#include <vector>
|
2016-10-10 15:58:06 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
namespace D3MF {
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
2021-08-12 19:13:07 +00:00
|
|
|
using OpcPackageRelationshipPtr = std::shared_ptr<OpcPackageRelationship>;
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-21 17:41:16 +00:00
|
|
|
class OpcPackageRelationshipReader {
|
2016-05-07 12:16:33 +00:00
|
|
|
public:
|
2021-08-12 19:13:07 +00:00
|
|
|
OpcPackageRelationshipReader(XmlParser &parser) :
|
|
|
|
m_relationShips() {
|
2020-09-01 19:48:50 +00:00
|
|
|
XmlNode root = parser.getRootNode();
|
|
|
|
ParseRootNode(root);
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2016-09-10 07:27:29 +00:00
|
|
|
|
2020-08-18 14:54:29 +00:00
|
|
|
void ParseRootNode(XmlNode &node) {
|
|
|
|
ParseAttributes(node);
|
2020-08-28 14:17:56 +00:00
|
|
|
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
|
2020-09-02 15:45:37 +00:00
|
|
|
std::string name = currentNode.name();
|
|
|
|
if (name == "Relationships") {
|
|
|
|
ParseRelationsNode(currentNode);
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:45:37 +00:00
|
|
|
void ParseAttributes(XmlNode & /*node*/) {
|
2017-11-23 21:47:18 +00:00
|
|
|
// empty
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2016-09-10 07:27:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
bool validateRels(OpcPackageRelationshipPtr &relPtr) {
|
|
|
|
if (relPtr->id.empty() || relPtr->type.empty() || relPtr->target.empty()) {
|
2017-12-06 21:15:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-08-12 19:13:07 +00:00
|
|
|
|
2017-12-06 21:15:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:45:37 +00:00
|
|
|
void ParseRelationsNode(XmlNode &node) {
|
2020-08-31 14:10:38 +00:00
|
|
|
if (node.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:45:37 +00:00
|
|
|
for (XmlNode currentNode = node.first_child(); currentNode; currentNode = currentNode.next_sibling()) {
|
2021-08-12 19:13:07 +00:00
|
|
|
const std::string name = currentNode.name();
|
2020-09-02 15:45:37 +00:00
|
|
|
if (name == "Relationship") {
|
|
|
|
OpcPackageRelationshipPtr relPtr(new OpcPackageRelationship());
|
2021-05-04 08:57:30 +00:00
|
|
|
relPtr->id = currentNode.attribute(XmlTag::RELS_ATTRIB_ID).as_string();
|
|
|
|
relPtr->type = currentNode.attribute(XmlTag::RELS_ATTRIB_TYPE).as_string();
|
|
|
|
relPtr->target = currentNode.attribute(XmlTag::RELS_ATTRIB_TARGET).as_string();
|
2020-09-02 15:45:37 +00:00
|
|
|
if (validateRels(relPtr)) {
|
|
|
|
m_relationShips.push_back(relPtr);
|
|
|
|
}
|
|
|
|
}
|
2017-12-06 21:15:34 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
2017-11-21 20:34:25 +00:00
|
|
|
|
2016-05-07 12:16:33 +00:00
|
|
|
std::vector<OpcPackageRelationshipPtr> m_relationShips;
|
|
|
|
};
|
|
|
|
|
2021-08-12 19:13:07 +00:00
|
|
|
static bool IsEmbeddedTexture( const std::string &filename ) {
|
|
|
|
const std::string extension = BaseImporter::GetExtension(filename);
|
|
|
|
if (extension == "jpg" || extension == "png") {
|
2021-08-28 11:33:25 +00:00
|
|
|
std::string::size_type pos = filename.find("thumbnail");
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-08-12 19:13:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-09 10:40:45 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
D3MFOpcPackage::D3MFOpcPackage(IOSystem *pIOHandler, const std::string &rFile) :
|
2020-08-18 14:54:29 +00:00
|
|
|
mRootStream(nullptr),
|
|
|
|
mZipArchive() {
|
2021-08-29 16:35:44 +00:00
|
|
|
mZipArchive = new ZipArchiveIOSystem(pIOHandler, rFile);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!mZipArchive->isOpen()) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Failed to open file ", rFile, ".");
|
2016-09-10 07:27:29 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
std::vector<std::string> fileList;
|
2017-11-21 20:34:25 +00:00
|
|
|
mZipArchive->getFileList(fileList);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (auto &file : fileList) {
|
|
|
|
if (file == D3MF::XmlTag::ROOT_RELATIONSHIPS_ARCHIVE) {
|
|
|
|
if (!mZipArchive->Exists(file.c_str())) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
IOStream *fileStream = mZipArchive->Open(file.c_str());
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == fileStream) {
|
2021-11-02 09:38:38 +00:00
|
|
|
ASSIMP_LOG_ERROR("Filestream is nullptr.");
|
2020-06-23 19:05:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
std::string rootFile = ReadPackageRootRelationship(fileStream);
|
2021-08-12 19:13:07 +00:00
|
|
|
if (!rootFile.empty() && rootFile[0] == '/') {
|
2020-06-23 19:05:42 +00:00
|
|
|
rootFile = rootFile.substr(1);
|
|
|
|
if (rootFile[0] == '/') {
|
2018-03-09 10:40:45 +00:00
|
|
|
// deal with zip-bug
|
2020-06-23 19:05:42 +00:00
|
|
|
rootFile = rootFile.substr(1);
|
2017-12-06 21:15:34 +00:00
|
|
|
}
|
2021-08-12 19:13:07 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-05-18 10:45:00 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(rootFile);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2019-07-25 08:54:39 +00:00
|
|
|
mZipArchive->Close(fileStream);
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
mRootStream = mZipArchive->Open(rootFile.c_str());
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(mRootStream != nullptr);
|
|
|
|
if (nullptr == mRootStream) {
|
2021-04-19 17:33:41 +00:00
|
|
|
throw DeadlyImportError("Cannot open root-file in archive : " + rootFile);
|
2017-12-06 21:15:34 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (file == D3MF::XmlTag::CONTENT_TYPES_ARCHIVE) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_WARN("Ignored file of unsupported type CONTENT_TYPES_ARCHIVES", file);
|
2021-08-12 19:13:07 +00:00
|
|
|
} else if (IsEmbeddedTexture(file)) {
|
|
|
|
IOStream *fileStream = mZipArchive->Open(file.c_str());
|
|
|
|
LoadEmbeddedTextures(fileStream, file);
|
|
|
|
mZipArchive->Close(fileStream);
|
2019-03-30 18:12:13 +00:00
|
|
|
} else {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_WARN("Ignored file of unknown type: ", file);
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
D3MFOpcPackage::~D3MFOpcPackage() {
|
2019-07-25 08:54:39 +00:00
|
|
|
mZipArchive->Close(mRootStream);
|
2021-08-29 16:35:44 +00:00
|
|
|
delete mZipArchive;
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
IOStream *D3MFOpcPackage::RootStream() const {
|
2017-11-21 20:34:25 +00:00
|
|
|
return mRootStream;
|
2016-05-07 12:16:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:13:07 +00:00
|
|
|
const std::vector<aiTexture *> &D3MFOpcPackage::GetEmbeddedTextures() const {
|
|
|
|
return mEmbeddedTextures;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *const ModelRef = "3D/3dmodel.model";
|
2018-03-09 10:40:45 +00:00
|
|
|
|
|
|
|
bool D3MFOpcPackage::validate() {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (nullptr == mRootStream || nullptr == mZipArchive) {
|
2018-03-09 10:40:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:13:07 +00:00
|
|
|
return mZipArchive->Exists(ModelRef);
|
2018-03-09 10:40:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::string D3MFOpcPackage::ReadPackageRootRelationship(IOStream *stream) {
|
2020-08-18 14:54:29 +00:00
|
|
|
XmlParser xmlParser;
|
|
|
|
if (!xmlParser.parse(stream)) {
|
2021-04-16 21:43:56 +00:00
|
|
|
return std::string();
|
2020-08-18 14:54:29 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-08-18 14:54:29 +00:00
|
|
|
OpcPackageRelationshipReader reader(xmlParser);
|
2016-05-07 12:16:33 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
auto itr = std::find_if(reader.m_relationShips.begin(), reader.m_relationShips.end(), [](const OpcPackageRelationshipPtr &rel) {
|
2016-05-07 12:16:33 +00:00
|
|
|
return rel->type == XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE;
|
|
|
|
});
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (itr == reader.m_relationShips.end()) {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("Cannot find ", XmlTag::PACKAGE_START_PART_RELATIONSHIP_TYPE);
|
2018-03-09 10:40:45 +00:00
|
|
|
}
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
return (*itr)->target;
|
|
|
|
}
|
|
|
|
|
2021-08-12 19:13:07 +00:00
|
|
|
void D3MFOpcPackage::LoadEmbeddedTextures(IOStream *fileStream, const std::string &filename) {
|
|
|
|
if (nullptr == fileStream) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t size = fileStream->FileSize();
|
|
|
|
if (0 == size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-28 11:33:25 +00:00
|
|
|
unsigned char *data = new unsigned char[size];
|
2021-08-12 19:13:07 +00:00
|
|
|
fileStream->Read(data, 1, size);
|
|
|
|
aiTexture *texture = new aiTexture;
|
2021-08-28 11:33:25 +00:00
|
|
|
std::string embName = "*" + filename;
|
|
|
|
texture->mFilename.Set(embName.c_str());
|
2021-08-12 19:13:07 +00:00
|
|
|
texture->mWidth = static_cast<unsigned int>(size);
|
|
|
|
texture->mHeight = 0;
|
2021-08-28 11:33:25 +00:00
|
|
|
texture->achFormatHint[0] = 'p';
|
|
|
|
texture->achFormatHint[1] = 'n';
|
|
|
|
texture->achFormatHint[2] = 'g';
|
|
|
|
texture->achFormatHint[3] = '\0';
|
2021-08-12 19:13:07 +00:00
|
|
|
texture->pcData = (aiTexel*) data;
|
|
|
|
mEmbeddedTextures.emplace_back(texture);
|
|
|
|
}
|
|
|
|
|
2017-11-21 20:34:25 +00:00
|
|
|
} // Namespace D3MF
|
|
|
|
} // Namespace Assimp
|
2016-05-07 12:16:33 +00:00
|
|
|
|
|
|
|
#endif //ASSIMP_BUILD_NO_3MF_IMPORTER
|