Exporter.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2019, assimp 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 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 Exporter.hpp
  35. * @brief Defines the CPP-API for the Assimp export interface
  36. */
  37. #pragma once
  38. #ifndef AI_EXPORT_HPP_INC
  39. #define AI_EXPORT_HPP_INC
  40. #ifndef ASSIMP_BUILD_NO_EXPORT
  41. #include "cexport.h"
  42. #include <map>
  43. namespace Assimp {
  44. class ExporterPimpl;
  45. class IOSystem;
  46. class ProgressHandler;
  47. // ----------------------------------------------------------------------------------
  48. /** CPP-API: The Exporter class forms an C++ interface to the export functionality
  49. * of the Open Asset Import Library. Note that the export interface is available
  50. * only if Assimp has been built with ASSIMP_BUILD_NO_EXPORT not defined.
  51. *
  52. * The interface is modeled after the importer interface and mostly
  53. * symmetric. The same rules for threading etc. apply.
  54. *
  55. * In a nutshell, there are two export interfaces: #Export, which writes the
  56. * output file(s) either to the regular file system or to a user-supplied
  57. * #IOSystem, and #ExportToBlob which returns a linked list of memory
  58. * buffers (blob), each referring to one output file (in most cases
  59. * there will be only one output file of course, but this extra complexity is
  60. * needed since Assimp aims at supporting a wide range of file formats).
  61. *
  62. * #ExportToBlob is especially useful if you intend to work
  63. * with the data in-memory.
  64. */
  65. class ASSIMP_API ExportProperties;
  66. class ASSIMP_API Exporter {
  67. public:
  68. /** Function pointer type of a Export worker function */
  69. typedef void (*fpExportFunc)(const char*, IOSystem*, const aiScene*, const ExportProperties*);
  70. /** Internal description of an Assimp export format option */
  71. struct ExportFormatEntry {
  72. /// Public description structure to be returned by aiGetExportFormatDescription()
  73. aiExportFormatDesc mDescription;
  74. // Worker function to do the actual exporting
  75. fpExportFunc mExportFunction;
  76. // Post-processing steps to be executed PRIOR to invoking mExportFunction
  77. unsigned int mEnforcePP;
  78. // Constructor to fill all entries
  79. ExportFormatEntry( const char* pId, const char* pDesc, const char* pExtension, fpExportFunc pFunction, unsigned int pEnforcePP = 0u)
  80. {
  81. mDescription.id = pId;
  82. mDescription.description = pDesc;
  83. mDescription.fileExtension = pExtension;
  84. mExportFunction = pFunction;
  85. mEnforcePP = pEnforcePP;
  86. }
  87. ExportFormatEntry() :
  88. mExportFunction()
  89. , mEnforcePP()
  90. {
  91. mDescription.id = NULL;
  92. mDescription.description = NULL;
  93. mDescription.fileExtension = NULL;
  94. }
  95. };
  96. /**
  97. * @brief The class constructor.
  98. */
  99. Exporter();
  100. /**
  101. * @brief The class destructor.
  102. */
  103. ~Exporter();
  104. // -------------------------------------------------------------------
  105. /** Supplies a custom IO handler to the exporter to use to open and
  106. * access files.
  107. *
  108. * If you need #Export to use custom IO logic to access the files,
  109. * you need to supply a custom implementation of IOSystem and
  110. * IOFile to the exporter.
  111. *
  112. * #Exporter takes ownership of the object and will destroy it
  113. * afterwards. The previously assigned handler will be deleted.
  114. * Pass NULL to take again ownership of your IOSystem and reset Assimp
  115. * to use its default implementation, which uses plain file IO.
  116. *
  117. * @param pIOHandler The IO handler to be used in all file accesses
  118. * of the Importer. */
  119. void SetIOHandler( IOSystem* pIOHandler);
  120. // -------------------------------------------------------------------
  121. /** Retrieves the IO handler that is currently set.
  122. * You can use #IsDefaultIOHandler() to check whether the returned
  123. * interface is the default IO handler provided by ASSIMP. The default
  124. * handler is active as long the application doesn't supply its own
  125. * custom IO handler via #SetIOHandler().
  126. * @return A valid IOSystem interface, never NULL. */
  127. IOSystem* GetIOHandler() const;
  128. // -------------------------------------------------------------------
  129. /** Checks whether a default IO handler is active
  130. * A default handler is active as long the application doesn't
  131. * supply its own custom IO handler via #SetIOHandler().
  132. * @return true by default */
  133. bool IsDefaultIOHandler() const;
  134. // -------------------------------------------------------------------
  135. /** Supplies a custom progress handler to the exporter. This
  136. * interface exposes an #Update() callback, which is called
  137. * more or less periodically (please don't sue us if it
  138. * isn't as periodically as you'd like it to have ...).
  139. * This can be used to implement progress bars and loading
  140. * timeouts.
  141. * @param pHandler Progress callback interface. Pass nullptr to
  142. * disable progress reporting.
  143. * @note Progress handlers can be used to abort the loading
  144. * at almost any time.*/
  145. void SetProgressHandler(ProgressHandler* pHandler);
  146. // -------------------------------------------------------------------
  147. /** Exports the given scene to a chosen file format. Returns the exported
  148. * data as a binary blob which you can write into a file or something.
  149. * When you're done with the data, simply let the #Exporter instance go
  150. * out of scope to have it released automatically.
  151. * @param pScene The scene to export. Stays in possession of the caller,
  152. * is not changed by the function.
  153. * @param pFormatId ID string to specify to which format you want to
  154. * export to. Use
  155. * #GetExportFormatCount / #GetExportFormatDescription to learn which
  156. * export formats are available.
  157. * @param pPreprocessing See the documentation for #Export
  158. * @return the exported data or NULL in case of error.
  159. * @note If the Exporter instance did already hold a blob from
  160. * a previous call to #ExportToBlob, it will be disposed.
  161. * Any IO handlers set via #SetIOHandler are ignored here.
  162. * @note Use aiCopyScene() to get a modifiable copy of a previously
  163. * imported scene. */
  164. const aiExportDataBlob* ExportToBlob(const aiScene* pScene, const char* pFormatId,
  165. unsigned int pPreprocessing = 0u, const ExportProperties* = nullptr);
  166. const aiExportDataBlob* ExportToBlob( const aiScene* pScene, const std::string& pFormatId,
  167. unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr);
  168. // -------------------------------------------------------------------
  169. /** Convenience function to export directly to a file. Use
  170. * #SetIOSystem to supply a custom IOSystem to gain fine-grained control
  171. * about the output data flow of the export process.
  172. * @param pBlob A data blob obtained from a previous call to #aiExportScene. Must not be NULL.
  173. * @param pPath Full target file name. Target must be accessible.
  174. * @param pPreprocessing Accepts any choice of the #aiPostProcessSteps enumerated
  175. * flags, but in reality only a subset of them makes sense here. Specifying
  176. * 'preprocessing' flags is useful if the input scene does not conform to
  177. * Assimp's default conventions as specified in the @link data Data Structures Page @endlink.
  178. * In short, this means the geometry data should use a right-handed coordinate systems, face
  179. * winding should be counter-clockwise and the UV coordinate origin is assumed to be in
  180. * the upper left. The #aiProcess_MakeLeftHanded, #aiProcess_FlipUVs and
  181. * #aiProcess_FlipWindingOrder flags are used in the import side to allow users
  182. * to have those defaults automatically adapted to their conventions. Specifying those flags
  183. * for exporting has the opposite effect, respectively. Some other of the
  184. * #aiPostProcessSteps enumerated values may be useful as well, but you'll need
  185. * to try out what their effect on the exported file is. Many formats impose
  186. * their own restrictions on the structure of the geometry stored therein,
  187. * so some preprocessing may have little or no effect at all, or may be
  188. * redundant as exporters would apply them anyhow. A good example
  189. * is triangulation - whilst you can enforce it by specifying
  190. * the #aiProcess_Triangulate flag, most export formats support only
  191. * triangulate data so they would run the step even if it wasn't requested.
  192. *
  193. * If assimp detects that the input scene was directly taken from the importer side of
  194. * the library (i.e. not copied using aiCopyScene and potentially modified afterwards),
  195. * any post-processing steps already applied to the scene will not be applied again, unless
  196. * they show non-idempotent behavior (#aiProcess_MakeLeftHanded, #aiProcess_FlipUVs and
  197. * #aiProcess_FlipWindingOrder).
  198. * @return AI_SUCCESS if everything was fine.
  199. * @note Use aiCopyScene() to get a modifiable copy of a previously
  200. * imported scene.*/
  201. aiReturn Export( const aiScene* pScene, const char* pFormatId, const char* pPath,
  202. unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr);
  203. aiReturn Export( const aiScene* pScene, const std::string& pFormatId, const std::string& pPath,
  204. unsigned int pPreprocessing = 0u, const ExportProperties* pProperties = nullptr);
  205. // -------------------------------------------------------------------
  206. /** Returns an error description of an error that occurred in #Export
  207. * or #ExportToBlob
  208. *
  209. * Returns an empty string if no error occurred.
  210. * @return A description of the last error, an empty string if no
  211. * error occurred. The string is never NULL.
  212. *
  213. * @note The returned function remains valid until one of the
  214. * following methods is called: #Export, #ExportToBlob, #FreeBlob */
  215. const char* GetErrorString() const;
  216. // -------------------------------------------------------------------
  217. /** Return the blob obtained from the last call to #ExportToBlob */
  218. const aiExportDataBlob* GetBlob() const;
  219. // -------------------------------------------------------------------
  220. /** Orphan the blob from the last call to #ExportToBlob. This means
  221. * the caller takes ownership and is thus responsible for calling
  222. * the C API function #aiReleaseExportBlob to release it. */
  223. const aiExportDataBlob* GetOrphanedBlob() const;
  224. // -------------------------------------------------------------------
  225. /** Frees the current blob.
  226. *
  227. * The function does nothing if no blob has previously been
  228. * previously produced via #ExportToBlob. #FreeBlob is called
  229. * automatically by the destructor. The only reason to call
  230. * it manually would be to reclaim as much storage as possible
  231. * without giving up the #Exporter instance yet. */
  232. void FreeBlob( );
  233. // -------------------------------------------------------------------
  234. /** Returns the number of export file formats available in the current
  235. * Assimp build. Use #Exporter::GetExportFormatDescription to
  236. * retrieve infos of a specific export format.
  237. *
  238. * This includes built-in exporters as well as exporters registered
  239. * using #RegisterExporter.
  240. **/
  241. size_t GetExportFormatCount() const;
  242. // -------------------------------------------------------------------
  243. /** Returns a description of the nth export file format. Use #
  244. * #Exporter::GetExportFormatCount to learn how many export
  245. * formats are supported.
  246. *
  247. * The returned pointer is of static storage duration if the
  248. * pIndex pertains to a built-in exporter (i.e. one not registered
  249. * via #RegistrerExporter). It is restricted to the life-time of the
  250. * #Exporter instance otherwise.
  251. *
  252. * @param pIndex Index of the export format to retrieve information
  253. * for. Valid range is 0 to #Exporter::GetExportFormatCount
  254. * @return A description of that specific export format.
  255. * NULL if pIndex is out of range. */
  256. const aiExportFormatDesc* GetExportFormatDescription( size_t pIndex ) const;
  257. // -------------------------------------------------------------------
  258. /** Register a custom exporter. Custom export formats are limited to
  259. * to the current #Exporter instance and do not affect the
  260. * library globally. The indexes under which the format's
  261. * export format description can be queried are assigned
  262. * monotonously.
  263. * @param desc Exporter description.
  264. * @return aiReturn_SUCCESS if the export format was successfully
  265. * registered. A common cause that would prevent an exporter
  266. * from being registered is that its format id is already
  267. * occupied by another format. */
  268. aiReturn RegisterExporter(const ExportFormatEntry& desc);
  269. // -------------------------------------------------------------------
  270. /** Remove an export format previously registered with #RegisterExporter
  271. * from the #Exporter instance (this can also be used to drop
  272. * built-in exporters because those are implicitly registered
  273. * using #RegisterExporter).
  274. * @param id Format id to be unregistered, this refers to the
  275. * 'id' field of #aiExportFormatDesc.
  276. * @note Calling this method on a format description not yet registered
  277. * has no effect.*/
  278. void UnregisterExporter(const char* id);
  279. protected:
  280. // Just because we don't want you to know how we're hacking around.
  281. ExporterPimpl* pimpl;
  282. };
  283. class ASSIMP_API ExportProperties {
  284. public:
  285. // Data type to store the key hash
  286. typedef unsigned int KeyType;
  287. // typedefs for our four configuration maps.
  288. // We don't need more, so there is no need for a generic solution
  289. typedef std::map<KeyType, int> IntPropertyMap;
  290. typedef std::map<KeyType, ai_real> FloatPropertyMap;
  291. typedef std::map<KeyType, std::string> StringPropertyMap;
  292. typedef std::map<KeyType, aiMatrix4x4> MatrixPropertyMap;
  293. public:
  294. /** Standard constructor
  295. * @see ExportProperties()
  296. */
  297. ExportProperties();
  298. // -------------------------------------------------------------------
  299. /** Copy constructor.
  300. *
  301. * This copies the configuration properties of another ExportProperties.
  302. * @see ExportProperties(const ExportProperties& other)
  303. */
  304. ExportProperties(const ExportProperties& other);
  305. // -------------------------------------------------------------------
  306. /** Set an integer configuration property.
  307. * @param szName Name of the property. All supported properties
  308. * are defined in the aiConfig.g header (all constants share the
  309. * prefix AI_CONFIG_XXX and are simple strings).
  310. * @param iValue New value of the property
  311. * @return true if the property was set before. The new value replaces
  312. * the previous value in this case.
  313. * @note Property of different types (float, int, string ..) are kept
  314. * on different stacks, so calling SetPropertyInteger() for a
  315. * floating-point property has no effect - the loader will call
  316. * GetPropertyFloat() to read the property, but it won't be there.
  317. */
  318. bool SetPropertyInteger(const char* szName, int iValue);
  319. // -------------------------------------------------------------------
  320. /** Set a boolean configuration property. Boolean properties
  321. * are stored on the integer stack internally so it's possible
  322. * to set them via #SetPropertyBool and query them with
  323. * #GetPropertyBool and vice versa.
  324. * @see SetPropertyInteger()
  325. */
  326. bool SetPropertyBool(const char* szName, bool value) {
  327. return SetPropertyInteger(szName,value);
  328. }
  329. // -------------------------------------------------------------------
  330. /** Set a floating-point configuration property.
  331. * @see SetPropertyInteger()
  332. */
  333. bool SetPropertyFloat(const char* szName, ai_real fValue);
  334. // -------------------------------------------------------------------
  335. /** Set a string configuration property.
  336. * @see SetPropertyInteger()
  337. */
  338. bool SetPropertyString(const char* szName, const std::string& sValue);
  339. // -------------------------------------------------------------------
  340. /** Set a matrix configuration property.
  341. * @see SetPropertyInteger()
  342. */
  343. bool SetPropertyMatrix(const char* szName, const aiMatrix4x4& sValue);
  344. // -------------------------------------------------------------------
  345. /** Get a configuration property.
  346. * @param szName Name of the property. All supported properties
  347. * are defined in the aiConfig.g header (all constants share the
  348. * prefix AI_CONFIG_XXX).
  349. * @param iErrorReturn Value that is returned if the property
  350. * is not found.
  351. * @return Current value of the property
  352. * @note Property of different types (float, int, string ..) are kept
  353. * on different lists, so calling SetPropertyInteger() for a
  354. * floating-point property has no effect - the loader will call
  355. * GetPropertyFloat() to read the property, but it won't be there.
  356. */
  357. int GetPropertyInteger(const char* szName,
  358. int iErrorReturn = 0xffffffff) const;
  359. // -------------------------------------------------------------------
  360. /** Get a boolean configuration property. Boolean properties
  361. * are stored on the integer stack internally so it's possible
  362. * to set them via #SetPropertyBool and query them with
  363. * #GetPropertyBool and vice versa.
  364. * @see GetPropertyInteger()
  365. */
  366. bool GetPropertyBool(const char* szName, bool bErrorReturn = false) const {
  367. return GetPropertyInteger(szName,bErrorReturn)!=0;
  368. }
  369. // -------------------------------------------------------------------
  370. /** Get a floating-point configuration property
  371. * @see GetPropertyInteger()
  372. */
  373. ai_real GetPropertyFloat(const char* szName,
  374. ai_real fErrorReturn = 10e10f) const;
  375. // -------------------------------------------------------------------
  376. /** Get a string configuration property
  377. *
  378. * The return value remains valid until the property is modified.
  379. * @see GetPropertyInteger()
  380. */
  381. const std::string GetPropertyString(const char* szName,
  382. const std::string& sErrorReturn = "") const;
  383. // -------------------------------------------------------------------
  384. /** Get a matrix configuration property
  385. *
  386. * The return value remains valid until the property is modified.
  387. * @see GetPropertyInteger()
  388. */
  389. const aiMatrix4x4 GetPropertyMatrix(const char* szName,
  390. const aiMatrix4x4& sErrorReturn = aiMatrix4x4()) const;
  391. // -------------------------------------------------------------------
  392. /** Determine a integer configuration property has been set.
  393. * @see HasPropertyInteger()
  394. */
  395. bool HasPropertyInteger(const char* szName) const;
  396. /** Determine a boolean configuration property has been set.
  397. * @see HasPropertyBool()
  398. */
  399. bool HasPropertyBool(const char* szName) const;
  400. /** Determine a boolean configuration property has been set.
  401. * @see HasPropertyFloat()
  402. */
  403. bool HasPropertyFloat(const char* szName) const;
  404. /** Determine a String configuration property has been set.
  405. * @see HasPropertyString()
  406. */
  407. bool HasPropertyString(const char* szName) const;
  408. /** Determine a Matrix configuration property has been set.
  409. * @see HasPropertyMatrix()
  410. */
  411. bool HasPropertyMatrix(const char* szName) const;
  412. protected:
  413. /** List of integer properties */
  414. IntPropertyMap mIntProperties;
  415. /** List of floating-point properties */
  416. FloatPropertyMap mFloatProperties;
  417. /** List of string properties */
  418. StringPropertyMap mStringProperties;
  419. /** List of Matrix properties */
  420. MatrixPropertyMap mMatrixProperties;
  421. };
  422. // ----------------------------------------------------------------------------------
  423. inline
  424. const aiExportDataBlob* Exporter::ExportToBlob( const aiScene* pScene, const std::string& pFormatId,
  425. unsigned int pPreprocessing, const ExportProperties* pProperties)
  426. {
  427. return ExportToBlob(pScene,pFormatId.c_str(),pPreprocessing, pProperties);
  428. }
  429. // ----------------------------------------------------------------------------------
  430. inline
  431. aiReturn Exporter :: Export( const aiScene* pScene, const std::string& pFormatId,
  432. const std::string& pPath, unsigned int pPreprocessing,
  433. const ExportProperties* pProperties)
  434. {
  435. return Export(pScene,pFormatId.c_str(),pPath.c_str(),pPreprocessing, pProperties);
  436. }
  437. } // namespace Assimp
  438. #endif // ASSIMP_BUILD_NO_EXPORT
  439. #endif // AI_EXPORT_HPP_INC