vrshoot

view libs/assimp/PlyExporter.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_PLY_EXPORTER)
45 #include "PlyExporter.h"
46 #include "assimp/version.h"
48 using namespace Assimp;
49 namespace Assimp {
51 // ------------------------------------------------------------------------------------------------
52 // Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp
53 void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
54 {
55 // invoke the exporter
56 PlyExporter 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 .ply 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
69 #define PLY_EXPORT_HAS_NORMALS 0x1
70 #define PLY_EXPORT_HAS_TANGENTS_BITANGENTS 0x2
71 #define PLY_EXPORT_HAS_TEXCOORDS 0x4
72 #define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
74 // ------------------------------------------------------------------------------------------------
75 PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
76 : filename(_filename)
77 , pScene(pScene)
78 , endl("\n")
79 {
80 // make sure that all formatting happens using the standard, C locale and not the user's current locale
81 const std::locale& l = std::locale("C");
82 mOutput.imbue(l);
84 unsigned int faces = 0u, vertices = 0u, components = 0u;
85 for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
86 const aiMesh& m = *pScene->mMeshes[i];
87 faces += m.mNumFaces;
88 vertices += m.mNumVertices;
90 if (m.HasNormals()) {
91 components |= PLY_EXPORT_HAS_NORMALS;
92 }
93 if (m.HasTangentsAndBitangents()) {
94 components |= PLY_EXPORT_HAS_TANGENTS_BITANGENTS;
95 }
96 for (unsigned int t = 0; m.HasTextureCoords(t); ++t) {
97 components |= PLY_EXPORT_HAS_TEXCOORDS << t;
98 }
99 for (unsigned int t = 0; m.HasVertexColors(t); ++t) {
100 components |= PLY_EXPORT_HAS_COLORS << t;
101 }
102 }
104 mOutput << "ply" << endl;
105 mOutput << "format ascii 1.0" << endl;
106 mOutput << "Created by Open Asset Import Library - http://assimp.sf.net (v"
107 << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.'
108 << aiGetVersionRevision() << ")" << endl;
110 mOutput << "element vertex " << vertices << endl;
111 mOutput << "property float x" << endl;
112 mOutput << "property float y" << endl;
113 mOutput << "property float z" << endl;
115 if(components & PLY_EXPORT_HAS_NORMALS) {
116 mOutput << "property float nx" << endl;
117 mOutput << "property float ny" << endl;
118 mOutput << "property float nz" << endl;
119 }
121 // write texcoords first, just in case an importer does not support tangents
122 // bitangents and just skips over the rest of the line upon encountering
123 // unknown fields (Ply leaves pretty much every vertex component open,
124 // but in reality most importers only know about vertex positions, normals
125 // and texture coordinates).
126 for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
127 if (!c) {
128 mOutput << "property float s" << endl;
129 mOutput << "property float t" << endl;
130 }
131 else {
132 mOutput << "property float s" << c << endl;
133 mOutput << "property float t" << c << endl;
134 }
135 }
137 for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
138 if (!c) {
139 mOutput << "property float r" << endl;
140 mOutput << "property float g" << endl;
141 mOutput << "property float b" << endl;
142 mOutput << "property float a" << endl;
143 }
144 else {
145 mOutput << "property float r" << c << endl;
146 mOutput << "property float g" << c << endl;
147 mOutput << "property float b" << c << endl;
148 mOutput << "property float a" << c << endl;
149 }
150 }
152 if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
153 mOutput << "property float tx" << endl;
154 mOutput << "property float ty" << endl;
155 mOutput << "property float tz" << endl;
156 mOutput << "property float bx" << endl;
157 mOutput << "property float by" << endl;
158 mOutput << "property float bz" << endl;
159 }
161 mOutput << "element face " << faces << endl;
162 mOutput << "property list uint uint vertex_indices" << endl;
163 mOutput << "end_header" << endl;
165 for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
166 WriteMeshVerts(pScene->mMeshes[i],components);
167 }
168 for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
169 WriteMeshIndices(pScene->mMeshes[i],ofs);
170 ofs += pScene->mMeshes[i]->mNumVertices;
171 }
172 }
174 // ------------------------------------------------------------------------------------------------
175 void PlyExporter :: WriteMeshVerts(const aiMesh* m, unsigned int components)
176 {
177 for (unsigned int i = 0; i < m->mNumVertices; ++i) {
178 mOutput <<
179 m->mVertices[i].x << " " <<
180 m->mVertices[i].y << " " <<
181 m->mVertices[i].z
182 ;
183 if(components & PLY_EXPORT_HAS_NORMALS) {
184 if (m->HasNormals()) {
185 mOutput <<
186 " " << m->mNormals[i].x <<
187 " " << m->mNormals[i].y <<
188 " " << m->mNormals[i].z;
189 }
190 else {
191 mOutput << " 0.0 0.0 0.0";
192 }
193 }
195 for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
196 if (m->HasTextureCoords(c)) {
197 mOutput <<
198 " " << m->mTextureCoords[c][i].x <<
199 " " << m->mTextureCoords[c][i].y;
200 }
201 else {
202 mOutput << " -1.0 -1.0";
203 }
204 }
206 for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
207 if (m->HasVertexColors(c)) {
208 mOutput <<
209 " " << m->mColors[c][i].r <<
210 " " << m->mColors[c][i].g <<
211 " " << m->mColors[c][i].b <<
212 " " << m->mColors[c][i].a;
213 }
214 else {
215 mOutput << " -1.0 -1.0 -1.0 -1.0";
216 }
217 }
219 if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
220 if (m->HasTangentsAndBitangents()) {
221 mOutput <<
222 " " << m->mTangents[i].x <<
223 " " << m->mTangents[i].y <<
224 " " << m->mTangents[i].z <<
225 " " << m->mBitangents[i].x <<
226 " " << m->mBitangents[i].y <<
227 " " << m->mBitangents[i].z
228 ;
229 }
230 else {
231 mOutput << " 0.0 0.0 0.0 0.0 0.0 0.0";
232 }
233 }
235 mOutput << endl;
236 }
237 }
239 // ------------------------------------------------------------------------------------------------
240 void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
241 {
242 for (unsigned int i = 0; i < m->mNumFaces; ++i) {
243 const aiFace& f = m->mFaces[i];
244 mOutput << f.mNumIndices << " ";
245 for(unsigned int c = 0; c < f.mNumIndices; ++c) {
246 mOutput << (f.mIndices[c] + offset) << (c == f.mNumIndices-1 ? endl : " ");
247 }
248 }
249 }
251 #endif