From dac25fd5b07a2e79fe710d61368f280ac8e48c38 Mon Sep 17 00:00:00 2001 From: Hanif Koh Date: Wed, 25 Nov 2026 23:15:22 +0800 Subject: [PATCH] Give the Solid Drag Model's Flat Faces the Top and Bottom Surface Colours --- docs/HLSD/gcode-preview-dragging.md | 17 ++++- resources/shaders/110/gouraud.fs | 14 ++++ resources/shaders/110/gouraud.vs | 8 +- resources/shaders/140/gouraud.fs | 14 ++++ resources/shaders/140/gouraud.vs | 8 +- src/libvgcode/src/Layers.cpp | 4 + src/libvgcode/src/Layers.hpp | 8 ++ src/libvgcode/src/Utils.hpp | 44 +++++++++++ src/libvgcode/src/ViewerImpl.cpp | 32 ++++---- src/slic3r/GUI/3DScene.cpp | 8 ++ src/slic3r/GUI/3DScene.hpp | 5 ++ src/slic3r/GUI/GCodeViewer.cpp | 19 ++++- src/slic3r/GUI/GCodeViewer.hpp | 3 + tests/slic3rutils/test_libvgcode_layers.cpp | 27 +++++++ tests/slic3rutils/test_libvgcode_sampling.cpp | 76 +++++++++++++++++++ 15 files changed, 264 insertions(+), 23 deletions(-) diff --git a/docs/HLSD/gcode-preview-dragging.md b/docs/HLSD/gcode-preview-dragging.md index 17fe9106c8..10a1a8debd 100644 --- a/docs/HLSD/gcode-preview-dragging.md +++ b/docs/HLSD/gcode-preview-dragging.md @@ -101,11 +101,20 @@ The solid stands in for the toolpaths, so it wears their colours. In the views t filament (Summary, Filament, Tool) each body keeps the filament colour its shell was loaded with. In every other view `GCodeViewer::update_solid_model_colors()` samples the toolpaths through `Viewer::get_vertex_color()`: the body takes the outer wall's colour over the layers the solid -stands for, each layer cap the infill's in the layer it is cut under, sparse infill before solid, and +stands for, its faces that point up, within about ten degrees, the top surfaces', and those that +point down the bridges' or, failing those, the bottom surfaces', so that a broad floor or ledge +keeps the colour it shows at rest rather than the walls'. +`GLVolumeCollection::set_flat_face_colors()` hands those two to the `gouraud` shader per body. Each +layer cap takes the infill's colour in the layer it is cut under, sparse infill before solid, and the prime tower its own role's. One vertex stands for the role, chosen by -`Viewer::find_extrusion_vertex()`: in a view that grades the toolpaths by a value, the -length-weighted median of that value over the role's extrusions in up to eight layers spread over -the interval, so that neither the acceleration ramp a wall starts on nor one layer with the fan +`Viewer::find_extrusion_vertex()` among up to eight of the interval's layers that have the role, +spread evenly over them, so that a role printed on a few layers only, as top surfaces and bridges +are, is found where it was printed; the layer table records the roles of each layer as it is built. +On a plate of several objects another object's layers with the role can take every pick; where none +of them has the role inside the body's footprint, the interval's other layers with the role are +walked until one has. In a view that grades the toolpaths by a value, it is the length-weighted +median of that value over the role's extrusions on the sampled layers, or on that one layer when +the walk found it, so that neither the acceleration ramp a wall starts on nor one layer with the fan turned up sets the colour of the whole body; in any other view the first extrusion found. The colours are resolved when a drag starts and when the view type or the bands change, not per frame. diff --git a/resources/shaders/110/gouraud.fs b/resources/shaders/110/gouraud.fs index 7ce85f89f1..bcee7181af 100644 --- a/resources/shaders/110/gouraud.fs +++ b/resources/shaders/110/gouraud.fs @@ -34,6 +34,13 @@ uniform bool use_color_clip_plane; uniform vec4 uniform_color_clip_plane_1; uniform vec4 uniform_color_clip_plane_2; uniform SlopeDetection slope; +// The G-code preview's solid model stands in for the toolpaths, so a face that points up or +// down, within FLAT_FACE_NORMAL_Z (about 10 degrees), wears the colour of the top or bottom surface +// printed there rather than the walls'. +uniform bool use_flat_face_colors; +uniform vec4 up_face_color; +uniform vec4 down_face_color; +const float FLAT_FACE_NORMAL_Z = 0.985; @@ -115,6 +122,13 @@ void main() else color = uniform_color; + if (use_flat_face_colors) { + if (world_normal_z > FLAT_FACE_NORMAL_Z) + color.rgb = up_face_color.rgb; + else if (world_normal_z < -FLAT_FACE_NORMAL_Z) + color.rgb = down_face_color.rgb; + } + if (slope.actived) { if(world_pos.z<0.1&&world_pos.z>-0.1) { diff --git a/resources/shaders/110/gouraud.vs b/resources/shaders/110/gouraud.vs index 2d7e70c1d6..400a7f4a2b 100644 --- a/resources/shaders/110/gouraud.vs +++ b/resources/shaders/110/gouraud.vs @@ -31,6 +31,8 @@ uniform mat4 projection_matrix; uniform mat3 view_normal_matrix; uniform mat4 volume_world_matrix; uniform SlopeDetection slope; +// The G-code preview's solid model colours the faces that point up and down apart, see gouraud.fs +uniform bool use_flat_face_colors; // Clipping plane, x = min z, y = max z. Used by the FFF and SLA previews to clip with a top / bottom plane. uniform vec2 z_range; @@ -76,8 +78,10 @@ void main() // Point in homogenous coordinates. world_pos = volume_world_matrix * vec4(v_position, 1.0); - // dot product of world normal with up direction, used for slope shading - world_normal_z = slope.actived ? dot(normalize(slope.volume_world_normal_matrix * v_normal), slope.up_direction) : 0.0; + // dot product of world normal with up direction, used for slope shading; the flat face colours + // follow the layers, which are horizontal whatever the build plate's tilt + vec3 world_normal = normalize(slope.volume_world_normal_matrix * v_normal); + world_normal_z = slope.actived ? dot(world_normal, slope.up_direction) : (use_flat_face_colors ? world_normal.z : 0.0); gl_Position = projection_matrix * position; // Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded. diff --git a/resources/shaders/140/gouraud.fs b/resources/shaders/140/gouraud.fs index 2a259fe04a..3b1ac38971 100644 --- a/resources/shaders/140/gouraud.fs +++ b/resources/shaders/140/gouraud.fs @@ -34,6 +34,13 @@ uniform bool use_color_clip_plane; uniform vec4 uniform_color_clip_plane_1; uniform vec4 uniform_color_clip_plane_2; uniform SlopeDetection slope; +// The G-code preview's solid model stands in for the toolpaths, so a face that points up or +// down, within FLAT_FACE_NORMAL_Z (about 10 degrees), wears the colour of the top or bottom surface +// printed there rather than the walls'. +uniform bool use_flat_face_colors; +uniform vec4 up_face_color; +uniform vec4 down_face_color; +const float FLAT_FACE_NORMAL_Z = 0.985; // ORCA: a part of a translucent filament, drawn as its print would show in premultiplied color, see // GLVolumeCollection::render(). It catches a highlight only with translucent_gloss, in realistic view. @@ -128,6 +135,13 @@ void main() else color = uniform_color; + if (use_flat_face_colors) { + if (world_normal_z > FLAT_FACE_NORMAL_Z) + color.rgb = up_face_color.rgb; + else if (world_normal_z < -FLAT_FACE_NORMAL_Z) + color.rgb = down_face_color.rgb; + } + if (slope.actived) { if(world_pos.z<0.1&&world_pos.z>-0.1) { diff --git a/resources/shaders/140/gouraud.vs b/resources/shaders/140/gouraud.vs index 5777a64102..260a8f18f6 100644 --- a/resources/shaders/140/gouraud.vs +++ b/resources/shaders/140/gouraud.vs @@ -31,6 +31,8 @@ uniform mat4 projection_matrix; uniform mat3 view_normal_matrix; uniform mat4 volume_world_matrix; uniform SlopeDetection slope; +// The G-code preview's solid model colours the faces that point up and down apart, see gouraud.fs +uniform bool use_flat_face_colors; // Clipping plane, x = min z, y = max z. Used by the FFF and SLA previews to clip with a top / bottom plane. uniform vec2 z_range; @@ -76,8 +78,10 @@ void main() // Point in homogenous coordinates. world_pos = volume_world_matrix * vec4(v_position, 1.0); - // dot product of world normal with up direction, used for slope shading - world_normal_z = slope.actived ? dot(normalize(slope.volume_world_normal_matrix * v_normal), slope.up_direction) : 0.0; + // dot product of world normal with up direction, used for slope shading; the flat face colours + // follow the layers, which are horizontal whatever the build plate's tilt + vec3 world_normal = normalize(slope.volume_world_normal_matrix * v_normal); + world_normal_z = slope.actived ? dot(world_normal, slope.up_direction) : (use_flat_face_colors ? world_normal.z : 0.0); gl_Position = projection_matrix * position; // Fill in the scalars for fragment shader clipping. Fragments with any of these components lower than zero are discarded. diff --git a/src/libvgcode/src/Layers.cpp b/src/libvgcode/src/Layers.cpp index cb30995b72..89e27f05ba 100644 --- a/src/libvgcode/src/Layers.cpp +++ b/src/libvgcode/src/Layers.cpp @@ -47,6 +47,8 @@ void Layers::update(const PathVertex& vertex, uint32_t vertex_id) item.contains_colorprint_options |= is_colorprint_option(vertex); item.has_infill = is_infill(vertex); item.has_sparse_infill = vertex.type == EMoveType::Extrude && vertex.role == EGCodeExtrusionRole::InternalInfill; + if (vertex.is_extrusion()) + item.roles = role_bit(vertex.role); } else { Item& item = m_items.back(); @@ -59,6 +61,8 @@ void Layers::update(const PathVertex& vertex, uint32_t vertex_id) item.contains_colorprint_options |= is_colorprint_option(vertex); item.has_infill |= is_infill(vertex); item.has_sparse_infill |= vertex.type == EMoveType::Extrude && vertex.role == EGCodeExtrusionRole::InternalInfill; + if (vertex.is_extrusion()) + item.roles |= role_bit(vertex.role); } } diff --git a/src/libvgcode/src/Layers.hpp b/src/libvgcode/src/Layers.hpp index 7226726e1f..546e111511 100644 --- a/src/libvgcode/src/Layers.hpp +++ b/src/libvgcode/src/Layers.hpp @@ -55,6 +55,10 @@ public: bool layer_has_sparse_infill(std::size_t layer_id) const { return (layer_id < m_items.size()) ? m_items[layer_id].has_sparse_infill : false; } + // whether the layer has an extrusion of the role + bool layer_has_role(std::size_t layer_id, EGCodeExtrusionRole role) const { + return (layer_id < m_items.size()) ? (m_items[layer_id].roles & role_bit(role)) != 0 : false; + } // Whether each layer's infill closes every object printed on it, as the caller knows it from // the objects the moves belong to: bit 0 as seen from above, bit 1 as seen from below, see // GCodeInputData::layers_closed. Ignored unless there is one entry per layer. @@ -92,7 +96,11 @@ private: bool has_sparse_infill{ false }; bool closed_from_above{ false }; bool closed_from_below{ false }; + // one bit per extrusion role printed on the layer, see role_bit() + uint32_t roles{ 0 }; }; + static_assert(static_cast(EGCodeExtrusionRole::COUNT) <= 32, "Item::roles holds one bit per role"); + static uint32_t role_bit(EGCodeExtrusionRole role) { return uint32_t(1) << static_cast(role); } std::vector m_items; Range m_view_range; diff --git a/src/libvgcode/src/Utils.hpp b/src/libvgcode/src/Utils.hpp index 01568cc145..b83566639c 100644 --- a/src/libvgcode/src/Utils.hpp +++ b/src/libvgcode/src/Utils.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef _WIN32 #define STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + __alignof(TYPE) - 1) / __alignof(TYPE)) * __alignof(TYPE) @@ -58,6 +59,49 @@ std::optional collect_role_samples(const std::vector& v } return std::nullopt; } +// Up to max_count of the interval's layers that pass has_role(), spread evenly over them from the +// first such layer to the last; the caller bounds the interval by the layer count. A role printed +// on a few layers only, as top surfaces and bridges are, is so sampled where it was printed rather +// than missed between samples. +template +std::vector pick_sample_layers(const Interval& layers, std::size_t max_count, const HasRole& has_role) +{ + std::vector with_role; + for (std::size_t id = layers[0]; id <= layers[1]; ++id) { + if (has_role(id)) + with_role.push_back(id); + } + const std::size_t count = with_role.size(); + const std::size_t steps = std::min(count, max_count); + std::vector picked; + picked.reserve(steps); + for (std::size_t step = 0; step < steps; ++step) + picked.push_back(with_role[(steps > 1) ? step * (count - 1) / (steps - 1) : 0]); + return picked; +} +// Samples the role over the interval's layers through sample_layer(), which returns an id to stop +// at, as the first extrusion of a view that grades by nothing, and collects samples otherwise: +// the layers pick_sample_layers() picks, then, while has_samples() is false, the interval's other +// layers with the role in order, since on a plate of several objects the layers another object +// has the role on can take every pick. The walk stops at the first layer that yields samples, so +// its median comes from that layer alone. +template +std::optional sample_role_layers(const Interval& layers, std::size_t max_count, const HasRole& has_role, + const SampleLayer& sample_layer, const HasSamples& has_samples) +{ + const std::vector picked = pick_sample_layers(layers, max_count, has_role); + for (const std::size_t layer_id : picked) { + if (const std::optional first = sample_layer(layer_id); first.has_value()) + return first; + } + for (std::size_t layer_id = layers[0]; !has_samples() && layer_id <= layers[1]; ++layer_id) { + if (!has_role(layer_id) || std::binary_search(picked.begin(), picked.end(), layer_id)) + continue; + if (const std::optional first = sample_layer(layer_id); first.has_value()) + return first; + } + return std::nullopt; +} } // namespace libvgcode diff --git a/src/libvgcode/src/ViewerImpl.cpp b/src/libvgcode/src/ViewerImpl.cpp index 5a3b272849..273c30c4dc 100644 --- a/src/libvgcode/src/ViewerImpl.cpp +++ b/src/libvgcode/src/ViewerImpl.cpp @@ -1835,25 +1835,31 @@ std::optional ViewerImpl::get_vertex_value(const PathVertex& v) const // The extrusion of the role that stands for the layers, or the vertex count. A view that grades // by a value takes the length-weighted median of it over the role's extrusions in up to -// VERTEX_SAMPLE_LAYERS layers spread evenly over the interval, so that neither the ramp a wall -// starts on nor one odd layer sets the colour; any other view takes the first extrusion found. -// A layer's vertices lie between its first and last ids, with those of other layers among them -// in a print by object. Only the extrusions that end inside the box count, so that one object's -// walls can be told from another's on the same layers. +// VERTEX_SAMPLE_LAYERS of the interval's layers that have the role, spread evenly over them, so +// that neither the ramp a wall starts on nor one odd layer sets the colour, or, where none of +// those has the role inside the box, in the first other layer that has; any other view takes the +// first extrusion found, see sample_role_layers(). A layer's vertices lie between its first and +// last ids, with those of other layers among them in a print by object. Only the extrusions that +// end inside the box count, so that one object's walls can be told from another's on the same +// layers. size_t ViewerImpl::find_extrusion_vertex(const Interval& layers, EGCodeExtrusionRole role, const XYBox& box) const { + if (m_layers.empty() || layers[0] > layers[1]) + return m_vertices.size(); + const Interval range = { layers[0], std::min(layers[1], m_layers.count() - 1) }; std::vector> samples; std::vector ids; - const size_t count = (layers[1] >= layers[0]) ? layers[1] - layers[0] + 1 : 0; - const size_t steps = std::min(count, VERTEX_SAMPLE_LAYERS); const auto value_of = [this](const PathVertex& v) { return get_vertex_value(v); }; - for (size_t step = 0; step < steps; ++step) { - const size_t layer_id = layers[0] + ((steps > 1) ? step * (count - 1) / (steps - 1) : 0); - const std::optional first = collect_role_samples(m_vertices, m_layers.get_layer_vertex_range(layer_id), + const auto has_role = [this, role](size_t layer_id) { return m_layers.layer_has_role(layer_id, role); }; + const auto sample_layer = [&](size_t layer_id) { + return collect_role_samples(m_vertices, m_layers.get_layer_vertex_range(layer_id), static_cast(layer_id), role, box, value_of, samples, ids); - if (first.has_value()) - return *first; - } + }; + const auto has_samples = [&samples]() { return !samples.empty(); }; + const std::optional first = sample_role_layers(range, VERTEX_SAMPLE_LAYERS, has_role, sample_layer, + has_samples); + if (first.has_value()) + return *first; return samples.empty() ? m_vertices.size() : ids[weighted_median_index(samples)]; } diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index d76c8fd4cd..2538a15844 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -1130,6 +1130,14 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type, shader->set_uniform("color_clip_plane", m_color_clip_plane); shader->set_uniform("uniform_color_clip_plane_1", m_color_clip_plane_colors[0]); shader->set_uniform("uniform_color_clip_plane_2", m_color_clip_plane_colors[1]); + const unsigned int volume_id = volume.second.first; + const bool flat_face_colors = volume_id < m_flat_face_colors.size() && + (m_flat_face_colors[volume_id][0].has_value() || m_flat_face_colors[volume_id][1].has_value()); + shader->set_uniform("use_flat_face_colors", flat_face_colors); + if (flat_face_colors) { + shader->set_uniform("up_face_color", m_flat_face_colors[volume_id][0].value_or(volume.first->render_color)); + shader->set_uniform("down_face_color", m_flat_face_colors[volume_id][1].value_or(volume.first->render_color)); + } //BOOST_LOG_TRIVIAL(info) << boost::format("set uniform_color to {%1%, %2%, %3%, %4%}, with_outline=%5%, selected %6%") // %volume.first->render_color[0]%volume.first->render_color[1]%volume.first->render_color[2]%volume.first->render_color[3] // %with_outline%volume.first->selected; diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 31b44f7a41..b0e4f0c811 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -448,6 +448,10 @@ private: bool m_use_color_clip_plane{ false }; std::array m_color_clip_plane_colors{ ColorRGBA::RED(), ColorRGBA::BLUE() }; + // per volume, by index: the colours of its faces that point up and down, used by the G-code + // preview's solid model; a volume past the end, or an unset colour, keeps its own + std::vector, 2>> m_flat_face_colors; + struct Slope { // toggle for slope rendering @@ -541,6 +545,7 @@ public: m_color_clip_plane[3] = offset; } void set_color_clip_plane_colors(const std::array& colors) { m_color_clip_plane_colors = colors; } + void set_flat_face_colors(std::vector, 2>> colors) { m_flat_face_colors = std::move(colors); } bool is_slope_GlobalActive() const { return m_slope.isGlobalActive; } bool is_slope_active() const { return m_slope.active; } diff --git a/src/slic3r/GUI/GCodeViewer.cpp b/src/slic3r/GUI/GCodeViewer.cpp index d7adc147a2..1e778d2ea6 100644 --- a/src/slic3r/GUI/GCodeViewer.cpp +++ b/src/slic3r/GUI/GCodeViewer.cpp @@ -3932,6 +3932,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) own_colors.reserve(m_shells.volumes.volumes.size()); // the tower draws itself in its own colours and leaves the volume's at the default; one list per volume std::vector> own_tower_colors(m_shells.volumes.volumes.size()); + std::vector, 2>> flat_face_colors(m_shells.volumes.volumes.size()); for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) { GLVolume* volume = m_shells.volumes.volumes[k]; own_colors.push_back(volume->color); @@ -3943,7 +3944,12 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) } volume->color = shade(body.value_or(volume->color)); volume->set_render_color(); + for (size_t f = 0; f < 2; ++f) { + if (const std::optional& face = colors.bodies[k].faces[f]; face.has_value()) + flat_face_colors[k][f] = shade(*face); + } } + m_shells.volumes.set_flat_face_colors(std::move(flat_face_colors)); m_shells.volumes.set_z_range(z_bottom, z_top); // the section view cuts the solid as it cuts the toolpaths m_shells.volumes.set_clipping_plane({ m_clipping_plane[0], m_clipping_plane[1], m_clipping_plane[2], m_clipping_plane[3] }); @@ -3962,6 +3968,8 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) shader->set_uniform("z_far", camera.get_far_z()); shader->set_uniform("z_near", camera.get_near_z()); m_shells.volumes.render(GLVolumeCollection::ERenderType::Opaque, false, camera.get_view_matrix(), camera.get_projection_matrix()); + // the program keeps the last body's value, and not every user of it goes through the collection + shader->set_uniform("use_flat_face_colors", false); shader->stop_using(); // The cut faces, one cap of one body at a time. A pixel looks into a body through a cut where, @@ -4026,6 +4034,7 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) } m_shells.volumes.set_z_range(-FLT_MAX, FLT_MAX); + m_shells.volumes.set_flat_face_colors({}); for (size_t k = 0; k < m_shells.volumes.volumes.size(); ++k) { GLVolume* volume = m_shells.volumes.volumes[k]; if (auto* tower = dynamic_cast(volume); tower != nullptr) @@ -4036,8 +4045,9 @@ void GCodeViewer::render_solid_model(int canvas_width, int canvas_height) } // The solid stands in for the toolpaths, so it takes their colours in the view that colours them by -// something other than the filament: each body the outer wall's, each layer cap the infill's of the -// layer it is cut under, sparse if the layer has any, else solid, the section view's cap the +// something other than the filament: each body the outer wall's, its faces that point up the top +// surfaces' and those that point down the bridges' or bottom surfaces', each layer cap the infill's +// of the layer it is cut under, sparse if the layer has any, else solid, the section view's cap the // infill's over all the layers, and the tower its own role's, each from the toolpaths within the // body's own footprint, so that objects printed at different grades keep their own. The views that // colour by filament leave every body its own colour. The bodies and the section view's cap are @@ -4076,6 +4086,9 @@ void GCodeViewer::update_solid_model_colors(const libvgcode::Interval& whole_lay }; const std::initializer_list walls = { Role::ExternalPerimeter, Role::Perimeter, Role::OverhangPerimeter }; const std::initializer_list tower_role = { Role::WipeTower }; + const std::initializer_list up_faces = { Role::TopSolidInfill }; + // a face that points down above the first layer is printed over air or over a support + const std::initializer_list down_faces = { Role::BridgeInfill, Role::BottomSurface }; const std::initializer_list infill = { Role::InternalInfill, Role::SolidInfill, Role::TopSolidInfill, Role::BottomSurface, Role::BridgeInfill, Role::InternalBridgeInfill, Role::Ironing }; for (size_t n = 0; n < volumes.size(); ++n) { @@ -4089,6 +4102,8 @@ void GCodeViewer::update_solid_model_colors(const libvgcode::Interval& whole_lay SolidModelColors::Body& body = colors.bodies[n]; if (!held) { body.body = sample(whole_layers, tower ? tower_role : walls, box); + if (!tower) + body.faces = { sample(whole_layers, up_faces, box), sample(whole_layers, down_faces, box) }; // the section view's cut may cross any of the layers, so it is sampled over all of them, as the body is body.caps[2] = tower ? std::nullopt : sample(whole_layers, infill, box); if (!body.caps[2].has_value()) diff --git a/src/slic3r/GUI/GCodeViewer.hpp b/src/slic3r/GUI/GCodeViewer.hpp index cad2bddbae..ffc72ad10f 100644 --- a/src/slic3r/GUI/GCodeViewer.hpp +++ b/src/slic3r/GUI/GCodeViewer.hpp @@ -394,6 +394,9 @@ private: std::optional body; // the bottom and top caps, then the section view's, sampled over every layer of the solid std::array, 3> caps; + // the faces that point up and down: the top surfaces' and the bridges' or bottom + // surfaces', sampled with the body + std::array, 2> faces; }; std::vector bodies; }; diff --git a/tests/slic3rutils/test_libvgcode_layers.cpp b/tests/slic3rutils/test_libvgcode_layers.cpp index b78a002c41..3273cf40b2 100644 --- a/tests/slic3rutils/test_libvgcode_layers.cpp +++ b/tests/slic3rutils/test_libvgcode_layers.cpp @@ -285,3 +285,30 @@ TEST_CASE("Between the end layers the shell view drops only the infill under the CHECK_FALSE(keep_in_reduced_set(shell, true, move_of(EMoveType::Extrude, EGCodeExtrusionRole::InternalInfill), false)); CHECK(keep_in_reduced_set(shell, false, move_of(EMoveType::Extrude, EGCodeExtrusionRole::InternalInfill), false)); } + +TEST_CASE("The layer table records which extrusion roles each layer has", "[Libvgcode]") +{ + const std::vector top = { EGCodeExtrusionRole::ExternalPerimeter, EGCodeExtrusionRole::TopSolidInfill }; + const Layers layers = layers_with(3, { walls_and_infill, top, support_only }); + CHECK(layers.layer_has_role(0, EGCodeExtrusionRole::InternalInfill)); + CHECK(!layers.layer_has_role(0, EGCodeExtrusionRole::TopSolidInfill)); + CHECK(layers.layer_has_role(1, EGCodeExtrusionRole::TopSolidInfill)); + CHECK(layers.layer_has_role(1, EGCodeExtrusionRole::ExternalPerimeter)); + CHECK(!layers.layer_has_role(2, EGCodeExtrusionRole::ExternalPerimeter)); + // the travel that opens each layer counts for no role + CHECK(!layers.layer_has_role(2, EGCodeExtrusionRole::None)); + CHECK(!layers.layer_has_role(3, EGCodeExtrusionRole::SupportMaterial)); +} + +TEST_CASE("A layer that opens with an extrusion records its role", "[Libvgcode]") +{ + Layers layers; + PathVertex v; + v.type = EMoveType::Extrude; + v.role = EGCodeExtrusionRole::BridgeInfill; + v.layer_id = 0; + v.position = { 0.0f, 0.0f, 0.2f }; + layers.update(v, 0); + CHECK(layers.layer_has_role(0, EGCodeExtrusionRole::BridgeInfill)); + CHECK(!layers.layer_has_role(0, EGCodeExtrusionRole::ExternalPerimeter)); +} diff --git a/tests/slic3rutils/test_libvgcode_sampling.cpp b/tests/slic3rutils/test_libvgcode_sampling.cpp index 0e8c750ab5..2cf11c9a0e 100644 --- a/tests/slic3rutils/test_libvgcode_sampling.cpp +++ b/tests/slic3rutils/test_libvgcode_sampling.cpp @@ -2,6 +2,7 @@ // length-weighted median of what the view grades by. #include +#include #include #include #include @@ -17,6 +18,8 @@ using libvgcode::EGCodeExtrusionRole; using libvgcode::EMoveType; using libvgcode::Interval; using libvgcode::PathVertex; +using libvgcode::pick_sample_layers; +using libvgcode::sample_role_layers; using libvgcode::weighted_median_index; using libvgcode::XYBox; @@ -102,3 +105,76 @@ TEST_CASE("A view that grades by nothing takes the first extrusion in the box", CHECK(collect_role_samples(vertices, range, 0, EGCodeExtrusionRole::ExternalPerimeter, fast_one, no_value, samples, ids) == 1); CHECK(samples.empty()); } + +TEST_CASE("Layers are picked among those that have the role, from the first to the last", "[Libvgcode]") +{ + // top surfaces on four layers of a hundred: spread evenly over all of them, a sample of eight + // would land on layers 0, 14, 28, ... 99 and miss every one + const std::vector top = { 7, 8, 9, 10 }; + const auto has_top = [&top](std::size_t id) { return std::find(top.begin(), top.end(), id) != top.end(); }; + CHECK(pick_sample_layers(Interval{ 0, 99 }, 8, has_top) == top); + CHECK(pick_sample_layers(Interval{ 9, 99 }, 8, has_top) == std::vector{ 9, 10 }); + CHECK(pick_sample_layers(Interval{ 11, 99 }, 8, has_top).empty()); +} + +TEST_CASE("A role on every layer is sampled at evenly spread layers including both ends", "[Libvgcode]") +{ + const auto every = [](std::size_t) { return true; }; + CHECK(pick_sample_layers(Interval{ 0, 70 }, 8, every) == std::vector{ 0, 10, 20, 30, 40, 50, 60, 70 }); + CHECK(pick_sample_layers(Interval{ 5, 7 }, 8, every) == std::vector{ 5, 6, 7 }); + CHECK(pick_sample_layers(Interval{ 4, 4 }, 8, every) == std::vector{ 4 }); +} + +TEST_CASE("Layers with the role among gaps are spread from the first to the last", "[Libvgcode]") +{ + // the role on every third layer from 2 to 59: twenty layers, of which eight are picked + const auto every_third = [](std::size_t id) { return id >= 2 && id <= 59 && id % 3 == 2; }; + CHECK(pick_sample_layers(Interval{ 0, 70 }, 8, every_third) == std::vector{ 2, 8, 17, 26, 32, 41, 50, 59 }); +} + +TEST_CASE("Where another object's layers take every pick, the other layers with the role are walked", "[Libvgcode]") +{ + // one object with top surfaces on layers 20 to 59, picked at 20, 25, 31, 36, 42, 47, 53 and 59, + // and the other, inside the box, only on layer 21 + const auto has_top = [](std::size_t id) { return id >= 20 && id <= 59; }; + std::vector visited; + const auto first_in_box = [&visited](std::size_t id) { + visited.push_back(id); + return (id == 21) ? std::optional(2100) : std::nullopt; + }; + const auto no_samples = []() { return false; }; + CHECK(sample_role_layers(Interval{ 0, 70 }, 8, has_top, first_in_box, no_samples) == std::optional(2100)); + // the eight picks, then the walk from the start of the interval, which skips the picked layer 20 + CHECK(visited == std::vector{ 20, 25, 31, 36, 42, 47, 53, 59, 21 }); +} + +TEST_CASE("The walk stops at the first layer that yields samples, and never runs once the picks have", "[Libvgcode]") +{ + const auto every = [](std::size_t) { return true; }; + std::vector visited; + std::size_t sampled = 0; + // only layer 33, which is not among the picks 0, 10, ... 70, has the role inside the box, and in + // a graded view it yields samples + const auto collect = [&](std::size_t id) { + visited.push_back(id); + sampled += (id == 33) ? 1 : 0; + return std::optional(); + }; + const auto has_samples = [&sampled]() { return sampled > 0; }; + CHECK(!sample_role_layers(Interval{ 0, 70 }, 8, every, collect, has_samples).has_value()); + CHECK(visited.back() == 33); + // the eight picks and layers 1 to 33 less the picked 10, 20 and 30 + CHECK(visited.size() == 8 + 30); + CHECK(sampled == 1); + + visited.clear(); + sampled = 0; + // layer 10 is among the picks, so nothing past them is visited + const auto collect_at_10 = [&](std::size_t id) { + visited.push_back(id); + sampled += (id == 10) ? 1 : 0; + return std::optional(); + }; + CHECK(!sample_role_layers(Interval{ 0, 70 }, 8, every, collect_at_10, has_samples).has_value()); + CHECK(visited == std::vector{ 0, 10, 20, 30, 40, 50, 60, 70 }); +}