BaseImporter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, 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 INCLUDED_AI_BASEIMPORTER_H
  35. #define INCLUDED_AI_BASEIMPORTER_H
  36. #include "Exceptional.h"
  37. #include <string>
  38. #include <map>
  39. #include <vector>
  40. #include "./../include/aiTypes.h"
  41. struct aiScene;
  42. namespace Assimp {
  43. class IOSystem;
  44. class Importer;
  45. class BaseImporter;
  46. class BaseProcess;
  47. class SharedPostProcessInfo;
  48. class IOStream;
  49. // utility to do char4 to uint32 in a portable manner
  50. #define AI_MAKE_MAGIC(string) ((uint32_t)((string[0] << 24) + \
  51. (string[1] << 16) + (string[2] << 8) + string[3]))
  52. // ---------------------------------------------------------------------------
  53. template <typename T>
  54. struct ScopeGuard
  55. {
  56. ScopeGuard(T* obj) : obj(obj), mdismiss() {}
  57. ~ScopeGuard () throw() {
  58. if (!mdismiss) {
  59. delete obj;
  60. }
  61. obj = NULL;
  62. }
  63. T* dismiss() {
  64. mdismiss=true;
  65. return obj;
  66. }
  67. operator T*() {
  68. return obj;
  69. }
  70. T* operator -> () {
  71. return obj;
  72. }
  73. private:
  74. T* obj;
  75. bool mdismiss;
  76. };
  77. // ---------------------------------------------------------------------------
  78. /** FOR IMPORTER PLUGINS ONLY: The BaseImporter defines a common interface
  79. * for all importer worker classes.
  80. *
  81. * The interface defines two functions: CanRead() is used to check if the
  82. * importer can handle the format of the given file. If an implementation of
  83. * this function returns true, the importer then calls ReadFile() which
  84. * imports the given file. ReadFile is not overridable, it just calls
  85. * InternReadFile() and catches any ImportErrorException that might occur.
  86. */
  87. class ASSIMP_API BaseImporter
  88. {
  89. friend class Importer;
  90. public:
  91. /** Constructor to be privately used by #Importer */
  92. BaseImporter();
  93. /** Destructor, private as well */
  94. virtual ~BaseImporter();
  95. public:
  96. // -------------------------------------------------------------------
  97. /** Returns whether the class can handle the format of the given file.
  98. *
  99. * The implementation should be as quick as possible. A check for
  100. * the file extension is enough. If no suitable loader is found with
  101. * this strategy, CanRead() is called again, the 'checkSig' parameter
  102. * set to true this time. Now the implementation is expected to
  103. * perform a full check of the file structure, possibly searching the
  104. * first bytes of the file for magic identifiers or keywords.
  105. *
  106. * @param pFile Path and file name of the file to be examined.
  107. * @param pIOHandler The IO handler to use for accessing any file.
  108. * @param checkSig Set to true if this method is called a second time.
  109. * This time, the implementation may take more time to examine the
  110. * contents of the file to be loaded for magic bytes, keywords, etc
  111. * to be able to load files with unknown/not existent file extensions.
  112. * @return true if the class can read this file, false if not.
  113. */
  114. virtual bool CanRead(
  115. const std::string& pFile,
  116. IOSystem* pIOHandler,
  117. bool checkSig
  118. ) const = 0;
  119. // -------------------------------------------------------------------
  120. /** Imports the given file and returns the imported data.
  121. * If the import succeeds, ownership of the data is transferred to
  122. * the caller. If the import fails, NULL is returned. The function
  123. * takes care that any partially constructed data is destroyed
  124. * beforehand.
  125. *
  126. * @param pImp #Importer object hosting this loader.
  127. * @param pFile Path of the file to be imported.
  128. * @param pIOHandler IO-Handler used to open this and possible other files.
  129. * @return The imported data or NULL if failed. If it failed a
  130. * human-readable error description can be retrieved by calling
  131. * GetErrorText()
  132. *
  133. * @note This function is not intended to be overridden. Implement
  134. * InternReadFile() to do the import. If an exception is thrown somewhere
  135. * in InternReadFile(), this function will catch it and transform it into
  136. * a suitable response to the caller.
  137. */
  138. aiScene* ReadFile(
  139. const Importer* pImp,
  140. const std::string& pFile,
  141. IOSystem* pIOHandler
  142. );
  143. // -------------------------------------------------------------------
  144. /** Returns the error description of the last error that occured.
  145. * @return A description of the last error that occured. An empty
  146. * string if there was no error.
  147. */
  148. const std::string& GetErrorText() const {
  149. return mErrorText;
  150. }
  151. // -------------------------------------------------------------------
  152. /** Called prior to ReadFile().
  153. * The function is a request to the importer to update its configuration
  154. * basing on the Importer's configuration property list.
  155. * @param pImp Importer instance
  156. */
  157. virtual void SetupProperties(
  158. const Importer* pImp
  159. );
  160. protected:
  161. // -------------------------------------------------------------------
  162. /** Called by Importer::GetExtensionList() for each loaded importer.
  163. * Implementations are expected to insert() all file extensions
  164. * handled by them into the extension set. A loader capable of
  165. * reading certain files with the extension BLA would place the
  166. * string bla (lower-case!) in the output set.
  167. * @param extensions Output set. */
  168. virtual void GetExtensionList(
  169. std::set<std::string>& extensions
  170. ) = 0;
  171. // -------------------------------------------------------------------
  172. /** Imports the given file into the given scene structure. The
  173. * function is expected to throw an ImportErrorException if there is
  174. * an error. If it terminates normally, the data in aiScene is
  175. * expected to be correct. Override this function to implement the
  176. * actual importing.
  177. * <br>
  178. * The output scene must meet the following requirements:<br>
  179. * <ul>
  180. * <li>At least a root node must be there, even if its only purpose
  181. * is to reference one mesh.</li>
  182. * <li>aiMesh::mPrimitiveTypes may be 0. The types of primitives
  183. * in the mesh are determined automatically in this case.</li>
  184. * <li>the vertex data is stored in a pseudo-indexed "verbose" format.
  185. * In fact this means that every vertex that is referenced by
  186. * a face is unique. Or the other way round: a vertex index may
  187. * not occur twice in a single aiMesh.</li>
  188. * <li>aiAnimation::mDuration may be -1. Assimp determines the length
  189. * of the animation automatically in this case as the length of
  190. * the longest animation channel.</li>
  191. * <li>aiMesh::mBitangents may be NULL if tangents and normals are
  192. * given. In this case bitangents are computed as the cross product
  193. * between normal and tangent.</li>
  194. * <li>There needn't be a material. If none is there a default material
  195. * is generated. However, it is recommended practice for loaders
  196. * to generate a default material for yourself that matches the
  197. * default material setting for the file format better than Assimp's
  198. * generic default material. Note that default materials *should*
  199. * be named AI_DEFAULT_MATERIAL_NAME if they're just color-shaded
  200. * or AI_DEFAULT_TEXTURED_MATERIAL_NAME if they define a (dummy)
  201. * texture. </li>
  202. * </ul>
  203. * If the AI_SCENE_FLAGS_INCOMPLETE-Flag is <b>not</b> set:<ul>
  204. * <li> at least one mesh must be there</li>
  205. * <li> there may be no meshes with 0 vertices or faces</li>
  206. * </ul>
  207. * This won't be checked (except by the validation step): Assimp will
  208. * crash if one of the conditions is not met!
  209. *
  210. * @param pFile Path of the file to be imported.
  211. * @param pScene The scene object to hold the imported data.
  212. * NULL is not a valid parameter.
  213. * @param pIOHandler The IO handler to use for any file access.
  214. * NULL is not a valid parameter. */
  215. virtual void InternReadFile(
  216. const std::string& pFile,
  217. aiScene* pScene,
  218. IOSystem* pIOHandler
  219. ) = 0;
  220. public: // static utilities
  221. // -------------------------------------------------------------------
  222. /** A utility for CanRead().
  223. *
  224. * The function searches the header of a file for a specific token
  225. * and returns true if this token is found. This works for text
  226. * files only. There is a rudimentary handling of UNICODE files.
  227. * The comparison is case independent.
  228. *
  229. * @param pIOSystem IO System to work with
  230. * @param file File name of the file
  231. * @param tokens List of tokens to search for
  232. * @param numTokens Size of the token array
  233. * @param searchBytes Number of bytes to be searched for the tokens.
  234. */
  235. static bool SearchFileHeaderForToken(
  236. IOSystem* pIOSystem,
  237. const std::string& file,
  238. const char** tokens,
  239. unsigned int numTokens,
  240. unsigned int searchBytes = 200);
  241. // -------------------------------------------------------------------
  242. /** @brief Check whether a file has a specific file extension
  243. * @param pFile Input file
  244. * @param ext0 Extension to check for. Lowercase characters only, no dot!
  245. * @param ext1 Optional second extension
  246. * @param ext2 Optional third extension
  247. * @note Case-insensitive
  248. */
  249. static bool SimpleExtensionCheck (
  250. const std::string& pFile,
  251. const char* ext0,
  252. const char* ext1 = NULL,
  253. const char* ext2 = NULL);
  254. // -------------------------------------------------------------------
  255. /** @brief Extract file extension from a string
  256. * @param pFile Input file
  257. * @return Extension without trailing dot, all lowercase
  258. */
  259. static std::string GetExtension (
  260. const std::string& pFile);
  261. // -------------------------------------------------------------------
  262. /** @brief Check whether a file starts with one or more magic tokens
  263. * @param pFile Input file
  264. * @param pIOHandler IO system to be used
  265. * @param magic n magic tokens
  266. * @params num Size of magic
  267. * @param offset Offset from file start where tokens are located
  268. * @param Size of one token, in bytes. Maximally 16 bytes.
  269. * @return true if one of the given tokens was found
  270. *
  271. * @note For convinence, the check is also performed for the
  272. * byte-swapped variant of all tokens (big endian). Only for
  273. * tokens of size 2,4.
  274. */
  275. static bool CheckMagicToken(
  276. IOSystem* pIOHandler,
  277. const std::string& pFile,
  278. const void* magic,
  279. unsigned int num,
  280. unsigned int offset = 0,
  281. unsigned int size = 4);
  282. // -------------------------------------------------------------------
  283. /** An utility for all text file loaders. It converts a file to our
  284. * UTF8 character set. Errors are reported, but ignored.
  285. *
  286. * @param data File buffer to be converted to UTF8 data. The buffer
  287. * is resized as appropriate. */
  288. static void ConvertToUTF8(
  289. std::vector<char>& data);
  290. // -------------------------------------------------------------------
  291. /** Utility for text file loaders which copies the contents of the
  292. * file into a memory buffer and converts it to our UTF8
  293. * representation.
  294. * @param stream Stream to read from.
  295. * @param data Output buffer to be resized and filled with the
  296. * converted text file data. The buffer is terminated with
  297. * a binary 0. */
  298. static void TextFileToBuffer(
  299. IOStream* stream,
  300. std::vector<char>& data);
  301. protected:
  302. /** Error description in case there was one. */
  303. std::string mErrorText;
  304. /** Currently set progress handler */
  305. ProgressHandler* progress;
  306. };
  307. } // end of namespace Assimp
  308. #endif // AI_BASEIMPORTER_H_INC