Exporter.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 "JoinVerticesProcess.h"
  50. #include "MakeVerboseFormat.h"
  51. #include "ConvertToLHProcess.h"
  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. // do not use const, because some exporter need to convert the scene temporary
  58. void ExportSceneCollada(const char*,IOSystem*, const aiScene*);
  59. void ExportSceneXFile(const char*,IOSystem*, const aiScene*);
  60. void ExportSceneObj(const char*,IOSystem*, const aiScene*);
  61. void ExportSceneSTL(const char*,IOSystem*, const aiScene*);
  62. void ExportSceneSTLBinary(const char*,IOSystem*, const aiScene*);
  63. void ExportScenePly(const char*,IOSystem*, const aiScene*);
  64. void ExportScene3DS(const char*, IOSystem*, const aiScene*);
  65. void ExportSceneAssbin(const char*, IOSystem*, const aiScene*);
  66. // ------------------------------------------------------------------------------------------------
  67. // global array of all export formats which Assimp supports in its current build
  68. Exporter::ExportFormatEntry gExporters[] =
  69. {
  70. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  71. Exporter::ExportFormatEntry( "collada", "COLLADA - Digital Asset Exchange Schema", "dae", &ExportSceneCollada),
  72. #endif
  73. #ifndef ASSIMP_BUILD_NO_FXILE_EXPORTER
  74. Exporter::ExportFormatEntry( "x", "X Files", "x", &ExportSceneXFile,
  75. aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_FlipUVs),
  76. #endif
  77. #ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER
  78. Exporter::ExportFormatEntry( "obj", "Wavefront OBJ format", "obj", &ExportSceneObj,
  79. aiProcess_GenSmoothNormals /*| aiProcess_PreTransformVertices */),
  80. #endif
  81. #ifndef ASSIMP_BUILD_NO_STL_EXPORTER
  82. Exporter::ExportFormatEntry( "stl", "Stereolithography", "stl" , &ExportSceneSTL,
  83. aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_PreTransformVertices
  84. ),
  85. Exporter::ExportFormatEntry( "stlb", "Stereolithography (binary)", "stl" , &ExportSceneSTLBinary,
  86. aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_PreTransformVertices
  87. ),
  88. #endif
  89. #ifndef ASSIMP_BUILD_NO_PLY_EXPORTER
  90. Exporter::ExportFormatEntry( "ply", "Stanford Polygon Library", "ply" , &ExportScenePly,
  91. aiProcess_PreTransformVertices
  92. ),
  93. #endif
  94. #ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
  95. Exporter::ExportFormatEntry( "3ds", "Autodesk 3DS (legacy)", "3ds" , &ExportScene3DS,
  96. aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_JoinIdenticalVertices),
  97. #endif
  98. #ifndef ASSIMP_BUILD_NO_ASSBIN_EXPORTER
  99. Exporter::ExportFormatEntry( "assbin", "Assimp Binary", "assbin" , &ExportSceneAssbin, NULL),
  100. #endif
  101. };
  102. #define ASSIMP_NUM_EXPORTERS (sizeof(gExporters)/sizeof(gExporters[0]))
  103. class ExporterPimpl {
  104. public:
  105. ExporterPimpl()
  106. : blob()
  107. , mIOSystem(new Assimp::DefaultIOSystem())
  108. , mIsDefaultIOHandler(true)
  109. {
  110. GetPostProcessingStepInstanceList(mPostProcessingSteps);
  111. // grab all builtin exporters
  112. mExporters.resize(ASSIMP_NUM_EXPORTERS);
  113. std::copy(gExporters,gExporters+ASSIMP_NUM_EXPORTERS,mExporters.begin());
  114. }
  115. ~ExporterPimpl()
  116. {
  117. delete blob;
  118. // Delete all post-processing plug-ins
  119. for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++) {
  120. delete mPostProcessingSteps[a];
  121. }
  122. }
  123. public:
  124. aiExportDataBlob* blob;
  125. boost::shared_ptr< Assimp::IOSystem > mIOSystem;
  126. bool mIsDefaultIOHandler;
  127. /** Post processing steps we can apply at the imported data. */
  128. std::vector< BaseProcess* > mPostProcessingSteps;
  129. /** Last fatal export error */
  130. std::string mError;
  131. /** Exporters, this includes those registered using #Assimp::Exporter::RegisterExporter */
  132. std::vector<Exporter::ExportFormatEntry> mExporters;
  133. };
  134. } // end of namespace Assimp
  135. using namespace Assimp;
  136. // ------------------------------------------------------------------------------------------------
  137. Exporter :: Exporter()
  138. : pimpl(new ExporterPimpl())
  139. {
  140. }
  141. // ------------------------------------------------------------------------------------------------
  142. Exporter :: ~Exporter()
  143. {
  144. FreeBlob();
  145. delete pimpl;
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. void Exporter :: SetIOHandler( IOSystem* pIOHandler)
  149. {
  150. pimpl->mIsDefaultIOHandler = !pIOHandler;
  151. pimpl->mIOSystem.reset(pIOHandler);
  152. }
  153. // ------------------------------------------------------------------------------------------------
  154. IOSystem* Exporter :: GetIOHandler() const
  155. {
  156. return pimpl->mIOSystem.get();
  157. }
  158. // ------------------------------------------------------------------------------------------------
  159. bool Exporter :: IsDefaultIOHandler() const
  160. {
  161. return pimpl->mIsDefaultIOHandler;
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. const aiExportDataBlob* Exporter :: ExportToBlob( const aiScene* pScene, const char* pFormatId, unsigned int )
  165. {
  166. if (pimpl->blob) {
  167. delete pimpl->blob;
  168. pimpl->blob = NULL;
  169. }
  170. boost::shared_ptr<IOSystem> old = pimpl->mIOSystem;
  171. BlobIOSystem* blobio = new BlobIOSystem();
  172. pimpl->mIOSystem = boost::shared_ptr<IOSystem>( blobio );
  173. if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName())) {
  174. pimpl->mIOSystem = old;
  175. return NULL;
  176. }
  177. pimpl->blob = blobio->GetBlobChain();
  178. pimpl->mIOSystem = old;
  179. return pimpl->blob;
  180. }
  181. // ------------------------------------------------------------------------------------------------
  182. bool IsVerboseFormat(const aiMesh* mesh)
  183. {
  184. // avoid slow vector<bool> specialization
  185. std::vector<unsigned int> seen(mesh->mNumVertices,0);
  186. for(unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  187. const aiFace& f = mesh->mFaces[i];
  188. for(unsigned int j = 0; j < f.mNumIndices; ++j) {
  189. if(++seen[f.mIndices[j]] == 2) {
  190. // found a duplicate index
  191. return false;
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. // ------------------------------------------------------------------------------------------------
  198. bool IsVerboseFormat(const aiScene* pScene)
  199. {
  200. for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
  201. if(!IsVerboseFormat(pScene->mMeshes[i])) {
  202. return false;
  203. }
  204. }
  205. return true;
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. aiReturn Exporter :: Export( const aiScene* pScene, const char* pFormatId, const char* pPath, unsigned int pPreprocessing )
  209. {
  210. ASSIMP_BEGIN_EXCEPTION_REGION();
  211. // when they create scenes from scratch, users will likely create them not in verbose
  212. // format. They will likely not be aware that there is a flag in the scene to indicate
  213. // this, however. To avoid surprises and bug reports, we check for duplicates in
  214. // meshes upfront.
  215. const bool is_verbose_format = !(pScene->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) || IsVerboseFormat(pScene);
  216. pimpl->mError = "";
  217. for (size_t i = 0; i < pimpl->mExporters.size(); ++i) {
  218. const Exporter::ExportFormatEntry& exp = pimpl->mExporters[i];
  219. if (!strcmp(exp.mDescription.id,pFormatId)) {
  220. try {
  221. // Always create a full copy of the scene. We might optimize this one day,
  222. // but for now it is the most pragmatic way.
  223. aiScene* scenecopy_tmp;
  224. SceneCombiner::CopyScene(&scenecopy_tmp,pScene);
  225. std::auto_ptr<aiScene> scenecopy(scenecopy_tmp);
  226. const ScenePrivateData* const priv = ScenePriv(pScene);
  227. // steps that are not idempotent, i.e. we might need to run them again, usually to get back to the
  228. // original state before the step was applied first. When checking which steps we don't need
  229. // to run, those are excluded.
  230. const unsigned int nonIdempotentSteps = aiProcess_FlipWindingOrder | aiProcess_FlipUVs | aiProcess_MakeLeftHanded;
  231. // Erase all pp steps that were already applied to this scene
  232. const unsigned int pp = (exp.mEnforcePP | pPreprocessing) & ~(priv && !priv->mIsCopy
  233. ? (priv->mPPStepsApplied & ~nonIdempotentSteps)
  234. : 0u);
  235. // If no extra postprocessing was specified, and we obtained this scene from an
  236. // Assimp importer, apply the reverse steps automatically.
  237. // TODO: either drop this, or document it. Otherwise it is just a bad surprise.
  238. //if (!pPreprocessing && priv) {
  239. // pp |= (nonIdempotentSteps & priv->mPPStepsApplied);
  240. //}
  241. // If the input scene is not in verbose format, but there is at least postprocessing step that relies on it,
  242. // we need to run the MakeVerboseFormat step first.
  243. bool must_join_again = false;
  244. if (!is_verbose_format) {
  245. bool verbosify = false;
  246. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  247. BaseProcess* const p = pimpl->mPostProcessingSteps[a];
  248. if (p->IsActive(pp) && p->RequireVerboseFormat()) {
  249. verbosify = true;
  250. break;
  251. }
  252. }
  253. if (verbosify || (exp.mEnforcePP & aiProcess_JoinIdenticalVertices)) {
  254. DefaultLogger::get()->debug("export: Scene data not in verbose format, applying MakeVerboseFormat step first");
  255. MakeVerboseFormatProcess proc;
  256. proc.Execute(scenecopy.get());
  257. if(!(exp.mEnforcePP & aiProcess_JoinIdenticalVertices)) {
  258. must_join_again = true;
  259. }
  260. }
  261. }
  262. if (pp) {
  263. // the three 'conversion' steps need to be executed first because all other steps rely on the standard data layout
  264. {
  265. FlipWindingOrderProcess step;
  266. if (step.IsActive(pp)) {
  267. step.Execute(scenecopy.get());
  268. }
  269. }
  270. {
  271. FlipUVsProcess step;
  272. if (step.IsActive(pp)) {
  273. step.Execute(scenecopy.get());
  274. }
  275. }
  276. {
  277. MakeLeftHandedProcess step;
  278. if (step.IsActive(pp)) {
  279. step.Execute(scenecopy.get());
  280. }
  281. }
  282. // dispatch other processes
  283. for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
  284. BaseProcess* const p = pimpl->mPostProcessingSteps[a];
  285. if (p->IsActive(pp)
  286. && !dynamic_cast<FlipUVsProcess*>(p)
  287. && !dynamic_cast<FlipWindingOrderProcess*>(p)
  288. && !dynamic_cast<MakeLeftHandedProcess*>(p)) {
  289. p->Execute(scenecopy.get());
  290. }
  291. }
  292. ScenePrivateData* const privOut = ScenePriv(scenecopy.get());
  293. ai_assert(privOut);
  294. privOut->mPPStepsApplied |= pp;
  295. }
  296. if(must_join_again) {
  297. JoinVerticesProcess proc;
  298. proc.Execute(scenecopy.get());
  299. }
  300. exp.mExportFunction(pPath,pimpl->mIOSystem.get(),scenecopy.get());
  301. }
  302. catch (DeadlyExportError& err) {
  303. pimpl->mError = err.what();
  304. return AI_FAILURE;
  305. }
  306. return AI_SUCCESS;
  307. }
  308. }
  309. pimpl->mError = std::string("Found no exporter to handle this file format: ") + pFormatId;
  310. ASSIMP_END_EXCEPTION_REGION(aiReturn);
  311. return AI_FAILURE;
  312. }
  313. // ------------------------------------------------------------------------------------------------
  314. const char* Exporter :: GetErrorString() const
  315. {
  316. return pimpl->mError.c_str();
  317. }
  318. // ------------------------------------------------------------------------------------------------
  319. void Exporter :: FreeBlob( )
  320. {
  321. delete pimpl->blob;
  322. pimpl->blob = NULL;
  323. pimpl->mError = "";
  324. }
  325. // ------------------------------------------------------------------------------------------------
  326. const aiExportDataBlob* Exporter :: GetBlob() const
  327. {
  328. return pimpl->blob;
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. const aiExportDataBlob* Exporter :: GetOrphanedBlob() const
  332. {
  333. const aiExportDataBlob* tmp = pimpl->blob;
  334. pimpl->blob = NULL;
  335. return tmp;
  336. }
  337. // ------------------------------------------------------------------------------------------------
  338. size_t Exporter :: GetExportFormatCount() const
  339. {
  340. return pimpl->mExporters.size();
  341. }
  342. // ------------------------------------------------------------------------------------------------
  343. const aiExportFormatDesc* Exporter :: GetExportFormatDescription( size_t pIndex ) const
  344. {
  345. if (pIndex >= GetExportFormatCount()) {
  346. return NULL;
  347. }
  348. // Return from static storage if the requested index is built-in.
  349. if (pIndex < sizeof(gExporters) / sizeof(gExporters[0])) {
  350. return &gExporters[pIndex].mDescription;
  351. }
  352. return &pimpl->mExporters[pIndex].mDescription;
  353. }
  354. // ------------------------------------------------------------------------------------------------
  355. aiReturn Exporter :: RegisterExporter(const ExportFormatEntry& desc)
  356. {
  357. BOOST_FOREACH(const ExportFormatEntry& e, pimpl->mExporters) {
  358. if (!strcmp(e.mDescription.id,desc.mDescription.id)) {
  359. return aiReturn_FAILURE;
  360. }
  361. }
  362. pimpl->mExporters.push_back(desc);
  363. return aiReturn_SUCCESS;
  364. }
  365. // ------------------------------------------------------------------------------------------------
  366. void Exporter :: UnregisterExporter(const char* id)
  367. {
  368. for(std::vector<ExportFormatEntry>::iterator it = pimpl->mExporters.begin(); it != pimpl->mExporters.end(); ++it) {
  369. if (!strcmp((*it).mDescription.id,id)) {
  370. pimpl->mExporters.erase(it);
  371. break;
  372. }
  373. }
  374. }
  375. #endif // !ASSIMP_BUILD_NO_EXPORT