vrshoot

diff libs/assimp/OFFLoader.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/OFFLoader.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,224 @@
     1.4 +/*
     1.5 +---------------------------------------------------------------------------
     1.6 +Open Asset Import Library (assimp)
     1.7 +---------------------------------------------------------------------------
     1.8 +
     1.9 +Copyright (c) 2006-2012, assimp team
    1.10 +
    1.11 +All rights reserved.
    1.12 +
    1.13 +Redistribution and use of this software in source and binary forms, 
    1.14 +with or without modification, are permitted provided that the following 
    1.15 +conditions are met:
    1.16 +
    1.17 +* Redistributions of source code must retain the above
    1.18 +  copyright notice, this list of conditions and the
    1.19 +  following disclaimer.
    1.20 +
    1.21 +* Redistributions in binary form must reproduce the above
    1.22 +  copyright notice, this list of conditions and the
    1.23 +  following disclaimer in the documentation and/or other
    1.24 +  materials provided with the distribution.
    1.25 +
    1.26 +* Neither the name of the assimp team, nor the names of its
    1.27 +  contributors may be used to endorse or promote products
    1.28 +  derived from this software without specific prior
    1.29 +  written permission of the assimp team.
    1.30 +
    1.31 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.32 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.33 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.34 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.35 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.36 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.37 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.38 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.39 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.40 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.41 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.42 +---------------------------------------------------------------------------
    1.43 +*/
    1.44 +
    1.45 +/** @file  OFFLoader.cpp
    1.46 + *  @brief Implementation of the OFF importer class 
    1.47 + */
    1.48 +
    1.49 +#include "AssimpPCH.h"
    1.50 +#ifndef ASSIMP_BUILD_NO_OFF_IMPORTER
    1.51 +
    1.52 +// internal headers
    1.53 +#include "OFFLoader.h"
    1.54 +#include "ParsingUtils.h"
    1.55 +#include "fast_atof.h"
    1.56 +
    1.57 +
    1.58 +using namespace Assimp;
    1.59 +
    1.60 +static const aiImporterDesc desc = {
    1.61 +	"OFF Importer",
    1.62 +	"",
    1.63 +	"",
    1.64 +	"",
    1.65 +	aiImporterFlags_SupportBinaryFlavour,
    1.66 +	0,
    1.67 +	0,
    1.68 +	0,
    1.69 +	0,
    1.70 +	"off" 
    1.71 +};
    1.72 +
    1.73 +// ------------------------------------------------------------------------------------------------
    1.74 +// Constructor to be privately used by Importer
    1.75 +OFFImporter::OFFImporter()
    1.76 +{}
    1.77 +
    1.78 +// ------------------------------------------------------------------------------------------------
    1.79 +// Destructor, private as well 
    1.80 +OFFImporter::~OFFImporter()
    1.81 +{}
    1.82 +
    1.83 +// ------------------------------------------------------------------------------------------------
    1.84 +// Returns whether the class can handle the format of the given file. 
    1.85 +bool OFFImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
    1.86 +{
    1.87 +	const std::string extension = GetExtension(pFile);
    1.88 +
    1.89 +	if (extension == "off")
    1.90 +		return true;
    1.91 +	else if (!extension.length() || checkSig)
    1.92 +	{
    1.93 +		if (!pIOHandler)return true;
    1.94 +		const char* tokens[] = {"off"};
    1.95 +		return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
    1.96 +	}
    1.97 +	return false;
    1.98 +}
    1.99 +
   1.100 +// ------------------------------------------------------------------------------------------------
   1.101 +const aiImporterDesc* OFFImporter::GetInfo () const
   1.102 +{
   1.103 +	return &desc;
   1.104 +}
   1.105 +
   1.106 +// ------------------------------------------------------------------------------------------------
   1.107 +// Imports the given file into the given scene structure. 
   1.108 +void OFFImporter::InternReadFile( const std::string& pFile, 
   1.109 +	aiScene* pScene, IOSystem* pIOHandler)
   1.110 +{
   1.111 +	boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
   1.112 +
   1.113 +	// Check whether we can read from the file
   1.114 +	if( file.get() == NULL) {
   1.115 +		throw DeadlyImportError( "Failed to open OFF file " + pFile + ".");
   1.116 +	}
   1.117 +	
   1.118 +	// allocate storage and copy the contents of the file to a memory buffer
   1.119 +	std::vector<char> mBuffer2;
   1.120 +	TextFileToBuffer(file.get(),mBuffer2);
   1.121 +	const char* buffer = &mBuffer2[0];
   1.122 +
   1.123 +	char line[4096];
   1.124 +	GetNextLine(buffer,line);
   1.125 +	if ('O' == line[0]) {
   1.126 +		GetNextLine(buffer,line); // skip the 'OFF' line
   1.127 +	}
   1.128 +
   1.129 +	const char* sz = line; SkipSpaces(&sz);
   1.130 +	const unsigned int numVertices = strtoul10(sz,&sz);SkipSpaces(&sz);
   1.131 +	const unsigned int numFaces = strtoul10(sz,&sz);
   1.132 +
   1.133 +	pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = 1 ];
   1.134 +	aiMesh* mesh = pScene->mMeshes[0] = new aiMesh();
   1.135 +	aiFace* faces = mesh->mFaces = new aiFace [mesh->mNumFaces = numFaces];
   1.136 +
   1.137 +	std::vector<aiVector3D> tempPositions(numVertices);
   1.138 +
   1.139 +	// now read all vertex lines
   1.140 +	for (unsigned int i = 0; i< numVertices;++i)
   1.141 +	{
   1.142 +		if(!GetNextLine(buffer,line))
   1.143 +		{
   1.144 +			DefaultLogger::get()->error("OFF: The number of verts in the header is incorrect");
   1.145 +			break;
   1.146 +		}
   1.147 +		aiVector3D& v = tempPositions[i];
   1.148 +
   1.149 +		sz = line; SkipSpaces(&sz);
   1.150 +		sz = fast_atoreal_move<float>(sz,(float&)v.x); SkipSpaces(&sz);
   1.151 +		sz = fast_atoreal_move<float>(sz,(float&)v.y); SkipSpaces(&sz);
   1.152 +		fast_atoreal_move<float>(sz,(float&)v.z);
   1.153 +	}
   1.154 +
   1.155 +	
   1.156 +	// First find out how many vertices we'll need
   1.157 +	const char* old = buffer;
   1.158 +	for (unsigned int i = 0; i< mesh->mNumFaces;++i)
   1.159 +	{
   1.160 +		if(!GetNextLine(buffer,line))
   1.161 +		{
   1.162 +			DefaultLogger::get()->error("OFF: The number of faces in the header is incorrect");
   1.163 +			break;
   1.164 +		}
   1.165 +		sz = line;SkipSpaces(&sz);
   1.166 +		if(!(faces->mNumIndices = strtoul10(sz,&sz)) || faces->mNumIndices > 9)
   1.167 +		{
   1.168 +			DefaultLogger::get()->error("OFF: Faces with zero indices aren't allowed");
   1.169 +			--mesh->mNumFaces;
   1.170 +			continue;
   1.171 +		}
   1.172 +		mesh->mNumVertices += faces->mNumIndices;
   1.173 +		++faces;
   1.174 +	}
   1.175 +
   1.176 +	if (!mesh->mNumVertices)
   1.177 +		throw DeadlyImportError("OFF: There are no valid faces");
   1.178 +
   1.179 +	// allocate storage for the output vertices
   1.180 +	aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
   1.181 +
   1.182 +	// second: now parse all face indices
   1.183 +	buffer = old;faces = mesh->mFaces;
   1.184 +	for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;)
   1.185 +	{
   1.186 +		if(!GetNextLine(buffer,line))break;
   1.187 +
   1.188 +		unsigned int idx;
   1.189 +		sz = line;SkipSpaces(&sz);
   1.190 +		if(!(idx = strtoul10(sz,&sz)) || idx > 9)
   1.191 +			continue;
   1.192 +
   1.193 +		faces->mIndices = new unsigned int [faces->mNumIndices];
   1.194 +		for (unsigned int m = 0; m < faces->mNumIndices;++m)
   1.195 +		{
   1.196 +			SkipSpaces(&sz);
   1.197 +			if ((idx = strtoul10(sz,&sz)) >= numVertices)
   1.198 +			{
   1.199 +				DefaultLogger::get()->error("OFF: Vertex index is out of range");
   1.200 +				idx = numVertices-1;
   1.201 +			}
   1.202 +			faces->mIndices[m] = p++;
   1.203 +			*verts++ = tempPositions[idx];
   1.204 +		}
   1.205 +		++i;
   1.206 +		++faces;
   1.207 +	}
   1.208 +	
   1.209 +	// generate the output node graph
   1.210 +	pScene->mRootNode = new aiNode();
   1.211 +	pScene->mRootNode->mName.Set("<OFFRoot>");
   1.212 +	pScene->mRootNode->mMeshes = new unsigned int [pScene->mRootNode->mNumMeshes = 1];
   1.213 +	pScene->mRootNode->mMeshes[0] = 0;
   1.214 +
   1.215 +	// generate a default material
   1.216 +	pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = 1];
   1.217 +	aiMaterial* pcMat = new aiMaterial();
   1.218 +
   1.219 +	aiColor4D clr(0.6f,0.6f,0.6f,1.0f);
   1.220 +	pcMat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
   1.221 +	pScene->mMaterials[0] = pcMat;
   1.222 +
   1.223 +	const int twosided =1;
   1.224 +	pcMat->AddProperty(&twosided,1,AI_MATKEY_TWOSIDED);
   1.225 +}
   1.226 +
   1.227 +#endif // !! ASSIMP_BUILD_NO_OFF_IMPORTER