Exporter.hpp 21 KB

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