BaseImporter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Definition of the base class for all importer worker classes. */
  34. #ifndef AI_BASEIMPORTER_H_INC
  35. #define AI_BASEIMPORTER_H_INC
  36. #include <string>
  37. #include "./../include/aiTypes.h"
  38. struct aiScene;
  39. namespace Assimp {
  40. class IOSystem;
  41. class Importer;
  42. // ---------------------------------------------------------------------------
  43. /** Simple exception class to be thrown if an error occurs while importing. */
  44. class ASSIMP_API ImportErrorException
  45. {
  46. public:
  47. /** Constructor with arguments */
  48. ImportErrorException( const std::string& pErrorText)
  49. {
  50. mErrorText = pErrorText;
  51. }
  52. // -------------------------------------------------------------------
  53. /** Returns the error text provided when throwing the exception */
  54. inline const std::string& GetErrorText() const
  55. { return mErrorText; }
  56. private:
  57. std::string mErrorText;
  58. };
  59. // ---------------------------------------------------------------------------
  60. /** The BaseImporter defines a common interface for all importer worker
  61. * classes.
  62. *
  63. * The interface defines two functions: CanRead() is used to check if the
  64. * importer can handle the format of the given file. If an implementation of
  65. * this function returns true, the importer then calls ReadFile() which
  66. * imports the given file. ReadFile is not overridable, it just calls
  67. * InternReadFile() and catches any ImportErrorException that might occur.
  68. */
  69. class ASSIMP_API BaseImporter
  70. {
  71. friend class Importer;
  72. protected:
  73. /** Constructor to be privately used by #Importer */
  74. BaseImporter();
  75. /** Destructor, private as well */
  76. virtual ~BaseImporter();
  77. public:
  78. // -------------------------------------------------------------------
  79. /** Returns whether the class can handle the format of the given file.
  80. * @param pFile Path and file name of the file to be examined.
  81. * @param pIOHandler The IO handler to use for accessing any file.
  82. * @return true if the class can read this file, false if not.
  83. *
  84. * @note Sometimes ASSIMP uses this method to determine whether a
  85. * a given file extension is generally supported. In this case the
  86. * file extension is passed in the pFile parameter, pIOHandler is NULL
  87. */
  88. virtual bool CanRead( const std::string& pFile,
  89. IOSystem* pIOHandler) const = 0;
  90. // -------------------------------------------------------------------
  91. /** Imports the given file and returns the imported data.
  92. * If the import succeeds, ownership of the data is transferred to
  93. * the caller. If the import fails, NULL is returned. The function
  94. * takes care that any partially constructed data is destroyed
  95. * beforehand.
  96. *
  97. * @param pFile Path of the file to be imported.
  98. * @param pIOHandler IO-Handler used to open this and possible other files.
  99. * @return The imported data or NULL if failed. If it failed a
  100. * human-readable error description can be retrieved by calling
  101. * GetErrorText()
  102. *
  103. * @note This function is not intended to be overridden. Implement
  104. * InternReadFile() to do the import. If an exception is thrown somewhere
  105. * in InternReadFile(), this function will catch it and transform it into
  106. * a suitable response to the caller.
  107. */
  108. aiScene* ReadFile( const std::string& pFile, IOSystem* pIOHandler);
  109. // -------------------------------------------------------------------
  110. /** Returns the error description of the last error that occured.
  111. * @return A description of the last error that occured. An empty
  112. * string if there was no error.
  113. */
  114. inline const std::string& GetErrorText() const
  115. { return mErrorText; }
  116. // -------------------------------------------------------------------
  117. /** Called prior to ReadFile().
  118. * The function is a request to the importer to update its configuration
  119. * basing on the Importer's configuration property list.
  120. * @param pImp Importer instance
  121. * @param ppFlags Post-processing steps to be executed on the data
  122. * returned by the loaders. This value is provided to allow some
  123. * internal optimizations.
  124. */
  125. virtual void SetupProperties(const Importer* pImp /*,
  126. unsigned int ppFlags*/);
  127. protected:
  128. // -------------------------------------------------------------------
  129. /** Called by Importer::GetExtensionList() for each loaded importer.
  130. * Importer implementations should append all file extensions
  131. * which they supported to the passed string.
  132. * Example: "*.blabb;*.quak;*.gug;*.foo" (no comma after the last!)
  133. * @param append Output string
  134. */
  135. virtual void GetExtensionList(std::string& append) = 0;
  136. // -------------------------------------------------------------------
  137. /** Imports the given file into the given scene structure. The
  138. * function is expected to throw an ImportErrorException if there is
  139. * an error. If it terminates normally, the data in aiScene is
  140. * expected to be correct. Override this function to implement the
  141. * actual importing.
  142. * <br>
  143. * The output scene must meet the following requirements:<br>
  144. * - at least a root node must be there<br>
  145. * - aiMesh::mPrimitiveTypes may be 0. The types of primitives
  146. * in the mesh are determined automatically in this case.<br>
  147. * - the vertex data is stored in a pseudo-indexed "verbose" format.
  148. * In fact this means that every vertex that is referenced by
  149. * a face is unique. Or the other way round: a vertex index may
  150. * not occur twice in a single aiMesh.
  151. * - aiAnimation::mDuration may be -1. Assimp determines the length
  152. * of the animation automatically in this case as the length of
  153. * the longest animation channel.
  154. *
  155. * If the AI_SCENE_FLAGS_INCOMPLETE-Flag is not set:<br>
  156. * - at least one mesh must be there<br>
  157. * - at least one material must be there<br>
  158. * - there may be no meshes with 0 vertices or faces<br>
  159. * This won't be checked (except by the validation step), Assimp will
  160. * crash if one of the conditions is not met!
  161. *
  162. * @param pFile Path of the file to be imported.
  163. * @param pScene The scene object to hold the imported data.
  164. * NULL is not a valid parameter.
  165. * @param pIOHandler The IO handler to use for any file access.
  166. * NULL is not a valid parameter.
  167. */
  168. virtual void InternReadFile( const std::string& pFile,
  169. aiScene* pScene, IOSystem* pIOHandler) = 0;
  170. // -------------------------------------------------------------------
  171. /** A utility for CanRead().
  172. *
  173. * The function searches the header of a file for a specific token
  174. * and returns true if this token is found. This works for text
  175. * files only. There is a rudimentary handling if UNICODE files.
  176. * The comparison is case independent.
  177. *
  178. * @param pIOSystem IO System to work with
  179. * @param file File name of the file
  180. * @param tokens List of tokens to search for
  181. * @param numTokens Size of the token array
  182. * @param searchBytes Number of bytes to be searched for the tokens.
  183. */
  184. static bool SearchFileHeaderForToken(IOSystem* pIOSystem,
  185. const std::string& file,
  186. const char** tokens,
  187. unsigned int numTokens,
  188. unsigned int searchBytes = 200);
  189. #if 0 /** TODO **/
  190. // -------------------------------------------------------------------
  191. /** An utility for all text file loaders. It converts a file to our
  192. * ASCII/UTF8 character set. Special unicode characters are lost.
  193. *
  194. * @param buffer Input buffer. Needn't be terminated with zero.
  195. * @param length Length of the input buffer, in bytes. Receives the
  196. * number of output characters, excluding the terminal char.
  197. * @return true if the source format did not match our internal
  198. * format so it was converted.
  199. */
  200. static bool ConvertToUTF8(const char* buffer,
  201. unsigned int& length);
  202. #endif
  203. protected:
  204. /** Error description in case there was one. */
  205. std::string mErrorText;
  206. };
  207. // ---------------------------------------------------------------------------
  208. /** A helper class that can be used by importers which need to load many
  209. * extern meshes recursively.
  210. *
  211. * The class uses several threads to load these meshes (or at least it
  212. * could, this has not yet been implemented at the moment).
  213. *
  214. * @note The class may not be used by more than one thread
  215. */
  216. class ASSIMP_API BatchLoader
  217. {
  218. // friend of Importer
  219. public:
  220. /** Represents a full list of configuration properties
  221. * for the importer.
  222. *
  223. * Properties can be set using SetGenericProperty
  224. */
  225. struct PropertyMap
  226. {
  227. Importer::IntPropertyMap ints;
  228. Importer::FloatPropertyMap floats;
  229. Importer::StringPropertyMap strings;
  230. };
  231. public:
  232. /** Construct a batch loader from a given IO system
  233. */
  234. BatchLoader(IOSystem* pIO);
  235. ~BatchLoader();
  236. /** Sets the base path to be used for all subsequent load
  237. * calls. This is the working directory of Assimp.
  238. *
  239. * Every (inplicit) occurence of '.\' will be replaced with it.
  240. *
  241. * @param pBase Base path. This *may* also be the path to
  242. * a file (the directory of the file is taken then, of course)
  243. */
  244. void SetBasePath (const std::string& pBase);
  245. /** Add a new file to the list of files to be loaded.
  246. *
  247. * @param file File to be loaded
  248. * @param steps Steps to be executed on the file
  249. * @param map Optional configuration properties
  250. */
  251. void AddLoadRequest (const std::string& file,
  252. unsigned int steps = 0, const PropertyMap* map = NULL);
  253. /** Get an imported scene.
  254. *
  255. * This polls the import from the internal request list.
  256. * If an import is requested several times, this function
  257. * can be called several times, too.
  258. *
  259. * @param file File name of the scene
  260. * @return NULL if there is no scene with this file name
  261. * in the queue of the scene hasn't been loaded yet.
  262. */
  263. aiScene* GetImport (const std::string& file);
  264. /** Waits until all scenes have been loaded.
  265. */
  266. void LoadAll();
  267. private:
  268. // No need to have that in the public API ...
  269. void* pimpl;
  270. };
  271. } // end of namespace Assimp
  272. #endif // AI_BASEIMPORTER_H_INC