Assimp.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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. namespace Assimp
  49. {
  50. /** Stores the importer objects for all active import processes */
  51. typedef std::map<const aiScene*, Assimp::Importer*> ImporterMap;
  52. /** Stores the LogStream objects for all active C log streams */
  53. struct mpred {
  54. bool operator () (const aiLogStream& s0, const aiLogStream& s1) const {
  55. return s0.callback<s1.callback&&s0.user<s1.user;
  56. }
  57. };
  58. typedef std::map<aiLogStream, Assimp::LogStream*, mpred> LogStreamMap;
  59. /** Stores the LogStream objects allocated by #aiGetPredefinedLogStream */
  60. typedef std::list<Assimp::LogStream*> PredefLogStreamMap;
  61. /** Local storage of all active import processes */
  62. static ImporterMap gActiveImports;
  63. /** Local storage of all active log streams */
  64. static LogStreamMap gActiveLogStreams;
  65. /** Local storage of LogStreams allocated by #aiGetPredefinedLogStream */
  66. static PredefLogStreamMap gPredefinedStreams;
  67. /** Error message of the last failed import process */
  68. static std::string gLastErrorString;
  69. /** Verbose logging active or not? */
  70. static aiBool gVerboseLogging = false;
  71. }
  72. /** Configuration properties */
  73. static ImporterPimpl::IntPropertyMap gIntProperties;
  74. static ImporterPimpl::FloatPropertyMap gFloatProperties;
  75. static ImporterPimpl::StringPropertyMap gStringProperties;
  76. #ifdef AI_C_THREADSAFE
  77. /** Global mutex to manage the access to the importer map */
  78. static boost::mutex gMutex;
  79. /** Global mutex to manage the access to the logstream map */
  80. static boost::mutex gLogStreamMutex;
  81. #endif
  82. class CIOSystemWrapper;
  83. class CIOStreamWrapper;
  84. // ------------------------------------------------------------------------------------------------
  85. // Custom IOStream implementation for the C-API
  86. class CIOStreamWrapper : public IOStream
  87. {
  88. friend class CIOSystemWrapper;
  89. public:
  90. CIOStreamWrapper(aiFile* pFile)
  91. : mFile(pFile)
  92. {}
  93. // ...................................................................
  94. size_t Read(void* pvBuffer,
  95. size_t pSize,
  96. size_t pCount
  97. ){
  98. // need to typecast here as C has no void*
  99. return mFile->ReadProc(mFile,(char*)pvBuffer,pSize,pCount);
  100. }
  101. // ...................................................................
  102. size_t Write(const void* pvBuffer,
  103. size_t pSize,
  104. size_t pCount
  105. ){
  106. // need to typecast here as C has no void*
  107. return mFile->WriteProc(mFile,(const char*)pvBuffer,pSize,pCount);
  108. }
  109. // ...................................................................
  110. aiReturn Seek(size_t pOffset,
  111. aiOrigin pOrigin
  112. ){
  113. return mFile->SeekProc(mFile,pOffset,pOrigin);
  114. }
  115. // ...................................................................
  116. size_t Tell(void) const {
  117. return mFile->TellProc(mFile);
  118. }
  119. // ...................................................................
  120. size_t FileSize() const {
  121. return mFile->FileSizeProc(mFile);
  122. }
  123. // ...................................................................
  124. void Flush () {
  125. return mFile->FlushProc(mFile);
  126. }
  127. private:
  128. aiFile* mFile;
  129. };
  130. // ------------------------------------------------------------------------------------------------
  131. // Custom IOStream implementation for the C-API
  132. class CIOSystemWrapper : public IOSystem
  133. {
  134. public:
  135. CIOSystemWrapper(aiFileIO* pFile)
  136. : mFileSystem(pFile)
  137. {}
  138. // ...................................................................
  139. bool Exists( const char* pFile) const {
  140. CIOSystemWrapper* pip = const_cast<CIOSystemWrapper*>(this);
  141. IOStream* p = pip->Open(pFile);
  142. if (p){
  143. pip->Close(p);
  144. return true;
  145. }
  146. return false;
  147. }
  148. // ...................................................................
  149. char getOsSeparator() const {
  150. #ifndef _WIN32
  151. return '/';
  152. #else
  153. return '\\';
  154. #endif
  155. }
  156. // ...................................................................
  157. IOStream* Open(const char* pFile,const char* pMode = "rb") {
  158. aiFile* p = mFileSystem->OpenProc(mFileSystem,pFile,pMode);
  159. if (!p) {
  160. return NULL;
  161. }
  162. return new CIOStreamWrapper(p);
  163. }
  164. // ...................................................................
  165. void Close( IOStream* pFile) {
  166. if (!pFile) {
  167. return;
  168. }
  169. mFileSystem->CloseProc(mFileSystem,((CIOStreamWrapper*) pFile)->mFile);
  170. delete pFile;
  171. }
  172. private:
  173. aiFileIO* mFileSystem;
  174. };
  175. // ------------------------------------------------------------------------------------------------
  176. // Custom LogStream implementation for the C-API
  177. class LogToCallbackRedirector : public LogStream
  178. {
  179. public:
  180. LogToCallbackRedirector(const aiLogStream& s)
  181. : stream (s) {
  182. ai_assert(NULL != s.callback);
  183. }
  184. ~LogToCallbackRedirector() {
  185. #ifdef AI_C_THREADSAFE
  186. boost::mutex::scoped_lock lock(gLogStreamMutex);
  187. #endif
  188. // (HACK) Check whether the 'stream.user' pointer points to a
  189. // custom LogStream allocated by #aiGetPredefinedLogStream.
  190. // In this case, we need to delete it, too. Of course, this
  191. // might cause strange problems, but the chance is quite low.
  192. PredefLogStreamMap::iterator it = std::find(gPredefinedStreams.begin(),
  193. gPredefinedStreams.end(), (Assimp::LogStream*)stream.user);
  194. if (it != gPredefinedStreams.end()) {
  195. delete *it;
  196. gPredefinedStreams.erase(it);
  197. }
  198. }
  199. /** @copydoc LogStream::write */
  200. void write(const char* message) {
  201. stream.callback(message,stream.user);
  202. }
  203. private:
  204. aiLogStream stream;
  205. };
  206. // ------------------------------------------------------------------------------------------------
  207. void ReportSceneNotFoundError()
  208. {
  209. DefaultLogger::get()->error("Unable to find the Assimp::Importer for this aiScene. "
  210. "Are you playing fools with us? Don't mix cpp and c API. Thanks.");
  211. assert(false);
  212. }
  213. // ------------------------------------------------------------------------------------------------
  214. // Reads the given file and returns its content.
  215. const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
  216. {
  217. return aiImportFileEx(pFile,pFlags,NULL);
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. const aiScene* aiImportFileEx( const char* pFile, unsigned int pFlags,
  221. aiFileIO* pFS)
  222. {
  223. ai_assert(NULL != pFile);
  224. const aiScene* scene = NULL;
  225. ASSIMP_BEGIN_EXCEPTION_REGION();
  226. // create an Importer for this file
  227. Assimp::Importer* imp = new Assimp::Importer();
  228. #ifdef AI_C_THREADSAFE
  229. boost::mutex::scoped_lock lock(gMutex);
  230. #endif
  231. // copy the global property lists to the Importer instance
  232. imp->pimpl->mIntProperties = gIntProperties;
  233. imp->pimpl->mFloatProperties = gFloatProperties;
  234. imp->pimpl->mStringProperties = gStringProperties;
  235. #ifdef AI_C_THREADSAFE
  236. lock.unlock();
  237. #endif
  238. // setup a custom IO system if necessary
  239. if (pFS) {
  240. imp->SetIOHandler( new CIOSystemWrapper (pFS) );
  241. }
  242. // and have it read the file
  243. scene = imp->ReadFile( pFile, pFlags);
  244. // if succeeded, place it in the collection of active processes
  245. if( scene) {
  246. #ifdef AI_C_THREADSAFE
  247. lock.lock();
  248. #endif
  249. gActiveImports[scene] = imp;
  250. }
  251. else {
  252. // if failed, extract error code and destroy the import
  253. gLastErrorString = imp->GetErrorString();
  254. delete imp;
  255. }
  256. // return imported data. If the import failed the pointer is NULL anyways
  257. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  258. return scene;
  259. }
  260. // ------------------------------------------------------------------------------------------------
  261. const aiScene* aiImportFileFromMemory(
  262. const char* pBuffer,
  263. unsigned int pLength,
  264. unsigned int pFlags,
  265. const char* pHint)
  266. {
  267. ai_assert(NULL != pBuffer && 0 != pLength);
  268. const aiScene* scene = NULL;
  269. ASSIMP_BEGIN_EXCEPTION_REGION();
  270. // create an Importer for this file
  271. Assimp::Importer* imp = new Assimp::Importer();
  272. #ifdef AI_C_THREADSAFE
  273. boost::mutex::scoped_lock lock(gMutex);
  274. #endif
  275. // copy the global property lists to the Importer instance
  276. imp->pimpl->mIntProperties = gIntProperties;
  277. imp->pimpl->mFloatProperties = gFloatProperties;
  278. imp->pimpl->mStringProperties = gStringProperties;
  279. #ifdef AI_C_THREADSAFE
  280. lock.unlock();
  281. #endif
  282. // and have it read the file from the memory buffer
  283. scene = imp->ReadFileFromMemory( pBuffer, pLength, pFlags,pHint);
  284. // if succeeded, place it in the collection of active processes
  285. if( scene) {
  286. #ifdef AI_C_THREADSAFE
  287. lock.lock();
  288. #endif
  289. gActiveImports[scene] = imp;
  290. }
  291. else {
  292. // if failed, extract error code and destroy the import
  293. gLastErrorString = imp->GetErrorString();
  294. delete imp;
  295. }
  296. // return imported data. If the import failed the pointer is NULL anyways
  297. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  298. return scene;
  299. }
  300. // ------------------------------------------------------------------------------------------------
  301. // Releases all resources associated with the given import process.
  302. void aiReleaseImport( const aiScene* pScene)
  303. {
  304. if (!pScene) {
  305. return;
  306. }
  307. ASSIMP_BEGIN_EXCEPTION_REGION();
  308. #ifdef AI_C_THREADSAFE
  309. boost::mutex::scoped_lock lock(gMutex);
  310. #endif
  311. // find the importer associated with this data
  312. ImporterMap::iterator it = gActiveImports.find( pScene);
  313. // it should be there... else the user is playing fools with us
  314. if( it == gActiveImports.end()) {
  315. ReportSceneNotFoundError();
  316. return;
  317. }
  318. // kill the importer, the data dies with it
  319. delete it->second;
  320. gActiveImports.erase( it);
  321. ASSIMP_END_EXCEPTION_REGION(void);
  322. }
  323. // ------------------------------------------------------------------------------------------------
  324. ASSIMP_API const aiScene* aiApplyPostProcessing(const aiScene* pScene,
  325. unsigned int pFlags)
  326. {
  327. const aiScene* sc = NULL;
  328. ASSIMP_BEGIN_EXCEPTION_REGION();
  329. #ifdef AI_C_THREADSAFE
  330. boost::mutex::scoped_lock lock(gMutex);
  331. #endif
  332. // find the importer associated with this data
  333. ImporterMap::iterator it = gActiveImports.find( pScene);
  334. // it should be there... else the user is playing fools with us
  335. if( it == gActiveImports.end()) {
  336. ReportSceneNotFoundError();
  337. return NULL;
  338. }
  339. #ifdef AI_C_THREADSAFE
  340. lock.unlock();
  341. #endif
  342. sc = it->second->ApplyPostProcessing(pFlags);
  343. #ifdef AI_C_THREADSAFE
  344. lock.lock();
  345. #endif
  346. if (!sc) {
  347. // kill the importer, the data dies with it
  348. delete it->second;
  349. gActiveImports.erase( it);
  350. return NULL;
  351. }
  352. ASSIMP_END_EXCEPTION_REGION(const aiScene*);
  353. return sc;
  354. }
  355. // ------------------------------------------------------------------------------------------------
  356. void CallbackToLogRedirector (const char* msg, char* dt)
  357. {
  358. ai_assert(NULL != msg && NULL != dt);
  359. LogStream* s = (LogStream*)dt;
  360. s->write(msg);
  361. }
  362. // ------------------------------------------------------------------------------------------------
  363. ASSIMP_API aiLogStream aiGetPredefinedLogStream(aiDefaultLogStream pStream,const char* file)
  364. {
  365. aiLogStream sout;
  366. ASSIMP_BEGIN_EXCEPTION_REGION();
  367. LogStream* stream = LogStream::createDefaultStream(pStream,file);
  368. if (!stream) {
  369. sout.callback = NULL;
  370. sout.user = NULL;
  371. }
  372. else {
  373. sout.callback = &CallbackToLogRedirector;
  374. sout.user = (char*)stream;
  375. }
  376. gPredefinedStreams.push_back(stream);
  377. ASSIMP_END_EXCEPTION_REGION(aiLogStream);
  378. return sout;
  379. }
  380. // ------------------------------------------------------------------------------------------------
  381. ASSIMP_API void aiAttachLogStream( const aiLogStream* stream )
  382. {
  383. ASSIMP_BEGIN_EXCEPTION_REGION();
  384. #ifdef AI_C_THREADSAFE
  385. boost::mutex::scoped_lock lock(gLogStreamMutex);
  386. #endif
  387. LogStream* lg = new LogToCallbackRedirector(*stream);
  388. gActiveLogStreams[*stream] = lg;
  389. if (DefaultLogger::isNullLogger()) {
  390. DefaultLogger::create(NULL,(gVerboseLogging == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
  391. }
  392. DefaultLogger::get()->attachStream(lg);
  393. ASSIMP_END_EXCEPTION_REGION(void);
  394. }
  395. // ------------------------------------------------------------------------------------------------
  396. ASSIMP_API aiReturn aiDetachLogStream( const aiLogStream* stream)
  397. {
  398. ASSIMP_BEGIN_EXCEPTION_REGION();
  399. #ifdef AI_C_THREADSAFE
  400. boost::mutex::scoped_lock lock(gLogStreamMutex);
  401. #endif
  402. // find the logstream associated with this data
  403. LogStreamMap::iterator it = gActiveLogStreams.find( *stream);
  404. // it should be there... else the user is playing fools with us
  405. if( it == gActiveLogStreams.end()) {
  406. return AI_FAILURE;
  407. }
  408. DefaultLogger::get()->detatchStream( it->second );
  409. delete it->second;
  410. gActiveLogStreams.erase( it);
  411. if (gActiveLogStreams.empty()) {
  412. DefaultLogger::kill();
  413. }
  414. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  415. return AI_SUCCESS;
  416. }
  417. // ------------------------------------------------------------------------------------------------
  418. ASSIMP_API void aiDetachAllLogStreams(void)
  419. {
  420. ASSIMP_BEGIN_EXCEPTION_REGION();
  421. #ifdef AI_C_THREADSAFE
  422. boost::mutex::scoped_lock lock(gLogStreamMutex);
  423. #endif
  424. for (LogStreamMap::iterator it = gActiveLogStreams.begin(); it != gActiveLogStreams.end(); ++it) {
  425. DefaultLogger::get()->detatchStream( it->second );
  426. delete it->second;
  427. }
  428. gActiveLogStreams.clear();
  429. DefaultLogger::kill();
  430. ASSIMP_END_EXCEPTION_REGION(void);
  431. }
  432. // ------------------------------------------------------------------------------------------------
  433. ASSIMP_API void aiEnableVerboseLogging(aiBool d)
  434. {
  435. if (!DefaultLogger::isNullLogger()) {
  436. DefaultLogger::get()->setLogSeverity((d == AI_TRUE ? Logger::VERBOSE : Logger::NORMAL));
  437. }
  438. gVerboseLogging = d;
  439. }
  440. // ------------------------------------------------------------------------------------------------
  441. // Returns the error text of the last failed import process.
  442. const char* aiGetErrorString()
  443. {
  444. return gLastErrorString.c_str();
  445. }
  446. // ------------------------------------------------------------------------------------------------
  447. // Returns the error text of the last failed import process.
  448. aiBool aiIsExtensionSupported(const char* szExtension)
  449. {
  450. ai_assert(NULL != szExtension);
  451. aiBool candoit=AI_FALSE;
  452. ASSIMP_BEGIN_EXCEPTION_REGION();
  453. #ifdef AI_C_THREADSAFE
  454. boost::mutex::scoped_lock lock(gMutex);
  455. #endif
  456. if (!gActiveImports.empty()) {
  457. return ((*(gActiveImports.begin())).second->IsExtensionSupported( szExtension )) ? AI_TRUE : AI_FALSE;
  458. }
  459. // fixme: no need to create a temporary Importer instance just for that ..
  460. Assimp::Importer tmp;
  461. candoit = tmp.IsExtensionSupported(std::string(szExtension)) ? AI_TRUE : AI_FALSE;
  462. ASSIMP_END_EXCEPTION_REGION(aiBool);
  463. return candoit;
  464. }
  465. // ------------------------------------------------------------------------------------------------
  466. // Get a list of all file extensions supported by ASSIMP
  467. void aiGetExtensionList(aiString* szOut)
  468. {
  469. ai_assert(NULL != szOut);
  470. ASSIMP_BEGIN_EXCEPTION_REGION();
  471. #ifdef AI_C_THREADSAFE
  472. boost::mutex::scoped_lock lock(gMutex);
  473. #endif
  474. if (!gActiveImports.empty()) {
  475. (*(gActiveImports.begin())).second->GetExtensionList(*szOut);
  476. return;
  477. }
  478. // fixme: no need to create a temporary Importer instance just for that ..
  479. Assimp::Importer tmp;
  480. tmp.GetExtensionList(*szOut);
  481. ASSIMP_END_EXCEPTION_REGION(void);
  482. }
  483. // ------------------------------------------------------------------------------------------------
  484. // Get the memory requirements for a particular import.
  485. void aiGetMemoryRequirements(const C_STRUCT aiScene* pIn,
  486. C_STRUCT aiMemoryInfo* in)
  487. {
  488. ASSIMP_BEGIN_EXCEPTION_REGION();
  489. #ifdef AI_C_THREADSAFE
  490. boost::mutex::scoped_lock lock(gMutex);
  491. #endif
  492. // find the importer associated with this data
  493. ImporterMap::iterator it = gActiveImports.find( pIn);
  494. // it should be there... else the user is playing fools with us
  495. if( it == gActiveImports.end()) {
  496. ReportSceneNotFoundError();
  497. return;
  498. }
  499. // get memory statistics
  500. #ifdef AI_C_THREADSAFE
  501. lock.unlock();
  502. #endif
  503. it->second->GetMemoryRequirements(*in);
  504. ASSIMP_END_EXCEPTION_REGION(void);
  505. }
  506. // ------------------------------------------------------------------------------------------------
  507. // Importer::SetPropertyInteger
  508. ASSIMP_API void aiSetImportPropertyInteger(const char* szName, int value)
  509. {
  510. ASSIMP_BEGIN_EXCEPTION_REGION();
  511. #ifdef AI_C_THREADSAFE
  512. boost::mutex::scoped_lock lock(gMutex);
  513. #endif
  514. SetGenericProperty<int>(gIntProperties,szName,value,NULL);
  515. ASSIMP_END_EXCEPTION_REGION(void);
  516. }
  517. // ------------------------------------------------------------------------------------------------
  518. // Importer::SetPropertyFloat
  519. ASSIMP_API void aiSetImportPropertyFloat(const char* szName, float value)
  520. {
  521. ASSIMP_BEGIN_EXCEPTION_REGION();
  522. #ifdef AI_C_THREADSAFE
  523. boost::mutex::scoped_lock lock(gMutex);
  524. #endif
  525. SetGenericProperty<float>(gFloatProperties,szName,value,NULL);
  526. ASSIMP_END_EXCEPTION_REGION(void);
  527. }
  528. // ------------------------------------------------------------------------------------------------
  529. // Importer::SetPropertyString
  530. ASSIMP_API void aiSetImportPropertyString(const char* szName,
  531. const C_STRUCT aiString* st)
  532. {
  533. if (!st) {
  534. return;
  535. }
  536. ASSIMP_BEGIN_EXCEPTION_REGION();
  537. #ifdef AI_C_THREADSAFE
  538. boost::mutex::scoped_lock lock(gMutex);
  539. #endif
  540. SetGenericProperty<std::string>(gStringProperties,szName,
  541. std::string( st->data ),NULL);
  542. ASSIMP_END_EXCEPTION_REGION(void);
  543. }
  544. // ------------------------------------------------------------------------------------------------
  545. // Rotation matrix to quaternion
  546. ASSIMP_API void aiCreateQuaternionFromMatrix(aiQuaternion* quat,const aiMatrix3x3* mat)
  547. {
  548. ai_assert(NULL != quat && NULL != mat);
  549. *quat = aiQuaternion(*mat);
  550. }
  551. // ------------------------------------------------------------------------------------------------
  552. // Matrix decomposition
  553. ASSIMP_API void aiDecomposeMatrix(const aiMatrix4x4* mat,aiVector3D* scaling,
  554. aiQuaternion* rotation,
  555. aiVector3D* position)
  556. {
  557. ai_assert(NULL != rotation && NULL != position && NULL != scaling && NULL != mat);
  558. mat->Decompose(*scaling,*rotation,*position);
  559. }
  560. // ------------------------------------------------------------------------------------------------
  561. // Matrix transpose
  562. ASSIMP_API void aiTransposeMatrix3(aiMatrix3x3* mat)
  563. {
  564. ai_assert(NULL != mat);
  565. mat->Transpose();
  566. }
  567. // ------------------------------------------------------------------------------------------------
  568. ASSIMP_API void aiTransposeMatrix4(aiMatrix4x4* mat)
  569. {
  570. ai_assert(NULL != mat);
  571. mat->Transpose();
  572. }
  573. // ------------------------------------------------------------------------------------------------
  574. // Vector transformation
  575. ASSIMP_API void aiTransformVecByMatrix3(C_STRUCT aiVector3D* vec,
  576. const C_STRUCT aiMatrix3x3* mat)
  577. {
  578. ai_assert(NULL != mat && NULL != vec);
  579. *vec *= (*mat);
  580. }
  581. // ------------------------------------------------------------------------------------------------
  582. ASSIMP_API void aiTransformVecByMatrix4(C_STRUCT aiVector3D* vec,
  583. const C_STRUCT aiMatrix4x4* mat)
  584. {
  585. ai_assert(NULL != mat && NULL != vec);
  586. *vec *= (*mat);
  587. }
  588. // ------------------------------------------------------------------------------------------------
  589. // Matrix multiplication
  590. ASSIMP_API void aiMultiplyMatrix4(
  591. C_STRUCT aiMatrix4x4* dst,
  592. const C_STRUCT aiMatrix4x4* src)
  593. {
  594. ai_assert(NULL != dst && NULL != src);
  595. *dst = (*dst) * (*src);
  596. }
  597. // ------------------------------------------------------------------------------------------------
  598. ASSIMP_API void aiMultiplyMatrix3(
  599. C_STRUCT aiMatrix3x3* dst,
  600. const C_STRUCT aiMatrix3x3* src)
  601. {
  602. ai_assert(NULL != dst && NULL != src);
  603. *dst = (*dst) * (*src);
  604. }
  605. // ------------------------------------------------------------------------------------------------
  606. // Matrix identity
  607. ASSIMP_API void aiIdentityMatrix3(
  608. C_STRUCT aiMatrix3x3* mat)
  609. {
  610. ai_assert(NULL != mat);
  611. *mat = aiMatrix3x3();
  612. }
  613. // ------------------------------------------------------------------------------------------------
  614. ASSIMP_API void aiIdentityMatrix4(
  615. C_STRUCT aiMatrix4x4* mat)
  616. {
  617. ai_assert(NULL != mat);
  618. *mat = aiMatrix4x4();
  619. }