Assimp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 ImporterPimpl::IntPropertyMap gIntProperties;
  53. static ImporterPimpl::FloatPropertyMap gFloatProperties;
  54. static ImporterPimpl::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. // -------------------------------------------------------------------
  103. void Flush ()
  104. {
  105. return mFile->FlushProc(mFile);
  106. }
  107. private:
  108. aiFile* mFile;
  109. };
  110. // ------------------------------------------------------------------------------------------------
  111. // Custom IOStream implementation for the C-API
  112. class CIOSystemWrapper : public IOSystem
  113. {
  114. public:
  115. CIOSystemWrapper(aiFileIO* pFile)
  116. : mFileSystem(pFile)
  117. {}
  118. // -------------------------------------------------------------------
  119. bool Exists( const char* pFile) const
  120. {
  121. CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
  122. IOStream* p = pip->Open(pFile);
  123. if (p){
  124. pip->Close(p);
  125. return true;
  126. }
  127. return false;
  128. }
  129. // -------------------------------------------------------------------
  130. char getOsSeparator() const
  131. {
  132. // FIXME
  133. return '/';
  134. }
  135. // -------------------------------------------------------------------
  136. IOStream* Open(const char* pFile,const char* pMode = "rb")
  137. {
  138. aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
  139. if (!p)return NULL;
  140. return new CIOStreamWrapper(p);
  141. }
  142. // -------------------------------------------------------------------
  143. void Close( IOStream* pFile)
  144. {
  145. if (!pFile)return;
  146. mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
  147. delete pFile;
  148. }
  149. private:
  150. aiFileIO* mFileSystem;
  151. };
  152. // ------------------------------------------------------------------------------------------------
  153. void ReportSceneNotFoundError()
  154. {
  155. DefaultLogger::get()->error("Unable to find the Importer instance for this scene. "
  156. "Are you sure it has been created by aiImportFile(ex)(...)?");
  157. }
  158. // ------------------------------------------------------------------------------------------------
  159. // Reads the given file and returns its content.
  160. const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
  161. {
  162. return aiImportFileEx(pFile,pFlags,NULL);
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
  166. aiFileIO* pFS)
  167. {
  168. ai_assert(NULL != pFile);
  169. // create an Importer for this file
  170. Assimp::Importer* imp = new Assimp::Importer;
  171. // copy the global property lists to the Importer instance
  172. // (we are a friend of Importer)
  173. imp->pimpl->mIntProperties = gIntProperties;
  174. imp->pimpl->mFloatProperties = gFloatProperties;
  175. imp->pimpl->mStringProperties = gStringProperties;
  176. // setup a custom IO system if necessary
  177. if (pFS)
  178. {
  179. imp->SetIOHandler( new CIOSystemWrapper (pFS) );
  180. }
  181. // and have it read the file
  182. const aiScene* scene = imp->ReadFile( pFile, pFlags);
  183. // if succeeded, place it in the collection of active processes
  184. if( scene)
  185. {
  186. #if (defined AI_C_THREADSAFE)
  187. boost::mutex::scoped_lock lock(gMutex);
  188. #endif
  189. gActiveImports[scene] = imp;
  190. }
  191. else
  192. {
  193. // if failed, extract error code and destroy the import
  194. gLastErrorString = imp->GetErrorString();
  195. delete imp;
  196. }
  197. // return imported data. If the import failed the pointer is NULL anyways
  198. return scene;
  199. }
  200. // ------------------------------------------------------------------------------------------------
  201. // Releases all resources associated with the given import process.
  202. void aiReleaseImport( const aiScene* pScene)
  203. {
  204. if (!pScene)return;
  205. // lock the mutex
  206. #if (defined AI_C_THREADSAFE)
  207. boost::mutex::scoped_lock lock(gMutex);
  208. #endif
  209. // find the importer associated with this data
  210. ImporterMap::iterator it = gActiveImports.find( pScene);
  211. // it should be there... else the user is playing fools with us
  212. if( it == gActiveImports.end())
  213. {
  214. ReportSceneNotFoundError();
  215. return;
  216. }
  217. // kill the importer, the data dies with it
  218. delete it->second;
  219. gActiveImports.erase( it);
  220. }
  221. // ------------------------------------------------------------------------------------------------
  222. // Returns the error text of the last failed import process.
  223. const char* aiGetErrorString()
  224. {
  225. return gLastErrorString.c_str();
  226. }
  227. // ------------------------------------------------------------------------------------------------
  228. // Returns the error text of the last failed import process.
  229. int aiIsExtensionSupported(const char* szExtension)
  230. {
  231. ai_assert(NULL != szExtension);
  232. // lock the mutex
  233. #if (defined AI_C_THREADSAFE)
  234. boost::mutex::scoped_lock lock(gMutex);
  235. #endif
  236. if (!gActiveImports.empty()) {
  237. return (int)((*(gActiveImports.begin())).second->IsExtensionSupported( szExtension ));
  238. }
  239. // need to create a temporary Importer instance.
  240. // TODO: Find a better solution ...
  241. Assimp::Importer* pcTemp = new Assimp::Importer();
  242. int i = (int)pcTemp->IsExtensionSupported(std::string(szExtension));
  243. delete pcTemp;
  244. return i;
  245. }
  246. // ------------------------------------------------------------------------------------------------
  247. // Get a list of all file extensions supported by ASSIMP
  248. void aiGetExtensionList(aiString* szOut)
  249. {
  250. ai_assert(NULL != szOut);
  251. // lock the mutex
  252. #if (defined AI_C_THREADSAFE)
  253. boost::mutex::scoped_lock lock(gMutex);
  254. #endif
  255. if (!gActiveImports.empty())
  256. {
  257. (*(gActiveImports.begin())).second->GetExtensionList(*szOut);
  258. return;
  259. }
  260. // need to create a temporary Importer instance.
  261. // TODO: Find a better solution ...
  262. Assimp::Importer* pcTemp = new Assimp::Importer();
  263. pcTemp->GetExtensionList(*szOut);
  264. delete pcTemp;
  265. }
  266. // ------------------------------------------------------------------------------------------------
  267. void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
  268. C_STRUCT aiMemoryInfo* in)
  269. {
  270. // lock the mutex
  271. #if (defined AI_C_THREADSAFE)
  272. boost::mutex::scoped_lock lock(gMutex);
  273. #endif
  274. // find the importer associated with this data
  275. ImporterMap::iterator it = gActiveImports.find( pIn);
  276. // it should be there... else the user is playing fools with us
  277. if( it == gActiveImports.end())
  278. {
  279. ReportSceneNotFoundError();
  280. return;
  281. }
  282. // get memory statistics
  283. it->second->GetMemoryRequirements(*in);
  284. }
  285. // ------------------------------------------------------------------------------------------------
  286. ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
  287. {
  288. SetGenericProperty<int>(gIntProperties,szName,value,NULL);
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
  292. {
  293. SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
  294. }
  295. // ------------------------------------------------------------------------------------------------
  296. ASSIMP_API void aiSetImportPropertyString(const char* szName,
  297. const C_STRUCT aiString* st)
  298. {
  299. if (!st)return;
  300. SetGenericProperty<std::string>(gStringProperties,szName,
  301. std::string( st->data ),NULL);
  302. }