Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

469 řádky
14 KiB

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