Assimp.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 Assimp.cpp
  35. * @brief Implementation of the Plain-C API
  36. */
  37. #include "AssimpPCH.h"
  38. #include "../include/assimp.h"
  39. #include "../include/aiFileIO.h"
  40. #include "GenericProperty.h"
  41. // ------------------------------------------------------------------------------------------------
  42. #ifdef AI_C_THREADSAFE
  43. # include <boost/thread/thread.hpp>
  44. # include <boost/thread/mutex.hpp>
  45. #endif
  46. // ------------------------------------------------------------------------------------------------
  47. using namespace Assimp;
  48. /** Stores the importer objects for all active import processes */
  49. typedef std::map<const aiScene*, Assimp::Importer*> ImporterMap;
  50. /** Stores the LogStream objects for all active C log streams */
  51. struct mpred {
  52. bool operator () (const aiLogStream& s0, const aiLogStream& s1) const {
  53. return s0.callback<s1.callback&&s0.user<s1.user;
  54. }
  55. };
  56. typedef std::map<aiLogStream, Assimp::LogStream*, mpred> LogStreamMap;
  57. /** Stores the LogStream objects allocated by #aiGetPredefinedLogStream */
  58. typedef std::list<Assimp::LogStream*> PredefLogStreamMap;
  59. /** Local storage of all active import processes */
  60. static ImporterMap gActiveImports;
  61. /** Local storage of all active log streams */
  62. static LogStreamMap gActiveLogStreams;
  63. /** Local storage of LogStreams allocated by #aiGetPredefinedLogStream */
  64. static PredefLogStreamMap gPredefinedStreams;
  65. /** Error message of the last failed import process */
  66. static std::string gLastErrorString;
  67. /** Verbose logging active or not? */
  68. static aiBool gVerboseLogging = false;
  69. /** Configuration properties */
  70. static ImporterPimpl::IntPropertyMap gIntProperties;
  71. static ImporterPimpl::FloatPropertyMap gFloatProperties;
  72. static ImporterPimpl::StringPropertyMap gStringProperties;
  73. #ifdef AI_C_THREADSAFE
  74. /** Global mutex to manage the access to the importer map */
  75. static boost::mutex gMutex;
  76. /** Global mutex to manage the access to the logstream map */
  77. static boost::mutex gLogStreamMutex;
  78. #endif
  79. class CIOSystemWrapper;
  80. class CIOStreamWrapper;
  81. // ------------------------------------------------------------------------------------------------
  82. // Custom IOStream implementation for the C-API
  83. class CIOStreamWrapper : public IOStream
  84. {
  85. friend class CIOSystemWrapper;
  86. public:
  87. CIOStreamWrapper(aiFile* pFile)
  88. : mFile(pFile)
  89. {}
  90. // ...................................................................
  91. size_t Read(void* pvBuffer,
  92. size_t pSize,
  93. size_t pCount
  94. ){
  95. // need to typecast here as C has no void*
  96. return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
  97. }
  98. // ...................................................................
  99. size_t Write(const void* pvBuffer,
  100. size_t pSize,
  101. size_t pCount
  102. ){
  103. // need to typecast here as C has no void*
  104. return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
  105. }
  106. // ...................................................................
  107. aiReturn Seek(size_t pOffset,
  108. aiOrigin pOrigin
  109. ){
  110. return mFile->SeekProc(mFile,pOffset,pOrigin);
  111. }
  112. // ...................................................................
  113. size_t Tell(void) const {
  114. return mFile->TellProc(mFile);
  115. }
  116. // ...................................................................
  117. size_t FileSize() const {
  118. return mFile->FileSizeProc(mFile);
  119. }
  120. // ...................................................................
  121. void Flush () {
  122. return mFile->FlushProc(mFile);
  123. }
  124. private:
  125. aiFile* mFile;
  126. };
  127. // ------------------------------------------------------------------------------------------------
  128. // Custom IOStream implementation for the C-API
  129. class CIOSystemWrapper : public IOSystem
  130. {
  131. public:
  132. CIOSystemWrapper(aiFileIO* pFile)
  133. : mFileSystem(pFile)
  134. {}
  135. // ...................................................................
  136. bool Exists( const char* pFile) const {
  137. CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
  138. IOStream* p = pip->Open(pFile);
  139. if (p){
  140. pip->Close(p);
  141. return true;
  142. }
  143. return false;
  144. }
  145. // ...................................................................
  146. char getOsSeparator() const {
  147. #ifndef _WIN32
  148. return '/';
  149. #else
  150. return '\\';
  151. #endif
  152. }
  153. // ...................................................................
  154. IOStream* Open(const char* pFile,const char* pMode = "rb") {
  155. aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
  156. if (!p) {
  157. return NULL;
  158. }
  159. return new CIOStreamWrapper(p);
  160. }
  161. // ...................................................................
  162. void Close( IOStream* pFile) {
  163. if (!pFile) {
  164. return;
  165. }
  166. mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
  167. delete pFile;
  168. }
  169. private:
  170. aiFileIO* mFileSystem;
  171. };
  172. // ------------------------------------------------------------------------------------------------
  173. // Custom LogStream implementation for the C-API
  174. class LogToCallbackRedirector : public LogStream
  175. {
  176. public:
  177. LogToCallbackRedirector(const aiLogStream& s)
  178. : stream (s) {
  179. ai_assert(NULL != s.callback);
  180. }
  181. ~LogToCallbackRedirector() {
  182. #ifdef AI_C_THREADSAFE
  183. boost::mutex::scoped_lock lock(gLogStreamMutex);
  184. #endif
  185. // (HACK) Check whether the 'stream.user' pointer points to a
  186. // custom LogStream allocated by #aiGetPredefinedLogStream.
  187. // In this case, we need to delete it, too. Of course, this
  188. // might cause strange problems, but the chance is quite low.
  189. PredefLogStreamMap::iterator it = std::find(gPredefinedStreams.begin(),
  190. gPredefinedStreams.end(), (Assimp::LogStream*)stream.user);
  191. if (it != gPredefinedStreams.end()) {
  192. delete *it;
  193. gPredefinedStreams.erase(it);
  194. }
  195. }
  196. /** @copydoc LogStream::write */
  197. void write(const char* message) {
  198. stream.callback(message,stream.user);
  199. }
  200. private:
  201. aiLogStream stream;
  202. };
  203. // ------------------------------------------------------------------------------------------------
  204. void ReportSceneNotFoundError()
  205. {
  206. DefaultLogger::get()->error("Unable to find the Assimp::Importer for this aiScene. "
  207. "Are you playing fools with us? Don't mix cpp and c API. Thanks.");
  208. assert(false);
  209. }
  210. // ------------------------------------------------------------------------------------------------
  211. // Reads the given file and returns its content.
  212. const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
  213. {
  214. return aiImportFileEx(pFile,pFlags,NULL);
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
  218. aiFileIO* pFS)
  219. {
  220. ai_assert(NULL != pFile);
  221. // create an Importer for this file
  222. Assimp::Importer* imp = new Assimp::Importer;
  223. #ifdef AI_C_THREADSAFE
  224. boost::mutex::scoped_lock lock(gMutex);
  225. #endif
  226. // copy the global property lists to the Importer instance
  227. imp->pimpl->mIntProperties = gIntProperties;
  228. imp->pimpl->mFloatProperties = gFloatProperties;
  229. imp->pimpl->mStringProperties = gStringProperties;
  230. #ifdef AI_C_THREADSAFE
  231. lock.unlock();
  232. #endif
  233. // setup a custom IO system if necessary
  234. if (pFS) {
  235. imp->SetIOHandler( new CIOSystemWrapper (pFS) );
  236. }
  237. // and have it read the file
  238. const aiScene* scene = imp->ReadFile( pFile, pFlags);
  239. // if succeeded, place it in the collection of active processes
  240. if( scene) {
  241. #ifdef AI_C_THREADSAFE
  242. lock.lock();
  243. #endif
  244. gActiveImports[scene] = imp;
  245. }
  246. else {
  247. // if failed, extract error code and destroy the import
  248. gLastErrorString = imp->GetErrorString();
  249. delete imp;
  250. }
  251. // return imported data. If the import failed the pointer is NULL anyways
  252. return scene;
  253. }
  254. // ------------------------------------------------------------------------------------------------
  255. // Releases all resources associated with the given import process.
  256. void aiReleaseImport( const aiScene* pScene)
  257. {
  258. if (!pScene) {
  259. return;
  260. }
  261. #ifdef AI_C_THREADSAFE
  262. boost::mutex::scoped_lock lock(gMutex);
  263. #endif
  264. // find the importer associated with this data
  265. ImporterMap::iterator it = gActiveImports.find( pScene);
  266. // it should be there... else the user is playing fools with us
  267. if( it == gActiveImports.end()) {
  268. ReportSceneNotFoundError();
  269. return;
  270. }
  271. // kill the importer, the data dies with it
  272. delete it->second;
  273. gActiveImports.erase( it);
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. ASSIMP_API const aiScene* aiApplyPostProcessing(const aiScene* pScene,
  277. unsigned int pFlags)
  278. {
  279. #ifdef AI_C_THREADSAFE
  280. boost::mutex::scoped_lock lock(gMutex);
  281. #endif
  282. // find the importer associated with this data
  283. ImporterMap::iterator it = gActiveImports.find( pScene);
  284. // it should be there... else the user is playing fools with us
  285. if( it == gActiveImports.end()) {
  286. ReportSceneNotFoundError();
  287. return NULL;
  288. }
  289. #ifdef AI_C_THREADSAFE
  290. lock.unlock();
  291. #endif
  292. const aiScene* sc = it->second->ApplyPostProcessing(pFlags);
  293. #ifdef AI_C_THREADSAFE
  294. lock.lock();
  295. #endif
  296. if (!sc) {
  297. // kill the importer, the data dies with it
  298. delete it->second;
  299. gActiveImports.erase( it);
  300. return NULL;
  301. }
  302. return it->first;
  303. }
  304. // ------------------------------------------------------------------------------------------------
  305. void CallbackToLogRedirector (const char* msg, char* dt)
  306. {
  307. ai_assert(NULL != msg && NULL != dt);
  308. LogStream* s = (LogStream*)dt;
  309. s->write(msg);
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. ASSIMP_API aiLogStream aiGetPredefinedLogStream(aiDefaultLogStream pStream,const char* file)
  313. {
  314. aiLogStream sout;
  315. LogStream* stream = LogStream::createDefaultStream(pStream,file);
  316. if (!stream) {
  317. sout.callback = NULL;
  318. }
  319. else {
  320. sout.callback = &CallbackToLogRedirector;
  321. sout.user = (char*)stream;
  322. }
  323. gPredefinedStreams.push_back(stream);
  324. return sout;
  325. }
  326. // ------------------------------------------------------------------------------------------------
  327. ASSIMP_API void aiAttachLogStream( const aiLogStream* stream )
  328. {
  329. #ifdef AI_C_THREADSAFE
  330. boost::mutex::scoped_lock lock(gLogStreamMutex);
  331. #endif
  332. LogStream* lg = new LogToCallbackRedirector(*stream);
  333. gActiveLogStreams[*stream] = lg;
  334. if (DefaultLogger::isNullLogger()) {
  335. DefaultLogger::create(NULL,(gVerboseLogging == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
  336. }
  337. DefaultLogger::get()->attachStream(lg);
  338. }
  339. // ------------------------------------------------------------------------------------------------
  340. ASSIMP_API aiReturn aiDetachLogStream( const aiLogStream* stream)
  341. {
  342. #ifdef AI_C_THREADSAFE
  343. boost::mutex::scoped_lock lock(gLogStreamMutex);
  344. #endif
  345. // find the logstream associated with this data
  346. LogStreamMap::iterator it = gActiveLogStreams.find( *stream);
  347. // it should be there... else the user is playing fools with us
  348. if( it == gActiveLogStreams.end()) {
  349. return AI_FAILURE;
  350. }
  351. delete it->second;
  352. gActiveLogStreams.erase( it);
  353. if (gActiveLogStreams.empty()) {
  354. DefaultLogger::kill();
  355. }
  356. return AI_SUCCESS;
  357. }
  358. // ------------------------------------------------------------------------------------------------
  359. ASSIMP_API void aiDetachAllLogStreams(void)
  360. {
  361. #ifdef AI_C_THREADSAFE
  362. boost::mutex::scoped_lock lock(gLogStreamMutex);
  363. #endif
  364. for (LogStreamMap::iterator it = gActiveLogStreams.begin(); it != gActiveLogStreams.end(); ++it) {
  365. delete it->second;
  366. }
  367. gActiveLogStreams.clear();
  368. DefaultLogger::kill();
  369. }
  370. // ------------------------------------------------------------------------------------------------
  371. ASSIMP_API void aiEnableVerboseLogging(aiBool d)
  372. {
  373. if (!DefaultLogger::isNullLogger()) {
  374. DefaultLogger::get()->setLogSeverity((d == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
  375. }
  376. gVerboseLogging = d;
  377. }
  378. // ------------------------------------------------------------------------------------------------
  379. // Returns the error text of the last failed import process.
  380. const char* aiGetErrorString()
  381. {
  382. return gLastErrorString.c_str();
  383. }
  384. // ------------------------------------------------------------------------------------------------
  385. // Returns the error text of the last failed import process.
  386. aiBool aiIsExtensionSupported(const char* szExtension)
  387. {
  388. ai_assert(NULL != szExtension);
  389. #ifdef AI_C_THREADSAFE
  390. boost::mutex::scoped_lock lock(gMutex);
  391. #endif
  392. if (!gActiveImports.empty()) {
  393. return ((*(gActiveImports.begin())).second->IsExtensionSupported( szExtension )) ? AI_TRUE : AI_FALSE;
  394. }
  395. // fixme: no need to create a temporary Importer instance just for that ..
  396. Assimp::Importer tmp;
  397. return tmp.IsExtensionSupported(std::string(szExtension)) ? AI_TRUE : AI_FALSE;
  398. }
  399. // ------------------------------------------------------------------------------------------------
  400. // Get a list of all file extensions supported by ASSIMP
  401. void aiGetExtensionList(aiString* szOut)
  402. {
  403. ai_assert(NULL != szOut);
  404. #ifdef AI_C_THREADSAFE
  405. boost::mutex::scoped_lock lock(gMutex);
  406. #endif
  407. if (!gActiveImports.empty())
  408. {
  409. (*(gActiveImports.begin())).second->GetExtensionList(*szOut);
  410. return;
  411. }
  412. // fixme: no need to create a temporary Importer instance just for that ..
  413. Assimp::Importer tmp;
  414. tmp.GetExtensionList(*szOut);
  415. }
  416. // ------------------------------------------------------------------------------------------------
  417. // Get the memory requirements for a particular import.
  418. void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
  419. C_STRUCT aiMemoryInfo* in)
  420. {
  421. #ifdef AI_C_THREADSAFE
  422. boost::mutex::scoped_lock lock(gMutex);
  423. #endif
  424. // find the importer associated with this data
  425. ImporterMap::iterator it = gActiveImports.find( pIn);
  426. // it should be there... else the user is playing fools with us
  427. if( it == gActiveImports.end()) {
  428. ReportSceneNotFoundError();
  429. return;
  430. }
  431. // get memory statistics
  432. #ifdef AI_C_THREADSAFE
  433. lock.unlock();
  434. #endif
  435. it->second->GetMemoryRequirements(*in);
  436. }
  437. // ------------------------------------------------------------------------------------------------
  438. // Importer::SetPropertyInteger
  439. ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
  440. {
  441. #ifdef AI_C_THREADSAFE
  442. boost::mutex::scoped_lock lock(gMutex);
  443. #endif
  444. SetGenericProperty<int>(gIntProperties,szName,value,NULL);
  445. }
  446. // ------------------------------------------------------------------------------------------------
  447. // Importer::SetPropertyFloat
  448. ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
  449. {
  450. #ifdef AI_C_THREADSAFE
  451. boost::mutex::scoped_lock lock(gMutex);
  452. #endif
  453. SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
  454. }
  455. // ------------------------------------------------------------------------------------------------
  456. // Importer::SetPropertyString
  457. ASSIMP_API void aiSetImportPropertyString(const char* szName,
  458. const C_STRUCT aiString* st)
  459. {
  460. if (!st) {
  461. return;
  462. }
  463. #ifdef AI_C_THREADSAFE
  464. boost::mutex::scoped_lock lock(gMutex);
  465. #endif
  466. SetGenericProperty<std::string>(gStringProperties,szName,
  467. std::string( st->data ),NULL);
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. // Rotation matrix to quaternion
  471. ASSIMP_API void aiCreateQuaternionFromMatrix(aiQuaternion* quat,const aiMatrix3x3* mat)
  472. {
  473. ai_assert(NULL != quat && NULL != mat);
  474. *quat = aiQuaternion(*mat);
  475. }
  476. // ------------------------------------------------------------------------------------------------
  477. // Affline matrix decomposition
  478. ASSIMP_API void aiDecomposeMatrix(const aiMatrix4x4* mat,aiVector3D* scaling,
  479. aiQuaternion* rotation,
  480. aiVector3D* position)
  481. {
  482. ai_assert(NULL != rotation && NULL != position && NULL != scaling && NULL != mat);
  483. mat->Decompose(*scaling,*rotation,*position);
  484. }