Exporter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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.cpp
  35. Assimp export interface. While it's public interface bears many similarities
  36. to the import interface (in fact, it is largely symmetric), the internal
  37. implementations differs a lot. Exporters are considered stateless and are
  38. simple callbacks which we maintain in a global list along with their
  39. description strings.
  40. Here we implement only the C++ interface (Assimp::Exporter).
  41. */
  42. #include "AssimpPCH.h"
  43. #ifndef ASSIMP_BUILD_NO_EXPORT
  44. #include "DefaultIOSystem.h"
  45. #include "BlobIOSystem.h"
  46. #include "SceneCombiner.h"
  47. #include "BaseProcess.h"
  48. #include "Importer.h" // need this for GetPostProcessingStepInstanceList()
  49. #include "MakeVerboseFormat.h"
  50. #include "ConvertToLHProcess.h"
  51. #include <memory>
  52. namespace Assimp {
  53. // PostStepRegistry.cpp
  54. void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out);
  55. // ------------------------------------------------------------------------------------------------
  56. // Exporter worker function prototypes. Should not be necessary to #ifndef them, it's just a prototype
  57. void ExportSceneCollada(const char*,IOSystem*, const aiScene*);
  58. void ExportSceneObj(const char*,IOSystem*, const aiScene*);
  59. void ExportSceneSTL(const char*,IOSystem*, const aiScene*);
  60. void ExportScene3DS(const char*, IOSystem*, const aiScene*) {}
  61. /// Function pointer type of a Export worker function
  62. typedef void (*fpExportFunc)(const char*,IOSystem*,const aiScene*);
  63. // ------------------------------------------------------------------------------------------------
  64. /// Internal description of an Assimp export format option
  65. struct ExportFormatEntry
  66. {
  67. /// Public description structure to be returned by aiGetExportFormatDescription()
  68. aiExportFormatDesc mDescription;
  69. // Worker function to do the actual exporting
  70. fpExportFunc mExportFunction;
  71. // Postprocessing steps to be executed PRIOR to calling mExportFunction
  72. unsigned int mEnforcePP;
  73. // Constructor to fill all entries
  74. ExportFormatEntry( const char* pId, const char* pDesc, const char* pExtension, fpExportFunc pFunction, unsigned int pEnforcePP = 0u)
  75. {
  76. mDescription.id = pId;
  77. mDescription.description = pDesc;
  78. mDescription.fileExtension = pExtension;
  79. mExportFunction = pFunction;
  80. mEnforcePP = pEnforcePP;
  81. }
  82. };
  83. // ------------------------------------------------------------------------------------------------
  84. // global array of all export formats which Assimp supports in its current build
  85. ExportFormatEntry gExporters[] =
  86. {
  87. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  88. ExportFormatEntry( "collada", "COLLADA - Digital Asset Exchange Schema", "dae", &ExportSceneCollada),
  89. #endif
  90. #ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER
  91. ExportFormatEntry( "obj", "Wavefront OBJ format", "obj", &ExportSceneObj),
  92. #endif
  93. #ifndef ASSIMP_BUILD_NO_STL_EXPORTER
  94. ExportFormatEntry( "stl", "Stereolithography", "stl" , &ExportSceneSTL,
  95. aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_PreTransformVertices
  96. ),
  97. #endif
  98. //#ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
  99. // ExportFormatEntry( "3ds", "Autodesk 3DS (legacy format)", "3ds" , &ExportScene3DS),
  100. //#endif
  101. };
  102. class ExporterPimpl {
  103. public:
  104. ExporterPimpl()
  105. : blob()
  106. , mIOSystem(new Assimp::DefaultIOSystem())
  107. , mIsDefaultIOHandler(true)
  108. {
  109. GetPostProcessingStepInstanceList(mPostProcessingSteps);
  110. }
  111. ~ExporterPimpl()
  112. {
  113. delete blob;
  114. // Delete all post-processing plug-ins
  115. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++) {
  116. delete mPostProcessingSteps[a];
  117. }
  118. }
  119. public:
  120. aiExportDataBlob* blob;
  121. boost::shared_ptr< Assimp::IOSystem > mIOSystem;
  122. bool mIsDefaultIOHandler;
  123. /** Post processing steps we can apply at the imported data. */
  124. std::vector< BaseProcess* > mPostProcessingSteps;
  125. /** Last fatal export error */
  126. std::string mError;
  127. };
  128. #define ASSIMP_NUM_EXPORTERS (sizeof(gExporters)/sizeof(gExporters[0]))
  129. } // end of namespace Assimp
  130. using namespace Assimp;
  131. // ------------------------------------------------------------------------------------------------
  132. Exporter :: Exporter()
  133. : pimpl(new ExporterPimpl())
  134. {
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. Exporter :: ~Exporter()
  138. {
  139. FreeBlob();
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. void Exporter :: SetIOHandler( IOSystem* pIOHandler)
  143. {
  144. pimpl->mIsDefaultIOHandler = !pIOHandler;
  145. pimpl->mIOSystem.reset(pIOHandler);
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. IOSystem* Exporter :: GetIOHandler() const
  149. {
  150. return pimpl->mIOSystem.get();
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. bool Exporter :: IsDefaultIOHandler() const
  154. {
  155. return pimpl->mIsDefaultIOHandler;
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. const aiExportDataBlob* Exporter :: ExportToBlob( const aiScene* pScene, const char* pFormatId, unsigned int pPreprocessing )
  159. {
  160. if (pimpl->blob) {
  161. delete pimpl->blob;
  162. pimpl->blob = NULL;
  163. }
  164. boost::shared_ptr<IOSystem> old = pimpl->mIOSystem;
  165. BlobIOSystem* blobio = new BlobIOSystem();
  166. pimpl->mIOSystem = boost::shared_ptr<IOSystem>( blobio );
  167. if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName())) {
  168. pimpl->mIOSystem = old;
  169. return NULL;
  170. }
  171. pimpl->blob = blobio->GetBlobChain();
  172. pimpl->mIOSystem = old;
  173. return pimpl->blob;
  174. }
  175. // ------------------------------------------------------------------------------------------------
  176. aiReturn Exporter :: Export( const aiScene* pScene, const char* pFormatId, const char* pPath, unsigned int pPreprocessing )
  177. {
  178. ASSIMP_BEGIN_EXCEPTION_REGION();
  179. pimpl->mError = "";
  180. for (size_t i = 0; i < ASSIMP_NUM_EXPORTERS; ++i) {
  181. if (!strcmp(gExporters[i].mDescription.id,pFormatId)) {
  182. try {
  183. // Always create a full copy of the scene. We might optimize this one day,
  184. // but for now it is the most pragmatic way.
  185. aiScene* scenecopy_tmp;
  186. SceneCombiner::CopyScene(&scenecopy_tmp,pScene);
  187. std::auto_ptr<aiScene> scenecopy(scenecopy_tmp);
  188. const ScenePrivateData* const priv = ScenePriv(pScene);
  189. // steps that are not idempotent, i.e. we might need to run them again, usually to get back to the
  190. // original state before the step was applied first. When checking which steps we don't need
  191. // to run, those are excluded.
  192. const unsigned int nonIdempotentSteps = aiProcess_FlipWindingOrder | aiProcess_FlipUVs | aiProcess_MakeLeftHanded;
  193. // Erase all pp steps that were already applied to this scene
  194. unsigned int pp = (gExporters[i].mEnforcePP | pPreprocessing) & ~(priv
  195. ? (priv->mPPStepsApplied & ~nonIdempotentSteps)
  196. : 0u);
  197. // If no extra postprocessing was specified, and we obtained this scene from an
  198. // Assimp importer, apply the reverse steps automatically.
  199. if (!pPreprocessing && priv) {
  200. pp |= (nonIdempotentSteps & priv->mPPStepsApplied);
  201. }
  202. // If the input scene is not in verbose format, but there is at least postprocessing step that relies on it,
  203. // we need to run the MakeVerboseFormat step first.
  204. if (scenecopy->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
  205. bool verbosify = false;
  206. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  207. BaseProcess* const p = pimpl->mPostProcessingSteps[a];
  208. if (p->IsActive(pp) && p->RequireVerboseFormat()) {
  209. verbosify = true;
  210. break;
  211. }
  212. }
  213. if (verbosify || (gExporters[i].mEnforcePP & aiProcess_JoinIdenticalVertices)) {
  214. DefaultLogger::get()->debug("export: Scene data not in verbose format, applying MakeVerboseFormat step first");
  215. MakeVerboseFormatProcess proc;
  216. proc.Execute(scenecopy.get());
  217. }
  218. }
  219. if (pp) {
  220. // the three 'conversion' steps need to be executed first because all other steps rely on the standard data layout
  221. {
  222. FlipWindingOrderProcess step;
  223. if (step.IsActive(pp)) {
  224. step.Execute(scenecopy.get());
  225. }
  226. }
  227. {
  228. FlipUVsProcess step;
  229. if (step.IsActive(pp)) {
  230. step.Execute(scenecopy.get());
  231. }
  232. }
  233. {
  234. MakeLeftHandedProcess step;
  235. if (step.IsActive(pp)) {
  236. step.Execute(scenecopy.get());
  237. }
  238. }
  239. // dispatch other processes
  240. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  241. BaseProcess* const p = pimpl->mPostProcessingSteps[a];
  242. if (p->IsActive(pp)
  243. && !dynamic_cast<FlipUVsProcess*>(p)
  244. && !dynamic_cast<FlipWindingOrderProcess*>(p)
  245. && !dynamic_cast<MakeLeftHandedProcess*>(p)) {
  246. p->Execute(scenecopy.get());
  247. }
  248. }
  249. ScenePrivateData* const privOut = ScenePriv(scenecopy.get());
  250. ai_assert(privOut);
  251. privOut->mPPStepsApplied |= pp;
  252. }
  253. gExporters[i].mExportFunction(pPath,pimpl->mIOSystem.get(),scenecopy.get());
  254. }
  255. catch (DeadlyExportError& err) {
  256. pimpl->mError = err.what();
  257. return AI_FAILURE;
  258. }
  259. return AI_SUCCESS;
  260. }
  261. }
  262. pimpl->mError = std::string("Found no exporter to handle this file format: ") + pFormatId;
  263. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  264. return AI_FAILURE;
  265. }
  266. // ------------------------------------------------------------------------------------------------
  267. const char* Exporter :: GetErrorString() const
  268. {
  269. return pimpl->mError.c_str();
  270. }
  271. // ------------------------------------------------------------------------------------------------
  272. void Exporter :: FreeBlob( )
  273. {
  274. delete pimpl->blob;
  275. pimpl->blob = NULL;
  276. pimpl->mError = "";
  277. }
  278. // ------------------------------------------------------------------------------------------------
  279. const aiExportDataBlob* Exporter :: GetBlob() const
  280. {
  281. return pimpl->blob;
  282. }
  283. // ------------------------------------------------------------------------------------------------
  284. const aiExportDataBlob* Exporter :: GetOrphanedBlob() const
  285. {
  286. const aiExportDataBlob* tmp = pimpl->blob;
  287. pimpl->blob = NULL;
  288. return tmp;
  289. }
  290. // ------------------------------------------------------------------------------------------------
  291. size_t Exporter :: GetExportFormatCount() const
  292. {
  293. return ASSIMP_NUM_EXPORTERS;
  294. }
  295. // ------------------------------------------------------------------------------------------------
  296. const aiExportFormatDesc* Exporter :: GetExportFormatDescription( size_t pIndex ) const
  297. {
  298. if (pIndex >= ASSIMP_NUM_EXPORTERS) {
  299. return NULL;
  300. }
  301. return &gExporters[pIndex].mDescription;
  302. }
  303. #endif // !ASSIMP_BUILD_NO_EXPORT