|
| 1 | +// Copyright (c) 2026 Ultimaker B.V. |
| 2 | +// CuraEngine is released under the terms of the AGPLv3 or higher |
| 3 | + |
| 4 | +#ifndef INFILL_ORDER_OPTIMIZER_H |
| 5 | +#define INFILL_ORDER_OPTIMIZER_H |
| 6 | + |
| 7 | +#include <optional> |
| 8 | +#include <unordered_map> |
| 9 | + |
| 10 | +#include "utils/ExtrusionLine.h" |
| 11 | + |
| 12 | +namespace cura |
| 13 | +{ |
| 14 | + |
| 15 | +enum class EFillMethod; |
| 16 | +class OpenLinesSet; |
| 17 | +class Shape; |
| 18 | +class LayerPlan; |
| 19 | +class Settings; |
| 20 | +class SliceDataStorage; |
| 21 | +class SliceMeshStorage; |
| 22 | +struct MeshPathConfigs; |
| 23 | + |
| 24 | +/*! |
| 25 | + * Optimizer for infill lines ordering. When an end position is given, infill extrusions are optimized in a way that ending printing the infill |
| 26 | + * will always end as close as possible to the seam of the wall that is printed after. |
| 27 | + * We also try to consider the following rules, if applicable and possible: |
| 28 | + * - It is better to print the skin support after the rest, so that bridging lines have more printed material |
| 29 | + * to stick to. This is ensured by setting the initial ordering of the infill_parts_. |
| 30 | + * - We consider that infill walls are printed on the contour, thus they will always contain the closest location |
| 31 | + * to the nearest incoming seam., and will always be printed at last. |
| 32 | + */ |
| 33 | +class InfillOrderOptimizer |
| 34 | +{ |
| 35 | +public: |
| 36 | + /*! Types of infill areas. The order determines the preferred printing order. */ |
| 37 | + enum class InfillPartArea |
| 38 | + { |
| 39 | + Infill, |
| 40 | + SkinSupport, |
| 41 | + }; |
| 42 | + |
| 43 | + /*! Constructor for infill ordering optimizer. */ |
| 44 | + InfillOrderOptimizer(); |
| 45 | + |
| 46 | + /*! Add a set of open polylines to be printed consecutively. */ |
| 47 | + void addPart(InfillPartArea part_area, OpenLinesSet& lines); |
| 48 | + |
| 49 | + /*! Add a set of closed polylines to be printed consecutively. */ |
| 50 | + void addPart(InfillPartArea part_area, const Shape& polygons); |
| 51 | + |
| 52 | + /*! Add a set of extrusion lines to be printed consecutively. */ |
| 53 | + void addPart(InfillPartArea part_area, const std::vector<std::vector<VariableWidthLines>>& walls); |
| 54 | + |
| 55 | + /*! Process the paths ordering optimization. */ |
| 56 | + void optimize(const bool skin_support_interlace_lines, const std::optional<Point2LL>& near_end_location = std::nullopt); |
| 57 | + |
| 58 | + /*! |
| 59 | + * Adds the ordered infill parts to the given layer plan. |
| 60 | + * \return Whether anything was added to the layer plan. |
| 61 | + */ |
| 62 | + bool addToLayer( |
| 63 | + LayerPlan& layer_plan, |
| 64 | + const Settings& settings, |
| 65 | + const std::optional<Point2LL>& near_end_location, |
| 66 | + const EFillMethod infill_pattern, |
| 67 | + const MeshPathConfigs& mesh_config, |
| 68 | + const SliceDataStorage& storage, |
| 69 | + const SliceMeshStorage& mesh, |
| 70 | + const size_t extruder_nr, |
| 71 | + const coord_t start_move_inwards_length, |
| 72 | + const coord_t end_move_inwards_length, |
| 73 | + const Shape& infill_inner_contour, |
| 74 | + const coord_t skin_support_line_distance, |
| 75 | + const Shape& infill_below_skin, |
| 76 | + const AngleDegrees& skin_support_angle) const; |
| 77 | + |
| 78 | +private: |
| 79 | + /*! Types of infill print types. The order determines the preferred printing order. */ |
| 80 | + enum class InfillPartType |
| 81 | + { |
| 82 | + Lines, |
| 83 | + Polygons, |
| 84 | + ExtrusionLines, |
| 85 | + }; |
| 86 | + |
| 87 | + /*! Helper structure that holds an infill part to be printed as a whole. */ |
| 88 | + struct InfillPart |
| 89 | + { |
| 90 | + InfillPartArea area; |
| 91 | + InfillPartType type; |
| 92 | + union |
| 93 | + { |
| 94 | + const Shape* polygons; |
| 95 | + OpenLinesSet* lines; |
| 96 | + const std::vector<std::vector<VariableWidthLines>>* extrusion_lines; |
| 97 | + } paths; |
| 98 | + }; |
| 99 | + |
| 100 | +private: |
| 101 | + /*! |
| 102 | + * Calculates whether the given lines set has a vertex closer to the given location |
| 103 | + * @param line_set The lines set to be printed |
| 104 | + * @param location The target location that we should be close to |
| 105 | + * @param[in,out] closest_point The index of the line and vertex that is closest to the given location, which will be updated if we found a closer vertex |
| 106 | + * @param[in,out] closest_distance_squared The global closest distance to the given location, which will be updated if we found a closer vertex |
| 107 | + * @return Whether we have found a vertex in the lines set closer to the given location than the actual closest location |
| 108 | + */ |
| 109 | + template<class LinesSetType> |
| 110 | + bool isCloserTo(const LinesSetType& line_set, const Point2LL& location, std::optional<std::pair<size_t, size_t>>& closest_point, coord_t& closest_distance_squared); |
| 111 | + |
| 112 | + /*! Add the given part to the layer plan using the given settings */ |
| 113 | + void addToLayer( |
| 114 | + const InfillPart& part, |
| 115 | + LayerPlan& layer_plan, |
| 116 | + const Settings& settings, |
| 117 | + const std::optional<Point2LL>& near_start_location, |
| 118 | + const bool reverse_print_direction, |
| 119 | + const EFillMethod infill_pattern, |
| 120 | + const MeshPathConfigs& mesh_config, |
| 121 | + const SliceDataStorage& storage, |
| 122 | + const SliceMeshStorage& mesh, |
| 123 | + const size_t extruder_nr, |
| 124 | + const coord_t start_move_inwards_length, |
| 125 | + const coord_t end_move_inwards_length, |
| 126 | + const Shape& infill_inner_contour, |
| 127 | + const coord_t skin_support_line_distance, |
| 128 | + const Shape& infill_below_skin, |
| 129 | + const AngleDegrees& skin_support_angle) const; |
| 130 | + |
| 131 | + /*! Add the given infill lines to the layer plan using the given settings */ |
| 132 | + void addInfillLinesToLayer( |
| 133 | + const OpenLinesSet& lines, |
| 134 | + LayerPlan& layer_plan, |
| 135 | + const Settings& settings, |
| 136 | + const std::optional<Point2LL>& near_start_location, |
| 137 | + const bool reverse_print_direction, |
| 138 | + const EFillMethod infill_pattern, |
| 139 | + const MeshPathConfigs& mesh_config, |
| 140 | + const coord_t start_move_inwards_length, |
| 141 | + const coord_t end_move_inwards_length, |
| 142 | + const Shape& infill_inner_contour, |
| 143 | + const bool enable_travel_optimization, |
| 144 | + const Ratio& flow_ratio, |
| 145 | + const double fan_speed, |
| 146 | + const std::unordered_multimap<const Polyline*, const Polyline*>& order_requirements) const; |
| 147 | + |
| 148 | + /*! Add the given skin support lines to the layer plan using the given settings */ |
| 149 | + void addSkinSupportLinesToLayer( |
| 150 | + const OpenLinesSet& lines, |
| 151 | + LayerPlan& layer_plan, |
| 152 | + const Settings& settings, |
| 153 | + const std::optional<Point2LL>& near_start_location, |
| 154 | + const bool reverse_print_direction, |
| 155 | + const MeshPathConfigs& mesh_config, |
| 156 | + const coord_t skin_support_line_distance, |
| 157 | + const Shape& infill_below_skin, |
| 158 | + const AngleDegrees& skin_support_angle, |
| 159 | + const bool enable_travel_optimization, |
| 160 | + const Ratio& flow_ratio) const; |
| 161 | + |
| 162 | + /*! Indicates whether part1 should be printed by default before part2 */ |
| 163 | + static bool shouldPrintBefore(const InfillPart& part1, const InfillPart& part2); |
| 164 | + |
| 165 | +private: |
| 166 | + std::vector<InfillPart> infill_parts_; |
| 167 | +}; |
| 168 | +} // namespace cura |
| 169 | + |
| 170 | +#endif // INSET_ORDER_OPTIMIZER_H |
0 commit comments