2008-08-06 23:01:38 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2010-04-10 15:30:22 +00:00
|
|
|
Copyright (c) 2006-2010, ASSIMP Development Team
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the ASSIMP team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the ASSIMP Development Team.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the STL importer class */
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_STL_IMPORTER
|
2008-10-13 16:45:48 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// internal headers
|
|
|
|
#include "STLLoader.h"
|
|
|
|
#include "ParsingUtils.h"
|
|
|
|
#include "fast_atof.h"
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
STLImporter::STLImporter()
|
2009-01-18 23:48:25 +00:00
|
|
|
{}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
STLImporter::~STLImporter()
|
2009-01-18 23:48:25 +00:00
|
|
|
{}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the class can handle the format of the given file.
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
bool STLImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
const std::string extension = GetExtension(pFile);
|
|
|
|
|
|
|
|
if (extension == "stl")
|
|
|
|
return true;
|
|
|
|
else if (!extension.length() || checkSig) {
|
|
|
|
if (!pIOHandler)
|
|
|
|
return true;
|
|
|
|
const char* tokens[] = {"STL","solid"};
|
|
|
|
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-03-02 17:38:01 +00:00
|
|
|
void STLImporter::GetExtensionList(std::set<std::string>& extensions)
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
{
|
2010-03-02 17:38:01 +00:00
|
|
|
extensions.insert("stl");
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Imports the given file into the given scene structure.
|
2009-08-21 22:49:58 +00:00
|
|
|
void STLImporter::InternReadFile( const std::string& pFile,
|
|
|
|
aiScene* pScene, IOSystem* pIOHandler)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
|
|
|
|
|
|
|
|
// Check whether we can read from the file
|
2009-08-21 22:49:58 +00:00
|
|
|
if( file.get() == NULL) {
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Failed to open STL file " + pFile + ".");
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 22:49:58 +00:00
|
|
|
fileSize = (unsigned int)file->FileSize();
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// allocate storage and copy the contents of the file to a memory buffer
|
|
|
|
// (terminate it with zero)
|
2009-08-21 22:49:58 +00:00
|
|
|
std::vector<char> mBuffer2;
|
|
|
|
TextFileToBuffer(file.get(),mBuffer2);
|
2008-08-13 15:45:57 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
this->pScene = pScene;
|
2008-08-13 15:45:57 +00:00
|
|
|
this->mBuffer = &mBuffer2[0];
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
// the default vertex color is white
|
|
|
|
clrColorDefault.r = clrColorDefault.g = clrColorDefault.b = clrColorDefault.a = 1.0f;
|
|
|
|
|
|
|
|
// allocate one mesh
|
|
|
|
pScene->mNumMeshes = 1;
|
|
|
|
pScene->mMeshes = new aiMesh*[1];
|
|
|
|
aiMesh* pMesh = pScene->mMeshes[0] = new aiMesh();
|
|
|
|
pMesh->mMaterialIndex = 0;
|
|
|
|
|
|
|
|
// allocate a single node
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mNumMeshes = 1;
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[1];
|
|
|
|
pScene->mRootNode->mMeshes[0] = 0;
|
|
|
|
|
|
|
|
bool bMatClr = false;
|
2008-08-13 15:45:57 +00:00
|
|
|
|
|
|
|
// check whether the file starts with 'solid' -
|
|
|
|
// in this case we can simply assume it IS a text file. finished.
|
2009-08-21 22:49:58 +00:00
|
|
|
if (!::strncmp(mBuffer,"solid",5)) {
|
|
|
|
LoadASCIIFile();
|
|
|
|
}
|
|
|
|
else bMatClr = LoadBinaryFile();
|
2008-08-13 15:45:57 +00:00
|
|
|
|
|
|
|
// now copy faces
|
|
|
|
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
|
2009-08-21 22:49:58 +00:00
|
|
|
for (unsigned int i = 0, p = 0; i < pMesh->mNumFaces;++i) {
|
|
|
|
|
2008-08-13 15:45:57 +00:00
|
|
|
aiFace& face = pMesh->mFaces[i];
|
|
|
|
face.mIndices = new unsigned int[face.mNumIndices = 3];
|
2009-08-21 22:49:58 +00:00
|
|
|
for (unsigned int o = 0; o < 3;++o,++p) {
|
2008-08-13 15:45:57 +00:00
|
|
|
face.mIndices[o] = p;
|
2009-08-21 22:49:58 +00:00
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
2008-08-13 15:45:57 +00:00
|
|
|
// create a single default material - everything white, as we have vertex colors
|
2011-08-22 20:22:51 +00:00
|
|
|
aiMaterial* pcMat = new aiMaterial();
|
2008-08-06 23:01:38 +00:00
|
|
|
aiString s;
|
|
|
|
s.Set(AI_DEFAULT_MATERIAL_NAME);
|
|
|
|
pcMat->AddProperty(&s, AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
aiColor4D clrDiffuse(1.0f,1.0f,1.0f,1.0f);
|
2009-08-21 22:49:58 +00:00
|
|
|
if (bMatClr) {
|
|
|
|
clrDiffuse = clrColorDefault;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
clrDiffuse = aiColor4D(0.05f,0.05f,0.05f,1.0f);
|
|
|
|
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
|
|
|
|
|
|
|
|
pScene->mNumMaterials = 1;
|
|
|
|
pScene->mMaterials = new aiMaterial*[1];
|
|
|
|
pScene->mMaterials[0] = pcMat;
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read an ASCII STL file
|
|
|
|
void STLImporter::LoadASCIIFile()
|
|
|
|
{
|
|
|
|
aiMesh* pMesh = pScene->mMeshes[0];
|
|
|
|
|
|
|
|
const char* sz = mBuffer + 5; // skip the "solid"
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
const char* szMe = sz;
|
2009-08-21 22:49:58 +00:00
|
|
|
while (!::IsSpaceOrNewLine(*sz)) {
|
|
|
|
sz++;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
2009-08-21 22:49:58 +00:00
|
|
|
size_t temp;
|
2008-08-06 23:01:38 +00:00
|
|
|
// setup the name of the node
|
2009-08-21 22:49:58 +00:00
|
|
|
if ((temp = (size_t)(sz-szMe))) {
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
pScene->mRootNode->mName.length = temp;
|
2009-08-21 22:49:58 +00:00
|
|
|
memcpy(pScene->mRootNode->mName.data,szMe,temp);
|
2008-08-06 23:01:38 +00:00
|
|
|
pScene->mRootNode->mName.data[temp] = '\0';
|
|
|
|
}
|
|
|
|
else pScene->mRootNode->mName.Set("<STL_ASCII>");
|
|
|
|
|
|
|
|
// try to guess how many vertices we could have
|
|
|
|
// assume we'll need 160 bytes for each face
|
2011-09-04 15:09:45 +00:00
|
|
|
pMesh->mNumVertices = ( pMesh->mNumFaces = std::max(1u,fileSize / 160u )) * 3;
|
2008-08-06 23:01:38 +00:00
|
|
|
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
2009-08-21 22:49:58 +00:00
|
|
|
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
2008-08-06 23:01:38 +00:00
|
|
|
|
General
- Added format auto-detection to most loaders
- Simplified BaseImporter::CanRead() with some utility methods
- improved fast_atof -> no overruns anymore. Fuck you, irrlicht.
- added assimp_cmd tool to allow command line model processing. Mainly adebugging tool for internal purposes, but others might find it useful, too.
- vc8/vc9: revision number is now written to DLL version header
- mkutil: some batch scripts to simplify tagging & building of release versions
- some API cleanup
- fixing some doxygen markup (+now explicit use of @file <filename>)
- Icon for assimp_view and assimp_cmd
3DS
- Normal vectors are not anymore inverted in some cases
- Improved pivot handling
- Improved handling of x-flipped meshes
Collada
- fixed a minor bug (visual_scene element)
LWS
- WIP implementation. No animations yet, some bugs and crashes.
- Animation system remains disabled, WIP code
- many test files for LWS, but most of them test the anim support, which is, read above, currently disabled.
STL
- fixing a log warning which appears for every model
- added binary&ascii test spider, exported from truespace
MD3
- Cleaning up output tags for automatically joined player models.
IRR
- Fixing coordinate system issues.
- Instance handling improved.
- Some of the reported crashes not yet fixed.
PretransformVertices
- Numerous performance improvements.
- Added config option to preserve the hierarchy during the step.
RemoveRedundantMaterials
- Added config option to specify a list of materials which are kept in every case.
UNREAL
- Added support for the old unreal data format (*.a,*.d,*.uc)
- tested only with exports from Milkshape
- more Unreal stuff to come soon
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@356 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
2009-03-05 22:32:13 +00:00
|
|
|
unsigned int curFace = 0, curVertex = 3;
|
2008-08-06 23:01:38 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// go to the next token
|
|
|
|
if(!SkipSpacesAndLineEnd(&sz))
|
|
|
|
{
|
|
|
|
// seems we're finished although there was no end marker
|
|
|
|
DefaultLogger::get()->warn("STL: unexpected EOF. \'endsolid\' keyword was expected");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// facet normal -0.13 -0.13 -0.98
|
2009-08-21 22:49:58 +00:00
|
|
|
if (!strncmp(sz,"facet",5) && IsSpaceOrNewLine(*(sz+5))) {
|
|
|
|
|
|
|
|
if (3 != curVertex) {
|
|
|
|
DefaultLogger::get()->warn("STL: A new facet begins but the old is not yet complete");
|
|
|
|
}
|
|
|
|
if (pMesh->mNumFaces == curFace) {
|
2011-09-04 15:09:45 +00:00
|
|
|
ai_assert(pMesh->mNumFaces != 0);
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// need to resize the arrays, our size estimate was wrong
|
2008-08-13 23:46:46 +00:00
|
|
|
unsigned int iNeededSize = (unsigned int)(sz-mBuffer) / pMesh->mNumFaces;
|
2008-08-06 23:01:38 +00:00
|
|
|
if (iNeededSize <= 160)iNeededSize >>= 1; // prevent endless looping
|
2008-08-13 23:46:46 +00:00
|
|
|
unsigned int add = (unsigned int)((mBuffer+fileSize)-sz) / iNeededSize;
|
2008-08-06 23:01:38 +00:00
|
|
|
add += add >> 3; // add 12.5% as buffer
|
|
|
|
iNeededSize = (pMesh->mNumFaces + add)*3;
|
|
|
|
aiVector3D* pv = new aiVector3D[iNeededSize];
|
2009-08-21 22:49:58 +00:00
|
|
|
memcpy(pv,pMesh->mVertices,pMesh->mNumVertices*sizeof(aiVector3D));
|
2008-08-06 23:01:38 +00:00
|
|
|
delete[] pMesh->mVertices;
|
|
|
|
pMesh->mVertices = pv;
|
|
|
|
pv = new aiVector3D[iNeededSize];
|
2009-08-21 22:49:58 +00:00
|
|
|
memcpy(pv,pMesh->mNormals,pMesh->mNumVertices*sizeof(aiVector3D));
|
2008-08-06 23:01:38 +00:00
|
|
|
delete[] pMesh->mNormals;
|
|
|
|
pMesh->mNormals = pv;
|
|
|
|
|
|
|
|
pMesh->mNumVertices = iNeededSize;
|
|
|
|
pMesh->mNumFaces += add;
|
|
|
|
}
|
|
|
|
aiVector3D* vn = &pMesh->mNormals[curFace++*3];
|
|
|
|
|
|
|
|
sz += 6;
|
|
|
|
curVertex = 0;
|
|
|
|
SkipSpaces(&sz);
|
2009-08-21 22:49:58 +00:00
|
|
|
if (strncmp(sz,"normal",6)) {
|
2008-08-06 23:01:38 +00:00
|
|
|
DefaultLogger::get()->warn("STL: a facet normal vector was expected but not found");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sz += 7;
|
|
|
|
SkipSpaces(&sz);
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->x );
|
2008-08-06 23:01:38 +00:00
|
|
|
SkipSpaces(&sz);
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->y );
|
2008-08-06 23:01:38 +00:00
|
|
|
SkipSpaces(&sz);
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->z );
|
2008-08-06 23:01:38 +00:00
|
|
|
*(vn+1) = *vn;
|
|
|
|
*(vn+2) = *vn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// vertex 1.50000 1.50000 0.00000
|
2009-08-21 22:49:58 +00:00
|
|
|
else if (!strncmp(sz,"vertex",6) && ::IsSpaceOrNewLine(*(sz+6)))
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
2009-08-21 22:49:58 +00:00
|
|
|
if (3 == curVertex) {
|
2008-08-06 23:01:38 +00:00
|
|
|
DefaultLogger::get()->error("STL: a facet with more than 3 vertices has been found");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sz += 7;
|
|
|
|
SkipSpaces(&sz);
|
|
|
|
aiVector3D* vn = &pMesh->mVertices[(curFace-1)*3 + curVertex++];
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->x );
|
2008-08-06 23:01:38 +00:00
|
|
|
SkipSpaces(&sz);
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->y );
|
2008-08-06 23:01:38 +00:00
|
|
|
SkipSpaces(&sz);
|
2012-01-31 16:18:32 +00:00
|
|
|
sz = fast_atoreal_move<float>(sz, (float&)vn->z );
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-21 22:49:58 +00:00
|
|
|
else if (!::strncmp(sz,"endsolid",8)) {
|
2008-08-06 23:01:38 +00:00
|
|
|
// finished!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// else skip the whole identifier
|
2009-08-21 22:49:58 +00:00
|
|
|
else while (!::IsSpaceOrNewLine(*sz)) {
|
|
|
|
++sz;
|
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
|
2009-08-21 22:49:58 +00:00
|
|
|
if (!curFace) {
|
2008-08-06 23:01:38 +00:00
|
|
|
pMesh->mNumFaces = 0;
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("STL: ASCII file is empty or invalid; no data loaded");
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
pMesh->mNumFaces = curFace;
|
|
|
|
pMesh->mNumVertices = curFace*3;
|
|
|
|
// we are finished!
|
|
|
|
}
|
2009-08-21 22:49:58 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Read a binary STL file
|
|
|
|
bool STLImporter::LoadBinaryFile()
|
|
|
|
{
|
|
|
|
// skip the first 80 bytes
|
2009-08-21 22:49:58 +00:00
|
|
|
if (fileSize < 84) {
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("STL: file is too small for the header");
|
2009-08-21 22:49:58 +00:00
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
bool bIsMaterialise = false;
|
|
|
|
|
|
|
|
// search for an occurence of "COLOR=" in the header
|
|
|
|
const char* sz2 = (const char*)mBuffer;
|
|
|
|
const char* const szEnd = sz2+80;
|
2009-08-21 22:49:58 +00:00
|
|
|
while (sz2 < szEnd) {
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
if ('C' == *sz2++ && 'O' == *sz2++ && 'L' == *sz2++ &&
|
2009-08-21 22:49:58 +00:00
|
|
|
'O' == *sz2++ && 'R' == *sz2++ && '=' == *sz2++) {
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// read the default vertex color for facets
|
|
|
|
bIsMaterialise = true;
|
2008-10-31 19:32:00 +00:00
|
|
|
DefaultLogger::get()->info("STL: Taking code path for Materialise files");
|
2009-08-21 22:49:58 +00:00
|
|
|
clrColorDefault.r = (*sz2++) / 255.0f;
|
|
|
|
clrColorDefault.g = (*sz2++) / 255.0f;
|
|
|
|
clrColorDefault.b = (*sz2++) / 255.0f;
|
|
|
|
clrColorDefault.a = (*sz2++) / 255.0f;
|
2008-08-06 23:01:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const unsigned char* sz = (const unsigned char*)mBuffer + 80;
|
|
|
|
|
|
|
|
// now read the number of facets
|
|
|
|
aiMesh* pMesh = pScene->mMeshes[0];
|
|
|
|
pScene->mRootNode->mName.Set("<STL_BINARY>");
|
|
|
|
|
|
|
|
pMesh->mNumFaces = *((uint32_t*)sz);
|
|
|
|
sz += 4;
|
|
|
|
|
2009-08-21 22:49:58 +00:00
|
|
|
if (fileSize < 84 + pMesh->mNumFaces*50) {
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("STL: file is too small to hold all facets");
|
2009-08-21 22:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!pMesh->mNumFaces) {
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("STL: file is empty. There are no facets defined");
|
2009-08-21 22:49:58 +00:00
|
|
|
}
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
pMesh->mNumVertices = pMesh->mNumFaces*3;
|
|
|
|
|
|
|
|
aiVector3D* vp,*vn;
|
|
|
|
vp = pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
|
|
|
vn = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
|
|
|
|
2009-08-21 22:49:58 +00:00
|
|
|
for (unsigned int i = 0; i < pMesh->mNumFaces;++i) {
|
|
|
|
|
|
|
|
// NOTE: Blender sometimes writes empty normals ... this is not
|
2008-10-13 16:45:48 +00:00
|
|
|
// our fault ... the RemoveInvalidData helper step should fix that
|
2008-08-06 23:01:38 +00:00
|
|
|
*vn = *((aiVector3D*)sz);
|
|
|
|
sz += sizeof(aiVector3D);
|
|
|
|
*(vn+1) = *vn;
|
|
|
|
*(vn+2) = *vn;
|
2008-08-07 22:27:17 +00:00
|
|
|
vn += 3;
|
2008-08-06 23:01:38 +00:00
|
|
|
|
|
|
|
*vp++ = *((aiVector3D*)sz);
|
|
|
|
sz += sizeof(aiVector3D);
|
|
|
|
|
|
|
|
*vp++ = *((aiVector3D*)sz);
|
|
|
|
sz += sizeof(aiVector3D);
|
|
|
|
|
|
|
|
*vp++ = *((aiVector3D*)sz);
|
|
|
|
sz += sizeof(aiVector3D);
|
|
|
|
|
|
|
|
uint16_t color = *((uint16_t*)sz);
|
|
|
|
sz += 2;
|
|
|
|
|
|
|
|
if (color & (1 << 15))
|
|
|
|
{
|
|
|
|
// seems we need to take the color
|
|
|
|
if (!pMesh->mColors[0])
|
|
|
|
{
|
|
|
|
pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
|
|
|
|
for (unsigned int i = 0; i <pMesh->mNumVertices;++i)
|
|
|
|
*pMesh->mColors[0]++ = this->clrColorDefault;
|
|
|
|
pMesh->mColors[0] -= pMesh->mNumVertices;
|
2008-10-31 19:32:00 +00:00
|
|
|
|
|
|
|
DefaultLogger::get()->info("STL: Mesh has vertex colors");
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
aiColor4D* clr = &pMesh->mColors[0][pMesh->mNumFaces*3];
|
|
|
|
clr->a = 1.0f;
|
|
|
|
if (bIsMaterialise) // fuck, this is reversed
|
|
|
|
{
|
|
|
|
clr->r = (color & 0x31u) / 31.0f;
|
|
|
|
clr->g = ((color & (0x31u<<5))>>5u) / 31.0f;
|
|
|
|
clr->b = ((color & (0x31u<<10))>>10u) / 31.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clr->b = (color & 0x31u) / 31.0f;
|
|
|
|
clr->g = ((color & (0x31u<<5))>>5u) / 31.0f;
|
|
|
|
clr->r = ((color & (0x31u<<10))>>10u) / 31.0f;
|
|
|
|
}
|
|
|
|
// assign the color to all vertices of the face
|
|
|
|
*(clr+1) = *clr;
|
|
|
|
*(clr+2) = *clr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bIsMaterialise && !pMesh->mColors[0])
|
|
|
|
{
|
2008-10-31 19:32:00 +00:00
|
|
|
// use the color as diffuse material color
|
2008-08-06 23:01:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_STL_IMPORTER
|