Torque3D/Engine/lib/assimp/code/AssetLib/STL/STLLoader.cpp

575 lines
20 KiB
C++
Raw Normal View History

2019-02-08 22:25:43 +00:00
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
2024-12-09 20:22:47 +00:00
Copyright (c) 2006-2024, assimp team
2019-02-08 22:25:43 +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 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 */
#ifndef ASSIMP_BUILD_NO_STL_IMPORTER
#include "STLLoader.h"
2019-03-05 20:39:38 +00:00
#include <assimp/ParsingUtils.h>
#include <assimp/fast_atof.h>
2022-04-26 16:56:24 +00:00
#include <assimp/importerdesc.h>
2019-02-08 22:25:43 +00:00
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
2022-04-26 16:56:24 +00:00
#include <assimp/IOSystem.hpp>
#include <memory>
2019-02-08 22:25:43 +00:00
2024-12-09 20:22:47 +00:00
namespace Assimp {
2019-02-08 22:25:43 +00:00
namespace {
2022-04-26 16:56:24 +00:00
2024-12-09 20:22:47 +00:00
static constexpr aiImporterDesc desc = {
2019-02-08 22:25:43 +00:00
"Stereolithography (STL) Importer",
"",
"",
"",
aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
0,
0,
0,
0,
"stl"
};
// A valid binary STL buffer should consist of the following elements, in order:
// 1) 80 byte header
// 2) 4 byte face count
// 3) 50 bytes per face
2022-10-02 18:02:49 +00:00
static bool IsBinarySTL(const char *buffer, size_t fileSize) {
2022-04-26 16:56:24 +00:00
if (fileSize < 84) {
2019-02-08 22:25:43 +00:00
return false;
}
const char *facecount_pos = buffer + 80;
2022-04-26 16:56:24 +00:00
uint32_t faceCount(0);
::memcpy(&faceCount, facecount_pos, sizeof(uint32_t));
2019-02-08 22:25:43 +00:00
const uint32_t expectedBinaryFileSize = faceCount * 50 + 84;
return expectedBinaryFileSize == fileSize;
}
2019-03-05 20:39:38 +00:00
static const size_t BufferSize = 500;
static const char UnicodeBoundary = 127;
2019-02-08 22:25:43 +00:00
// 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".
2022-10-02 18:02:49 +00:00
static bool IsAsciiSTL(const char *buffer, size_t fileSize) {
2019-02-08 22:25:43 +00:00
if (IsBinarySTL(buffer, fileSize))
return false;
2022-04-26 16:56:24 +00:00
const char *bufferEnd = buffer + fileSize;
2019-02-08 22:25:43 +00:00
2024-12-09 20:22:47 +00:00
if (!SkipSpaces(&buffer, bufferEnd)) {
2019-02-08 22:25:43 +00:00
return false;
2022-04-26 16:56:24 +00:00
}
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
if (buffer + 5 >= bufferEnd) {
2019-02-08 22:25:43 +00:00
return false;
2022-04-26 16:56:24 +00:00
}
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
bool isASCII(strncmp(buffer, "solid", 5) == 0);
if (isASCII) {
2019-02-08 22:25:43 +00:00
// A lot of importers are write solid even if the file is binary. So we have to check for ASCII-characters.
2022-04-26 16:56:24 +00:00
if (fileSize >= BufferSize) {
2019-02-08 22:25:43 +00:00
isASCII = true;
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < BufferSize; i++) {
if (buffer[i] > UnicodeBoundary) {
2019-02-08 22:25:43 +00:00
isASCII = false;
break;
}
}
}
}
return isASCII;
}
} // namespace
// ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer
2022-04-26 16:56:24 +00:00
STLImporter::STLImporter() :
mBuffer(),
mFileSize(0),
mScene() {
2024-12-09 20:22:47 +00:00
// empty
2022-04-26 16:56:24 +00:00
}
2019-02-08 22:25:43 +00:00
// ------------------------------------------------------------------------------------------------
// Destructor, private as well
2022-10-02 18:02:49 +00:00
STLImporter::~STLImporter() = default;
2019-02-08 22:25:43 +00:00
// ------------------------------------------------------------------------------------------------
// Returns whether the class can handle the format of the given file.
2022-04-26 16:56:24 +00:00
bool STLImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
static const char *tokens[] = { "STL", "solid" };
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
2019-02-08 22:25:43 +00:00
}
// ------------------------------------------------------------------------------------------------
2022-04-26 16:56:24 +00:00
const aiImporterDesc *STLImporter::GetInfo() const {
2019-02-08 22:25:43 +00:00
return &desc;
}
2022-04-26 16:56:24 +00:00
void addFacesToMesh(aiMesh *pMesh) {
2019-02-08 22:25:43 +00:00
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0, p = 0; i < pMesh->mNumFaces; ++i) {
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
aiFace &face = pMesh->mFaces[i];
2019-02-08 22:25:43 +00:00
face.mIndices = new unsigned int[face.mNumIndices = 3];
2022-04-26 16:56:24 +00:00
for (unsigned int o = 0; o < 3; ++o, ++p) {
2019-02-08 22:25:43 +00:00
face.mIndices[o] = p;
}
}
}
// ------------------------------------------------------------------------------------------------
// Imports the given file into the given scene structure.
2022-04-26 16:56:24 +00:00
void STLImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
2019-02-08 22:25:43 +00:00
// Check whether we can read from the file
2024-12-09 20:22:47 +00:00
if (file == nullptr) {
2022-04-26 16:56:24 +00:00
throw DeadlyImportError("Failed to open STL file ", pFile, ".");
2019-02-08 22:25:43 +00:00
}
2022-10-02 18:02:49 +00:00
mFileSize = file->FileSize();
2019-02-08 22:25:43 +00:00
// allocate storage and copy the contents of the file to a memory buffer
// (terminate it with zero)
2019-03-05 20:39:38 +00:00
std::vector<char> buffer2;
2022-04-26 16:56:24 +00:00
TextFileToBuffer(file.get(), buffer2);
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
mScene = pScene;
mBuffer = &buffer2[0];
2019-02-08 22:25:43 +00:00
// the default vertex color is light gray.
2024-12-09 20:22:47 +00:00
mClrColorDefault.r = mClrColorDefault.g = mClrColorDefault.b = mClrColorDefault.a = 0.6f;
2019-02-08 22:25:43 +00:00
// allocate a single node
2022-04-26 16:56:24 +00:00
mScene->mRootNode = new aiNode();
2019-02-08 22:25:43 +00:00
bool bMatClr = false;
2022-04-26 16:56:24 +00:00
if (IsBinarySTL(mBuffer, mFileSize)) {
2019-02-08 22:25:43 +00:00
bMatClr = LoadBinaryFile();
2022-04-26 16:56:24 +00:00
} else if (IsAsciiSTL(mBuffer, mFileSize)) {
LoadASCIIFile(mScene->mRootNode);
2019-02-08 22:25:43 +00:00
} else {
2022-04-26 16:56:24 +00:00
throw DeadlyImportError("Failed to determine STL storage representation for ", pFile, ".");
2019-02-08 22:25:43 +00:00
}
// create a single default material, using a white diffuse color for consistency with
// other geometric types (e.g., PLY).
2022-04-26 16:56:24 +00:00
aiMaterial *pcMat = new aiMaterial();
2019-02-08 22:25:43 +00:00
aiString s;
s.Set(AI_DEFAULT_MATERIAL_NAME);
pcMat->AddProperty(&s, AI_MATKEY_NAME);
2022-04-26 16:56:24 +00:00
aiColor4D clrDiffuse(ai_real(1.0), ai_real(1.0), ai_real(1.0), ai_real(1.0));
2019-02-08 22:25:43 +00:00
if (bMatClr) {
2022-04-26 16:56:24 +00:00
clrDiffuse = mClrColorDefault;
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_SPECULAR);
2024-12-09 20:22:47 +00:00
clrDiffuse = aiColor4D(0.05f, 0.05f, 0.05f, 1.0f);
2022-04-26 16:56:24 +00:00
pcMat->AddProperty(&clrDiffuse, 1, AI_MATKEY_COLOR_AMBIENT);
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
mScene->mNumMaterials = 1;
mScene->mMaterials = new aiMaterial *[1];
mScene->mMaterials[0] = pcMat;
2019-03-05 20:39:38 +00:00
mBuffer = nullptr;
2019-02-08 22:25:43 +00:00
}
// ------------------------------------------------------------------------------------------------
// Read an ASCII STL file
2022-04-26 16:56:24 +00:00
void STLImporter::LoadASCIIFile(aiNode *root) {
std::vector<aiMesh *> meshes;
std::vector<aiNode *> nodes;
const char *sz = mBuffer;
const char *bufferEnd = mBuffer + mFileSize;
2019-02-08 22:25:43 +00:00
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
2022-10-02 18:02:49 +00:00
size_t sizeEstimate = std::max(1ull, mFileSize / 160ull) * 3ull;
2019-02-08 22:25:43 +00:00
positionBuffer.reserve(sizeEstimate);
normalBuffer.reserve(sizeEstimate);
while (IsAsciiSTL(sz, static_cast<unsigned int>(bufferEnd - sz))) {
std::vector<unsigned int> meshIndices;
2022-04-26 16:56:24 +00:00
aiMesh *pMesh = new aiMesh();
2019-02-08 22:25:43 +00:00
pMesh->mMaterialIndex = 0;
2022-04-26 16:56:24 +00:00
meshIndices.push_back((unsigned int)meshes.size());
2019-02-08 22:25:43 +00:00
meshes.push_back(pMesh);
aiNode *node = new aiNode;
node->mParent = root;
2022-04-26 16:56:24 +00:00
nodes.push_back(node);
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2019-02-08 22:25:43 +00:00
ai_assert(!IsLineEnd(sz));
sz += 5; // skip the "solid"
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-04-26 16:56:24 +00:00
const char *szMe = sz;
2024-12-09 20:22:47 +00:00
while (!IsSpaceOrNewLine(*sz)) {
2019-02-08 22:25:43 +00:00
sz++;
}
2022-04-26 16:56:24 +00:00
size_t temp = (size_t)(sz - szMe);
2019-02-08 22:25:43 +00:00
// setup the name of the node
2024-12-09 20:22:47 +00:00
if (temp) {
if (temp >= AI_MAXLEN) {
2022-04-26 16:56:24 +00:00
throw DeadlyImportError("STL: Node name too long");
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
std::string name(szMe, temp);
node->mName.Set(name.c_str());
pMesh->mName.Set(name.c_str());
2019-02-08 22:25:43 +00:00
} else {
2022-04-26 16:56:24 +00:00
mScene->mRootNode->mName.Set("<STL_ASCII>");
2019-02-08 22:25:43 +00:00
}
unsigned int faceVertexCounter = 3;
2022-04-26 16:56:24 +00:00
for (;;) {
2019-02-08 22:25:43 +00:00
// go to the next token
2024-12-09 20:22:47 +00:00
if (!SkipSpacesAndLineEnd(&sz, bufferEnd)) {
2019-02-08 22:25:43 +00:00
// seems we're finished although there was no end marker
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("STL: unexpected EOF. \'endsolid\' keyword was expected");
2019-02-08 22:25:43 +00:00
break;
}
// facet normal -0.13 -0.13 -0.98
2022-04-26 16:56:24 +00:00
if (!strncmp(sz, "facet", 5) && IsSpaceOrNewLine(*(sz + 5)) && *(sz + 5) != '\0') {
2019-02-08 22:25:43 +00:00
if (faceVertexCounter != 3) {
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("STL: A new facet begins but the old is not yet complete");
2019-02-08 22:25:43 +00:00
}
faceVertexCounter = 0;
sz += 6;
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-04-26 16:56:24 +00:00
if (strncmp(sz, "normal", 6)) {
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("STL: a facet normal vector was expected but not found");
2019-02-08 22:25:43 +00:00
} else {
if (sz[6] == '\0') {
throw DeadlyImportError("STL: unexpected EOF while parsing facet");
}
2022-10-02 18:02:49 +00:00
aiVector3D vn;
2019-02-08 22:25:43 +00:00
sz += 7;
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-10-02 18:02:49 +00:00
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn.x);
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-10-02 18:02:49 +00:00
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn.y);
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-10-02 18:02:49 +00:00
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn.z);
normalBuffer.emplace_back(vn);
normalBuffer.emplace_back(vn);
normalBuffer.emplace_back(vn);
2019-02-08 22:25:43 +00:00
}
2024-12-09 20:22:47 +00:00
} else if (!strncmp(sz, "vertex", 6) && IsSpaceOrNewLine(*(sz + 6))) { // vertex 1.50000 1.50000 0.00000
2019-02-08 22:25:43 +00:00
if (faceVertexCounter >= 3) {
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_ERROR("STL: a facet with more than 3 vertices has been found");
2019-02-08 22:25:43 +00:00
++sz;
} else {
if (sz[6] == '\0') {
throw DeadlyImportError("STL: unexpected EOF while parsing facet");
}
sz += 7;
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-10-02 18:02:49 +00:00
positionBuffer.emplace_back();
2022-04-26 16:56:24 +00:00
aiVector3D *vn = &positionBuffer.back();
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->x);
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-04-26 16:56:24 +00:00
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->y);
2024-12-09 20:22:47 +00:00
SkipSpaces(&sz, bufferEnd);
2022-04-26 16:56:24 +00:00
sz = fast_atoreal_move<ai_real>(sz, (ai_real &)vn->z);
2019-02-08 22:25:43 +00:00
faceVertexCounter++;
}
2022-04-26 16:56:24 +00:00
} else if (!::strncmp(sz, "endsolid", 8)) {
2019-02-08 22:25:43 +00:00
do {
++sz;
2024-12-09 20:22:47 +00:00
} while (!IsLineEnd(*sz));
SkipSpacesAndLineEnd(&sz, bufferEnd);
2019-02-08 22:25:43 +00:00
// finished!
break;
} else { // else skip the whole identifier
do {
++sz;
2024-12-09 20:22:47 +00:00
} while (!IsSpaceOrNewLine(*sz));
2019-02-08 22:25:43 +00:00
}
}
2022-04-26 16:56:24 +00:00
if (positionBuffer.empty()) {
2019-02-08 22:25:43 +00:00
pMesh->mNumFaces = 0;
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_WARN("STL: mesh is empty or invalid; no data loaded");
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
if (positionBuffer.size() % 3 != 0) {
2019-02-08 22:25:43 +00:00
pMesh->mNumFaces = 0;
throw DeadlyImportError("STL: Invalid number of vertices");
}
2022-04-26 16:56:24 +00:00
if (normalBuffer.size() != positionBuffer.size()) {
2019-02-08 22:25:43 +00:00
pMesh->mNumFaces = 0;
throw DeadlyImportError("Normal buffer size does not match position buffer size");
}
2019-03-05 20:39:38 +00:00
2024-12-09 20:22:47 +00:00
// only process position buffer when filled, else exception when accessing with index operator
2019-03-05 20:39:38 +00:00
// see line 353: only warning is triggered
2024-12-09 20:22:47 +00:00
// see line 373(now): access to empty position buffer with index operator forced exception
2019-03-05 20:39:38 +00:00
if (!positionBuffer.empty()) {
pMesh->mNumFaces = static_cast<unsigned int>(positionBuffer.size() / 3);
pMesh->mNumVertices = static_cast<unsigned int>(positionBuffer.size());
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
2024-12-09 20:22:47 +00:00
for (size_t i = 0; i < pMesh->mNumVertices; ++i) {
2022-04-26 16:56:24 +00:00
pMesh->mVertices[i].x = positionBuffer[i].x;
pMesh->mVertices[i].y = positionBuffer[i].y;
pMesh->mVertices[i].z = positionBuffer[i].z;
}
2019-03-05 20:39:38 +00:00
positionBuffer.clear();
}
// also only process normalBuffer when filled, else exception when accessing with index operator
if (!normalBuffer.empty()) {
pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
2024-12-09 20:22:47 +00:00
for (size_t i = 0; i < pMesh->mNumVertices; ++i) {
2022-04-26 16:56:24 +00:00
pMesh->mNormals[i].x = normalBuffer[i].x;
pMesh->mNormals[i].y = normalBuffer[i].y;
pMesh->mNormals[i].z = normalBuffer[i].z;
}
2019-03-05 20:39:38 +00:00
normalBuffer.clear();
}
2019-02-08 22:25:43 +00:00
// now copy faces
addFacesToMesh(pMesh);
// assign the meshes to the current node
2022-04-26 16:56:24 +00:00
pushMeshesToNode(meshIndices, node);
2019-02-08 22:25:43 +00:00
}
// now add the loaded meshes
2022-04-26 16:56:24 +00:00
mScene->mNumMeshes = (unsigned int)meshes.size();
mScene->mMeshes = new aiMesh *[mScene->mNumMeshes];
2019-02-08 22:25:43 +00:00
for (size_t i = 0; i < meshes.size(); i++) {
2022-04-26 16:56:24 +00:00
mScene->mMeshes[i] = meshes[i];
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
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];
2019-02-08 22:25:43 +00:00
}
}
// ------------------------------------------------------------------------------------------------
// Read a binary STL file
2022-04-26 16:56:24 +00:00
bool STLImporter::LoadBinaryFile() {
2019-02-08 22:25:43 +00:00
// allocate one mesh
2022-04-26 16:56:24 +00:00
mScene->mNumMeshes = 1;
mScene->mMeshes = new aiMesh *[1];
aiMesh *pMesh = mScene->mMeshes[0] = new aiMesh();
2019-02-08 22:25:43 +00:00
pMesh->mMaterialIndex = 0;
// skip the first 80 bytes
2022-04-26 16:56:24 +00:00
if (mFileSize < 84) {
2019-02-08 22:25:43 +00:00
throw DeadlyImportError("STL: file is too small for the header");
}
bool bIsMaterialise = false;
// search for an occurrence of "COLOR=" in the header
2022-04-26 16:56:24 +00:00
const unsigned char *sz2 = (const unsigned char *)mBuffer;
const unsigned char *const szEnd = sz2 + 80;
2019-02-08 22:25:43 +00:00
while (sz2 < szEnd) {
if ('C' == *sz2++ && 'O' == *sz2++ && 'L' == *sz2++ &&
2022-04-26 16:56:24 +00:00
'O' == *sz2++ && 'R' == *sz2++ && '=' == *sz2++) {
2019-02-08 22:25:43 +00:00
// read the default vertex color for facets
bIsMaterialise = true;
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_INFO("STL: Taking code path for Materialise files");
2022-04-26 16:56:24 +00:00
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;
2019-02-08 22:25:43 +00:00
break;
}
}
2022-04-26 16:56:24 +00:00
const unsigned char *sz = (const unsigned char *)mBuffer + 80;
2019-02-08 22:25:43 +00:00
// now read the number of facets
2022-04-26 16:56:24 +00:00
mScene->mRootNode->mName.Set("<STL_BINARY>");
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
pMesh->mNumFaces = *((uint32_t *)sz);
2019-02-08 22:25:43 +00:00
sz += 4;
2022-10-02 18:02:49 +00:00
if (mFileSize < 84ull + pMesh->mNumFaces * 50ull) {
2019-02-08 22:25:43 +00:00
throw DeadlyImportError("STL: file is too small to hold all facets");
}
if (!pMesh->mNumFaces) {
throw DeadlyImportError("STL: file is empty. There are no facets defined");
}
2022-04-26 16:56:24 +00:00
pMesh->mNumVertices = pMesh->mNumFaces * 3;
2019-02-08 22:25:43 +00:00
2019-03-05 20:39:38 +00:00
aiVector3D *vp = pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
aiVector3D *vn = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
2019-02-08 22:25:43 +00:00
2024-12-09 20:22:47 +00:00
aiVector3f *theVec;
aiVector3f theVec3F;
2022-04-26 16:56:24 +00:00
for (unsigned int i = 0; i < pMesh->mNumFaces; ++i) {
2019-02-08 22:25:43 +00:00
// NOTE: Blender sometimes writes empty normals ... this is not
// our fault ... the RemoveInvalidData helper step should fix that
2019-03-05 20:39:38 +00:00
// There's one normal for the face in the STL; use it three times
// for vertex normals
2024-12-09 20:22:47 +00:00
theVec = (aiVector3f *)sz;
::memcpy(&theVec3F, theVec, sizeof(aiVector3f));
2022-04-26 16:56:24 +00:00
vn->x = theVec3F.x;
vn->y = theVec3F.y;
vn->z = theVec3F.z;
*(vn + 1) = *vn;
*(vn + 2) = *vn;
2019-03-05 20:39:38 +00:00
++theVec;
2019-02-08 22:25:43 +00:00
vn += 3;
2019-03-05 20:39:38 +00:00
// vertex 1
2024-12-09 20:22:47 +00:00
::memcpy(&theVec3F, theVec, sizeof(aiVector3f));
2022-04-26 16:56:24 +00:00
vp->x = theVec3F.x;
vp->y = theVec3F.y;
vp->z = theVec3F.z;
2019-03-05 20:39:38 +00:00
++theVec;
++vp;
// vertex 2
2024-12-09 20:22:47 +00:00
::memcpy(&theVec3F, theVec, sizeof(aiVector3f));
2022-04-26 16:56:24 +00:00
vp->x = theVec3F.x;
vp->y = theVec3F.y;
vp->z = theVec3F.z;
2019-03-05 20:39:38 +00:00
++theVec;
++vp;
// vertex 3
2024-12-09 20:22:47 +00:00
::memcpy(&theVec3F, theVec, sizeof(aiVector3f));
2022-04-26 16:56:24 +00:00
vp->x = theVec3F.x;
vp->y = theVec3F.y;
vp->z = theVec3F.z;
2019-03-05 20:39:38 +00:00
++theVec;
++vp;
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
sz = (const unsigned char *)theVec;
uint16_t color = *((uint16_t *)sz);
2019-02-08 22:25:43 +00:00
sz += 2;
2022-04-26 16:56:24 +00:00
if (color & (1 << 15)) {
2019-02-08 22:25:43 +00:00
// seems we need to take the color
2022-04-26 16:56:24 +00:00
if (!pMesh->mColors[0]) {
2019-02-08 22:25:43 +00:00
pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
2022-04-26 16:56:24 +00:00
for (unsigned int j = 0; j < pMesh->mNumVertices; ++j) {
*pMesh->mColors[0]++ = mClrColorDefault;
}
2019-02-08 22:25:43 +00:00
pMesh->mColors[0] -= pMesh->mNumVertices;
2019-03-05 20:39:38 +00:00
ASSIMP_LOG_INFO("STL: Mesh has vertex colors");
2019-02-08 22:25:43 +00:00
}
2022-04-26 16:56:24 +00:00
aiColor4D *clr = &pMesh->mColors[0][i * 3];
2019-02-08 22:25:43 +00:00
clr->a = 1.0;
2022-04-26 16:56:24 +00:00
const ai_real invVal((ai_real)1.0 / (ai_real)31.0);
2019-02-08 22:25:43 +00:00
if (bIsMaterialise) // this is reversed
{
2022-10-02 18:02:49 +00:00
clr->r = (color & 0x1fu) * invVal;
clr->g = ((color & (0x1fu << 5)) >> 5u) * invVal;
clr->b = ((color & (0x1fu << 10)) >> 10u) * invVal;
2022-04-26 16:56:24 +00:00
} else {
2022-10-02 18:02:49 +00:00
clr->b = (color & 0x1fu) * invVal;
clr->g = ((color & (0x1fu << 5)) >> 5u) * invVal;
clr->r = ((color & (0x1fu << 10)) >> 10u) * invVal;
2019-02-08 22:25:43 +00:00
}
// assign the color to all vertices of the face
2022-04-26 16:56:24 +00:00
*(clr + 1) = *clr;
*(clr + 2) = *clr;
2019-02-08 22:25:43 +00:00
}
}
// now copy faces
addFacesToMesh(pMesh);
2022-04-26 16:56:24 +00:00
aiNode *root = mScene->mRootNode;
2019-03-05 20:39:38 +00:00
// allocate one node
2022-04-26 16:56:24 +00:00
aiNode *node = new aiNode();
2019-03-05 20:39:38 +00:00
node->mParent = root;
root->mNumChildren = 1u;
2022-04-26 16:56:24 +00:00
root->mChildren = new aiNode *[root->mNumChildren];
2019-03-05 20:39:38 +00:00
root->mChildren[0] = node;
2019-02-08 22:25:43 +00:00
// add all created meshes to the single node
2022-04-26 16:56:24 +00:00
node->mNumMeshes = mScene->mNumMeshes;
node->mMeshes = new unsigned int[mScene->mNumMeshes];
for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
2019-03-05 20:39:38 +00:00
node->mMeshes[i] = i;
2022-04-26 16:56:24 +00:00
}
2019-02-08 22:25:43 +00:00
2022-04-26 16:56:24 +00:00
if (bIsMaterialise && !pMesh->mColors[0]) {
2019-02-08 22:25:43 +00:00
// use the color as diffuse material color
return true;
}
return false;
}
2022-04-26 16:56:24 +00:00
void STLImporter::pushMeshesToNode(std::vector<unsigned int> &meshIndices, aiNode *node) {
ai_assert(nullptr != node);
if (meshIndices.empty()) {
2019-02-08 22:25:43 +00:00
return;
}
2022-04-26 16:56:24 +00:00
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];
2019-02-08 22:25:43 +00:00
}
meshIndices.clear();
}
2024-12-09 20:22:47 +00:00
} // namespace Assimp
2019-02-08 22:25:43 +00:00
#endif // !! ASSIMP_BUILD_NO_STL_IMPORTER