Importer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. // STL/CSL heades
  36. #include <fstream>
  37. #include <string>
  38. // public Assimp API
  39. #include "../include/assimp.hpp"
  40. #include "../include/aiAssert.h"
  41. #include "../include/aiScene.h"
  42. #include "../include/aiPostProcess.h"
  43. #include "../include/DefaultLogger.h"
  44. // internal headers
  45. #include "BaseImporter.h"
  46. #include "BaseProcess.h"
  47. #include "DefaultIOStream.h"
  48. #include "DefaultIOSystem.h"
  49. // Importers
  50. #if (!defined AI_BUILD_NO_X_IMPORTER)
  51. # include "XFileImporter.h"
  52. #endif
  53. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  54. # include "3DSLoader.h"
  55. #endif
  56. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  57. # include "MD3Loader.h"
  58. #endif
  59. #if (!defined AI_BUILD_NO_MD4_IMPORTER)
  60. # include "MD4Loader.h"
  61. #endif
  62. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  63. # include "MDLLoader.h"
  64. #endif
  65. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  66. # include "MD2Loader.h"
  67. #endif
  68. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  69. # include "PlyLoader.h"
  70. #endif
  71. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  72. # include "ASELoader.h"
  73. #endif
  74. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  75. # include "ObjFileImporter.h"
  76. #endif
  77. #if (!defined AI_BUILD_NO_HMP_IMPORTER)
  78. # include "HMPLoader.h"
  79. #endif
  80. #if (!defined AI_BUILD_NO_SMD_IMPORTER)
  81. # include "SMDLoader.h"
  82. #endif
  83. #if (!defined AI_BUILD_NO_MD5_IMPORTER)
  84. # include "MD5Loader.h"
  85. #endif
  86. #if (!defined AI_BUILD_NO_STL_IMPORTER)
  87. # include "STLLoader.h"
  88. #endif
  89. #if (!defined AI_BUILD_NO_LWO_IMPORTER)
  90. # include "LWOLoader.h"
  91. #endif
  92. // PostProcess-Steps
  93. #if (!defined AI_BUILD_NO_CALCTANGENTS_PROCESS)
  94. # include "CalcTangentsProcess.h"
  95. #endif
  96. #if (!defined AI_BUILD_NO_JOINVERTICES_PROCESS)
  97. # include "JoinVerticesProcess.h"
  98. #endif
  99. #if (!defined AI_BUILD_NO_CONVERTTOLH_PROCESS)
  100. # include "ConvertToLHProcess.h"
  101. #endif
  102. #if (!defined AI_BUILD_NO_TRIANGULATE_PROCESS)
  103. # include "TriangulateProcess.h"
  104. #endif
  105. #if (!defined AI_BUILD_NO_GENFACENORMALS_PROCESS)
  106. # include "GenFaceNormalsProcess.h"
  107. #endif
  108. #if (!defined AI_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  109. # include "GenVertexNormalsProcess.h"
  110. #endif
  111. #if (!defined AI_BUILD_NO_KILLNORMALS_PROCESS)
  112. # include "KillNormalsProcess.h"
  113. #endif
  114. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  115. # include "SplitLargeMeshes.h"
  116. #endif
  117. #if (!defined AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  118. # include "PretransformVertices.h"
  119. #endif
  120. #if (!defined AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  121. # include "LimitBoneWeightsProcess.h"
  122. #endif
  123. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  124. # include "ValidateDataStructure.h"
  125. #endif
  126. #if (!defined AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  127. # include "ImproveCacheLocality.h"
  128. #endif
  129. #if (!defined AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  130. # include "RemoveRedundantMaterials.h"
  131. #endif
  132. using namespace Assimp;
  133. // ------------------------------------------------------------------------------------------------
  134. // Constructor.
  135. Importer::Importer() :
  136. mIOHandler(NULL),
  137. mScene(NULL),
  138. mErrorString("")
  139. {
  140. // allocate a default IO handler
  141. mIOHandler = new DefaultIOSystem;
  142. mIsDefaultHandler = true;
  143. bExtraVerbose = false; // disable extra verbose mode by default
  144. // add an instance of each worker class here
  145. #if (!defined AI_BUILD_NO_X_IMPORTER)
  146. mImporter.push_back( new XFileImporter());
  147. #endif
  148. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  149. mImporter.push_back( new ObjFileImporter());
  150. #endif
  151. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  152. mImporter.push_back( new Dot3DSImporter());
  153. #endif
  154. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  155. mImporter.push_back( new MD3Importer());
  156. #endif
  157. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  158. mImporter.push_back( new MD2Importer());
  159. #endif
  160. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  161. mImporter.push_back( new PLYImporter());
  162. #endif
  163. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  164. mImporter.push_back( new MDLImporter());
  165. #endif
  166. #if (!defined AI_BUILD_NO_MD4_IMPORTER)
  167. mImporter.push_back( new MD4Importer());
  168. #endif
  169. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  170. mImporter.push_back( new ASEImporter());
  171. #endif
  172. #if (!defined AI_BUILD_NO_HMP_IMPORTER)
  173. mImporter.push_back( new HMPImporter());
  174. #endif
  175. #if (!defined AI_BUILD_NO_SMD_IMPORTER)
  176. mImporter.push_back( new SMDImporter());
  177. #endif
  178. #if (!defined AI_BUILD_NO_MD5_IMPORTER)
  179. mImporter.push_back( new MD5Importer());
  180. #endif
  181. #if (!defined AI_BUILD_NO_STL_IMPORTER)
  182. mImporter.push_back( new STLImporter());
  183. #endif
  184. #if (!defined AI_BUILD_NO_LWO_IMPORTER)
  185. mImporter.push_back( new LWOImporter());
  186. #endif
  187. // add an instance of each post processing step here in the order
  188. // of sequence it is executed
  189. #if (!defined AI_BUILD_NO_VALIDATEDS_PROCESS)
  190. mPostProcessingSteps.push_back( new ValidateDSProcess()); // must be first
  191. #endif
  192. #if (!defined AI_BUILD_NO_REMOVE_REDUNDANTMATERIALS_PROCESS)
  193. mPostProcessingSteps.push_back( new RemoveRedundantMatsProcess());
  194. #endif
  195. #if (!defined AI_BUILD_NO_TRIANGULATE_PROCESS)
  196. mPostProcessingSteps.push_back( new TriangulateProcess());
  197. #endif
  198. #if (!defined AI_BUILD_NO_PRETRANSFORMVERTICES_PROCESS)
  199. mPostProcessingSteps.push_back( new PretransformVertices());
  200. #endif
  201. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  202. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  203. #endif
  204. #if (!defined AI_BUILD_NO_KILLNORMALS_PROCESS)
  205. mPostProcessingSteps.push_back( new KillNormalsProcess());
  206. #endif
  207. #if (!defined AI_BUILD_NO_GENFACENORMALS_PROCESS)
  208. mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  209. #endif
  210. #if (!defined AI_BUILD_NO_GENVERTEXNORMALS_PROCESS)
  211. mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  212. #endif
  213. #if (!defined AI_BUILD_NO_CALCTANGENTS_PROCESS)
  214. mPostProcessingSteps.push_back( new CalcTangentsProcess());
  215. #endif
  216. #if (!defined AI_BUILD_NO_JOINVERTICES_PROCESS)
  217. mPostProcessingSteps.push_back( new JoinVerticesProcess());
  218. #endif
  219. #if (!defined AI_BUILD_NO_SPLITLARGEMESHES_PROCESS)
  220. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  221. #endif
  222. #if (!defined AI_BUILD_NO_CONVERTTOLH_PROCESS)
  223. mPostProcessingSteps.push_back( new ConvertToLHProcess());
  224. #endif
  225. #if (!defined AI_BUILD_NO_LIMITBONEWEIGHTS_PROCESS)
  226. mPostProcessingSteps.push_back( new LimitBoneWeightsProcess());
  227. #endif
  228. #if (!defined AI_BUILD_NO_IMPROVECACHELOCALITY_PROCESS)
  229. mPostProcessingSteps.push_back( new ImproveCacheLocalityProcess());
  230. #endif
  231. }
  232. // ------------------------------------------------------------------------------------------------
  233. // Destructor.
  234. Importer::~Importer()
  235. {
  236. for( unsigned int a = 0; a < mImporter.size(); a++)
  237. delete mImporter[a];
  238. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  239. delete mPostProcessingSteps[a];
  240. // delete the assigned IO handler
  241. delete mIOHandler;
  242. // kill imported scene. Destructors should do that recursivly
  243. delete mScene;
  244. }
  245. // ------------------------------------------------------------------------------------------------
  246. // Supplies a custom IO handler to the importer to open and access files.
  247. void Importer::SetIOHandler( IOSystem* pIOHandler)
  248. {
  249. if (!pIOHandler)
  250. {
  251. delete mIOHandler;
  252. mIOHandler = new DefaultIOSystem();
  253. mIsDefaultHandler = true;
  254. }
  255. else if (mIOHandler != pIOHandler)
  256. {
  257. delete mIOHandler;
  258. mIOHandler = pIOHandler;
  259. mIsDefaultHandler = false;
  260. }
  261. return;
  262. }
  263. // ------------------------------------------------------------------------------------------------
  264. IOSystem* Importer::GetIOHandler()
  265. {
  266. return mIOHandler;
  267. }
  268. // ------------------------------------------------------------------------------------------------
  269. bool Importer::IsDefaultIOHandler()
  270. {
  271. return mIsDefaultHandler;
  272. }
  273. // ------------------------------------------------------------------------------------------------
  274. // Validate post process step flags
  275. bool ValidateFlags(unsigned int pFlags)
  276. {
  277. if (pFlags & aiProcess_GenSmoothNormals &&
  278. pFlags & aiProcess_GenNormals)
  279. {
  280. DefaultLogger::get()->error("aiProcess_GenSmoothNormals and aiProcess_GenNormals "
  281. "may not be specified together");
  282. return false;
  283. }
  284. return true;
  285. }
  286. // ------------------------------------------------------------------------------------------------
  287. // Reads the given file and returns its contents if successful.
  288. const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
  289. {
  290. // validate the flags
  291. ai_assert(ValidateFlags(pFlags));
  292. // check whether this Importer instance has already loaded
  293. // a scene. In this case we need to delete the old one
  294. if (this->mScene)
  295. {
  296. delete mScene;
  297. this->mScene = NULL;
  298. }
  299. // first check if the file is accessable at all
  300. if( !mIOHandler->Exists( pFile))
  301. {
  302. mErrorString = "Unable to open file \"" + pFile + "\".";
  303. DefaultLogger::get()->error(mErrorString);
  304. return NULL;
  305. }
  306. // find an worker class which can handle the file
  307. BaseImporter* imp = NULL;
  308. for( unsigned int a = 0; a < mImporter.size(); a++)
  309. {
  310. if( mImporter[a]->CanRead( pFile, mIOHandler))
  311. {
  312. imp = mImporter[a];
  313. break;
  314. }
  315. }
  316. // put a proper error message if no suitable importer was found
  317. if( !imp)
  318. {
  319. mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  320. DefaultLogger::get()->error(mErrorString);
  321. return NULL;
  322. }
  323. // dispatch the reading to the worker class for this format
  324. mScene = imp->ReadFile( pFile, mIOHandler);
  325. // if successful, apply all active post processing steps to the imported data
  326. if( mScene)
  327. {
  328. if (bExtraVerbose)
  329. {
  330. pFlags |= aiProcess_ValidateDataStructure;
  331. // use the MSB to tell the ValidateDS-Step that e're in extra verbose mode
  332. // TODO: temporary solution, clean up later
  333. mScene->mFlags |= 0x80000000;
  334. }
  335. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  336. {
  337. BaseProcess* process = mPostProcessingSteps[a];
  338. if( process->IsActive( pFlags))
  339. {
  340. process->ExecuteOnScene( this );
  341. }
  342. if( !mScene)break; // error string has already been set ...
  343. // if the extra verbose mode is active execute the
  344. // VaidateDataStructureStep again after each step
  345. if (bExtraVerbose && a)
  346. {
  347. DefaultLogger::get()->debug("Extra verbose: revalidating data structures");
  348. ((ValidateDSProcess*)mPostProcessingSteps[0])->ExecuteOnScene (this);
  349. if( !mScene)
  350. {
  351. DefaultLogger::get()->error("Extra verbose: failed to revalidate data structures");
  352. break; // error string has already been set ...
  353. }
  354. }
  355. }
  356. if (bExtraVerbose)mScene->mFlags &= ~0x80000000;
  357. }
  358. // if failed, extract the error string
  359. else if( !mScene)
  360. {
  361. mErrorString = imp->GetErrorText();
  362. }
  363. // either successful or failure - the pointer expresses it anyways
  364. return mScene;
  365. }
  366. // ------------------------------------------------------------------------------------------------
  367. // Empty and private copy constructor
  368. Importer::Importer(const Importer &other)
  369. {
  370. // empty
  371. }
  372. // ------------------------------------------------------------------------------------------------
  373. // Helper function to check whether an extension is supported by ASSIMP
  374. bool Importer::IsExtensionSupported(const std::string& szExtension)
  375. {
  376. for (std::vector<BaseImporter*>::const_iterator
  377. i = this->mImporter.begin();
  378. i != this->mImporter.end();++i)
  379. {
  380. // pass the file extension to the CanRead(..,NULL)-method
  381. if ((*i)->CanRead(szExtension,NULL))return true;
  382. }
  383. return false;
  384. }
  385. // ------------------------------------------------------------------------------------------------
  386. // Helper function to build a list of all file extensions supported by ASSIMP
  387. void Importer::GetExtensionList(std::string& szOut)
  388. {
  389. unsigned int iNum = 0;
  390. for (std::vector<BaseImporter*>::const_iterator
  391. i = this->mImporter.begin();
  392. i != this->mImporter.end();++i,++iNum)
  393. {
  394. // insert a comma as delimiter character
  395. if (0 != iNum)
  396. szOut.append(";");
  397. (*i)->GetExtensionList(szOut);
  398. }
  399. return;
  400. }