Skip to content

Commit b157550

Browse files
committed
Apply suggestions from code review
CURA-11978
1 parent c833194 commit b157550

File tree

4 files changed

+121
-86
lines changed

4 files changed

+121
-86
lines changed

include/LayerPlan.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,27 @@ class LayerPlan : public NoCopy
10721072
std::optional<TravelAntiOozing>& retraction_amounts,
10731073
std::optional<TravelAntiOozing>& priming_amounts) const;
10741074

1075+
/*!
1076+
* Compute the anti-ooze amounts to be processed during stationary/Z-hop/travel steps for either a retraction or a priming
1077+
* @param travel_durations The pre-calculated travel durations
1078+
* @param gcode The gcode exporter
1079+
* @param path The raw travel path to be exported
1080+
* @param distance The retraction/prime distance to be applied
1081+
* @param during_travel_ratio The ratio of much of the retraction/prime should be processed during travel
1082+
* @param speed The retraction/prime speed
1083+
* @param extra_time_still The extra time to be allowed during stationary retraction/prime
1084+
* @param reversed Indicates if we should process the path forwards (retraction at the beginning) or backwards (prime at the end)
1085+
*/
1086+
static TravelAntiOozing computeAntiOozeAmount(
1087+
const TravelDurations& travel_durations,
1088+
const GCodeExport& gcode,
1089+
const GCodePath& path,
1090+
const double distance,
1091+
const Ratio& during_travel_ratio,
1092+
const Velocity& speed,
1093+
const Duration& extra_time_still,
1094+
const bool reversed);
1095+
10751096
/*!
10761097
* Write a single travel segment, taking care of the retraction and priming during travel
10771098
* @param travel_retraction_state The current travel retraction state, which may be updated

include/gcodeExport.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ class GCodeExport : public NoCopy
125125
/*!
126126
* Indicates whether this retraction actually has something to process
127127
*/
128-
inline bool has_retraction() const
128+
[[nodiscard]] bool has_retraction() const noexcept
129129
{
130-
return std::abs(diff_e) >= 0.000001;
130+
constexpr double threshold{ 1e-6 };
131+
return std::abs(diff_e) >= threshold;
131132
}
132133
};
133134

src/LayerPlan.cpp

