From cbc138a525acc647194b91fe34368e1f5fa8a741 Mon Sep 17 00:00:00 2001 From: Filip Sykala Date: Thu, 14 Apr 2022 08:58:00 +0200 Subject: [PATCH] Fix of assertation from sinking contour assertation at: GLModel::init_from(Slic3r::GUI::GLModel::Geometry && data) Line 407 It is called from: GLVolume::SinkingContours::update() Line 357 polygons(defined on line 344) are empty. --> also init_data will be empty. --- src/slic3r/GUI/3DScene.cpp | 123 ++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 57 deletions(-) diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index f25b89da8..8eff3e76e 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -324,64 +324,73 @@ void GLVolume::SinkingContours::update() const int object_idx = m_parent.object_idx(); const Model& model = GUI::wxGetApp().plater()->model(); - if (0 <= object_idx && object_idx < int(model.objects.size()) && m_parent.is_sinking() && !m_parent.is_below_printbed()) { - const BoundingBoxf3& box = m_parent.transformed_convex_hull_bounding_box(); - if (!m_old_box.size().isApprox(box.size()) || m_old_box.min.z() != box.min.z()) { - m_old_box = box; - m_shift = Vec3d::Zero(); - - const TriangleMesh& mesh = model.objects[object_idx]->volumes[m_parent.volume_idx()]->mesh(); - - m_model.reset(); - GUI::GLModel::Geometry init_data; -#if ENABLE_LEGACY_OPENGL_REMOVAL - init_data.format = { GUI::GLModel::Geometry::EPrimitiveType::Triangles, GUI::GLModel::Geometry::EVertexLayout::P3 }; - init_data.color = ColorRGBA::WHITE(); - unsigned int vertices_counter = 0; -#endif // ENABLE_LEGACY_OPENGL_REMOVAL - MeshSlicingParams slicing_params; - slicing_params.trafo = m_parent.world_matrix(); - const Polygons polygons = union_(slice_mesh(mesh.its, 0.0f, slicing_params)); - for (const ExPolygon& expoly : diff_ex(expand(polygons, float(scale_(HalfWidth))), shrink(polygons, float(scale_(HalfWidth))))) { -#if ENABLE_LEGACY_OPENGL_REMOVAL - const std::vector triangulation = triangulate_expolygon_3d(expoly); - init_data.reserve_vertices(init_data.vertices_count() + triangulation.size()); - init_data.reserve_indices(init_data.indices_count() + triangulation.size()); - for (const Vec3d& v : triangulation) { - init_data.add_vertex((Vec3f)(v.cast() + 0.015f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting - ++vertices_counter; - if (vertices_counter % 3 == 0) - init_data.add_triangle(vertices_counter - 3, vertices_counter - 2, vertices_counter - 1); - } - } - m_model.init_from(std::move(init_data)); -#else - GUI::GLModel::Geometry::Entity entity; - entity.type = GUI::GLModel::EPrimitiveType::Triangles; - const std::vector triangulation = triangulate_expolygon_3d(expoly); - entity.positions.reserve(entity.positions.size() + triangulation.size()); - entity.normals.reserve(entity.normals.size() + triangulation.size()); - entity.indices.reserve(entity.indices.size() + triangulation.size() / 3); - for (const Vec3d& v : triangulation) { - entity.positions.emplace_back(v.cast() + 0.015f * Vec3f::UnitZ()); // add a small positive z to avoid z-fighting - entity.normals.emplace_back(Vec3f::UnitZ()); - const size_t positions_count = entity.positions.size(); - if (positions_count % 3 == 0) { - entity.indices.emplace_back(positions_count - 3); - entity.indices.emplace_back(positions_count - 2); - entity.indices.emplace_back(positions_count - 1); - } - } - init_data.entities.emplace_back(entity); - } - m_model.init_from(init_data); -#endif // ENABLE_LEGACY_OPENGL_REMOVAL - } - else - m_shift = box.center() - m_old_box.center(); - } - else + if (object_idx < 0 || + object_idx >= int(model.objects.size()) || + !m_parent.is_sinking() || + m_parent.is_below_printbed()){ m_model.reset(); + return; + } + + const BoundingBoxf3& box = m_parent.transformed_convex_hull_bounding_box(); + if (m_old_box.size().isApprox(box.size()) && + m_old_box.min.z() == box.min.z()){ + // Fix it !!! It is not working all the time + m_shift = box.center() - m_old_box.center(); + return; + } + + m_old_box = box; + m_shift = Vec3d::Zero(); + + const TriangleMesh& mesh = model.objects[object_idx]->volumes[m_parent.volume_idx()]->mesh(); + + m_model.reset(); + GUI::GLModel::Geometry init_data; +#if ENABLE_LEGACY_OPENGL_REMOVAL + init_data.format = { GUI::GLModel::Geometry::EPrimitiveType::Triangles, GUI::GLModel::Geometry::EVertexLayout::P3 }; + init_data.color = ColorRGBA::WHITE(); + unsigned int vertices_counter = 0; +#endif // ENABLE_LEGACY_OPENGL_REMOVAL + MeshSlicingParams slicing_params; + slicing_params.trafo = m_parent.world_matrix(); + const Polygons polygons = union_(slice_mesh(mesh.its, 0.0f, slicing_params)); + if (polygons.empty()) return; + + for (const ExPolygon& expoly : diff_ex(expand(polygons, float(scale_(HalfWidth))), shrink(polygons, float(scale_(HalfWidth))))) { +#if ENABLE_LEGACY_OPENGL_REMOVAL + const std::vector triangulation = triangulate_expolygon_3d(expoly); + init_data.reserve_vertices(init_data.vertices_count() + triangulation.size()); + init_data.reserve_indices(init_data.indices_count() + triangulation.size()); + for (const Vec3d& v : triangulation) { + init_data.add_vertex((Vec3f)(v.cast() + 0.015f * Vec3f::UnitZ())); // add a small positive z to avoid z-fighting + ++vertices_counter; + if (vertices_counter % 3 == 0) + init_data.add_triangle(vertices_counter - 3, vertices_counter - 2, vertices_counter - 1); + } + } + m_model.init_from(std::move(init_data)); +#else + GUI::GLModel::Geometry::Entity entity; + entity.type = GUI::GLModel::EPrimitiveType::Triangles; + const std::vector triangulation = triangulate_expolygon_3d(expoly); + entity.positions.reserve(entity.positions.size() + triangulation.size()); + entity.normals.reserve(entity.normals.size() + triangulation.size()); + entity.indices.reserve(entity.indices.size() + triangulation.size() / 3); + for (const Vec3d& v : triangulation) { + entity.positions.emplace_back(v.cast() + 0.015f * Vec3f::UnitZ()); // add a small positive z to avoid z-fighting + entity.normals.emplace_back(Vec3f::UnitZ()); + const size_t positions_count = entity.positions.size(); + if (positions_count % 3 == 0) { + entity.indices.emplace_back(positions_count - 3); + entity.indices.emplace_back(positions_count - 2); + entity.indices.emplace_back(positions_count - 1); + } + } + init_data.entities.emplace_back(entity); + } + m_model.init_from(init_data); +#endif // ENABLE_LEGACY_OPENGL_REMOVAL } #if ENABLE_SHOW_NON_MANIFOLD_EDGES