vrshoot

view libs/assimp/STLExporter.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 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 #include "AssimpPCH.h"
43 #if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_STL_EXPORTER)
45 #include "STLExporter.h"
46 #include "assimp/version.h"
48 using namespace Assimp;
49 namespace Assimp {
51 // ------------------------------------------------------------------------------------------------
52 // Worker function for exporting a scene to Stereolithograpy. Prototyped and registered in Exporter.cpp
53 void ExportSceneSTL(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
54 {
55 // invoke the exporter
56 STLExporter exporter(pFile, pScene);
58 // we're still here - export successfully completed. Write the file.
59 boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
60 if(outfile == NULL) {
61 throw DeadlyExportError("could not open output .stl file: " + std::string(pFile));
62 }
64 outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
65 }
67 } // end of namespace Assimp
70 // ------------------------------------------------------------------------------------------------
71 STLExporter :: STLExporter(const char* _filename, const aiScene* pScene)
72 : filename(_filename)
73 , pScene(pScene)
74 , endl("\n")
75 {
76 // make sure that all formatting happens using the standard, C locale and not the user's current locale
77 const std::locale& l = std::locale("C");
78 mOutput.imbue(l);
80 const std::string& name = "AssimpScene";
82 mOutput << "solid " << name << endl;
83 for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
84 WriteMesh(pScene->mMeshes[i]);
85 }
86 mOutput << "endsolid " << name << endl;
87 }
89 // ------------------------------------------------------------------------------------------------
90 void STLExporter :: WriteMesh(const aiMesh* m)
91 {
92 for (unsigned int i = 0; i < m->mNumFaces; ++i) {
93 const aiFace& f = m->mFaces[i];
95 // we need per-face normals. We specified aiProcess_GenNormals as pre-requisite for this exporter,
96 // but nonetheless we have to expect per-vertex normals.
97 aiVector3D nor;
98 if (m->mNormals) {
99 for(unsigned int a = 0; a < f.mNumIndices; ++a) {
100 nor += m->mNormals[f.mIndices[a]];
101 }
102 nor.Normalize();
103 }
104 mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl;
105 mOutput << " outer loop" << endl;
106 for(unsigned int a = 0; a < f.mNumIndices; ++a) {
107 const aiVector3D& v = m->mVertices[f.mIndices[a]];
108 mOutput << " vertex " << v.x << " " << v.y << " " << v.z << endl;
109 }
111 mOutput << " endloop" << endl;
112 mOutput << " endfacet" << endl << endl;
113 }
114 }
116 #endif