Exporter.cpp 13 KB

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