Importer.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Importer.h mostly internal stuff for use by #Assimp::Importer */
  34. #pragma once
  35. #ifndef INCLUDED_AI_IMPORTER_H
  36. #define INCLUDED_AI_IMPORTER_H
  37. #include <exception>
  38. #include <map>
  39. #include <vector>
  40. #include <string>
  41. #include <assimp/matrix4x4.h>
  42. struct aiScene;
  43. namespace Assimp {
  44. class ProgressHandler;
  45. class IOSystem;
  46. class BaseImporter;
  47. class BaseProcess;
  48. class SharedPostProcessInfo;
  49. //! @cond never
  50. // ---------------------------------------------------------------------------
  51. /** @brief Internal PIMPL implementation for Assimp::Importer
  52. *
  53. * Using this idiom here allows us to drop the dependency from
  54. * std::vector and std::map in the public headers. Furthermore we are dropping
  55. * any STL interface problems caused by mismatching STL settings. All
  56. * size calculation are now done by us, not the app heap. */
  57. class ImporterPimpl {
  58. public:
  59. // Data type to store the key hash
  60. typedef unsigned int KeyType;
  61. // typedefs for our configuration maps.
  62. using IntPropertyMap = std::map<KeyType, int>;
  63. using FloatPropertyMap = std::map<KeyType, ai_real>;
  64. using StringPropertyMap = std::map<KeyType, std::string>;
  65. using MatrixPropertyMap = std::map<KeyType, aiMatrix4x4>;
  66. using PointerPropertyMap = std::map<KeyType, void*>;
  67. /** IO handler to use for all file accesses. */
  68. IOSystem* mIOHandler;
  69. bool mIsDefaultHandler;
  70. /** Progress handler for feedback. */
  71. ProgressHandler* mProgressHandler;
  72. bool mIsDefaultProgressHandler;
  73. /** Format-specific importer worker objects - one for each format we can read.*/
  74. std::vector< BaseImporter* > mImporter;
  75. /** Post processing steps we can apply at the imported data. */
  76. std::vector< BaseProcess* > mPostProcessingSteps;
  77. /** The imported data, if ReadFile() was successful, nullptr otherwise. */
  78. aiScene* mScene;
  79. /** The error description, if there was one. In the case of an exception,
  80. * mException will carry the full details. */
  81. std::string mErrorString;
  82. /** Any exception which occurred */
  83. std::exception_ptr mException;
  84. /** List of integer properties */
  85. IntPropertyMap mIntProperties;
  86. /** List of floating-point properties */
  87. FloatPropertyMap mFloatProperties;
  88. /** List of string properties */
  89. StringPropertyMap mStringProperties;
  90. /** List of Matrix properties */
  91. MatrixPropertyMap mMatrixProperties;
  92. /** List of pointer properties */
  93. PointerPropertyMap mPointerProperties;
  94. /** Used for testing - extra verbose mode causes the ValidateDataStructure-Step
  95. * to be executed before and after every single post-process step */
  96. bool bExtraVerbose;
  97. /** Used by post-process steps to share data */
  98. SharedPostProcessInfo* mPPShared;
  99. /// The default class constructor.
  100. ImporterPimpl() AI_NO_EXCEPT;
  101. /// The class destructor.
  102. ~ImporterPimpl() = default;
  103. };
  104. inline ImporterPimpl::ImporterPimpl() AI_NO_EXCEPT :
  105. mIOHandler( nullptr ),
  106. mIsDefaultHandler( false ),
  107. mProgressHandler( nullptr ),
  108. mIsDefaultProgressHandler( false ),
  109. mImporter(),
  110. mPostProcessingSteps(),
  111. mScene( nullptr ),
  112. mErrorString(),
  113. mException(),
  114. mIntProperties(),
  115. mFloatProperties(),
  116. mStringProperties(),
  117. mMatrixProperties(),
  118. mPointerProperties(),
  119. bExtraVerbose( false ),
  120. mPPShared( nullptr ) {
  121. // empty
  122. }
  123. //! @endcond
  124. struct BatchData;
  125. // ---------------------------------------------------------------------------
  126. /** FOR IMPORTER PLUGINS ONLY: A helper class to the pleasure of importers
  127. * that need to load many external meshes recursively.
  128. *
  129. * The class uses several threads to load these meshes (or at least it
  130. * could, this has not yet been implemented at the moment).
  131. *
  132. * @note The class may not be used by more than one thread*/
  133. class ASSIMP_API BatchLoader {
  134. public:
  135. //! @cond never
  136. // -------------------------------------------------------------------
  137. /** Wraps a full list of configuration properties for an importer.
  138. * Properties can be set using SetGenericProperty */
  139. struct PropertyMap {
  140. ImporterPimpl::IntPropertyMap ints;
  141. ImporterPimpl::FloatPropertyMap floats;
  142. ImporterPimpl::StringPropertyMap strings;
  143. ImporterPimpl::MatrixPropertyMap matrices;
  144. bool operator == (const PropertyMap& prop) const {
  145. // fixme: really isocpp? gcc complains
  146. return ints == prop.ints && floats == prop.floats && strings == prop.strings && matrices == prop.matrices;
  147. }
  148. bool empty () const {
  149. return ints.empty() && floats.empty() && strings.empty() && matrices.empty();
  150. }
  151. };
  152. //! @endcond
  153. // -------------------------------------------------------------------
  154. /** Construct a batch loader from a given IO system to be used
  155. * to access external files
  156. */
  157. explicit BatchLoader(IOSystem* pIO, bool validate = false );
  158. // -------------------------------------------------------------------
  159. /** The class destructor.
  160. */
  161. ~BatchLoader();
  162. // -------------------------------------------------------------------
  163. /** Sets the validation step. True for enable validation during postprocess.
  164. * @param enable True for validation.
  165. */
  166. void setValidation( bool enabled );
  167. // -------------------------------------------------------------------
  168. /** Returns the current validation step.
  169. * @return The current validation step.
  170. */
  171. bool getValidation() const;
  172. // -------------------------------------------------------------------
  173. /** Add a new file to the list of files to be loaded.
  174. * @param file File to be loaded
  175. * @param steps Post-processing steps to be executed on the file
  176. * @param map Optional configuration properties
  177. * @return 'Load request channel' - an unique ID that can later
  178. * be used to access the imported file data.
  179. * @see GetImport */
  180. unsigned int AddLoadRequest (
  181. const std::string& file,
  182. unsigned int steps = 0,
  183. const PropertyMap *map = nullptr
  184. );
  185. // -------------------------------------------------------------------
  186. /** Get an imported scene.
  187. * This polls the import from the internal request list.
  188. * If an import is requested several times, this function
  189. * can be called several times, too.
  190. *
  191. * @param which LRWC returned by AddLoadRequest().
  192. * @return nullptr if there is no scene with this file name
  193. * in the queue of the scene hasn't been loaded yet. */
  194. aiScene* GetImport(
  195. unsigned int which
  196. );
  197. // -------------------------------------------------------------------
  198. /** Waits until all scenes have been loaded. This returns
  199. * immediately if no scenes are queued.*/
  200. void LoadAll();
  201. private:
  202. // No need to have that in the public API ...
  203. BatchData *m_data;
  204. };
  205. } // Namespace Assimp
  206. #endif // INCLUDED_AI_IMPORTER_H