vrshoot

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/PlyExporter.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,251 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +#include "AssimpPCH.h"
    1.45 +
    1.46 +#if !defined(ASSIMP_BUILD_NO_EXPORT) && !defined(ASSIMP_BUILD_NO_PLY_EXPORTER)
    1.47 +
    1.48 +#include "PlyExporter.h"
    1.49 +#include "assimp/version.h"
    1.50 +
    1.51 +using namespace Assimp;
    1.52 +namespace Assimp	{
    1.53 +
    1.54 +// ------------------------------------------------------------------------------------------------
    1.55 +// Worker function for exporting a scene to PLY. Prototyped and registered in Exporter.cpp
    1.56 +void ExportScenePly(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
    1.57 +{
    1.58 +	// invoke the exporter 
    1.59 +	PlyExporter exporter(pFile, pScene);
    1.60 +
    1.61 +	// we're still here - export successfully completed. Write the file.
    1.62 +	boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
    1.63 +	if(outfile == NULL) {
    1.64 +		throw DeadlyExportError("could not open output .ply file: " + std::string(pFile));
    1.65 +	}
    1.66 +
    1.67 +	outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
    1.68 +}
    1.69 +
    1.70 +} // end of namespace Assimp
    1.71 +
    1.72 +#define PLY_EXPORT_HAS_NORMALS 0x1
    1.73 +#define PLY_EXPORT_HAS_TANGENTS_BITANGENTS 0x2
    1.74 +#define PLY_EXPORT_HAS_TEXCOORDS 0x4
    1.75 +#define PLY_EXPORT_HAS_COLORS (PLY_EXPORT_HAS_TEXCOORDS << AI_MAX_NUMBER_OF_TEXTURECOORDS)
    1.76 +
    1.77 +// ------------------------------------------------------------------------------------------------
    1.78 +PlyExporter :: PlyExporter(const char* _filename, const aiScene* pScene)
    1.79 +: filename(_filename)
    1.80 +, pScene(pScene)
    1.81 +, endl("\n") 
    1.82 +{
    1.83 +	// make sure that all formatting happens using the standard, C locale and not the user's current locale
    1.84 +	const std::locale& l = std::locale("C");
    1.85 +	mOutput.imbue(l);
    1.86 +
    1.87 +	unsigned int faces = 0u, vertices = 0u, components = 0u;
    1.88 +	for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
    1.89 +		const aiMesh& m = *pScene->mMeshes[i];
    1.90 +		faces += m.mNumFaces;
    1.91 +		vertices += m.mNumVertices;
    1.92 +
    1.93 +		if (m.HasNormals()) {
    1.94 +			components |= PLY_EXPORT_HAS_NORMALS;
    1.95 +		}
    1.96 +		if (m.HasTangentsAndBitangents()) {
    1.97 +			components |= PLY_EXPORT_HAS_TANGENTS_BITANGENTS;
    1.98 +		}
    1.99 +		for (unsigned int t = 0; m.HasTextureCoords(t); ++t) {
   1.100 +			components |= PLY_EXPORT_HAS_TEXCOORDS << t;
   1.101 +		}
   1.102 +		for (unsigned int t = 0; m.HasVertexColors(t); ++t) {
   1.103 +			components |= PLY_EXPORT_HAS_COLORS << t;
   1.104 +		}
   1.105 +	}
   1.106 +
   1.107 +	mOutput << "ply" << endl;
   1.108 +	mOutput << "format ascii 1.0" << endl;
   1.109 +	mOutput << "Created by Open Asset Import Library - http://assimp.sf.net (v"
   1.110 +		<< aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' 
   1.111 +		<< aiGetVersionRevision() << ")" << endl;
   1.112 +
   1.113 +	mOutput << "element vertex " << vertices << endl;
   1.114 +	mOutput << "property float x" << endl;
   1.115 +	mOutput << "property float y" << endl;
   1.116 +	mOutput << "property float z" << endl;
   1.117 +
   1.118 +	if(components & PLY_EXPORT_HAS_NORMALS) {
   1.119 +		mOutput << "property float nx" << endl;
   1.120 +		mOutput << "property float ny" << endl;
   1.121 +		mOutput << "property float nz" << endl;
   1.122 +	}
   1.123 +
   1.124 +	// write texcoords first, just in case an importer does not support tangents
   1.125 +	// bitangents and just skips over the rest of the line upon encountering
   1.126 +	// unknown fields (Ply leaves pretty much every vertex component open,
   1.127 +	// but in reality most importers only know about vertex positions, normals
   1.128 +	// and texture coordinates).
   1.129 +	for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
   1.130 +		if (!c) {
   1.131 +			mOutput << "property float s" << endl;
   1.132 +			mOutput << "property float t" << endl;
   1.133 +		}
   1.134 +		else {
   1.135 +			mOutput << "property float s" << c << endl;
   1.136 +			mOutput << "property float t" << c << endl;
   1.137 +		}
   1.138 +	}
   1.139 +
   1.140 +	for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
   1.141 +		if (!c) {
   1.142 +			mOutput << "property float r" << endl;
   1.143 +			mOutput << "property float g" << endl;
   1.144 +			mOutput << "property float b" << endl;
   1.145 +			mOutput << "property float a" << endl;
   1.146 +		}
   1.147 +		else {
   1.148 +			mOutput << "property float r" << c << endl;
   1.149 +			mOutput << "property float g" << c << endl;
   1.150 +			mOutput << "property float b" << c << endl;
   1.151 +			mOutput << "property float a" << c << endl;
   1.152 +		}
   1.153 +	}
   1.154 +
   1.155 +	if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
   1.156 +		mOutput << "property float tx" << endl;
   1.157 +		mOutput << "property float ty" << endl;
   1.158 +		mOutput << "property float tz" << endl;
   1.159 +		mOutput << "property float bx" << endl;
   1.160 +		mOutput << "property float by" << endl;
   1.161 +		mOutput << "property float bz" << endl;
   1.162 +	}
   1.163 +
   1.164 +	mOutput << "element face " << faces << endl;
   1.165 +	mOutput << "property list uint uint vertex_indices" << endl;
   1.166 +	mOutput << "end_header" << endl;
   1.167 +
   1.168 +	for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
   1.169 +		WriteMeshVerts(pScene->mMeshes[i],components);
   1.170 +	}
   1.171 +	for (unsigned int i = 0, ofs = 0; i < pScene->mNumMeshes; ++i) {
   1.172 +		WriteMeshIndices(pScene->mMeshes[i],ofs);
   1.173 +		ofs += pScene->mMeshes[i]->mNumVertices;
   1.174 +	}
   1.175 +}
   1.176 +
   1.177 +// ------------------------------------------------------------------------------------------------
   1.178 +void PlyExporter :: WriteMeshVerts(const aiMesh* m, unsigned int components)
   1.179 +{
   1.180 +	for (unsigned int i = 0; i < m->mNumVertices; ++i) {
   1.181 +		mOutput << 
   1.182 +			m->mVertices[i].x << " " << 
   1.183 +			m->mVertices[i].y << " " << 
   1.184 +			m->mVertices[i].z
   1.185 +		;
   1.186 +		if(components & PLY_EXPORT_HAS_NORMALS) {
   1.187 +			if (m->HasNormals()) {
   1.188 +				mOutput << 
   1.189 +				" " << m->mNormals[i].x << 
   1.190 +				" " << m->mNormals[i].y << 
   1.191 +				" " << m->mNormals[i].z;
   1.192 +			}
   1.193 +			else {
   1.194 +				mOutput << " 0.0 0.0 0.0"; 
   1.195 +			}
   1.196 +		}
   1.197 +
   1.198 +		for (unsigned int n = PLY_EXPORT_HAS_TEXCOORDS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_TEXTURECOORDS; n <<= 1, ++c) {
   1.199 +			if (m->HasTextureCoords(c)) {
   1.200 +				mOutput << 
   1.201 +					" " << m->mTextureCoords[c][i].x << 
   1.202 +					" " << m->mTextureCoords[c][i].y;
   1.203 +			}
   1.204 +			else {
   1.205 +				mOutput << " -1.0 -1.0"; 
   1.206 +			}
   1.207 +		}
   1.208 +
   1.209 +		for (unsigned int n = PLY_EXPORT_HAS_COLORS, c = 0; (components & n) && c != AI_MAX_NUMBER_OF_COLOR_SETS; n <<= 1, ++c) {
   1.210 +			if (m->HasVertexColors(c)) {
   1.211 +				mOutput << 
   1.212 +					" " << m->mColors[c][i].r << 
   1.213 +					" " << m->mColors[c][i].g <<
   1.214 +					" " << m->mColors[c][i].b <<
   1.215 +					" " << m->mColors[c][i].a;
   1.216 +			}
   1.217 +			else {
   1.218 +				mOutput << " -1.0 -1.0 -1.0 -1.0"; 
   1.219 +			}
   1.220 +		}
   1.221 +
   1.222 +		if(components & PLY_EXPORT_HAS_TANGENTS_BITANGENTS) {
   1.223 +			if (m->HasTangentsAndBitangents()) {
   1.224 +				mOutput << 
   1.225 +				" " << m->mTangents[i].x << 
   1.226 +				" " << m->mTangents[i].y << 
   1.227 +				" " << m->mTangents[i].z << 
   1.228 +				" " << m->mBitangents[i].x << 
   1.229 +				" " << m->mBitangents[i].y << 
   1.230 +				" " << m->mBitangents[i].z
   1.231 +				;
   1.232 +			}
   1.233 +			else {
   1.234 +				mOutput << " 0.0 0.0 0.0 0.0 0.0 0.0"; 
   1.235 +			}
   1.236 +		}
   1.237 +
   1.238 +		mOutput << endl;
   1.239 +	}
   1.240 +}
   1.241 +
   1.242 +// ------------------------------------------------------------------------------------------------
   1.243 +void PlyExporter :: WriteMeshIndices(const aiMesh* m, unsigned int offset)
   1.244 +{
   1.245 +	for (unsigned int i = 0; i < m->mNumFaces; ++i) {
   1.246 +		const aiFace& f = m->mFaces[i];
   1.247 +		mOutput << f.mNumIndices << " ";
   1.248 +		for(unsigned int c = 0; c < f.mNumIndices; ++c) {
   1.249 +			mOutput << (f.mIndices[c] + offset) << (c == f.mNumIndices-1 ? endl : " ");
   1.250 +		}
   1.251 +	}
   1.252 +}
   1.253 +
   1.254 +#endif