Share One Build Plate Tilt Up-Direction Helper Across the GUI

The bed gravity arrow, volume rendering and the painter/support gizmos each rebuilt
the tilt up-vector from build_plate_tilt_x/y; use one helper that also tolerates
presets without the keys.
This commit is contained in:
Hanif Koh
2026-09-11 16:05:40 +08:00
parent 81e1482f9b
commit 37a806cd06
7 changed files with 23 additions and 46 deletions

View File

@@ -746,20 +746,14 @@ void Bed3D::render_custom(GLCanvas3D& canvas, const Transform3d& view_matrix, co
void Bed3D::render_gravity_arrow(const Transform3d& view_matrix, const Transform3d& projection_matrix)
{
const DynamicPrintConfig& cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config;
// build_plate_tilt_{x,y} are kept in sync with the belt tilt (see TabPrinter), so
// reading them here covers both belt and non-belt tilted printers.
double tilt_x_deg = cfg.opt_float("build_plate_tilt_x");
double tilt_y_deg = cfg.opt_float("build_plate_tilt_y");
if (tilt_x_deg == 0. && tilt_y_deg == 0.) {
const Vec3d up_dir = build_plate_tilt_up_direction();
if (up_dir == Vec3d::UnitZ()) {
m_gravity_arrow.reset();
return;
}
// Gravity direction (matching the slicer's tilt convention)
double tilt_x_rad = Geometry::deg2rad(tilt_x_deg);
double tilt_y_rad = Geometry::deg2rad(tilt_y_deg);
Vec3d gravity_dir = Vec3d(-tan(tilt_y_rad), -tan(tilt_x_rad), -1.0).normalized();
const Vec3d gravity_dir = -up_dir;
// Build the arrow model (same dimensions as the axis arrows)
if (!m_gravity_arrow.is_initialized()) {

View File

@@ -1133,17 +1133,7 @@ void GLVolumeCollection::render(GLVolumeCollection::ERenderType type,
// Compute up direction accounting for build plate tilt. This is frame-invariant
// (config cannot change mid-render), so compute it once before the volume loop.
Vec3f up_direction = Vec3f::UnitZ();
{
const DynamicPrintConfig& prt_cfg = GUI::wxGetApp().preset_bundle->printers.get_edited_preset().config;
double tilt_x_deg = prt_cfg.opt_float("build_plate_tilt_x");
double tilt_y_deg = prt_cfg.opt_float("build_plate_tilt_y");
if (tilt_x_deg != 0. || tilt_y_deg != 0.) {
double tilt_x_rad = Geometry::deg2rad(tilt_x_deg);
double tilt_y_rad = Geometry::deg2rad(tilt_y_deg);
up_direction = Vec3f(float(tan(tilt_y_rad)), float(tan(tilt_x_rad)), 1.f).normalized();
}
}
const Vec3f up_direction = GUI::build_plate_tilt_up_direction().cast<float>();
for (GLVolumeWithIdAndZ& volume : to_render) {
#if ENABLE_MODIFIERS_ALWAYS_TRANSPARENT

View File

@@ -71,6 +71,7 @@
#include <openssl/evp.h>
#include "libslic3r/Utils.hpp"
#include "libslic3r/Geometry.hpp"
#include "libslic3r/Model.hpp"
#include "libslic3r/I18N.hpp"
#include "libslic3r/PresetBundle.hpp"
@@ -10023,5 +10024,17 @@ bool is_support_filament(int extruder_id, bool strict_check)
return support_option->get_at(0);
};
Vec3d build_plate_tilt_up_direction()
{
const DynamicPrintConfig &cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config;
const auto *opt_x = cfg.option<ConfigOptionFloat>("build_plate_tilt_x");
const auto *opt_y = cfg.option<ConfigOptionFloat>("build_plate_tilt_y");
const double tilt_x = opt_x != nullptr ? opt_x->value : 0.;
const double tilt_y = opt_y != nullptr ? opt_y->value : 0.;
if (tilt_x == 0. && tilt_y == 0.)
return Vec3d::UnitZ();
return Vec3d(std::tan(Geometry::deg2rad(tilt_y)), std::tan(Geometry::deg2rad(tilt_x)), 1.).normalized();
}
} // GUI
} //Slic3r

View File

@@ -833,6 +833,8 @@ bool is_support_filament(int extruder_id, bool strict_check = true);
bool is_soluble_filament(int extruder_id);
// check if the filament for model is in the list
bool has_filaments(const std::vector<string>& model_filaments);
// Up direction of the edited printer's tilted build plate (+Z when untilted).
Vec3d build_plate_tilt_up_direction();
} // namespace GUI
} // Slic3r

View File

@@ -548,14 +548,6 @@ int GLGizmoFdmSupports::get_selection_support_threshold_angle()
return auto_support ? support_threshold_angle : 0;
}
std::pair<double, double> GLGizmoFdmSupports::get_build_plate_tilt()
{
const DynamicPrintConfig& cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config;
double tilt_x = cfg.opt_float("build_plate_tilt_x");
double tilt_y = cfg.opt_float("build_plate_tilt_y");
return {tilt_x, tilt_y};
}
void GLGizmoFdmSupports::select_facets_by_angle(float threshold_deg, bool block)
{
float threshold = (float(M_PI)/180.f)*threshold_deg;
@@ -564,15 +556,9 @@ void GLGizmoFdmSupports::select_facets_by_angle(float threshold_deg, bool block)
const ModelInstance* mi = mo->instances[selection.get_instance_idx()];
// Compute gravity direction accounting for build plate tilt
auto [tilt_x_deg, tilt_y_deg] = get_build_plate_tilt();
double tilt_x_rad = tilt_x_deg * M_PI / 180.0;
double tilt_y_rad = tilt_y_deg * M_PI / 180.0;
const bool has_tilt = (tilt_x_deg != 0. || tilt_y_deg != 0.);
// NB: use an if, not a ?:, so each branch converts to Vec3d independently
// (the two Eigen expression types don't unify in a ternary).
Vec3d gravity_dir = -Vec3d::UnitZ();
if (has_tilt)
gravity_dir = Vec3d(-tan(tilt_y_rad), -tan(tilt_x_rad), -1.0).normalized();
const Vec3d up_dir = build_plate_tilt_up_direction();
const bool has_tilt = up_dir != Vec3d::UnitZ();
const Vec3d gravity_dir = -up_dir;
int mesh_id = -1;
for (const ModelVolume* mv : mo->volumes) {

View File

@@ -60,7 +60,6 @@ private:
void select_facets_by_angle(float threshold, bool block);
// BBS
int get_selection_support_threshold_angle();
std::pair<double, double> get_build_plate_tilt();
int m_support_threshold_angle = -1;

View File

@@ -75,14 +75,7 @@ GLGizmoPainterBase::ClippingPlaneDataWrapper GLGizmoPainterBase::get_clipping_pl
Vec3f GLGizmoPainterBase::get_tilt_up_direction() const
{
const DynamicPrintConfig& cfg = wxGetApp().preset_bundle->printers.get_edited_preset().config;
double tilt_x_deg = cfg.opt_float("build_plate_tilt_x");
double tilt_y_deg = cfg.opt_float("build_plate_tilt_y");
if (tilt_x_deg == 0. && tilt_y_deg == 0.)
return Vec3f::UnitZ();
double tilt_x_rad = Geometry::deg2rad(tilt_x_deg);
double tilt_y_rad = Geometry::deg2rad(tilt_y_deg);
return Vec3f(float(tan(tilt_y_rad)), float(tan(tilt_x_rad)), 1.f).normalized();
return build_plate_tilt_up_direction().cast<float>();
}
void GLGizmoPainterBase::render_triangles(const Selection& selection) const