From e3db1879b128ecf5af383c7c23a103f5b6300e4d Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Wed, 12 Oct 2022 10:05:45 +0200 Subject: [PATCH] Fixed differences after rebase to master --- src/slic3r/GUI/GLCanvas3D.cpp | 26 ++- src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp | 252 +++++++++++------------ src/slic3r/GUI/Gizmos/GLGizmoMeasure.hpp | 1 - 3 files changed, 133 insertions(+), 146 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 7d8ca616b4..62912fa5e1 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -5537,15 +5537,19 @@ void GLCanvas3D::_picking_pass() imgui.text_colored(col_2_color, col_2.c_str()); }; - char buf[1024]; if (hit.type != SceneRaycaster::EType::None) { if (ImGui::BeginTable("Hit", 2)) { - add_strings_row_to_table("Object ID", ImGuiWrapper::COL_ORANGE_LIGHT, std::to_string(hit.raycaster_id), ImGui::GetStyleColorVec4(ImGuiCol_Text)); - add_strings_row_to_table("Type", ImGuiWrapper::COL_ORANGE_LIGHT, object_type, ImGui::GetStyleColorVec4(ImGuiCol_Text)); + char buf[1024]; + add_strings_row_to_table("Object ID:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::to_string(hit.raycaster_id), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); + add_strings_row_to_table("Type:", ImGuiWrapper::COL_ORANGE_LIGHT, + object_type, ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); sprintf(buf, "%.3f, %.3f, %.3f", hit.position.x(), hit.position.y(), hit.position.z()); - add_strings_row_to_table("Position", ImGuiWrapper::COL_ORANGE_LIGHT, std::string(buf), ImGui::GetStyleColorVec4(ImGuiCol_Text)); + add_strings_row_to_table("Position:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::string(buf), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); sprintf(buf, "%.3f, %.3f, %.3f", hit.normal.x(), hit.normal.y(), hit.normal.z()); - add_strings_row_to_table("Normal", ImGuiWrapper::COL_ORANGE_LIGHT, std::string(buf), ImGui::GetStyleColorVec4(ImGuiCol_Text)); + add_strings_row_to_table("Normal:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::string(buf), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); ImGui::EndTable(); } } @@ -5554,13 +5558,17 @@ void GLCanvas3D::_picking_pass() ImGui::Separator(); imgui.text("Registered for picking:"); - if (ImGui::BeginTable("Raycasters", 2)) { + if (ImGui::BeginTable("Counters", 2)) { + char buf[1024]; sprintf(buf, "%d (%d)", (int)m_scene_raycaster.beds_count(), (int)m_scene_raycaster.active_beds_count()); - add_strings_row_to_table("Beds", ImGuiWrapper::COL_ORANGE_LIGHT, std::string(buf), ImGui::GetStyleColorVec4(ImGuiCol_Text)); + add_strings_row_to_table("Beds:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::string(buf), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); sprintf(buf, "%d (%d)", (int)m_scene_raycaster.volumes_count(), (int)m_scene_raycaster.active_volumes_count()); - add_strings_row_to_table("Volumes", ImGuiWrapper::COL_ORANGE_LIGHT, std::string(buf), ImGui::GetStyleColorVec4(ImGuiCol_Text)); + add_strings_row_to_table("Volumes:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::string(buf), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); sprintf(buf, "%d (%d)", (int)m_scene_raycaster.gizmos_count(), (int)m_scene_raycaster.active_gizmos_count()); - add_strings_row_to_table("Gizmo elements", ImGuiWrapper::COL_ORANGE_LIGHT, std::string(buf), ImGui::GetStyleColorVec4(ImGuiCol_Text)); + add_strings_row_to_table("Gizmo elements:", ImGuiWrapper::COL_ORANGE_LIGHT, + std::string(buf), ImGuiWrapper::to_ImVec4(ColorRGBA::WHITE())); ImGui::EndTable(); } imgui.end(); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp index 70bbc92107..3af58dd960 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoFlatten.cpp @@ -10,7 +10,6 @@ #include "libslic3r/Geometry/ConvexHull.hpp" #include "libslic3r/Model.hpp" -#include "libslic3r/SurfaceMesh.hpp" #include @@ -22,31 +21,9 @@ namespace GUI { static const Slic3r::ColorRGBA DEFAULT_PLANE_COLOR = { 0.9f, 0.9f, 0.9f, 0.5f }; static const Slic3r::ColorRGBA DEFAULT_HOVER_PLANE_COLOR = { 0.9f, 0.9f, 0.9f, 0.75f }; - - - -// TESTING: -static Halfedge_index hi; -static bool hi_initialized = false; -static std::unique_ptr sm_ptr; -static Vertex_index src; -static Vertex_index tgt; - - - - - - GLGizmoFlatten::GLGizmoFlatten(GLCanvas3D& parent, const std::string& icon_filename, unsigned int sprite_id) : GLGizmoBase(parent, icon_filename, sprite_id) -{ - indexed_triangle_set a = its_make_cone(0.05, .2); - its_rotate_x(a, M_PI); - its_translate(a, stl_vertex(0., 0., .8)); - indexed_triangle_set b = its_make_cylinder(.02, 0.8); - its_merge(a, b); - arrow.init_from(a); -} +{} bool GLGizmoFlatten::on_mouse(const wxMouseEvent &mouse_event) { @@ -60,7 +37,9 @@ bool GLGizmoFlatten::on_mouse(const wxMouseEvent &mouse_event) m_mouse_left_down = true; Selection &selection = m_parent.get_selection(); if (selection.is_single_full_instance()) { - hi = sm_ptr->halfedge(Face_index(m_hover_id)); + // Rotate the object so the normal points downward: + selection.flattening_rotate(m_planes[m_hover_id].normal); + m_parent.do_rotate(L("Gizmo-Place on Face")); } return true; } @@ -122,18 +101,6 @@ void GLGizmoFlatten::on_render() const Selection& selection = m_parent.get_selection(); #if ENABLE_LEGACY_OPENGL_REMOVAL - - - - if (!hi_initialized) { - const indexed_triangle_set & its = m_c->selection_info()->model_object()->volumes.front()->mesh().its; - sm_ptr.reset(new SurfaceMesh(its)); - hi = sm_ptr->halfedge(Face_index(0)); - hi_initialized = true; - } - SurfaceMesh & sm = *sm_ptr; - - GLShaderProgram* shader = wxGetApp().get_shader("flat"); if (shader == nullptr) return; @@ -169,13 +136,8 @@ void GLGizmoFlatten::on_render() m_planes[i].vbo.model.set_color(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR : DEFAULT_PLANE_COLOR); m_planes[i].vbo.model.render(); #else - int cur_face = hi.is_invalid() ? 1000000 : sm.face(hi); - for (int i = 0; i < m_planes.size(); ++i) { - m_planes[i].vbo.set_color(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR : DEFAULT_PLANE_COLOR); - if (i == cur_face) - m_planes[i].vbo.set_color(i == m_hover_id ? ColorRGBA(.5f, 0.f, 0.f, 1.f) : ColorRGBA(1.f, 0.f, 0.f, 1.f)); - m_planes[i].vbo.render(); - } + m_planes[i].vbo.set_color(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR : DEFAULT_PLANE_COLOR); + m_planes[i].vbo.render(); #endif // ENABLE_RAYCAST_PICKING #else glsafe(::glColor4fv(i == m_hover_id ? DEFAULT_HOVER_PLANE_COLOR.data() : DEFAULT_PLANE_COLOR.data())); @@ -188,90 +150,6 @@ void GLGizmoFlatten::on_render() #endif // !ENABLE_LEGACY_OPENGL_REMOVAL } - - - - - ///////////////// - //////////////// - ////////////////// - - auto draw_arrow = [&](const Vec3d& from, const Vec3d& to) -> void { - Vec3d desired_pos = from; - Vec3d desired_dir = to - from; - double desired_len = desired_dir.norm(); - desired_dir.normalize(); - - Transform3d m = selection.get_volume(*selection.get_volume_idxs().begin())->get_instance_transformation().get_matrix(); - m.translate(desired_pos); - Eigen::Quaterniond q; - Transform3d rot = Transform3d::Identity(); - rot.matrix().block(0, 0, 3, 3) = q.setFromTwoVectors(Vec3d::UnitZ(), desired_dir).toRotationMatrix(); - Transform3d sc = Transform3d::Identity(); - sc.scale(desired_len); - m = m * sc * rot; - - const Camera & camera = wxGetApp().plater()->get_camera(); - Transform3d view_model_matrix = camera.get_view_matrix() * - Geometry::assemble_transform(selection.get_volume(*selection.get_volume_idxs().begin())->get_sla_shift_z() * Vec3d::UnitZ()) * m; - - shader->set_uniform("view_model_matrix", view_model_matrix); - arrow.render(); - }; - - m_imgui->begin(std::string("DEBUG")); - bool invalid = hi.is_invalid(); - if (invalid) { - if (m_imgui->button(std::string("HALFEDGE INVALID (Click to reset)"))) - hi = sm.halfedge(Face_index(0)); - } - else { - m_imgui->text(sm.is_border(hi) ? "BORDER HALFEDGE !" : "Halfedge is not border"); - m_imgui->text((std::string("Face: ") + std::to_string(int(hi.face()))).c_str()); - m_imgui->text(std::string("Target degree:" + std::to_string(sm.degree(sm.target(hi))))); - m_imgui->text(std::string("Face degree:" + std::to_string(sm.degree(sm.face(hi))))); - } - m_imgui->disabled_begin(invalid); - if (m_imgui->button(std::string("next"))) - hi = sm.next(hi); - if (m_imgui->button(std::string("prev"))) - hi = sm.prev(hi); - if (m_imgui->button(std::string("opposite"))) - hi = sm.opposite(hi); - if (m_imgui->button(std::string("next_around_target"))) - hi = sm.next_around_target(hi); - if (m_imgui->button(std::string("prev_around_target"))) - hi = sm.prev_around_target(hi); - if (m_imgui->button(std::string("next_around_source"))) - hi = sm.next_around_source(hi); - if (m_imgui->button(std::string("prev_around_source"))) - hi = sm.prev_around_source(hi); - if (m_imgui->button(std::string("remember one"))) - src = sm.target(hi); - if (m_imgui->button(std::string("switch to halfedge"))) { - tgt = sm.target(hi); - hi = sm.halfedge(src, tgt); - } - - if (invalid) - m_imgui->disabled_end(); - m_imgui->end(); - - if (!hi.is_invalid()) { - Vec3d a = sm.point(sm.source(hi)).cast(); - Vec3d b = sm.point(sm.target(hi)).cast(); - draw_arrow(a, b); - } - - - ///////////////// - //////////////// - ////////////////// - - - - - glsafe(::glEnable(GL_CULL_FACE)); glsafe(::glDisable(GL_BLEND)); @@ -375,11 +253,11 @@ void GLGizmoFlatten::update_planes() for (const ModelVolume* vol : mo->volumes) { if (vol->type() != ModelVolumeType::MODEL_PART) continue; - TriangleMesh vol_ch = vol->mesh(); //vol->get_convex_hull(); + TriangleMesh vol_ch = vol->get_convex_hull(); vol_ch.transform(vol->get_matrix()); ch.merge(vol_ch); } - //ch = ch.convex_hull_3d(); + ch = ch.convex_hull_3d(); m_planes.clear(); #if ENABLE_RAYCAST_PICKING on_unregister_raycasters_for_picking(); @@ -403,17 +281,47 @@ void GLGizmoFlatten::update_planes() std::vector facet_visited(num_of_facets, false); int facet_queue_cnt = 0; const stl_normal* normal_ptr = nullptr; + int facet_idx = 0; + while (1) { + // Find next unvisited triangle: + for (; facet_idx < num_of_facets; ++ facet_idx) + if (!facet_visited[facet_idx]) { + facet_queue[facet_queue_cnt ++] = facet_idx; + facet_visited[facet_idx] = true; + normal_ptr = &face_normals[facet_idx]; + m_planes.emplace_back(); + break; + } + if (facet_idx == num_of_facets) + break; // Everything was visited already + + while (facet_queue_cnt > 0) { + int facet_idx = facet_queue[-- facet_queue_cnt]; + const stl_normal& this_normal = face_normals[facet_idx]; + if (std::abs(this_normal(0) - (*normal_ptr)(0)) < 0.001 && std::abs(this_normal(1) - (*normal_ptr)(1)) < 0.001 && std::abs(this_normal(2) - (*normal_ptr)(2)) < 0.001) { + const Vec3i face = ch.its.indices[facet_idx]; + for (int j=0; j<3; ++j) + m_planes.back().vertices.emplace_back(ch.its.vertices[face[j]].cast()); + + facet_visited[facet_idx] = true; + for (int j = 0; j < 3; ++ j) + if (int neighbor_idx = face_neighbors[facet_idx][j]; neighbor_idx >= 0 && ! facet_visited[neighbor_idx]) + facet_queue[facet_queue_cnt ++] = neighbor_idx; + } + } + m_planes.back().normal = normal_ptr->cast(); - for (size_t i = 0; i < ch.its.indices.size(); ++i) { - const Vec3i & face = ch.its.indices[i]; - m_planes.emplace_back(); - for (int j = 0; j < 3; ++j) - m_planes.back().vertices.emplace_back(ch.its.vertices[face[j]].cast()); - m_planes.back().normal = face_normals[i].cast(); Pointf3s& verts = m_planes.back().vertices; // Now we'll transform all the points into world coordinates, so that the areas, angles and distances // make real sense. verts = transform(verts, inst_matrix); + + // if this is a just a very small triangle, remove it to speed up further calculations (it would be rejected later anyway): + if (verts.size() == 3 && + ((verts[0] - verts[1]).norm() < minimal_side + || (verts[0] - verts[2]).norm() < minimal_side + || (verts[1] - verts[2]).norm() < minimal_side)) + m_planes.pop_back(); } // Let's prepare transformation of the normal vector from mesh to instance coordinates. @@ -445,12 +353,84 @@ void GLGizmoFlatten::update_planes() polygon = Slic3r::Geometry::convex_hull(polygon); polygon = transform(polygon, tr.inverse()); + // Calculate area of the polygons and discard ones that are too small + float& area = m_planes[polygon_id].area; + area = 0.f; + for (unsigned int i = 0; i < polygon.size(); i++) // Shoelace formula + area += polygon[i](0)*polygon[i + 1 < polygon.size() ? i + 1 : 0](1) - polygon[i + 1 < polygon.size() ? i + 1 : 0](0)*polygon[i](1); + area = 0.5f * std::abs(area); + + bool discard = false; + if (area < minimal_area) + discard = true; + else { + // We also check the inner angles and discard polygons with angles smaller than the following threshold + const double angle_threshold = ::cos(10.0 * (double)PI / 180.0); + + for (unsigned int i = 0; i < polygon.size(); ++i) { + const Vec3d& prec = polygon[(i == 0) ? polygon.size() - 1 : i - 1]; + const Vec3d& curr = polygon[i]; + const Vec3d& next = polygon[(i == polygon.size() - 1) ? 0 : i + 1]; + + if ((prec - curr).normalized().dot((next - curr).normalized()) > angle_threshold) { + discard = true; + break; + } + } + } + + if (discard) { + m_planes[polygon_id--] = std::move(m_planes.back()); + m_planes.pop_back(); + continue; + } + // We will shrink the polygon a little bit so it does not touch the object edges: Vec3d centroid = std::accumulate(polygon.begin(), polygon.end(), Vec3d(0.0, 0.0, 0.0)); centroid /= (double)polygon.size(); for (auto& vertex : polygon) vertex = 0.9f*vertex + 0.1f*centroid; + // Polygon is now simple and convex, we'll round the corners to make them look nicer. + // The algorithm takes a vertex, calculates middles of respective sides and moves the vertex + // towards their average (controlled by 'aggressivity'). This is repeated k times. + // In next iterations, the neighbours are not always taken at the middle (to increase the + // rounding effect at the corners, where we need it most). + const unsigned int k = 10; // number of iterations + const float aggressivity = 0.2f; // agressivity + const unsigned int N = polygon.size(); + std::vector> neighbours; + if (k != 0) { + Pointf3s points_out(2*k*N); // vector long enough to store the future vertices + for (unsigned int j=0; j