Skip to content

Commit

Permalink
Ported filament shrinkage compensation for XY and independent Z from …
Browse files Browse the repository at this point in the history
…Prusa Slicer (fixing MMU painting, seam painting, support painting issues) (SoftFever#6507)

* Ported filament shrinkage compensation from Prusa Slicer. Updated logic to be 100 = no shrinkage to be consistent with orca definitions

* Code comments update

* Merge branch 'main' into Filament-Shrinkage-compension---port-from-Prusa-slicer

* Merge remote-tracking branch 'upstream/main' into Filament-Shrinkage-compension---port-from-Prusa-slicer

* Merge branch 'main' into Filament-Shrinkage-compension---port-from-Prusa-slicer
  • Loading branch information
igiannakas authored Aug 28, 2024
1 parent d1e7bb2 commit 0ba4181
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 52 deletions.
16 changes: 16 additions & 0 deletions src/libslic3r/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,22 @@ Transform3d Transformation::get_matrix_no_scaling_factor() const
return copy.get_matrix();
}

// Orca: Implement prusa's filament shrink compensation approach
Transform3d Transformation::get_matrix_with_applied_shrinkage_compensation(const Vec3d &shrinkage_compensation) const {
const Transform3d shrinkage_trafo = Geometry::scale_transform(shrinkage_compensation);
const Vec3d trafo_offset = this->get_offset();
const Vec3d trafo_offset_xy = Vec3d(trafo_offset.x(), trafo_offset.y(), 0.);

Transformation copy(*this);
copy.set_offset(Axis::X, 0.);
copy.set_offset(Axis::Y, 0.);

Transform3d trafo_after_shrinkage = (shrinkage_trafo * copy.get_matrix());
trafo_after_shrinkage.translation() += trafo_offset_xy;

return trafo_after_shrinkage;
}

Transformation Transformation::operator * (const Transformation& other) const
{
return Transformation(get_matrix() * other.get_matrix());
Expand Down
3 changes: 3 additions & 0 deletions src/libslic3r/Geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ class Transformation
Transform3d get_matrix_no_offset() const;
Transform3d get_matrix_no_scaling_factor() const;

// Orca: Implement prusa's filament shrink compensation approach
Transform3d get_matrix_with_applied_shrinkage_compensation(const Vec3d &shrinkage_compensation) const;

void set_matrix(const Transform3d& transform) { m_matrix = transform; }

Transformation operator * (const Transformation& other) const;
Expand Down
18 changes: 18 additions & 0 deletions src/libslic3r/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2779,6 +2779,24 @@ void ModelVolume::convert_from_meters()
this->source.is_converted_from_meters = true;
}

// Orca: Implement prusa's filament shrink compensation approach
// Returns 0-based indices of extruders painted by multi-material painting gizmo.
std::vector<size_t> ModelVolume::get_extruders_from_multi_material_painting() const {
if (!this->is_mm_painted())
return {};

assert(static_cast<size_t>(TriangleStateType::Extruder1) - 1 == 0);
const TriangleSelector::TriangleSplittingData &data = this->mmu_segmentation_facets.get_data();

std::vector<size_t> extruders;
for (size_t state_idx = static_cast<size_t>(EnforcerBlockerType::Extruder1); state_idx < data.used_states.size(); ++state_idx) {
if (data.used_states[state_idx])
extruders.emplace_back(state_idx - 1);
}

return extruders;
}

