Importer.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Importer.cpp
  35. * @brief Implementation of the CPP-API class #Importer
  36. */
  37. #include <assimp/version.h>
  38. #include <assimp/config.h>
  39. #include <assimp/importerdesc.h>
  40. // ------------------------------------------------------------------------------------------------
  41. /* Uncomment this line to prevent Assimp from catching unknown exceptions.
  42. *
  43. * Note that any Exception except DeadlyImportError may lead to
  44. * undefined behaviour -> loaders could remain in an unusable state and
  45. * further imports with the same Importer instance could fail/crash/burn ...
  46. */
  47. // ------------------------------------------------------------------------------------------------
  48. #ifndef ASSIMP_BUILD_DEBUG
  49. # define ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  50. #endif
  51. // ------------------------------------------------------------------------------------------------
  52. // Internal headers
  53. // ------------------------------------------------------------------------------------------------
  54. #include "Common/Importer.h"
  55. #include "Common/BaseProcess.h"
  56. #include "Common/DefaultProgressHandler.h"
  57. #include "PostProcessing/ProcessHelper.h"
  58. #include "Common/ScenePreprocessor.h"
  59. #include "Common/ScenePrivate.h"
  60. #include <assimp/BaseImporter.h>
  61. #include <assimp/GenericProperty.h>
  62. #include <assimp/MemoryIOWrapper.h>
  63. #include <assimp/Profiler.h>
  64. #include <assimp/TinyFormatter.h>
  65. #include <assimp/Exceptional.h>
  66. #include <assimp/Profiler.h>
  67. #include <assimp/commonMetaData.h>
  68. #include <exception>
  69. #include <set>
  70. #include <memory>
  71. #include <cctype>
  72. #include <assimp/DefaultIOStream.h>
  73. #include <assimp/DefaultIOSystem.h>
  74. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  75. # include "PostProcessing/ValidateDataStructure.h"
  76. #endif
  77. using namespace Assimp::Profiling;
  78. using namespace Assimp::Formatter;
  79. namespace Assimp {
  80. // ImporterRegistry.cpp
  81. void GetImporterInstanceList(std::vector< BaseImporter* >& out);
  82. void DeleteImporterInstanceList(std::vector< BaseImporter* >& out);
  83. // PostStepRegistry.cpp
  84. void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out);
  85. }
  86. using namespace Assimp;
  87. using namespace Assimp::Intern;
  88. // ------------------------------------------------------------------------------------------------
  89. // Intern::AllocateFromAssimpHeap serves as abstract base class. It overrides
  90. // new and delete (and their array counterparts) of public API classes (e.g. Logger) to
  91. // utilize our DLL heap.
  92. // See http://www.gotw.ca/publications/mill15.htm
  93. // ------------------------------------------------------------------------------------------------
  94. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes) {
  95. return ::operator new(num_bytes);
  96. }
  97. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes, const std::nothrow_t& ) throw() {
  98. try {
  99. return AllocateFromAssimpHeap::operator new( num_bytes );
  100. }
  101. catch( ... ) {
  102. return nullptr;
  103. }
  104. }
  105. void AllocateFromAssimpHeap::operator delete ( void* data) {
  106. return ::operator delete(data);
  107. }
  108. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes) {
  109. return ::operator new[](num_bytes);
  110. }
  111. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw() {
  112. try {
  113. return AllocateFromAssimpHeap::operator new[]( num_bytes );
  114. } catch( ... ) {
  115. return nullptr;
  116. }
  117. }
  118. void AllocateFromAssimpHeap::operator delete[] ( void* data) {
  119. return ::operator delete[](data);
  120. }
  121. // ------------------------------------------------------------------------------------------------
  122. // Importer constructor.
  123. Importer::Importer()
  124. : pimpl( new ImporterPimpl ) {
  125. pimpl->mScene = nullptr;
  126. pimpl->mErrorString = std::string();
  127. // Allocate a default IO handler
  128. pimpl->mIOHandler = new DefaultIOSystem;
  129. pimpl->mIsDefaultHandler = true;
  130. pimpl->bExtraVerbose = false; // disable extra verbose mode by default
  131. pimpl->mProgressHandler = new DefaultProgressHandler();
  132. pimpl->mIsDefaultProgressHandler = true;
  133. GetImporterInstanceList(pimpl->mImporter);
  134. GetPostProcessingStepInstanceList(pimpl->mPostProcessingSteps);
  135. // Allocate a SharedPostProcessInfo object and store pointers to it in all post-process steps in the list.
  136. pimpl->mPPShared = new SharedPostProcessInfo();
  137. for (std::vector<BaseProcess*>::iterator it = pimpl->mPostProcessingSteps.begin();
  138. it != pimpl->mPostProcessingSteps.end();
  139. ++it) {
  140. (*it)->SetSharedData(pimpl->mPPShared);
  141. }
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. // Destructor of Importer
  145. Importer::~Importer() {
  146. // Delete all import plugins
  147. DeleteImporterInstanceList(pimpl->mImporter);
  148. // Delete all post-processing plug-ins
  149. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); ++a ) {
  150. delete pimpl->mPostProcessingSteps[a];
  151. }
  152. // Delete the assigned IO and progress handler
  153. delete pimpl->mIOHandler;
  154. delete pimpl->mProgressHandler;
  155. // Kill imported scene. Destructor's should do that recursively
  156. delete pimpl->mScene;
  157. // Delete shared post-processing data
  158. delete pimpl->mPPShared;
  159. // and finally the pimpl itself
  160. delete pimpl;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. // Register a custom post-processing step
  164. aiReturn Importer::RegisterPPStep(BaseProcess* pImp) {
  165. ai_assert( nullptr != pImp );
  166. ASSIMP_BEGIN_EXCEPTION_REGION();
  167. pimpl->mPostProcessingSteps.push_back(pImp);
  168. ASSIMP_LOG_INFO("Registering custom post-processing step");
  169. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  170. return AI_SUCCESS;
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Register a custom loader plugin
  174. aiReturn Importer::RegisterLoader(BaseImporter* pImp) {
  175. ai_assert(nullptr != pImp);
  176. ASSIMP_BEGIN_EXCEPTION_REGION();
  177. // --------------------------------------------------------------------
  178. // Check whether we would have two loaders for the same file extension
  179. // This is absolutely OK, but we should warn the developer of the new
  180. // loader that his code will probably never be called if the first
  181. // loader is a bit too lazy in his file checking.
  182. // --------------------------------------------------------------------
  183. std::set<std::string> st;
  184. std::string baked;
  185. pImp->GetExtensionList(st);
  186. for(std::set<std::string>::const_iterator it = st.begin(); it != st.end(); ++it) {
  187. #ifdef ASSIMP_BUILD_DEBUG
  188. if (IsExtensionSupported(*it)) {
  189. ASSIMP_LOG_WARN("The file extension ", *it, " is already in use");
  190. }
  191. #endif
  192. baked += *it;
  193. }
  194. // add the loader
  195. pimpl->mImporter.push_back(pImp);
  196. ASSIMP_LOG_INFO("Registering custom importer for these file extensions: ", baked);
  197. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  198. return AI_SUCCESS;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. // Unregister a custom loader plugin
  202. aiReturn Importer::UnregisterLoader(BaseImporter* pImp) {
  203. if(!pImp) {
  204. // unregistering a nullptr importer is no problem for us ... really!
  205. return AI_SUCCESS;
  206. }
  207. ASSIMP_BEGIN_EXCEPTION_REGION();
  208. std::vector<BaseImporter*>::iterator it = std::find(pimpl->mImporter.begin(),
  209. pimpl->mImporter.end(),pImp);
  210. if (it != pimpl->mImporter.end()) {
  211. pimpl->mImporter.erase(it);
  212. ASSIMP_LOG_INFO("Unregistering custom importer: ");
  213. return AI_SUCCESS;
  214. }
  215. ASSIMP_LOG_WARN("Unable to remove custom importer: I can't find you ...");
  216. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  217. return AI_FAILURE;
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. // Unregister a custom loader plugin
  221. aiReturn Importer::UnregisterPPStep(BaseProcess* pImp) {
  222. if(!pImp) {
  223. // unregistering a nullptr ppstep is no problem for us ... really!
  224. return AI_SUCCESS;
  225. }
  226. ASSIMP_BEGIN_EXCEPTION_REGION();
  227. std::vector<BaseProcess*>::iterator it = std::find(pimpl->mPostProcessingSteps.begin(),
  228. pimpl->mPostProcessingSteps.end(),pImp);
  229. if (it != pimpl->mPostProcessingSteps.end()) {
  230. pimpl->mPostProcessingSteps.erase(it);
  231. ASSIMP_LOG_INFO("Unregistering custom post-processing step");
  232. return AI_SUCCESS;
  233. }
  234. ASSIMP_LOG_WARN("Unable to remove custom post-processing step: I can't find you ..");
  235. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  236. return AI_FAILURE;
  237. }
  238. // ------------------------------------------------------------------------------------------------
  239. // Supplies a custom IO handler to the importer to open and access files.
  240. void Importer::SetIOHandler( IOSystem* pIOHandler) {
  241. ai_assert(nullptr != pimpl);
  242. ASSIMP_BEGIN_EXCEPTION_REGION();
  243. // If the new handler is zero, allocate a default IO implementation.
  244. if (!pIOHandler) {
  245. // Release pointer in the possession of the caller
  246. pimpl->mIOHandler = new DefaultIOSystem();
  247. pimpl->mIsDefaultHandler = true;
  248. } else if (pimpl->mIOHandler != pIOHandler) { // Otherwise register the custom handler
  249. delete pimpl->mIOHandler;
  250. pimpl->mIOHandler = pIOHandler;
  251. pimpl->mIsDefaultHandler = false;
  252. }
  253. ASSIMP_END_EXCEPTION_REGION(void);
  254. }
  255. // ------------------------------------------------------------------------------------------------
  256. // Get the currently set IO handler
  257. IOSystem* Importer::GetIOHandler() const {
  258. ai_assert(nullptr != pimpl);
  259. return pimpl->mIOHandler;
  260. }
  261. // ------------------------------------------------------------------------------------------------
  262. // Check whether a custom IO handler is currently set
  263. bool Importer::IsDefaultIOHandler() const {
  264. ai_assert(nullptr != pimpl);
  265. return pimpl->mIsDefaultHandler;
  266. }
  267. // ------------------------------------------------------------------------------------------------
  268. // Supplies a custom progress handler to get regular callbacks during importing
  269. void Importer::SetProgressHandler(ProgressHandler* pHandler) {
  270. ai_assert(nullptr != pimpl);
  271. ASSIMP_BEGIN_EXCEPTION_REGION();
  272. // If the new handler is zero, allocate a default implementation.
  273. if (!pHandler) {
  274. // Release pointer in the possession of the caller
  275. pimpl->mProgressHandler = new DefaultProgressHandler();
  276. pimpl->mIsDefaultProgressHandler = true;
  277. } else if (pimpl->mProgressHandler != pHandler) { // Otherwise register the custom handler
  278. delete pimpl->mProgressHandler;
  279. pimpl->mProgressHandler = pHandler;
  280. pimpl->mIsDefaultProgressHandler = false;
  281. }
  282. ASSIMP_END_EXCEPTION_REGION(void);
  283. }
  284. // ------------------------------------------------------------------------------------------------
  285. // Get the currently set progress handler
  286. ProgressHandler* Importer::GetProgressHandler() const {
  287. ai_assert(nullptr != pimpl);
  288. return pimpl->mProgressHandler;
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. // Check whether a custom progress handler is currently set
  292. bool Importer::IsDefaultProgressHandler() const {
  293. ai_assert(nullptr != pimpl);
  294. return pimpl->mIsDefaultProgressHandler;
  295. }
  296. // ------------------------------------------------------------------------------------------------
  297. // Validate post process step flags
  298. bool _ValidateFlags(unsigned int pFlags) {
  299. if (pFlags & aiProcess_GenSmoothNormals && pFlags & aiProcess_GenNormals) {
  300. ASSIMP_LOG_ERROR("#aiProcess_GenSmoothNormals and #aiProcess_GenNormals are incompatible");
  301. return false;
  302. }
  303. if (pFlags & aiProcess_OptimizeGraph && pFlags & aiProcess_PreTransformVertices) {
  304. ASSIMP_LOG_ERROR("#aiProcess_OptimizeGraph and #aiProcess_PreTransformVertices are incompatible");
  305. return false;
  306. }
  307. return true;
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Free the current scene
  311. void Importer::FreeScene( ) {
  312. ai_assert(nullptr != pimpl);
  313. ASSIMP_BEGIN_EXCEPTION_REGION();
  314. delete pimpl->mScene;
  315. pimpl->mScene = nullptr;
  316. pimpl->mErrorString = std::string();
  317. pimpl->mException = std::exception_ptr();
  318. ASSIMP_END_EXCEPTION_REGION(void);
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. // Get the current error string, if any
  322. const char* Importer::GetErrorString() const {
  323. ai_assert(nullptr != pimpl);
  324. // Must remain valid as long as ReadFile() or FreeFile() are not called
  325. return pimpl->mErrorString.c_str();
  326. }
  327. const std::exception_ptr& Importer::GetException() const {
  328. ai_assert(nullptr != pimpl);
  329. // Must remain valid as long as ReadFile() or FreeFile() are not called
  330. return pimpl->mException;
  331. }
  332. // ------------------------------------------------------------------------------------------------
  333. // Enable extra-verbose mode
  334. void Importer::SetExtraVerbose(bool bDo) {
  335. ai_assert(nullptr != pimpl);
  336. pimpl->bExtraVerbose = bDo;
  337. }
  338. // ------------------------------------------------------------------------------------------------
  339. // Get the current scene
  340. const aiScene* Importer::GetScene() const {
  341. ai_assert(nullptr != pimpl);
  342. return pimpl->mScene;
  343. }
  344. // ------------------------------------------------------------------------------------------------
  345. // Orphan the current scene and return it.
  346. aiScene* Importer::GetOrphanedScene() {
  347. ai_assert(nullptr != pimpl);
  348. aiScene* s = pimpl->mScene;
  349. ASSIMP_BEGIN_EXCEPTION_REGION();
  350. pimpl->mScene = nullptr;
  351. pimpl->mErrorString = std::string();
  352. pimpl->mException = std::exception_ptr();
  353. ASSIMP_END_EXCEPTION_REGION(aiScene*);
  354. return s;
  355. }
  356. // ------------------------------------------------------------------------------------------------
  357. // Validate post-processing flags
  358. bool Importer::ValidateFlags(unsigned int pFlags) const {
  359. ASSIMP_BEGIN_EXCEPTION_REGION();
  360. // run basic checks for mutually exclusive flags
  361. if(!_ValidateFlags(pFlags)) {
  362. return false;
  363. }
  364. // ValidateDS does not anymore occur in the pp list, it plays an awesome extra role ...
  365. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  366. if (pFlags & aiProcess_ValidateDataStructure) {
  367. return false;
  368. }
  369. #endif
  370. pFlags &= ~aiProcess_ValidateDataStructure;
  371. // Now iterate through all bits which are set in the flags and check whether we find at least
  372. // one pp plugin which handles it.
  373. for (unsigned int mask = 1; mask < (1u << (sizeof(unsigned int)*8-1));mask <<= 1) {
  374. if (pFlags & mask) {
  375. bool have = false;
  376. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  377. if (pimpl->mPostProcessingSteps[a]-> IsActive(mask) ) {
  378. have = true;
  379. break;
  380. }
  381. }
  382. if (!have) {
  383. return false;
  384. }
  385. }
  386. }
  387. ASSIMP_END_EXCEPTION_REGION(bool);
  388. return true;
  389. }
  390. // ------------------------------------------------------------------------------------------------
  391. const aiScene* Importer::ReadFileFromMemory(const void* pBuffer, size_t pLength, unsigned int pFlags, const char* pHint ) {
  392. ai_assert(nullptr != pimpl);
  393. IOSystem* io = pimpl->mIOHandler;
  394. try {
  395. if (pHint == nullptr) {
  396. pHint = "";
  397. }
  398. if (!pBuffer || !pLength || strlen(pHint) > MaxLenHint ) {
  399. pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
  400. return nullptr;
  401. }
  402. // prevent deletion of the previous IOHandler
  403. pimpl->mIOHandler = nullptr;
  404. SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength,io));
  405. // read the file and recover the previous IOSystem
  406. static const size_t BufSize(Importer::MaxLenHint + 28);
  407. char fbuff[BufSize];
  408. ai_snprintf(fbuff, BufSize, "%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
  409. ReadFile(fbuff,pFlags);
  410. SetIOHandler(io);
  411. } catch(const DeadlyImportError &e) {
  412. pimpl->mErrorString = e.what();
  413. pimpl->mException = std::current_exception();
  414. SetIOHandler(io);
  415. return ExceptionSwallower<const aiScene*>()(); \
  416. } catch(...) {
  417. pimpl->mErrorString = "Unknown exception";
  418. pimpl->mException = std::current_exception();
  419. SetIOHandler(io);
  420. return ExceptionSwallower<const aiScene*>()(); \
  421. }
  422. return pimpl->mScene;
  423. }
  424. // ------------------------------------------------------------------------------------------------
  425. void WriteLogOpening(const std::string& file) {
  426. ASSIMP_LOG_INFO("Load ", file);
  427. // print a full version dump. This is nice because we don't
  428. // need to ask the authors of incoming bug reports for
  429. // the library version they're using - a log dump is
  430. // sufficient.
  431. const unsigned int flags = aiGetCompileFlags();
  432. std::stringstream stream;
  433. stream << "Assimp " << aiGetVersionMajor() << "." << aiGetVersionMinor() << "." << aiGetVersionRevision() << " "
  434. #if defined(ASSIMP_BUILD_ARCHITECTURE)
  435. << ASSIMP_BUILD_ARCHITECTURE
  436. #elif defined(_M_IX86) || defined(__x86_32__) || defined(__i386__)
  437. << "x86"
  438. #elif defined(_M_X64) || defined(__x86_64__)
  439. << "amd64"
  440. #elif defined(_M_IA64) || defined(__ia64__)
  441. << "itanium"
  442. #elif defined(__ppc__) || defined(__powerpc__)
  443. << "ppc32"
  444. #elif defined(__powerpc64__)
  445. << "ppc64"
  446. #elif defined(__arm__)
  447. << "arm"
  448. #else
  449. << "<unknown architecture>"
  450. #endif
  451. << " "
  452. #if defined(ASSIMP_BUILD_COMPILER)
  453. << (ASSIMP_BUILD_COMPILER)
  454. #elif defined(_MSC_VER)
  455. << "msvc"
  456. #elif defined(__GNUC__)
  457. << "gcc"
  458. #elif defined(__clang__)
  459. << "clang"
  460. #elif defined(__EMSCRIPTEN__)
  461. << "emscripten"
  462. #elif defined(__MINGW32__)
  463. << "MinGW-w64 32bit"
  464. #elif defined(__MINGW64__)
  465. << "MinGW-w64 64bit"
  466. #else
  467. << "<unknown compiler>"
  468. #endif
  469. #ifdef ASSIMP_BUILD_DEBUG
  470. << " debug"
  471. #endif
  472. << (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
  473. << (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
  474. << (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
  475. << (flags & ASSIMP_CFLAGS_DOUBLE_SUPPORT ? " double : " : "single : ");
  476. ASSIMP_LOG_DEBUG(stream.str());
  477. }
  478. // ------------------------------------------------------------------------------------------------
  479. // Reads the given file and returns its contents if successful.
  480. const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) {
  481. ai_assert(nullptr != pimpl);
  482. ASSIMP_BEGIN_EXCEPTION_REGION();
  483. const std::string pFile(_pFile);
  484. // ----------------------------------------------------------------------
  485. // Put a large try block around everything to catch all std::exception's
  486. // that might be thrown by STL containers or by new().
  487. // ImportErrorException's are throw by ourselves and caught elsewhere.
  488. //-----------------------------------------------------------------------
  489. WriteLogOpening(pFile);
  490. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  491. try
  492. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  493. {
  494. // Check whether this Importer instance has already loaded
  495. // a scene. In this case we need to delete the old one
  496. if (pimpl->mScene) {
  497. ASSIMP_LOG_DEBUG("(Deleting previous scene)");
  498. FreeScene();
  499. }
  500. // First check if the file is accessible at all
  501. if( !pimpl->mIOHandler->Exists( pFile)) {
  502. pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
  503. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  504. return nullptr;
  505. }
  506. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  507. if (profiler) {
  508. profiler->BeginRegion("total");
  509. }
  510. // Find an worker class which can handle the file extension.
  511. // Multiple importers may be able to handle the same extension (.xml!); gather them all.
  512. SetPropertyInteger("importerIndex", -1);
  513. struct ImporterAndIndex {
  514. BaseImporter * importer;
  515. unsigned int index;
  516. };
  517. std::vector<ImporterAndIndex> possibleImporters;
  518. for (unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  519. // Every importer has a list of supported extensions.
  520. std::set<std::string> extensions;
  521. pimpl->mImporter[a]->GetExtensionList(extensions);
  522. if (BaseImporter::HasExtension(pFile, extensions)) {
  523. ImporterAndIndex candidate = { pimpl->mImporter[a], a };
  524. possibleImporters.push_back(candidate);
  525. }
  526. }
  527. // If just one importer supports this extension, pick it and close the case.
  528. BaseImporter* imp = nullptr;
  529. if (1 == possibleImporters.size()) {
  530. imp = possibleImporters[0].importer;
  531. SetPropertyInteger("importerIndex", possibleImporters[0].index);
  532. }
  533. // If multiple importers claim this file extension, ask them to look at the actual file data to decide.
  534. // This can happen e.g. with XML (COLLADA vs. Irrlicht).
  535. else {
  536. for (std::vector<ImporterAndIndex>::const_iterator it = possibleImporters.begin(); it < possibleImporters.end(); ++it) {
  537. BaseImporter & importer = *it->importer;
  538. ASSIMP_LOG_INFO("Found a possible importer: " + std::string(importer.GetInfo()->mName) + "; trying signature-based detection");
  539. if (importer.CanRead( pFile, pimpl->mIOHandler, true)) {
  540. imp = &importer;
  541. SetPropertyInteger("importerIndex", it->index);
  542. break;
  543. }
  544. }
  545. }
  546. if (!imp) {
  547. // not so bad yet ... try format auto detection.
  548. ASSIMP_LOG_INFO("File extension not known, trying signature-based detection");
  549. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  550. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
  551. imp = pimpl->mImporter[a];
  552. SetPropertyInteger("importerIndex", a);
  553. break;
  554. }
  555. }
  556. // Put a proper error message if no suitable importer was found
  557. if( !imp) {
  558. pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  559. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  560. return nullptr;
  561. }
  562. }
  563. // Get file size for progress handler
  564. IOStream * fileIO = pimpl->mIOHandler->Open( pFile );
  565. uint32_t fileSize = 0;
  566. if (fileIO)
  567. {
  568. fileSize = static_cast<uint32_t>(fileIO->FileSize());
  569. pimpl->mIOHandler->Close( fileIO );
  570. }
  571. // Dispatch the reading to the worker class for this format
  572. const aiImporterDesc *desc( imp->GetInfo() );
  573. std::string ext( "unknown" );
  574. if ( nullptr != desc ) {
  575. ext = desc->mName;
  576. }
  577. ASSIMP_LOG_INFO("Found a matching importer for this file format: ", ext, "." );
  578. pimpl->mProgressHandler->UpdateFileRead( 0, fileSize );
  579. if (profiler) {
  580. profiler->BeginRegion("import");
  581. }
  582. pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
  583. pimpl->mProgressHandler->UpdateFileRead( fileSize, fileSize );
  584. if (profiler) {
  585. profiler->EndRegion("import");
  586. }
  587. SetPropertyString("sourceFilePath", pFile);
  588. // If successful, apply all active post processing steps to the imported data
  589. if( pimpl->mScene) {
  590. if (!pimpl->mScene->mMetaData || !pimpl->mScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT)) {
  591. if (!pimpl->mScene->mMetaData) {
  592. pimpl->mScene->mMetaData = new aiMetadata;
  593. }
  594. pimpl->mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT, aiString(ext));
  595. }
  596. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  597. // The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
  598. if (pFlags & aiProcess_ValidateDataStructure) {
  599. ValidateDSProcess ds;
  600. ds.ExecuteOnScene (this);
  601. if (!pimpl->mScene) {
  602. return nullptr;
  603. }
  604. }
  605. #endif // no validation
  606. // Preprocess the scene and prepare it for post-processing
  607. if (profiler) {
  608. profiler->BeginRegion("preprocess");
  609. }
  610. ScenePreprocessor pre(pimpl->mScene);
  611. pre.ProcessScene();
  612. if (profiler) {
  613. profiler->EndRegion("preprocess");
  614. }
  615. // Ensure that the validation process won't be called twice
  616. ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
  617. }
  618. // if failed, extract the error string
  619. else if( !pimpl->mScene) {
  620. pimpl->mErrorString = imp->GetErrorText();
  621. pimpl->mException = imp->GetException();
  622. }
  623. // clear any data allocated by post-process steps
  624. pimpl->mPPShared->Clean();
  625. if (profiler) {
  626. profiler->EndRegion("total");
  627. }
  628. }
  629. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  630. catch (std::exception &e) {
  631. #if (defined _MSC_VER) && (defined _CPPRTTI)
  632. // if we have RTTI get the full name of the exception that occurred
  633. pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  634. #else
  635. pimpl->mErrorString = std::string("std::exception: ") + e.what();
  636. #endif
  637. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  638. delete pimpl->mScene; pimpl->mScene = nullptr;
  639. }
  640. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  641. // either successful or failure - the pointer expresses it anyways
  642. ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
  643. return pimpl->mScene;
  644. }
  645. // ------------------------------------------------------------------------------------------------
  646. // Apply post-processing to the currently bound scene
  647. const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags) {
  648. ai_assert(nullptr != pimpl);
  649. ASSIMP_BEGIN_EXCEPTION_REGION();
  650. // Return immediately if no scene is active
  651. if (!pimpl->mScene) {
  652. return nullptr;
  653. }
  654. // If no flags are given, return the current scene with no further action
  655. if (!pFlags) {
  656. return pimpl->mScene;
  657. }
  658. // In debug builds: run basic flag validation
  659. ai_assert(_ValidateFlags(pFlags));
  660. ASSIMP_LOG_INFO("Entering post processing pipeline");
  661. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  662. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  663. // list of post-processing steps, so we need to call it manually.
  664. if (pFlags & aiProcess_ValidateDataStructure) {
  665. ValidateDSProcess ds;
  666. ds.ExecuteOnScene (this);
  667. if (!pimpl->mScene) {
  668. return nullptr;
  669. }
  670. }
  671. #endif // no validation
  672. #ifdef ASSIMP_BUILD_DEBUG
  673. if (pimpl->bExtraVerbose)
  674. {
  675. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  676. ASSIMP_LOG_ERROR("Verbose Import is not available due to build settings");
  677. #endif // no validation
  678. pFlags |= aiProcess_ValidateDataStructure;
  679. }
  680. #else
  681. if (pimpl->bExtraVerbose) {
  682. ASSIMP_LOG_WARN("Not a debug build, ignoring extra verbose setting");
  683. }
  684. #endif // ! DEBUG
  685. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  686. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  687. BaseProcess* process = pimpl->mPostProcessingSteps[a];
  688. pimpl->mProgressHandler->UpdatePostProcess(static_cast<int>(a), static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  689. if( process->IsActive( pFlags)) {
  690. if (profiler) {
  691. profiler->BeginRegion("postprocess");
  692. }
  693. process->ExecuteOnScene ( this );
  694. if (profiler) {
  695. profiler->EndRegion("postprocess");
  696. }
  697. }
  698. if( !pimpl->mScene) {
  699. break;
  700. }
  701. #ifdef ASSIMP_BUILD_DEBUG
  702. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  703. continue;
  704. #endif // no validation
  705. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  706. if (pimpl->bExtraVerbose) {
  707. ASSIMP_LOG_DEBUG("Verbose Import: re-validating data structures");
  708. ValidateDSProcess ds;
  709. ds.ExecuteOnScene (this);
  710. if( !pimpl->mScene) {
  711. ASSIMP_LOG_ERROR("Verbose Import: failed to re-validate data structures");
  712. break;
  713. }
  714. }
  715. #endif // ! DEBUG
  716. }
  717. pimpl->mProgressHandler->UpdatePostProcess( static_cast<int>(pimpl->mPostProcessingSteps.size()),
  718. static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  719. // update private scene flags
  720. if( pimpl->mScene ) {
  721. ScenePriv(pimpl->mScene)->mPPStepsApplied |= pFlags;
  722. }
  723. // clear any data allocated by post-process steps
  724. pimpl->mPPShared->Clean();
  725. ASSIMP_LOG_INFO("Leaving post processing pipeline");
  726. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  727. return pimpl->mScene;
  728. }
  729. // ------------------------------------------------------------------------------------------------
  730. const aiScene* Importer::ApplyCustomizedPostProcessing( BaseProcess *rootProcess, bool requestValidation ) {
  731. ai_assert(nullptr != pimpl);
  732. ASSIMP_BEGIN_EXCEPTION_REGION();
  733. // Return immediately if no scene is active
  734. if ( nullptr == pimpl->mScene ) {
  735. return nullptr;
  736. }
  737. // If no flags are given, return the current scene with no further action
  738. if (nullptr == rootProcess) {
  739. return pimpl->mScene;
  740. }
  741. // In debug builds: run basic flag validation
  742. ASSIMP_LOG_INFO( "Entering customized post processing pipeline" );
  743. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  744. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  745. // list of post-processing steps, so we need to call it manually.
  746. if ( requestValidation )
  747. {
  748. ValidateDSProcess ds;
  749. ds.ExecuteOnScene( this );
  750. if ( !pimpl->mScene ) {
  751. return nullptr;
  752. }
  753. }
  754. #endif // no validation
  755. #ifdef ASSIMP_BUILD_DEBUG
  756. if ( pimpl->bExtraVerbose )
  757. {
  758. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  759. ASSIMP_LOG_ERROR( "Verbose Import is not available due to build settings" );
  760. #endif // no validation
  761. }
  762. #else
  763. if ( pimpl->bExtraVerbose ) {
  764. ASSIMP_LOG_WARN( "Not a debug build, ignoring extra verbose setting" );
  765. }
  766. #endif // ! DEBUG
  767. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  768. if ( profiler ) {
  769. profiler->BeginRegion( "postprocess" );
  770. }
  771. rootProcess->ExecuteOnScene( this );
  772. if ( profiler ) {
  773. profiler->EndRegion( "postprocess" );
  774. }
  775. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  776. if ( pimpl->bExtraVerbose || requestValidation ) {
  777. ASSIMP_LOG_DEBUG( "Verbose Import: revalidating data structures" );
  778. ValidateDSProcess ds;
  779. ds.ExecuteOnScene( this );
  780. if ( !pimpl->mScene ) {
  781. ASSIMP_LOG_ERROR( "Verbose Import: failed to revalidate data structures" );
  782. }
  783. }
  784. // clear any data allocated by post-process steps
  785. pimpl->mPPShared->Clean();
  786. ASSIMP_LOG_INFO( "Leaving customized post processing pipeline" );
  787. ASSIMP_END_EXCEPTION_REGION( const aiScene* );
  788. return pimpl->mScene;
  789. }
  790. // ------------------------------------------------------------------------------------------------
  791. // Helper function to check whether an extension is supported by ASSIMP
  792. bool Importer::IsExtensionSupported(const char* szExtension) const {
  793. return nullptr != GetImporter(szExtension);
  794. }
  795. // ------------------------------------------------------------------------------------------------
  796. size_t Importer::GetImporterCount() const {
  797. ai_assert(nullptr != pimpl);
  798. return pimpl->mImporter.size();
  799. }
  800. // ------------------------------------------------------------------------------------------------
  801. const aiImporterDesc* Importer::GetImporterInfo(size_t index) const {
  802. ai_assert(nullptr != pimpl);
  803. if (index >= pimpl->mImporter.size()) {
  804. return nullptr;
  805. }
  806. return pimpl->mImporter[index]->GetInfo();
  807. }
  808. // ------------------------------------------------------------------------------------------------
  809. BaseImporter* Importer::GetImporter (size_t index) const {
  810. ai_assert(nullptr != pimpl);
  811. if (index >= pimpl->mImporter.size()) {
  812. return nullptr;
  813. }
  814. return pimpl->mImporter[index];
  815. }
  816. // ------------------------------------------------------------------------------------------------
  817. // Find a loader plugin for a given file extension
  818. BaseImporter* Importer::GetImporter (const char* szExtension) const {
  819. ai_assert(nullptr != pimpl);
  820. return GetImporter(GetImporterIndex(szExtension));
  821. }
  822. // ------------------------------------------------------------------------------------------------
  823. // Find a loader plugin for a given file extension
  824. size_t Importer::GetImporterIndex (const char* szExtension) const {
  825. ai_assert(nullptr != pimpl);
  826. ai_assert(nullptr != szExtension);
  827. ASSIMP_BEGIN_EXCEPTION_REGION();
  828. // skip over wild-card and dot characters at string head --
  829. for ( ; *szExtension == '*' || *szExtension == '.'; ++szExtension );
  830. std::string ext(szExtension);
  831. if (ext.empty()) {
  832. return static_cast<size_t>(-1);
  833. }
  834. ext = ai_tolower(ext);
  835. std::set<std::string> str;
  836. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  837. str.clear();
  838. (*i)->GetExtensionList(str);
  839. for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
  840. if (ext == *it) {
  841. return std::distance(static_cast< std::vector<BaseImporter*>::const_iterator >(pimpl->mImporter.begin()), i);
  842. }
  843. }
  844. }
  845. ASSIMP_END_EXCEPTION_REGION(size_t);
  846. return static_cast<size_t>(-1);
  847. }
  848. // ------------------------------------------------------------------------------------------------
  849. // Helper function to build a list of all file extensions supported by ASSIMP
  850. void Importer::GetExtensionList(aiString& szOut) const {
  851. ai_assert(nullptr != pimpl);
  852. ASSIMP_BEGIN_EXCEPTION_REGION();
  853. std::set<std::string> str;
  854. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  855. (*i)->GetExtensionList(str);
  856. }
  857. // List can be empty
  858. if( !str.empty() ) {
  859. for (std::set<std::string>::const_iterator it = str.begin();; ) {
  860. szOut.Append("*.");
  861. szOut.Append((*it).c_str());
  862. if (++it == str.end()) {
  863. break;
  864. }
  865. szOut.Append(";");
  866. }
  867. }
  868. ASSIMP_END_EXCEPTION_REGION(void);
  869. }
  870. // ------------------------------------------------------------------------------------------------
  871. // Set a configuration property
  872. bool Importer::SetPropertyInteger(const char* szName, int iValue) {
  873. ai_assert(nullptr != pimpl);
  874. bool existing;
  875. ASSIMP_BEGIN_EXCEPTION_REGION();
  876. existing = SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue);
  877. ASSIMP_END_EXCEPTION_REGION(bool);
  878. return existing;
  879. }
  880. // ------------------------------------------------------------------------------------------------
  881. // Set a configuration property
  882. bool Importer::SetPropertyFloat(const char* szName, ai_real iValue) {
  883. ai_assert(nullptr != pimpl);
  884. bool existing;
  885. ASSIMP_BEGIN_EXCEPTION_REGION();
  886. existing = SetGenericProperty<ai_real>(pimpl->mFloatProperties, szName,iValue);
  887. ASSIMP_END_EXCEPTION_REGION(bool);
  888. return existing;
  889. }
  890. // ------------------------------------------------------------------------------------------------
  891. // Set a configuration property
  892. bool Importer::SetPropertyString(const char* szName, const std::string& value) {
  893. ai_assert(nullptr != pimpl);
  894. bool existing;
  895. ASSIMP_BEGIN_EXCEPTION_REGION();
  896. existing = SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value);
  897. ASSIMP_END_EXCEPTION_REGION(bool);
  898. return existing;
  899. }
  900. // ------------------------------------------------------------------------------------------------
  901. // Set a configuration property
  902. bool Importer::SetPropertyMatrix(const char* szName, const aiMatrix4x4& value) {
  903. ai_assert(nullptr != pimpl);
  904. bool existing;
  905. ASSIMP_BEGIN_EXCEPTION_REGION();
  906. existing = SetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties, szName,value);
  907. ASSIMP_END_EXCEPTION_REGION(bool);
  908. return existing;
  909. }
  910. // ------------------------------------------------------------------------------------------------
  911. // Set a configuration property
  912. bool Importer::SetPropertyPointer(const char* szName, void* value) {
  913. ai_assert(nullptr != pimpl);
  914. bool existing;
  915. ASSIMP_BEGIN_EXCEPTION_REGION();
  916. existing = SetGenericProperty<void*>(pimpl->mPointerProperties, szName,value);
  917. ASSIMP_END_EXCEPTION_REGION(bool);
  918. return existing;
  919. }
  920. // ------------------------------------------------------------------------------------------------
  921. // Get a configuration property
  922. int Importer::GetPropertyInteger(const char* szName, int iErrorReturn /*= 0xffffffff*/) const {
  923. ai_assert(nullptr != pimpl);
  924. return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
  925. }
  926. // ------------------------------------------------------------------------------------------------
  927. // Get a configuration property
  928. ai_real Importer::GetPropertyFloat(const char* szName, ai_real iErrorReturn /*= 10e10*/) const {
  929. ai_assert(nullptr != pimpl);
  930. return GetGenericProperty<ai_real>(pimpl->mFloatProperties,szName,iErrorReturn);
  931. }
  932. // ------------------------------------------------------------------------------------------------
  933. // Get a configuration property
  934. std::string Importer::GetPropertyString(const char* szName, const std::string& iErrorReturn /*= ""*/) const {
  935. ai_assert(nullptr != pimpl);
  936. return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
  937. }
  938. // ------------------------------------------------------------------------------------------------
  939. // Get a configuration property
  940. aiMatrix4x4 Importer::GetPropertyMatrix(const char* szName, const aiMatrix4x4& iErrorReturn /*= aiMatrix4x4()*/) const {
  941. ai_assert(nullptr != pimpl);
  942. return GetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties,szName,iErrorReturn);
  943. }
  944. // ------------------------------------------------------------------------------------------------
  945. // Get a configuration property
  946. void* Importer::GetPropertyPointer(const char* szName, void* iErrorReturn /*= nullptr*/) const {
  947. ai_assert(nullptr != pimpl);
  948. return GetGenericProperty<void*>(pimpl->mPointerProperties,szName,iErrorReturn);
  949. }
  950. // ------------------------------------------------------------------------------------------------
  951. // Get the memory requirements of a single node
  952. inline
  953. void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode) {
  954. if ( nullptr == pcNode ) {
  955. return;
  956. }
  957. iScene += sizeof(aiNode);
  958. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  959. iScene += sizeof(void*) * pcNode->mNumChildren;
  960. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  961. AddNodeWeight(iScene,pcNode->mChildren[i]);
  962. }
  963. }
  964. // ------------------------------------------------------------------------------------------------
  965. // Get the memory requirements of the scene
  966. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const {
  967. ai_assert(nullptr != pimpl);
  968. in = aiMemoryInfo();
  969. aiScene* mScene = pimpl->mScene;
  970. // return if we have no scene loaded
  971. if (!mScene)
  972. return;
  973. in.total = sizeof(aiScene);
  974. // add all meshes
  975. for (unsigned int i = 0; i < mScene->mNumMeshes;++i) {
  976. in.meshes += sizeof(aiMesh);
  977. if (mScene->mMeshes[i]->HasPositions()) {
  978. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  979. }
  980. if (mScene->mMeshes[i]->HasNormals()) {
  981. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  982. }
  983. if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
  984. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  985. }
  986. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
  987. if (mScene->mMeshes[i]->HasVertexColors(a)) {
  988. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  989. } else {
  990. break;
  991. }
  992. }
  993. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
  994. if (mScene->mMeshes[i]->HasTextureCoords(a)) {
  995. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  996. } else {
  997. break;
  998. }
  999. }
  1000. if (mScene->mMeshes[i]->HasBones()) {
  1001. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  1002. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
  1003. in.meshes += sizeof(aiBone);
  1004. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  1005. }
  1006. }
  1007. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  1008. }
  1009. in.total += in.meshes;
  1010. // add all embedded textures
  1011. for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
  1012. const aiTexture* pc = mScene->mTextures[i];
  1013. in.textures += sizeof(aiTexture);
  1014. if (pc->mHeight) {
  1015. in.textures += 4 * pc->mHeight * pc->mWidth;
  1016. } else {
  1017. in.textures += pc->mWidth;
  1018. }
  1019. }
  1020. in.total += in.textures;
  1021. // add all animations
  1022. for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
  1023. const aiAnimation* pc = mScene->mAnimations[i];
  1024. in.animations += sizeof(aiAnimation);
  1025. // add all bone anims
  1026. for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
  1027. const aiNodeAnim* pc2 = pc->mChannels[a];
  1028. in.animations += sizeof(aiNodeAnim);
  1029. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  1030. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  1031. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  1032. }
  1033. }
  1034. in.total += in.animations;
  1035. // add all cameras and all lights
  1036. in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
  1037. in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
  1038. // add all nodes
  1039. AddNodeWeight(in.nodes,mScene->mRootNode);
  1040. in.total += in.nodes;
  1041. // add all materials
  1042. for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
  1043. const aiMaterial* pc = mScene->mMaterials[i];
  1044. in.materials += sizeof(aiMaterial);
  1045. in.materials += pc->mNumAllocated * sizeof(void*);
  1046. for (unsigned int a = 0; a < pc->mNumProperties;++a) {
  1047. in.materials += pc->mProperties[a]->mDataLength;
  1048. }
  1049. }
  1050. in.total += in.materials;
  1051. }