mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-10-20 14:08:20 +00:00
* Shorten the travel between extrusion paths chain_and_reorder_extrusion_entities() builds the print order of the infill, ironing and support paths of a region from the nozzle position. It took the greedy chain, which leaves its longest joins for last; it now shortens it: - Up to eight paths, chain_segments_exact() finds the shortest chain over every order and allowed reversal, by dynamic programming over the subsets of paths, with a fixed start and a free end. - Longer chains are refined by local search: 2-opt, or-opt of up to three paths, and moving a run of any length next to the nozzle to the front, each tried only toward the 32 end points nearest a link, which come from a uniform grid. A move is applied only if it shortens the chain, so the result is never longer than the greedy one. - A nearest-next chain is built too, and refined and used instead when it is already shorter than the refined greedy chain, which happens when the greedy chain sweeps away from the nozzle and has to come back with a long jump. The shared greedy chaining also chains two to four segments exactly, which covers the islands of a layer and the loops of a perimeter collection. Paths that cannot reverse, such as Grid's, are only reordered, and collections that keep their order are left alone. The overload without a start point keeps the greedy chain. Co-authored-by: Tobias Gloth <tobias.gloth@3dsystems.com> * Order objects and islands along shorter tours Objects under print_order Default, and the tour of islands across objects, are visited in the same order on every layer, so chain_points_with_postprocessing() orders them as a closed tour. Up to ten points, tsp_exact_cycle() finds the shortest tour by dynamic programming over subsets, starting at the point nearest the start point when one is given. Larger sets add or-opt to the existing 2-opt and crossing removal: runs of up to three points move next to one of the eight points nearest an end of the run, reversed or not, and crossing removal runs last so no crossing is left. Snake ordering is unchanged; Best of strategies gets the shorter Default tour as a candidate. * Join infill lines into fewer paths Every separate infill path costs a travel to it, so the fill generators now leave fewer of them: - connect_infill() pairs the line ends along each wall in the order that joins the most lines: besides shortest arc first, it replays arcs of about the same length taken along each contour, forwards and backwards, including the last pass that joins neighboring open ends, and keeps the order that joins the most. Regular patterns no longer leave loose ends where the pairs along a wall fall out of step, and multiline copies print out and back. - Ends whose anchors would meet are joined along the wall instead, up to two anchors and a line width apart. - An unpaired end anchors toward the nearest open end of another path, where the next path usually starts, instead of the side with more free wall. With multiline infill, an anchor toward a copy of the same line stops short of the copy instead of printing over it. - Last, sparse infill lines whose ends are at most two line spacings apart are joined with a straight piece, shortest first, without closing a loop or crossing a line; with anchors off this is the only joining. Copies of neighboring multiline groups are joined at the same side, and lines that cannot reverse only join end to start. * Describe the travel optimization design How the extrusion chains, the object and island tours and the infill line joining shorten the travel, and why each part is built the way it is. --------- Co-authored-by: Tobias Gloth <tobias.gloth@3dsystems.com>
154 lines
5.9 KiB
C++
154 lines
5.9 KiB
C++
// Print-object ordering strategies and shared TSP post-processing utilities.
|
|
|
|
#ifndef slic3r_OrderingStrategies_hpp_
|
|
#define slic3r_OrderingStrategies_hpp_
|
|
|
|
#include "../Point.hpp"
|
|
#include <cstddef>
|
|
|
|
#ifndef SLIC3R_TEST_HARNESS
|
|
#include "../Print.hpp"
|
|
#endif
|
|
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace Slic3r { enum class PrintOrder; }
|
|
|
|
namespace Slic3r {
|
|
|
|
// --- Path improvement (operate on index vectors into `centers`) ---
|
|
|
|
// 2-opt improvement: reverses segments that reduce total cycle path length.
|
|
// Returns true if any improvement was made.
|
|
bool tsp_2opt_improve(std::vector<size_t>& path, const Points& centers, int max_passes = 10);
|
|
|
|
// Crossing removal: reverse any segment pair whose edges geometrically cross.
|
|
// Returns true if any crossing was removed.
|
|
bool tsp_remove_crossings(std::vector<size_t>& path, const Points& centers);
|
|
|
|
// The shortest cycle through the points, starting at point 0, by dynamic programming over subsets; for a few points only.
|
|
std::vector<size_t> tsp_exact_cycle(const Points& centers);
|
|
|
|
// Rotate the cycle so the closing edge (last -> first) is minimized.
|
|
void tsp_rotate_minimize_closing(std::vector<size_t>& path, const Points& centers);
|
|
|
|
// Total Euclidean path length of a cycle (including closing edge).
|
|
inline double tsp_cycle_path_length(const std::vector<size_t>& path, const Points& centers)
|
|
{
|
|
if (path.size() < 2) return 0.0;
|
|
double total = 0.0;
|
|
for (size_t i = 0; i < path.size(); ++i) {
|
|
size_t next = (i + 1) % path.size();
|
|
total += (centers[path[i]].cast<double>() - centers[path[next]].cast<double>()).norm();
|
|
}
|
|
return total;
|
|
}
|
|
|
|
// Maximum edge length of a cycle (including closing edge).
|
|
inline double tsp_max_edge_length(const std::vector<size_t>& path, const Points& centers)
|
|
{
|
|
if (path.size() < 2) return 0.0;
|
|
double mx = 0.0;
|
|
for (size_t i = 0; i < path.size(); ++i) {
|
|
size_t next = (i + 1) % path.size();
|
|
double d = (centers[path[i]].cast<double>() - centers[path[next]].cast<double>()).norm();
|
|
if (d > mx) mx = d;
|
|
}
|
|
return mx;
|
|
}
|
|
|
|
|
|
|
|
#ifndef SLIC3R_TEST_HARNESS
|
|
|
|
// --- Wrapper boilerplate ---
|
|
|
|
// Collect instance centers from PrintObjects, optionally pre-rotate to honour
|
|
// start_near, call a core algorithm, and map the result back to PrintInstance*.
|
|
template<typename CoreFn>
|
|
std::vector<const PrintInstance*> chain_instances_with_core(
|
|
const std::vector<const PrintObject*>& print_objects,
|
|
const Point* start_near,
|
|
CoreFn&& core_fn)
|
|
{
|
|
Points instance_centers;
|
|
std::vector<std::pair<size_t, size_t>> instances;
|
|
for (size_t i = 0; i < print_objects.size(); ++i) {
|
|
const PrintObject& object = *print_objects[i];
|
|
for (size_t j = 0; j < object.instances().size(); ++j) {
|
|
instance_centers.emplace_back(object.instances()[j].shift);
|
|
instances.emplace_back(i, j);
|
|
}
|
|
}
|
|
|
|
if (instance_centers.empty()) return {};
|
|
|
|
// If start_near is provided, pre-rotate so closest point is first.
|
|
if (start_near != nullptr) {
|
|
size_t best_start = 0;
|
|
double best_d2 = std::numeric_limits<double>::max();
|
|
for (size_t k = 0; k < instance_centers.size(); ++k) {
|
|
double d2 = (instance_centers[k].cast<double>() - start_near->cast<double>()).squaredNorm();
|
|
if (d2 < best_d2) { best_d2 = d2; best_start = k; }
|
|
}
|
|
std::rotate(instance_centers.begin(), instance_centers.begin() + best_start, instance_centers.end());
|
|
std::rotate(instances.begin(), instances.begin() + best_start, instances.end());
|
|
}
|
|
|
|
auto path = core_fn(instance_centers);
|
|
|
|
// Rotate the cycle so the first element is the best starting point.
|
|
// When start_near is provided, pick the point closest to it (preserving
|
|
// the pre-rotation). Otherwise minimise the closing edge.
|
|
if (start_near != nullptr && !path.empty()) {
|
|
// Pre-rotation already put the closest point at index 0.
|
|
// Find where index 0 appears in the path and rotate it to the front.
|
|
auto it = std::find(path.begin(), path.end(), size_t(0));
|
|
if (it != path.begin())
|
|
std::rotate(path.begin(), it, path.end());
|
|
} else {
|
|
tsp_rotate_minimize_closing(path, instance_centers);
|
|
}
|
|
|
|
std::vector<const PrintInstance*> out;
|
|
out.reserve(path.size());
|
|
for (size_t step : path) {
|
|
out.emplace_back(&print_objects[instances[step].first]->instances()[instances[step].second]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
#endif // SLIC3R_TEST_HARNESS
|
|
|
|
// --- Core algorithms (operate on raw Points, return index permutations) ---
|
|
|
|
// Snake ordering: row grouping + serpentine traversal + post-processing.
|
|
std::vector<size_t> snake_core(const Points& centers);
|
|
|
|
#ifndef SLIC3R_TEST_HARNESS
|
|
|
|
// --- Production wrappers ---
|
|
|
|
// Snake ordering.
|
|
std::vector<const PrintInstance*> chain_print_object_instances_snake(const std::vector<const PrintObject*>& print_objects, const Point* start_near);
|
|
std::vector<const PrintInstance*> chain_print_object_instances_snake(const Print& print);
|
|
|
|
// Best-of-strategies: run all strategies and return the shortest result.
|
|
// Primary: shortest total path; secondary tiebreaker: smallest max edge.
|
|
std::vector<const PrintInstance*> chain_print_object_instances_best_of(const std::vector<const PrintObject*>& print_objects, const Point* start_near);
|
|
std::vector<const PrintInstance*> chain_print_object_instances_best_of(const Print& print);
|
|
|
|
// Order raw points with the selected strategy, returning an index permutation. Island-level
|
|
// counterpart of the chain_print_object_instances_* helpers. The returned cycle starts at the
|
|
// point closest to start_near; orders without a dedicated strategy use nearest-neighbor chaining.
|
|
std::vector<size_t> order_points_with_strategy(const Points& points, PrintOrder print_order, const Point* start_near);
|
|
|
|
#endif // SLIC3R_TEST_HARNESS
|
|
|
|
} // namespace Slic3r
|
|
|
|
#endif /* slic3r_OrderingStrategies_hpp_ */
|