vrshoot
view libs/assimp/Exporter.cpp @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line source
1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
6 Copyright (c) 2006-2012, assimp team
8 All rights reserved.
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
42 /** @file Exporter.cpp
44 Assimp export interface. While it's public interface bears many similarities
45 to the import interface (in fact, it is largely symmetric), the internal
46 implementations differs a lot. Exporters are considered stateless and are
47 simple callbacks which we maintain in a global list along with their
48 description strings.
50 Here we implement only the C++ interface (Assimp::Exporter).
51 */
53 #include "AssimpPCH.h"
55 #ifndef ASSIMP_BUILD_NO_EXPORT
57 #include "DefaultIOSystem.h"
58 #include "BlobIOSystem.h"
59 #include "SceneCombiner.h"
60 #include "BaseProcess.h"
61 #include "Importer.h" // need this for GetPostProcessingStepInstanceList()
63 #include "MakeVerboseFormat.h"
64 #include "ConvertToLHProcess.h"
66 namespace Assimp {
68 // PostStepRegistry.cpp
69 void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out);
71 // ------------------------------------------------------------------------------------------------
72 // Exporter worker function prototypes. Should not be necessary to #ifndef them, it's just a prototype
73 void ExportSceneCollada(const char*,IOSystem*, const aiScene*);
74 void ExportSceneObj(const char*,IOSystem*, const aiScene*);
75 void ExportSceneSTL(const char*,IOSystem*, const aiScene*);
76 void ExportScenePly(const char*,IOSystem*, const aiScene*);
77 void ExportScene3DS(const char*, IOSystem*, const aiScene*) {}
79 // ------------------------------------------------------------------------------------------------
80 // global array of all export formats which Assimp supports in its current build
81 Exporter::ExportFormatEntry gExporters[] =
82 {
83 #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
84 Exporter::ExportFormatEntry( "collada", "COLLADA - Digital Asset Exchange Schema", "dae", &ExportSceneCollada),
85 #endif
87 #ifndef ASSIMP_BUILD_NO_OBJ_EXPORTER
88 Exporter::ExportFormatEntry( "obj", "Wavefront OBJ format", "obj", &ExportSceneObj,
89 aiProcess_GenNormals | aiProcess_PreTransformVertices),
90 #endif
92 #ifndef ASSIMP_BUILD_NO_STL_EXPORTER
93 Exporter::ExportFormatEntry( "stl", "Stereolithography", "stl" , &ExportSceneSTL,
94 aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_PreTransformVertices
95 ),
96 #endif
98 #ifndef ASSIMP_BUILD_NO_PLY_EXPORTER
99 Exporter::ExportFormatEntry( "ply", "Stanford Polygon Library", "ply" , &ExportScenePly,
100 aiProcess_PreTransformVertices
101 ),
102 #endif
104 //#ifndef ASSIMP_BUILD_NO_3DS_EXPORTER
105 // ExportFormatEntry( "3ds", "Autodesk 3DS (legacy format)", "3ds" , &ExportScene3DS),
106 //#endif
107 };
109 #define ASSIMP_NUM_EXPORTERS (sizeof(gExporters)/sizeof(gExporters[0]))
112 class ExporterPimpl {
113 public:
115 ExporterPimpl()
116 : blob()
117 , mIOSystem(new Assimp::DefaultIOSystem())
118 , mIsDefaultIOHandler(true)
119 {
120 GetPostProcessingStepInstanceList(mPostProcessingSteps);
122 // grab all builtin exporters
123 mExporters.resize(ASSIMP_NUM_EXPORTERS);
124 std::copy(gExporters,gExporters+ASSIMP_NUM_EXPORTERS,mExporters.begin());
125 }
127 ~ExporterPimpl()
128 {
129 delete blob;
131 // Delete all post-processing plug-ins
132 for( unsigned int a = 0; a < mPostProcessingSteps.size(); a++) {
133 delete mPostProcessingSteps[a];
134 }
135 }
137 public:
139 aiExportDataBlob* blob;
140 boost::shared_ptr< Assimp::IOSystem > mIOSystem;
141 bool mIsDefaultIOHandler;
143 /** Post processing steps we can apply at the imported data. */
144 std::vector< BaseProcess* > mPostProcessingSteps;
146 /** Last fatal export error */
147 std::string mError;
149 /** Exporters, this includes those registered using #Assimp::Exporter::RegisterExporter */
150 std::vector<Exporter::ExportFormatEntry> mExporters;
151 };
154 } // end of namespace Assimp
160 using namespace Assimp;
163 // ------------------------------------------------------------------------------------------------
164 Exporter :: Exporter()
165 : pimpl(new ExporterPimpl())
166 {
167 }
170 // ------------------------------------------------------------------------------------------------
171 Exporter :: ~Exporter()
172 {
173 FreeBlob();
174 }
177 // ------------------------------------------------------------------------------------------------
178 void Exporter :: SetIOHandler( IOSystem* pIOHandler)
179 {
180 pimpl->mIsDefaultIOHandler = !pIOHandler;
181 pimpl->mIOSystem.reset(pIOHandler);
182 }
185 // ------------------------------------------------------------------------------------------------
186 IOSystem* Exporter :: GetIOHandler() const
187 {
188 return pimpl->mIOSystem.get();
189 }
192 // ------------------------------------------------------------------------------------------------
193 bool Exporter :: IsDefaultIOHandler() const
194 {
195 return pimpl->mIsDefaultIOHandler;
196 }
199 // ------------------------------------------------------------------------------------------------
200 const aiExportDataBlob* Exporter :: ExportToBlob( const aiScene* pScene, const char* pFormatId, unsigned int pPreprocessing )
201 {
202 if (pimpl->blob) {
203 delete pimpl->blob;
204 pimpl->blob = NULL;
205 }
208 boost::shared_ptr<IOSystem> old = pimpl->mIOSystem;
210 BlobIOSystem* blobio = new BlobIOSystem();
211 pimpl->mIOSystem = boost::shared_ptr<IOSystem>( blobio );
213 if (AI_SUCCESS != Export(pScene,pFormatId,blobio->GetMagicFileName())) {
214 pimpl->mIOSystem = old;
215 return NULL;
216 }
218 pimpl->blob = blobio->GetBlobChain();
219 pimpl->mIOSystem = old;
221 return pimpl->blob;
222 }
225 // ------------------------------------------------------------------------------------------------
226 aiReturn Exporter :: Export( const aiScene* pScene, const char* pFormatId, const char* pPath, unsigned int pPreprocessing )
227 {
228 ASSIMP_BEGIN_EXCEPTION_REGION();
230 pimpl->mError = "";
231 for (size_t i = 0; i < pimpl->mExporters.size(); ++i) {
232 const Exporter::ExportFormatEntry& exp = pimpl->mExporters[i];
233 if (!strcmp(exp.mDescription.id,pFormatId)) {
235 try {
237 // Always create a full copy of the scene. We might optimize this one day,
238 // but for now it is the most pragmatic way.
239 aiScene* scenecopy_tmp;
240 SceneCombiner::CopyScene(&scenecopy_tmp,pScene);
242 std::auto_ptr<aiScene> scenecopy(scenecopy_tmp);
243 const ScenePrivateData* const priv = ScenePriv(pScene);
245 // steps that are not idempotent, i.e. we might need to run them again, usually to get back to the
246 // original state before the step was applied first. When checking which steps we don't need
247 // to run, those are excluded.
248 const unsigned int nonIdempotentSteps = aiProcess_FlipWindingOrder | aiProcess_FlipUVs | aiProcess_MakeLeftHanded;
250 // Erase all pp steps that were already applied to this scene
251 unsigned int pp = (exp.mEnforcePP | pPreprocessing) & ~(priv
252 ? (priv->mPPStepsApplied & ~nonIdempotentSteps)
253 : 0u);
255 // If no extra postprocessing was specified, and we obtained this scene from an
256 // Assimp importer, apply the reverse steps automatically.
257 if (!pPreprocessing && priv) {
258 pp |= (nonIdempotentSteps & priv->mPPStepsApplied);
259 }
261 // If the input scene is not in verbose format, but there is at least postprocessing step that relies on it,
262 // we need to run the MakeVerboseFormat step first.
263 if (scenecopy->mFlags & AI_SCENE_FLAGS_NON_VERBOSE_FORMAT) {
265 bool verbosify = false;
266 for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
267 BaseProcess* const p = pimpl->mPostProcessingSteps[a];
269 if (p->IsActive(pp) && p->RequireVerboseFormat()) {
270 verbosify = true;
271 break;
272 }
273 }
275 if (verbosify || (exp.mEnforcePP & aiProcess_JoinIdenticalVertices)) {
276 DefaultLogger::get()->debug("export: Scene data not in verbose format, applying MakeVerboseFormat step first");
278 MakeVerboseFormatProcess proc;
279 proc.Execute(scenecopy.get());
280 }
281 }
283 if (pp) {
284 // the three 'conversion' steps need to be executed first because all other steps rely on the standard data layout
285 {
286 FlipWindingOrderProcess step;
287 if (step.IsActive(pp)) {
288 step.Execute(scenecopy.get());
289 }
290 }
292 {
293 FlipUVsProcess step;
294 if (step.IsActive(pp)) {
295 step.Execute(scenecopy.get());
296 }
297 }
299 {
300 MakeLeftHandedProcess step;
301 if (step.IsActive(pp)) {
302 step.Execute(scenecopy.get());
303 }
304 }
306 // dispatch other processes
307 for( unsigned int a = 0; a < pimpl->mPostProcessingSteps.size(); a++) {
308 BaseProcess* const p = pimpl->mPostProcessingSteps[a];
310 if (p->IsActive(pp)
311 && !dynamic_cast<FlipUVsProcess*>(p)
312 && !dynamic_cast<FlipWindingOrderProcess*>(p)
313 && !dynamic_cast<MakeLeftHandedProcess*>(p)) {
315 p->Execute(scenecopy.get());
316 }
317 }
318 ScenePrivateData* const privOut = ScenePriv(scenecopy.get());
319 ai_assert(privOut);
321 privOut->mPPStepsApplied |= pp;
322 }
324 exp.mExportFunction(pPath,pimpl->mIOSystem.get(),scenecopy.get());
325 }
326 catch (DeadlyExportError& err) {
327 pimpl->mError = err.what();
328 return AI_FAILURE;
329 }
330 return AI_SUCCESS;
331 }
332 }
334 pimpl->mError = std::string("Found no exporter to handle this file format: ") + pFormatId;
335 ASSIMP_END_EXCEPTION_REGION(aiReturn);
336 return AI_FAILURE;
337 }
340 // ------------------------------------------------------------------------------------------------
341 const char* Exporter :: GetErrorString() const
342 {
343 return pimpl->mError.c_str();
344 }
347 // ------------------------------------------------------------------------------------------------
348 void Exporter :: FreeBlob( )
349 {
350 delete pimpl->blob;
351 pimpl->blob = NULL;
353 pimpl->mError = "";
354 }
357 // ------------------------------------------------------------------------------------------------
358 const aiExportDataBlob* Exporter :: GetBlob() const
359 {
360 return pimpl->blob;
361 }
364 // ------------------------------------------------------------------------------------------------
365 const aiExportDataBlob* Exporter :: GetOrphanedBlob() const
366 {
367 const aiExportDataBlob* tmp = pimpl->blob;
368 pimpl->blob = NULL;
369 return tmp;
370 }
373 // ------------------------------------------------------------------------------------------------
374 size_t Exporter :: GetExportFormatCount() const
375 {
376 return pimpl->mExporters.size();
377 }
379 // ------------------------------------------------------------------------------------------------
380 const aiExportFormatDesc* Exporter :: GetExportFormatDescription( size_t pIndex ) const
381 {
382 if (pIndex >= GetExportFormatCount()) {
383 return NULL;
384 }
386 return &pimpl->mExporters[pIndex].mDescription;
387 }
389 // ------------------------------------------------------------------------------------------------
390 aiReturn Exporter :: RegisterExporter(const ExportFormatEntry& desc)
391 {
392 BOOST_FOREACH(const ExportFormatEntry& e, pimpl->mExporters) {
393 if (!strcmp(e.mDescription.id,desc.mDescription.id)) {
394 return aiReturn_FAILURE;
395 }
396 }
398 pimpl->mExporters.push_back(desc);
399 return aiReturn_SUCCESS;
400 }
403 // ------------------------------------------------------------------------------------------------
404 void Exporter :: UnregisterExporter(const char* id)
405 {
406 for(std::vector<ExportFormatEntry>::iterator it = pimpl->mExporters.begin(); it != pimpl->mExporters.end(); ++it) {
407 if (!strcmp((*it).mDescription.id,id)) {
408 pimpl->mExporters.erase(it);
409 break;
410 }
411 }
412 }
414 #endif // !ASSIMP_BUILD_NO_EXPORT