Skip to content

Commit

Permalink
Merge pull request #2026 from zenustech/import-stl
Browse files Browse the repository at this point in the history
import stl
  • Loading branch information
zhxx1987 authored Oct 10, 2024
2 parents e45c919 + db89a94 commit 27c46aa
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions projects/FBX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ target_include_directories(zeno PRIVATE ../../ui/include)
set(ASSIMP_BUILD_ALL_EXPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE)
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "GUN TMD ASS" FORCE)
set(ASSIMP_BUILD_FBX_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
set(ASSIMP_BUILD_STL_IMPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
set(ASSIMP_BUILD_FBX_EXPORTER TRUE CACHE BOOL "CIHOU FBX" FORCE)
add_subdirectory(assimp)
target_link_libraries(zeno PRIVATE assimp)
Expand Down
60 changes: 60 additions & 0 deletions projects/FBX/ReadFBX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,4 +1340,64 @@ ZENDEFNODE(ReadLightFromFile,
"FBX",
}
});

struct ReadSTL : zeno::INode {
virtual void apply() override {
auto filepath = get_input2<std::string>("path");

Assimp::Importer importer;

// Import the STL file with triangulation and joining identical vertices
const aiScene* scene = importer.ReadFile(filepath, 0);

// Check if the import was successful
if (!scene) {
std::cerr << "Failed to load model: " << importer.GetErrorString() << std::endl;
}

auto prim = std::make_shared<zeno::PrimitiveObject>();

// Iterate through all meshes in the scene
for (unsigned int meshIndex = 0; meshIndex < scene->mNumMeshes; ++meshIndex) {
aiMesh* mesh = scene->mMeshes[meshIndex];
prim->verts.resize(mesh->mNumVertices);
std::cout << "Mesh " << meshIndex << ": " << mesh->mNumVertices << " vertices, " << mesh->mNumFaces << " faces." << std::endl;

// Iterate through all vertices in the mesh
for (unsigned int vertexIndex = 0; vertexIndex < mesh->mNumVertices; ++vertexIndex) {
aiVector3D position = mesh->mVertices[vertexIndex];
prim->verts[vertexIndex] = {position.x, position.y, position.z};
}

prim->loops.reserve(mesh->mNumFaces * 3);
prim->polys.resize(mesh->mNumFaces);

int counter = 0;

// Iterate through all faces in the mesh
for (unsigned int faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex) {
aiFace face = mesh->mFaces[faceIndex];

for (unsigned int i = 0; i < face.mNumIndices; ++i) {

prim->loops.push_back(face.mIndices[i]);
}

prim->polys[faceIndex] = {counter, int(face.mNumIndices)};
counter += face.mNumIndices;
}
}
set_output("prim", prim);
}
};
ZENDEFNODE(ReadSTL, {
{
{"readpath", "path"},
},
{
{"prim"},
},
{},
{"BaseIO"},
});
}

0 comments on commit 27c46aa

Please sign in to comment.