mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-12-02 01:43:36 -05:00
Give the Solid Drag Model's Flat Faces the Top and Bottom Surface Colours
This commit is contained in:
1 parent
7f9bd80ebd
commit
dac25fd5b0
15 files changed
+264
-23
No files matched your search
@@ -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.
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<std::size_t>(EGCodeExtrusionRole::COUNT) <= 32, "Item::roles holds one bit per role");
|
||||
static uint32_t role_bit(EGCodeExtrusionRole role) { return uint32_t(1) << static_cast<uint32_t>(role); }
|
||||
|
||||
std::vector<Item> m_items;
|
||||
Range m_view_range;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <cstddef>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define STDVEC_MEMSIZE(NAME, TYPE) NAME.capacity() * ((sizeof(TYPE) + __alignof(TYPE) - 1) / __alignof(TYPE)) * __alignof(TYPE)
|
||||
@@ -58,6 +59,49 @@ std::optional<std::size_t> collect_role_samples(const std::vector<PathVertex>& 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<typename HasRole>
|
||||
std::vector<std::size_t> pick_sample_layers(const Interval& layers, std::size_t max_count, const HasRole& has_role)
|
||||
{
|
||||
std::vector<std::size_t> 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<std::size_t> 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<typename HasRole, typename SampleLayer, typename HasSamples>
|
||||
std::optional<std::size_t> 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<std::size_t> picked = pick_sample_layers(layers, max_count, has_role);
|
||||
for (const std::size_t layer_id : picked) {
|
||||
if (const std::optional<std::size_t> 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<std::size_t> first = sample_layer(layer_id); first.has_value())
|
||||
return first;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace libvgcode
|
||||
|
||||
|
||||
@@ -1835,25 +1835,31 @@ std::optional<float> 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<std::pair<float, float>> samples;
|
||||
std::vector<size_t> 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<size_t> 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<uint32_t>(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<size_t> 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)];
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -448,6 +448,10 @@ private:
|
||||
bool m_use_color_clip_plane{ false };
|
||||
std::array<ColorRGBA, 2> 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<std::array<std::optional<ColorRGBA>, 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<ColorRGBA, 2>& colors) { m_color_clip_plane_colors = colors; }
|
||||
void set_flat_face_colors(std::vector<std::array<std::optional<ColorRGBA>, 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; }
|
||||
|
||||
@@ -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<std::vector<ColorRGBA>> own_tower_colors(m_shells.volumes.volumes.size());
|
||||
std::vector<std::array<std::optional<ColorRGBA>, 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<ColorRGBA>& 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<GLWipeTowerVolume*>(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<Role> walls = { Role::ExternalPerimeter, Role::Perimeter, Role::OverhangPerimeter };
|
||||
const std::initializer_list<Role> tower_role = { Role::WipeTower };
|
||||
const std::initializer_list<Role> 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<Role> down_faces = { Role::BridgeInfill, Role::BottomSurface };
|
||||
const std::initializer_list<Role> 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())
|
||||
|
||||
@@ -394,6 +394,9 @@ private:
|
||||
std::optional<ColorRGBA> body;
|
||||
// the bottom and top caps, then the section view's, sampled over every layer of the solid
|
||||
std::array<std::optional<ColorRGBA>, 3> caps;
|
||||
// the faces that point up and down: the top surfaces' and the bridges' or bottom
|
||||
// surfaces', sampled with the body
|
||||
std::array<std::optional<ColorRGBA>, 2> faces;
|
||||
};
|
||||
std::vector<Body> bodies;
|
||||
};
|
||||
|
||||
@@ -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<EGCodeExtrusionRole> 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));
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
// length-weighted median of what the view grades by.
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
@@ -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<std::size_t> 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<std::size_t>{ 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<std::size_t>{ 0, 10, 20, 30, 40, 50, 60, 70 });
|
||||
CHECK(pick_sample_layers(Interval{ 5, 7 }, 8, every) == std::vector<std::size_t>{ 5, 6, 7 });
|
||||
CHECK(pick_sample_layers(Interval{ 4, 4 }, 8, every) == std::vector<std::size_t>{ 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<std::size_t>{ 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<std::size_t> visited;
|
||||
const auto first_in_box = [&visited](std::size_t id) {
|
||||
visited.push_back(id);
|
||||
return (id == 21) ? std::optional<std::size_t>(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<std::size_t>(2100));
|
||||
// the eight picks, then the walk from the start of the interval, which skips the picked layer 20
|
||||
CHECK(visited == std::vector<std::size_t>{ 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<std::size_t> 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<std::size_t>();
|
||||
};
|
||||
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<std::size_t>();
|
||||
};
|
||||
CHECK(!sample_role_layers(Interval{ 0, 70 }, 8, every, collect_at_10, has_samples).has_value());
|
||||
CHECK(visited == std::vector<std::size_t>{ 0, 10, 20, 30, 40, 50, 60, 70 });
|
||||
}
|
||||
Reference in new issue
Block a user