Importer.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the CPP-API class #Importer */
  35. #include "AssimpPCH.h"
  36. // internal headers
  37. #include "BaseImporter.h"
  38. #include "BaseProcess.h"
  39. #include "DefaultIOStream.h"
  40. #include "DefaultIOSystem.h"
  41. #include "GenericProperty.h"
  42. #include "ProcessHelper.h"
  43. // Importers
  44. #ifndef AI_BUILD_NO_X_IMPORTER
  45. # include "XFileImporter.h"
  46. #endif
  47. #ifndef AI_BUILD_NO_3DS_IMPORTER
  48. # include "3DSLoader.h"
  49. #endif
  50. #ifndef AI_BUILD_NO_MD3_IMPORTER
  51. # include "MD3Loader.h"
  52. #endif
  53. #ifndef AI_BUILD_NO_MDL_IMPORTER
  54. # include "MDLLoader.h"
  55. #endif
  56. #ifndef AI_BUILD_NO_MD2_IMPORTER
  57. # include "MD2Loader.h"
  58. #endif
  59. #ifndef AI_BUILD_NO_PLY_IMPORTER
  60. # include "PlyLoader.h"
  61. #endif
  62. #ifndef AI_BUILD_NO_ASE_IMPORTER
  63. # include "ASELoader.h"
  64. #endif
  65. #ifndef AI_BUILD_NO_OBJ_IMPORTER
  66. # include "ObjFileImporter.h"
  67. #endif
  68. #ifndef AI_BUILD_NO_HMP_IMPORTER
  69. # include "HMPLoader.h"
  70. #endif
  71. #ifndef AI_BUILD_NO_SMD_IMPORTER
  72. # include "SMDLoader.h"
  73. #endif
  74. #ifndef AI_BUILD_NO_MDR_IMPORTER
  75. # include "MDRLoader.h"
  76. #endif
  77. #ifndef AI_BUILD_NO_MDC_IMPORTER
  78. # include "MDCLoader.h"
  79. #endif
  80. #ifndef AI_BUILD_NO_MD5_IMPORTER
  81. # include "MD5Loader.h"
  82. #endif
  83. #ifndef AI_BUILD_NO_STL_IMPORTER
  84. # include "STLLoader.h"
  85. #endif
  86. #ifndef AI_BUILD_NO_LWO_IMPORTER
  87. # include "LWOLoader.h"
  88. #endif
  89. #ifndef AI_BUILD_NO_DXF_IMPORTER
  90. # include "DXFLoader.h"
  91. #endif
  92. #ifndef AI_BUILD_NO_NFF_IMPORTER
  93. # include "NFFLoader.h"
  94. #endif
  95. #ifndef AI_BUILD_NO_RAW_IMPORTER
  96. # include "RawLoader.h"
  97. #endif
  98. #ifndef AI_BUILD_NO_OFF_IMPORTER
  99. # include "OFFLoader.h"
  100. #endif
  101. #ifndef AI_BUILD_NO_AC_IMPORTER
  102. # include "ACLoader.h"
  103. #endif
  104. #ifndef AI_BUILD_NO_BVH_IMPORTER
  105. # include "BVHLoader.h"
  106. #endif
  107. #ifndef AI_BUILD_NO_IRRMESH_IMPORTER
  108. # include "IRRMeshLoader.h"
  109. #endif
  110. #ifndef AI_BUILD_NO_IRR_IMPORTER
  111. # include "IRRLoader.h"
  112. #endif
  113. // PostProcess-Steps
  114. #ifndef AI_BUILD_NO_CALCTANGENTS_PROCESS
  115. # include "CalcTangentsProcess.h"
  116. #endif
  117. #ifndef AI_BUILD_NO_JOINVERTICES_PROCESS
  118. # include "JoinVerticesProcess.h"
  119. #endif
  120. #ifndef AI_BUILD_NO_CONVERTTOLH_PROCESS
  121. # include "ConvertToLHProcess.h"
  122. #endif
  123. #ifndef AI_BUILD_NO_TRIANGULATE_PROCESS
  124. # include "TriangulateProcess.h"
  125. #endif
  126. #ifndef AI_BUILD_NO_GENFACENORMALS_PROCESS
  127. # include "GenFaceNormalsProcess.h"
  128. #endif
  129. #ifndef AI_BUILD_NO_GENVERTEXNORMALS_PROCESS
  130. # include "GenVertexNormalsProcess.h"
  131. #endif
  132. #ifndef AI_BUILD_NO_REMOVEVC_PROCESS
  133. # include "RemoveVCProcess.h"
  134. #endif
  135. #ifndef AI_BUILD_NO_SPLITLARGEMESHES_PROCESS
  136. # include "SplitLargeMeshes.h"
  137. #endif
  138. #ifndef AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS
  139. # include "PretransformVertices.h"
  140. #endif
  141. #ifndef AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS
  142. # include "LimitBoneWeightsProcess.h"
  143. #endif
  144. #ifndef AI_BUILD_NO_VALIDATEDS_PROCESS
  145. # include "ValidateDataStructure.h"
  146. #endif
  147. #ifndef AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS
  148. # include "ImproveCacheLocality.h"
  149. #endif
  150. #ifndef AI_BUILD_NO_FIXINFACINGNORMALS_PROCESS
  151. # include "FixNormalsStep.h"
  152. #endif
  153. #ifndef AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS
  154. # include "RemoveRedundantMaterials.h"
  155. #endif
  156. #ifndef AI_BUILD_NO_OPTIMIZEGRAPH_PROCESS
  157. # include "OptimizeGraphProcess.h"
  158. #endif
  159. #ifndef AI_BUILD_NO_FINDINVALIDDATA_PROCESS
  160. # include "FindInvalidDataProcess.h"
  161. #endif
  162. #ifndef AI_BUILD_NO_FINDDEGENERATES_PROCESS
  163. # include "FindDegenerates.h"
  164. #endif
  165. // NOTE: the preprocessor code has been moved to the header as
  166. // we've also declared the DeterminePType process in it, which
  167. // can't be removed.
  168. //#ifndef AI_BUILD_NO_SORTBYPTYPE_PROCESS
  169. # include "SortByPTypeProcess.h"
  170. //#endif
  171. using namespace Assimp;
  172. // ------------------------------------------------------------------------------------------------
  173. // Constructor.
  174. Importer::Importer() :
  175. mIOHandler(NULL),
  176. mScene(NULL),
  177. mErrorString("")
  178. {
  179. // allocate a default IO handler
  180. mIOHandler = new DefaultIOSystem;
  181. mIsDefaultHandler = true;
  182. bExtraVerbose = false; // disable extra verbose mode by default
  183. // add an instance of each worker class here
  184. // the order doesn't really care, however file formats that are
  185. // used more frequently than others should be at the beginning.
  186. mImporter.reserve(25);
  187. #if (!defined AI_BUILD_NO_X_IMPORTER)
  188. mImporter.push_back( new XFileImporter());
  189. #endif
  190. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  191. mImporter.push_back( new ObjFileImporter());
  192. #endif
  193. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  194. mImporter.push_back( new Dot3DSImporter());
  195. #endif
  196. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  197. mImporter.push_back( new MD3Importer());
  198. #endif
  199. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  200. mImporter.push_back( new MD2Importer());
  201. #endif
  202. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  203. mImporter.push_back( new PLYImporter());
  204. #endif
  205. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  206. mImporter.push_back( new MDLImporter());
  207. #endif
  208. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  209. mImporter.push_back( new ASEImporter());
  210. #endif
  211. #if (!defined AI_BUILD_NO_HMP_IMPORTER)
  212. mImporter.push_back( new HMPImporter());
  213. #endif
  214. #if (!defined AI_BUILD_NO_SMD_IMPORTER)
  215. mImporter.push_back( new SMDImporter());
  216. #endif
  217. #if (!defined AI_BUILD_NO_MDR_IMPORTER)
  218. mImporter.push_back( new MDRImporter());
  219. #endif
  220. #if (!defined AI_BUILD_NO_MDC_IMPORTER)
  221. mImporter.push_back( new MDCImporter());
  222. #endif
  223. #if (!defined AI_BUILD_NO_MD5_IMPORTER)
  224. mImporter.push_back( new MD5Importer());
  225. #endif
  226. #if (!defined AI_BUILD_NO_STL_IMPORTER)
  227. mImporter.push_back( new STLImporter());
  228. #endif
  229. #if (!defined AI_BUILD_NO_LWO_IMPORTER)
  230. mImporter.push_back( new LWOImporter());
  231. #endif
  232. #if (!defined AI_BUILD_NO_DXF_IMPORTER)
  233. mImporter.push_back( new DXFImporter());
  234. #endif
  235. #if (!defined AI_BUILD_NO_NFF_IMPORTER)
  236. mImporter.push_back( new NFFImporter());
  237. #endif
  238. #if (!defined AI_BUILD_NO_RAW_IMPORTER)
  239. mImporter.push_back( new RAWImporter());
  240. #endif
  241. #if (!defined AI_BUILD_NO_OFF_IMPORTER)
  242. mImporter.push_back( new OFFImporter());
  243. #endif
  244. #if (!defined AI_BUILD_NO_AC_IMPORTER)
  245. mImporter.push_back( new AC3DImporter());
  246. #endif
  247. #if (!defined AI_BUILD_NO_BVH_IMPORTER)
  248. mImporter.push_back( new BVHLoader());
  249. #endif
  250. #if (!defined AI_BUILD_NO_IRRMESH_IMPORTER)
  251. mImporter.push_back( new IRRMeshImporter());
  252. #endif
  253. #if (!defined AI_BUILD_NO_IRR_IMPORTER)
  254. mImporter.push_back( new IRRImporter());
  255. #endif
  256. // add an instance of each post processing step here in the order
  257. // of sequence it is executed. steps that are added here are not validated -
  258. // as RegisterPPStep() does - all dependencies must be there.
  259. mPostProcessingSteps.reserve(25);
  260. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  261. mPostProcessingSteps.push_back( new ValidateDSProcess());
  262. #endif
  263. mPostProcessingSteps.push_back( new DeterminePTypeHelperProcess());
  264. #if (!defined AI_BUILD_NO_FINDDEGENERATES_PROCESS)
  265. mPostProcessingSteps.push_back( new FindDegeneratesProcess());
  266. #endif
  267. #if (!defined AI_BUILD_NO_REMOVEVC_PROCESS)
  268. mPostProcessingSteps.push_back( new RemoveVCProcess());
  269. #endif
  270. #if (!defined AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  271. mPostProcessingSteps.push_back( new RemoveRedundantMatsProcess());
  272. #endif
  273. #if (!defined AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  274. mPostProcessingSteps.push_back( new PretransformVertices());
  275. #endif
  276. #if (!defined AI_BUILD_NO_TRIANGULATE_PROCESS)
  277. mPostProcessingSteps.push_back( new TriangulateProcess());
  278. #endif
  279. #if (!defined AI_BUILD_NO_SORTBYPTYPE_PROCESS)
  280. mPostProcessingSteps.push_back( new SortByPTypeProcess());
  281. #endif
  282. #if (!defined AI_BUILD_NO_FINDINVALIDDATA_PROCESS)
  283. mPostProcessingSteps.push_back( new FindInvalidDataProcess());
  284. #endif
  285. #if (!defined AI_BUILD_NO_OPTIMIZEGRAPH_PROCESS)
  286. mPostProcessingSteps.push_back( new OptimizeGraphProcess());
  287. #endif
  288. #if (!defined AI_BUILD_NO_FIXINFACINGNORMALS_PROCESS)
  289. mPostProcessingSteps.push_back( new FixInfacingNormalsProcess());
  290. #endif
  291. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  292. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  293. #endif
  294. #if (!defined AI_BUILD_NO_GENFACENORMALS_PROCESS)
  295. mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  296. #endif
  297. // DON'T change the order of these five!
  298. mPostProcessingSteps.push_back( new ComputeSpatialSortProcess());
  299. #if (!defined AI_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  300. mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  301. #endif
  302. #if (!defined AI_BUILD_NO_CALCTANGENTS_PROCESS)
  303. mPostProcessingSteps.push_back( new CalcTangentsProcess());
  304. #endif
  305. #if (!defined AI_BUILD_NO_JOINVERTICES_PROCESS)
  306. mPostProcessingSteps.push_back( new JoinVerticesProcess());
  307. #endif
  308. mPostProcessingSteps.push_back( new DestroySpatialSortProcess());
  309. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  310. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  311. #endif
  312. #if (!defined AI_BUILD_NO_CONVERTTOLH_PROCESS)
  313. mPostProcessingSteps.push_back( new ConvertToLHProcess());
  314. #endif
  315. #if (!defined AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  316. mPostProcessingSteps.push_back( new LimitBoneWeightsProcess());
  317. #endif
  318. #if (!defined AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  319. mPostProcessingSteps.push_back( new ImproveCacheLocalityProcess());
  320. #endif
  321. // allocate a SharedPostProcessInfo object and store pointers to it
  322. // in all post-process steps in the list.
  323. mPPShared = new SharedPostProcessInfo();
  324. for (std::vector<BaseProcess*>::iterator it = mPostProcessingSteps.begin(),
  325. end = mPostProcessingSteps.end(); it != end; ++it)
  326. {
  327. (*it)->SetSharedData(mPPShared);
  328. }
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. // Destructor.
  332. Importer::~Importer()
  333. {
  334. for( unsigned int a = 0; a < mImporter.size(); a++)
  335. delete mImporter[a];
  336. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  337. delete mPostProcessingSteps[a];
  338. // delete the assigned IO handler
  339. delete mIOHandler;
  340. // kill imported scene. Destructors should do that recursivly
  341. delete mScene;
  342. // delete shared post-processing data
  343. delete mPPShared;
  344. }
  345. // ------------------------------------------------------------------------------------------------
  346. // Empty and private copy constructor
  347. Importer::Importer(const Importer &other)
  348. {
  349. // empty
  350. }
  351. // ------------------------------------------------------------------------------------------------
  352. aiReturn Importer::RegisterLoader(BaseImporter* pImp)
  353. {
  354. ai_assert(NULL != pImp);
  355. // check whether we would have two loaders for the same file extension now
  356. std::string st;
  357. pImp->GetExtensionList(st);
  358. #ifdef _DEBUG
  359. const char* sz = ::strtok(const_cast<char*>(st.c_str()),";");
  360. while (sz)
  361. {
  362. if (IsExtensionSupported(std::string(sz)))
  363. {
  364. DefaultLogger::get()->error(std::string( "The file extension " ) + sz + " is already in use");
  365. return AI_FAILURE;
  366. }
  367. sz = ::strtok(NULL,";");
  368. }
  369. #endif
  370. // add the loader
  371. this->mImporter.push_back(pImp);
  372. DefaultLogger::get()->info("Registering custom importer: " + st);
  373. return AI_SUCCESS;
  374. }
  375. // ------------------------------------------------------------------------------------------------
  376. aiReturn Importer::UnregisterLoader(BaseImporter* pImp)
  377. {
  378. ai_assert(NULL != pImp);
  379. for (std::vector<BaseImporter*>::iterator
  380. it = mImporter.begin(),end = mImporter.end();
  381. it != end;++it)
  382. {
  383. if (pImp == (*it))
  384. {
  385. mImporter.erase(it);
  386. std::string st;
  387. pImp->GetExtensionList(st);
  388. DefaultLogger::get()->info("Unregistering custom importer: " + st);
  389. return AI_SUCCESS;
  390. }
  391. }
  392. DefaultLogger::get()->warn("Unable to remove importer: importer not found");
  393. return AI_FAILURE;
  394. }
  395. // ------------------------------------------------------------------------------------------------
  396. // Supplies a custom IO handler to the importer to open and access files.
  397. void Importer::SetIOHandler( IOSystem* pIOHandler)
  398. {
  399. if (!pIOHandler)
  400. {
  401. delete mIOHandler;
  402. mIOHandler = new DefaultIOSystem();
  403. mIsDefaultHandler = true;
  404. }
  405. else if (mIOHandler != pIOHandler)
  406. {
  407. delete mIOHandler;
  408. mIOHandler = pIOHandler;
  409. mIsDefaultHandler = false;
  410. }
  411. return;
  412. }
  413. // ------------------------------------------------------------------------------------------------
  414. IOSystem* Importer::GetIOHandler()
  415. {
  416. return mIOHandler;
  417. }
  418. // ------------------------------------------------------------------------------------------------
  419. bool Importer::IsDefaultIOHandler()
  420. {
  421. return mIsDefaultHandler;
  422. }
  423. #ifdef _DEBUG
  424. // ------------------------------------------------------------------------------------------------
  425. // Validate post process step flags
  426. bool ValidateFlags(unsigned int pFlags)
  427. {
  428. if (pFlags & aiProcess_GenSmoothNormals &&
  429. pFlags & aiProcess_GenNormals)
  430. {
  431. DefaultLogger::get()->error("aiProcess_GenSmoothNormals and "
  432. "aiProcess_GenNormals may not be specified together");
  433. return false;
  434. }
  435. if (pFlags & aiProcess_PreTransformVertices &&
  436. pFlags & aiProcess_OptimizeGraph)
  437. {
  438. DefaultLogger::get()->error("aiProcess_PreTransformVertives and "
  439. "aiProcess_OptimizeGraph may not be specified together");
  440. return false;
  441. }
  442. return true;
  443. }
  444. #endif // ! DEBUG
  445. // ------------------------------------------------------------------------------------------------
  446. // Reads the given file and returns its contents if successful.
  447. const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
  448. {
  449. // validate the flags
  450. ai_assert(ValidateFlags(pFlags));
  451. // put a large try block around everything to catch all std::exception's
  452. // that might be thrown by STL containers or by new().
  453. // ImportErrorException's are throw by ourselves and caught elsewhere.
  454. try
  455. {
  456. // check whether this Importer instance has already loaded
  457. // a scene. In this case we need to delete the old one
  458. if (this->mScene)
  459. {
  460. DefaultLogger::get()->debug("The previous scene has been deleted");
  461. delete mScene;
  462. this->mScene = NULL;
  463. }
  464. // first check if the file is accessable at all
  465. if( !mIOHandler->Exists( pFile))
  466. {
  467. mErrorString = "Unable to open file \"" + pFile + "\".";
  468. DefaultLogger::get()->error(mErrorString);
  469. return NULL;
  470. }
  471. // find an worker class which can handle the file
  472. BaseImporter* imp = NULL;
  473. for( unsigned int a = 0; a < mImporter.size(); a++)
  474. {
  475. if( mImporter[a]->CanRead( pFile, mIOHandler))
  476. {
  477. imp = mImporter[a];
  478. break;
  479. }
  480. }
  481. // put a proper error message if no suitable importer was found
  482. if( !imp)
  483. {
  484. mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  485. DefaultLogger::get()->error(mErrorString);
  486. return NULL;
  487. }
  488. // dispatch the reading to the worker class for this format
  489. DefaultLogger::get()->info("Found a matching importer for this file format");
  490. imp->SetupProperties( this );
  491. mScene = imp->ReadFile( pFile, mIOHandler);
  492. // if successful, apply all active post processing steps to the imported data
  493. if( mScene)
  494. {
  495. DefaultLogger::get()->info("Import successful, entering postprocessing-steps");
  496. #ifdef _DEBUG
  497. if (bExtraVerbose)
  498. {
  499. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  500. DefaultLogger::get()->error("Extra verbose mode not available, library"
  501. " wasn't build with the ValidateDS-Step");
  502. #endif
  503. pFlags |= aiProcess_ValidateDataStructure;
  504. }
  505. #else
  506. if (bExtraVerbose)DefaultLogger::get()->warn("Not a debug build, ignoring extra verbose setting");
  507. #endif // ! DEBUG
  508. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  509. {
  510. BaseProcess* process = mPostProcessingSteps[a];
  511. if( process->IsActive( pFlags))
  512. {
  513. process->SetupProperties( this );
  514. process->ExecuteOnScene ( this );
  515. }
  516. if( !mScene)break;
  517. #ifdef _DEBUG
  518. #ifndef AI_BUILD_NO_VALIDATEDS_PROCESS
  519. continue;
  520. #endif
  521. // if the extra verbose mode is active execute the
  522. // VaidateDataStructureStep again after each step
  523. if (bExtraVerbose && a)
  524. {
  525. DefaultLogger::get()->debug("Extra verbose: revalidating data structures");
  526. ((ValidateDSProcess*)mPostProcessingSteps[0])->ExecuteOnScene (this);
  527. if( !mScene)
  528. {
  529. DefaultLogger::get()->error("Extra verbose: failed to revalidate data structures");
  530. break;
  531. }
  532. }
  533. #endif // ! DEBUG
  534. }
  535. }
  536. // if failed, extract the error string
  537. else if( !mScene)mErrorString = imp->GetErrorText();
  538. // clear any data allocated by post-process steps
  539. mPPShared->Clean();
  540. }
  541. catch (std::exception &e)
  542. {
  543. #if (defined _MSC_VER) && (defined _CPPRTTI) && (defined _DEBUG)
  544. // if we have RTTI get the full name of the exception that occured
  545. mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  546. #else
  547. mErrorString = std::string("std::exception: ") + e.what();
  548. #endif
  549. DefaultLogger::get()->error(mErrorString);
  550. delete mScene;mScene = NULL;
  551. }
  552. // either successful or failure - the pointer expresses it anyways
  553. return mScene;
  554. }
  555. // ------------------------------------------------------------------------------------------------
  556. // Helper function to check whether an extension is supported by ASSIMP
  557. bool Importer::IsExtensionSupported(const std::string& szExtension)
  558. {
  559. return NULL != FindLoader(szExtension);
  560. }
  561. // ------------------------------------------------------------------------------------------------
  562. BaseImporter* Importer::FindLoader (const std::string& szExtension)
  563. {
  564. for (std::vector<BaseImporter*>::const_iterator
  565. i = this->mImporter.begin();
  566. i != this->mImporter.end();++i)
  567. {
  568. // pass the file extension to the CanRead(..,NULL)-method
  569. if ((*i)->CanRead(szExtension,NULL))return *i;
  570. }
  571. return NULL;
  572. }
  573. // ------------------------------------------------------------------------------------------------
  574. // Helper function to build a list of all file extensions supported by ASSIMP
  575. void Importer::GetExtensionList(std::string& szOut)
  576. {
  577. unsigned int iNum = 0;
  578. for (std::vector<BaseImporter*>::const_iterator
  579. i = this->mImporter.begin();
  580. i != this->mImporter.end();++i,++iNum)
  581. {
  582. // insert a comma as delimiter character
  583. if (0 != iNum)
  584. szOut.append(";");
  585. (*i)->GetExtensionList(szOut);
  586. }
  587. return;
  588. }
  589. // ------------------------------------------------------------------------------------------------
  590. // Set a configuration property
  591. void Importer::SetPropertyInteger(const char* szName, int iValue,
  592. bool* bWasExisting /*= NULL*/)
  593. {
  594. SetGenericProperty<int>(mIntProperties, szName,iValue,bWasExisting);
  595. }
  596. // ------------------------------------------------------------------------------------------------
  597. void Importer::SetPropertyFloat(const char* szName, float iValue,
  598. bool* bWasExisting /*= NULL*/)
  599. {
  600. SetGenericProperty<float>(mFloatProperties, szName,iValue,bWasExisting);
  601. }
  602. // ------------------------------------------------------------------------------------------------
  603. void Importer::SetPropertyString(const char* szName, const std::string& value,
  604. bool* bWasExisting /*= NULL*/)
  605. {
  606. SetGenericProperty<std::string>(mStringProperties, szName,value,bWasExisting);
  607. }
  608. // ------------------------------------------------------------------------------------------------
  609. // Get a configuration property
  610. int Importer::GetPropertyInteger(const char* szName,
  611. int iErrorReturn /*= 0xffffffff*/) const
  612. {
  613. return GetGenericProperty<int>(mIntProperties,szName,iErrorReturn);
  614. }
  615. // ------------------------------------------------------------------------------------------------
  616. float Importer::GetPropertyFloat(const char* szName,
  617. float iErrorReturn /*= 10e10*/) const
  618. {
  619. return GetGenericProperty<float>(mFloatProperties,szName,iErrorReturn);
  620. }
  621. // ------------------------------------------------------------------------------------------------
  622. std::string Importer::GetPropertyString(const char* szName,
  623. const std::string& iErrorReturn /*= ""*/) const
  624. {
  625. return GetGenericProperty<std::string>(mStringProperties,szName,iErrorReturn);
  626. }
  627. // ------------------------------------------------------------------------------------------------
  628. void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode)
  629. {
  630. iScene += sizeof(aiNode);
  631. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  632. iScene += sizeof(void*) * pcNode->mNumChildren;
  633. for (unsigned int i = 0; i < pcNode->mNumChildren;++i)
  634. AddNodeWeight(iScene,pcNode->mChildren[i]);
  635. }
  636. // ------------------------------------------------------------------------------------------------
  637. // Get the memory requirements of the scene
  638. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const
  639. {
  640. in = aiMemoryInfo();
  641. if (!this->mScene)return;
  642. in.total = sizeof(aiScene);
  643. // add all meshes
  644. for (unsigned int i = 0; i < mScene->mNumMeshes;++i)
  645. {
  646. in.meshes += sizeof(aiMesh);
  647. if (mScene->mMeshes[i]->HasPositions())
  648. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  649. if (mScene->mMeshes[i]->HasNormals())
  650. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  651. if (mScene->mMeshes[i]->HasTangentsAndBitangents())
  652. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  653. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a)
  654. {
  655. if (mScene->mMeshes[i]->HasVertexColors(a))
  656. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  657. else break;
  658. }
  659. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a)
  660. {
  661. if (mScene->mMeshes[i]->HasTextureCoords(a))
  662. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  663. else break;
  664. }
  665. if (mScene->mMeshes[i]->HasBones())
  666. {
  667. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  668. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p)
  669. {
  670. in.meshes += sizeof(aiBone);
  671. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  672. }
  673. }
  674. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  675. }
  676. in.total += in.meshes;
  677. // add all embedded textures
  678. for (unsigned int i = 0; i < mScene->mNumTextures;++i)
  679. {
  680. const aiTexture* pc = mScene->mTextures[i];
  681. in.textures += sizeof(aiTexture);
  682. if (pc->mHeight)
  683. {
  684. in.textures += 4 * pc->mHeight * pc->mWidth;
  685. }
  686. else in.textures += pc->mWidth;
  687. }
  688. in.total += in.textures;
  689. // add all animations
  690. for (unsigned int i = 0; i < mScene->mNumAnimations;++i)
  691. {
  692. const aiAnimation* pc = mScene->mAnimations[i];
  693. in.animations += sizeof(aiAnimation);
  694. // add all bone anims
  695. for (unsigned int a = 0; a < pc->mNumChannels; ++a)
  696. {
  697. const aiNodeAnim* pc2 = pc->mChannels[i];
  698. in.animations += sizeof(aiNodeAnim);
  699. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  700. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  701. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  702. }
  703. }
  704. in.total += in.animations;
  705. // add all nodes
  706. AddNodeWeight(in.nodes,mScene->mRootNode);
  707. in.total += in.nodes;
  708. // add all materials
  709. for (unsigned int i = 0; i < mScene->mNumMaterials;++i)
  710. {
  711. const aiMaterial* pc = mScene->mMaterials[i];
  712. in.materials += sizeof(aiMaterial);
  713. in.materials += pc->mNumAllocated * sizeof(void*);
  714. for (unsigned int a = 0; a < pc->mNumProperties;++a)
  715. {
  716. in.materials += pc->mProperties[a]->mDataLength;
  717. }
  718. }
  719. in.total += in.materials;
  720. return;
  721. }