clean all warnings for vs-2019
This commit is contained in:
@@ -43,23 +43,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
/** @file Implementation of the STL importer class */
|
||||
|
||||
|
||||
#ifndef ASSIMP_BUILD_NO_STL_IMPORTER
|
||||
|
||||
// internal headers
|
||||
#include "STLLoader.h"
|
||||
#include <assimp/ParsingUtils.h>
|
||||
#include <assimp/fast_atof.h>
|
||||
#include <memory>
|
||||
#include <assimp/IOSystem.hpp>
|
||||
#include <assimp/importerdesc.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/DefaultLogger.hpp>
|
||||
#include <assimp/importerdesc.h>
|
||||
#include <assimp/IOSystem.hpp>
|
||||
#include <memory>
|
||||
|
||||
using namespace Assimp;
|
||||
|
||||
namespace {
|
||||
|
||||
|
||||
static const aiImporterDesc desc = {
|
||||
"Stereolithography (STL) Importer",
|
||||
"",
|
||||
@@ -77,14 +76,14 @@ static const aiImporterDesc desc = {
|
||||
// 1) 80 byte header
|
||||
// 2) 4 byte face count
|
||||
// 3) 50 bytes per face
|
||||
static bool IsBinarySTL(const char* buffer, unsigned int fileSize) {
|
||||
if( fileSize < 84 ) {
|
||||
static bool IsBinarySTL(const char *buffer, unsigned int fileSize) {
|
||||
if (fileSize < 84) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *facecount_pos = buffer + 80;
|
||||
uint32_t faceCount( 0 );
|
||||
::memcpy( &faceCount, facecount_pos, sizeof( uint32_t ) );
|
||||
uint32_t faceCount(0);
|
||||
::memcpy(&faceCount, facecount_pos, sizeof(uint32_t));
|
||||
const uint32_t expectedBinaryFileSize = faceCount * 50 + 84;
|
||||
|
||||
return expectedBinaryFileSize == fileSize;
|
||||
@@ -96,11 +95,11 @@ static const char UnicodeBoundary = 127;
|
||||
// An ascii STL buffer will begin with "solid NAME", where NAME is optional.
|
||||
// Note: The "solid NAME" check is necessary, but not sufficient, to determine
|
||||
// if the buffer is ASCII; a binary header could also begin with "solid NAME".
|
||||
static bool IsAsciiSTL(const char* buffer, unsigned int fileSize) {
|
||||
static bool IsAsciiSTL(const char *buffer, unsigned int fileSize) {
|
||||
if (IsBinarySTL(buffer, fileSize))
|
||||
return false;
|
||||
|
||||
const char* bufferEnd = buffer + fileSize;
|
||||
const char *bufferEnd = buffer + fileSize;
|
||||
|
||||
if (!SkipSpaces(&buffer))
|
||||
return false;
|
||||
@@ -108,13 +107,13 @@ static bool IsAsciiSTL(const char* buffer, unsigned int fileSize) {
|
||||
if (buffer + 5 >= bufferEnd)
|
||||
return false;
|
||||
|
||||
bool isASCII( strncmp( buffer, "solid", 5 ) == 0 );
|
||||
if( isASCII ) {
|
||||
bool isASCII(strncmp(buffer, "solid", 5) == 0);
|
||||
if (isASCII) {
|
||||
// A lot of importers are write solid even if the file is binary. So we have to check for ASCII-characters.
|
||||
if( fileSize >= BufferSize) {
|
||||
if (fileSize >= BufferSize) {
|
||||
isASCII = true;
|
||||
for( unsigned int i = 0; i < BufferSize; i++ ) {
|
||||
if( buffer[ i ] > UnicodeBoundary) {
|
||||
for (unsigned int i = 0; i < BufferSize; i++) {
|
||||
if (buffer[i] > UnicodeBoundary) {
|
||||
isASCII = false;
|
||||
break;
|
||||
}
|
||||
@@ -127,49 +126,49 @@ static bool IsAsciiSTL(const char* buffer, unsigned int fileSize) {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Constructor to be privately used by Importer
|
||||
STLImporter::STLImporter()
|
||||
: mBuffer(),
|
||||
fileSize(),
|
||||
pScene()
|
||||
{}
|
||||
STLImporter::STLImporter() :
|
||||
mBuffer(),
|
||||
mFileSize(0),
|
||||
mScene() {
|
||||
// empty
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Destructor, private as well
|
||||
STLImporter::~STLImporter()
|
||||
{}
|
||||
STLImporter::~STLImporter() {
|
||||
// empty
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Returns whether the class can handle the format of the given file.
|
||||
bool STLImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
|
||||
{
|
||||
bool STLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
|
||||
const std::string extension = GetExtension(pFile);
|
||||
|
||||
if( extension == "stl" ) {
|
||||
if (extension == "stl") {
|
||||
return true;
|
||||
} else if (!extension.length() || checkSig) {
|
||||
if( !pIOHandler ) {
|
||||
} else if (!extension.length() || checkSig) {
|
||||
if (!pIOHandler) {
|
||||
return true;
|
||||
}
|
||||
const char* tokens[] = {"STL","solid"};
|
||||
return SearchFileHeaderForToken(pIOHandler,pFile,tokens,2);
|
||||
const char *tokens[] = { "STL", "solid" };
|
||||
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 2);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
const aiImporterDesc* STLImporter::GetInfo () const {
|
||||
const aiImporterDesc *STLImporter::GetInfo() const {
|
||||
return &desc;
|
||||
}
|
||||
|
||||
void addFacesToMesh(aiMesh* pMesh)
|
||||
{
|
||||
void addFacesToMesh(aiMesh *pMesh) {
|
||||
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
|
||||
for (unsigned int i = 0, p = 0; i < pMesh->mNumFaces;++i) {
|
||||
for (unsigned int i = 0, p = 0; i < pMesh->mNumFaces; ++i) {
|
||||
|
||||
aiFace& face = pMesh->mFaces[i];
|
||||
aiFace &face = pMesh->mFaces[i];
|
||||
face.mIndices = new unsigned int[face.mNumIndices = 3];
|
||||
for (unsigned int o = 0; o < 3;++o,++p) {
|
||||
for (unsigned int o = 0; o < 3; ++o, ++p) {
|
||||
face.mIndices[o] = p;
|
||||
}
|
||||
}
|
||||
@@ -177,137 +176,132 @@ void addFacesToMesh(aiMesh* pMesh)
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Imports the given file into the given scene structure.
|
||||
void STLImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler )
|
||||
{
|
||||
std::unique_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
|
||||
void STLImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
|
||||
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
|
||||
|
||||
// Check whether we can read from the file
|
||||
if( file.get() == nullptr) {
|
||||
throw DeadlyImportError( "Failed to open STL file " + pFile + ".");
|
||||
if (file.get() == nullptr) {
|
||||
throw DeadlyImportError("Failed to open STL file " + pFile + ".");
|
||||
}
|
||||
|
||||
fileSize = (unsigned int)file->FileSize();
|
||||
mFileSize = (unsigned int)file->FileSize();
|
||||
|
||||
// allocate storage and copy the contents of the file to a memory buffer
|
||||
// (terminate it with zero)
|
||||
std::vector<char> buffer2;
|
||||
TextFileToBuffer(file.get(),buffer2);
|
||||
TextFileToBuffer(file.get(), buffer2);
|
||||
|
||||
this->pScene = pScene;
|
||||
this->mBuffer = &buffer2[0];
|
||||
mScene = pScene;
|
||||
mBuffer = &buffer2[0];
|
||||
|
||||
// the default vertex color is light gray.
|
||||
clrColorDefault.r = clrColorDefault.g = clrColorDefault.b = clrColorDefault.a = (ai_real) 0.6;
|
||||
mClrColorDefault.r = mClrColorDefault.g = mClrColorDefault.b = mClrColorDefault.a = (ai_real)0.6;
|
||||
|
||||
// allocate a single node
|
||||
pScene->mRootNode = new aiNode();
|
||||
mScene->mRootNode = new aiNode();
|
||||
|
||||
bool bMatClr = false;
|
||||
|
||||
if (IsBinarySTL(mBuffer, fileSize)) {
|
||||
if (IsBinarySTL(mBuffer, mFileSize)) {
|
||||
bMatClr = LoadBinaryFile();
|
||||
} else if (IsAsciiSTL(mBuffer, fileSize)) {
|
||||
LoadASCIIFile( pScene->mRootNode );
|
||||
} else if (IsAsciiSTL(mBuffer, mFileSize)) {
|
||||
LoadASCIIFile(mScene->mRootNode);
|
||||
} else {
|
||||
throw DeadlyImportError( "Failed to determine STL storage representation for " + pFile + ".");
|
||||
throw DeadlyImportError("Failed to determine STL storage representation for " + pFile + ".");
|
||||
}
|
||||
|
||||
// create a single default material, using a white diffuse color for consistency with
|
||||
// other geometric types (e.g., PLY).
|
||||
aiMaterial* pcMat = new aiMaterial();
|
||||
aiMaterial *pcMat = new aiMaterial();
|
||||
aiString s;
|
||||
s.Set(AI_DEFAULT_MATERIAL_NAME);
|
||||
pcMat->AddProperty(&s, AI_MATKEY_NAME);
|
||||
|
||||
aiColor4D clrDiffuse(ai_real(1.0),ai_real(1.0),ai_real(1.0),ai_real(1.0));
|
||||
aiColor4D clrDiffuse(ai_real(1.0), ai_real(1.0), ai_real(1.0), ai_real(1.0));
|
||||
if (bMatClr) {
|
||||
clrDiffuse = clrColorDefault;
|
||||
clrDiffuse = mClrColorDefault;
|
||||
}
|
||||
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_DIFFUSE);
|
||||
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_SPECULAR);
|
||||
clrDiffuse = aiColor4D( ai_real(0.05), ai_real(0.05), ai_real(0.05), ai_real(1.0));
|
||||
pcMat->AddProperty(&clrDiffuse,1,AI_MATKEY_COLOR_AMBIENT);
|
||||
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
|
||||
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_SPECULAR);
|
||||
clrDiffuse = aiColor4D(ai_real(0.05), ai_real(0.05), ai_real(0.05), ai_real(1.0));
|
||||
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_AMBIENT);
|
||||
|
||||
pScene->mNumMaterials = 1;
|
||||
pScene->mMaterials = new aiMaterial*[1];
|
||||
pScene->mMaterials[0] = pcMat;
|
||||
mScene->mNumMaterials = 1;
|
||||
mScene->mMaterials = new aiMaterial *[1];
|
||||
mScene->mMaterials[0] = pcMat;
|
||||
|
||||
mBuffer = nullptr;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Read an ASCII STL file
|
||||
void STLImporter::LoadASCIIFile( aiNode *root ) {
|
||||
std::vector<aiMesh*> meshes;
|
||||
std::vector<aiNode*> nodes;
|
||||
const char* sz = mBuffer;
|
||||
const char* bufferEnd = mBuffer + fileSize;
|
||||
void STLImporter::LoadASCIIFile(aiNode *root) {
|
||||
std::vector<aiMesh *> meshes;
|
||||
std::vector<aiNode *> nodes;
|
||||
const char *sz = mBuffer;
|
||||
const char *bufferEnd = mBuffer + mFileSize;
|
||||
std::vector<aiVector3D> positionBuffer;
|
||||
std::vector<aiVector3D> normalBuffer;
|
||||
|
||||
// try to guess how many vertices we could have
|
||||
// assume we'll need 160 bytes for each face
|
||||
size_t sizeEstimate = std::max(1u, fileSize / 160u ) * 3;
|
||||
size_t sizeEstimate = std::max(1u, mFileSize / 160u) * 3;
|
||||
positionBuffer.reserve(sizeEstimate);
|
||||
normalBuffer.reserve(sizeEstimate);
|
||||
|
||||
while (IsAsciiSTL(sz, static_cast<unsigned int>(bufferEnd - sz))) {
|
||||
std::vector<unsigned int> meshIndices;
|
||||
aiMesh* pMesh = new aiMesh();
|
||||
aiMesh *pMesh = new aiMesh();
|
||||
pMesh->mMaterialIndex = 0;
|
||||
meshIndices.push_back((unsigned int) meshes.size() );
|
||||
meshIndices.push_back((unsigned int)meshes.size());
|
||||
meshes.push_back(pMesh);
|
||||
aiNode *node = new aiNode;
|
||||
node->mParent = root;
|
||||
nodes.push_back( node );
|
||||
nodes.push_back(node);
|
||||
SkipSpaces(&sz);
|
||||
ai_assert(!IsLineEnd(sz));
|
||||
|
||||
sz += 5; // skip the "solid"
|
||||
SkipSpaces(&sz);
|
||||
const char* szMe = sz;
|
||||
const char *szMe = sz;
|
||||
while (!::IsSpaceOrNewLine(*sz)) {
|
||||
sz++;
|
||||
}
|
||||
|
||||
size_t temp;
|
||||
size_t temp = (size_t)(sz - szMe);
|
||||
// setup the name of the node
|
||||
if ((temp = (size_t)(sz-szMe))) {
|
||||
if ( temp ) {
|
||||
if (temp >= MAXLEN) {
|
||||
throw DeadlyImportError( "STL: Node name too long" );
|
||||
throw DeadlyImportError("STL: Node name too long");
|
||||
}
|
||||
std::string name( szMe, temp );
|
||||
node->mName.Set( name.c_str() );
|
||||
pMesh->mName.Set( name.c_str() );
|
||||
//pScene->mRootNode->mName.length = temp;
|
||||
//memcpy(pScene->mRootNode->mName.data,szMe,temp);
|
||||
//pScene->mRootNode->mName.data[temp] = '\0';
|
||||
std::string name(szMe, temp);
|
||||
node->mName.Set(name.c_str());
|
||||
pMesh->mName.Set(name.c_str());
|
||||
} else {
|
||||
pScene->mRootNode->mName.Set("<STL_ASCII>");
|
||||
mScene->mRootNode->mName.Set("<STL_ASCII>");
|
||||
}
|
||||
|
||||
unsigned int faceVertexCounter = 3;
|
||||
for ( ;; ) {
|
||||
for (;;) {
|
||||
// go to the next token
|
||||
if(!SkipSpacesAndLineEnd(&sz))
|
||||
{
|
||||
if (!SkipSpacesAndLineEnd(&sz)) {
|
||||
// seems we're finished although there was no end marker
|
||||
ASSIMP_LOG_WARN("STL: unexpected EOF. \'endsolid\' keyword was expected");
|
||||
break;
|
||||
}
|
||||
// facet normal -0.13 -0.13 -0.98
|
||||
if (!strncmp(sz,"facet",5) && IsSpaceOrNewLine(*(sz+5)) && *(sz + 5) != '\0') {
|
||||
if (!strncmp(sz, "facet", 5) && IsSpaceOrNewLine(*(sz + 5)) && *(sz + 5) != '\0') {
|
||||
|
||||
if (faceVertexCounter != 3) {
|
||||
ASSIMP_LOG_WARN("STL: A new facet begins but the old is not yet complete");
|
||||
}
|
||||
faceVertexCounter = 0;
|
||||
normalBuffer.push_back(aiVector3D());
|
||||
aiVector3D* vn = &normalBuffer.back();
|
||||
aiVector3D *vn = &normalBuffer.back();
|
||||
|
||||
sz += 6;
|
||||
SkipSpaces(&sz);
|
||||
if (strncmp(sz,"normal",6)) {
|
||||
if (strncmp(sz, "normal", 6)) {
|
||||
ASSIMP_LOG_WARN("STL: a facet normal vector was expected but not found");
|
||||
} else {
|
||||
if (sz[6] == '\0') {
|
||||
@@ -315,15 +309,15 @@ void STLImporter::LoadASCIIFile( aiNode *root ) {
|
||||
}
|
||||
sz += 7;
|
||||
SkipSpaces(&sz);
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->x );
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->x);
|
||||
SkipSpaces(&sz);
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->y );
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->y);
|
||||
SkipSpaces(&sz);
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->z );
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->z);
|
||||
normalBuffer.push_back(*vn);
|
||||
normalBuffer.push_back(*vn);
|
||||
}
|
||||
} else if (!strncmp(sz,"vertex",6) && ::IsSpaceOrNewLine(*(sz+6))) { // vertex 1.50000 1.50000 0.00000
|
||||
} else if (!strncmp(sz, "vertex", 6) && ::IsSpaceOrNewLine(*(sz + 6))) { // vertex 1.50000 1.50000 0.00000
|
||||
if (faceVertexCounter >= 3) {
|
||||
ASSIMP_LOG_ERROR("STL: a facet with more than 3 vertices has been found");
|
||||
++sz;
|
||||
@@ -334,15 +328,15 @@ void STLImporter::LoadASCIIFile( aiNode *root ) {
|
||||
sz += 7;
|
||||
SkipSpaces(&sz);
|
||||
positionBuffer.push_back(aiVector3D());
|
||||
aiVector3D* vn = &positionBuffer.back();
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->x );
|
||||
aiVector3D *vn = &positionBuffer.back();
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->x);
|
||||
SkipSpaces(&sz);
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->y );
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->y);
|
||||
SkipSpaces(&sz);
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real&)vn->z );
|
||||
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->z);
|
||||
faceVertexCounter++;
|
||||
}
|
||||
} else if (!::strncmp(sz,"endsolid",8)) {
|
||||
} else if (!::strncmp(sz, "endsolid", 8)) {
|
||||
do {
|
||||
++sz;
|
||||
} while (!::IsLineEnd(*sz));
|
||||
@@ -356,15 +350,15 @@ void STLImporter::LoadASCIIFile( aiNode *root ) {
|
||||
}
|
||||
}
|
||||
|
||||
if (positionBuffer.empty()) {
|
||||
if (positionBuffer.empty()) {
|
||||
pMesh->mNumFaces = 0;
|
||||
ASSIMP_LOG_WARN("STL: mesh is empty or invalid; no data loaded");
|
||||
}
|
||||
if (positionBuffer.size() % 3 != 0) {
|
||||
if (positionBuffer.size() % 3 != 0) {
|
||||
pMesh->mNumFaces = 0;
|
||||
throw DeadlyImportError("STL: Invalid number of vertices");
|
||||
}
|
||||
if (normalBuffer.size() != positionBuffer.size()) {
|
||||
if (normalBuffer.size() != positionBuffer.size()) {
|
||||
pMesh->mNumFaces = 0;
|
||||
throw DeadlyImportError("Normal buffer size does not match position buffer size");
|
||||
}
|
||||
@@ -390,67 +384,66 @@ void STLImporter::LoadASCIIFile( aiNode *root ) {
|
||||
addFacesToMesh(pMesh);
|
||||
|
||||
// assign the meshes to the current node
|
||||
pushMeshesToNode( meshIndices, node );
|
||||
pushMeshesToNode(meshIndices, node);
|
||||
}
|
||||
|
||||
// now add the loaded meshes
|
||||
pScene->mNumMeshes = (unsigned int)meshes.size();
|
||||
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
|
||||
mScene->mNumMeshes = (unsigned int)meshes.size();
|
||||
mScene->mMeshes = new aiMesh *[mScene->mNumMeshes];
|
||||
for (size_t i = 0; i < meshes.size(); i++) {
|
||||
pScene->mMeshes[ i ] = meshes[i];
|
||||
mScene->mMeshes[i] = meshes[i];
|
||||
}
|
||||
|
||||
root->mNumChildren = (unsigned int) nodes.size();
|
||||
root->mChildren = new aiNode*[ root->mNumChildren ];
|
||||
for ( size_t i=0; i<nodes.size(); ++i ) {
|
||||
root->mChildren[ i ] = nodes[ i ];
|
||||
root->mNumChildren = (unsigned int)nodes.size();
|
||||
root->mChildren = new aiNode *[root->mNumChildren];
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
root->mChildren[i] = nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Read a binary STL file
|
||||
bool STLImporter::LoadBinaryFile()
|
||||
{
|
||||
bool STLImporter::LoadBinaryFile() {
|
||||
// allocate one mesh
|
||||
pScene->mNumMeshes = 1;
|
||||
pScene->mMeshes = new aiMesh*[1];
|
||||
aiMesh* pMesh = pScene->mMeshes[0] = new aiMesh();
|
||||
mScene->mNumMeshes = 1;
|
||||
mScene->mMeshes = new aiMesh *[1];
|
||||
aiMesh *pMesh = mScene->mMeshes[0] = new aiMesh();
|
||||
pMesh->mMaterialIndex = 0;
|
||||
|
||||
// skip the first 80 bytes
|
||||
if (fileSize < 84) {
|
||||
if (mFileSize < 84) {
|
||||
throw DeadlyImportError("STL: file is too small for the header");
|
||||
}
|
||||
bool bIsMaterialise = false;
|
||||
|
||||
// search for an occurrence of "COLOR=" in the header
|
||||
const unsigned char* sz2 = (const unsigned char*)mBuffer;
|
||||
const unsigned char* const szEnd = sz2+80;
|
||||
const unsigned char *sz2 = (const unsigned char *)mBuffer;
|
||||
const unsigned char *const szEnd = sz2 + 80;
|
||||
while (sz2 < szEnd) {
|
||||
|
||||
if ('C' == *sz2++ && 'O' == *sz2++ && 'L' == *sz2++ &&
|
||||
'O' == *sz2++ && 'R' == *sz2++ && '=' == *sz2++) {
|
||||
'O' == *sz2++ && 'R' == *sz2++ && '=' == *sz2++) {
|
||||
|
||||
// read the default vertex color for facets
|
||||
bIsMaterialise = true;
|
||||
ASSIMP_LOG_INFO("STL: Taking code path for Materialise files");
|
||||
const ai_real invByte = (ai_real)1.0 / ( ai_real )255.0;
|
||||
clrColorDefault.r = (*sz2++) * invByte;
|
||||
clrColorDefault.g = (*sz2++) * invByte;
|
||||
clrColorDefault.b = (*sz2++) * invByte;
|
||||
clrColorDefault.a = (*sz2++) * invByte;
|
||||
const ai_real invByte = (ai_real)1.0 / (ai_real)255.0;
|
||||
mClrColorDefault.r = (*sz2++) * invByte;
|
||||
mClrColorDefault.g = (*sz2++) * invByte;
|
||||
mClrColorDefault.b = (*sz2++) * invByte;
|
||||
mClrColorDefault.a = (*sz2++) * invByte;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const unsigned char* sz = (const unsigned char*)mBuffer + 80;
|
||||
const unsigned char *sz = (const unsigned char *)mBuffer + 80;
|
||||
|
||||
// now read the number of facets
|
||||
pScene->mRootNode->mName.Set("<STL_BINARY>");
|
||||
mScene->mRootNode->mName.Set("<STL_BINARY>");
|
||||
|
||||
pMesh->mNumFaces = *((uint32_t*)sz);
|
||||
pMesh->mNumFaces = *((uint32_t *)sz);
|
||||
sz += 4;
|
||||
|
||||
if (fileSize < 84 + pMesh->mNumFaces*50) {
|
||||
if (mFileSize < 84 + pMesh->mNumFaces * 50) {
|
||||
throw DeadlyImportError("STL: file is too small to hold all facets");
|
||||
}
|
||||
|
||||
@@ -458,123 +451,127 @@ bool STLImporter::LoadBinaryFile()
|
||||
throw DeadlyImportError("STL: file is empty. There are no facets defined");
|
||||
}
|
||||
|
||||
pMesh->mNumVertices = pMesh->mNumFaces*3;
|
||||
pMesh->mNumVertices = pMesh->mNumFaces * 3;
|
||||
|
||||
|
||||
aiVector3D *vp = pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
||||
aiVector3D *vn = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
||||
|
||||
typedef aiVector3t<float> aiVector3F;
|
||||
aiVector3F* theVec;
|
||||
aiVector3F *theVec;
|
||||
aiVector3F theVec3F;
|
||||
|
||||
for ( unsigned int i = 0; i < pMesh->mNumFaces; ++i ) {
|
||||
|
||||
for (unsigned int i = 0; i < pMesh->mNumFaces; ++i) {
|
||||
// NOTE: Blender sometimes writes empty normals ... this is not
|
||||
// our fault ... the RemoveInvalidData helper step should fix that
|
||||
|
||||
// There's one normal for the face in the STL; use it three times
|
||||
// for vertex normals
|
||||
theVec = (aiVector3F*) sz;
|
||||
::memcpy( &theVec3F, theVec, sizeof(aiVector3F) );
|
||||
vn->x = theVec3F.x; vn->y = theVec3F.y; vn->z = theVec3F.z;
|
||||
*(vn+1) = *vn;
|
||||
*(vn+2) = *vn;
|
||||
theVec = (aiVector3F *)sz;
|
||||
::memcpy(&theVec3F, theVec, sizeof(aiVector3F));
|
||||
vn->x = theVec3F.x;
|
||||
vn->y = theVec3F.y;
|
||||
vn->z = theVec3F.z;
|
||||
*(vn + 1) = *vn;
|
||||
*(vn + 2) = *vn;
|
||||
++theVec;
|
||||
vn += 3;
|
||||
|
||||
// vertex 1
|
||||
::memcpy( &theVec3F, theVec, sizeof(aiVector3F) );
|
||||
vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z;
|
||||
::memcpy(&theVec3F, theVec, sizeof(aiVector3F));
|
||||
vp->x = theVec3F.x;
|
||||
vp->y = theVec3F.y;
|
||||
vp->z = theVec3F.z;
|
||||
++theVec;
|
||||
++vp;
|
||||
|
||||
// vertex 2
|
||||
::memcpy( &theVec3F, theVec, sizeof(aiVector3F) );
|
||||
vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z;
|
||||
::memcpy(&theVec3F, theVec, sizeof(aiVector3F));
|
||||
vp->x = theVec3F.x;
|
||||
vp->y = theVec3F.y;
|
||||
vp->z = theVec3F.z;
|
||||
++theVec;
|
||||
++vp;
|
||||
|
||||
// vertex 3
|
||||
::memcpy( &theVec3F, theVec, sizeof(aiVector3F) );
|
||||
vp->x = theVec3F.x; vp->y = theVec3F.y; vp->z = theVec3F.z;
|
||||
::memcpy(&theVec3F, theVec, sizeof(aiVector3F));
|
||||
vp->x = theVec3F.x;
|
||||
vp->y = theVec3F.y;
|
||||
vp->z = theVec3F.z;
|
||||
++theVec;
|
||||
++vp;
|
||||
|
||||
sz = (const unsigned char*) theVec;
|
||||
|
||||
uint16_t color = *((uint16_t*)sz);
|
||||
sz = (const unsigned char *)theVec;
|
||||
|
||||
uint16_t color = *((uint16_t *)sz);
|
||||
sz += 2;
|
||||
|
||||
if (color & (1 << 15))
|
||||
{
|
||||
if (color & (1 << 15)) {
|
||||
// seems we need to take the color
|
||||
if (!pMesh->mColors[0])
|
||||
{
|
||||
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;
|
||||
for (unsigned int j = 0; j < pMesh->mNumVertices; ++j) {
|
||||
*pMesh->mColors[0]++ = mClrColorDefault;
|
||||
}
|
||||
pMesh->mColors[0] -= pMesh->mNumVertices;
|
||||
|
||||
ASSIMP_LOG_INFO("STL: Mesh has vertex colors");
|
||||
}
|
||||
aiColor4D* clr = &pMesh->mColors[0][i*3];
|
||||
aiColor4D *clr = &pMesh->mColors[0][i * 3];
|
||||
clr->a = 1.0;
|
||||
const ai_real invVal( (ai_real)1.0 / ( ai_real )31.0 );
|
||||
const ai_real invVal((ai_real)1.0 / (ai_real)31.0);
|
||||
if (bIsMaterialise) // this is reversed
|
||||
{
|
||||
clr->r = (color & 0x31u) *invVal;
|
||||
clr->g = ((color & (0x31u<<5))>>5u) *invVal;
|
||||
clr->b = ((color & (0x31u<<10))>>10u) *invVal;
|
||||
}
|
||||
else
|
||||
{
|
||||
clr->b = (color & 0x31u) *invVal;
|
||||
clr->g = ((color & (0x31u<<5))>>5u) *invVal;
|
||||
clr->r = ((color & (0x31u<<10))>>10u) *invVal;
|
||||
clr->r = (color & 0x31u) * invVal;
|
||||
clr->g = ((color & (0x31u << 5)) >> 5u) * invVal;
|
||||
clr->b = ((color & (0x31u << 10)) >> 10u) * invVal;
|
||||
} else {
|
||||
clr->b = (color & 0x31u) * invVal;
|
||||
clr->g = ((color & (0x31u << 5)) >> 5u) * invVal;
|
||||
clr->r = ((color & (0x31u << 10)) >> 10u) * invVal;
|
||||
}
|
||||
// assign the color to all vertices of the face
|
||||
*(clr+1) = *clr;
|
||||
*(clr+2) = *clr;
|
||||
*(clr + 1) = *clr;
|
||||
*(clr + 2) = *clr;
|
||||
}
|
||||
}
|
||||
|
||||
// now copy faces
|
||||
addFacesToMesh(pMesh);
|
||||
|
||||
aiNode* root = pScene->mRootNode;
|
||||
aiNode *root = mScene->mRootNode;
|
||||
|
||||
// allocate one node
|
||||
aiNode* node = new aiNode();
|
||||
aiNode *node = new aiNode();
|
||||
node->mParent = root;
|
||||
|
||||
root->mNumChildren = 1u;
|
||||
root->mChildren = new aiNode*[root->mNumChildren];
|
||||
root->mChildren = new aiNode *[root->mNumChildren];
|
||||
root->mChildren[0] = node;
|
||||
|
||||
// add all created meshes to the single node
|
||||
node->mNumMeshes = pScene->mNumMeshes;
|
||||
node->mMeshes = new unsigned int[pScene->mNumMeshes];
|
||||
for (unsigned int i = 0; i < pScene->mNumMeshes; i++)
|
||||
node->mNumMeshes = mScene->mNumMeshes;
|
||||
node->mMeshes = new unsigned int[mScene->mNumMeshes];
|
||||
for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
|
||||
node->mMeshes[i] = i;
|
||||
}
|
||||
|
||||
if (bIsMaterialise && !pMesh->mColors[0])
|
||||
{
|
||||
if (bIsMaterialise && !pMesh->mColors[0]) {
|
||||
// use the color as diffuse material color
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void STLImporter::pushMeshesToNode( std::vector<unsigned int> &meshIndices, aiNode *node ) {
|
||||
ai_assert( nullptr != node );
|
||||
if ( meshIndices.empty() ) {
|
||||
void STLImporter::pushMeshesToNode(std::vector<unsigned int> &meshIndices, aiNode *node) {
|
||||
ai_assert(nullptr != node);
|
||||
if (meshIndices.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
node->mNumMeshes = static_cast<unsigned int>( meshIndices.size() );
|
||||
node->mMeshes = new unsigned int[ meshIndices.size() ];
|
||||
for ( size_t i=0; i<meshIndices.size(); ++i ) {
|
||||
node->mMeshes[ i ] = meshIndices[ i ];
|
||||
node->mNumMeshes = static_cast<unsigned int>(meshIndices.size());
|
||||
node->mMeshes = new unsigned int[meshIndices.size()];
|
||||
for (size_t i = 0; i < meshIndices.size(); ++i) {
|
||||
node->mMeshes[i] = meshIndices[i];
|
||||
}
|
||||
meshIndices.clear();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user