Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix remaining raft issues #2012

Merged
merged 15 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/holes.wkt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions benchmark/wall_benchmark.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 UltiMaker
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher

#ifndef CURAENGINE_WALL_BENCHMARK_H
Expand Down Expand Up @@ -105,7 +105,7 @@ class HolesWallTestFixture : public benchmark::Fixture

void SetUp(const ::benchmark::State& state)
{
auto wkt_file = std::filesystem::path(std::source_location::current().file_name()).parent_path().append("hole.wkt");
auto wkt_file = std::filesystem::path(__FILE__).parent_path().append("hole.wkt");
std::ifstream file{ wkt_file };

std::stringstream buffer;
Expand Down
69 changes: 61 additions & 8 deletions src/FffGcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
constexpr bool retract_before_outer_wall = false;
constexpr coord_t wipe_dist = 0;

Polygons raft_polygons; // should remain empty, since we only have the lines pattern for the raft...
Polygons raft_polygons;
std::optional<Point2LL> last_planned_position = std::optional<Point2LL>();

unsigned int current_extruder_nr = base_extruder_nr;
Expand Down Expand Up @@ -604,7 +604,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)

Application::getInstance().communication_->sendLayerComplete(layer_nr, z, layer_height);

Polygons raftLines;
Polygons raft_lines;
AngleDegrees fill_angle = (num_surface_layers + num_interface_layers) % 2 ? 45 : 135; // 90 degrees rotated from the interface layer.
constexpr bool zig_zaggify_infill = false;
constexpr bool connect_polygons = true; // causes less jerks, so better adhesion
Expand Down Expand Up @@ -674,7 +674,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
zag_skip_count,
pocket_size);
std::vector<VariableWidthLines> raft_paths;
infill_comp.generate(raft_paths, raft_polygons, raftLines, base_settings, layer_nr, SectionType::ADHESION);
infill_comp.generate(raft_paths, raft_polygons, raft_lines, base_settings, layer_nr, SectionType::ADHESION);
if (! raft_paths.empty())
{
const GCodePathConfig& config = gcode_layer.configs_storage_.raft_base_config;
Expand All @@ -698,14 +698,39 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
raft_paths);
wall_orderer.addToLayer();
}
gcode_layer.addLinesByOptimizer(raftLines, gcode_layer.configs_storage_.raft_base_config, SpaceFillType::Lines);

const auto wipe_dist = 0;
const auto spiralize = false;
const auto flow_ratio = 1.0_r;
const auto enable_travel_optimization = false;
const auto always_retract = false;
const auto reverse_order = false;

gcode_layer.addLinesByOptimizer(
raft_lines,
gcode_layer.configs_storage_.raft_base_config,
SpaceFillType::Lines,
enable_travel_optimization,
wipe_dist,
flow_ratio,
last_planned_position);
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();
gcode_layer.addPolygonsByOptimizer(
raft_polygons,
gcode_layer.configs_storage_.raft_base_config,
ZSeamConfig(),
wipe_dist,
spiralize,
flow_ratio,
always_retract,
reverse_order,
last_planned_position);
raft_polygons.clear();
raftLines.clear();
raft_lines.clear();
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();
}

layer_plan_buffer.handle(gcode_layer, gcode);
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();
}

const coord_t interface_layer_height = interface_settings.get<coord_t>("raft_interface_thickness");
Expand Down Expand Up @@ -834,7 +859,33 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
raft_paths);
wall_orderer.addToLayer();
}
gcode_layer.addLinesByOptimizer(raft_lines, gcode_layer.configs_storage_.raft_interface_config, SpaceFillType::Lines, false, 0, 1.0, last_planned_position);

const auto wipe_dist = 0;
const auto spiralize = false;
const auto flow_ratio = 1.0_r;
const auto enable_travel_optimization = false;
const auto always_retract = false;
const auto reverse_order = false;

gcode_layer.addLinesByOptimizer(
raft_lines,
gcode_layer.configs_storage_.raft_interface_config,
SpaceFillType::Lines,
enable_travel_optimization,
wipe_dist,
flow_ratio,
last_planned_position);
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();
gcode_layer.addPolygonsByOptimizer(
raft_polygons,
gcode_layer.configs_storage_.raft_interface_config,
ZSeamConfig(),
wipe_dist,
spiralize,
flow_ratio,
always_retract,
reverse_order,
last_planned_position);

raft_polygons.clear();
raft_lines.clear();
Expand Down Expand Up @@ -995,6 +1046,7 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
wipe_dist,
flow_ratio,
last_planned_position);
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();
gcode_layer.addPolygonsByOptimizer(
raft_polygons,
gcode_layer.configs_storage_.raft_surface_config,
Expand All @@ -1004,7 +1056,8 @@ void FffGcodeWriter::processRaft(const SliceDataStorage& storage)
flow_ratio,
always_retract,
reverse_order,
gcode_layer.getLastPlannedPositionOrStartingPosition());
last_planned_position);
last_planned_position = gcode_layer.getLastPlannedPositionOrStartingPosition();

raft_polygons.clear();
raft_lines.clear();
Expand Down
45 changes: 44 additions & 1 deletion src/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <polyclipping/clipper.hpp>

#include <spdlog/spdlog.h>

