From 853b8adf807b1c3fbef09361cd73336feebfa3d6 Mon Sep 17 00:00:00 2001 From: Vojtech Bubnik Date: Mon, 11 Apr 2022 09:49:41 +0200 Subject: [PATCH] Little refactoring of SeamPlacer. Moved color mapping functions to Color.hpp Removed the "extern" keyword from Color.hpp --- src/libslic3r/Color.hpp | 62 ++++++++++++++++++----------- src/libslic3r/GCode/SeamPlacer.cpp | 63 ++++++++++++------------------ 2 files changed, 64 insertions(+), 61 deletions(-) diff --git a/src/libslic3r/Color.hpp b/src/libslic3r/Color.hpp index 183705c4a..bb62ffdaa 100644 --- a/src/libslic3r/Color.hpp +++ b/src/libslic3r/Color.hpp @@ -133,41 +133,57 @@ public: static const ColorRGBA Z() { return { 0.0f, 0.0f, 0.75f, 1.0f }; } }; -extern ColorRGB operator * (float value, const ColorRGB& other); -extern ColorRGBA operator * (float value, const ColorRGBA& other); +ColorRGB operator * (float value, const ColorRGB& other); +ColorRGBA operator * (float value, const ColorRGBA& other); -extern ColorRGB lerp(const ColorRGB& a, const ColorRGB& b, float t); -extern ColorRGBA lerp(const ColorRGBA& a, const ColorRGBA& b, float t); +ColorRGB lerp(const ColorRGB& a, const ColorRGB& b, float t); +ColorRGBA lerp(const ColorRGBA& a, const ColorRGBA& b, float t); -extern ColorRGB complementary(const ColorRGB& color); -extern ColorRGBA complementary(const ColorRGBA& color); +ColorRGB complementary(const ColorRGB& color); +ColorRGBA complementary(const ColorRGBA& color); -extern ColorRGB saturate(const ColorRGB& color, float factor); -extern ColorRGBA saturate(const ColorRGBA& color, float factor); +ColorRGB saturate(const ColorRGB& color, float factor); +ColorRGBA saturate(const ColorRGBA& color, float factor); -extern ColorRGB opposite(const ColorRGB& color); -extern ColorRGB opposite(const ColorRGB& a, const ColorRGB& b); +ColorRGB opposite(const ColorRGB& color); +ColorRGB opposite(const ColorRGB& a, const ColorRGB& b); -extern bool can_decode_color(const std::string& color); +bool can_decode_color(const std::string& color); -extern bool decode_color(const std::string& color_in, ColorRGB& color_out); -extern bool decode_color(const std::string& color_in, ColorRGBA& color_out); +bool decode_color(const std::string& color_in, ColorRGB& color_out); +bool decode_color(const std::string& color_in, ColorRGBA& color_out); -extern bool decode_colors(const std::vector& colors_in, std::vector& colors_out); -extern bool decode_colors(const std::vector& colors_in, std::vector& colors_out); +bool decode_colors(const std::vector& colors_in, std::vector& colors_out); +bool decode_colors(const std::vector& colors_in, std::vector& colors_out); -extern std::string encode_color(const ColorRGB& color); -extern std::string encode_color(const ColorRGBA& color); +std::string encode_color(const ColorRGB& color); +std::string encode_color(const ColorRGBA& color); -extern ColorRGB to_rgb(const ColorRGBA& other_rgba); -extern ColorRGBA to_rgba(const ColorRGB& other_rgb); -extern ColorRGBA to_rgba(const ColorRGB& other_rgb, float alpha); +ColorRGB to_rgb(const ColorRGBA& other_rgba); +ColorRGBA to_rgba(const ColorRGB& other_rgb); +ColorRGBA to_rgba(const ColorRGB& other_rgb, float alpha); -extern ColorRGBA picking_decode(unsigned int id); -extern unsigned int picking_encode(unsigned char r, unsigned char g, unsigned char b); +// Color mapping of a value into RGB false colors. +inline Vec3f value_to_rgbf(float minimum, float maximum, float value) +{ + float ratio = 2.0f * (value - minimum) / (maximum - minimum); + float b = std::max(0.0f, (1.0f - ratio)); + float r = std::max(0.0f, (ratio - 1.0f)); + float g = 1.0f - b - r; + return Vec3f { r, g, b }; +} + +// Color mapping of a value into RGB false colors. +inline Vec3i value_to_rgbi(float minimum, float maximum, float value) +{ + return (value_to_rgbf(minimum, maximum, value) * 255).cast(); +} + +ColorRGBA picking_decode(unsigned int id); +unsigned int picking_encode(unsigned char r, unsigned char g, unsigned char b); // Produce an alpha channel checksum for the red green blue components. The alpha channel may then be used to verify, whether the rgb components // were not interpolated by alpha blending or multi sampling. -extern unsigned char picking_checksum_alpha_channel(unsigned char red, unsigned char green, unsigned char blue); +unsigned char picking_checksum_alpha_channel(unsigned char red, unsigned char green, unsigned char blue); } // namespace Slic3r diff --git a/src/libslic3r/GCode/SeamPlacer.cpp b/src/libslic3r/GCode/SeamPlacer.cpp index c729e4982..738164b87 100644 --- a/src/libslic3r/GCode/SeamPlacer.cpp +++ b/src/libslic3r/GCode/SeamPlacer.cpp @@ -11,6 +11,7 @@ #include "libslic3r/ExtrusionEntity.hpp" #include "libslic3r/Print.hpp" #include "libslic3r/BoundingBox.hpp" +#include "libslic3r/Color.hpp" #include "libslic3r/EdgeGrid.hpp" #include "libslic3r/ClipperUtils.hpp" #include "libslic3r/Layer.hpp" @@ -45,18 +46,6 @@ float gauss(float value, float mean_x_coord, float mean_value, float falloff_spe return mean_value * (std::exp(exponent) - 1.0f) / (std::exp(1.0f) - 1.0f); } -Vec3f value_to_rgbf(float minimum, float maximum, float value) { - float ratio = 2.0f * (value - minimum) / (maximum - minimum); - float b = std::max(0.0f, (1.0f - ratio)); - float r = std::max(0.0f, (ratio - 1.0f)); - float g = 1.0f - b - r; - return Vec3f { r, g, b }; -} - -Vec3i value_rgbi(float minimum, float maximum, float value) { - return (value_to_rgbf(minimum, maximum, value) * 255).cast(); -} - /// Coordinate frame class Frame { public: @@ -616,16 +605,6 @@ struct SeamComparator { setup(setup) { } - float compute_angle_penalty(float ccw_angle) const { - // This function is used: - // ((ℯ^(((1)/(x^(2)*3+1)))-1)/(ℯ-1))*1+((1)/(2+ℯ^(-x))) - // looks scary, but it is gaussian combined with sigmoid, - // so that concave points have much smaller penalty over convex ones - - return gauss(ccw_angle, 0.0f, 1.0f, 3.0f) + - 1.0f / (2 + std::exp(-ccw_angle)); // sigmoid, which heavily favourizes concave angles - } - // Standard comparator, must respect the requirements of comparators (e.g. give same result on same inputs) for sorting usage // should return if a is better seamCandidate than b bool is_first_better(const SeamCandidate &a, const SeamCandidate &b, const Vec2f &preffered_location = Vec2f { 0.0f, @@ -748,8 +727,18 @@ struct SeamComparator { return (a.visibility + SeamPlacer::additional_angle_importance) * compute_angle_penalty(a.local_ccw_angle); } -} -; + +private: + float compute_angle_penalty(float ccw_angle) const { + // This function is used: + // ((ℯ^(((1)/(x^(2)*3+1)))-1)/(ℯ-1))*1+((1)/(2+ℯ^(-x))) + // looks scary, but it is gaussian combined with sigmoid, + // so that concave points have much smaller penalty over convex ones + + return gauss(ccw_angle, 0.0f, 1.0f, 3.0f) + + 1.0f / (2 + std::exp(-ccw_angle)); // sigmoid, which heavily favourizes concave angles + } +}; #ifdef DEBUG_FILES void debug_export_points(const std::vector &layers, @@ -1327,28 +1316,27 @@ void SeamPlacer::place_seam(const Layer *layer, ExtrusionLoop &loop, bool extern assert(dynamic_cast(layer) == nullptr); // Object layer IDs are incremented by the number of raft layers. assert(layer->id() >= po->slicing_parameters().raft_layers()); - size_t layer_index = layer->id() - po->slicing_parameters().raft_layers(); - double unscaled_z = layer->slice_z; + const size_t layer_index = layer->id() - po->slicing_parameters().raft_layers(); + const double unscaled_z = layer->slice_z; const PrintObjectSeamData::LayerSeams &layer_perimeters = m_seam_per_object.find(layer->object())->second.layers[layer_index]; - const Point &fp = loop.first_point(); - - Vec2f unscaled_p = unscaled(fp); - size_t closest_perimeter_point_index = find_closest_point(*layer_perimeters.points_tree.get(), to_3d(unscaled_p, float(unscaled_z))); + // Find the closest perimeter in the SeamPlacer to the first point of this loop. + size_t closest_perimeter_point_index; + { + const Point &fp = loop.first_point(); + Vec2f unscaled_p = unscaled(fp); + closest_perimeter_point_index = find_closest_point(*layer_perimeters.points_tree.get(), to_3d(unscaled_p, float(unscaled_z))); + } Vec3f seam_position; if (const Perimeter &perimeter = layer_perimeters.points[closest_perimeter_point_index].perimeter; perimeter.finalized) { seam_position = perimeter.final_seam_position; } else { - size_t seam_index; - if (po->config().seam_position == spNearest) { - seam_index = pick_nearest_seam_point_index(layer_perimeters.points, perimeter.start_index, - unscaled(last_pos)); - } else { - seam_index = perimeter.seam_index; - } + size_t seam_index = po->config().seam_position == spNearest ? + pick_nearest_seam_point_index(layer_perimeters.points, perimeter.start_index, unscaled(last_pos)) : + perimeter.seam_index; seam_position = layer_perimeters.points[seam_index].position; } @@ -1361,4 +1349,3 @@ void SeamPlacer::place_seam(const Layer *layer, ExtrusionLoop &loop, bool extern } } // namespace Slic3r -