Importer.cpp 9.2 KB

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