Exporter.hpp 21 KB

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