Assimp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 Plain-C API */
  35. #include "AssimpPCH.h"
  36. #include "../include/assimp.h"
  37. // public ASSIMP headers
  38. #include "../include/aiFileIO.h"
  39. #include "GenericProperty.h"
  40. #if (defined AI_C_THREADSAFE)
  41. # include <boost/thread/thread.hpp>
  42. # include <boost/thread/mutex.hpp>
  43. #endif
  44. using namespace Assimp;
  45. /** Stores the importer objects for all active import processes */
  46. typedef std::map< const aiScene*, Assimp::Importer* > ImporterMap;
  47. /** Local storage of all active import processes */
  48. static ImporterMap gActiveImports;
  49. /** Error message of the last failed import process */
  50. static std::string gLastErrorString;
  51. /** Configuration properties */
  52. static Importer::IntPropertyMap gIntProperties;
  53. static Importer::FloatPropertyMap gFloatProperties;
  54. static Importer::StringPropertyMap gStringProperties;
  55. #if (defined AI_C_THREADSAFE)
  56. /** Global mutex to manage the access to the importer map */
  57. static boost::mutex gMutex;
  58. #endif
  59. class CIOSystemWrapper;
  60. class CIOStreamWrapper;
  61. // ------------------------------------------------------------------------------------------------
  62. // Custom IOStream implementation for the C-API
  63. class CIOStreamWrapper : public IOStream
  64. {
  65. friend class CIOSystemWrapper;
  66. public:
  67. CIOStreamWrapper(aiFile* pFile)
  68. : mFile(pFile)
  69. {}
  70. // -------------------------------------------------------------------
  71. size_t Read(void* pvBuffer,
  72. size_t pSize,
  73. size_t pCount)
  74. {
  75. // need to typecast here as C has no void*
  76. return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
  77. }
  78. // -------------------------------------------------------------------
  79. size_t Write(const void* pvBuffer,
  80. size_t pSize,
  81. size_t pCount)
  82. {
  83. // need to typecast here as C has no void*
  84. return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
  85. }
  86. // -------------------------------------------------------------------
  87. aiReturn Seek(size_t pOffset,
  88. aiOrigin pOrigin)
  89. {
  90. return mFile->SeekProc(mFile,pOffset,pOrigin);
  91. }
  92. // -------------------------------------------------------------------
  93. size_t Tell(void) const
  94. {
  95. return mFile->TellProc(mFile);
  96. }
  97. // -------------------------------------------------------------------
  98. size_t FileSize() const
  99. {
  100. return mFile->FileSizeProc(mFile);
  101. }
  102. private:
  103. aiFile* mFile;
  104. };
  105. // ------------------------------------------------------------------------------------------------
  106. // Custom IOStream implementation for the C-API
  107. class CIOSystemWrapper : public IOSystem
  108. {
  109. public:
  110. CIOSystemWrapper(aiFileIO* pFile)
  111. : mFileSystem(pFile)
  112. {}
  113. // -------------------------------------------------------------------
  114. bool Exists( const std::string& pFile) const
  115. {
  116. CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
  117. IOStream* p = pip->Open(pFile);
  118. if (p){pip->Close(p);return true;}
  119. return false;
  120. }
  121. // -------------------------------------------------------------------
  122. std::string getOsSeparator() const
  123. {
  124. return "/";
  125. }
  126. // -------------------------------------------------------------------
  127. IOStream* Open(const std::string& pFile,
  128. const std::string& pMode = std::string("rb"))
  129. {
  130. aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile.c_str(),pMode.c_str());
  131. if (!p)return NULL;
  132. return new CIOStreamWrapper(p);
  133. }
  134. // -------------------------------------------------------------------
  135. void Close( IOStream* pFile)
  136. {
  137. if (!pFile)return;
  138. mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
  139. delete pFile;
  140. }
  141. private:
  142. aiFileIO* mFileSystem;
  143. };
  144. // ------------------------------------------------------------------------------------------------
  145. void ReportSceneNotFoundError()
  146. {
  147. DefaultLogger::get()->error("Unable to find the Importer instance for this scene. "
  148. "Are you sure it has been created by aiImportFile(ex)(...)?");
  149. }
  150. // ------------------------------------------------------------------------------------------------
  151. // Reads the given file and returns its content.
  152. const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
  153. {
  154. return aiImportFileEx(pFile,pFlags,NULL);
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
  158. aiFileIO* pFS)
  159. {
  160. ai_assert(NULL != pFile);
  161. // create an Importer for this file
  162. Assimp::Importer* imp = new Assimp::Importer;
  163. // copy the global property lists to the Importer instance
  164. // (we are a friend of Importer)
  165. imp->mIntProperties = gIntProperties;
  166. imp->mFloatProperties = gFloatProperties;
  167. imp->mStringProperties = gStringProperties;
  168. // setup a custom IO system if necessary
  169. if (pFS)
  170. {
  171. imp->SetIOHandler( new CIOSystemWrapper (pFS) );
  172. }
  173. // and have it read the file
  174. const aiScene* scene = imp->ReadFile( pFile, pFlags);
  175. // if succeeded, place it in the collection of active processes
  176. if( scene)
  177. {
  178. #if (defined AI_C_THREADSAFE)
  179. boost::mutex::scoped_lock lock(gMutex);
  180. #endif
  181. gActiveImports[scene] = imp;
  182. }
  183. else
  184. {
  185. // if failed, extract error code and destroy the import
  186. gLastErrorString = imp->GetErrorString();
  187. delete imp;
  188. }
  189. // return imported data. If the import failed the pointer is NULL anyways
  190. return scene;
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. // Releases all resources associated with the given import process.
  194. void aiReleaseImport( const aiScene* pScene)
  195. {
  196. if (!pScene)return;
  197. // lock the mutex
  198. #if (defined AI_C_THREADSAFE)
  199. boost::mutex::scoped_lock lock(gMutex);
  200. #endif
  201. // find the importer associated with this data
  202. ImporterMap::iterator it = gActiveImports.find( pScene);
  203. // it should be there... else the user is playing fools with us
  204. if( it == gActiveImports.end())
  205. {
  206. ReportSceneNotFoundError();
  207. return;
  208. }
  209. // kill the importer, the data dies with it
  210. delete it->second;
  211. gActiveImports.erase( it);
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. // Returns the error text of the last failed import process.
  215. const char* aiGetErrorString()
  216. {
  217. return gLastErrorString.c_str();
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. // Returns the error text of the last failed import process.
  221. int aiIsExtensionSupported(const char* szExtension)
  222. {
  223. ai_assert(NULL != szExtension);
  224. // lock the mutex
  225. #if (defined AI_C_THREADSAFE)
  226. boost::mutex::scoped_lock lock(gMutex);
  227. #endif
  228. if (!gActiveImports.empty())
  229. {
  230. return (int)((*(gActiveImports.begin())).second->IsExtensionSupported(
  231. std::string ( szExtension )));
  232. }
  233. // need to create a temporary Importer instance.
  234. // TODO: Find a better solution ...
  235. Assimp::Importer* pcTemp = new Assimp::Importer();
  236. int i = (int)pcTemp->IsExtensionSupported(std::string ( szExtension ));
  237. delete pcTemp;
  238. return i;
  239. }
  240. // ------------------------------------------------------------------------------------------------
  241. // Get a list of all file extensions supported by ASSIMP
  242. void aiGetExtensionList(aiString* szOut)
  243. {
  244. ai_assert(NULL != szOut);
  245. // lock the mutex
  246. #if (defined AI_C_THREADSAFE)
  247. boost::mutex::scoped_lock lock(gMutex);
  248. #endif
  249. std::string szTemp;
  250. if (!gActiveImports.empty())
  251. {
  252. (*(gActiveImports.begin())).second->GetExtensionList(szTemp);
  253. szOut->Set ( szTemp );
  254. return;
  255. }
  256. // need to create a temporary Importer instance.
  257. // TODO: Find a better solution ...
  258. Assimp::Importer* pcTemp = new Assimp::Importer();
  259. pcTemp->GetExtensionList(szTemp);
  260. szOut->Set ( szTemp );
  261. delete pcTemp;
  262. }
  263. // ------------------------------------------------------------------------------------------------
  264. void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
  265. C_STRUCT aiMemoryInfo* in)
  266. {
  267. // lock the mutex
  268. #if (defined AI_C_THREADSAFE)
  269. boost::mutex::scoped_lock lock(gMutex);
  270. #endif
  271. // find the importer associated with this data
  272. ImporterMap::iterator it = gActiveImports.find( pIn);
  273. // it should be there... else the user is playing fools with us
  274. if( it == gActiveImports.end())
  275. {
  276. ReportSceneNotFoundError();
  277. return;
  278. }
  279. // get memory statistics
  280. it->second->GetMemoryRequirements(*in);
  281. }
  282. // ------------------------------------------------------------------------------------------------
  283. ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
  284. {
  285. SetGenericProperty<int>(gIntProperties,szName,value,NULL);
  286. }
  287. // ------------------------------------------------------------------------------------------------
  288. ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
  289. {
  290. SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
  291. }
  292. // ------------------------------------------------------------------------------------------------
  293. ASSIMP_API void aiSetImportPropertyString(const char* szName,
  294. const C_STRUCT aiString* st)
  295. {
  296. if (!st)return;
  297. SetGenericProperty<std::string>(gStringProperties,szName,
  298. std::string( st->data ),NULL);
  299. }