diff --git a/xs/src/libslic3r/TriangleMesh.cpp b/xs/src/libslic3r/TriangleMesh.cpp index 957515db3..19dcc6a07 100644 --- a/xs/src/libslic3r/TriangleMesh.cpp +++ b/xs/src/libslic3r/TriangleMesh.cpp @@ -728,6 +728,7 @@ TriangleMesh TriangleMesh::convex_hull_3d() const TriangleMesh output_mesh(det_vertices, facets); output_mesh.repair(); + output_mesh.require_shared_vertices(); return output_mesh; } diff --git a/xs/src/slic3r/GUI/GLCanvas3D.cpp b/xs/src/slic3r/GUI/GLCanvas3D.cpp index 2bf516e3f..947af6754 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.cpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.cpp @@ -1409,14 +1409,14 @@ Pointf3 GLCanvas3D::Gizmos::get_flattening_normal() const return (it != m_gizmos.end()) ? reinterpret_cast(it->second)->get_flattening_normal() : Pointf3(0.f, 0.f, 0.f); } -void GLCanvas3D::Gizmos::set_flattening_data(std::vector vertices_list) +void GLCanvas3D::Gizmos::set_flattening_data(const ModelObject* model_object) { if (!m_enabled) return; GizmosMap::const_iterator it = m_gizmos.find(Flatten); if (it != m_gizmos.end()) - reinterpret_cast(it->second)->set_flattening_data(vertices_list); + reinterpret_cast(it->second)->set_flattening_data(model_object); } void GLCanvas3D::Gizmos::render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const @@ -2202,62 +2202,7 @@ void GLCanvas3D::update_gizmos_data() { m_gizmos.set_scale(model_instance->scaling_factor); m_gizmos.set_angle_z(model_instance->rotation); - - ///////////////////////////////////////////////////////////////////////// - // Following block provides convex hull data to the Flatten gizmo - // It is temporary, it should be optimized and moved elsewhere later - TriangleMesh ch = model_object->mesh().convex_hull_3d(); - stl_facet* facet_ptr = ch.stl.facet_start; - std::vector points; - const unsigned int k = 20; - const float ratio = 0.2f; - const unsigned int N = 3; // 3 - triangle - - while (facet_ptr < ch.stl.facet_start+ch.stl.stats.number_of_facets) { - Pointf3 a = Pointf3(facet_ptr->vertex[1].x - facet_ptr->vertex[0].x, facet_ptr->vertex[1].y - facet_ptr->vertex[0].y, facet_ptr->vertex[1].z - facet_ptr->vertex[0].z); - Pointf3 b = Pointf3(facet_ptr->vertex[2].x - facet_ptr->vertex[0].x, facet_ptr->vertex[2].y - facet_ptr->vertex[0].y, facet_ptr->vertex[2].z - facet_ptr->vertex[0].z); - - - if (0.5 * sqrt(dot(cross(a, b), cross(a,b))) > 50.f) { - points.emplace_back(Pointf3s(2*k*N)); - - std::vector> neighbours; - if (k != 0) { - for (unsigned int j=0; jvertex[j].x, facet_ptr->vertex[j].y, facet_ptr->vertex[j].z); - neighbours.push_back(std::make_pair((int)(j*2*k-k) < 0 ? (N-1)*2*k+k : j*2*k-k, j*2*k+k)); - } - - for (unsigned int i=0; ivertex[j].x, facet_ptr->vertex[j].y, facet_ptr->vertex[j].z)); - - points.back().emplace_back(Pointf3(facet_ptr->normal.x, facet_ptr->normal.y, facet_ptr->normal.z)); - } - facet_ptr+=1; - } - m_gizmos.set_flattening_data(points); - //////////////////////////////////////////////////////////////////////// + m_gizmos.set_flattening_data(model_object); } } } @@ -2265,7 +2210,7 @@ void GLCanvas3D::update_gizmos_data() { m_gizmos.set_scale(1.0f); m_gizmos.set_angle_z(0.0f); - m_gizmos.set_flattening_data(std::vector()); + m_gizmos.set_flattening_data(nullptr); } } diff --git a/xs/src/slic3r/GUI/GLCanvas3D.hpp b/xs/src/slic3r/GUI/GLCanvas3D.hpp index 09a7de823..5f955cce2 100644 --- a/xs/src/slic3r/GUI/GLCanvas3D.hpp +++ b/xs/src/slic3r/GUI/GLCanvas3D.hpp @@ -383,7 +383,7 @@ public: float get_angle_z() const; void set_angle_z(float angle_z); - void set_flattening_data(std::vector vertices_list); + void set_flattening_data(const ModelObject* model_object); Pointf3 get_flattening_normal() const; void render(const GLCanvas3D& canvas, const BoundingBoxf3& box) const; diff --git a/xs/src/slic3r/GUI/GLGizmo.cpp b/xs/src/slic3r/GUI/GLGizmo.cpp index 5a69bc9b7..f0a8f2e71 100644 --- a/xs/src/slic3r/GUI/GLGizmo.cpp +++ b/xs/src/slic3r/GUI/GLGizmo.cpp @@ -2,6 +2,8 @@ #include "../../libslic3r/Utils.hpp" #include "../../libslic3r/BoundingBox.hpp" +#include "../../libslic3r/Model.hpp" +#include "../../libslic3r/Geometry.hpp" #include @@ -534,22 +536,19 @@ void GLGizmoFlatten::on_start_dragging() m_normal = m_planes[m_hover_id].normal; } -void GLGizmoFlatten::on_update(const Pointf& mouse_pos) -{ - /*Pointf center(0.5 * (m_grabbers[1].center.x + m_grabbers[0].center.x), 0.5 * (m_grabbers[3].center.y + m_grabbers[0].center.y)); - - coordf_t orig_len = length(m_starting_drag_position - center); - coordf_t new_len = length(mouse_pos - center); - coordf_t ratio = (orig_len != 0.0) ? new_len / orig_len : 1.0; - - m_scale = m_starting_scale * (float)ratio;*/ -} - void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const { + // the dragged_offset is a vector measuring where was the object moved + // with the gizmo being on. This is reset in set_flattening_data and + // does not work correctly when there are multiple copies. + if (!m_center) // this is the first bounding box that we see + m_center.reset(new Pointf3(box.center().x, box.center().y)); + Pointf3 dragged_offset = box.center() - *m_center; + bool blending_was_enabled = ::glIsEnabled(GL_BLEND); + bool depth_test_was_enabled = ::glIsEnabled(GL_DEPTH_TEST); ::glEnable(GL_BLEND); - ::glDisable(GL_DEPTH_TEST); + ::glEnable(GL_DEPTH_TEST); for (int i=0; i<(int)m_planes.size(); ++i) { if (i == m_hover_id) @@ -557,14 +556,19 @@ void GLGizmoFlatten::on_render(const BoundingBoxf3& box) const else ::glColor4f(0.9f, 0.9f, 0.9f, 0.5f); - ::glBegin(GL_POLYGON); - for (const auto& vertex : m_planes[i].vertices) - ::glVertex3f((GLfloat)vertex.x, (GLfloat)vertex.y, (GLfloat)vertex.z); - ::glEnd(); + for (Pointf offset : m_instances_positions) { + offset += dragged_offset; + ::glBegin(GL_POLYGON); + for (const auto& vertex : m_planes[i].vertices) + ::glVertex3f((GLfloat)vertex.x + offset.x, (GLfloat)vertex.y + offset.y, (GLfloat)vertex.z); + ::glEnd(); + } } if (!blending_was_enabled) ::glDisable(GL_BLEND); + if (!depth_test_was_enabled) + ::glDisable(GL_DEPTH_TEST); } void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const @@ -576,27 +580,220 @@ void GLGizmoFlatten::on_render_for_picking(const BoundingBoxf3& box) const for (unsigned int i = 0; i < m_planes.size(); ++i) { ::glColor3f(1.f, 1.f, (254.0f - (float)i) * INV_255); - ::glBegin(GL_POLYGON); - for (const auto& vertex : m_planes[i].vertices) - ::glVertex3f((GLfloat)vertex.x, (GLfloat)vertex.y, (GLfloat)vertex.z); - ::glEnd(); + for (const Pointf& offset : m_instances_positions) { + ::glBegin(GL_POLYGON); + for (const auto& vertex : m_planes[i].vertices) + ::glVertex3f((GLfloat)vertex.x + offset.x, (GLfloat)vertex.y + offset.y, (GLfloat)vertex.z); + ::glEnd(); + } } } -void GLGizmoFlatten::set_flattening_data(std::vector vertices_list) + +// TODO - remove and use Eigen instead +static Pointf3 super_rotation(const Pointf3& axis, float angle, const Pointf3& point) { - // Each entry in vertices_list describe one polygon that can be laid flat. - // All points but the last one are vertices of the polygon, the last "point" is the outer normal vector. + float axis_length = axis.distance_to(Pointf3(0.f, 0.f, 0.f)); + float x = axis.x / axis_length; + float y = axis.y / axis_length; + float z = axis.z / axis_length; + float s = sin(angle); + float c = cos(angle); + float D = 1-c; + float matrix[3][3] = { { c + x*x*D, x*y*D-z*s, x*z*D+y*s }, + { y*x*D+z*s, c+y*y*D, y*z*D-x*s }, + { z*x*D-y*s, z*y*D+x*s, c+z*z*D } }; + float in[3] = { (float)point.x, (float)point.y, (float)point.z }; + float out[3] = { 0, 0, 0 }; + + for (unsigned char i=0; i<3; ++i) + for (unsigned char j=0; j<3; ++j) + out[i] += matrix[i][j] * in[j]; + + return Pointf3(out[0], out[1], out[2]); +} + + +void GLGizmoFlatten::set_flattening_data(const ModelObject* model_object) +{ + m_center.release(); // object is not being dragged (this would not be called otherwise) - we must forget about the bounding box position... + m_model_object = model_object; + + // ...and save the updated positions of the object instances: + if (m_model_object && !m_model_object->instances.empty()) { + m_instances_positions.clear(); + for (const auto* instance : m_model_object->instances) + m_instances_positions.emplace_back(instance->offset); + } + + if (is_plane_update_necessary()) + update_planes(); +} + +void GLGizmoFlatten::update_planes() +{ + TriangleMesh ch; + for (const ModelVolume* vol : m_model_object->volumes) + ch.merge(vol->get_convex_hull()); + ch = ch.convex_hull_3d(); + ch.scale(m_model_object->instances.front()->scaling_factor); + ch.rotate_z(m_model_object->instances.front()->rotation); m_planes.clear(); - m_planes.reserve(vertices_list.size()); - for (const auto& plane_data : vertices_list) { - m_planes.emplace_back(PlaneData()); - for (unsigned int i=0; i facet_queue(num_of_facets, 0); + std::vector facet_visited(num_of_facets, false); + int facet_queue_cnt = 0; + const stl_normal* normal_ptr = nullptr; + while (1) { + // Find next unvisited triangle: + int facet_idx = 0; + 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 = &ch.stl.facet_start[facet_idx].normal; + 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_ptr = &ch.stl.facet_start[facet_idx].normal; + //if (this_normal_ptr->x == normal_ptr->x && this_normal_ptr->y == normal_ptr->y && this_normal_ptr->z == normal_ptr->z) { + if (std::abs(this_normal_ptr->x-normal_ptr->x) < 0.001 && std::abs(this_normal_ptr->y-normal_ptr->y) < 0.001 && std::abs(this_normal_ptr->z-normal_ptr->z) < 0.001) { + stl_vertex* first_vertex = ch.stl.facet_start[facet_idx].vertex; + for (int j=0; j<3; ++j) + m_planes.back().vertices.emplace_back(first_vertex[j].x, first_vertex[j].y, first_vertex[j].z); + + facet_visited[facet_idx] = true; + for (int j = 0; j < 3; ++ j) { + int neighbor_idx = ch.stl.neighbors_start[facet_idx].neighbor[j]; + if (! facet_visited[neighbor_idx]) + facet_queue[facet_queue_cnt ++] = neighbor_idx; + } + } + } + m_planes.back().normal = Pointf3(normal_ptr->x, normal_ptr->y, normal_ptr->z); } + + // Now we'll go through all the polygons, transform the points into xy plane to process them: + for (unsigned int polygon_id=0; polygon_id < m_planes.size(); ++polygon_id) { + Pointf3s& polygon = m_planes[polygon_id].vertices; + const Pointf3& normal = m_planes[polygon_id].normal; + + // We are going to rotate about z and y to flatten the plane + float angle_z = 0.f; + float angle_y = 0.f; + if (std::abs(normal.y) > 0.001) + angle_z = -atan2(normal.y, normal.x); // angle to rotate so that normal ends up in xz-plane + if (std::abs(normal.x*cos(angle_z)-normal.y*sin(angle_z)) > 0.001) + angle_y = - atan2(normal.x*cos(angle_z)-normal.y*sin(angle_z), normal.z); // angle to rotate to make normal point upwards + else { + // In case it already was in z-direction, we must ensure it is not the wrong way: + angle_y = normal.z > 0.f ? 0 : -M_PI; + } + + // Rotate all points to the xy plane: + for (auto& vertex : polygon) { + vertex = super_rotation(Pointf3(0,0,1), angle_z, vertex); + vertex = super_rotation(Pointf3(0,1,0), angle_y, vertex); + } + polygon = Slic3r::Geometry::convex_hull(polygon); // To remove the inner points + + // Calculate area of the polygon and discard ones that are too small + float area = 0.f; + for (unsigned int i = 0; i < polygon.size(); i++) // Shoelace formula + area += polygon[i].x*polygon[i+1 < polygon.size() ? i+1 : 0 ].y - polygon[i+1 < polygon.size() ? i+1 : 0].x*polygon[i].y; + area = std::abs(area/2.f); + if (area < 20.f) { + m_planes.erase(m_planes.begin()+(polygon_id--)); + continue; + } + + // We will shrink the polygon a little bit so it does not touch the object edges: + Pointf3 centroid = std::accumulate(polygon.begin(), polygon.end(), Pointf3(0.f, 0.f, 0.f)); + centroid.scale(1.f/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; jvolumes) + m_source_data.bounding_boxes.push_back(vol->get_convex_hull().bounding_box()); + m_source_data.scaling_factor = m_model_object->instances.front()->scaling_factor; + m_source_data.rotation = m_model_object->instances.front()->rotation; +} + +// Check if the bounding boxes of each volume's convex hull is the same as before +// and that scaling and rotation has not changed. In that case we don't have to recalculate it. +bool GLGizmoFlatten::is_plane_update_necessary() const +{ + if (m_state != On || !m_model_object || m_model_object->instances.empty()) + return false; + + if (m_model_object->volumes.size() != m_source_data.bounding_boxes.size() + || m_model_object->instances.front()->scaling_factor != m_source_data.scaling_factor + || m_model_object->instances.front()->rotation != m_source_data.rotation) + return true; + + // now compare the bounding boxes: + for (unsigned int i=0; ivolumes.size(); ++i) + if (m_model_object->volumes[i]->get_convex_hull().bounding_box() != m_source_data.bounding_boxes[i]) + return true; + + return false; } Pointf3 GLGizmoFlatten::get_flattening_normal() const { @@ -607,7 +804,5 @@ Pointf3 GLGizmoFlatten::get_flattening_normal() const { - - } // namespace GUI } // namespace Slic3r diff --git a/xs/src/slic3r/GUI/GLGizmo.hpp b/xs/src/slic3r/GUI/GLGizmo.hpp index d1ef6452a..0cab603eb 100644 --- a/xs/src/slic3r/GUI/GLGizmo.hpp +++ b/xs/src/slic3r/GUI/GLGizmo.hpp @@ -10,6 +10,7 @@ namespace Slic3r { class BoundingBoxf3; class Pointf3; +class ModelObject; namespace GUI { @@ -160,23 +161,39 @@ private: struct PlaneData { std::vector vertices; Pointf3 normal; - float color[3]; + }; + struct SourceDataSummary { + std::vector bounding_boxes; // bounding boxes of convex hulls of individual volumes + float scaling_factor; + float rotation; }; + // This holds information to decide whether recalculation is necessary: + SourceDataSummary m_source_data; + std::vector m_planes; + std::vector m_instances_positions; + mutable std::unique_ptr m_center = nullptr; + const ModelObject* m_model_object = nullptr; + void update_planes(); + bool is_plane_update_necessary() const; public: GLGizmoFlatten(); - void set_flattening_data(std::vector vertices_list); + void set_flattening_data(const ModelObject* model_object); Pointf3 get_flattening_normal() const; protected: - virtual bool on_init(); - virtual void on_start_dragging(); - virtual void on_update(const Pointf& mouse_pos); - virtual void on_render(const BoundingBoxf3& box) const; - virtual void on_render_for_picking(const BoundingBoxf3& box) const; + bool on_init() override; + void on_start_dragging() override; + void on_update(const Pointf& mouse_pos) override {}; + void on_render(const BoundingBoxf3& box) const override; + void on_render_for_picking(const BoundingBoxf3& box) const override; + void on_set_state() override { + if (m_state == On && is_plane_update_necessary()) + update_planes(); + } };