assimp/code/Common/Importer.cpp

1294 lines
46 KiB
C++
Raw Normal View History

/*
---------------------------------------------------------------------------
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
All rights reserved.
2015-05-19 03:52:10 +00:00
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.
2015-05-19 03:52:10 +00:00
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
2015-05-19 03:52:10 +00:00
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2015-05-19 03:52:10 +00:00
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2015-05-19 03:52:10 +00:00
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.
---------------------------------------------------------------------------
*/
/** @file Importer.cpp
* @brief Implementation of the CPP-API class #Importer
*/
2016-06-06 20:04:29 +00:00
#include <assimp/version.h>
#include <assimp/config.h>
2017-02-22 16:20:26 +00:00
#include <assimp/importerdesc.h>
// ------------------------------------------------------------------------------------------------
/* Uncomment this line to prevent Assimp from catching unknown exceptions.
*
2015-05-19 03:52:10 +00:00
* Note that any Exception except DeadlyImportError may lead to
* undefined behaviour -> loaders could remain in an unusable state and
* further imports with the same Importer instance could fail/crash/burn ...
*/
// ------------------------------------------------------------------------------------------------
#ifndef ASSIMP_BUILD_DEBUG
2015-05-19 03:57:13 +00:00
# define ASSIMP_CATCH_GLOBAL_EXCEPTIONS
#endif
// ------------------------------------------------------------------------------------------------
// Internal headers
// ------------------------------------------------------------------------------------------------
#include "Common/Importer.h"
#include "Common/BaseProcess.h"
#include "Common/DefaultProgressHandler.h"
#include "PostProcessing/ProcessHelper.h"
#include "Common/ScenePreprocessor.h"
#include "Common/ScenePrivate.h"
#include <assimp/BaseImporter.h>
#include <assimp/GenericProperty.h>
#include <assimp/MemoryIOWrapper.h>
#include <assimp/Profiler.h>
#include <assimp/TinyFormatter.h>
#include <assimp/Exceptional.h>
#include <assimp/Profiler.h>
2019-12-05 11:25:12 +00:00
#include <assimp/commonMetaData.h>
2021-01-24 09:04:46 +00:00
#include <exception>
#include <set>
#include <memory>
#include <cctype>
#include <assimp/DefaultIOStream.h>
#include <assimp/DefaultIOSystem.h>
#ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
# include "PostProcessing/ValidateDataStructure.h"
#endif
using namespace Assimp::Profiling;
using namespace Assimp::Formatter;
namespace Assimp {
2015-05-19 03:57:13 +00:00
// ImporterRegistry.cpp
void GetImporterInstanceList(std::vector< BaseImporter* >& out);
2016-01-16 14:02:45 +00:00
void DeleteImporterInstanceList(std::vector< BaseImporter* >& out);
2015-05-19 03:57:13 +00:00
// PostStepRegistry.cpp
void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out);
}
using namespace Assimp;
using namespace Assimp::Intern;
// ------------------------------------------------------------------------------------------------
// Intern::AllocateFromAssimpHeap serves as abstract base class. It overrides
// new and delete (and their array counterparts) of public API classes (e.g. Logger) to
// utilize our DLL heap.
// See http://www.gotw.ca/publications/mill15.htm
// ------------------------------------------------------------------------------------------------
2015-05-19 03:57:13 +00:00
void* AllocateFromAssimpHeap::operator new ( size_t num_bytes) {
return ::operator new(num_bytes);
}
2015-05-19 03:57:13 +00:00
void* AllocateFromAssimpHeap::operator new ( size_t num_bytes, const std::nothrow_t& ) throw() {
try {
return AllocateFromAssimpHeap::operator new( num_bytes );
}
catch( ... ) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
}
2015-05-19 03:57:13 +00:00
void AllocateFromAssimpHeap::operator delete ( void* data) {
return ::operator delete(data);
}
2015-05-19 03:57:13 +00:00
void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes) {
return ::operator new[](num_bytes);
}
void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw() {
2015-05-19 03:57:13 +00:00
try {
return AllocateFromAssimpHeap::operator new[]( num_bytes );
} catch( ... ) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
}
2015-05-19 03:57:13 +00:00
void AllocateFromAssimpHeap::operator delete[] ( void* data) {
return ::operator delete[](data);
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Importer constructor.
Importer::Importer()
2018-04-08 19:27:18 +00:00
: pimpl( new ImporterPimpl ) {
pimpl->mScene = nullptr;
pimpl->mErrorString = std::string();
2015-05-19 03:57:13 +00:00
// Allocate a default IO handler
pimpl->mIOHandler = new DefaultIOSystem;
pimpl->mIsDefaultHandler = true;
pimpl->bExtraVerbose = false; // disable extra verbose mode by default
2015-05-19 03:57:13 +00:00
pimpl->mProgressHandler = new DefaultProgressHandler();
pimpl->mIsDefaultProgressHandler = true;
2015-05-19 03:57:13 +00:00
GetImporterInstanceList(pimpl->mImporter);
GetPostProcessingStepInstanceList(pimpl->mPostProcessingSteps);
2015-05-19 03:57:13 +00:00
// Allocate a SharedPostProcessInfo object and store pointers to it in all post-process steps in the list.
pimpl->mPPShared = new SharedPostProcessInfo();
for (std::vector<BaseProcess*>::iterator it = pimpl->mPostProcessingSteps.begin();
it != pimpl->mPostProcessingSteps.end();
++it) {
2015-05-19 03:57:13 +00:00
(*it)->SetSharedData(pimpl->mPPShared);
}
}
// ------------------------------------------------------------------------------------------------
// Destructor of Importer
Importer::~Importer() {
2015-05-19 03:57:13 +00:00
// Delete all import plugins
2016-01-16 14:02:45 +00:00
DeleteImporterInstanceList(pimpl->mImporter);
2015-05-19 03:57:13 +00:00
// Delete all post-processing plug-ins
for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); ++a ) {
2015-05-19 03:57:13 +00:00
delete pimpl->mPostProcessingSteps[a];
}
2015-05-19 03:57:13 +00:00
// Delete the assigned IO and progress handler
delete pimpl->mIOHandler;
delete pimpl->mProgressHandler;
// Kill imported scene. Destructor's should do that recursively
2015-05-19 03:57:13 +00:00
delete pimpl->mScene;
2015-05-19 03:57:13 +00:00
// Delete shared post-processing data
delete pimpl->mPPShared;
2015-05-19 03:57:13 +00:00
// and finally the pimpl itself
delete pimpl;
}
// ------------------------------------------------------------------------------------------------
// Register a custom post-processing step
aiReturn Importer::RegisterPPStep(BaseProcess* pImp) {
ai_assert( nullptr != pImp );
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2015-05-19 03:57:13 +00:00
pimpl->mPostProcessingSteps.push_back(pImp);
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO("Registering custom post-processing step");
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(aiReturn);
return AI_SUCCESS;
}
// ------------------------------------------------------------------------------------------------
// Register a custom loader plugin
aiReturn Importer::RegisterLoader(BaseImporter* pImp) {
ai_assert(nullptr != pImp);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2015-05-19 03:57:13 +00:00
// --------------------------------------------------------------------
// Check whether we would have two loaders for the same file extension
// This is absolutely OK, but we should warn the developer of the new
// loader that his code will probably never be called if the first
// loader is a bit too lazy in his file checking.
// --------------------------------------------------------------------
std::set<std::string> st;
std::string baked;
pImp->GetExtensionList(st);
2015-05-19 03:57:13 +00:00
for(std::set<std::string>::const_iterator it = st.begin(); it != st.end(); ++it) {
#ifdef ASSIMP_BUILD_DEBUG
2015-05-19 03:57:13 +00:00
if (IsExtensionSupported(*it)) {
ASSIMP_LOG_WARN("The file extension ", *it, " is already in use");
2015-05-19 03:57:13 +00:00
}
#endif
2015-05-19 03:57:13 +00:00
baked += *it;
}
// add the loader
pimpl->mImporter.push_back(pImp);
ASSIMP_LOG_INFO("Registering custom importer for these file extensions: ", baked);
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(aiReturn);
2015-05-19 03:57:13 +00:00
return AI_SUCCESS;
}
// ------------------------------------------------------------------------------------------------
// Unregister a custom loader plugin
aiReturn Importer::UnregisterLoader(BaseImporter* pImp) {
2015-05-19 03:57:13 +00:00
if(!pImp) {
// unregistering a nullptr importer is no problem for us ... really!
2015-05-19 03:57:13 +00:00
return AI_SUCCESS;
}
ASSIMP_BEGIN_EXCEPTION_REGION();
std::vector<BaseImporter*>::iterator it = std::find(pimpl->mImporter.begin(),
pimpl->mImporter.end(),pImp);
if (it != pimpl->mImporter.end()) {
pimpl->mImporter.erase(it);
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO("Unregistering custom importer: ");
2015-05-19 03:57:13 +00:00
return AI_SUCCESS;
}
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("Unable to remove custom importer: I can't find you ...");
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(aiReturn);
2015-05-19 03:57:13 +00:00
return AI_FAILURE;
}
// ------------------------------------------------------------------------------------------------
// Unregister a custom loader plugin
aiReturn Importer::UnregisterPPStep(BaseProcess* pImp) {
2015-05-19 03:57:13 +00:00
if(!pImp) {
// unregistering a nullptr ppstep is no problem for us ... really!
2015-05-19 03:57:13 +00:00
return AI_SUCCESS;
}
ASSIMP_BEGIN_EXCEPTION_REGION();
std::vector<BaseProcess*>::iterator it = std::find(pimpl->mPostProcessingSteps.begin(),
pimpl->mPostProcessingSteps.end(),pImp);
if (it != pimpl->mPostProcessingSteps.end()) {
pimpl->mPostProcessingSteps.erase(it);
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO("Unregistering custom post-processing step");
2015-05-19 03:57:13 +00:00
return AI_SUCCESS;
}
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("Unable to remove custom post-processing step: I can't find you ..");
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(aiReturn);
2015-05-19 03:57:13 +00:00
return AI_FAILURE;
}
// ------------------------------------------------------------------------------------------------
// Supplies a custom IO handler to the importer to open and access files.
void Importer::SetIOHandler( IOSystem* pIOHandler) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
// If the new handler is zero, allocate a default IO implementation.
if (!pIOHandler) {
2015-05-19 03:57:13 +00:00
// Release pointer in the possession of the caller
pimpl->mIOHandler = new DefaultIOSystem();
pimpl->mIsDefaultHandler = true;
} else if (pimpl->mIOHandler != pIOHandler) { // Otherwise register the custom handler
2015-05-19 03:57:13 +00:00
delete pimpl->mIOHandler;
pimpl->mIOHandler = pIOHandler;
pimpl->mIsDefaultHandler = false;
}
ASSIMP_END_EXCEPTION_REGION(void);
}
// ------------------------------------------------------------------------------------------------
// Get the currently set IO handler
IOSystem* Importer::GetIOHandler() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mIOHandler;
}
// ------------------------------------------------------------------------------------------------
// Check whether a custom IO handler is currently set
bool Importer::IsDefaultIOHandler() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mIsDefaultHandler;
}
// ------------------------------------------------------------------------------------------------
// Supplies a custom progress handler to get regular callbacks during importing
2021-12-26 10:16:02 +00:00
void Importer::SetProgressHandler(ProgressHandler* pHandler) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2015-05-19 03:57:13 +00:00
// If the new handler is zero, allocate a default implementation.
if (!pHandler) {
2015-05-19 03:57:13 +00:00
// Release pointer in the possession of the caller
pimpl->mProgressHandler = new DefaultProgressHandler();
pimpl->mIsDefaultProgressHandler = true;
} else if (pimpl->mProgressHandler != pHandler) { // Otherwise register the custom handler
2015-05-19 03:57:13 +00:00
delete pimpl->mProgressHandler;
pimpl->mProgressHandler = pHandler;
pimpl->mIsDefaultProgressHandler = false;
}
ASSIMP_END_EXCEPTION_REGION(void);
}
// ------------------------------------------------------------------------------------------------
// Get the currently set progress handler
ProgressHandler* Importer::GetProgressHandler() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mProgressHandler;
}
// ------------------------------------------------------------------------------------------------
// Check whether a custom progress handler is currently set
bool Importer::IsDefaultProgressHandler() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mIsDefaultProgressHandler;
}
// ------------------------------------------------------------------------------------------------
2015-05-19 03:52:10 +00:00
// Validate post process step flags
bool _ValidateFlags(unsigned int pFlags) {
2015-05-19 03:57:13 +00:00
if (pFlags & aiProcess_GenSmoothNormals && pFlags & aiProcess_GenNormals) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("#aiProcess_GenSmoothNormals and #aiProcess_GenNormals are incompatible");
2015-05-19 03:57:13 +00:00
return false;
}
if (pFlags & aiProcess_OptimizeGraph && pFlags & aiProcess_PreTransformVertices) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("#aiProcess_OptimizeGraph and #aiProcess_PreTransformVertices are incompatible");
2015-05-19 03:57:13 +00:00
return false;
}
return true;
}
// ------------------------------------------------------------------------------------------------
// Free the current scene
void Importer::FreeScene( ) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2015-05-19 03:57:13 +00:00
delete pimpl->mScene;
pimpl->mScene = nullptr;
pimpl->mErrorString = std::string();
2020-08-18 17:14:51 +00:00
pimpl->mException = std::exception_ptr();
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(void);
}
// ------------------------------------------------------------------------------------------------
// Get the current error string, if any
const char* Importer::GetErrorString() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
// Must remain valid as long as ReadFile() or FreeFile() are not called
2015-05-19 03:57:13 +00:00
return pimpl->mErrorString.c_str();
}
2020-08-18 17:14:51 +00:00
const std::exception_ptr& Importer::GetException() const {
ai_assert(nullptr != pimpl);
// Must remain valid as long as ReadFile() or FreeFile() are not called
2020-08-18 17:14:51 +00:00
return pimpl->mException;
}
// ------------------------------------------------------------------------------------------------
// Enable extra-verbose mode
void Importer::SetExtraVerbose(bool bDo) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
pimpl->bExtraVerbose = bDo;
}
// ------------------------------------------------------------------------------------------------
// Get the current scene
const aiScene* Importer::GetScene() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mScene;
}
// ------------------------------------------------------------------------------------------------
// Orphan the current scene and return it.
aiScene* Importer::GetOrphanedScene() {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
aiScene* s = pimpl->mScene;
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
pimpl->mScene = nullptr;
pimpl->mErrorString = std::string();
2020-08-18 17:14:51 +00:00
pimpl->mException = std::exception_ptr();
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(aiScene*);
2015-05-19 03:57:13 +00:00
return s;
}
// ------------------------------------------------------------------------------------------------
// Validate post-processing flags
bool Importer::ValidateFlags(unsigned int pFlags) const {
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
// run basic checks for mutually exclusive flags
if(!_ValidateFlags(pFlags)) {
return false;
}
2015-05-19 03:57:13 +00:00
// ValidateDS does not anymore occur in the pp list, it plays an awesome extra role ...
#ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2015-05-19 03:57:13 +00:00
if (pFlags & aiProcess_ValidateDataStructure) {
return false;
}
#endif
2015-05-19 03:57:13 +00:00
pFlags &= ~aiProcess_ValidateDataStructure;
// Now iterate through all bits which are set in the flags and check whether we find at least
// one pp plugin which handles it.
for (unsigned int mask = 1; mask < (1u << (sizeof(unsigned int)*8-1));mask <<= 1) {
if (pFlags & mask) {
bool have = false;
for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
if (pimpl->mPostProcessingSteps[a]-> IsActive(mask) ) {
have = true;
break;
}
}
if (!have) {
return false;
}
}
}
ASSIMP_END_EXCEPTION_REGION(bool);
return true;
}
// ------------------------------------------------------------------------------------------------
const aiScene* Importer::ReadFileFromMemory(const void* pBuffer, size_t pLength, unsigned int pFlags, const char* pHint ) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
IOSystem* io = pimpl->mIOHandler;
try {
if (pHint == nullptr) {
pHint = "";
}
if (!pBuffer || !pLength || strlen(pHint) > MaxLenHint ) {
pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
return nullptr;
}
// prevent deletion of the previous IOHandler
pimpl->mIOHandler = nullptr;
SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength,io));
// read the file and recover the previous IOSystem
static const size_t BufSize(Importer::MaxLenHint + 28);
char fbuff[BufSize];
ai_snprintf(fbuff, BufSize, "%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
ReadFile(fbuff,pFlags);
SetIOHandler(io);
} catch(const DeadlyImportError &e) {
pimpl->mErrorString = e.what();
pimpl->mException = std::current_exception();
SetIOHandler(io);
return ExceptionSwallower<const aiScene*>()(); \
} catch(...) {
pimpl->mErrorString = "Unknown exception";
pimpl->mException = std::current_exception();
SetIOHandler(io);
return ExceptionSwallower<const aiScene*>()(); \
}
2015-05-19 03:57:13 +00:00
return pimpl->mScene;
}
// ------------------------------------------------------------------------------------------------
void WriteLogOpening(const std::string& file) {
ASSIMP_LOG_INFO("Load ", file);
2015-05-19 03:57:13 +00:00
// print a full version dump. This is nice because we don't
// need to ask the authors of incoming bug reports for
// the library version they're using - a log dump is
// sufficient.
const unsigned int flags = aiGetCompileFlags();
2018-04-26 12:10:18 +00:00
std::stringstream stream;
stream << "Assimp " << aiGetVersionMajor() << "." << aiGetVersionMinor() << "." << aiGetVersionRevision() << " "
#if defined(ASSIMP_BUILD_ARCHITECTURE)
<< ASSIMP_BUILD_ARCHITECTURE
#elif defined(_M_IX86) || defined(__x86_32__) || defined(__i386__)
<< "x86"
2015-05-19 03:52:10 +00:00
#elif defined(_M_X64) || defined(__x86_64__)
<< "amd64"
#elif defined(_M_IA64) || defined(__ia64__)
<< "itanium"
#elif defined(__ppc__) || defined(__powerpc__)
<< "ppc32"
#elif defined(__powerpc64__)
<< "ppc64"
#elif defined(__arm__)
<< "arm"
#else
<< "<unknown architecture>"
#endif
<< " "
#if defined(ASSIMP_BUILD_COMPILER)
<< (ASSIMP_BUILD_COMPILER)
#elif defined(_MSC_VER)
<< "msvc"
#elif defined(__GNUC__)
<< "gcc"
#elif defined(__clang__)
<< "clang"
#elif defined(__EMSCRIPTEN__)
<< "emscripten"
#elif defined(__MINGW32__)
<< "MinGW-w64 32bit"
#elif defined(__MINGW64__)
<< "MinGW-w64 64bit"
#else
<< "<unknown compiler>"
#endif
#ifdef ASSIMP_BUILD_DEBUG
<< " debug"
#endif
<< (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
<< (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
<< (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
<< (flags & ASSIMP_CFLAGS_DOUBLE_SUPPORT ? " double : " : "single : ");
2018-04-26 12:10:18 +00:00
ASSIMP_LOG_DEBUG(stream.str());
}
// ------------------------------------------------------------------------------------------------
// Reads the given file and returns its contents if successful.
const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
const std::string pFile(_pFile);
2015-05-19 03:57:13 +00:00
// ----------------------------------------------------------------------
// Put a large try block around everything to catch all std::exception's
// that might be thrown by STL containers or by new().
// ImportErrorException's are throw by ourselves and caught elsewhere.
//-----------------------------------------------------------------------
2015-05-19 03:57:13 +00:00
WriteLogOpening(pFile);
#ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
2015-05-19 03:57:13 +00:00
try
#endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
2015-05-19 03:57:13 +00:00
{
// Check whether this Importer instance has already loaded
// a scene. In this case we need to delete the old one
if (pimpl->mScene) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_DEBUG("(Deleting previous scene)");
2015-05-19 03:57:13 +00:00
FreeScene();
}
2016-04-03 00:38:00 +00:00
// First check if the file is accessible at all
2015-05-19 03:57:13 +00:00
if( !pimpl->mIOHandler->Exists( pFile)) {
pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR(pimpl->mErrorString);
return nullptr;
2015-05-19 03:57:13 +00:00
}
std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
2015-05-19 03:57:13 +00:00
if (profiler) {
profiler->BeginRegion("total");
}
// Find an worker class which can handle the file extension.
// Multiple importers may be able to handle the same extension (.xml!); gather them all.
SetPropertyInteger("importerIndex", -1);
struct ImporterAndIndex {
BaseImporter * importer;
unsigned int index;
};
std::vector<ImporterAndIndex> possibleImporters;
for (unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
// Every importer has a list of supported extensions.
std::set<std::string> extensions;
pimpl->mImporter[a]->GetExtensionList(extensions);
// CAUTION: Do not just search for the extension!
// GetExtension() returns the part after the *last* dot, but some extensions have dots
// inside them, e.g. ogre.mesh.xml. Compare the entire end of the string.
for (std::set<std::string>::const_iterator it = extensions.cbegin(); it != extensions.cend(); ++it) {
// Yay for C++<20 not having std::string::ends_with()
std::string extension = "." + *it;
if (extension.length() <= pFile.length()) {
// Possible optimization: Fetch the lowercase filename!
if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - extension.length(), extension.c_str())) {
ImporterAndIndex candidate = { pimpl->mImporter[a], a };
possibleImporters.push_back(candidate);
break;
}
}
2015-05-19 03:57:13 +00:00
}
}
// If just one importer supports this extension, pick it and close the case.
BaseImporter* imp = nullptr;
if (1 == possibleImporters.size()) {
imp = possibleImporters[0].importer;
SetPropertyInteger("importerIndex", possibleImporters[0].index);
}
// If multiple importers claim this file extension, ask them to look at the actual file data to decide.
// This can happen e.g. with XML (COLLADA vs. Irrlicht).
else {
for (std::vector<ImporterAndIndex>::const_iterator it = possibleImporters.begin(); it < possibleImporters.end(); ++it) {
BaseImporter & importer = *it->importer;
ASSIMP_LOG_INFO("Found a possible importer: " + std::string(importer.GetInfo()->mName) + "; trying signature-based detection");
if (importer.CanRead( pFile, pimpl->mIOHandler, true)) {
imp = &importer;
SetPropertyInteger("importerIndex", it->index);
break;
}
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:57:13 +00:00
}
if (!imp) {
// not so bad yet ... try format auto detection.
ASSIMP_LOG_INFO("File extension not known, trying signature-based detection");
for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
imp = pimpl->mImporter[a];
SetPropertyInteger("importerIndex", a);
break;
2015-05-19 03:57:13 +00:00
}
}
// Put a proper error message if no suitable importer was found
if( !imp) {
pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR(pimpl->mErrorString);
return nullptr;
2015-05-19 03:57:13 +00:00
}
}
// Get file size for progress handler
IOStream * fileIO = pimpl->mIOHandler->Open( pFile );
uint32_t fileSize = 0;
if (fileIO)
{
fileSize = static_cast<uint32_t>(fileIO->FileSize());
2015-05-19 03:57:13 +00:00
pimpl->mIOHandler->Close( fileIO );
}
// Dispatch the reading to the worker class for this format
const aiImporterDesc *desc( imp->GetInfo() );
std::string ext( "unknown" );
if ( nullptr != desc ) {
ext = desc->mName;
}
2021-05-13 11:05:31 +00:00
ASSIMP_LOG_INFO("Found a matching importer for this file format: ", ext, "." );
2015-05-19 03:57:13 +00:00
pimpl->mProgressHandler->UpdateFileRead( 0, fileSize );
if (profiler) {
profiler->BeginRegion("import");
}
pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
pimpl->mProgressHandler->UpdateFileRead( fileSize, fileSize );
if (profiler) {
profiler->EndRegion("import");
}
2018-01-08 08:26:26 +00:00
SetPropertyString("sourceFilePath", pFile);
2015-05-19 03:57:13 +00:00
// If successful, apply all active post processing steps to the imported data
if( pimpl->mScene) {
if (!pimpl->mScene->mMetaData || !pimpl->mScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT)) {
if (!pimpl->mScene->mMetaData) {
2019-12-05 11:37:22 +00:00
pimpl->mScene->mMetaData = new aiMetadata;
}
2019-12-05 11:25:12 +00:00
pimpl->mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT, aiString(ext));
}
#ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2015-05-19 03:57:13 +00:00
// The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
if (pFlags & aiProcess_ValidateDataStructure) {
2015-05-19 03:57:13 +00:00
ValidateDSProcess ds;
ds.ExecuteOnScene (this);
if (!pimpl->mScene) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
}
#endif // no validation
2015-05-19 03:57:13 +00:00
// Preprocess the scene and prepare it for post-processing
if (profiler) {
profiler->BeginRegion("preprocess");
}
ScenePreprocessor pre(pimpl->mScene);
pre.ProcessScene();
if (profiler) {
profiler->EndRegion("preprocess");
}
// Ensure that the validation process won't be called twice
ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
}
// if failed, extract the error string
else if( !pimpl->mScene) {
pimpl->mErrorString = imp->GetErrorText();
2020-08-18 17:14:51 +00:00
pimpl->mException = imp->GetException();
2015-05-19 03:57:13 +00:00
}
// clear any data allocated by post-process steps
pimpl->mPPShared->Clean();
if (profiler) {
profiler->EndRegion("total");
}
}
#ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
catch (std::exception &e) {
2015-05-19 03:57:13 +00:00
#if (defined _MSC_VER) && (defined _CPPRTTI)
2016-04-03 00:38:00 +00:00
// if we have RTTI get the full name of the exception that occurred
2015-05-19 03:57:13 +00:00
pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
#else
2015-05-19 03:57:13 +00:00
pimpl->mErrorString = std::string("std::exception: ") + e.what();
#endif
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR(pimpl->mErrorString);
delete pimpl->mScene; pimpl->mScene = nullptr;
2015-05-19 03:57:13 +00:00
}
#endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
2015-05-19 03:57:13 +00:00
// either successful or failure - the pointer expresses it anyways
2020-08-18 17:14:51 +00:00
ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
2015-05-19 03:57:13 +00:00
return pimpl->mScene;
}
// ------------------------------------------------------------------------------------------------
// Apply post-processing to the currently bound scene
const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
// Return immediately if no scene is active
if (!pimpl->mScene) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
2015-05-19 03:57:13 +00:00
// If no flags are given, return the current scene with no further action
if (!pFlags) {
return pimpl->mScene;
}
2015-05-19 03:57:13 +00:00
// In debug builds: run basic flag validation
ai_assert(_ValidateFlags(pFlags));
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO("Entering post processing pipeline");
#ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2015-05-19 03:57:13 +00:00
// The ValidateDS process plays an exceptional role. It isn't contained in the global
// list of post-processing steps, so we need to call it manually.
if (pFlags & aiProcess_ValidateDataStructure) {
2015-05-19 03:57:13 +00:00
ValidateDSProcess ds;
ds.ExecuteOnScene (this);
if (!pimpl->mScene) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
}
#endif // no validation
#ifdef ASSIMP_BUILD_DEBUG
2015-05-19 03:57:13 +00:00
if (pimpl->bExtraVerbose)
{
#ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("Verbose Import is not available due to build settings");
#endif // no validation
2015-05-19 03:57:13 +00:00
pFlags |= aiProcess_ValidateDataStructure;
}
#else
2015-05-19 03:57:13 +00:00
if (pimpl->bExtraVerbose) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN("Not a debug build, ignoring extra verbose setting");
2015-05-19 03:57:13 +00:00
}
#endif // ! DEBUG
std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
2015-05-19 03:57:13 +00:00
for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
BaseProcess* process = pimpl->mPostProcessingSteps[a];
pimpl->mProgressHandler->UpdatePostProcess(static_cast<int>(a), static_cast<int>(pimpl->mPostProcessingSteps.size()) );
2015-05-19 03:57:13 +00:00
if( process->IsActive( pFlags)) {
if (profiler) {
profiler->BeginRegion("postprocess");
}
2015-05-19 03:57:13 +00:00
process->ExecuteOnScene ( this );
2015-05-19 03:57:13 +00:00
if (profiler) {
profiler->EndRegion("postprocess");
}
}
if( !pimpl->mScene) {
break;
}
#ifdef ASSIMP_BUILD_DEBUG
#ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2015-05-19 03:57:13 +00:00
continue;
#endif // no validation
2015-05-19 03:57:13 +00:00
// If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
if (pimpl->bExtraVerbose) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_DEBUG("Verbose Import: re-validating data structures");
2015-05-19 03:57:13 +00:00
ValidateDSProcess ds;
ds.ExecuteOnScene (this);
if( !pimpl->mScene) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR("Verbose Import: failed to re-validate data structures");
2015-05-19 03:57:13 +00:00
break;
}
}
#endif // ! DEBUG
2015-05-19 03:57:13 +00:00
}
pimpl->mProgressHandler->UpdatePostProcess( static_cast<int>(pimpl->mPostProcessingSteps.size()),
2018-04-19 14:48:43 +00:00
static_cast<int>(pimpl->mPostProcessingSteps.size()) );
2015-05-19 03:57:13 +00:00
// update private scene flags
if( pimpl->mScene ) {
ScenePriv(pimpl->mScene)->mPPStepsApplied |= pFlags;
}
2015-05-19 03:57:13 +00:00
// clear any data allocated by post-process steps
pimpl->mPPShared->Clean();
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO("Leaving post processing pipeline");
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(const aiScene*);
2015-05-19 03:57:13 +00:00
return pimpl->mScene;
}
// ------------------------------------------------------------------------------------------------
const aiScene* Importer::ApplyCustomizedPostProcessing( BaseProcess *rootProcess, bool requestValidation ) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
ASSIMP_BEGIN_EXCEPTION_REGION();
// Return immediately if no scene is active
if ( nullptr == pimpl->mScene ) {
return nullptr;
}
// If no flags are given, return the current scene with no further action
if (nullptr == rootProcess) {
return pimpl->mScene;
}
// In debug builds: run basic flag validation
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO( "Entering customized post processing pipeline" );
#ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
// The ValidateDS process plays an exceptional role. It isn't contained in the global
// list of post-processing steps, so we need to call it manually.
if ( requestValidation )
{
ValidateDSProcess ds;
ds.ExecuteOnScene( this );
if ( !pimpl->mScene ) {
return nullptr;
}
}
#endif // no validation
#ifdef ASSIMP_BUILD_DEBUG
if ( pimpl->bExtraVerbose )
{
#ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR( "Verbose Import is not available due to build settings" );
#endif // no validation
}
#else
if ( pimpl->bExtraVerbose ) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_WARN( "Not a debug build, ignoring extra verbose setting" );
}
#endif // ! DEBUG
std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
if ( profiler ) {
profiler->BeginRegion( "postprocess" );
}
rootProcess->ExecuteOnScene( this );
if ( profiler ) {
profiler->EndRegion( "postprocess" );
}
// If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
if ( pimpl->bExtraVerbose || requestValidation ) {
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_DEBUG( "Verbose Import: revalidating data structures" );
ValidateDSProcess ds;
ds.ExecuteOnScene( this );
if ( !pimpl->mScene ) {
2018-04-19 14:48:43 +00:00
ASSIMP_LOG_ERROR( "Verbose Import: failed to revalidate data structures" );
}
}
// clear any data allocated by post-process steps
pimpl->mPPShared->Clean();
2018-04-19 15:21:21 +00:00
ASSIMP_LOG_INFO( "Leaving customized post processing pipeline" );
ASSIMP_END_EXCEPTION_REGION( const aiScene* );
return pimpl->mScene;
}
// ------------------------------------------------------------------------------------------------
// Helper function to check whether an extension is supported by ASSIMP
bool Importer::IsExtensionSupported(const char* szExtension) const {
2018-04-19 15:21:21 +00:00
return nullptr != GetImporter(szExtension);
}
// ------------------------------------------------------------------------------------------------
size_t Importer::GetImporterCount() const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return pimpl->mImporter.size();
}
// ------------------------------------------------------------------------------------------------
const aiImporterDesc* Importer::GetImporterInfo(size_t index) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
if (index >= pimpl->mImporter.size()) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
return pimpl->mImporter[index]->GetInfo();
}
// ------------------------------------------------------------------------------------------------
BaseImporter* Importer::GetImporter (size_t index) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
if (index >= pimpl->mImporter.size()) {
return nullptr;
2015-05-19 03:57:13 +00:00
}
return pimpl->mImporter[index];
}
// ------------------------------------------------------------------------------------------------
// Find a loader plugin for a given file extension
BaseImporter* Importer::GetImporter (const char* szExtension) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return GetImporter(GetImporterIndex(szExtension));
}
// ------------------------------------------------------------------------------------------------
// Find a loader plugin for a given file extension
size_t Importer::GetImporterIndex (const char* szExtension) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
ai_assert(nullptr != szExtension);
2018-03-20 17:43:54 +00:00
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
// skip over wild-card and dot characters at string head --
for ( ; *szExtension == '*' || *szExtension == '.'; ++szExtension );
2015-05-19 03:57:13 +00:00
std::string ext(szExtension);
if (ext.empty()) {
return static_cast<size_t>(-1);
}
2021-04-03 08:25:03 +00:00
ext = ai_tolower(ext);
2015-05-19 03:57:13 +00:00
std::set<std::string> str;
for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
str.clear();
(*i)->GetExtensionList(str);
for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
if (ext == *it) {
return std::distance(static_cast< std::vector<BaseImporter*>::const_iterator >(pimpl->mImporter.begin()), i);
}
}
}
ASSIMP_END_EXCEPTION_REGION(size_t);
return static_cast<size_t>(-1);
}
// ------------------------------------------------------------------------------------------------
// Helper function to build a list of all file extensions supported by ASSIMP
void Importer::GetExtensionList(aiString& szOut) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
std::set<std::string> str;
for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
(*i)->GetExtensionList(str);
}
2018-05-25 18:16:08 +00:00
// List can be empty
if( !str.empty() ) {
2018-05-25 18:16:08 +00:00
for (std::set<std::string>::const_iterator it = str.begin();; ) {
szOut.Append("*.");
szOut.Append((*it).c_str());
if (++it == str.end()) {
break;
}
szOut.Append(";");
}
}
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(void);
}
// ------------------------------------------------------------------------------------------------
// Set a configuration property
bool Importer::SetPropertyInteger(const char* szName, int iValue) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
bool existing;
ASSIMP_BEGIN_EXCEPTION_REGION();
existing = SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue);
ASSIMP_END_EXCEPTION_REGION(bool);
return existing;
}
// ------------------------------------------------------------------------------------------------
// Set a configuration property
bool Importer::SetPropertyFloat(const char* szName, ai_real iValue) {
2019-12-07 08:08:15 +00:00
ai_assert(nullptr != pimpl);
2018-05-13 14:35:03 +00:00
bool existing;
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2018-05-13 14:35:03 +00:00
existing = SetGenericProperty<ai_real>(pimpl->mFloatProperties, szName,iValue);
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(bool);
2018-05-13 14:35:03 +00:00
return existing;
}
// ------------------------------------------------------------------------------------------------
// Set a configuration property
bool Importer::SetPropertyString(const char* szName, const std::string& value) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2018-05-13 14:35:03 +00:00
bool existing;
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2018-05-13 14:35:03 +00:00
existing = SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value);
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(bool);
2018-05-13 14:35:03 +00:00
return existing;
}
// ------------------------------------------------------------------------------------------------
// Set a configuration property
bool Importer::SetPropertyMatrix(const char* szName, const aiMatrix4x4& value) {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2018-05-13 14:35:03 +00:00
bool existing;
2015-05-19 03:57:13 +00:00
ASSIMP_BEGIN_EXCEPTION_REGION();
2018-05-13 14:35:03 +00:00
existing = SetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties, szName,value);
2015-05-19 03:57:13 +00:00
ASSIMP_END_EXCEPTION_REGION(bool);
2018-05-13 14:35:03 +00:00
return existing;
}
2021-10-06 12:41:45 +00:00
// ------------------------------------------------------------------------------------------------
// Set a configuration property
bool Importer::SetPropertyPointer(const char* szName, void* value) {
ai_assert(nullptr != pimpl);
2023-01-16 08:12:35 +00:00
2021-10-06 12:41:45 +00:00
bool existing;
ASSIMP_BEGIN_EXCEPTION_REGION();
existing = SetGenericProperty<void*>(pimpl->mPointerProperties, szName,value);
ASSIMP_END_EXCEPTION_REGION(bool);
return existing;
}
// ------------------------------------------------------------------------------------------------
// Get a configuration property
int Importer::GetPropertyInteger(const char* szName, int iErrorReturn /*= 0xffffffff*/) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
}
// ------------------------------------------------------------------------------------------------
// Get a configuration property
ai_real Importer::GetPropertyFloat(const char* szName, ai_real iErrorReturn /*= 10e10*/) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
return GetGenericProperty<ai_real>(pimpl->mFloatProperties,szName,iErrorReturn);
}
// ------------------------------------------------------------------------------------------------
// Get a configuration property
2020-02-18 16:42:59 +00:00
std::string Importer::GetPropertyString(const char* szName, const std::string& iErrorReturn /*= ""*/) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
}
// ------------------------------------------------------------------------------------------------
// Get a configuration property
2020-02-18 16:42:59 +00:00
aiMatrix4x4 Importer::GetPropertyMatrix(const char* szName, const aiMatrix4x4& iErrorReturn /*= aiMatrix4x4()*/) const {
2019-12-07 08:18:45 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
return GetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties,szName,iErrorReturn);
}
2021-10-06 12:41:45 +00:00
// ------------------------------------------------------------------------------------------------
// Get a configuration property
void* Importer::GetPropertyPointer(const char* szName, void* iErrorReturn /*= nullptr*/) const {
ai_assert(nullptr != pimpl);
2023-01-16 08:12:35 +00:00
2021-10-06 12:41:45 +00:00
return GetGenericProperty<void*>(pimpl->mPointerProperties,szName,iErrorReturn);
}
// ------------------------------------------------------------------------------------------------
// Get the memory requirements of a single node
inline
void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode) {
if ( nullptr == pcNode ) {
return;
}
2015-05-19 03:57:13 +00:00
iScene += sizeof(aiNode);
iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
iScene += sizeof(void*) * pcNode->mNumChildren;
2015-05-19 03:52:10 +00:00
2015-05-19 03:57:13 +00:00
for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
AddNodeWeight(iScene,pcNode->mChildren[i]);
}
}
// ------------------------------------------------------------------------------------------------
// Get the memory requirements of the scene
void Importer::GetMemoryRequirements(aiMemoryInfo& in) const {
2019-12-07 07:58:47 +00:00
ai_assert(nullptr != pimpl);
2015-05-19 03:57:13 +00:00
in = aiMemoryInfo();
aiScene* mScene = pimpl->mScene;
// return if we have no scene loaded
2020-02-18 16:42:59 +00:00
if (!mScene)
2015-05-19 03:57:13 +00:00
return;
in.total = sizeof(aiScene);
// add all meshes
for (unsigned int i = 0; i < mScene->mNumMeshes;++i) {
2015-05-19 03:57:13 +00:00
in.meshes += sizeof(aiMesh);
if (mScene->mMeshes[i]->HasPositions()) {
in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
}
if (mScene->mMeshes[i]->HasNormals()) {
in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
}
if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
}
for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
if (mScene->mMeshes[i]->HasVertexColors(a)) {
in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
} else {
break;
2015-05-19 03:57:13 +00:00
}
}
for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
if (mScene->mMeshes[i]->HasTextureCoords(a)) {
in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
} else {
break;
2015-05-19 03:57:13 +00:00
}
}
if (mScene->mMeshes[i]->HasBones()) {
in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
in.meshes += sizeof(aiBone);
in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
}
}
in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
}
in.total += in.meshes;
2015-05-19 03:57:13 +00:00
// add all embedded textures
for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
const aiTexture* pc = mScene->mTextures[i];
in.textures += sizeof(aiTexture);
if (pc->mHeight) {
in.textures += 4 * pc->mHeight * pc->mWidth;
} else {
in.textures += pc->mWidth;
2015-05-19 03:57:13 +00:00
}
}
in.total += in.textures;
// add all animations
for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
const aiAnimation* pc = mScene->mAnimations[i];
in.animations += sizeof(aiAnimation);
// add all bone anims
for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
2020-07-16 06:26:02 +00:00
const aiNodeAnim* pc2 = pc->mChannels[a];
2015-05-19 03:57:13 +00:00
in.animations += sizeof(aiNodeAnim);
in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
}
}
in.total += in.animations;
// add all cameras and all lights
in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
// add all nodes
AddNodeWeight(in.nodes,mScene->mRootNode);
in.total += in.nodes;
// add all materials
for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
const aiMaterial* pc = mScene->mMaterials[i];
in.materials += sizeof(aiMaterial);
in.materials += pc->mNumAllocated * sizeof(void*);
for (unsigned int a = 0; a < pc->mNumProperties;++a) {
in.materials += pc->mProperties[a]->mDataLength;
}
}
2020-02-18 16:42:59 +00:00
2015-05-19 03:57:13 +00:00
in.total += in.materials;
}