void ModelInstance::transform_mesh(TriangleMesh* mesh, bool dont_translate) const
{
mesh->transform(dont_translate ? get_matrix_no_offset() : get_matrix());
Expand Down
4 changes: 4 additions & 0 deletions src/libslic3r/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,10 @@ class ModelVolume final : public ObjectBase
bool is_fdm_support_painted() const { return !this->supported_facets.empty(); }
bool is_seam_painted() const { return !this->seam_facets.empty(); }
bool is_mm_painted() const { return !this->mmu_segmentation_facets.empty(); }

// Orca: Implement prusa's filament shrink compensation approach
// Returns 0-based indices of extruders painted by multi-material painting gizmo.
std::vector<size_t> get_extruders_from_multi_material_painting() const;

protected:
friend class Print;
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ static std::vector<std::string> s_Preset_filament_options {
"filament_wipe_distance", "additional_cooling_fan_speed",
"nozzle_temperature_range_low", "nozzle_temperature_range_high",
//SoftFever
"enable_pressure_advance", "pressure_advance","adaptive_pressure_advance","adaptive_pressure_advance_model","adaptive_pressure_advance_overhangs", "adaptive_pressure_advance_bridges","chamber_temperature", "filament_shrink", "support_material_interface_fan_speed", "filament_notes" /*,"filament_seam_gap"*/,
"enable_pressure_advance", "pressure_advance","adaptive_pressure_advance","adaptive_pressure_advance_model","adaptive_pressure_advance_overhangs", "adaptive_pressure_advance_bridges","chamber_temperature", "filament_shrink","filament_shrinkage_compensation_z", "support_material_interface_fan_speed", "filament_notes" /*,"filament_seam_gap"*/,
"filament_loading_speed", "filament_loading_speed_start",
"filament_unloading_speed", "filament_unloading_speed_start", "filament_toolchange_delay", "filament_cooling_moves", "filament_stamping_loading_speed", "filament_stamping_distance",
"filament_cooling_initial_speed", "filament_cooling_final_speed", "filament_ramming_parameters",
Expand Down
67 changes: 63 additions & 4 deletions src/libslic3r/Print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ bool Print::invalidate_state_by_config_options(const ConfigOptionResolver & /* n
opt_key == "initial_layer_print_height"
|| opt_key == "nozzle_diameter"
|| opt_key == "filament_shrink"
|| opt_key == "filament_shrinkage_compensation_z"
|| opt_key == "resolution"
|| opt_key == "precise_z_height"
// Spiral Vase forces different kind of slicing than the normal model:
Expand Down Expand Up @@ -1120,13 +1121,29 @@ StringObjectException Print::validate(StringObjectException *warning, Polygons*
const PrintObject &print_object = *m_objects[print_object_idx];
//FIXME It is quite expensive to generate object layers just to get the print height!
if (auto layers = generate_object_layers(print_object.slicing_parameters(), layer_height_profile(print_object_idx), print_object.config().precise_z_height.value);
! layers.empty() && layers.back() > this->config().printable_height + EPSILON) {
return
!layers.empty()) {

Vec3d test =this->shrinkage_compensation();
const double shrinkage_compensation_z = this->shrinkage_compensation().z();

if (shrinkage_compensation_z != 1. && layers.back() > (this->config().printable_height / shrinkage_compensation_z + EPSILON)) {
// The object exceeds the maximum build volume height because of shrinkage compensation.
return StringObjectException{
Slic3r::format(_u8L("While the object %1% itself fits the build volume, it exceeds the maximum build volume height because of material shrinkage compensation."), print_object.model_object()->name),
print_object.model_object(),
""
};
} else if (layers.back() > this->config().printable_height + EPSILON) {
// Test whether the last slicing plane is below or above the print volume.
{ 0.5 * (layers[layers.size() - 2] + layers.back()) > this->config().printable_height + EPSILON ?
return StringObjectException{
0.5 * (layers[layers.size() - 2] + layers.back()) > this->config().printable_height + EPSILON ?
Slic3r::format(_u8L("The object %1% exceeds the maximum build volume height."), print_object.model_object()->name) :
Slic3r::format(_u8L("While the object %1% itself fits the build volume, its last layer exceeds the maximum build volume height."), print_object.model_object()->name) +
" " + _u8L("You might want to reduce the size of your model or change current print settings and retry.") };
" " + _u8L("You might want to reduce the size of your model or change current print settings and retry."),
print_object.model_object(),
""
};
}
}
}

Expand Down Expand Up @@ -1568,6 +1585,10 @@ StringObjectException Print::validate(StringObjectException *warning, Polygons*
BOOST_LOG_TRIVIAL(warning) << "Orca: validate motion ability failed: " << e.what() << std::endl;
}
}
if (!this->has_same_shrinkage_compensations()){
warning->string = L("Filament shrinkage will not be used because filament shrinkage for the used filaments differs significantly.");
warning->opt_key = "";
}
return {};
}

Expand Down Expand Up @@ -2949,6 +2970,44 @@ std::string PrintStatistics::finalize_output_path(const std::string &path_in) co
return final_path;
}

// Orca: Implement prusa's filament shrink compensation approach
// Returns if all used filaments have same shrinkage compensations.
bool Print::has_same_shrinkage_compensations() const {
const std::vector<unsigned int> extruders = this->extruders();
if (extruders.empty())
return false;

const double filament_shrinkage_compensation_xy = m_config.filament_shrink.get_at(extruders.front());
const double filament_shrinkage_compensation_z = m_config.filament_shrinkage_compensation_z.get_at(extruders.front());

for (unsigned int extruder : extruders) {
if (filament_shrinkage_compensation_xy != m_config.filament_shrink.get_at(extruder) ||
filament_shrinkage_compensation_z != m_config.filament_shrinkage_compensation_z.get_at(extruder)) {
return false;
}
}

return true;
}

// Orca: Implement prusa's filament shrink compensation approach, but amended so 100% from the user is the equivalent to 0 in orca.
// Returns scaling for each axis representing shrinkage compensations in each axis.
Vec3d Print::shrinkage_compensation() const
{
if (!this->has_same_shrinkage_compensations())
return Vec3d::Ones();

const unsigned int first_extruder = this->extruders().front();

const double xy_shrinkage_percent = m_config.filament_shrink.get_at(first_extruder);
const double z_shrinkage_percent = m_config.filament_shrinkage_compensation_z.get_at(first_extruder);

const double xy_compensation = 100.0 / xy_shrinkage_percent;
const double z_compensation = 100.0 / z_shrinkage_percent;

return { xy_compensation, xy_compensation, z_compensation };
}

const std::string PrintStatistics::FilamentUsedG = "filament used [g]";
const std::string PrintStatistics::FilamentUsedGMask = "; filament used [g] =";

Expand Down
9 changes: 8 additions & 1 deletion src/libslic3r/Print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ class PrintObject : public PrintObjectBaseWithState<Print, PrintObjectStep, posC
// The slicing parameters are dependent on various configuration values
// (layer height, first layer height, raft settings, print nozzle diameter etc).
const SlicingParameters& slicing_parameters() const { return m_slicing_params; }
static SlicingParameters slicing_parameters(const DynamicPrintConfig &full_config, const ModelObject &model_object, float object_max_z);
// Orca: XYZ shrinkage compensation has introduced the const Vec3d &object_shrinkage_compensation parameter to the function below
static SlicingParameters slicing_parameters(const DynamicPrintConfig &full_config, const ModelObject &model_object, float object_max_z, const Vec3d &object_shrinkage_compensation);

size_t num_printing_regions() const throw() { return m_shared_regions->all_regions.size(); }
const PrintRegion& printing_region(size_t idx) const throw() { return *m_shared_regions->all_regions[idx].get(); }
Expand Down Expand Up @@ -981,6 +982,12 @@ class Print : public PrintBaseWithState<PrintStep, psCount>
bool is_all_objects_are_short() const {
return std::all_of(this->objects().begin(), this->objects().end(), [&](PrintObject* obj) { return obj->height() < scale_(this->config().nozzle_height.value); });
}

// Orca: Implement prusa's filament shrink compensation approach
// Returns if all used filaments have same shrinkage compensations.
bool has_same_shrinkage_compensations() const;
// Returns scaling for each axis representing shrinkage compensations in each axis.
Vec3d shrinkage_compensation() const;

protected:
// Invalidates the step, and its depending steps in Print.
Expand Down
11 changes: 8 additions & 3 deletions src/libslic3r/PrintApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,19 @@ struct PrintObjectTrafoAndInstances
};

// Generate a list of trafos and XY offsets for instances of a ModelObject
static std::vector<PrintObjectTrafoAndInstances> print_objects_from_model_object(const ModelObject &model_object)
// Orca: Updated to include XYZ filament shrinkage compensation
static std::vector<PrintObjectTrafoAndInstances> print_objects_from_model_object(const ModelObject &model_object, const Vec3d &shrinkage_compensation)
{
std::set<PrintObjectTrafoAndInstances> trafos;
PrintObjectTrafoAndInstances trafo;
//BBS: add useful logs for debug
int index = 0;
for (ModelInstance *model_instance : model_object.instances) {
if (model_instance->is_printable()) {
trafo.trafo = model_instance->get_matrix();
// Orca: Updated with XYZ filament shrinkage compensation
Geometry::Transformation model_instance_transformation = model_instance->get_transformation();
trafo.trafo = model_instance_transformation.get_matrix_with_applied_shrinkage_compensation(shrinkage_compensation);

auto shift = Point::new_scale(trafo.trafo.data()[12], trafo.trafo.data()[13]);
// Reset the XY axes of the transformation.
trafo.trafo.data()[12] = 0;
Expand Down Expand Up @@ -1358,7 +1362,8 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_
// Walk over all new model objects and check, whether there are matching PrintObjects.
for (ModelObject *model_object : m_model.objects) {
ModelObjectStatus &model_object_status = const_cast<ModelObjectStatus&>(model_object_status_db.reuse(*model_object));
model_object_status.print_instances = print_objects_from_model_object(*model_object);
// Orca: Updated for XYZ filament shrink compensation
model_object_status.print_instances = print_objects_from_model_object(*model_object, this->shrinkage_compensation());
std::vector<const PrintObjectStatus*> old;
old.reserve(print_object_status_db.count(*model_object));
for (const PrintObjectStatus &print_object_status : print_object_status_db.get_range(*model_object))
Expand Down
12 changes: 11 additions & 1 deletion src/libslic3r/PrintConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ void PrintConfigDef::init_fff_params()
def->set_default_value(new ConfigOptionFloats{ 0.4157 });

def = this->add("filament_shrink", coPercents);
def->label = L("Shrinkage");
def->label = L("Shrinkage (XY)");
// xgettext:no-c-format, no-boost-format
def->tooltip = L("Enter the shrinkage percentage that the filament will get after cooling (94% if you measure 94mm instead of 100mm)."
" The part will be scaled in xy to compensate."
Expand All @@ -1918,6 +1918,16 @@ void PrintConfigDef::init_fff_params()
def->min = 10;
def->mode = comAdvanced;
def->set_default_value(new ConfigOptionPercents{ 100 });

def = this->add("filament_shrinkage_compensation_z", coPercents);
def->label = L("Shrinkage (Z)");
def->tooltip = L("Enter the shrinkage percentage that the filament will get after cooling (94% if you measure 94mm instead of 100mm)."
" The part will be scaled in Z to compensate.");
def->sidetext = L("%");
def->ratio_over = "";
def->min = 10;
def->mode = comAdvanced;
def->set_default_value(new ConfigOptionPercents{ 100 });

def = this->add("filament_loading_speed", coFloats);
def->label = L("Loading speed");
Expand Down
1 change: 1 addition & 0 deletions src/libslic3r/PrintConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ PRINT_CONFIG_CLASS_DERIVED_DEFINE(
((ConfigOptionBool, independent_support_layer_height))
// SoftFever
((ConfigOptionPercents, filament_shrink))
((ConfigOptionPercents, filament_shrinkage_compensation_z))
((ConfigOptionBool, gcode_label_objects))
((ConfigOptionBool, exclude_object))
((ConfigOptionBool, gcode_comments))
Expand Down
15 changes: 9 additions & 6 deletions src/libslic3r/PrintObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2966,12 +2966,15 @@ void PrintObject::generate_support_preview()

void PrintObject::update_slicing_parameters()
{
if (!m_slicing_params.valid)
m_slicing_params = SlicingParameters::create_from_config(
this->print()->config(), m_config, this->model_object()->max_z(), this->object_extruders());
// Orca: updated function call for XYZ shrinkage compensation
if (!m_slicing_params.valid) {
m_slicing_params = SlicingParameters::create_from_config(this->print()->config(), m_config, this->model_object()->max_z(),
this->object_extruders(), this->print()->shrinkage_compensation());
}
}

SlicingParameters PrintObject::slicing_parameters(const DynamicPrintConfig& full_config, const ModelObject& model_object, float object_max_z)
// Orca: XYZ shrinkage compensation has introduced the const Vec3d &object_shrinkage_compensation parameter to the function below
SlicingParameters PrintObject::slicing_parameters(const DynamicPrintConfig &full_config, const ModelObject &model_object, float object_max_z, const Vec3d &object_shrinkage_compensation)
{
PrintConfig print_config;
PrintObjectConfig object_config;
Expand Down Expand Up @@ -3006,7 +3009,7 @@ SlicingParameters PrintObject::slicing_parameters(const DynamicPrintConfig& full

if (object_max_z <= 0.f)
object_max_z = (float)model_object.raw_bounding_box().size().z();
return SlicingParameters::create_from_config(print_config, object_config, object_max_z, object_extruders);
return SlicingParameters::create_from_config(print_config, object_config, object_max_z, object_extruders, object_shrinkage_compensation);
}

// returns 0-based indices of extruders used to print the object (without brim, support and other helper extrusions)
Expand Down Expand Up @@ -3049,7 +3052,7 @@ bool PrintObject::update_layer_height_profile(const ModelObject &model_object, c
// Must not be of even length.
((layer_height_profile.size() & 1) != 0 ||
// Last entry must be at the top of the object.
std::abs(layer_height_profile[layer_height_profile.size() - 2] - slicing_parameters.object_print_z_max + slicing_parameters.object_print_z_min) > 1e-3))
std::abs(layer_height_profile[layer_height_profile.size() - 2] - slicing_parameters.object_print_z_uncompensated_max + slicing_parameters.object_print_z_min) > 1e-3))
layer_height_profile.clear();

if (layer_height_profile.empty() || layer_height_profile[1] != slicing_parameters.first_object_layer_height) {
Expand Down
16 changes: 0 additions & 16 deletions src/libslic3r/PrintObjectSlice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,22 +449,6 @@ static std::vector<std::vector<ExPolygons>> slices_to_regions(
});
}

// SoftFever: ported from SuperSlicer
// filament shrink
for (const std::unique_ptr<PrintRegion>& pr : print_object_regions.all_regions) {
if (pr.get()) {
std::vector<ExPolygons>& region_polys = slices_by_region[pr->print_object_region_id()];
const size_t extruder_id = pr->extruder(FlowRole::frPerimeter) - 1;
double scale = print_config.filament_shrink.values[extruder_id] * 0.01;
if (scale != 1) {
scale = 1 / scale;
for (ExPolygons& polys : region_polys)
for (ExPolygon& poly : polys)
poly.scale(scale);
}
}
}

return slices_by_region;
}

Expand Down
Loading

0 comments on commit 0ba4181

Please sign in to comment.