From 9ef826cb5f61f1865aeef8ef44812cf0d4bbdfc6 Mon Sep 17 00:00:00 2001 From: tamasmeszaros Date: Thu, 8 Sep 2022 11:50:58 +0200 Subject: [PATCH] Fix potential crashes in SLA normal calculation routine --- src/libslic3r/MeshNormals.cpp | 8 +++++--- src/libslic3r/SLA/SupportTreeUtils.hpp | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libslic3r/MeshNormals.cpp b/src/libslic3r/MeshNormals.cpp index b77ab8ba22..3f7117899a 100644 --- a/src/libslic3r/MeshNormals.cpp +++ b/src/libslic3r/MeshNormals.cpp @@ -24,6 +24,7 @@ Vec3d get_normal(const AABBMesh &mesh, Vec3d p; mesh.squared_distance(picking_point, faceid, p); + assert(int(faceid) < int(mesh.get_triangle_mesh()->indices.size())); auto trindex = mesh.indices(faceid); @@ -89,9 +90,10 @@ Vec3d get_normal(const AABBMesh &mesh, } } else if (edge_idx >= 0) { // the point is on and edge size_t neighbor_face = mesh.face_neighbor_index()[faceid](edge_idx); - - neigh.emplace_back(mesh.normal_by_face_id(faceid)); - neigh.emplace_back(mesh.normal_by_face_id(neighbor_face)); + if (neighbor_face < mesh.indices().size()) { + neigh.emplace_back(mesh.normal_by_face_id(faceid)); + neigh.emplace_back(mesh.normal_by_face_id(neighbor_face)); + } } if (!neigh.empty()) { // there were neighbors to count with diff --git a/src/libslic3r/SLA/SupportTreeUtils.hpp b/src/libslic3r/SLA/SupportTreeUtils.hpp index d5707dc5c1..38515f8793 100644 --- a/src/libslic3r/SLA/SupportTreeUtils.hpp +++ b/src/libslic3r/SLA/SupportTreeUtils.hpp @@ -517,6 +517,7 @@ bool optimize_pinhead_placement(Ex policy, Head &head) { Vec3d n = get_normal(m.emesh, head.pos); + assert(std::abs(n.norm() - 1.0) < EPSILON); // for all normals the spherical coordinates are generated and // the polar angle is saturated to 45 degrees from the bottom then