Assimp.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <map>
  36. #include "../include/assimp.h"
  37. #include "../include/assimp.hpp"
  38. #include "../include/aiAssert.h"
  39. using namespace Assimp;
  40. #if (defined AI_C_THREADSAFE)
  41. # include <boost/thread/thread.hpp>
  42. # include <boost/thread/mutex.hpp>
  43. #endif
  44. /** Stores the importer objects for all active import processes */
  45. typedef std::map< const aiScene*, Assimp::Importer* > ImporterMap;
  46. /** Local storage of all active import processes */
  47. static ImporterMap gActiveImports;
  48. /** Error message of the last failed import process */
  49. static std::string gLastErrorString;
  50. #if (defined AI_C_THREADSAFE)
  51. /** Global mutex to manage the access to the importer map */
  52. static boost::mutex gMutex;
  53. #endif
  54. // ------------------------------------------------------------------------------------------------
  55. // Reads the given file and returns its content.
  56. const aiScene* aiImportFile( const char* pFile, unsigned int pFlags)
  57. {
  58. ai_assert(NULL != pFile);
  59. // create an Importer for this file
  60. Assimp::Importer* imp = new Assimp::Importer;
  61. // and have it read the file
  62. const aiScene* scene = imp->ReadFile( pFile, pFlags);
  63. // if succeeded, place it in the collection of active processes
  64. if( scene)
  65. {
  66. #if (defined AI_C_THREADSAFE)
  67. boost::mutex::scoped_lock lock(gMutex);
  68. #endif
  69. gActiveImports[scene] = imp;
  70. }
  71. else
  72. {
  73. // if failed, extract error code and destroy the import
  74. gLastErrorString = imp->GetErrorString();
  75. delete imp;
  76. }
  77. // return imported data. If the import failed the pointer is NULL anyways
  78. return scene;
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Releases all resources associated with the given import process.
  82. void aiReleaseImport( const aiScene* pScene)
  83. {
  84. if (!pScene)return;
  85. // lock the mutex
  86. #if (defined AI_C_THREADSAFE)
  87. boost::mutex::scoped_lock lock(gMutex);
  88. #endif
  89. // find the importer associated with this data
  90. ImporterMap::iterator it = gActiveImports.find( pScene);
  91. // it should be there... else the user is playing fools with us
  92. if( it == gActiveImports.end())
  93. return;
  94. // kill the importer, the data dies with it
  95. delete it->second;
  96. gActiveImports.erase( it);
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Returns the error text of the last failed import process.
  100. const char* aiGetErrorString()
  101. {
  102. return gLastErrorString.c_str();
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Returns the error text of the last failed import process.
  106. int aiIsExtensionSupported(const char* szExtension)
  107. {
  108. ai_assert(NULL != szExtension);
  109. // lock the mutex
  110. #if (defined AI_C_THREADSAFE)
  111. boost::mutex::scoped_lock lock(gMutex);
  112. #endif
  113. if (!gActiveImports.empty())
  114. {
  115. return (int)((*(gActiveImports.begin())).second->IsExtensionSupported(
  116. std::string ( szExtension )));
  117. }
  118. // need to create a temporary Importer instance.
  119. // TODO: Find a better solution ...
  120. Assimp::Importer* pcTemp = new Assimp::Importer();
  121. int i = (int)pcTemp->IsExtensionSupported(std::string ( szExtension ));
  122. delete pcTemp;
  123. return i;
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. // Get a list of all file extensions supported by ASSIMP
  127. void aiGetExtensionList(aiString* szOut)
  128. {
  129. ai_assert(NULL != szOut);
  130. // lock the mutex
  131. #if (defined AI_C_THREADSAFE)
  132. boost::mutex::scoped_lock lock(gMutex);
  133. #endif
  134. std::string szTemp;
  135. if (!gActiveImports.empty())
  136. {
  137. (*(gActiveImports.begin())).second->GetExtensionList(szTemp);
  138. szOut->Set ( szTemp );
  139. return;
  140. }
  141. // need to create a temporary Importer instance.
  142. // TODO: Find a better solution ...
  143. Assimp::Importer* pcTemp = new Assimp::Importer();
  144. pcTemp->GetExtensionList(szTemp);
  145. szOut->Set ( szTemp );
  146. delete pcTemp;
  147. }