Importer.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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,
  392. size_t pLength,
  393. unsigned int pFlags,
  394. const char* pHint /*= ""*/) {
  395. ai_assert(nullptr != pimpl);
  396. ASSIMP_BEGIN_EXCEPTION_REGION();
  397. if (!pHint) {
  398. pHint = "";
  399. }
  400. if (!pBuffer || !pLength || strlen(pHint) > MaxLenHint ) {
  401. pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
  402. return nullptr;
  403. }
  404. // prevent deletion of the previous IOHandler
  405. IOSystem* io = pimpl->mIOHandler;
  406. pimpl->mIOHandler = nullptr;
  407. SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength,io));
  408. // read the file and recover the previous IOSystem
  409. static const size_t BufSize(Importer::MaxLenHint + 28);
  410. char fbuff[BufSize];
  411. ai_snprintf(fbuff, BufSize, "%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
  412. ReadFile(fbuff,pFlags);
  413. SetIOHandler(io);
  414. ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
  415. return pimpl->mScene;
  416. }
  417. // ------------------------------------------------------------------------------------------------
  418. void WriteLogOpening(const std::string& file) {
  419. ASSIMP_LOG_INFO("Load ", file);
  420. // print a full version dump. This is nice because we don't
  421. // need to ask the authors of incoming bug reports for
  422. // the library version they're using - a log dump is
  423. // sufficient.
  424. const unsigned int flags = aiGetCompileFlags();
  425. std::stringstream stream;
  426. stream << "Assimp " << aiGetVersionMajor() << "." << aiGetVersionMinor() << "." << aiGetVersionRevision() << " "
  427. #if defined(ASSIMP_BUILD_ARCHITECTURE)
  428. << ASSIMP_BUILD_ARCHITECTURE
  429. #elif defined(_M_IX86) || defined(__x86_32__) || defined(__i386__)
  430. << "x86"
  431. #elif defined(_M_X64) || defined(__x86_64__)
  432. << "amd64"
  433. #elif defined(_M_IA64) || defined(__ia64__)
  434. << "itanium"
  435. #elif defined(__ppc__) || defined(__powerpc__)
  436. << "ppc32"
  437. #elif defined(__powerpc64__)
  438. << "ppc64"
  439. #elif defined(__arm__)
  440. << "arm"
  441. #else
  442. << "<unknown architecture>"
  443. #endif
  444. << " "
  445. #if defined(ASSIMP_BUILD_COMPILER)
  446. << (ASSIMP_BUILD_COMPILER)
  447. #elif defined(_MSC_VER)
  448. << "msvc"
  449. #elif defined(__GNUC__)
  450. << "gcc"
  451. #elif defined(__clang__)
  452. << "clang"
  453. #elif defined(__EMSCRIPTEN__)
  454. << "emscripten"
  455. #elif defined(__MINGW32__)
  456. << "MinGW-w64 32bit"
  457. #elif defined(__MINGW64__)
  458. << "MinGW-w64 64bit"
  459. #else
  460. << "<unknown compiler>"
  461. #endif
  462. #ifdef ASSIMP_BUILD_DEBUG
  463. << " debug"
  464. #endif
  465. << (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
  466. << (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
  467. << (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
  468. << (flags & ASSIMP_CFLAGS_DOUBLE_SUPPORT ? " double : " : "single : ");
  469. ASSIMP_LOG_DEBUG(stream.str());
  470. }
  471. // ------------------------------------------------------------------------------------------------
  472. // Reads the given file and returns its contents if successful.
  473. const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags) {
  474. ai_assert(nullptr != pimpl);
  475. ASSIMP_BEGIN_EXCEPTION_REGION();
  476. const std::string pFile(_pFile);
  477. // ----------------------------------------------------------------------
  478. // Put a large try block around everything to catch all std::exception's
  479. // that might be thrown by STL containers or by new().
  480. // ImportErrorException's are throw by ourselves and caught elsewhere.
  481. //-----------------------------------------------------------------------
  482. WriteLogOpening(pFile);
  483. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  484. try
  485. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  486. {
  487. // Check whether this Importer instance has already loaded
  488. // a scene. In this case we need to delete the old one
  489. if (pimpl->mScene) {
  490. ASSIMP_LOG_DEBUG("(Deleting previous scene)");
  491. FreeScene();
  492. }
  493. // First check if the file is accessible at all
  494. if( !pimpl->mIOHandler->Exists( pFile)) {
  495. pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
  496. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  497. return nullptr;
  498. }
  499. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  500. if (profiler) {
  501. profiler->BeginRegion("total");
  502. }
  503. // Find an worker class which can handle the file extension.
  504. // Multiple importers may be able to handle the same extension (.xml!); gather them all.
  505. SetPropertyInteger("importerIndex", -1);
  506. struct ImporterAndIndex {
  507. BaseImporter * importer;
  508. unsigned int index;
  509. };
  510. std::vector<ImporterAndIndex> possibleImporters;
  511. for (unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  512. // Every importer has a list of supported extensions.
  513. std::set<std::string> extensions;
  514. pimpl->mImporter[a]->GetExtensionList(extensions);
  515. // CAUTION: Do not just search for the extension!
  516. // GetExtension() returns the part after the *last* dot, but some extensions have dots
  517. // inside them, e.g. ogre.mesh.xml. Compare the entire end of the string.
  518. for (std::set<std::string>::const_iterator it = extensions.cbegin(); it != extensions.cend(); ++it) {
  519. // Yay for C++<20 not having std::string::ends_with()
  520. std::string extension = "." + *it;
  521. if (extension.length() <= pFile.length()) {
  522. // Possible optimization: Fetch the lowercase filename!
  523. if (0 == ASSIMP_stricmp(pFile.c_str() + pFile.length() - extension.length(), extension.c_str())) {
  524. ImporterAndIndex candidate = { pimpl->mImporter[a], a };
  525. possibleImporters.push_back(candidate);
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. // If just one importer supports this extension, pick it and close the case.
  532. BaseImporter* imp = nullptr;
  533. if (1 == possibleImporters.size()) {
  534. imp = possibleImporters[0].importer;
  535. SetPropertyInteger("importerIndex", possibleImporters[0].index);
  536. }
  537. // If multiple importers claim this file extension, ask them to look at the actual file data to decide.
  538. // This can happen e.g. with XML (COLLADA vs. Irrlicht).
  539. else {
  540. for (std::vector<ImporterAndIndex>::const_iterator it = possibleImporters.begin(); it < possibleImporters.end(); ++it) {
  541. BaseImporter & importer = *it->importer;
  542. ASSIMP_LOG_INFO("Found a possible importer: " + std::string(importer.GetInfo()->mName) + "; trying signature-based detection");
  543. if (importer.CanRead( pFile, pimpl->mIOHandler, true)) {
  544. imp = &importer;
  545. SetPropertyInteger("importerIndex", it->index);
  546. break;
  547. }
  548. }
  549. }
  550. if (!imp) {
  551. // not so bad yet ... try format auto detection.
  552. ASSIMP_LOG_INFO("File extension not known, trying signature-based detection");
  553. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  554. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
  555. imp = pimpl->mImporter[a];
  556. SetPropertyInteger("importerIndex", a);
  557. break;
  558. }
  559. }
  560. // Put a proper error message if no suitable importer was found
  561. if( !imp) {
  562. pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  563. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  564. return nullptr;
  565. }
  566. }
  567. // Get file size for progress handler
  568. IOStream * fileIO = pimpl->mIOHandler->Open( pFile );
  569. uint32_t fileSize = 0;
  570. if (fileIO)
  571. {
  572. fileSize = static_cast<uint32_t>(fileIO->FileSize());
  573. pimpl->mIOHandler->Close( fileIO );
  574. }
  575. // Dispatch the reading to the worker class for this format
  576. const aiImporterDesc *desc( imp->GetInfo() );
  577. std::string ext( "unknown" );
  578. if ( nullptr != desc ) {
  579. ext = desc->mName;
  580. }
  581. ASSIMP_LOG_INFO("Found a matching importer for this file format: ", ext, "." );
  582. pimpl->mProgressHandler->UpdateFileRead( 0, fileSize );
  583. if (profiler) {
  584. profiler->BeginRegion("import");
  585. }
  586. pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
  587. pimpl->mProgressHandler->UpdateFileRead( fileSize, fileSize );
  588. if (profiler) {
  589. profiler->EndRegion("import");
  590. }
  591. SetPropertyString("sourceFilePath", pFile);
  592. // If successful, apply all active post processing steps to the imported data
  593. if( pimpl->mScene) {
  594. if (!pimpl->mScene->mMetaData || !pimpl->mScene->mMetaData->HasKey(AI_METADATA_SOURCE_FORMAT)) {
  595. if (!pimpl->mScene->mMetaData) {
  596. pimpl->mScene->mMetaData = new aiMetadata;
  597. }
  598. pimpl->mScene->mMetaData->Add(AI_METADATA_SOURCE_FORMAT, aiString(ext));
  599. }
  600. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  601. // The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
  602. if (pFlags & aiProcess_ValidateDataStructure) {
  603. ValidateDSProcess ds;
  604. ds.ExecuteOnScene (this);
  605. if (!pimpl->mScene) {
  606. return nullptr;
  607. }
  608. }
  609. #endif // no validation
  610. // Preprocess the scene and prepare it for post-processing
  611. if (profiler) {
  612. profiler->BeginRegion("preprocess");
  613. }
  614. ScenePreprocessor pre(pimpl->mScene);
  615. pre.ProcessScene();
  616. if (profiler) {
  617. profiler->EndRegion("preprocess");
  618. }
  619. // Ensure that the validation process won't be called twice
  620. ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
  621. }
  622. // if failed, extract the error string
  623. else if( !pimpl->mScene) {
  624. pimpl->mErrorString = imp->GetErrorText();
  625. pimpl->mException = imp->GetException();
  626. }
  627. // clear any data allocated by post-process steps
  628. pimpl->mPPShared->Clean();
  629. if (profiler) {
  630. profiler->EndRegion("total");
  631. }
  632. }
  633. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  634. catch (std::exception &e) {
  635. #if (defined _MSC_VER) && (defined _CPPRTTI)
  636. // if we have RTTI get the full name of the exception that occurred
  637. pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  638. #else
  639. pimpl->mErrorString = std::string("std::exception: ") + e.what();
  640. #endif
  641. ASSIMP_LOG_ERROR(pimpl->mErrorString);
  642. delete pimpl->mScene; pimpl->mScene = nullptr;
  643. }
  644. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  645. // either successful or failure - the pointer expresses it anyways
  646. ASSIMP_END_EXCEPTION_REGION_WITH_ERROR_STRING(const aiScene*, pimpl->mErrorString, pimpl->mException);
  647. return pimpl->mScene;
  648. }
  649. // ------------------------------------------------------------------------------------------------
  650. // Apply post-processing to the currently bound scene
  651. const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags) {
  652. ai_assert(nullptr != pimpl);
  653. ASSIMP_BEGIN_EXCEPTION_REGION();
  654. // Return immediately if no scene is active
  655. if (!pimpl->mScene) {
  656. return nullptr;
  657. }
  658. // If no flags are given, return the current scene with no further action
  659. if (!pFlags) {
  660. return pimpl->mScene;
  661. }
  662. // In debug builds: run basic flag validation
  663. ai_assert(_ValidateFlags(pFlags));
  664. ASSIMP_LOG_INFO("Entering post processing pipeline");
  665. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  666. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  667. // list of post-processing steps, so we need to call it manually.
  668. if (pFlags & aiProcess_ValidateDataStructure) {
  669. ValidateDSProcess ds;
  670. ds.ExecuteOnScene (this);
  671. if (!pimpl->mScene) {
  672. return nullptr;
  673. }
  674. }
  675. #endif // no validation
  676. #ifdef ASSIMP_BUILD_DEBUG
  677. if (pimpl->bExtraVerbose)
  678. {
  679. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  680. ASSIMP_LOG_ERROR("Verbose Import is not available due to build settings");
  681. #endif // no validation
  682. pFlags |= aiProcess_ValidateDataStructure;
  683. }
  684. #else
  685. if (pimpl->bExtraVerbose) {
  686. ASSIMP_LOG_WARN("Not a debug build, ignoring extra verbose setting");
  687. }
  688. #endif // ! DEBUG
  689. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  690. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  691. BaseProcess* process = pimpl->mPostProcessingSteps[a];
  692. pimpl->mProgressHandler->UpdatePostProcess(static_cast<int>(a), static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  693. if( process->IsActive( pFlags)) {
  694. if (profiler) {
  695. profiler->BeginRegion("postprocess");
  696. }
  697. process->ExecuteOnScene ( this );
  698. if (profiler) {
  699. profiler->EndRegion("postprocess");
  700. }
  701. }
  702. if( !pimpl->mScene) {
  703. break;
  704. }
  705. #ifdef ASSIMP_BUILD_DEBUG
  706. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  707. continue;
  708. #endif // no validation
  709. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  710. if (pimpl->bExtraVerbose) {
  711. ASSIMP_LOG_DEBUG("Verbose Import: re-validating data structures");
  712. ValidateDSProcess ds;
  713. ds.ExecuteOnScene (this);
  714. if( !pimpl->mScene) {
  715. ASSIMP_LOG_ERROR("Verbose Import: failed to re-validate data structures");
  716. break;
  717. }
  718. }
  719. #endif // ! DEBUG
  720. }
  721. pimpl->mProgressHandler->UpdatePostProcess( static_cast<int>(pimpl->mPostProcessingSteps.size()),
  722. static_cast<int>(pimpl->mPostProcessingSteps.size()) );
  723. // update private scene flags
  724. if( pimpl->mScene ) {
  725. ScenePriv(pimpl->mScene)->mPPStepsApplied |= pFlags;
  726. }
  727. // clear any data allocated by post-process steps
  728. pimpl->mPPShared->Clean();
  729. ASSIMP_LOG_INFO("Leaving post processing pipeline");
  730. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  731. return pimpl->mScene;
  732. }
  733. // ------------------------------------------------------------------------------------------------
  734. const aiScene* Importer::ApplyCustomizedPostProcessing( BaseProcess *rootProcess, bool requestValidation ) {
  735. ai_assert(nullptr != pimpl);
  736. ASSIMP_BEGIN_EXCEPTION_REGION();
  737. // Return immediately if no scene is active
  738. if ( nullptr == pimpl->mScene ) {
  739. return nullptr;
  740. }
  741. // If no flags are given, return the current scene with no further action
  742. if (nullptr == rootProcess) {
  743. return pimpl->mScene;
  744. }
  745. // In debug builds: run basic flag validation
  746. ASSIMP_LOG_INFO( "Entering customized post processing pipeline" );
  747. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  748. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  749. // list of post-processing steps, so we need to call it manually.
  750. if ( requestValidation )
  751. {
  752. ValidateDSProcess ds;
  753. ds.ExecuteOnScene( this );
  754. if ( !pimpl->mScene ) {
  755. return nullptr;
  756. }
  757. }
  758. #endif // no validation
  759. #ifdef ASSIMP_BUILD_DEBUG
  760. if ( pimpl->bExtraVerbose )
  761. {
  762. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  763. ASSIMP_LOG_ERROR( "Verbose Import is not available due to build settings" );
  764. #endif // no validation
  765. }
  766. #else
  767. if ( pimpl->bExtraVerbose ) {
  768. ASSIMP_LOG_WARN( "Not a debug build, ignoring extra verbose setting" );
  769. }
  770. #endif // ! DEBUG
  771. std::unique_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME, 0) ? new Profiler() : nullptr);
  772. if ( profiler ) {
  773. profiler->BeginRegion( "postprocess" );
  774. }
  775. rootProcess->ExecuteOnScene( this );
  776. if ( profiler ) {
  777. profiler->EndRegion( "postprocess" );
  778. }
  779. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  780. if ( pimpl->bExtraVerbose || requestValidation ) {
  781. ASSIMP_LOG_DEBUG( "Verbose Import: revalidating data structures" );
  782. ValidateDSProcess ds;
  783. ds.ExecuteOnScene( this );
  784. if ( !pimpl->mScene ) {
  785. ASSIMP_LOG_ERROR( "Verbose Import: failed to revalidate data structures" );
  786. }
  787. }
  788. // clear any data allocated by post-process steps
  789. pimpl->mPPShared->Clean();
  790. ASSIMP_LOG_INFO( "Leaving customized post processing pipeline" );
  791. ASSIMP_END_EXCEPTION_REGION( const aiScene* );
  792. return pimpl->mScene;
  793. }
  794. // ------------------------------------------------------------------------------------------------
  795. // Helper function to check whether an extension is supported by ASSIMP
  796. bool Importer::IsExtensionSupported(const char* szExtension) const {
  797. return nullptr != GetImporter(szExtension);
  798. }
  799. // ------------------------------------------------------------------------------------------------
  800. size_t Importer::GetImporterCount() const {
  801. ai_assert(nullptr != pimpl);
  802. return pimpl->mImporter.size();
  803. }
  804. // ------------------------------------------------------------------------------------------------
  805. const aiImporterDesc* Importer::GetImporterInfo(size_t index) const {
  806. ai_assert(nullptr != pimpl);
  807. if (index >= pimpl->mImporter.size()) {
  808. return nullptr;
  809. }
  810. return pimpl->mImporter[index]->GetInfo();
  811. }
  812. // ------------------------------------------------------------------------------------------------
  813. BaseImporter* Importer::GetImporter (size_t index) const {
  814. ai_assert(nullptr != pimpl);
  815. if (index >= pimpl->mImporter.size()) {
  816. return nullptr;
  817. }
  818. return pimpl->mImporter[index];
  819. }
  820. // ------------------------------------------------------------------------------------------------
  821. // Find a loader plugin for a given file extension
  822. BaseImporter* Importer::GetImporter (const char* szExtension) const {
  823. ai_assert(nullptr != pimpl);
  824. return GetImporter(GetImporterIndex(szExtension));
  825. }
  826. // ------------------------------------------------------------------------------------------------
  827. // Find a loader plugin for a given file extension
  828. size_t Importer::GetImporterIndex (const char* szExtension) const {
  829. ai_assert(nullptr != pimpl);
  830. ai_assert(nullptr != szExtension);
  831. ASSIMP_BEGIN_EXCEPTION_REGION();
  832. // skip over wild-card and dot characters at string head --
  833. for ( ; *szExtension == '*' || *szExtension == '.'; ++szExtension );
  834. std::string ext(szExtension);
  835. if (ext.empty()) {
  836. return static_cast<size_t>(-1);
  837. }
  838. ext = ai_tolower(ext);
  839. std::set<std::string> str;
  840. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  841. str.clear();
  842. (*i)->GetExtensionList(str);
  843. for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
  844. if (ext == *it) {
  845. return std::distance(static_cast< std::vector<BaseImporter*>::const_iterator >(pimpl->mImporter.begin()), i);
  846. }
  847. }
  848. }
  849. ASSIMP_END_EXCEPTION_REGION(size_t);
  850. return static_cast<size_t>(-1);
  851. }
  852. // ------------------------------------------------------------------------------------------------
  853. // Helper function to build a list of all file extensions supported by ASSIMP
  854. void Importer::GetExtensionList(aiString& szOut) const {
  855. ai_assert(nullptr != pimpl);
  856. ASSIMP_BEGIN_EXCEPTION_REGION();
  857. std::set<std::string> str;
  858. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  859. (*i)->GetExtensionList(str);
  860. }
  861. // List can be empty
  862. if( !str.empty() ) {
  863. for (std::set<std::string>::const_iterator it = str.begin();; ) {
  864. szOut.Append("*.");
  865. szOut.Append((*it).c_str());
  866. if (++it == str.end()) {
  867. break;
  868. }
  869. szOut.Append(";");
  870. }
  871. }
  872. ASSIMP_END_EXCEPTION_REGION(void);
  873. }
  874. // ------------------------------------------------------------------------------------------------
  875. // Set a configuration property
  876. bool Importer::SetPropertyInteger(const char* szName, int iValue) {
  877. ai_assert(nullptr != pimpl);
  878. bool existing;
  879. ASSIMP_BEGIN_EXCEPTION_REGION();
  880. existing = SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue);
  881. ASSIMP_END_EXCEPTION_REGION(bool);
  882. return existing;
  883. }
  884. // ------------------------------------------------------------------------------------------------
  885. // Set a configuration property
  886. bool Importer::SetPropertyFloat(const char* szName, ai_real iValue) {
  887. ai_assert(nullptr != pimpl);
  888. bool existing;
  889. ASSIMP_BEGIN_EXCEPTION_REGION();
  890. existing = SetGenericProperty<ai_real>(pimpl->mFloatProperties, szName,iValue);
  891. ASSIMP_END_EXCEPTION_REGION(bool);
  892. return existing;
  893. }
  894. // ------------------------------------------------------------------------------------------------
  895. // Set a configuration property
  896. bool Importer::SetPropertyString(const char* szName, const std::string& value) {
  897. ai_assert(nullptr != pimpl);
  898. bool existing;
  899. ASSIMP_BEGIN_EXCEPTION_REGION();
  900. existing = SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value);
  901. ASSIMP_END_EXCEPTION_REGION(bool);
  902. return existing;
  903. }
  904. // ------------------------------------------------------------------------------------------------
  905. // Set a configuration property
  906. bool Importer::SetPropertyMatrix(const char* szName, const aiMatrix4x4& value) {
  907. ai_assert(nullptr != pimpl);
  908. bool existing;
  909. ASSIMP_BEGIN_EXCEPTION_REGION();
  910. existing = SetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties, szName,value);
  911. ASSIMP_END_EXCEPTION_REGION(bool);
  912. return existing;
  913. }
  914. // ------------------------------------------------------------------------------------------------
  915. // Set a configuration property
  916. bool Importer::SetPropertyPointer(const char* szName, void* value) {
  917. ai_assert(nullptr != pimpl);
  918. bool existing;
  919. ASSIMP_BEGIN_EXCEPTION_REGION();
  920. existing = SetGenericProperty<void*>(pimpl->mPointerProperties, szName,value);
  921. ASSIMP_END_EXCEPTION_REGION(bool);
  922. return existing;
  923. }
  924. // ------------------------------------------------------------------------------------------------
  925. // Get a configuration property
  926. int Importer::GetPropertyInteger(const char* szName, int iErrorReturn /*= 0xffffffff*/) const {
  927. ai_assert(nullptr != pimpl);
  928. return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
  929. }
  930. // ------------------------------------------------------------------------------------------------
  931. // Get a configuration property
  932. ai_real Importer::GetPropertyFloat(const char* szName, ai_real iErrorReturn /*= 10e10*/) const {
  933. ai_assert(nullptr != pimpl);
  934. return GetGenericProperty<ai_real>(pimpl->mFloatProperties,szName,iErrorReturn);
  935. }
  936. // ------------------------------------------------------------------------------------------------
  937. // Get a configuration property
  938. std::string Importer::GetPropertyString(const char* szName, const std::string& iErrorReturn /*= ""*/) const {
  939. ai_assert(nullptr != pimpl);
  940. return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
  941. }
  942. // ------------------------------------------------------------------------------------------------
  943. // Get a configuration property
  944. aiMatrix4x4 Importer::GetPropertyMatrix(const char* szName, const aiMatrix4x4& iErrorReturn /*= aiMatrix4x4()*/) const {
  945. ai_assert(nullptr != pimpl);
  946. return GetGenericProperty<aiMatrix4x4>(pimpl->mMatrixProperties,szName,iErrorReturn);
  947. }
  948. // ------------------------------------------------------------------------------------------------
  949. // Get a configuration property
  950. void* Importer::GetPropertyPointer(const char* szName, void* iErrorReturn /*= nullptr*/) const {
  951. ai_assert(nullptr != pimpl);
  952. return GetGenericProperty<void*>(pimpl->mPointerProperties,szName,iErrorReturn);
  953. }
  954. // ------------------------------------------------------------------------------------------------
  955. // Get the memory requirements of a single node
  956. inline
  957. void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode) {
  958. if ( nullptr == pcNode ) {
  959. return;
  960. }
  961. iScene += sizeof(aiNode);
  962. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  963. iScene += sizeof(void*) * pcNode->mNumChildren;
  964. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  965. AddNodeWeight(iScene,pcNode->mChildren[i]);
  966. }
  967. }
  968. // ------------------------------------------------------------------------------------------------
  969. // Get the memory requirements of the scene
  970. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const {
  971. ai_assert(nullptr != pimpl);
  972. in = aiMemoryInfo();
  973. aiScene* mScene = pimpl->mScene;
  974. // return if we have no scene loaded
  975. if (!mScene)
  976. return;
  977. in.total = sizeof(aiScene);
  978. // add all meshes
  979. for (unsigned int i = 0; i < mScene->mNumMeshes;++i) {
  980. in.meshes += sizeof(aiMesh);
  981. if (mScene->mMeshes[i]->HasPositions()) {
  982. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  983. }
  984. if (mScene->mMeshes[i]->HasNormals()) {
  985. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  986. }
  987. if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
  988. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  989. }
  990. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
  991. if (mScene->mMeshes[i]->HasVertexColors(a)) {
  992. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  993. } else {
  994. break;
  995. }
  996. }
  997. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
  998. if (mScene->mMeshes[i]->HasTextureCoords(a)) {
  999. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1000. } else {
  1001. break;
  1002. }
  1003. }
  1004. if (mScene->mMeshes[i]->HasBones()) {
  1005. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  1006. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
  1007. in.meshes += sizeof(aiBone);
  1008. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  1009. }
  1010. }
  1011. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  1012. }
  1013. in.total += in.meshes;
  1014. // add all embedded textures
  1015. for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
  1016. const aiTexture* pc = mScene->mTextures[i];
  1017. in.textures += sizeof(aiTexture);
  1018. if (pc->mHeight) {
  1019. in.textures += 4 * pc->mHeight * pc->mWidth;
  1020. } else {
  1021. in.textures += pc->mWidth;
  1022. }
  1023. }
  1024. in.total += in.textures;
  1025. // add all animations
  1026. for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
  1027. const aiAnimation* pc = mScene->mAnimations[i];
  1028. in.animations += sizeof(aiAnimation);
  1029. // add all bone anims
  1030. for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
  1031. const aiNodeAnim* pc2 = pc->mChannels[a];
  1032. in.animations += sizeof(aiNodeAnim);
  1033. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  1034. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  1035. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  1036. }
  1037. }
  1038. in.total += in.animations;
  1039. // add all cameras and all lights
  1040. in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
  1041. in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
  1042. // add all nodes
  1043. AddNodeWeight(in.nodes,mScene->mRootNode);
  1044. in.total += in.nodes;
  1045. // add all materials
  1046. for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
  1047. const aiMaterial* pc = mScene->mMaterials[i];
  1048. in.materials += sizeof(aiMaterial);
  1049. in.materials += pc->mNumAllocated * sizeof(void*);
  1050. for (unsigned int a = 0; a < pc->mNumProperties;++a) {
  1051. in.materials += pc->mProperties[a]->mDataLength;
  1052. }
  1053. }
  1054. in.total += in.materials;
  1055. }