Importer.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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 Importer.cpp
  35. * @brief Implementation of the CPP-API class #Importer
  36. */
  37. #include "AssimpPCH.h"
  38. #include "../include/aiVersion.h"
  39. // ------------------------------------------------------------------------------------------------
  40. /* Uncomment this line to prevent Assimp from catching unknown exceptions.
  41. *
  42. * Note that any Exception except DeadlyImportError may lead to
  43. * undefined behaviour -> loaders could remain in an unusable state and
  44. * further imports with the same Importer instance could fail/crash/burn ...
  45. */
  46. // ------------------------------------------------------------------------------------------------
  47. #ifndef ASSIMP_BUILD_DEBUG
  48. # define ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  49. #endif
  50. // ------------------------------------------------------------------------------------------------
  51. // Internal headers
  52. // ------------------------------------------------------------------------------------------------
  53. #include "BaseImporter.h"
  54. #include "BaseProcess.h"
  55. #include "DefaultIOStream.h"
  56. #include "DefaultIOSystem.h"
  57. #include "DefaultProgressHandler.h"
  58. #include "GenericProperty.h"
  59. #include "ProcessHelper.h"
  60. #include "ScenePreprocessor.h"
  61. #include "MemoryIOWrapper.h"
  62. #include "Profiler.h"
  63. #include "TinyFormatter.h"
  64. using namespace Assimp::Profiling;
  65. using namespace Assimp::Formatter;
  66. // ------------------------------------------------------------------------------------------------
  67. // Importers
  68. // (include_new_importers_here)
  69. // ------------------------------------------------------------------------------------------------
  70. #ifndef ASSIMP_BUILD_NO_X_IMPORTER
  71. # include "XFileImporter.h"
  72. #endif
  73. #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER
  74. # include "3DSLoader.h"
  75. #endif
  76. #ifndef ASSIMP_BUILD_NO_MD3_IMPORTER
  77. # include "MD3Loader.h"
  78. #endif
  79. #ifndef ASSIMP_BUILD_NO_MDL_IMPORTER
  80. # include "MDLLoader.h"
  81. #endif
  82. #ifndef ASSIMP_BUILD_NO_MD2_IMPORTER
  83. # include "MD2Loader.h"
  84. #endif
  85. #ifndef ASSIMP_BUILD_NO_PLY_IMPORTER
  86. # include "PlyLoader.h"
  87. #endif
  88. #ifndef ASSIMP_BUILD_NO_ASE_IMPORTER
  89. # include "ASELoader.h"
  90. #endif
  91. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  92. # include "ObjFileImporter.h"
  93. #endif
  94. #ifndef ASSIMP_BUILD_NO_HMP_IMPORTER
  95. # include "HMPLoader.h"
  96. #endif
  97. #ifndef ASSIMP_BUILD_NO_SMD_IMPORTER
  98. # include "SMDLoader.h"
  99. #endif
  100. #ifndef ASSIMP_BUILD_NO_MDC_IMPORTER
  101. # include "MDCLoader.h"
  102. #endif
  103. #ifndef ASSIMP_BUILD_NO_MD5_IMPORTER
  104. # include "MD5Loader.h"
  105. #endif
  106. #ifndef ASSIMP_BUILD_NO_STL_IMPORTER
  107. # include "STLLoader.h"
  108. #endif
  109. #ifndef ASSIMP_BUILD_NO_LWO_IMPORTER
  110. # include "LWOLoader.h"
  111. #endif
  112. #ifndef ASSIMP_BUILD_NO_DXF_IMPORTER
  113. # include "DXFLoader.h"
  114. #endif
  115. #ifndef ASSIMP_BUILD_NO_NFF_IMPORTER
  116. # include "NFFLoader.h"
  117. #endif
  118. #ifndef ASSIMP_BUILD_NO_RAW_IMPORTER
  119. # include "RawLoader.h"
  120. #endif
  121. #ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
  122. # include "OFFLoader.h"
  123. #endif
  124. #ifndef ASSIMP_BUILD_NO_AC_IMPORTER
  125. # include "ACLoader.h"
  126. #endif
  127. #ifndef ASSIMP_BUILD_NO_BVH_IMPORTER
  128. # include "BVHLoader.h"
  129. #endif
  130. #ifndef ASSIMP_BUILD_NO_IRRMESH_IMPORTER
  131. # include "IRRMeshLoader.h"
  132. #endif
  133. #ifndef ASSIMP_BUILD_NO_IRR_IMPORTER
  134. # include "IRRLoader.h"
  135. #endif
  136. #ifndef ASSIMP_BUILD_NO_Q3D_IMPORTER
  137. # include "Q3DLoader.h"
  138. #endif
  139. #ifndef ASSIMP_BUILD_NO_B3D_IMPORTER
  140. # include "B3DImporter.h"
  141. #endif
  142. #ifndef ASSIMP_BUILD_NO_COLLADA_IMPORTER
  143. # include "ColladaLoader.h"
  144. #endif
  145. #ifndef ASSIMP_BUILD_NO_TERRAGEN_IMPORTER
  146. # include "TerragenLoader.h"
  147. #endif
  148. #ifndef ASSIMP_BUILD_NO_CSM_IMPORTER
  149. # include "CSMLoader.h"
  150. #endif
  151. #ifndef ASSIMP_BUILD_NO_3D_IMPORTER
  152. # include "UnrealLoader.h"
  153. #endif
  154. #ifndef ASSIMP_BUILD_NO_LWS_IMPORTER
  155. # include "LWSLoader.h"
  156. #endif
  157. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  158. # include "OgreImporter.h"
  159. #endif
  160. #ifndef ASSIMP_BUILD_NO_MS3D_IMPORTER
  161. # include "MS3DLoader.h"
  162. #endif
  163. #ifndef ASSIMP_BUILD_NO_COB_IMPORTER
  164. # include "COBLoader.h"
  165. #endif
  166. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  167. # include "BlenderLoader.h"
  168. #endif
  169. #ifndef ASSIMP_BUILD_NO_Q3BSP_IMPORTER
  170. # include "Q3BSPFileImporter.h"
  171. #endif
  172. #ifndef ASSIMP_BUILD_NO_NDO_IMPORTER
  173. # include "NDOLoader.h"
  174. #endif
  175. #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
  176. # include "IFCLoader.h"
  177. #endif
  178. // ------------------------------------------------------------------------------------------------
  179. // Post processing-Steps
  180. // ------------------------------------------------------------------------------------------------
  181. #ifndef ASSIMP_BUILD_NO_CALCTANGENTS_PROCESS
  182. # include "CalcTangentsProcess.h"
  183. #endif
  184. #ifndef ASSIMP_BUILD_NO_JOINVERTICES_PROCESS
  185. # include "JoinVerticesProcess.h"
  186. #endif
  187. #if !(defined ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS && defined ASSIMP_BUILD_NO_FLIPUVS_PROCESS && defined ASSIMP_BUILD_NO_FLIPWINDINGORDER_PROCESS)
  188. # include "ConvertToLHProcess.h"
  189. #endif
  190. #ifndef ASSIMP_BUILD_NO_TRIANGULATE_PROCESS
  191. # include "TriangulateProcess.h"
  192. #endif
  193. #ifndef ASSIMP_BUILD_NO_GENFACENORMALS_PROCESS
  194. # include "GenFaceNormalsProcess.h"
  195. #endif
  196. #ifndef ASSIMP_BUILD_NO_GENVERTEXNORMALS_PROCESS
  197. # include "GenVertexNormalsProcess.h"
  198. #endif
  199. #ifndef ASSIMP_BUILD_NO_REMOVEVC_PROCESS
  200. # include "RemoveVCProcess.h"
  201. #endif
  202. #ifndef ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS
  203. # include "SplitLargeMeshes.h"
  204. #endif
  205. #ifndef ASSIMP_BUILD_NO_PRETRANSFORMVERTICES_PROCESS
  206. # include "PretransformVertices.h"
  207. #endif
  208. #ifndef ASSIMP_BUILD_NO_LIMITBONEWEIGHTS_PROCESS
  209. # include "LimitBoneWeightsProcess.h"
  210. #endif
  211. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  212. # include "ValidateDataStructure.h"
  213. #endif
  214. #ifndef ASSIMP_BUILD_NO_IMPROVECACHELOCALITY_PROCESS
  215. # include "ImproveCacheLocality.h"
  216. #endif
  217. #ifndef ASSIMP_BUILD_NO_FIXINFACINGNORMALS_PROCESS
  218. # include "FixNormalsStep.h"
  219. #endif
  220. #ifndef ASSIMP_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS
  221. # include "RemoveRedundantMaterials.h"
  222. #endif
  223. #ifndef ASSIMP_BUILD_NO_FINDINVALIDDATA_PROCESS
  224. # include "FindInvalidDataProcess.h"
  225. #endif
  226. #ifndef ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS
  227. # include "FindDegenerates.h"
  228. #endif
  229. #ifndef ASSIMP_BUILD_NO_SORTBYPTYPE_PROCESS
  230. # include "SortByPTypeProcess.h"
  231. #endif
  232. #ifndef ASSIMP_BUILD_NO_GENUVCOORDS_PROCESS
  233. # include "ComputeUVMappingProcess.h"
  234. #endif
  235. #ifndef ASSIMP_BUILD_NO_TRANSFORMTEXCOORDS_PROCESS
  236. # include "TextureTransform.h"
  237. #endif
  238. #ifndef ASSIMP_BUILD_NO_FINDINSTANCES_PROCESS
  239. # include "FindInstancesProcess.h"
  240. #endif
  241. #ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
  242. # include "OptimizeMeshes.h"
  243. #endif
  244. #ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS
  245. # include "OptimizeGraph.h"
  246. #endif
  247. #ifndef ASSIMP_BUILD_NO_SPLITBYBONECOUNT_PROCESS
  248. # include "SplitByBoneCountProcess.h"
  249. #endif
  250. #ifndef ASSIMP_BUILD_NO_DEBONE_PROCESS
  251. # include "DeboneProcess.h"
  252. #endif
  253. using namespace Assimp;
  254. using namespace Assimp::Intern;
  255. // ------------------------------------------------------------------------------------------------
  256. // Intern::AllocateFromAssimpHeap serves as abstract base class. It overrides
  257. // new and delete (and their array counterparts) of public API classes (e.g. Logger) to
  258. // utilize our DLL heap.
  259. // See http://www.gotw.ca/publications/mill15.htm
  260. // ------------------------------------------------------------------------------------------------
  261. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes) {
  262. return ::operator new(num_bytes);
  263. }
  264. void* AllocateFromAssimpHeap::operator new ( size_t num_bytes, const std::nothrow_t& ) throw() {
  265. try {
  266. return AllocateFromAssimpHeap::operator new( num_bytes );
  267. }
  268. catch( ... ) {
  269. return NULL;
  270. }
  271. }
  272. void AllocateFromAssimpHeap::operator delete ( void* data) {
  273. return ::operator delete(data);
  274. }
  275. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes) {
  276. return ::operator new[](num_bytes);
  277. }
  278. void* AllocateFromAssimpHeap::operator new[] ( size_t num_bytes, const std::nothrow_t& ) throw() {
  279. try {
  280. return AllocateFromAssimpHeap::operator new[]( num_bytes );
  281. }
  282. catch( ... ) {
  283. return NULL;
  284. }
  285. }
  286. void AllocateFromAssimpHeap::operator delete[] ( void* data) {
  287. return ::operator delete[](data);
  288. }
  289. // ------------------------------------------------------------------------------------------------
  290. // Importer constructor.
  291. Importer::Importer()
  292. {
  293. // allocate the pimpl first
  294. pimpl = new ImporterPimpl();
  295. pimpl->mScene = NULL;
  296. pimpl->mErrorString = "";
  297. // Allocate a default IO handler
  298. pimpl->mIOHandler = new DefaultIOSystem;
  299. pimpl->mIsDefaultHandler = true;
  300. pimpl->bExtraVerbose = false; // disable extra verbose mode by default
  301. pimpl->mProgressHandler = new DefaultProgressHandler();
  302. pimpl->mIsDefaultProgressHandler = true;
  303. // ----------------------------------------------------------------------------
  304. // Add an instance of each worker class here
  305. // (register_new_importers_here)
  306. // ----------------------------------------------------------------------------
  307. pimpl->mImporter.reserve(64);
  308. #if (!defined ASSIMP_BUILD_NO_X_IMPORTER)
  309. pimpl->mImporter.push_back( new XFileImporter());
  310. #endif
  311. #if (!defined ASSIMP_BUILD_NO_OBJ_IMPORTER)
  312. pimpl->mImporter.push_back( new ObjFileImporter());
  313. #endif
  314. #if (!defined ASSIMP_BUILD_NO_3DS_IMPORTER)
  315. pimpl->mImporter.push_back( new Discreet3DSImporter());
  316. #endif
  317. #if (!defined ASSIMP_BUILD_NO_MD3_IMPORTER)
  318. pimpl->mImporter.push_back( new MD3Importer());
  319. #endif
  320. #if (!defined ASSIMP_BUILD_NO_MD2_IMPORTER)
  321. pimpl->mImporter.push_back( new MD2Importer());
  322. #endif
  323. #if (!defined ASSIMP_BUILD_NO_PLY_IMPORTER)
  324. pimpl->mImporter.push_back( new PLYImporter());
  325. #endif
  326. #if (!defined ASSIMP_BUILD_NO_MDL_IMPORTER)
  327. pimpl->mImporter.push_back( new MDLImporter());
  328. #endif
  329. #if (!defined ASSIMP_BUILD_NO_ASE_IMPORTER)
  330. pimpl->mImporter.push_back( new ASEImporter());
  331. #endif
  332. #if (!defined ASSIMP_BUILD_NO_HMP_IMPORTER)
  333. pimpl->mImporter.push_back( new HMPImporter());
  334. #endif
  335. #if (!defined ASSIMP_BUILD_NO_SMD_IMPORTER)
  336. pimpl->mImporter.push_back( new SMDImporter());
  337. #endif
  338. #if (!defined ASSIMP_BUILD_NO_MDC_IMPORTER)
  339. pimpl->mImporter.push_back( new MDCImporter());
  340. #endif
  341. #if (!defined ASSIMP_BUILD_NO_MD5_IMPORTER)
  342. pimpl->mImporter.push_back( new MD5Importer());
  343. #endif
  344. #if (!defined ASSIMP_BUILD_NO_STL_IMPORTER)
  345. pimpl->mImporter.push_back( new STLImporter());
  346. #endif
  347. #if (!defined ASSIMP_BUILD_NO_LWO_IMPORTER)
  348. pimpl->mImporter.push_back( new LWOImporter());
  349. #endif
  350. #if (!defined ASSIMP_BUILD_NO_DXF_IMPORTER)
  351. pimpl->mImporter.push_back( new DXFImporter());
  352. #endif
  353. #if (!defined ASSIMP_BUILD_NO_NFF_IMPORTER)
  354. pimpl->mImporter.push_back( new NFFImporter());
  355. #endif
  356. #if (!defined ASSIMP_BUILD_NO_RAW_IMPORTER)
  357. pimpl->mImporter.push_back( new RAWImporter());
  358. #endif
  359. #if (!defined ASSIMP_BUILD_NO_OFF_IMPORTER)
  360. pimpl->mImporter.push_back( new OFFImporter());
  361. #endif
  362. #if (!defined ASSIMP_BUILD_NO_AC_IMPORTER)
  363. pimpl->mImporter.push_back( new AC3DImporter());
  364. #endif
  365. #if (!defined ASSIMP_BUILD_NO_BVH_IMPORTER)
  366. pimpl->mImporter.push_back( new BVHLoader());
  367. #endif
  368. #if (!defined ASSIMP_BUILD_NO_IRRMESH_IMPORTER)
  369. pimpl->mImporter.push_back( new IRRMeshImporter());
  370. #endif
  371. #if (!defined ASSIMP_BUILD_NO_IRR_IMPORTER)
  372. pimpl->mImporter.push_back( new IRRImporter());
  373. #endif
  374. #if (!defined ASSIMP_BUILD_NO_Q3D_IMPORTER)
  375. pimpl->mImporter.push_back( new Q3DImporter());
  376. #endif
  377. #if (!defined ASSIMP_BUILD_NO_B3D_IMPORTER)
  378. pimpl->mImporter.push_back( new B3DImporter());
  379. #endif
  380. #if (!defined ASSIMP_BUILD_NO_COLLADA_IMPORTER)
  381. pimpl->mImporter.push_back( new ColladaLoader());
  382. #endif
  383. #if (!defined ASSIMP_BUILD_NO_TERRAGEN_IMPORTER)
  384. pimpl->mImporter.push_back( new TerragenImporter());
  385. #endif
  386. #if (!defined ASSIMP_BUILD_NO_CSM_IMPORTER)
  387. pimpl->mImporter.push_back( new CSMImporter());
  388. #endif
  389. #if (!defined ASSIMP_BUILD_NO_3D_IMPORTER)
  390. pimpl->mImporter.push_back( new UnrealImporter());
  391. #endif
  392. #if (!defined ASSIMP_BUILD_NO_LWS_IMPORTER)
  393. pimpl->mImporter.push_back( new LWSImporter());
  394. #endif
  395. #if (!defined ASSIMP_BUILD_NO_OGRE_IMPORTER)
  396. pimpl->mImporter.push_back( new Ogre::OgreImporter());
  397. #endif
  398. #if (!defined ASSIMP_BUILD_NO_MS3D_IMPORTER)
  399. pimpl->mImporter.push_back( new MS3DImporter());
  400. #endif
  401. #if (!defined ASSIMP_BUILD_NO_COB_IMPORTER)
  402. pimpl->mImporter.push_back( new COBImporter());
  403. #endif
  404. #if (!defined ASSIMP_BUILD_NO_BLEND_IMPORTER)
  405. pimpl->mImporter.push_back( new BlenderImporter());
  406. #endif
  407. #if (!defined ASSIMP_BUILD_NO_Q3BSP_IMPORTER)
  408. pimpl->mImporter.push_back( new Q3BSPFileImporter() );
  409. #endif
  410. #if (!defined ASSIMP_BUILD_NO_NDO_IMPORTER)
  411. pimpl->mImporter.push_back( new NDOImporter() );
  412. #endif
  413. #if (!defined ASSIMP_BUILD_NO_IFC_IMPORTER)
  414. pimpl->mImporter.push_back( new IFCImporter() );
  415. #endif
  416. // ----------------------------------------------------------------------------
  417. // Add an instance of each post processing step here in the order
  418. // of sequence it is executed. Steps that are added here are not
  419. // validated - as RegisterPPStep() does - all dependencies must be given.
  420. // ----------------------------------------------------------------------------
  421. pimpl->mPostProcessingSteps.reserve(25);
  422. #if (!defined ASSIMP_BUILD_NO_REMOVEVC_PROCESS)
  423. pimpl->mPostProcessingSteps.push_back( new RemoveVCProcess());
  424. #endif
  425. #if (!defined ASSIMP_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  426. pimpl->mPostProcessingSteps.push_back( new RemoveRedundantMatsProcess());
  427. #endif
  428. #if (!defined ASSIMP_BUILD_NO_FINDINSTANCES_PROCESS)
  429. pimpl->mPostProcessingSteps.push_back( new FindInstancesProcess());
  430. #endif
  431. #if (!defined ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS)
  432. pimpl->mPostProcessingSteps.push_back( new OptimizeGraphProcess());
  433. #endif
  434. #if (!defined ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS)
  435. pimpl->mPostProcessingSteps.push_back( new OptimizeMeshesProcess());
  436. #endif
  437. #if (!defined ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS)
  438. pimpl->mPostProcessingSteps.push_back( new FindDegeneratesProcess());
  439. #endif
  440. #ifndef ASSIMP_BUILD_NO_GENUVCOORDS_PROCESS
  441. pimpl->mPostProcessingSteps.push_back( new ComputeUVMappingProcess());
  442. #endif
  443. #ifndef ASSIMP_BUILD_NO_TRANSFORMTEXCOORDS_PROCESS
  444. pimpl->mPostProcessingSteps.push_back( new TextureTransformStep());
  445. #endif
  446. #if (!defined ASSIMP_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  447. pimpl->mPostProcessingSteps.push_back( new PretransformVertices());
  448. #endif
  449. #if (!defined ASSIMP_BUILD_NO_TRIANGULATE_PROCESS)
  450. pimpl->mPostProcessingSteps.push_back( new TriangulateProcess());
  451. #endif
  452. #if (!defined ASSIMP_BUILD_NO_SORTBYPTYPE_PROCESS)
  453. pimpl->mPostProcessingSteps.push_back( new SortByPTypeProcess());
  454. #endif
  455. #if (!defined ASSIMP_BUILD_NO_FINDINVALIDDATA_PROCESS)
  456. pimpl->mPostProcessingSteps.push_back( new FindInvalidDataProcess());
  457. #endif
  458. #if (!defined ASSIMP_BUILD_NO_FIXINFACINGNORMALS_PROCESS)
  459. pimpl->mPostProcessingSteps.push_back( new FixInfacingNormalsProcess());
  460. #endif
  461. #if (!defined ASSIMP_BUILD_NO_SPLITBYBONECOUNT_PROCESS)
  462. pimpl->mPostProcessingSteps.push_back( new SplitByBoneCountProcess());
  463. #endif
  464. #if (!defined ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  465. pimpl->mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  466. #endif
  467. #if (!defined ASSIMP_BUILD_NO_GENFACENORMALS_PROCESS)
  468. pimpl->mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  469. #endif
  470. // .........................................................................
  471. // DON'T change the order of these five!
  472. pimpl->mPostProcessingSteps.push_back( new ComputeSpatialSortProcess());
  473. // .........................................................................
  474. #if (!defined ASSIMP_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  475. pimpl->mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  476. #endif
  477. #if (!defined ASSIMP_BUILD_NO_CALCTANGENTS_PROCESS)
  478. pimpl->mPostProcessingSteps.push_back( new CalcTangentsProcess());
  479. #endif
  480. #if (!defined ASSIMP_BUILD_NO_JOINVERTICES_PROCESS)
  481. pimpl->mPostProcessingSteps.push_back( new JoinVerticesProcess());
  482. #endif
  483. // .........................................................................
  484. pimpl->mPostProcessingSteps.push_back( new DestroySpatialSortProcess());
  485. // .........................................................................
  486. #if (!defined ASSIMP_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  487. pimpl->mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  488. #endif
  489. #if (!defined ASSIMP_BUILD_NO_MAKELEFTHANDED_PROCESS)
  490. pimpl->mPostProcessingSteps.push_back( new MakeLeftHandedProcess());
  491. #endif
  492. #if (!defined ASSIMP_BUILD_NO_FLIPUVS_PROCESS)
  493. pimpl->mPostProcessingSteps.push_back( new FlipUVsProcess());
  494. #endif
  495. #if (!defined ASSIMP_BUILD_NO_FLIPWINDINGORDER_PROCESS)
  496. pimpl->mPostProcessingSteps.push_back( new FlipWindingOrderProcess());
  497. #endif
  498. #if (!defined ASSIMP_BUILD_DEBONE_PROCESS)
  499. pimpl->mPostProcessingSteps.push_back( new DeboneProcess());
  500. #endif
  501. #if (!defined ASSIMP_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  502. pimpl->mPostProcessingSteps.push_back( new LimitBoneWeightsProcess());
  503. #endif
  504. #if (!defined ASSIMP_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  505. pimpl->mPostProcessingSteps.push_back( new ImproveCacheLocalityProcess());
  506. #endif
  507. // Allocate a SharedPostProcessInfo object and store pointers to it in all post-process steps in the list.
  508. pimpl->mPPShared = new SharedPostProcessInfo();
  509. for (std::vector<BaseProcess*>::iterator it = pimpl->mPostProcessingSteps.begin();
  510. it != pimpl->mPostProcessingSteps.end();
  511. ++it) {
  512. (*it)->SetSharedData(pimpl->mPPShared);
  513. }
  514. }
  515. // ------------------------------------------------------------------------------------------------
  516. // Destructor of Importer
  517. Importer::~Importer()
  518. {
  519. // Delete all import plugins
  520. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++)
  521. delete pimpl->mImporter[a];
  522. // Delete all post-processing plug-ins
  523. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++)
  524. delete pimpl->mPostProcessingSteps[a];
  525. // Delete the assigned IO and progress handler
  526. delete pimpl->mIOHandler;
  527. delete pimpl->mProgressHandler;
  528. // Kill imported scene. Destructors should do that recursivly
  529. delete pimpl->mScene;
  530. // Delete shared post-processing data
  531. delete pimpl->mPPShared;
  532. // and finally the pimpl itself
  533. delete pimpl;
  534. }
  535. // ------------------------------------------------------------------------------------------------
  536. // Copy constructor - copies the config of another Importer, not the scene
  537. Importer::Importer(const Importer &other)
  538. {
  539. new(this) Importer();
  540. pimpl->mIntProperties = other.pimpl->mIntProperties;
  541. pimpl->mFloatProperties = other.pimpl->mFloatProperties;
  542. pimpl->mStringProperties = other.pimpl->mStringProperties;
  543. }
  544. // ------------------------------------------------------------------------------------------------
  545. // Register a custom post-processing step
  546. aiReturn Importer::RegisterPPStep(BaseProcess* pImp)
  547. {
  548. ai_assert(NULL != pImp);
  549. ASSIMP_BEGIN_EXCEPTION_REGION();
  550. pimpl->mPostProcessingSteps.push_back(pImp);
  551. DefaultLogger::get()->info("Registering custom post-processing step");
  552. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  553. return AI_SUCCESS;
  554. }
  555. // ------------------------------------------------------------------------------------------------
  556. // Register a custom loader plugin
  557. aiReturn Importer::RegisterLoader(BaseImporter* pImp)
  558. {
  559. ai_assert(NULL != pImp);
  560. ASSIMP_BEGIN_EXCEPTION_REGION();
  561. // --------------------------------------------------------------------
  562. // Check whether we would have two loaders for the same file extension
  563. // This is absolutely OK, but we should warn the developer of the new
  564. // loader that his code will probably never be called if the first
  565. // loader is a bit too lazy in his file checking.
  566. // --------------------------------------------------------------------
  567. std::set<std::string> st;
  568. std::string baked;
  569. pImp->GetExtensionList(st);
  570. for(std::set<std::string>::const_iterator it = st.begin(); it != st.end(); ++it) {
  571. #ifdef _DEBUG
  572. if (IsExtensionSupported(*it)) {
  573. DefaultLogger::get()->warn("The file extension " + *it + " is already in use");
  574. }
  575. #endif
  576. baked += *it;
  577. }
  578. // add the loader
  579. pimpl->mImporter.push_back(pImp);
  580. DefaultLogger::get()->info("Registering custom importer for these file extensions: " + baked);
  581. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  582. return AI_SUCCESS;
  583. }
  584. // ------------------------------------------------------------------------------------------------
  585. // Unregister a custom loader plugin
  586. aiReturn Importer::UnregisterLoader(BaseImporter* pImp)
  587. {
  588. if(!pImp) {
  589. // unregistering a NULL importer is no problem for us ... really!
  590. return AI_SUCCESS;
  591. }
  592. ASSIMP_BEGIN_EXCEPTION_REGION();
  593. std::vector<BaseImporter*>::iterator it = std::find(pimpl->mImporter.begin(),
  594. pimpl->mImporter.end(),pImp);
  595. if (it != pimpl->mImporter.end()) {
  596. pimpl->mImporter.erase(it);
  597. std::set<std::string> st;
  598. pImp->GetExtensionList(st);
  599. DefaultLogger::get()->info("Unregistering custom importer: ");
  600. return AI_SUCCESS;
  601. }
  602. DefaultLogger::get()->warn("Unable to remove custom importer: I can't find you ...");
  603. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  604. return AI_FAILURE;
  605. }
  606. // ------------------------------------------------------------------------------------------------
  607. // Unregister a custom loader plugin
  608. aiReturn Importer::UnregisterPPStep(BaseProcess* pImp)
  609. {
  610. if(!pImp) {
  611. // unregistering a NULL ppstep is no problem for us ... really!
  612. return AI_SUCCESS;
  613. }
  614. ASSIMP_BEGIN_EXCEPTION_REGION();
  615. std::vector<BaseProcess*>::iterator it = std::find(pimpl->mPostProcessingSteps.begin(),
  616. pimpl->mPostProcessingSteps.end(),pImp);
  617. if (it != pimpl->mPostProcessingSteps.end()) {
  618. pimpl->mPostProcessingSteps.erase(it);
  619. DefaultLogger::get()->info("Unregistering custom post-processing step");
  620. return AI_SUCCESS;
  621. }
  622. DefaultLogger::get()->warn("Unable to remove custom post-processing step: I can't find you ..");
  623. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  624. return AI_FAILURE;
  625. }
  626. // ------------------------------------------------------------------------------------------------
  627. // Supplies a custom IO handler to the importer to open and access files.
  628. void Importer::SetIOHandler( IOSystem* pIOHandler)
  629. {
  630. ASSIMP_BEGIN_EXCEPTION_REGION();
  631. // If the new handler is zero, allocate a default IO implementation.
  632. if (!pIOHandler)
  633. {
  634. // Release pointer in the possession of the caller
  635. pimpl->mIOHandler = new DefaultIOSystem();
  636. pimpl->mIsDefaultHandler = true;
  637. }
  638. // Otherwise register the custom handler
  639. else if (pimpl->mIOHandler != pIOHandler)
  640. {
  641. delete pimpl->mIOHandler;
  642. pimpl->mIOHandler = pIOHandler;
  643. pimpl->mIsDefaultHandler = false;
  644. }
  645. ASSIMP_END_EXCEPTION_REGION(void);
  646. }
  647. // ------------------------------------------------------------------------------------------------
  648. // Get the currently set IO handler
  649. IOSystem* Importer::GetIOHandler() const
  650. {
  651. return pimpl->mIOHandler;
  652. }
  653. // ------------------------------------------------------------------------------------------------
  654. // Check whether a custom IO handler is currently set
  655. bool Importer::IsDefaultIOHandler() const
  656. {
  657. return pimpl->mIsDefaultHandler;
  658. }
  659. // ------------------------------------------------------------------------------------------------
  660. // Supplies a custom progress handler to get regular callbacks during importing
  661. void Importer::SetProgressHandler ( ProgressHandler* pHandler )
  662. {
  663. ASSIMP_BEGIN_EXCEPTION_REGION();
  664. // If the new handler is zero, allocate a default implementation.
  665. if (!pHandler)
  666. {
  667. // Release pointer in the possession of the caller
  668. pimpl->mProgressHandler = new DefaultProgressHandler();
  669. pimpl->mIsDefaultProgressHandler = true;
  670. }
  671. // Otherwise register the custom handler
  672. else if (pimpl->mProgressHandler != pHandler)
  673. {
  674. delete pimpl->mProgressHandler;
  675. pimpl->mProgressHandler = pHandler;
  676. pimpl->mIsDefaultProgressHandler = false;
  677. }
  678. ASSIMP_END_EXCEPTION_REGION(void);
  679. }
  680. // ------------------------------------------------------------------------------------------------
  681. // Get the currently set progress handler
  682. ProgressHandler* Importer::GetProgressHandler() const
  683. {
  684. return pimpl->mProgressHandler;
  685. }
  686. // ------------------------------------------------------------------------------------------------
  687. // Check whether a custom progress handler is currently set
  688. bool Importer::IsDefaultProgressHandler() const
  689. {
  690. return pimpl->mIsDefaultProgressHandler;
  691. }
  692. // ------------------------------------------------------------------------------------------------
  693. // Validate post process step flags
  694. bool _ValidateFlags(unsigned int pFlags)
  695. {
  696. if (pFlags & aiProcess_GenSmoothNormals && pFlags & aiProcess_GenNormals) {
  697. DefaultLogger::get()->error("#aiProcess_GenSmoothNormals and #aiProcess_GenNormals are incompatible");
  698. return false;
  699. }
  700. if (pFlags & aiProcess_OptimizeGraph && pFlags & aiProcess_PreTransformVertices) {
  701. DefaultLogger::get()->error("#aiProcess_OptimizeGraph and #aiProcess_PreTransformVertices are incompatible");
  702. return false;
  703. }
  704. return true;
  705. }
  706. // ------------------------------------------------------------------------------------------------
  707. // Free the current scene
  708. void Importer::FreeScene( )
  709. {
  710. ASSIMP_BEGIN_EXCEPTION_REGION();
  711. delete pimpl->mScene;
  712. pimpl->mScene = NULL;
  713. pimpl->mErrorString = "";
  714. ASSIMP_END_EXCEPTION_REGION(void);
  715. }
  716. // ------------------------------------------------------------------------------------------------
  717. // Get the current error string, if any
  718. const char* Importer::GetErrorString() const
  719. {
  720. /* Must remain valid as long as ReadFile() or FreeFile() are not called */
  721. return pimpl->mErrorString.c_str();
  722. }
  723. // ------------------------------------------------------------------------------------------------
  724. // Enable extra-verbose mode
  725. void Importer::SetExtraVerbose(bool bDo)
  726. {
  727. pimpl->bExtraVerbose = bDo;
  728. }
  729. // ------------------------------------------------------------------------------------------------
  730. // Get the current scene
  731. const aiScene* Importer::GetScene() const
  732. {
  733. return pimpl->mScene;
  734. }
  735. // ------------------------------------------------------------------------------------------------
  736. // Orphan the current scene and return it.
  737. aiScene* Importer::GetOrphanedScene()
  738. {
  739. aiScene* s = pimpl->mScene;
  740. ASSIMP_BEGIN_EXCEPTION_REGION();
  741. pimpl->mScene = NULL;
  742. pimpl->mErrorString = ""; /* reset error string */
  743. ASSIMP_END_EXCEPTION_REGION(aiScene*);
  744. return s;
  745. }
  746. // ------------------------------------------------------------------------------------------------
  747. // Validate post-processing flags
  748. bool Importer::ValidateFlags(unsigned int pFlags) const
  749. {
  750. ASSIMP_BEGIN_EXCEPTION_REGION();
  751. // run basic checks for mutually exclusive flags
  752. if(!_ValidateFlags(pFlags)) {
  753. return false;
  754. }
  755. // ValidateDS does not anymore occur in the pp list, it plays an awesome extra role ...
  756. #ifdef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  757. if (pFlags & aiProcess_ValidateDataStructure) {
  758. return false;
  759. }
  760. #endif
  761. pFlags &= ~aiProcess_ValidateDataStructure;
  762. // Now iterate through all bits which are set in the flags and check whether we find at least
  763. // one pp plugin which handles it.
  764. for (unsigned int mask = 1; mask < (1u << (sizeof(unsigned int)*8-1));mask <<= 1) {
  765. if (pFlags & mask) {
  766. bool have = false;
  767. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  768. if (pimpl->mPostProcessingSteps[a]-> IsActive(mask) ) {
  769. have = true;
  770. break;
  771. }
  772. }
  773. if (!have) {
  774. return false;
  775. }
  776. }
  777. }
  778. ASSIMP_END_EXCEPTION_REGION(bool);
  779. return true;
  780. }
  781. // ------------------------------------------------------------------------------------------------
  782. const aiScene* Importer::ReadFileFromMemory( const void* pBuffer,
  783. size_t pLength,
  784. unsigned int pFlags,
  785. const char* pHint /*= ""*/)
  786. {
  787. ASSIMP_BEGIN_EXCEPTION_REGION();
  788. if (!pHint) {
  789. pHint = "";
  790. }
  791. if (!pBuffer || !pLength || strlen(pHint) > 100) {
  792. pimpl->mErrorString = "Invalid parameters passed to ReadFileFromMemory()";
  793. return NULL;
  794. }
  795. // prevent deletion of the previous IOHandler
  796. IOSystem* io = pimpl->mIOHandler;
  797. pimpl->mIOHandler = NULL;
  798. SetIOHandler(new MemoryIOSystem((const uint8_t*)pBuffer,pLength));
  799. // read the file and recover the previous IOSystem
  800. char fbuff[128];
  801. sprintf(fbuff,"%s.%s",AI_MEMORYIO_MAGIC_FILENAME,pHint);
  802. ReadFile(fbuff,pFlags);
  803. SetIOHandler(io);
  804. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  805. return pimpl->mScene;
  806. }
  807. // ------------------------------------------------------------------------------------------------
  808. void WriteLogOpening(const std::string& file)
  809. {
  810. Logger* l = DefaultLogger::get();
  811. if (!l) {
  812. return;
  813. }
  814. l->info("Load " + file);
  815. // print a full version dump. This is nice because we don't
  816. // need to ask the authors of incoming bug reports for
  817. // the library version they're using - a log dump is
  818. // sufficient.
  819. const unsigned int flags = aiGetCompileFlags();
  820. l->debug(format()
  821. << "Assimp "
  822. << aiGetVersionMajor()
  823. << "."
  824. << aiGetVersionMinor()
  825. << "."
  826. << aiGetVersionRevision()
  827. #if defined(ASSIMP_BUILD_X86_32BIT_ARCHITECTURE)
  828. << " x86"
  829. #elif defined(ASSIMP_BUILD_X86_64BIT_ARCHITECTURE)
  830. << " amd64"
  831. #elif defined(ASSIMP_BUILD_IA_64BIT_ARCHITECTURE)
  832. << " itanium"
  833. #elif defined(ASSIMP_BUILD_PPC_32BIT_ARCHITECTURE)
  834. << " ppc32"
  835. #elif defined(ASSIMP_BUILD_ARM_32BIT_ARCHITECTURE)
  836. << " arm"
  837. #else
  838. # error unknown architecture
  839. #endif
  840. #if defined(_MSC_VER)
  841. << " msvc"
  842. #elif defined(__GNUC__)
  843. << " gcc"
  844. #else
  845. # error unknown compiler
  846. #endif
  847. #ifndef NDEBUG
  848. << " debug"
  849. #endif
  850. << (flags & ASSIMP_CFLAGS_NOBOOST ? " noboost" : "")
  851. << (flags & ASSIMP_CFLAGS_SHARED ? " shared" : "")
  852. << (flags & ASSIMP_CFLAGS_SINGLETHREADED ? " singlethreaded" : "")
  853. );
  854. }
  855. // ------------------------------------------------------------------------------------------------
  856. // Reads the given file and returns its contents if successful.
  857. const aiScene* Importer::ReadFile( const char* _pFile, unsigned int pFlags)
  858. {
  859. ASSIMP_BEGIN_EXCEPTION_REGION();
  860. const std::string pFile(_pFile);
  861. // ----------------------------------------------------------------------
  862. // Put a large try block around everything to catch all std::exception's
  863. // that might be thrown by STL containers or by new().
  864. // ImportErrorException's are throw by ourselves and caught elsewhere.
  865. //-----------------------------------------------------------------------
  866. WriteLogOpening(pFile);
  867. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  868. try
  869. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  870. {
  871. // Check whether this Importer instance has already loaded
  872. // a scene. In this case we need to delete the old one
  873. if (pimpl->mScene) {
  874. DefaultLogger::get()->debug("(Deleting previous scene)");
  875. FreeScene();
  876. }
  877. // First check if the file is accessable at all
  878. if( !pimpl->mIOHandler->Exists( pFile)) {
  879. pimpl->mErrorString = "Unable to open file \"" + pFile + "\".";
  880. DefaultLogger::get()->error(pimpl->mErrorString);
  881. return NULL;
  882. }
  883. boost::scoped_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME,0)?new Profiler():NULL);
  884. if (profiler) {
  885. profiler->BeginRegion("total");
  886. }
  887. // Find an worker class which can handle the file
  888. BaseImporter* imp = NULL;
  889. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  890. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, false)) {
  891. imp = pimpl->mImporter[a];
  892. break;
  893. }
  894. }
  895. if (!imp) {
  896. // not so bad yet ... try format auto detection.
  897. const std::string::size_type s = pFile.find_last_of('.');
  898. if (s != std::string::npos) {
  899. DefaultLogger::get()->info("File extension not known, trying signature-based detection");
  900. for( unsigned int a = 0; a < pimpl->mImporter.size(); a++) {
  901. if( pimpl->mImporter[a]->CanRead( pFile, pimpl->mIOHandler, true)) {
  902. imp = pimpl->mImporter[a];
  903. break;
  904. }
  905. }
  906. }
  907. // Put a proper error message if no suitable importer was found
  908. if( !imp) {
  909. pimpl->mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  910. DefaultLogger::get()->error(pimpl->mErrorString);
  911. return NULL;
  912. }
  913. }
  914. // Dispatch the reading to the worker class for this format
  915. DefaultLogger::get()->info("Found a matching importer for this file format");
  916. pimpl->mProgressHandler->Update();
  917. if (profiler) {
  918. profiler->BeginRegion("import");
  919. }
  920. pimpl->mScene = imp->ReadFile( this, pFile, pimpl->mIOHandler);
  921. pimpl->mProgressHandler->Update();
  922. if (profiler) {
  923. profiler->EndRegion("import");
  924. }
  925. // If successful, apply all active post processing steps to the imported data
  926. if( pimpl->mScene) {
  927. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  928. // The ValidateDS process is an exception. It is executed first, even before ScenePreprocessor is called.
  929. if (pFlags & aiProcess_ValidateDataStructure)
  930. {
  931. ValidateDSProcess ds;
  932. ds.ExecuteOnScene (this);
  933. if (!pimpl->mScene) {
  934. return NULL;
  935. }
  936. }
  937. #endif // no validation
  938. // Preprocess the scene and prepare it for post-processing
  939. if (profiler) {
  940. profiler->BeginRegion("preprocess");
  941. }
  942. ScenePreprocessor pre(pimpl->mScene);
  943. pre.ProcessScene();
  944. pimpl->mProgressHandler->Update();
  945. if (profiler) {
  946. profiler->EndRegion("preprocess");
  947. }
  948. // Ensure that the validation process won't be called twice
  949. ApplyPostProcessing(pFlags & (~aiProcess_ValidateDataStructure));
  950. }
  951. // if failed, extract the error string
  952. else if( !pimpl->mScene) {
  953. pimpl->mErrorString = imp->GetErrorText();
  954. }
  955. // clear any data allocated by post-process steps
  956. pimpl->mPPShared->Clean();
  957. if (profiler) {
  958. profiler->EndRegion("total");
  959. }
  960. }
  961. #ifdef ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  962. catch (std::exception &e)
  963. {
  964. #if (defined _MSC_VER) && (defined _CPPRTTI)
  965. // if we have RTTI get the full name of the exception that occured
  966. pimpl->mErrorString = std::string(typeid( e ).name()) + ": " + e.what();
  967. #else
  968. pimpl->mErrorString = std::string("std::exception: ") + e.what();
  969. #endif
  970. DefaultLogger::get()->error(pimpl->mErrorString);
  971. delete pimpl->mScene; pimpl->mScene = NULL;
  972. }
  973. #endif // ! ASSIMP_CATCH_GLOBAL_EXCEPTIONS
  974. // either successful or failure - the pointer expresses it anyways
  975. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  976. return pimpl->mScene;
  977. }
  978. // ------------------------------------------------------------------------------------------------
  979. // Apply post-processing to the currently bound scene
  980. const aiScene* Importer::ApplyPostProcessing(unsigned int pFlags)
  981. {
  982. ASSIMP_BEGIN_EXCEPTION_REGION();
  983. // Return immediately if no scene is active
  984. if (!pimpl->mScene) {
  985. return NULL;
  986. }
  987. // If no flags are given, return the current scene with no further action
  988. if (!pFlags) {
  989. return pimpl->mScene;
  990. }
  991. // In debug builds: run basic flag validation
  992. ai_assert(_ValidateFlags(pFlags));
  993. DefaultLogger::get()->info("Entering post processing pipeline");
  994. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  995. // The ValidateDS process plays an exceptional role. It isn't contained in the global
  996. // list of post-processing steps, so we need to call it manually.
  997. if (pFlags & aiProcess_ValidateDataStructure)
  998. {
  999. ValidateDSProcess ds;
  1000. ds.ExecuteOnScene (this);
  1001. if (!pimpl->mScene) {
  1002. return NULL;
  1003. }
  1004. }
  1005. #endif // no validation
  1006. #ifdef _DEBUG
  1007. if (pimpl->bExtraVerbose)
  1008. {
  1009. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  1010. DefaultLogger::get()->error("Verbose Import is not available due to build settings");
  1011. #endif // no validation
  1012. pFlags |= aiProcess_ValidateDataStructure;
  1013. }
  1014. #else
  1015. if (pimpl->bExtraVerbose) {
  1016. DefaultLogger::get()->warn("Not a debug build, ignoring extra verbose setting");
  1017. }
  1018. #endif // ! DEBUG
  1019. boost::scoped_ptr<Profiler> profiler(GetPropertyInteger(AI_CONFIG_GLOB_MEASURE_TIME,0)?new Profiler():NULL);
  1020. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  1021. BaseProcess* process = pimpl->mPostProcessingSteps[a];
  1022. if( process->IsActive( pFlags)) {
  1023. if (profiler) {
  1024. profiler->BeginRegion("postprocess");
  1025. }
  1026. process->ExecuteOnScene ( this );
  1027. pimpl->mProgressHandler->Update();
  1028. if (profiler) {
  1029. profiler->EndRegion("postprocess");
  1030. }
  1031. }
  1032. if( !pimpl->mScene) {
  1033. break;
  1034. }
  1035. #ifdef _DEBUG
  1036. #ifndef ASSIMP_BUILD_NO_VALIDATEDS_PROCESS
  1037. continue;
  1038. #endif // no validation
  1039. // If the extra verbose mode is active, execute the ValidateDataStructureStep again - after each step
  1040. if (pimpl->bExtraVerbose) {
  1041. DefaultLogger::get()->debug("Verbose Import: revalidating data structures");
  1042. ValidateDSProcess ds;
  1043. ds.ExecuteOnScene (this);
  1044. if( !pimpl->mScene) {
  1045. DefaultLogger::get()->error("Verbose Import: failed to revalidate data structures");
  1046. break;
  1047. }
  1048. }
  1049. #endif // ! DEBUG
  1050. }
  1051. // clear any data allocated by post-process steps
  1052. pimpl->mPPShared->Clean();
  1053. DefaultLogger::get()->info("Leaving post processing pipeline");
  1054. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  1055. return pimpl->mScene;
  1056. }
  1057. // ------------------------------------------------------------------------------------------------
  1058. // Helper function to check whether an extension is supported by ASSIMP
  1059. bool Importer::IsExtensionSupported(const char* szExtension) const
  1060. {
  1061. return NULL != FindLoader(szExtension);
  1062. }
  1063. // ------------------------------------------------------------------------------------------------
  1064. // Find a loader plugin for a given file extension
  1065. BaseImporter* Importer::FindLoader (const char* szExtension) const
  1066. {
  1067. ai_assert(szExtension);
  1068. ASSIMP_BEGIN_EXCEPTION_REGION();
  1069. // skip over wildcard and dot characters at string head --
  1070. for(;*szExtension == '*' || *szExtension == '.'; ++szExtension);
  1071. std::string ext(szExtension);
  1072. if (ext.empty()) {
  1073. return NULL;
  1074. }
  1075. std::transform(ext.begin(),ext.end(), ext.begin(), tolower);
  1076. std::set<std::string> str;
  1077. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  1078. str.clear();
  1079. (*i)->GetExtensionList(str);
  1080. for (std::set<std::string>::const_iterator it = str.begin(); it != str.end(); ++it) {
  1081. if (ext == *it) {
  1082. return (*i);
  1083. }
  1084. }
  1085. }
  1086. ASSIMP_END_EXCEPTION_REGION(BaseImporter*);
  1087. return NULL;
  1088. }
  1089. // ------------------------------------------------------------------------------------------------
  1090. // Helper function to build a list of all file extensions supported by ASSIMP
  1091. void Importer::GetExtensionList(aiString& szOut) const
  1092. {
  1093. ASSIMP_BEGIN_EXCEPTION_REGION();
  1094. std::set<std::string> str;
  1095. for (std::vector<BaseImporter*>::const_iterator i = pimpl->mImporter.begin();i != pimpl->mImporter.end();++i) {
  1096. (*i)->GetExtensionList(str);
  1097. }
  1098. for (std::set<std::string>::const_iterator it = str.begin();; ) {
  1099. szOut.Append("*.");
  1100. szOut.Append((*it).c_str());
  1101. if (++it == str.end()) {
  1102. break;
  1103. }
  1104. szOut.Append(";");
  1105. }
  1106. ASSIMP_END_EXCEPTION_REGION(void);
  1107. }
  1108. // ------------------------------------------------------------------------------------------------
  1109. // Set a configuration property
  1110. void Importer::SetPropertyInteger(const char* szName, int iValue,
  1111. bool* bWasExisting /*= NULL*/)
  1112. {
  1113. ASSIMP_BEGIN_EXCEPTION_REGION();
  1114. SetGenericProperty<int>(pimpl->mIntProperties, szName,iValue,bWasExisting);
  1115. ASSIMP_END_EXCEPTION_REGION(void);
  1116. }
  1117. // ------------------------------------------------------------------------------------------------
  1118. // Set a configuration property
  1119. void Importer::SetPropertyFloat(const char* szName, float iValue,
  1120. bool* bWasExisting /*= NULL*/)
  1121. {
  1122. ASSIMP_BEGIN_EXCEPTION_REGION();
  1123. SetGenericProperty<float>(pimpl->mFloatProperties, szName,iValue,bWasExisting);
  1124. ASSIMP_END_EXCEPTION_REGION(void);
  1125. }
  1126. // ------------------------------------------------------------------------------------------------
  1127. // Set a configuration property
  1128. void Importer::SetPropertyString(const char* szName, const std::string& value,
  1129. bool* bWasExisting /*= NULL*/)
  1130. {
  1131. ASSIMP_BEGIN_EXCEPTION_REGION();
  1132. SetGenericProperty<std::string>(pimpl->mStringProperties, szName,value,bWasExisting);
  1133. ASSIMP_END_EXCEPTION_REGION(void);
  1134. }
  1135. // ------------------------------------------------------------------------------------------------
  1136. // Get a configuration property
  1137. int Importer::GetPropertyInteger(const char* szName,
  1138. int iErrorReturn /*= 0xffffffff*/) const
  1139. {
  1140. return GetGenericProperty<int>(pimpl->mIntProperties,szName,iErrorReturn);
  1141. }
  1142. // ------------------------------------------------------------------------------------------------
  1143. // Get a configuration property
  1144. float Importer::GetPropertyFloat(const char* szName,
  1145. float iErrorReturn /*= 10e10*/) const
  1146. {
  1147. return GetGenericProperty<float>(pimpl->mFloatProperties,szName,iErrorReturn);
  1148. }
  1149. // ------------------------------------------------------------------------------------------------
  1150. // Get a configuration property
  1151. const std::string& Importer::GetPropertyString(const char* szName,
  1152. const std::string& iErrorReturn /*= ""*/) const
  1153. {
  1154. return GetGenericProperty<std::string>(pimpl->mStringProperties,szName,iErrorReturn);
  1155. }
  1156. // ------------------------------------------------------------------------------------------------
  1157. // Get the memory requirements of a single node
  1158. inline void AddNodeWeight(unsigned int& iScene,const aiNode* pcNode)
  1159. {
  1160. iScene += sizeof(aiNode);
  1161. iScene += sizeof(unsigned int) * pcNode->mNumMeshes;
  1162. iScene += sizeof(void*) * pcNode->mNumChildren;
  1163. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  1164. AddNodeWeight(iScene,pcNode->mChildren[i]);
  1165. }
  1166. }
  1167. // ------------------------------------------------------------------------------------------------
  1168. // Get the memory requirements of the scene
  1169. void Importer::GetMemoryRequirements(aiMemoryInfo& in) const
  1170. {
  1171. in = aiMemoryInfo();
  1172. aiScene* mScene = pimpl->mScene;
  1173. // return if we have no scene loaded
  1174. if (!pimpl->mScene)
  1175. return;
  1176. in.total = sizeof(aiScene);
  1177. // add all meshes
  1178. for (unsigned int i = 0; i < mScene->mNumMeshes;++i)
  1179. {
  1180. in.meshes += sizeof(aiMesh);
  1181. if (mScene->mMeshes[i]->HasPositions()) {
  1182. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1183. }
  1184. if (mScene->mMeshes[i]->HasNormals()) {
  1185. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1186. }
  1187. if (mScene->mMeshes[i]->HasTangentsAndBitangents()) {
  1188. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices * 2;
  1189. }
  1190. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS;++a) {
  1191. if (mScene->mMeshes[i]->HasVertexColors(a)) {
  1192. in.meshes += sizeof(aiColor4D) * mScene->mMeshes[i]->mNumVertices;
  1193. }
  1194. else break;
  1195. }
  1196. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS;++a) {
  1197. if (mScene->mMeshes[i]->HasTextureCoords(a)) {
  1198. in.meshes += sizeof(aiVector3D) * mScene->mMeshes[i]->mNumVertices;
  1199. }
  1200. else break;
  1201. }
  1202. if (mScene->mMeshes[i]->HasBones()) {
  1203. in.meshes += sizeof(void*) * mScene->mMeshes[i]->mNumBones;
  1204. for (unsigned int p = 0; p < mScene->mMeshes[i]->mNumBones;++p) {
  1205. in.meshes += sizeof(aiBone);
  1206. in.meshes += mScene->mMeshes[i]->mBones[p]->mNumWeights * sizeof(aiVertexWeight);
  1207. }
  1208. }
  1209. in.meshes += (sizeof(aiFace) + 3 * sizeof(unsigned int))*mScene->mMeshes[i]->mNumFaces;
  1210. }
  1211. in.total += in.meshes;
  1212. // add all embedded textures
  1213. for (unsigned int i = 0; i < mScene->mNumTextures;++i) {
  1214. const aiTexture* pc = mScene->mTextures[i];
  1215. in.textures += sizeof(aiTexture);
  1216. if (pc->mHeight) {
  1217. in.textures += 4 * pc->mHeight * pc->mWidth;
  1218. }
  1219. else in.textures += pc->mWidth;
  1220. }
  1221. in.total += in.textures;
  1222. // add all animations
  1223. for (unsigned int i = 0; i < mScene->mNumAnimations;++i) {
  1224. const aiAnimation* pc = mScene->mAnimations[i];
  1225. in.animations += sizeof(aiAnimation);
  1226. // add all bone anims
  1227. for (unsigned int a = 0; a < pc->mNumChannels; ++a) {
  1228. const aiNodeAnim* pc2 = pc->mChannels[i];
  1229. in.animations += sizeof(aiNodeAnim);
  1230. in.animations += pc2->mNumPositionKeys * sizeof(aiVectorKey);
  1231. in.animations += pc2->mNumScalingKeys * sizeof(aiVectorKey);
  1232. in.animations += pc2->mNumRotationKeys * sizeof(aiQuatKey);
  1233. }
  1234. }
  1235. in.total += in.animations;
  1236. // add all cameras and all lights
  1237. in.total += in.cameras = sizeof(aiCamera) * mScene->mNumCameras;
  1238. in.total += in.lights = sizeof(aiLight) * mScene->mNumLights;
  1239. // add all nodes
  1240. AddNodeWeight(in.nodes,mScene->mRootNode);
  1241. in.total += in.nodes;
  1242. // add all materials
  1243. for (unsigned int i = 0; i < mScene->mNumMaterials;++i) {
  1244. const aiMaterial* pc = mScene->mMaterials[i];
  1245. in.materials += sizeof(aiMaterial);
  1246. in.materials += pc->mNumAllocated * sizeof(void*);
  1247. for (unsigned int a = 0; a < pc->mNumProperties;++a) {
  1248. in.materials += pc->mProperties[a]->mDataLength;
  1249. }
  1250. }
  1251. in.total += in.materials;
  1252. }