vrshoot

diff libs/assimp/STLLoader.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/STLLoader.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,412 @@
     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 Implementation of the STL importer class */
    1.46 +
    1.47 +#include "AssimpPCH.h"
    1.48 +#ifndef ASSIMP_BUILD_NO_STL_IMPORTER
    1.49 +
    1.50 +// internal headers
    1.51 +#include "STLLoader.h"
    1.52 +#include "ParsingUtils.h"
    1.53 +#include "fast_atof.h"
    1.54 +
    1.55 +using namespace Assimp;
    1.56 +
    1.57 +static const aiImporterDesc desc = {
    1.58 +	"Stereolithography (STL) Importer",
    1.59 +	"",
    1.60 +	"",
    1.61 +	"",
    1.62 +	aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
    1.63 +	0,
    1.64 +	0,
    1.65 +	0,
    1.66 +	0,
    1.67 +	"stl" 
    1.68 +};
    1.69 +
    1.70 +// ------------------------------------------------------------------------------------------------
    1.71 +// Constructor to be privately used by Importer
    1.72 +STLImporter::STLImporter()
    1.73 +{}
    1.74 +
    1.75 +// ------------------------------------------------------------------------------------------------
    1.76 +// Destructor, private as well 
    1.77 +STLImporter::~STLImporter()
    1.78 +{}
    1.79 +
    1.80 +// ------------------------------------------------------------------------------------------------
    1.81 +// Returns whether the class can handle the format of the given file. 
    1.82 +bool STLImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
    1.83 +{
    1.84 +	const std::string extension = GetExtension(pFile);
    1.85 +
    1.86 +	if (extension == "stl")
    1.87 +		return true;
    1.88 +	else if (!extension.length() || checkSig)	{
    1.89 +		if (!pIOHandler)
    1.90 +			return true;
    1.91 +		const char* tokens[] = {"STL","solid"};
    1.92 +		return SearchFileHeaderForToken(pIOHandler,pFile,tokens,2);
    1.93 +	}
    1.94 +	return false;
    1.95 +}
    1.96 +
    1.97 +// ------------------------------------------------------------------------------------------------
    1.98 +const aiImporterDesc* STLImporter::GetInfo () const
    1.99 +{
   1.100 +	return &desc;
   1.101 +}
   1.102 +
   1.103 +// ------------------------------------------------------------------------------------------------
   1.104 +// Imports the given file into the given scene structure. 
   1.105 +void STLImporter::InternReadFile( const std::string& pFile, 
   1.106 +	aiScene* pScene, IOSystem* pIOHandler)
   1.107 +{
   1.108 +	boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
   1.109 +
   1.110 +	// Check whether we can read from the file
   1.111 +	if( file.get() == NULL)	{
   1.112 +		throw DeadlyImportError( "Failed to open STL file " + pFile + ".");
   1.113 +	}
   1.114 +
   1.115 +	fileSize = (unsigned int)file->FileSize();
   1.116 +
   1.117 +	// allocate storage and copy the contents of the file to a memory buffer
   1.118 +	// (terminate it with zero)
   1.119 +	std::vector<char> mBuffer2;
   1.120 +	TextFileToBuffer(file.get(),mBuffer2);
   1.121 +
   1.122 +	this->pScene = pScene;
   1.123 +	this->mBuffer = &mBuffer2[0];
   1.124 +
   1.125 +	// the default vertex color is white
   1.126 +	clrColorDefault.r = clrColorDefault.g = clrColorDefault.b = clrColorDefault.a = 1.0f;
   1.127 +
   1.128 +	// allocate one mesh
   1.129 +	pScene->mNumMeshes = 1;
   1.130 +	pScene->mMeshes = new aiMesh*[1];
   1.131 +	aiMesh* pMesh = pScene->mMeshes[0] = new aiMesh();
   1.132 +	pMesh->mMaterialIndex = 0;
   1.133 +
   1.134 +	// allocate a single node
   1.135 +	pScene->mRootNode = new aiNode();
   1.136 +	pScene->mRootNode->mNumMeshes = 1;
   1.137 +	pScene->mRootNode->mMeshes = new unsigned int[1];
   1.138 +	pScene->mRootNode->mMeshes[0] = 0;
   1.139 +
   1.140 +	bool bMatClr = false;
   1.141 +
   1.142 +	// check whether the file starts with 'solid' -
   1.143 +	// in this case we can simply assume it IS a text file. finished.
   1.144 +	if (!::strncmp(mBuffer,"solid",5)) {
   1.145 +		LoadASCIIFile();
   1.146 +	}
   1.147 +	else bMatClr = LoadBinaryFile();
   1.148 +
   1.149 +	// now copy faces
   1.150 +	pMesh->mFaces = new aiFace[pMesh->mNumFaces];
   1.151 +	for (unsigned int i = 0, p = 0; i < pMesh->mNumFaces;++i)	{
   1.152 +
   1.153 +		aiFace& face = pMesh->mFaces[i];
   1.154 +		face.mIndices = new unsigned int[face.mNumIndices = 3];
   1.155 +		for (unsigned int o = 0; o < 3;++o,++p) {
   1.156 +			face.mIndices[o] = p;
   1.157 +		}
   1.158 +	}
   1.159 +
   1.160 +	// create a single default material - everything white, as we have vertex colors
   1.161 +	aiMaterial* pcMat = new aiMaterial();
   1.162 +	aiString s;
   1.163 +	s.Set(AI_DEFAULT_MATERIAL_NAME);
   1.164 +	pcMat->AddProperty(&s, AI_MATKEY_NAME);
   1.165 +
   1.166 +	aiColor4D clrDiffuse(1.0f,1.0f,1.0f,1.0f);
   1.167 +	if (bMatClr) {
   1.168 +		clrDiffuse = clrColorDefault;
   1.169 +	}
   1.170 +	pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
   1.171 +	pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
   1.172 +	clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
   1.173 +	pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
   1.174 +
   1.175 +	pScene->mNumMaterials = 1;
   1.176 +	pScene->mMaterials = new aiMaterial*[1];
   1.177 +	pScene->mMaterials[0] = pcMat;
   1.178 +}
   1.179 +// ------------------------------------------------------------------------------------------------
   1.180 +// Read an ASCII STL file
   1.181 +void STLImporter::LoadASCIIFile()
   1.182 +{
   1.183 +	aiMesh* pMesh = pScene->mMeshes[0];
   1.184 +
   1.185 +	const char* sz = mBuffer + 5; // skip the "solid"
   1.186 +	SkipSpaces(&sz);
   1.187 +	const char* szMe = sz;
   1.188 +	while (!::IsSpaceOrNewLine(*sz)) {
   1.189 +		sz++;
   1.190 +	}
   1.191 +
   1.192 +	size_t temp;
   1.193 +	// setup the name of the node
   1.194 +	if ((temp = (size_t)(sz-szMe)))	{
   1.195 +
   1.196 +		pScene->mRootNode->mName.length = temp;
   1.197 +		memcpy(pScene->mRootNode->mName.data,szMe,temp);
   1.198 +		pScene->mRootNode->mName.data[temp] = '\0';
   1.199 +	}
   1.200 +	else pScene->mRootNode->mName.Set("<STL_ASCII>");
   1.201 +
   1.202 +	// try to guess how many vertices we could have
   1.203 +	// assume we'll need 160 bytes for each face
   1.204 +	pMesh->mNumVertices = ( pMesh->mNumFaces = std::max(1u,fileSize / 160u )) * 3;
   1.205 +	pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
   1.206 +	pMesh->mNormals  = new aiVector3D[pMesh->mNumVertices];
   1.207 +	
   1.208 +	unsigned int curFace = 0, curVertex = 3;
   1.209 +	for ( ;; )
   1.210 +	{
   1.211 +		// go to the next token
   1.212 +		if(!SkipSpacesAndLineEnd(&sz))
   1.213 +		{
   1.214 +			// seems we're finished although there was no end marker
   1.215 +			DefaultLogger::get()->warn("STL: unexpected EOF. \'endsolid\' keyword was expected");
   1.216 +			break;
   1.217 +		}
   1.218 +		// facet normal -0.13 -0.13 -0.98
   1.219 +		if (!strncmp(sz,"facet",5) && IsSpaceOrNewLine(*(sz+5)))	{
   1.220 +
   1.221 +			if (3 != curVertex) {
   1.222 +				DefaultLogger::get()->warn("STL: A new facet begins but the old is not yet complete");
   1.223 +			}
   1.224 +			if (pMesh->mNumFaces == curFace)	{
   1.225 +				ai_assert(pMesh->mNumFaces != 0);
   1.226 +
   1.227 +				// need to resize the arrays, our size estimate was wrong
   1.228 +				unsigned int iNeededSize = (unsigned int)(sz-mBuffer) / pMesh->mNumFaces;
   1.229 +				if (iNeededSize <= 160)iNeededSize >>= 1; // prevent endless looping
   1.230 +				unsigned int add = (unsigned int)((mBuffer+fileSize)-sz) / iNeededSize;
   1.231 +				add += add >> 3; // add 12.5% as buffer
   1.232 +				iNeededSize = (pMesh->mNumFaces + add)*3;
   1.233 +				aiVector3D* pv = new aiVector3D[iNeededSize];
   1.234 +				memcpy(pv,pMesh->mVertices,pMesh->mNumVertices*sizeof(aiVector3D));
   1.235 +				delete[] pMesh->mVertices;
   1.236 +				pMesh->mVertices = pv;
   1.237 +				pv = new aiVector3D[iNeededSize];
   1.238 +				memcpy(pv,pMesh->mNormals,pMesh->mNumVertices*sizeof(aiVector3D));
   1.239 +				delete[] pMesh->mNormals;
   1.240 +				pMesh->mNormals = pv;
   1.241 +
   1.242 +				pMesh->mNumVertices = iNeededSize;
   1.243 +				pMesh->mNumFaces += add;
   1.244 +			}
   1.245 +			aiVector3D* vn = &pMesh->mNormals[curFace++*3];
   1.246 +
   1.247 +			sz += 6;
   1.248 +			curVertex = 0;
   1.249 +			SkipSpaces(&sz);
   1.250 +			if (strncmp(sz,"normal",6))	{
   1.251 +				DefaultLogger::get()->warn("STL: a facet normal vector was expected but not found");
   1.252 +			}
   1.253 +			else
   1.254 +			{
   1.255 +				sz += 7;
   1.256 +				SkipSpaces(&sz);
   1.257 +				sz = fast_atoreal_move<float>(sz, (float&)vn->x ); 
   1.258 +				SkipSpaces(&sz);
   1.259 +				sz = fast_atoreal_move<float>(sz, (float&)vn->y ); 
   1.260 +				SkipSpaces(&sz);
   1.261 +				sz = fast_atoreal_move<float>(sz, (float&)vn->z ); 
   1.262 +				*(vn+1) = *vn;
   1.263 +				*(vn+2) = *vn;
   1.264 +			}
   1.265 +		}
   1.266 +		// vertex 1.50000 1.50000 0.00000
   1.267 +		else if (!strncmp(sz,"vertex",6) && ::IsSpaceOrNewLine(*(sz+6)))
   1.268 +		{
   1.269 +			if (3 == curVertex)	{
   1.270 +				DefaultLogger::get()->error("STL: a facet with more than 3 vertices has been found");
   1.271 +			}
   1.272 +			else
   1.273 +			{
   1.274 +				sz += 7;
   1.275 +				SkipSpaces(&sz);
   1.276 +				aiVector3D* vn = &pMesh->mVertices[(curFace-1)*3 + curVertex++];
   1.277 +				sz = fast_atoreal_move<float>(sz, (float&)vn->x ); 
   1.278 +				SkipSpaces(&sz);
   1.279 +				sz = fast_atoreal_move<float>(sz, (float&)vn->y ); 
   1.280 +				SkipSpaces(&sz);
   1.281 +				sz = fast_atoreal_move<float>(sz, (float&)vn->z ); 
   1.282 +			}
   1.283 +		}
   1.284 +		else if (!::strncmp(sz,"endsolid",8))	{
   1.285 +			// finished!
   1.286 +			break;
   1.287 +		}
   1.288 +		// else skip the whole identifier
   1.289 +		else while (!::IsSpaceOrNewLine(*sz)) {
   1.290 +			++sz;
   1.291 +		}
   1.292 +	}
   1.293 +
   1.294 +	if (!curFace)	{
   1.295 +		pMesh->mNumFaces = 0;
   1.296 +		throw DeadlyImportError("STL: ASCII file is empty or invalid; no data loaded");
   1.297 +	}
   1.298 +	pMesh->mNumFaces = curFace;
   1.299 +	pMesh->mNumVertices = curFace*3;
   1.300 +	// we are finished!
   1.301 +}
   1.302 +
   1.303 +// ------------------------------------------------------------------------------------------------
   1.304 +// Read a binary STL file
   1.305 +bool STLImporter::LoadBinaryFile()
   1.306 +{
   1.307 +	// skip the first 80 bytes
   1.308 +	if (fileSize < 84) {
   1.309 +		throw DeadlyImportError("STL: file is too small for the header");
   1.310 +	}
   1.311 +	bool bIsMaterialise = false;
   1.312 +
   1.313 +	// search for an occurence of "COLOR=" in the header
   1.314 +	const char* sz2 = (const char*)mBuffer;
   1.315 +	const char* const szEnd = sz2+80;
   1.316 +	while (sz2 < szEnd)	{
   1.317 +
   1.318 +		if ('C' == *sz2++ && 'O' == *sz2++ && 'L' == *sz2++ &&
   1.319 +			'O' == *sz2++ && 'R' == *sz2++ && '=' == *sz2++)	{
   1.320 +
   1.321 +			// read the default vertex color for facets
   1.322 +			bIsMaterialise = true;
   1.323 +			DefaultLogger::get()->info("STL: Taking code path for Materialise files");
   1.324 +			clrColorDefault.r = (*sz2++) / 255.0f;
   1.325 +			clrColorDefault.g = (*sz2++) / 255.0f;
   1.326 +			clrColorDefault.b = (*sz2++) / 255.0f;
   1.327 +			clrColorDefault.a = (*sz2++) / 255.0f;
   1.328 +			break;
   1.329 +		}
   1.330 +	}
   1.331 +	const unsigned char* sz = (const unsigned char*)mBuffer + 80;
   1.332 +
   1.333 +	// now read the number of facets
   1.334 +	aiMesh* pMesh = pScene->mMeshes[0];
   1.335 +	pScene->mRootNode->mName.Set("<STL_BINARY>");
   1.336 +
   1.337 +	pMesh->mNumFaces = *((uint32_t*)sz);
   1.338 +	sz += 4;
   1.339 +
   1.340 +	if (fileSize < 84 + pMesh->mNumFaces*50) {
   1.341 +		throw DeadlyImportError("STL: file is too small to hold all facets");
   1.342 +	}
   1.343 +
   1.344 +	if (!pMesh->mNumFaces) {
   1.345 +		throw DeadlyImportError("STL: file is empty. There are no facets defined");
   1.346 +	}
   1.347 +
   1.348 +	pMesh->mNumVertices = pMesh->mNumFaces*3;
   1.349 +
   1.350 +	aiVector3D* vp,*vn;
   1.351 +	vp = pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
   1.352 +	vn = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
   1.353 +
   1.354 +	for (unsigned int i = 0; i < pMesh->mNumFaces;++i)	{
   1.355 +
   1.356 +		// NOTE: Blender sometimes writes empty normals ... this is not
   1.357 +		// our fault ... the RemoveInvalidData helper step should fix that
   1.358 +		*vn = *((aiVector3D*)sz);
   1.359 +		sz += sizeof(aiVector3D);
   1.360 +		*(vn+1) = *vn;
   1.361 +		*(vn+2) = *vn;
   1.362 +		vn += 3;
   1.363 +
   1.364 +		*vp++ = *((aiVector3D*)sz);
   1.365 +		sz += sizeof(aiVector3D);
   1.366 +
   1.367 +		*vp++ = *((aiVector3D*)sz);
   1.368 +		sz += sizeof(aiVector3D);
   1.369 +
   1.370 +		*vp++ = *((aiVector3D*)sz);
   1.371 +		sz += sizeof(aiVector3D);
   1.372 +
   1.373 +		uint16_t color = *((uint16_t*)sz);
   1.374 +		sz += 2;
   1.375 +
   1.376 +		if (color & (1 << 15))
   1.377 +		{
   1.378 +			// seems we need to take the color
   1.379 +			if (!pMesh->mColors[0])
   1.380 +			{
   1.381 +				pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
   1.382 +				for (unsigned int i = 0; i <pMesh->mNumVertices;++i)
   1.383 +					*pMesh->mColors[0]++ = this->clrColorDefault;
   1.384 +				pMesh->mColors[0] -= pMesh->mNumVertices;
   1.385 +
   1.386 +				DefaultLogger::get()->info("STL: Mesh has vertex colors");
   1.387 +			}
   1.388 +			aiColor4D* clr = &pMesh->mColors[0][i*3];
   1.389 +			clr->a = 1.0f;
   1.390 +			if (bIsMaterialise) // this is reversed
   1.391 +			{
   1.392 +				clr->r = (color & 0x31u) / 31.0f;
   1.393 +				clr->g = ((color & (0x31u<<5))>>5u) / 31.0f;
   1.394 +				clr->b = ((color & (0x31u<<10))>>10u) / 31.0f;
   1.395 +			}
   1.396 +			else
   1.397 +			{
   1.398 +				clr->b = (color & 0x31u) / 31.0f;
   1.399 +				clr->g = ((color & (0x31u<<5))>>5u) / 31.0f;
   1.400 +				clr->r = ((color & (0x31u<<10))>>10u) / 31.0f;
   1.401 +			}
   1.402 +			// assign the color to all vertices of the face
   1.403 +			*(clr+1) = *clr;
   1.404 +			*(clr+2) = *clr;
   1.405 +		}
   1.406 +	}
   1.407 +	if (bIsMaterialise && !pMesh->mColors[0])
   1.408 +	{
   1.409 +		// use the color as diffuse material color
   1.410 +		return true;
   1.411 +	}
   1.412 +	return false;
   1.413 +}
   1.414 +
   1.415 +#endif // !! ASSIMP_BUILD_NO_STL_IMPORTER