BaseImporter.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, 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 Definition of the base class for all importer worker classes.
  34. #pragma once
  35. #ifndef INCLUDED_AI_BASEIMPORTER_H
  36. #define INCLUDED_AI_BASEIMPORTER_H
  37. #ifdef __GNUC__
  38. #pragma GCC system_header
  39. #endif
  40. #include "Exceptional.h"
  41. #include <assimp/types.h>
  42. #include <assimp/ProgressHandler.hpp>
  43. #include <exception>
  44. #include <set>
  45. #include <vector>
  46. #include <memory>
  47. struct aiScene;
  48. struct aiImporterDesc;
  49. namespace Assimp {
  50. // Forward declarations
  51. class Importer;
  52. class IOSystem;
  53. class BaseProcess;
  54. class SharedPostProcessInfo;
  55. class IOStream;
  56. // utility to do char4 to uint32 in a portable manner
  57. #define AI_MAKE_MAGIC(string) ((uint32_t)((string[0] << 24) + \
  58. (string[1] << 16) + (string[2] << 8) + string[3]))
  59. using UByteBuffer = std::vector<uint8_t>;
  60. using ByteBuffer = std::vector<int8_t>;
  61. // ---------------------------------------------------------------------------
  62. /** FOR IMPORTER PLUGINS ONLY: The BaseImporter defines a common interface
  63. * for all importer worker classes.
  64. *
  65. * The interface defines two functions: CanRead() is used to check if the
  66. * importer can handle the format of the given file. If an implementation of
  67. * this function returns true, the importer then calls ReadFile() which
  68. * imports the given file. ReadFile is not overridable, it just calls
  69. * InternReadFile() and catches any ImportErrorException that might occur.
  70. */
  71. class ASSIMP_API BaseImporter {
  72. friend class Importer;
  73. public:
  74. /** Constructor to be privately used by #Importer */
  75. BaseImporter() AI_NO_EXCEPT;
  76. /** Destructor, private as well */
  77. virtual ~BaseImporter();
  78. // -------------------------------------------------------------------
  79. /** Returns whether the class can handle the format of the given file.
  80. *
  81. * The implementation is expected to perform a full check of the file
  82. * structure, possibly searching the first bytes of the file for magic
  83. * identifiers or keywords.
  84. *
  85. * @param pFile Path and file name of the file to be examined.
  86. * @param pIOHandler The IO handler to use for accessing any file.
  87. * @param checkSig Legacy; do not use.
  88. * @return true if the class can read this file, false if not or if
  89. * unsure.
  90. */
  91. virtual bool CanRead(
  92. const std::string &pFile,
  93. IOSystem *pIOHandler,
  94. bool checkSig) const = 0;
  95. // -------------------------------------------------------------------
  96. /** Imports the given file and returns the imported data.
  97. * If the import succeeds, ownership of the data is transferred to
  98. * the caller. If the import fails, nullptr is returned. The function
  99. * takes care that any partially constructed data is destroyed
  100. * beforehand.
  101. *
  102. * @param pImp #Importer object hosting this loader.
  103. * @param pFile Path of the file to be imported.
  104. * @param pIOHandler IO-Handler used to open this and possible other files.
  105. * @return The imported data or nullptr if failed. If it failed a
  106. * human-readable error description can be retrieved by calling
  107. * GetErrorText()
  108. *
  109. * @note This function is not intended to be overridden. Implement
  110. * InternReadFile() to do the import. If an exception is thrown somewhere
  111. * in InternReadFile(), this function will catch it and transform it into
  112. * a suitable response to the caller.
  113. */
  114. aiScene *ReadFile(
  115. Importer *pImp,
  116. const std::string &pFile,
  117. IOSystem *pIOHandler);
  118. // -------------------------------------------------------------------
  119. /** Returns the error description of the last error that occurred.
  120. * If the error is due to a std::exception, this will return the message.
  121. * Exceptions can also be accessed with GetException().
  122. * @return A description of the last error that occurred. An empty
  123. * string if there was no error.
  124. */
  125. const std::string &GetErrorText() const {
  126. return m_ErrorText;
  127. }
  128. // -------------------------------------------------------------------
  129. /** Returns the exception of the last exception that occurred.
  130. * Note: Exceptions are not the only source of error details, so GetErrorText
  131. * should be consulted too.
  132. * @return The last exception that occurred.
  133. */
  134. const std::exception_ptr& GetException() const {
  135. return m_Exception;
  136. }
  137. // -------------------------------------------------------------------
  138. /** Called prior to ReadFile().
  139. * The function is a request to the importer to update its configuration
  140. * basing on the Importer's configuration property list.
  141. * @param pImp Importer instance
  142. */
  143. virtual void SetupProperties(
  144. const Importer *pImp);
  145. // -------------------------------------------------------------------
  146. /** Called by #Importer::GetImporterInfo to get a description of
  147. * some loader features. Importers must provide this information. */
  148. virtual const aiImporterDesc *GetInfo() const = 0;
  149. /**
  150. * Will be called only by scale process when scaling is requested.
  151. */
  152. void SetFileScale(double scale) {
  153. fileScale = scale;
  154. }
  155. // -------------------------------------------------------------------
  156. /** Called by #Importer::GetExtensionList for each loaded importer.
  157. * Take the extension list contained in the structure returned by
  158. * #GetInfo and insert all file extensions into the given set.
  159. * @param extension set to collect file extensions in*/
  160. void GetExtensionList(std::set<std::string> &extensions);
  161. protected:
  162. double importerScale = 1.0;
  163. double fileScale = 1.0;
  164. // -------------------------------------------------------------------
  165. /** Imports the given file into the given scene structure. The
  166. * function is expected to throw an ImportErrorException if there is
  167. * an error. If it terminates normally, the data in aiScene is
  168. * expected to be correct. Override this function to implement the
  169. * actual importing.
  170. * <br>
  171. * The output scene must meet the following requirements:<br>
  172. * <ul>
  173. * <li>At least a root node must be there, even if its only purpose
  174. * is to reference one mesh.</li>
  175. * <li>aiMesh::mPrimitiveTypes may be 0. The types of primitives
  176. * in the mesh are determined automatically in this case.</li>
  177. * <li>the vertex data is stored in a pseudo-indexed "verbose" format.
  178. * In fact this means that every vertex that is referenced by
  179. * a face is unique. Or the other way round: a vertex index may
  180. * not occur twice in a single aiMesh.</li>
  181. * <li>aiAnimation::mDuration may be -1. Assimp determines the length
  182. * of the animation automatically in this case as the length of
  183. * the longest animation channel.</li>
  184. * <li>aiMesh::mBitangents may be nullptr if tangents and normals are
  185. * given. In this case bitangents are computed as the cross product
  186. * between normal and tangent.</li>
  187. * <li>There needn't be a material. If none is there a default material
  188. * is generated. However, it is recommended practice for loaders
  189. * to generate a default material for yourself that matches the
  190. * default material setting for the file format better than Assimp's
  191. * generic default material. Note that default materials *should*
  192. * be named AI_DEFAULT_MATERIAL_NAME if they're just color-shaded
  193. * or AI_DEFAULT_TEXTURED_MATERIAL_NAME if they define a (dummy)
  194. * texture. </li>
  195. * </ul>
  196. * If the AI_SCENE_FLAGS_INCOMPLETE-Flag is <b>not</b> set:<ul>
  197. * <li> at least one mesh must be there</li>
  198. * <li> there may be no meshes with 0 vertices or faces</li>
  199. * </ul>
  200. * This won't be checked (except by the validation step): Assimp will
  201. * crash if one of the conditions is not met!
  202. *
  203. * @param pFile Path of the file to be imported.
  204. * @param pScene The scene object to hold the imported data.
  205. * nullptr is not a valid parameter.
  206. * @param pIOHandler The IO handler to use for any file access.
  207. * nullptr is not a valid parameter. */
  208. virtual void InternReadFile(
  209. const std::string &pFile,
  210. aiScene *pScene,
  211. IOSystem *pIOHandler) = 0;
  212. public: // static utilities
  213. // -------------------------------------------------------------------
  214. /** A utility for CanRead().
  215. *
  216. * The function searches the header of a file for a specific token
  217. * and returns true if this token is found. This works for text
  218. * files only. There is a rudimentary handling of UNICODE files.
  219. * The comparison is case independent.
  220. *
  221. * @param pIOSystem IO System to work with
  222. * @param file File name of the file
  223. * @param tokens List of tokens to search for
  224. * @param numTokens Size of the token array
  225. * @param searchBytes Number of bytes to be searched for the tokens.
  226. */
  227. static bool SearchFileHeaderForToken(
  228. IOSystem *pIOSystem,
  229. const std::string &file,
  230. const char **tokens,
  231. std::size_t numTokens,
  232. unsigned int searchBytes = 200,
  233. bool tokensSol = false,
  234. bool noGraphBeforeTokens = false);
  235. // -------------------------------------------------------------------
  236. /** @brief Check whether a file has a specific file extension
  237. * @param pFile Input file
  238. * @param ext0 Extension to check for. Lowercase characters only, no dot!
  239. * @param ext1 Optional second extension
  240. * @param ext2 Optional third extension
  241. * @note Case-insensitive
  242. */
  243. static bool SimpleExtensionCheck(
  244. const std::string &pFile,
  245. const char *ext0,
  246. const char *ext1 = nullptr,
  247. const char *ext2 = nullptr);
  248. // -------------------------------------------------------------------
  249. /** @brief Check whether a file has one of the passed file extensions
  250. * @param pFile Input file
  251. * @param extensions Extensions to check for. Lowercase characters only, no dot!
  252. * @note Case-insensitive
  253. */
  254. static bool HasExtension(
  255. const std::string &pFile,
  256. const std::set<std::string> &extensions);
  257. // -------------------------------------------------------------------
  258. /** @brief Extract file extension from a string
  259. * @param pFile Input file
  260. * @return Extension without trailing dot, all lowercase
  261. */
  262. static std::string GetExtension(
  263. const std::string &pFile);
  264. // -------------------------------------------------------------------
  265. /** @brief Check whether a file starts with one or more magic tokens
  266. * @param pFile Input file
  267. * @param pIOHandler IO system to be used
  268. * @param magic n magic tokens
  269. * @params num Size of magic
  270. * @param offset Offset from file start where tokens are located
  271. * @param Size of one token, in bytes. Maximally 16 bytes.
  272. * @return true if one of the given tokens was found
  273. *
  274. * @note For convenience, the check is also performed for the
  275. * byte-swapped variant of all tokens (big endian). Only for
  276. * tokens of size 2,4.
  277. */
  278. static bool CheckMagicToken(
  279. IOSystem *pIOHandler,
  280. const std::string &pFile,
  281. const void *magic,
  282. std::size_t num,
  283. unsigned int offset = 0,
  284. unsigned int size = 4);
  285. // -------------------------------------------------------------------
  286. /** An utility for all text file loaders. It converts a file to our
  287. * UTF8 character set. Errors are reported, but ignored.
  288. *
  289. * @param data File buffer to be converted to UTF8 data. The buffer
  290. * is resized as appropriate. */
  291. static void ConvertToUTF8(
  292. std::vector<char> &data);
  293. // -------------------------------------------------------------------
  294. /** An utility for all text file loaders. It converts a file from our
  295. * UTF8 character set back to ISO-8859-1. Errors are reported, but ignored.
  296. *
  297. * @param data File buffer to be converted from UTF8 to ISO-8859-1. The buffer
  298. * is resized as appropriate. */
  299. static void ConvertUTF8toISO8859_1(
  300. std::string &data);
  301. // -------------------------------------------------------------------
  302. /// @brief Enum to define, if empty files are ok or not.
  303. enum TextFileMode {
  304. ALLOW_EMPTY,
  305. FORBID_EMPTY
  306. };
  307. // -------------------------------------------------------------------
  308. /** Utility for text file loaders which copies the contents of the
  309. * file into a memory buffer and converts it to our UTF8
  310. * representation.
  311. * @param stream Stream to read from.
  312. * @param data Output buffer to be resized and filled with the
  313. * converted text file data. The buffer is terminated with
  314. * a binary 0.
  315. * @param mode Whether it is OK to load empty text files. */
  316. static void TextFileToBuffer(
  317. IOStream *stream,
  318. std::vector<char> &data,
  319. TextFileMode mode = FORBID_EMPTY);
  320. // -------------------------------------------------------------------
  321. /** Utility function to move a std::vector into a aiScene array
  322. * @param vec The vector to be moved
  323. * @param out The output pointer to the allocated array.
  324. * @param numOut The output count of elements copied. */
  325. template <typename T>
  326. AI_FORCE_INLINE static void CopyVector(
  327. std::vector<T> &vec,
  328. T *&out,
  329. unsigned int &outLength) {
  330. outLength = unsigned(vec.size());
  331. if (outLength) {
  332. out = new T[outLength];
  333. std::swap_ranges(vec.begin(), vec.end(), out);
  334. }
  335. }
  336. // -------------------------------------------------------------------
  337. /** Utility function to move a std::vector of unique_ptrs into a aiScene array
  338. * @param vec The vector of unique_ptrs to be moved
  339. * @param out The output pointer to the allocated array.
  340. * @param numOut The output count of elements copied. */
  341. template <typename T>
  342. AI_FORCE_INLINE static void CopyVector(
  343. std::vector<std::unique_ptr<T> > &vec,
  344. T **&out,
  345. unsigned int &outLength) {
  346. outLength = unsigned(vec.size());
  347. if (outLength) {
  348. out = new T*[outLength];
  349. T** outPtr = out;
  350. std::for_each(vec.begin(), vec.end(), [&outPtr](std::unique_ptr<T>& uPtr){*outPtr = uPtr.release(); ++outPtr; });
  351. }
  352. }
  353. private:
  354. /* Pushes state into importer for the importer scale */
  355. void UpdateImporterScale(Importer *pImp);
  356. protected:
  357. /// Error description in case there was one.
  358. std::string m_ErrorText;
  359. /// The exception, in case there was one.
  360. std::exception_ptr m_Exception;
  361. /// Currently set progress handler.
  362. ProgressHandler *m_progress;
  363. };
  364. } // end of namespace Assimp
  365. #endif // AI_BASEIMPORTER_H_INC