From c37d18f046bfe5d868968815235e75b17e5c6f04 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 17 May 2021 12:53:05 +0200 Subject: [PATCH 1/2] Follow-up of ca14ea4c33a0cb368e7bf4dc1576c80900882e24 -> Fixed arrange with sinking objects --- src/libslic3r/Model.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 2a5f736458..83c03eef0e 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -1906,8 +1906,13 @@ arrangement::ArrangePolygon ModelInstance::get_arrange_polygon() const Vec3d rotation = get_rotation(); rotation.z() = 0.; Transform3d trafo_instance = +#if ENABLE_ALLOW_NEGATIVE_Z + Geometry::assemble_transform(get_offset().z() * Vec3d::UnitZ(), rotation, + get_scaling_factor(), get_mirror()); +#else Geometry::assemble_transform(Vec3d::Zero(), rotation, get_scaling_factor(), get_mirror()); +#endif // ENABLE_ALLOW_NEGATIVE_Z Polygon p = get_object()->convex_hull_2d(trafo_instance); From 0605813e683ed06ba114d9a48e8697ba22864b21 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Mon, 17 May 2021 13:02:38 +0200 Subject: [PATCH 2/2] Faster ModelObject::convex_hull_2d() by using ModelVolume 3D convex hulls --- src/libslic3r/Model.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 83c03eef0e..8d73782a40 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -893,6 +893,30 @@ Polygon ModelObject::convex_hull_2d(const Transform3d &trafo_instance) const Points pts; for (const ModelVolume *v : this->volumes) if (v->is_model_part()) { +#if ENABLE_ALLOW_NEGATIVE_Z + const Transform3d trafo = trafo_instance * v->get_matrix(); + const TriangleMesh& hull_3d = v->get_convex_hull(); + const indexed_triangle_set& its = hull_3d.its; + if (its.vertices.empty()) { + // Using the STL faces. + const stl_file& stl = hull_3d.stl; + for (const stl_facet& facet : stl.facet_start) { + for (size_t j = 0; j < 3; ++j) { + const Vec3d p = trafo * facet.vertex[j].cast(); + if (p.z() >= 0.0) + pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); + } + } + } + else { + // Using the shared vertices should be a bit quicker than using the STL faces. + for (size_t i = 0; i < its.vertices.size(); ++i) { + const Vec3d p = trafo * its.vertices[i].cast(); + if (p.z() >= 0.0) + pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); + } + } +#else Transform3d trafo = trafo_instance * v->get_matrix(); const indexed_triangle_set &its = v->mesh().its; if (its.vertices.empty()) { @@ -901,21 +925,16 @@ Polygon ModelObject::convex_hull_2d(const Transform3d &trafo_instance) const for (const stl_facet &facet : stl.facet_start) for (size_t j = 0; j < 3; ++ j) { Vec3d p = trafo * facet.vertex[j].cast(); -#if ENABLE_ALLOW_NEGATIVE_Z - if (p.z() >= 0.0) -#endif // ENABLE_ALLOW_NEGATIVE_Z pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); } } else { // Using the shared vertices should be a bit quicker than using the STL faces. for (size_t i = 0; i < its.vertices.size(); ++ i) { Vec3d p = trafo * its.vertices[i].cast(); -#if ENABLE_ALLOW_NEGATIVE_Z - if (p.z() >= 0.0) -#endif // ENABLE_ALLOW_NEGATIVE_Z pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); } } +#endif // ENABLE_ALLOW_NEGATIVE_Z } std::sort(pts.begin(), pts.end(), [](const Point& a, const Point& b) { return a(0) < b(0) || (a(0) == b(0) && a(1) < b(1)); }); pts.erase(std::unique(pts.begin(), pts.end(), [](const Point& a, const Point& b) { return a(0) == b(0) && a(1) == b(1); }), pts.end());