From 6f4d24ab957f66ab9c74e329385b12bbfccf9017 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 4 Jun 2020 13:49:49 +0200 Subject: [PATCH 1/5] WIP: Generating offset curves with properly rounded corners from a Voronoi diagram. Curve extraction is based on the OpenVoronoi implementation. --- src/libslic3r/CMakeLists.txt | 2 + src/libslic3r/Geometry.hpp | 20 +- src/libslic3r/VoronoiOffset.cpp | 376 +++++++++++++++++++++++++++++++ src/libslic3r/VoronoiOffset.hpp | 14 ++ tests/libslic3r/test_voronoi.cpp | 41 +++- 5 files changed, 437 insertions(+), 16 deletions(-) create mode 100644 src/libslic3r/VoronoiOffset.cpp create mode 100644 src/libslic3r/VoronoiOffset.hpp diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index 26f2190e5..1a58bdbbd 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -188,6 +188,8 @@ add_library(libslic3r STATIC Time.cpp Time.hpp MTUtils.hpp + VoronoiOffset.cpp + VoronoiOffset.hpp Zipper.hpp Zipper.cpp MinAreaBoundingBox.hpp diff --git a/src/libslic3r/Geometry.hpp b/src/libslic3r/Geometry.hpp index 40c622f04..87fb0c9c7 100644 --- a/src/libslic3r/Geometry.hpp +++ b/src/libslic3r/Geometry.hpp @@ -252,8 +252,16 @@ bool arrange( // output Pointfs &positions); +class VoronoiDiagram : public boost::polygon::voronoi_diagram { +public: + typedef double coord_type; + typedef boost::polygon::point_data point_type; + typedef boost::polygon::segment_data segment_type; + typedef boost::polygon::rectangle_data rect_type; +}; + class MedialAxis { - public: +public: Lines lines; const ExPolygon* expolygon; double max_width; @@ -263,14 +271,8 @@ class MedialAxis { void build(ThickPolylines* polylines); void build(Polylines* polylines); - private: - class VD : public boost::polygon::voronoi_diagram { - public: - typedef double coord_type; - typedef boost::polygon::point_data point_type; - typedef boost::polygon::segment_data segment_type; - typedef boost::polygon::rectangle_data rect_type; - }; +private: + using VD = VoronoiDiagram; VD vd; std::set edges, valid_edges; std::map > thickness; diff --git a/src/libslic3r/VoronoiOffset.cpp b/src/libslic3r/VoronoiOffset.cpp new file mode 100644 index 000000000..acf693305 --- /dev/null +++ b/src/libslic3r/VoronoiOffset.cpp @@ -0,0 +1,376 @@ +// Polygon offsetting code inspired by OpenVoronoi by Anders Wallin +// https://github.com/aewallin/openvoronoi +// This offsetter uses results of boost::polygon Voronoi. + +#include "VoronoiOffset.hpp" + +#include + +namespace Slic3r { + +using VD = Geometry::VoronoiDiagram; + +namespace detail { + // Intersect a circle with a ray, return the two parameters + double first_circle_segment_intersection_parameter( + const Vec2d ¢er, const double r, const Vec2d &pt, const Vec2d &v) + { + const Vec2d d = pt - center; +#ifndef NDEBUG + double d0 = (pt - center).norm(); + double d1 = (pt + v - center).norm(); + assert(r < std::max(d0, d1) + EPSILON); +#endif /* NDEBUG */ + const double a = v.squaredNorm(); + const double b = 2. * d.dot(v); + const double c = d.squaredNorm() - r * r; + std::pair> out; + double u = b * b - 4. * a * c; + assert(u > - EPSILON); + double t; + if (u <= 0) { + // Degenerate to a single closest point. + t = - b / (2. * a); + assert(t >= - EPSILON && t <= 1. + EPSILON); + return Slic3r::clamp(0., 1., t); + } else { + u = sqrt(u); + out.first = 2; + double t0 = (- b - u) / (2. * a); + double t1 = (- b + u) / (2. * a); + // One of the intersections shall be found inside the segment. + assert((t0 >= - EPSILON && t0 <= 1. + EPSILON) || (t1 >= - EPSILON && t1 <= 1. + EPSILON)); + if (t1 < 0.) + return 0.; + if (t0 > 1.) + return 1.; + return (t0 > 0.) ? t0 : t1; + } + } + + Vec2d voronoi_edge_offset_point( + const VD &vd, + const Lines &lines, + // Distance of a VD vertex to the closest site (input polygon edge or vertex). + const std::vector &vertex_dist, + // Minium distance of a VD edge to the closest site (input polygon edge or vertex). + // For a parabolic segment the distance may be smaller than the distance of the two end points. + const std::vector &edge_dist, + // Edge for which to calculate the offset point. If the distance towards the input polygon + // is not monotonical, pick the offset point closer to edge.vertex0(). + const VD::edge_type &edge, + // Distance from the input polygon along the edge. + const double offset_distance) + { + const VD::vertex_type *v0 = edge.vertex0(); + const VD::vertex_type *v1 = edge.vertex1(); + const VD::cell_type *cell = edge.cell(); + const VD::cell_type *cell2 = edge.twin()->cell(); + const Line &line0 = lines[cell->source_index()]; + const Line &line1 = lines[cell2->source_index()]; + if (v0 == nullptr || v1 == nullptr) { + assert(edge.is_infinite()); + assert(v0 != nullptr || v1 != nullptr); + // Offsetting on an unconstrained edge. + assert(offset_distance > vertex_dist[(v0 ? v0 : v1) - &vd.vertices().front()] - EPSILON); + Vec2d pt, dir; + double t; + if (cell->contains_point() && cell2->contains_point()) { + const Point &pt0 = (cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b; + const Point &pt1 = (cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b; + // Direction vector of this unconstrained Voronoi edge. + dir = Vec2d(double(pt0.y() - pt1.y()), double(pt1.x() - pt0.x())); + if (v0 == nullptr) { + v0 = v1; + dir = - dir; + } + pt = Vec2d(v0->x(), v0->y()); + t = detail::first_circle_segment_intersection_parameter(Vec2d(pt0.x(), pt0.y()), offset_distance, pt, dir); + } else { + // Infinite edges could not be created by two segment sites. + assert(cell->contains_point() != cell2->contains_point()); + // Linear edge goes through the endpoint of a segment. + assert(edge.is_linear()); + assert(edge.is_secondary()); + const Line &line = cell->contains_segment() ? line0 : line1; + const Point &ipt = cell->contains_segment() ? + ((cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b) : + ((cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b); + assert(line.a == ipt || line.b == ipt); + pt = Vec2d(ipt.x(), ipt.y()); + dir = Vec2d(line.a.y() - line.b.y(), line.b.x() - line.a.x()); + assert(dir.norm() > 0.); + t = offset_distance / dir.norm(); + if (((line.a == ipt) == cell->contains_point()) == (v0 == nullptr)) + t = - t; + } + return pt + t * dir; + } else { + // Constrained edge. + Vec2d p0(v0->x(), v0->y()); + Vec2d p1(v1->x(), v1->y()); + double d0 = vertex_dist[v0 - &vd.vertices().front()]; + double d1 = vertex_dist[v1 - &vd.vertices().front()]; + if (cell->contains_segment() && cell2->contains_segment()) { + // This edge is a bisector of two line segments. Distance to the input polygon increases/decreases monotonically. + double ddif = d1 - d0; + assert(offset_distance > std::min(d0, d1) - EPSILON && offset_distance < std::max(d0, d1) + EPSILON); + double t = (ddif == 0) ? 0. : clamp(0., 1., (offset_distance - d0) / ddif); + return Slic3r::lerp(p0, p1, t); + } else { + // One cell contains a point, the other contains an edge or a point. + assert(cell->contains_point() || cell2->contains_point()); + const Point &ipt = cell->contains_point() ? + ((cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b) : + ((cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b); + double t = detail::first_circle_segment_intersection_parameter( + Vec2d(ipt.x(), ipt.y()), offset_distance, p0, p1 - p0); + return Slic3r::lerp(p0, p1, t); + } + } + } +}; + +Polygons voronoi_offset(const VD &vd, const Lines &lines, double offset_distance, double discretization_error) +{ + // Distance of a VD vertex to the closest site (input polygon edge or vertex). + std::vector vertex_dist(vd.num_vertices(), std::numeric_limits::max()); + + // Minium distance of a VD edge to the closest site (input polygon edge or vertex). + // For a parabolic segment the distance may be smaller than the distance of the two end points. + std::vector edge_dist(vd.num_edges(), std::numeric_limits::max()); + + // Calculate minimum distance of input polygons to voronoi vertices and voronoi edges. + for (const VD::edge_type &edge : vd.edges()) { + const VD::vertex_type *v0 = edge.vertex0(); + const VD::vertex_type *v1 = edge.vertex1(); + const VD::cell_type *cell = edge.cell(); + const VD::cell_type *cell2 = edge.twin()->cell(); + const Line &line0 = lines[cell->source_index()]; + const Line &line1 = lines[cell2->source_index()]; + double d0, d1, dmin; + if (v0 == nullptr || v1 == nullptr) { + assert(edge.is_infinite()); + if (cell->contains_point() && cell2->contains_point()) { + const Point &pt0 = (cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b; + const Point &pt1 = (cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b; + d0 = d1 = std::numeric_limits::max(); + if (v0 == nullptr && v1 == nullptr) { + dmin = (pt1.cast() - pt0.cast()).norm(); + } else { + Vec2d pt((pt0 + pt1).cast() * 0.5); + Vec2d dir(double(pt0.y() - pt1.y()), double(pt1.x() - pt0.x())); + Vec2d pt0d(pt0.x(), pt0.y()); + if (v0) { + Vec2d a(v0->x(), v0->y()); + d0 = (a - pt0d).norm(); + dmin = ((a - pt).dot(dir) < 0.) ? (a - pt0d).norm() : d0; + vertex_dist[v0 - &vd.vertices().front()] = d0; + } else { + Vec2d a(v1->x(), v1->y()); + d1 = (a - pt0d).norm(); + dmin = ((a - pt).dot(dir) < 0.) ? (a - pt0d).norm() : d1; + vertex_dist[v1 - &vd.vertices().front()] = d1; + } + } + } else { + // Infinite edges could not be created by two segment sites. + assert(cell->contains_point() != cell2->contains_point()); + // Linear edge goes through the endpoint of a segment. + assert(edge.is_linear()); + assert(edge.is_secondary()); +#ifndef NDEBUG + if (cell->contains_segment()) { + const Point &pt1 = (cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b; + assert((pt1.x() == line0.a.x() && pt1.y() == line0.a.y()) || + (pt1.x() == line0.b.x() && pt1.y() == line0.b.y())); + } else { + const Point &pt0 = (cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b; + assert((pt0.x() == line1.a.x() && pt0.y() == line1.a.y()) || + (pt0.x() == line1.b.x() && pt0.y() == line1.b.y())); + } + const Point &pt = cell->contains_segment() ? + ((cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b) : + ((cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b); +#endif /* NDEBUG */ + if (v0) { + assert((Point(v0->x(), v0->y()) - pt).cast().norm() < SCALED_EPSILON); + d0 = dmin = 0.; + vertex_dist[v0 - &vd.vertices().front()] = d0; + } else { + assert((Point(v1->x(), v1->y()) - pt).cast().norm() < SCALED_EPSILON); + d1 = dmin = 0.; + vertex_dist[v1 - &vd.vertices().front()] = d1; + } + } + } else { + // Finite edge has valid points at both sides. + if (cell->contains_segment() && cell2->contains_segment()) { + // This edge is a bisector of two line segments. + d0 = std::hypot(v0->x() - line0.a.x(), v0->y() - line0.a.y()); + d1 = std::hypot(v0->x() - line0.b.x(), v0->y() - line0.b.y()); + if (d0 < d1) + d1 = std::hypot(v1->x() - line0.a.x(), v1->y() - line0.a.y()); + else { + d0 = d1; + d1 = std::hypot(v1->x() - line0.b.x(), v1->y() - line0.b.y()); + } + dmin = std::min(d0, d1); + } else { + assert(cell->contains_point() || cell2->contains_point()); + const Point &pt0 = cell->contains_point() ? + ((cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b) : + ((cell2->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line1.a : line1.b); + // Project p0 to line segment . + Vec2d p0(v0->x(), v0->y()); + Vec2d p1(v1->x(), v1->y()); + Vec2d px(pt0.x(), pt0.y()); + Vec2d v = p1 - p0; + d0 = (p0 - px).norm(); + d1 = (p1 - px).norm(); + double t = v.dot(px - p0); + double l2 = v.squaredNorm(); + if (t > 0. && t < l2) { + // Foot point on the line segment. + Vec2d foot = p0 + (t / l2) * v; + dmin = (foot - px).norm(); + } else + dmin = std::min(d0, d1); + } + vertex_dist[v0 - &vd.vertices().front()] = d0; + vertex_dist[v1 - &vd.vertices().front()] = d1; + } + edge_dist[&edge - &vd.edges().front()] = dmin; + } + + // Mark cells intersected by the offset curve. + std::vector seed_cells(vd.num_cells(), false); + for (const VD::cell_type &cell : vd.cells()) { + const VD::edge_type *first_edge = cell.incident_edge(); + const VD::edge_type *edge = first_edge; + do { + double dmin = edge_dist[edge - &vd.edges().front()]; + double dmax = std::numeric_limits::max(); + const VD::vertex_type *v0 = edge->vertex0(); + const VD::vertex_type *v1 = edge->vertex1(); + if (v0 != nullptr) + dmax = vertex_dist[v0 - &vd.vertices().front()]; + if (v1 != nullptr) + dmax = std::max(dmax, vertex_dist[v1 - &vd.vertices().front()]); + if (offset_distance >= dmin && offset_distance <= dmax) { + // This cell is being intersected by the offset curve. + seed_cells[&cell - &vd.cells().front()] = true; + break; + } + edge = edge->next(); + } while (edge != first_edge); + } + + auto edge_dir = [&vd, &vertex_dist, &edge_dist, offset_distance](const VD::edge_type *edge) { + const VD::vertex_type *v0 = edge->vertex0(); + const VD::vertex_type *v1 = edge->vertex1(); + double d0 = v0 ? vertex_dist[v0 - &vd.vertices().front()] : std::numeric_limits::max(); + double d1 = v1 ? vertex_dist[v1 - &vd.vertices().front()] : std::numeric_limits::max(); + if (d0 < offset_distance && offset_distance < d1) + return true; + else if (d1 < offset_distance && offset_distance < d0) + return false; + else { + assert(false); + return false; + } + }; + + /// \brief starting at e, find the next edge on the face that brackets t + /// + /// we can be in one of two modes. + /// if direction==false then we are looking for an edge where src_t < t < trg_t + /// if direction==true we are looning for an edge where trg_t < t < src_t + auto next_offset_edge = + [&vd, &vertex_dist, &edge_dist, offset_distance] + (const VD::edge_type *start_edge, bool direction) -> const VD::edge_type* { + const VD::edge_type *edge = start_edge; + do { + const VD::vertex_type *v0 = edge->vertex0(); + const VD::vertex_type *v1 = edge->vertex1(); + double d0 = v0 ? vertex_dist[v0 - &vd.vertices().front()] : std::numeric_limits::max(); + double d1 = v1 ? vertex_dist[v1 - &vd.vertices().front()] : std::numeric_limits::max(); + if (direction ? (d1 < offset_distance && offset_distance < d0) : (d0 < offset_distance && offset_distance < d1)) + return edge; + edge = edge->next(); + } while (edge != start_edge); + assert(false); + return nullptr; + }; + + // Track the offset curves. + Polygons out; + double angle_step = 2. * acos((offset_distance - discretization_error) / offset_distance); + double sin_threshold = sin(angle_step) + EPSILON; + for (size_t seed_cell_idx = 0; seed_cell_idx < vd.num_cells(); ++ seed_cell_idx) + if (seed_cells[seed_cell_idx]) { + seed_cells[seed_cell_idx] = false; + // Initial direction should not matter, an offset curve shall intersect a cell at least at two points + // (if it is not just touching the cell at a single vertex), and such two intersection points shall have + // opposite direction. + bool direction = false; + // the first edge on the start-face + const VD::cell_type &cell = vd.cells()[seed_cell_idx]; + const VD::edge_type *start_edge = next_offset_edge(cell.incident_edge(), direction); + assert(start_edge->cell() == &cell); + const VD::edge_type *edge = start_edge; + Polygon poly; + do { + direction = edge_dir(edge); + // find the next edge + const VD::edge_type *next_edge = next_offset_edge(edge->next(), direction); + //std::cout << "offset-output: "; print_edge(edge); std::cout << " to "; print_edge(next_edge); std::cout << "\n"; + // Interpolate a circular segment or insert a linear segment between edge and next_edge. + const VD::cell_type *cell = edge->cell(); + Vec2d p1 = detail::voronoi_edge_offset_point(vd, lines, vertex_dist, edge_dist, *edge, offset_distance); + Vec2d p2 = detail::voronoi_edge_offset_point(vd, lines, vertex_dist, edge_dist, *next_edge, offset_distance); + if (cell->contains_point()) { + // Discretize an arc from p1 to p2 with radius = offset_distance and discretization_error. + // The arc should cover angle < PI. + //FIXME we should be able to produce correctly oriented output curves based on the first edge taken! + const Line &line0 = lines[cell->source_index()]; + const Vec2d ¢er = ((cell->source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line0.a : line0.b).cast(); + const Vec2d v1 = p1 - center; + const Vec2d v2 = p2 - center; + double orient = cross2(v1, v2); + double orient_norm = v1.norm() * v2.norm(); + bool ccw = orient > 0; + bool obtuse = v1.dot(v2) < 0.; + if (! ccw) + orient = - orient; + assert(orient != 0.); + if (obtuse || orient > orient_norm * sin_threshold) { + // Angle is bigger than the threshold, therefore the arc will be discretized. + double angle = asin(orient / orient_norm); + if (obtuse) + angle = M_PI - angle; + size_t n_steps = size_t(ceil(angle / angle_step)); + double astep = angle / n_steps; + if (! ccw) + astep *= -1.; + double a = astep; + for (size_t i = 1; i < n_steps; ++ i, a += astep) { + double c = cos(a); + double s = sin(a); + Vec2d p = center + Vec2d(c * v1.x() - s * v1.y(), s * v1.x() + c * v1.y()); + poly.points.emplace_back(Point(coord_t(p.x()), coord_t(p.y()))); + } + } + } + poly.points.emplace_back(Point(coord_t(p2.x()), coord_t(p2.y()))); + // although we may revisit current_face (if it is non-convex), it seems safe to mark it "done" here. + seed_cells[cell - &vd.cells().front()] = false; + edge = next_edge->twin(); + } while (edge != start_edge); + out.emplace_back(std::move(poly)); + } + + return out; +} + +} // namespace Slic3r diff --git a/src/libslic3r/VoronoiOffset.hpp b/src/libslic3r/VoronoiOffset.hpp new file mode 100644 index 000000000..9f5485c00 --- /dev/null +++ b/src/libslic3r/VoronoiOffset.hpp @@ -0,0 +1,14 @@ +#ifndef slic3r_VoronoiOffset_hpp_ +#define slic3r_VoronoiOffset_hpp_ + +#include "libslic3r.h" + +#include "Geometry.hpp" + +namespace Slic3r { + +Polygons voronoi_offset(const Geometry::VoronoiDiagram &vd, const Lines &lines, double offset_distance, double discretization_error); + +} // namespace Slic3r + +#endif // slic3r_VoronoiOffset_hpp_ diff --git a/tests/libslic3r/test_voronoi.cpp b/tests/libslic3r/test_voronoi.cpp index 4615a93ae..ef05119ad 100644 --- a/tests/libslic3r/test_voronoi.cpp +++ b/tests/libslic3r/test_voronoi.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #define BOOST_VORONOI_USE_GMP 1 #include "boost/polygon/voronoi.hpp" @@ -16,12 +17,7 @@ using boost::polygon::voronoi_diagram; using namespace Slic3r; -struct VD : public boost::polygon::voronoi_diagram { - typedef double coord_type; - typedef boost::polygon::point_data point_type; - typedef boost::polygon::segment_data segment_type; - typedef boost::polygon::rectangle_data rect_type; -}; +using VD = Geometry::VoronoiDiagram; // #define VORONOI_DEBUG_OUT @@ -322,6 +318,7 @@ static inline void dump_voronoi_to_svg( /* const */ VD &vd, const Points &points, const Lines &lines, + const Polygons &offset_curves = Polygons(), const double scale = 0.7) // 0.2? { const std::string inputSegmentPointColor = "lightseagreen"; @@ -336,6 +333,9 @@ static inline void dump_voronoi_to_svg( const std::string voronoiArcColor = "red"; const coord_t voronoiLineWidth = coord_t(0.02 * scale / SCALING_FACTOR); + const std::string offsetCurveColor = "magenta"; + const coord_t offsetCurveLineWidth = coord_t(0.09 * scale / SCALING_FACTOR); + const bool internalEdgesOnly = false; const bool primaryEdgesOnly = false; @@ -408,6 +408,7 @@ static inline void dump_voronoi_to_svg( } #endif + svg.draw_outline(offset_curves, offsetCurveColor, offsetCurveLineWidth); svg.Close(); } #endif @@ -1585,6 +1586,32 @@ TEST_CASE("Voronoi NaN coordinates 12139", "[Voronoi][!hide][!mayfail]") #ifdef VORONOI_DEBUG_OUT dump_voronoi_to_svg(debug_out_path("voronoi-NaNs.svg").c_str(), - vd, Points(), lines, 0.015); + vd, Points(), lines, Polygons(), 0.015); #endif } + +TEST_CASE("Voronoi offset", "[VoronoiOffset]") +{ + Polygons poly_with_hole = { Polygon { + { 0, 10000000}, + { 700000, 0}, + { 700000, 9000000}, + { 9100000, 9000000}, + { 9100000, 0}, + {10000000, 10000000} + } + }; + + VD vd; + Lines lines = to_lines(poly_with_hole); + construct_voronoi(lines.begin(), lines.end(), &vd); + + Polygons offsetted_polygons = voronoi_offset(vd, lines, scale_(0.2), scale_(0.005)); + +#ifdef VORONOI_DEBUG_OUT + dump_voronoi_to_svg(debug_out_path("voronoi-offset.svg").c_str(), + vd, Points(), lines, offsetted_polygons); +#endif + + REQUIRE(offsetted_polygons.size() == 2); +} From ad7e7ae1cd65e1df26bb156054e4e230d8f5a587 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Thu, 4 Jun 2020 14:48:52 +0200 Subject: [PATCH 2/5] Added tech ENABLE_OPENGL_ERROR_LOGGING -> log opengl errors when SLIC3R_LOGLEVEL=5 --- src/libslic3r/Technologies.hpp | 3 +++ src/slic3r/GUI/3DScene.cpp | 27 +++++++++++++++++++++++++++ src/slic3r/GUI/3DScene.hpp | 7 +++++++ 3 files changed, 37 insertions(+) diff --git a/src/libslic3r/Technologies.hpp b/src/libslic3r/Technologies.hpp index 0c5720c6b..e0d534e00 100644 --- a/src/libslic3r/Technologies.hpp +++ b/src/libslic3r/Technologies.hpp @@ -48,5 +48,8 @@ // Enable smoothing of objects normals #define ENABLE_SMOOTH_NORMALS (0 && ENABLE_2_3_0_ALPHA1) +// Enable error logging for OpenGL calls when SLIC3R_LOGLEVEL >= 5 +#define ENABLE_OPENGL_ERROR_LOGGING (1 && ENABLE_2_3_0_ALPHA1) + #endif // _prusaslicer_technologies_h_ diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index dc57500c4..4b3864552 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -39,6 +39,32 @@ #include +#if ENABLE_OPENGL_ERROR_LOGGING +void glAssertRecentCallImpl(const char* file_name, unsigned int line, const char* function_name) +{ +#if NDEBUG + if (Slic3r::get_logging_level() < 5) + return; +#endif // NDEBUG + + GLenum err = glGetError(); + if (err == GL_NO_ERROR) + return; + const char* sErr = 0; + switch (err) { + case GL_INVALID_ENUM: sErr = "Invalid Enum"; break; + case GL_INVALID_VALUE: sErr = "Invalid Value"; break; + // be aware that GL_INVALID_OPERATION is generated if glGetError is executed between the execution of glBegin and the corresponding execution of glEnd + case GL_INVALID_OPERATION: sErr = "Invalid Operation"; break; + case GL_STACK_OVERFLOW: sErr = "Stack Overflow"; break; + case GL_STACK_UNDERFLOW: sErr = "Stack Underflow"; break; + case GL_OUT_OF_MEMORY: sErr = "Out Of Memory"; break; + default: sErr = "Unknown"; break; + } + BOOST_LOG_TRIVIAL(error) << "OpenGL error in " << file_name << ":" << line << ", function " << function_name << "() : " << (int)err << " - " << sErr; + assert(false); +} +#else #ifdef HAS_GLSAFE void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char *function_name) { @@ -60,6 +86,7 @@ void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char assert(false); } #endif +#endif // ENABLE_OPENGL_ERROR_LOGGING namespace Slic3r { diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index da111d7aa..410a3fbcd 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -10,6 +10,12 @@ #include +#if ENABLE_OPENGL_ERROR_LOGGING +extern void glAssertRecentCallImpl(const char* file_name, unsigned int line, const char* function_name); +inline void glAssertRecentCall() { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } +#define glsafe(cmd) do { cmd; glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) +#define glcheck() do { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) +#else #ifndef NDEBUG #define HAS_GLSAFE #endif @@ -24,6 +30,7 @@ inline void glAssertRecentCall() { } #define glsafe(cmd) cmd #define glcheck() #endif +#endif // ENABLE_OPENGL_ERROR_LOGGING namespace Slic3r { namespace GUI { From 3e0e537b7ae5fdcc7d9b2c854b70b2285e56c157 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 4 Jun 2020 15:34:23 +0200 Subject: [PATCH 3/5] Offset curve from a Voronoi diagram: Fixed distance calculation to a bisector of two segments. --- src/libslic3r/VoronoiOffset.cpp | 37 ++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/libslic3r/VoronoiOffset.cpp b/src/libslic3r/VoronoiOffset.cpp index acf693305..cd96e3cdc 100644 --- a/src/libslic3r/VoronoiOffset.cpp +++ b/src/libslic3r/VoronoiOffset.cpp @@ -206,15 +206,15 @@ Polygons voronoi_offset(const VD &vd, const Lines &lines, double offset_distance } else { // Finite edge has valid points at both sides. if (cell->contains_segment() && cell2->contains_segment()) { - // This edge is a bisector of two line segments. - d0 = std::hypot(v0->x() - line0.a.x(), v0->y() - line0.a.y()); - d1 = std::hypot(v0->x() - line0.b.x(), v0->y() - line0.b.y()); - if (d0 < d1) - d1 = std::hypot(v1->x() - line0.a.x(), v1->y() - line0.a.y()); - else { - d0 = d1; - d1 = std::hypot(v1->x() - line0.b.x(), v1->y() - line0.b.y()); - } + // This edge is a bisector of two line segments. Project v0, v1 onto one of the line segments. + Vec2d pt(line0.a.cast()); + Vec2d dir(line0.b.cast() - pt); + Vec2d vec0 = Vec2d(v0->x(), v0->y()) - pt; + Vec2d vec1 = Vec2d(v1->x(), v1->y()) - pt; + double l2 = dir.squaredNorm(); + assert(l2 > 0.); + d0 = (dir * (vec0.dot(dir) / l2) - vec0).norm(); + d1 = (dir * (vec1.dot(dir) / l2) - vec1).norm(); dmin = std::min(d0, d1); } else { assert(cell->contains_point() || cell2->contains_point()); @@ -303,6 +303,15 @@ Polygons voronoi_offset(const VD &vd, const Lines &lines, double offset_distance return nullptr; }; +#ifndef NDEBUG + auto dist_to_site = [&lines](const VD::cell_type &cell, const Vec2d &point) { + const Line &line = lines[cell.source_index()]; + return cell.contains_point() ? + (((cell.source_category() == boost::polygon::SOURCE_CATEGORY_SEGMENT_START_POINT) ? line.a : line.b).cast() - point).norm() : + line.distance_to(point.cast()); + }; +#endif /* NDEBUG */ + // Track the offset curves. Polygons out; double angle_step = 2. * acos((offset_distance - discretization_error) / offset_distance); @@ -329,7 +338,15 @@ Polygons voronoi_offset(const VD &vd, const Lines &lines, double offset_distance const VD::cell_type *cell = edge->cell(); Vec2d p1 = detail::voronoi_edge_offset_point(vd, lines, vertex_dist, edge_dist, *edge, offset_distance); Vec2d p2 = detail::voronoi_edge_offset_point(vd, lines, vertex_dist, edge_dist, *next_edge, offset_distance); - if (cell->contains_point()) { +#ifndef NDEBUG + { + double err = dist_to_site(*cell, p1) - offset_distance; + assert(std::abs(err) < SCALED_EPSILON); + err = dist_to_site(*cell, p2) - offset_distance; + assert(std::abs(err) < SCALED_EPSILON); + } +#endif /* NDEBUG */ + if (cell->contains_point()) { // Discretize an arc from p1 to p2 with radius = offset_distance and discretization_error. // The arc should cover angle < PI. //FIXME we should be able to produce correctly oriented output curves based on the first edge taken! From 1e3290fee1979b4e13fd6429c1dd205893f2a6ed Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Thu, 4 Jun 2020 15:53:58 +0200 Subject: [PATCH 4/5] Reduced ugly copy / paste for ENABLE_OPENGL_ERROR_LOGGING --- src/slic3r/GUI/3DScene.cpp | 32 ++++++-------------------------- src/slic3r/GUI/3DScene.hpp | 29 +++++++++++------------------ 2 files changed, 17 insertions(+), 44 deletions(-) diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index 4b3864552..5dc0d430f 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -39,13 +39,15 @@ #include -#if ENABLE_OPENGL_ERROR_LOGGING +#ifdef HAS_GLSAFE void glAssertRecentCallImpl(const char* file_name, unsigned int line, const char* function_name) { -#if NDEBUG +#if defined(NDEBUG) && ENABLE_OPENGL_ERROR_LOGGING + // In release mode, if OpenGL debugging was forced by ENABLE_OPENGL_ERROR_LOGGING, only show + // OpenGL errors if sufficiently high loglevel. if (Slic3r::get_logging_level() < 5) return; -#endif // NDEBUG +#endif // ENABLE_OPENGL_ERROR_LOGGING GLenum err = glGetError(); if (err == GL_NO_ERROR) @@ -64,29 +66,7 @@ void glAssertRecentCallImpl(const char* file_name, unsigned int line, const char BOOST_LOG_TRIVIAL(error) << "OpenGL error in " << file_name << ":" << line << ", function " << function_name << "() : " << (int)err << " - " << sErr; assert(false); } -#else -#ifdef HAS_GLSAFE -void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char *function_name) -{ - GLenum err = glGetError(); - if (err == GL_NO_ERROR) - return; - const char *sErr = 0; - switch (err) { - case GL_INVALID_ENUM: sErr = "Invalid Enum"; break; - case GL_INVALID_VALUE: sErr = "Invalid Value"; break; - // be aware that GL_INVALID_OPERATION is generated if glGetError is executed between the execution of glBegin and the corresponding execution of glEnd - case GL_INVALID_OPERATION: sErr = "Invalid Operation"; break; - case GL_STACK_OVERFLOW: sErr = "Stack Overflow"; break; - case GL_STACK_UNDERFLOW: sErr = "Stack Underflow"; break; - case GL_OUT_OF_MEMORY: sErr = "Out Of Memory"; break; - default: sErr = "Unknown"; break; - } - BOOST_LOG_TRIVIAL(error) << "OpenGL error in " << file_name << ":" << line << ", function " << function_name << "() : " << (int)err << " - " << sErr; - assert(false); -} -#endif -#endif // ENABLE_OPENGL_ERROR_LOGGING +#endif // HAS_GLSAFE namespace Slic3r { diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 410a3fbcd..f935f0fb4 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -10,27 +10,20 @@ #include -#if ENABLE_OPENGL_ERROR_LOGGING -extern void glAssertRecentCallImpl(const char* file_name, unsigned int line, const char* function_name); -inline void glAssertRecentCall() { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } -#define glsafe(cmd) do { cmd; glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) -#define glcheck() do { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) -#else -#ifndef NDEBUG -#define HAS_GLSAFE +#if ENABLE_OPENGL_ERROR_LOGGING || ! defined(NDEBUG) + #define HAS_GLSAFE #endif #ifdef HAS_GLSAFE -extern void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char *function_name); -inline void glAssertRecentCall() { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } -#define glsafe(cmd) do { cmd; glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) -#define glcheck() do { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) -#else -inline void glAssertRecentCall() { } -#define glsafe(cmd) cmd -#define glcheck() -#endif -#endif // ENABLE_OPENGL_ERROR_LOGGING + extern void glAssertRecentCallImpl(const char *file_name, unsigned int line, const char *function_name); + inline void glAssertRecentCall() { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } + #define glsafe(cmd) do { cmd; glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) + #define glcheck() do { glAssertRecentCallImpl(__FILE__, __LINE__, __FUNCTION__); } while (false) +#else // HAS_GLSAFE + inline void glAssertRecentCall() { } + #define glsafe(cmd) cmd + #define glcheck() +#endif // HAS_GLSAFE namespace Slic3r { namespace GUI { From 4f63095d9af21fefd2a287d103dea91d29e056e7 Mon Sep 17 00:00:00 2001 From: enricoturri1966 Date: Fri, 5 Jun 2020 12:33:09 +0200 Subject: [PATCH 5/5] Collapse toolbar moved from GLCanvas3D to Plater::priv --- src/slic3r/GUI/GLCanvas3D.cpp | 98 +++++++++------------------------- src/slic3r/GUI/GLCanvas3D.hpp | 2 - src/slic3r/GUI/GLToolbar.cpp | 4 +- src/slic3r/GUI/GUI_Preview.cpp | 2 - src/slic3r/GUI/Plater.cpp | 65 ++++++++++++++++++++++ src/slic3r/GUI/Plater.hpp | 4 ++ 6 files changed, 96 insertions(+), 79 deletions(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 9c4a8d2b3..14617d9f4 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -1534,9 +1534,8 @@ GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas) , m_retina_helper(nullptr) #endif , m_in_render(false) - , m_main_toolbar(GLToolbar::Normal, "Top") - , m_undoredo_toolbar(GLToolbar::Normal, "Top") - , m_collapse_toolbar(GLToolbar::Normal, "Top") + , m_main_toolbar(GLToolbar::Normal, "Main") + , m_undoredo_toolbar(GLToolbar::Normal, "Undo_Redo") , m_gizmos(*this) , m_use_clipping_planes(false) , m_sidebar_field("") @@ -1914,11 +1913,6 @@ void GLCanvas3D::enable_undoredo_toolbar(bool enable) m_undoredo_toolbar.set_enabled(enable); } -void GLCanvas3D::enable_collapse_toolbar(bool enable) -{ - m_collapse_toolbar.set_enabled(enable); -} - void GLCanvas3D::enable_dynamic_background(bool enable) { m_dynamic_background_enabled = enable; @@ -2112,7 +2106,7 @@ void GLCanvas3D::render() tooltip = m_undoredo_toolbar.get_tooltip(); if (tooltip.empty()) - tooltip = m_collapse_toolbar.get_tooltip(); + tooltip = wxGetApp().plater()->get_collapse_toolbar().get_tooltip(); if (tooltip.empty()) tooltip = wxGetApp().plater()->get_view_toolbar().get_tooltip(); @@ -2854,8 +2848,8 @@ void GLCanvas3D::on_idle(wxIdleEvent& evt) m_dirty |= m_main_toolbar.update_items_state(); m_dirty |= m_undoredo_toolbar.update_items_state(); - m_dirty |= m_collapse_toolbar.update_items_state(); m_dirty |= wxGetApp().plater()->get_view_toolbar().update_items_state(); + m_dirty |= wxGetApp().plater()->get_collapse_toolbar().update_items_state(); bool mouse3d_controller_applied = wxGetApp().plater()->get_mouse3d_controller().apply(wxGetApp().plater()->get_camera()); m_dirty |= mouse3d_controller_applied; @@ -3473,7 +3467,7 @@ void GLCanvas3D::on_mouse(wxMouseEvent& evt) return; } - if (m_collapse_toolbar.on_mouse(evt, *this)) + if (wxGetApp().plater()->get_collapse_toolbar().on_mouse(evt, *this)) { if (evt.LeftUp() || evt.MiddleUp() || evt.RightUp()) mouse_up_cleanup(); @@ -4187,7 +4181,7 @@ void GLCanvas3D::update_ui_from_settings() #endif // ENABLE_RETINA_GL bool enable_collapse = wxGetApp().app_config->get("show_collapse_button") == "1"; - enable_collapse_toolbar(enable_collapse); + wxGetApp().plater()->get_collapse_toolbar().set_enabled(enable_collapse); } @@ -5055,51 +5049,7 @@ bool GLCanvas3D::_init_view_toolbar() bool GLCanvas3D::_init_collapse_toolbar() { - if (!m_collapse_toolbar.is_enabled() && m_collapse_toolbar.get_items_count() > 0) - return true; - - BackgroundTexture::Metadata background_data; - background_data.filename = "toolbar_background.png"; - background_data.left = 16; - background_data.top = 16; - background_data.right = 16; - background_data.bottom = 16; - - if (!m_collapse_toolbar.init(background_data)) - { - // unable to init the toolbar texture, disable it - m_collapse_toolbar.set_enabled(false); - return true; - } - - m_collapse_toolbar.set_layout_type(GLToolbar::Layout::Vertical); - m_collapse_toolbar.set_horizontal_orientation(GLToolbar::Layout::HO_Right); - m_collapse_toolbar.set_vertical_orientation(GLToolbar::Layout::VO_Top); - m_collapse_toolbar.set_border(5.0f); - m_collapse_toolbar.set_separator_size(5); - m_collapse_toolbar.set_gap_size(2); - - GLToolbarItem::Data item; - - item.name = "collapse_sidebar"; - item.icon_filename = "collapse.svg"; - item.tooltip = wxGetApp().plater()->is_sidebar_collapsed() ? _utf8(L("Expand right panel")) : _utf8(L("Collapse right panel")); - item.sprite_id = 0; - item.left.action_callback = [this, item]() { - std::string new_tooltip = wxGetApp().plater()->is_sidebar_collapsed() ? - _utf8(L("Collapse right panel")) : _utf8(L("Expand right panel")); - - int id = m_collapse_toolbar.get_item_id("collapse_sidebar"); - m_collapse_toolbar.set_tooltip(id, new_tooltip); - set_tooltip(""); - - wxGetApp().plater()->collapse_sidebar(!wxGetApp().plater()->is_sidebar_collapsed()); - }; - - if (!m_collapse_toolbar.add_item(item)) - return false; - - return true; + return wxGetApp().plater()->init_collapse_toolbar(); } bool GLCanvas3D::_set_current() @@ -5427,20 +5377,21 @@ void GLCanvas3D::_check_and_update_toolbar_icon_scale() const float size = GLToolbar::Default_Icons_Size * scale; // Set current size for all top toolbars. It will be used for next calculations + GLToolbar& collapse_toolbar = wxGetApp().plater()->get_collapse_toolbar(); #if ENABLE_RETINA_GL const float sc = m_retina_helper->get_scale_factor() * scale; m_main_toolbar.set_scale(sc); m_undoredo_toolbar.set_scale(sc); - m_collapse_toolbar.set_scale(sc); + collapse_toolbar.set_scale(sc); size *= m_retina_helper->get_scale_factor(); #else m_main_toolbar.set_icons_size(size); m_undoredo_toolbar.set_icons_size(size); - m_collapse_toolbar.set_icons_size(size); + collapse_toolbar.set_icons_size(size); #endif // ENABLE_RETINA_GL - float top_tb_width = m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + m_collapse_toolbar.get_width(); - int items_cnt = m_main_toolbar.get_visible_items_cnt() + m_undoredo_toolbar.get_visible_items_cnt() + m_collapse_toolbar.get_visible_items_cnt(); + float top_tb_width = m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + collapse_toolbar.get_width(); + int items_cnt = m_main_toolbar.get_visible_items_cnt() + m_undoredo_toolbar.get_visible_items_cnt() + collapse_toolbar.get_visible_items_cnt(); float noitems_width = top_tb_width - size * items_cnt; // width of separators and borders in top toolbars // calculate scale needed for items in all top toolbars @@ -5460,7 +5411,6 @@ void GLCanvas3D::_check_and_update_toolbar_icon_scale() const wxGetApp().set_auto_toolbar_icon_scale(new_scale); } - void GLCanvas3D::_render_overlays() const { glsafe(::glDisable(GL_DEPTH_TEST)); @@ -5485,12 +5435,12 @@ void GLCanvas3D::_render_overlays() const const float scale = m_retina_helper->get_scale_factor() * wxGetApp().toolbar_icon_scale(/*true*/); m_main_toolbar.set_scale(scale); m_undoredo_toolbar.set_scale(scale); - m_collapse_toolbar.set_scale(scale); + wxGetApp().plater()->get_collapse_toolbar().set_scale(scale); #else const float size = int(GLToolbar::Default_Icons_Size * wxGetApp().toolbar_icon_scale(/*true*/)); m_main_toolbar.set_icons_size(size); m_undoredo_toolbar.set_icons_size(size); - m_collapse_toolbar.set_icons_size(size); + wxGetApp().plater()->get_collapse_toolbar().set_icons_size(size); #endif // ENABLE_RETINA_GL _render_main_toolbar(); @@ -5594,7 +5544,8 @@ void GLCanvas3D::_render_main_toolbar() const float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float top = 0.5f * (float)cnv_size.get_height() * inv_zoom; - float collapse_toolbar_width = m_collapse_toolbar.is_enabled() ? m_collapse_toolbar.get_width() : 0.0f; + const GLToolbar& collapse_toolbar = wxGetApp().plater()->get_collapse_toolbar(); + float collapse_toolbar_width = collapse_toolbar.is_enabled() ? collapse_toolbar.get_width() : 0.0f; float left = -0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + collapse_toolbar_width) * inv_zoom; m_main_toolbar.set_position(top, left); @@ -5610,7 +5561,8 @@ void GLCanvas3D::_render_undoredo_toolbar() const float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); float top = 0.5f * (float)cnv_size.get_height() * inv_zoom; - float collapse_toolbar_width = m_collapse_toolbar.is_enabled() ? m_collapse_toolbar.get_width() : 0.0f; + const GLToolbar& collapse_toolbar = wxGetApp().plater()->get_collapse_toolbar(); + float collapse_toolbar_width = collapse_toolbar.is_enabled() ? collapse_toolbar.get_width() : 0.0f; float left = (m_main_toolbar.get_width() - 0.5f * (m_main_toolbar.get_width() + m_undoredo_toolbar.get_width() + collapse_toolbar_width)) * inv_zoom; m_undoredo_toolbar.set_position(top, left); m_undoredo_toolbar.render(*this); @@ -5618,8 +5570,7 @@ void GLCanvas3D::_render_undoredo_toolbar() const void GLCanvas3D::_render_collapse_toolbar() const { - if (!m_collapse_toolbar.is_enabled()) - return; + GLToolbar& collapse_toolbar = wxGetApp().plater()->get_collapse_toolbar(); Size cnv_size = get_canvas_size(); float inv_zoom = (float)wxGetApp().plater()->get_camera().get_inv_zoom(); @@ -5627,10 +5578,10 @@ void GLCanvas3D::_render_collapse_toolbar() const float band = m_layers_editing.is_enabled() ? (wxGetApp().imgui()->get_style_scaling() * LayersEditing::THICKNESS_BAR_WIDTH) : 0.0; float top = 0.5f * (float)cnv_size.get_height() * inv_zoom; - float left = (0.5f * (float)cnv_size.get_width() - (float)m_collapse_toolbar.get_width() - band) * inv_zoom; + float left = (0.5f * (float)cnv_size.get_width() - (float)collapse_toolbar.get_width() - band) * inv_zoom; - m_collapse_toolbar.set_position(top, left); - m_collapse_toolbar.render(*this); + collapse_toolbar.set_position(top, left); + collapse_toolbar.render(*this); } void GLCanvas3D::_render_view_toolbar() const @@ -7164,9 +7115,10 @@ bool GLCanvas3D::_activate_search_toolbar_item() bool GLCanvas3D::_deactivate_collapse_toolbar_items() { - if (m_collapse_toolbar.is_item_pressed("print")) + GLToolbar& collapse_toolbar = wxGetApp().plater()->get_collapse_toolbar(); + if (collapse_toolbar.is_item_pressed("print")) { - m_collapse_toolbar.force_left_action(m_collapse_toolbar.get_item_id("print"), *this); + collapse_toolbar.force_left_action(collapse_toolbar.get_item_id("print"), *this); return true; } diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index 2dc8dbecd..c9433a10e 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -452,7 +452,6 @@ private: mutable GLGizmosManager m_gizmos; mutable GLToolbar m_main_toolbar; mutable GLToolbar m_undoredo_toolbar; - mutable GLToolbar m_collapse_toolbar; ClippingPlane m_clipping_planes[2]; mutable ClippingPlane m_camera_clipping_plane; bool m_use_clipping_planes; @@ -588,7 +587,6 @@ public: void enable_selection(bool enable); void enable_main_toolbar(bool enable); void enable_undoredo_toolbar(bool enable); - void enable_collapse_toolbar(bool enable); void enable_dynamic_background(bool enable); void enable_labels(bool enable) { m_labels.enable(enable); } #if ENABLE_SLOPE_RENDERING diff --git a/src/slic3r/GUI/GLToolbar.cpp b/src/slic3r/GUI/GLToolbar.cpp index 59401b11a..4ab282b06 100644 --- a/src/slic3r/GUI/GLToolbar.cpp +++ b/src/slic3r/GUI/GLToolbar.cpp @@ -1238,7 +1238,7 @@ bool GLToolbar::generate_icons_texture() const } std::vector> states; - if (m_name == "Top") + if (m_type == Normal) { states.push_back({ 1, false }); // Normal states.push_back({ 0, false }); // Pressed @@ -1247,7 +1247,7 @@ bool GLToolbar::generate_icons_texture() const states.push_back({ 0, false }); // HoverPressed states.push_back({ 2, false }); // HoverDisabled } - else if (m_name == "View") + else { states.push_back({ 1, false }); // Normal states.push_back({ 1, true }); // Pressed diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index 5084a7052..51f5c8d2d 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -68,7 +68,6 @@ bool View3D::init(wxWindow* parent, Model* model, DynamicPrintConfig* config, Ba m_canvas->enable_selection(true); m_canvas->enable_main_toolbar(true); m_canvas->enable_undoredo_toolbar(true); - m_canvas->enable_collapse_toolbar(true); m_canvas->enable_labels(true); #if ENABLE_SLOPE_RENDERING m_canvas->enable_slope(true); @@ -222,7 +221,6 @@ bool Preview::init(wxWindow* parent, Model* model) m_canvas->set_process(m_process); m_canvas->enable_legend_texture(true); m_canvas->enable_dynamic_background(true); - m_canvas->enable_collapse_toolbar(true); m_double_slider_sizer = new wxBoxSizer(wxHORIZONTAL); create_double_slider(); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 34b151a93..61500bfaf 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -1589,6 +1589,7 @@ struct Plater::priv Mouse3DController mouse3d_controller; View3D* view3D; GLToolbar view_toolbar; + GLToolbar collapse_toolbar; Preview *preview; BackgroundSlicingProcess background_process; @@ -1683,6 +1684,7 @@ struct Plater::priv void reset_canvas_volumes(); bool init_view_toolbar(); + bool init_collapse_toolbar(); void reset_all_gizmos(); void update_ui_from_settings(); @@ -1878,6 +1880,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) , m_ui_jobs(this) , delayed_scene_refresh(false) , view_toolbar(GLToolbar::Radio, "View") + , collapse_toolbar(GLToolbar::Normal, "Collapse") , m_project_filename(wxEmptyString) { this->q->SetFont(Slic3r::GUI::wxGetApp().normal_font()); @@ -3922,6 +3925,53 @@ bool Plater::priv::init_view_toolbar() return true; } +bool Plater::priv::init_collapse_toolbar() +{ + if (collapse_toolbar.get_items_count() > 0) + // already initialized + return true; + + BackgroundTexture::Metadata background_data; + background_data.filename = "toolbar_background.png"; + background_data.left = 16; + background_data.top = 16; + background_data.right = 16; + background_data.bottom = 16; + + if (!collapse_toolbar.init(background_data)) + return false; + + collapse_toolbar.set_layout_type(GLToolbar::Layout::Vertical); + collapse_toolbar.set_horizontal_orientation(GLToolbar::Layout::HO_Right); + collapse_toolbar.set_vertical_orientation(GLToolbar::Layout::VO_Top); + collapse_toolbar.set_border(5.0f); + collapse_toolbar.set_separator_size(5); + collapse_toolbar.set_gap_size(2); + + GLToolbarItem::Data item; + + item.name = "collapse_sidebar"; + item.icon_filename = "collapse.svg"; + item.tooltip = wxGetApp().plater()->is_sidebar_collapsed() ? _utf8(L("Expand right panel")) : _utf8(L("Collapse right panel")); + item.sprite_id = 0; + item.left.action_callback = [this, item]() { + std::string new_tooltip = wxGetApp().plater()->is_sidebar_collapsed() ? + _utf8(L("Collapse right panel")) : _utf8(L("Expand right panel")); + + int id = collapse_toolbar.get_item_id("collapse_sidebar"); + collapse_toolbar.set_tooltip(id, new_tooltip); + + wxGetApp().plater()->collapse_sidebar(!wxGetApp().plater()->is_sidebar_collapsed()); + }; + + if (!collapse_toolbar.add_item(item)) + return false; + + collapse_toolbar.set_enabled(true); + + return true; +} + bool Plater::priv::can_set_instance_to_object() const { const int obj_idx = get_selected_object_idx(); @@ -5531,6 +5581,11 @@ bool Plater::init_view_toolbar() return p->init_view_toolbar(); } +bool Plater::init_collapse_toolbar() +{ + return p->init_collapse_toolbar(); +} + const Camera& Plater::get_camera() const { return p->camera; @@ -5574,6 +5629,16 @@ GLToolbar& Plater::get_view_toolbar() return p->view_toolbar; } +const GLToolbar& Plater::get_collapse_toolbar() const +{ + return p->collapse_toolbar; +} + +GLToolbar& Plater::get_collapse_toolbar() +{ + return p->collapse_toolbar; +} + const Mouse3DController& Plater::get_mouse3d_controller() const { return p->mouse3d_controller; diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 10b6b354e..5d60e006b 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -315,6 +315,7 @@ public: void sys_color_changed(); bool init_view_toolbar(); + bool init_collapse_toolbar(); const Camera& get_camera() const; Camera& get_camera(); @@ -330,6 +331,9 @@ public: const GLToolbar& get_view_toolbar() const; GLToolbar& get_view_toolbar(); + const GLToolbar& get_collapse_toolbar() const; + GLToolbar& get_collapse_toolbar(); + const Mouse3DController& get_mouse3d_controller() const; Mouse3DController& get_mouse3d_controller();