Lines changed: 97 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ GCodePath* LayerPlan::getLatestPathWithConfig(
5858
{
5959
return &paths.back();
6060
}
61-
paths.emplace_back(GCodePath{ .z_offset = z_offset,
62-
.config = config,
63-
.mesh = current_mesh_,
64-
.space_fill_type = space_fill_type,
65-
.flow = flow,
66-
.width_factor = width_factor,
67-
.spiralize = spiralize,
68-
.speed_factor = speed_factor });
61+
paths.emplace_back(
62+
GCodePath{ .z_offset = z_offset,
63+
.config = config,
64+
.mesh = current_mesh_,
65+
.space_fill_type = space_fill_type,
66+
.flow = flow,
67+
.width_factor = width_factor,
68+
.spiralize = spiralize,
69+
.speed_factor = speed_factor });
6970

7071
GCodePath* ret = &paths.back();
7172
ret->skip_agressive_merge_hint = mode_skip_agressive_merge_;
@@ -2222,94 +2223,107 @@ void LayerPlan::computeAntiOozeAmounts(
22222223
extra_time_still_prime = prime_duration_during_travel - allowed_prime_duration_during_travel;
22232224
}
22242225

2225-
const auto compute_anti_ooze_amounts
2226-
= [&](const double distance, const Ratio& during_travel_ratio, const Velocity& speed, const Duration& extra_time_still, const bool reversed) -> TravelAntiOozing
2227-
{
2228-
TravelAntiOozing anti_oozing;
2226+
retraction_amounts = computeAntiOozeAmount(
2227+
travel_durations,
2228+
gcode,
2229+
path,
2230+
retract_distance,
2231+
retraction_config->retraction_config.retract_during_travel,
2232+
retraction_config->retraction_config.speed,
2233+
extra_time_still_retraction,
2234+
false);
22292235

2230-
// First, assume we have enough time to perform retraction and priming during travel as required by the settings
2231-
anti_oozing.amount_while_still = distance * (1.0 - during_travel_ratio);
2236+
priming_amounts = computeAntiOozeAmount(
2237+
travel_durations,
2238+
gcode,
2239+
path,
2240+
prime_distance,
2241+
retraction_config->retraction_config.prime_during_travel,
2242+
retraction_config->retraction_config.primeSpeed,
2243+
extra_time_still_prime,
2244+
true);
2245+
}
22322246

2233-
const double max_amount_during_z_hop = anti_oozing.amount_while_still + speed * travel_durations.z_hop;
2234-
anti_oozing.z_hop.amount = std::min(distance, max_amount_during_z_hop);
2247+
TravelAntiOozing LayerPlan::computeAntiOozeAmount(
2248+
const TravelDurations& travel_durations,
2249+
const GCodeExport& gcode,
2250+
const GCodePath& path,
2251+
const double distance,
2252+
const Ratio& during_travel_ratio,
2253+
const Velocity& speed,
2254+
const Duration& extra_time_still,
2255+
const bool reversed)
2256+
{
2257+
TravelAntiOozing anti_oozing;
22352258

2236-
anti_oozing.amount_while_travel = distance;
2259+
// First, assume we have enough time to perform retraction and priming during travel as required by the settings
2260+
anti_oozing.amount_while_still = distance * (1.0 - during_travel_ratio);
22372261

2238-
if (extra_time_still > 0)
2239-
{
2240-
// Now, if we need some extra time, adjust the stationary and z-hop amounts so that actual travel is not overloaded
2241-
const double missing_amount = speed * extra_time_still;
2242-
anti_oozing.amount_while_still += missing_amount;
2243-
anti_oozing.z_hop.amount += missing_amount;
2244-
}
2262+
const double max_amount_during_z_hop = anti_oozing.amount_while_still + speed * travel_durations.z_hop;
2263+
anti_oozing.z_hop.amount = std::min(distance, max_amount_during_z_hop);
22452264

2246-
if (travel_durations.z_hop > 0)
2247-
{
2248-
// If the retraction/prime should stop/start during z-hop move, compute the ratio of the move which should contain the retraction/prime
2249-
anti_oozing.z_hop.ratio = ((anti_oozing.z_hop.amount - anti_oozing.amount_while_still) / speed) / travel_durations.z_hop;
2250-
}
2265+
anti_oozing.amount_while_travel = distance;
22512266

2252-
// Now we are going to iterate over all the points of the travel move, including the start position, so create a temporary list and fill it appropriately
2253-
const Point2LL start_position = gcode.getPosition().toPoint2LL();
2254-
std::vector<Point3LL> points;
2255-
if (reversed)
2256-
{
2257-
points.insert(points.end(), path.points.rbegin(), path.points.rend());
2258-
points.push_back(start_position);
2259-
}
2260-
else
2261-
{
2262-
points = path.points;
2263-
points.insert(points.begin(), start_position);
2264-
}
2267+
if (extra_time_still > 0)
2268+
{
2269+
// Now, if we need some extra time, adjust the stationary and z-hop amounts so that actual travel is not overloaded
2270+
const double missing_amount = speed * extra_time_still;
2271+
anti_oozing.amount_while_still += missing_amount;
2272+
anti_oozing.z_hop.amount += missing_amount;
2273+
}
22652274

2266-
// Now loop over the segments of the travel move to find when and where the retraction/prime should stop/start
2267-
const Duration duration_during_travel = (anti_oozing.amount_while_travel - anti_oozing.z_hop.amount) / speed;
2268-
Duration travel_duration;
2269-
for (const auto& segment : points | ranges::views::sliding(2))
2270-
{
2271-
const Point2LL& segment_start = segment[0].toPoint2LL();
2272-
const Point2LL& segment_end = segment[1].toPoint2LL();
2275+
if (travel_durations.z_hop > 0)
2276+
{
2277+
// If the retraction/prime should stop/start during z-hop move, compute the ratio of the move which should contain the retraction/prime
2278+
anti_oozing.z_hop.ratio = ((anti_oozing.z_hop.amount - anti_oozing.amount_while_still) / speed) / travel_durations.z_hop;
2279+
}
22732280

2274-
const Duration segment_duration = (vSize(segment_end - segment_start) / path.config.getSpeed()) / 1000.0;
2275-
if (travel_duration + segment_duration >= (duration_during_travel - 0.001_s))
2281+
// Now we are going to iterate over all the points of the travel move, including the start position, so create a temporary list and fill it appropriately
2282+
const Point2LL start_position = gcode.getPosition().toPoint2LL();
2283+
std::vector<Point3LL> points;
2284+
if (reversed)
2285+
{
2286+
points.insert(points.end(), path.points.rbegin(), path.points.rend());
2287+
points.push_back(start_position);
2288+
}
2289+
else
2290+
{
2291+
points = path.points;
2292+
points.insert(points.begin(), start_position);
2293+
}
2294+
2295+
// Now loop over the segments of the travel move to find when and where the retraction/prime should stop/start
2296+
const Duration duration_during_travel = (anti_oozing.amount_while_travel - anti_oozing.z_hop.amount) / speed;
2297+
Duration travel_duration;
2298+
for (const auto& segment : points | ranges::views::sliding(2))
2299+
{
2300+
const Point2LL& segment_start = segment[0].toPoint2LL();
2301+
const Point2LL& segment_end = segment[1].toPoint2LL();
2302+
2303+
const Duration segment_duration = (vSize(segment_end - segment_start) / path.config.getSpeed()) / 1000.0;
2304+
if (travel_duration + segment_duration >= (duration_during_travel - 0.001_s))
2305+
{
2306+
// Retraction/prime ends/starts on this segment, so calculate the intermediate position and final/start amount
2307+
const double segment_ratio = (duration_during_travel - travel_duration) / segment_duration;
2308+
anti_oozing.segment_split_position = cura::lerp(segment_start, segment_end, segment_ratio).toPoint2LL();
2309+
if (reversed)
22762310
{
2277-
// Retraction/prime ends/starts on this segment, so calculate the intermediate position and final/start amount
2278-
const double segment_ratio = (duration_during_travel - travel_duration) / segment_duration;
2279-
anti_oozing.segment_split_position = cura::lerp(segment_start, segment_end, segment_ratio).toPoint2LL();
2280-
if (reversed)
2281-
{
2282-
anti_oozing.amount_by_segment.insert(anti_oozing.amount_by_segment.begin(), anti_oozing.z_hop.amount);
2283-
}
2284-
else
2285-
{
2286-
anti_oozing.amount_by_segment.push_back(anti_oozing.amount_while_travel);
2287-
}
2288-
break;
2311+
anti_oozing.amount_by_segment.insert(anti_oozing.amount_by_segment.begin(), anti_oozing.z_hop.amount);
22892312
}
2290-
2291-
// This segment fully contains the retraction/prime, set the proper intermediate amount and keep looping
2292-
anti_oozing.amount_by_segment.push_back(
2293-
std::lerp(anti_oozing.z_hop.amount, anti_oozing.amount_while_travel, (travel_duration + segment_duration) / duration_during_travel));
2294-
travel_duration += segment_duration;
2313+
else
2314+
{
2315+
anti_oozing.amount_by_segment.push_back(anti_oozing.amount_while_travel);
2316+
}
2317+
break;
22952318
}
22962319

2297-
return anti_oozing;
2298-
};
2299-
2300-
retraction_amounts = compute_anti_ooze_amounts(
2301-
retract_distance,
2302-
retraction_config->retraction_config.retract_during_travel,
2303-
retraction_config->retraction_config.speed,
2304-
extra_time_still_retraction,
2305-
false);
2320+
// This segment fully contains the retraction/prime, set the proper intermediate amount and keep looping
2321+
anti_oozing.amount_by_segment.push_back(
2322+
std::lerp(anti_oozing.z_hop.amount, anti_oozing.amount_while_travel, (travel_duration + segment_duration) / duration_during_travel));
2323+
travel_duration += segment_duration;
2324+
}
23062325

2307-
priming_amounts = compute_anti_ooze_amounts(
2308-
prime_distance,
2309-
retraction_config->retraction_config.prime_during_travel,
2310-
retraction_config->retraction_config.primeSpeed,
2311-
extra_time_still_prime,
2312-
true);
2326+
return anti_oozing;
23132327
}
23142328

23152329
void LayerPlan::writeTravelSegment(

src/gcodeExport.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,6 @@ void GCodeExport::writeZhop(Velocity speed /*= 0*/, const coord_t height, const
13611361

13621362
is_z_hopped_ = height;
13631363
const coord_t target_z = current_layer_z_ + is_z_hopped_;
1364-
// current_position_.z_ = current_layer_z_;
13651364
current_speed_ = speed;
13661365
*output_stream_ << "G1 F" << PrecisionedDouble{ 1, speed * 60 } << " Z" << MMtoStream{ target_z };
13671366
if (retraction_amounts.has_retraction())

0 commit comments

Comments
 (0)