Importer.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <fstream>
  36. #include <string>
  37. #include "../include/assimp.hpp"
  38. #include "../include/aiAssert.h"
  39. #include "../include/aiScene.h"
  40. #include "../include/aiPostProcess.h"
  41. #include "BaseImporter.h"
  42. #include "BaseProcess.h"
  43. #include "DefaultIOStream.h"
  44. #include "DefaultIOSystem.h"
  45. #if (!defined AI_BUILD_NO_X_IMPORTER)
  46. # include "XFileImporter.h"
  47. #endif
  48. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  49. # include "3DSLoader.h"
  50. #endif
  51. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  52. # include "MD3Loader.h"
  53. #endif
  54. #if (!defined AI_BUILD_NO_MD4_IMPORTER)
  55. # include "MD4Loader.h"
  56. #endif
  57. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  58. # include "MDLLoader.h"
  59. #endif
  60. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  61. # include "MD2Loader.h"
  62. #endif
  63. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  64. # include "PlyLoader.h"
  65. #endif
  66. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  67. # include "ASELoader.h"
  68. #endif
  69. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  70. # include "ObjFileImporter.h"
  71. #endif
  72. #include "CalcTangentsProcess.h"
  73. #include "JoinVerticesProcess.h"
  74. #include "ConvertToLHProcess.h"
  75. #include "TriangulateProcess.h"
  76. #include "GenFaceNormalsProcess.h"
  77. #include "GenVertexNormalsProcess.h"
  78. #include "KillNormalsProcess.h"
  79. #include "SplitLargeMeshes.h"
  80. #include "PretransformVertices.h"
  81. #include "../include/DefaultLogger.h"
  82. using namespace Assimp;
  83. // ------------------------------------------------------------------------------------------------
  84. // Constructor.
  85. Importer::Importer() :
  86. mIOHandler(NULL),
  87. mScene(NULL),
  88. mErrorString("")
  89. {
  90. // allocate a default IO handler
  91. mIOHandler = new DefaultIOSystem;
  92. // add an instance of each worker class here
  93. #if (!defined AI_BUILD_NO_X_IMPORTER)
  94. mImporter.push_back( new XFileImporter());
  95. #endif
  96. #if (!defined AI_BUILD_NO_OBJ_IMPORTER)
  97. mImporter.push_back( new ObjFileImporter());
  98. #endif
  99. #if (!defined AI_BUILD_NO_3DS_IMPORTER)
  100. mImporter.push_back( new Dot3DSImporter());
  101. #endif
  102. #if (!defined AI_BUILD_NO_MD3_IMPORTER)
  103. mImporter.push_back( new MD3Importer());
  104. #endif
  105. #if (!defined AI_BUILD_NO_MD2_IMPORTER)
  106. mImporter.push_back( new MD2Importer());
  107. #endif
  108. #if (!defined AI_BUILD_NO_PLY_IMPORTER)
  109. mImporter.push_back( new PLYImporter());
  110. #endif
  111. #if (!defined AI_BUILD_NO_MDL_IMPORTER)
  112. mImporter.push_back( new MDLImporter());
  113. #endif
  114. #if (!defined AI_BUILD_NO_MD4_IMPORTER)
  115. mImporter.push_back( new MD4Importer());
  116. #endif
  117. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  118. mImporter.push_back( new ASEImporter());
  119. #endif
  120. // add an instance of each post processing step here in the order of sequence it is executed
  121. mPostProcessingSteps.push_back( new TriangulateProcess());
  122. mPostProcessingSteps.push_back( new PretransformVertices());
  123. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Triangle());
  124. mPostProcessingSteps.push_back( new KillNormalsProcess());
  125. mPostProcessingSteps.push_back( new GenFaceNormalsProcess());
  126. mPostProcessingSteps.push_back( new GenVertexNormalsProcess());
  127. mPostProcessingSteps.push_back( new CalcTangentsProcess());
  128. mPostProcessingSteps.push_back( new JoinVerticesProcess());
  129. mPostProcessingSteps.push_back( new SplitLargeMeshesProcess_Vertex());
  130. mPostProcessingSteps.push_back( new ConvertToLHProcess());
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. // Destructor.
  134. Importer::~Importer()
  135. {
  136. for( unsigned int a = 0; a < mImporter.size(); a++)
  137. delete mImporter[a];
  138. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  139. delete mPostProcessingSteps[a];
  140. // delete the assigned IO handler
  141. delete mIOHandler;
  142. // kill imported scene. Destructors should do that recursivly
  143. delete mScene;
  144. }
  145. // ------------------------------------------------------------------------------------------------
  146. // Supplies a custom IO handler to the importer to open and access files.
  147. void Importer::SetIOHandler( IOSystem* pIOHandler)
  148. {
  149. if (NULL == pIOHandler)
  150. {
  151. delete mIOHandler;
  152. mIOHandler = new DefaultIOSystem();
  153. }
  154. else if (mIOHandler != pIOHandler)
  155. {
  156. delete mIOHandler;
  157. mIOHandler = pIOHandler;
  158. }
  159. return;
  160. }
  161. // ------------------------------------------------------------------------------------------------
  162. // Validate post process step flags
  163. bool ValidateFlags(unsigned int pFlags)
  164. {
  165. if (pFlags & aiProcess_GenSmoothNormals &&
  166. pFlags & aiProcess_GenNormals)
  167. {
  168. DefaultLogger::get()->error("aiProcess_GenSmoothNormals and aiProcess_GenNormals "
  169. "may not be specified together");
  170. return false;
  171. }
  172. return true;
  173. }
  174. // ------------------------------------------------------------------------------------------------
  175. // Reads the given file and returns its contents if successful.
  176. const aiScene* Importer::ReadFile( const std::string& pFile, unsigned int pFlags)
  177. {
  178. // validate the flags
  179. ai_assert(ValidateFlags(pFlags));
  180. // check whether this Importer instance has already loaded
  181. // a scene. In this case we need to delete the old one
  182. if (this->mScene)
  183. {
  184. delete mScene;
  185. this->mScene = NULL;
  186. }
  187. // first check if the file is accessable at all
  188. if( !mIOHandler->Exists( pFile))
  189. {
  190. mErrorString = "Unable to open file \"" + pFile + "\".";
  191. DefaultLogger::get()->error(mErrorString);
  192. return NULL;
  193. }
  194. // find an worker class which can handle the file
  195. BaseImporter* imp = NULL;
  196. for( unsigned int a = 0; a < mImporter.size(); a++)
  197. {
  198. if( mImporter[a]->CanRead( pFile, mIOHandler))
  199. {
  200. imp = mImporter[a];
  201. break;
  202. }
  203. }
  204. // put a proper error message if no suitable importer was found
  205. if( !imp)
  206. {
  207. mErrorString = "No suitable reader found for the file format of file \"" + pFile + "\".";
  208. DefaultLogger::get()->error(mErrorString);
  209. return NULL;
  210. }
  211. // dispatch the reading to the worker class for this format
  212. mScene = imp->ReadFile( pFile, mIOHandler);
  213. // if failed, extract the error string
  214. if( !mScene)
  215. mErrorString = imp->GetErrorText();
  216. // if successful, apply all active post processing steps to the imported data
  217. if( mScene)
  218. {
  219. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++)
  220. {
  221. BaseProcess* process = mPostProcessingSteps[a];
  222. if( process->IsActive( pFlags))
  223. process->Execute( mScene);
  224. }
  225. }
  226. // either successful or failure - the pointer expresses it anyways
  227. return mScene;
  228. }
  229. // ------------------------------------------------------------------------------------------------
  230. // Empty and private copy constructor
  231. Importer::Importer(const Importer &other)
  232. {
  233. // empty
  234. }
  235. // ------------------------------------------------------------------------------------------------
  236. // Helper function to check whether an extension is supported by ASSIMP
  237. bool Importer::IsExtensionSupported(const std::string& szExtension)
  238. {
  239. for (std::vector<BaseImporter*>::const_iterator
  240. i = this->mImporter.begin();
  241. i != this->mImporter.end();++i)
  242. {
  243. // pass the file extension to the CanRead(..,NULL)-method
  244. if ((*i)->CanRead(szExtension,NULL))return true;
  245. }
  246. return false;
  247. }
  248. // ------------------------------------------------------------------------------------------------
  249. // Helper function to build a list of all file extensions supported by ASSIMP
  250. void Importer::GetExtensionList(std::string& szOut)
  251. {
  252. unsigned int iNum = 0;
  253. for (std::vector<BaseImporter*>::const_iterator
  254. i = this->mImporter.begin();
  255. i != this->mImporter.end();++i,++iNum)
  256. {
  257. // insert a comma as delimiter character
  258. if (0 != iNum)
  259. szOut.append(";");
  260. (*i)->GetExtensionList(szOut);
  261. }
  262. return;
  263. }