#include "Application.h" //To get settings.
#include "ExtruderTrain.h"
#include "Slice.h"
Expand Down Expand Up @@ -59,7 +61,48 @@ void Raft::generate(SliceDataStorage& storage)
{
if (remove_inside_corners)
{
outline.makeConvex();
// Make all parts convex. We could take the convex hull of the union of all parts, but that would
// result in a massive brim if you would have models in all corners of the build plate. We instead
// perform a convex hull operation on each polygon part separately, this will result in much smaller
// raft islands. However, this might result in inside corners in the raft outline in the situation
// where after the merge operations some parts become connected. To properly make sure that there are
// no inside corners we perform a convex hull operation followed by a merge. If the number of polygon
// parts no longer decrease we have found the polygons that have both no longer inside corners and
// occupy the smallest possible area.
outline = outline.unionPolygons();
auto outline_parts = outline.unionPolygons().splitIntoParts();
auto nr_of_parts = outline_parts.size();

while (true)
{
outline.clear();

for (auto& part : outline_parts)
{
part.makeConvex();
outline.add(part);
}

outline = outline.unionPolygons();
outline_parts = outline.splitIntoParts();
const auto new_nr_of_parts = outline_parts.size();

if (new_nr_of_parts > nr_of_parts)
{
// from the recursive convex hull + merge operation the number of parts cannot logically increase
// if it does increase, and allow the loop to continue we might get into an infinite loop; so break out of the loop
// this might produce a raft with inside corners, but that is better than an infinite loop
spdlog::warn("Error while removing inside corners from raft; merge operation increased the number of parts");
assert(false);
break;
}

if (new_nr_of_parts == nr_of_parts)
{
break;
}
nr_of_parts = new_nr_of_parts;
}
}
else
{
Expand Down
72 changes: 43 additions & 29 deletions src/utils/polygon.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Ultimaker B.V.
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher.

#include "utils/polygon.h"
Expand All @@ -14,8 +14,10 @@
#include <range/v3/view/c_str.hpp>
#include <range/v3/view/filter.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/sliding.hpp>
#include <range/v3/view/transform.hpp>
#include <range/v3/view/zip.hpp>
#include <spdlog/spdlog.h>

#include "utils/ListPolyIt.h"
#include "utils/PolylineStitcher.h"
Expand Down Expand Up @@ -104,44 +106,56 @@ Polygons Polygons::approxConvexHull(int extra_outset)

void Polygons::makeConvex()
{
for (PolygonRef poly : *this)
// early out if there is nothing to do
if (empty())
{
if (poly.size() <= 3)
{
continue; // Already convex.
}
return;
}

Polygon convexified;
// Andrew’s Monotone Chain Convex Hull Algorithm
std::vector<Point2LL> points;

// Start from a vertex that is known to be on the convex hull: The one with the lowest X.
const size_t start_index = std::min_element(
poly.begin(),
poly.end(),
[](Point2LL a, Point2LL b)
{
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
})
- poly.begin();
convexified.path->push_back(poly[start_index]);
for (const auto& poly : this->paths)
{
points.insert(points.end(), poly.begin(), poly.end());
}

for (size_t i = 1; i <= poly.size(); ++i)
ClipperLib::Path convexified;
auto make_sorted_poly_convex = [&convexified](std::vector<Point2LL>& poly)
{
convexified.push_back(poly[0]);

for (const auto window : poly | ranges::views::sliding(2))
{
const Point2LL& current = poly[(start_index + i) % poly.size()];
const Point2LL& current = window[0];
const Point2LL& after = window[1];

// Track backwards to make sure we haven't been in a concave pocket for multiple vertices already.
while (convexified.size() >= 2
&& (LinearAlg2D::pointIsLeftOfLine(convexified.path->back(), (*convexified.path)[convexified.size() - 2], current) >= 0
|| LinearAlg2D::pointIsLeftOfLine(convexified.path->back(), (*convexified.path)[convexified.size() - 2], convexified.path->front()) > 0))
if (LinearAlg2D::pointIsLeftOfLine(current, convexified.back(), after) < 0)
{
convexified.path->pop_back();
// Track backwards to make sure we haven't been in a concave pocket for multiple vertices already.
while (convexified.size() >= 2
&& (LinearAlg2D::pointIsLeftOfLine(convexified.back(), convexified[convexified.size() - 2], current) >= 0
|| LinearAlg2D::pointIsLeftOfLine(convexified.back(), convexified[convexified.size() - 2], convexified.front()) > 0))
{
convexified.pop_back();
}
convexified.push_back(current);
}
convexified.path->push_back(current);
}
// remove last vertex as the starting vertex is added in the last iteration of the loop
convexified.path->pop_back();
};

poly.path->swap(*convexified.path); // Due to vector's implementation, this is constant time.
}
std::sort(
points.begin(),
points.end(),
[](Point2LL a, Point2LL b)
{
return a.X == b.X ? a.Y < b.Y : a.X < b.X;
});
make_sorted_poly_convex(points);
std::reverse(points.begin(), points.end());
make_sorted_poly_convex(points);

this->paths = { convexified };
}

size_t Polygons::pointCount() const
Expand Down
2 changes: 2 additions & 0 deletions tests/LayerPlanTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ class LayerPlanTest : public testing::Test
settings->add("raft_interface_line_width", "0.402");
settings->add("raft_interface_speed", "52");
settings->add("raft_interface_thickness", "0.102");
settings->add("raft_interface_layers", "3");
settings->add("raft_surface_acceleration", "5003");
settings->add("raft_surface_jerk", "5.3");
settings->add("raft_surface_line_width", "0.403");
settings->add("raft_surface_speed", "53");
settings->add("raft_surface_thickness", "0.103");
settings->add("raft_surface_layers", "3");
settings->add("retraction_amount", "8");
settings->add("retraction_combing", "off");
settings->add("retraction_count_max", "30");
Expand Down
Loading