Merge pull request #4630 from tanolino/fix_stl_for_over_4GB

Fix stl for over 4 gb
pull/4642/head
Kim Kulling 2022-07-09 10:46:34 +02:00 committed by GitHub
commit d8f42279a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 14 deletions

View File

@ -73,7 +73,7 @@ static const aiImporterDesc desc = {
// 1) 80 byte header // 1) 80 byte header
// 2) 4 byte face count // 2) 4 byte face count
// 3) 50 bytes per face // 3) 50 bytes per face
static bool IsBinarySTL(const char *buffer, unsigned int fileSize) { static bool IsBinarySTL(const char *buffer, size_t fileSize) {
if (fileSize < 84) { if (fileSize < 84) {
return false; return false;
} }
@ -92,7 +92,7 @@ static const char UnicodeBoundary = 127;
// An ascii STL buffer will begin with "solid NAME", where NAME is optional. // 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 // 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". // 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, size_t fileSize) {
if (IsBinarySTL(buffer, fileSize)) if (IsBinarySTL(buffer, fileSize))
return false; return false;
@ -172,7 +172,7 @@ void STLImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSy
throw DeadlyImportError("Failed to open STL file ", pFile, "."); throw DeadlyImportError("Failed to open STL file ", pFile, ".");
} }
mFileSize = (unsigned int)file->FileSize(); mFileSize = file->FileSize();
// allocate storage and copy the contents of the file to a memory buffer // allocate storage and copy the contents of the file to a memory buffer
// (terminate it with zero) // (terminate it with zero)
@ -233,7 +233,7 @@ void STLImporter::LoadASCIIFile(aiNode *root) {
// try to guess how many vertices we could have // try to guess how many vertices we could have
// assume we'll need 160 bytes for each face // assume we'll need 160 bytes for each face
size_t sizeEstimate = std::max(1u, mFileSize / 160u) * 3; size_t sizeEstimate = std::max(1ull, mFileSize / 160ull) * 3ull;
positionBuffer.reserve(sizeEstimate); positionBuffer.reserve(sizeEstimate);
normalBuffer.reserve(sizeEstimate); normalBuffer.reserve(sizeEstimate);
@ -284,8 +284,6 @@ void STLImporter::LoadASCIIFile(aiNode *root) {
ASSIMP_LOG_WARN("STL: A new facet begins but the old is not yet complete"); ASSIMP_LOG_WARN("STL: A new facet begins but the old is not yet complete");
} }
faceVertexCounter = 0; faceVertexCounter = 0;
normalBuffer.push_back(aiVector3D());
aiVector3D *vn = &normalBuffer.back();
sz += 6; sz += 6;
SkipSpaces(&sz); SkipSpaces(&sz);
@ -295,15 +293,17 @@ void STLImporter::LoadASCIIFile(aiNode *root) {
if (sz[6] == '\0') { if (sz[6] == '\0') {
throw DeadlyImportError("STL: unexpected EOF while parsing facet"); throw DeadlyImportError("STL: unexpected EOF while parsing facet");
} }
aiVector3D vn;
sz += 7; sz += 7;
SkipSpaces(&sz); 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); 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); 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.emplace_back(vn);
normalBuffer.push_back(*vn); normalBuffer.emplace_back(vn);
normalBuffer.emplace_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) { if (faceVertexCounter >= 3) {
@ -315,7 +315,7 @@ void STLImporter::LoadASCIIFile(aiNode *root) {
} }
sz += 7; sz += 7;
SkipSpaces(&sz); SkipSpaces(&sz);
positionBuffer.push_back(aiVector3D()); positionBuffer.emplace_back();
aiVector3D *vn = &positionBuffer.back(); aiVector3D *vn = &positionBuffer.back();
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->x); sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->x);
SkipSpaces(&sz); SkipSpaces(&sz);
@ -439,7 +439,7 @@ bool STLImporter::LoadBinaryFile() {
pMesh->mNumFaces = *((uint32_t *)sz); pMesh->mNumFaces = *((uint32_t *)sz);
sz += 4; sz += 4;
if (mFileSize < 84 + pMesh->mNumFaces * 50) { if (mFileSize < 84ull + pMesh->mNumFaces * 50ull) {
throw DeadlyImportError("STL: file is too small to hold all facets"); throw DeadlyImportError("STL: file is too small to hold all facets");
} }

View File

@ -109,7 +109,7 @@ protected:
const char* mBuffer; const char* mBuffer;
/** Size of the file, in bytes */ /** Size of the file, in bytes */
unsigned int mFileSize; size_t mFileSize;
/** Output scene */ /** Output scene */
aiScene* mScene; aiScene* mScene;