Skip to content

Commit e53e941

Browse files
committed
Properly set parent when changing operations list at once
CURA-12250
1 parent 6a74ae2 commit e53e941

File tree

11 files changed

+123
-71
lines changed

11 files changed

+123
-71
lines changed

include/operation_transformation/DirectTravelMoveGenerator.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Copyright (c) 2024 UltiMaker
22
// CuraEngine is released under the terms of the AGPLv3 or higher
33

4-
#ifndef PATHPROCESSING_DIRECTTRAVELMOVEGENERATOR_H
5-
#define PATHPROCESSING_DIRECTTRAVELMOVEGENERATOR_H
4+
#pragma once
65

76
#include "operation_transformation/TravelMoveGenerator.h"
87

@@ -16,5 +15,3 @@ class DirectTravelMoveGenerator : public TravelMoveGenerator
1615
};
1716

1817
} // namespace cura
19-
20-
#endif // PATHPROCESSING_DIRECTTRAVELMOVEGENERATOR_H

include/operation_transformation/LayerPlanTravelMovesInserter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LayerPlanTravelMovesInserter : public PrintOperationTransformer<LayerPlan>
2727

2828
void appendTravelMovesRecursively(const std::shared_ptr<PrintOperation>& operation, const SpeedDerivatives& speed);
2929

30-
const std::shared_ptr<PrintOperation> makeTravelMove(const Point3LL& start_position, const Point3LL& end_position, const SpeedDerivatives& speed);
30+
const std::shared_ptr<PrintOperation> makeTravelRoute(const Point3LL& start_position, const Point3LL& end_position, const SpeedDerivatives& speed);
3131

3232
private:
3333
std::vector<std::shared_ptr<TravelMoveGenerator>> generators_;

include/print_operation/ContinuousExtruderMoveSequence.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33

44
#pragma once
55

6-
#include "GCodePathConfig.h"
7-
#include "SpaceFillType.h"
86
#include "geometry/Point3LL.h"
97
#include "print_operation/PrintOperationSequence.h"
10-
#include "settings/types/Ratio.h"
11-
#include "utils/Coord_t.h"
128

139
namespace cura
1410
{

include/print_operation/PrintOperationSequence.h

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// Copyright (c) 2024 UltiMaker
22
// CuraEngine is released under the terms of the AGPLv3 or higher
33

4-
#ifndef PATHPLANNING_PRINTOPERATIONSEQUENCE_H
5-
#define PATHPLANNING_PRINTOPERATIONSEQUENCE_H
4+
#pragma once
5+
6+
#include <range/v3/algorithm/contains.hpp>
67

78
#include "geometry/Point3LL.h"
89
#include "operation_transformation/PrintOperationTransformer.h"
910
#include "print_operation/PrintOperation.h"
11+
#include "print_operation/PrintOperationPtr.h"
1012

1113
namespace cura
1214
{
@@ -56,41 +58,42 @@ class PrintOperationSequence : public PrintOperation, public std::enable_shared_
5658
* @return The first found operation, or a null ptr if none was found
5759
* @note This function can also be used to iterate over children by providing a search function that always returns false
5860
*/
59-
std::shared_ptr<PrintOperation> findOperation(
60-
const std::function<bool(const std::shared_ptr<PrintOperation>&)>& search_function,
61+
PrintOperationPtr findOperation(
62+
const std::function<bool(const PrintOperationPtr&)>& search_function,
6163
const SearchOrder search_order = SearchOrder::Forward,
6264
const std::optional<size_t> max_depth = SearchDepth::DirectChildren) const;
6365

6466
template<class OperationType>
6567
std::shared_ptr<OperationType>
6668
findOperationByType(const SearchOrder search_order = SearchOrder::Forward, const std::optional<size_t> max_depth = SearchDepth::DirectChildren) const;
6769

68-
const std::vector<std::shared_ptr<PrintOperation>>& getOperations() const noexcept;
69-
70-
std::vector<std::shared_ptr<PrintOperation>>& getOperations() noexcept;
70+
const std::vector<PrintOperationPtr>& getOperations() const noexcept;
7171

7272
template<class OperationType>
7373
std::vector<std::shared_ptr<OperationType>> getOperationsAs() noexcept;
7474

75-
void setOperations(std::vector<std::shared_ptr<PrintOperation>>& operations) noexcept;
75+
// void setOperations(std::vector<PrintOperationPtr>& operations) noexcept;
76+
77+
template<class OperationType>
78+
void setOperations(std::vector<std::shared_ptr<OperationType>>& operations) noexcept;
7679

7780
protected:
78-
void appendOperation(const std::shared_ptr<PrintOperation>& operation);
81+
void appendOperation(const PrintOperationPtr& operation);
7982

80-
void removeOperation(const std::shared_ptr<PrintOperation>& operation);
83+
void removeOperation(const PrintOperationPtr& operation);
8184

8285
template<class ChildType>
8386
void applyProcessorToOperationsRecursively(PrintOperationTransformer<ChildType>& processor);
8487

8588
private:
86-
std::vector<std::shared_ptr<PrintOperation>> operations_;
89+
std::vector<PrintOperationPtr> operations_;
8790
};
8891

8992
template<class OperationType>
9093
std::shared_ptr<OperationType> PrintOperationSequence::findOperationByType(const SearchOrder search_order, const std::optional<size_t> max_depth) const
9194
{
92-
std::shared_ptr<PrintOperation> found_operation = findOperation(
93-
[](const std::shared_ptr<PrintOperation>& operation)
95+
PrintOperationPtr found_operation = findOperation(
96+
[](const PrintOperationPtr& operation)
9497
{
9598
return static_cast<bool>(std::dynamic_pointer_cast<OperationType>(operation));
9699
},
@@ -126,6 +129,32 @@ std::vector<std::shared_ptr<OperationType>> PrintOperationSequence::getOperation
126129
return result;
127130
}
128131

132+
template<class OperationType>
133+
void PrintOperationSequence::setOperations(std::vector<std::shared_ptr<OperationType>>& operations) noexcept
134+
{
135+
for (const PrintOperationPtr& removed_operation : operations_)
136+
{
137+
if (! ranges::contains(operations, removed_operation))
138+
{
139+
removed_operation->setParent({});
140+
}
141+
}
142+
143+
for (const PrintOperationPtr& added_operation : operations)
144+
{
145+
if (! ranges::contains(operations_, added_operation))
146+
{
147+
added_operation->setParent(weak_from_this());
148+
}
149+
}
150+
151+
operations_.resize(operations.size());
152+
for (size_t index = 0; index < operations.size(); ++index)
153+
{
154+
operations_[index] = operations[index];
155+
}
156+
}
157+
129158
template<class ChildType>
130159
void PrintOperationSequence::applyProcessorToOperationsRecursively(PrintOperationTransformer<ChildType>& processor)
131160
{
@@ -144,5 +173,3 @@ void PrintOperationSequence::applyProcessorToOperationsRecursively(PrintOperatio
144173
}
145174

146175
} // namespace cura
147-
148-
#endif // PATHPLANNING_PRINTOPERATIONSEQUENCE_H

include/print_operation/TravelRoute.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
#ifndef PATHPLANNING_TRAVELROUTE_H
55
#define PATHPLANNING_TRAVELROUTE_H
66

7+
#include "path_planning/SpeedDerivatives.h"
78
#include "print_operation/ContinuousExtruderMoveSequence.h"
89

910
namespace cura
1011
{
1112

13+
enum class PrintFeatureType : unsigned char;
1214
class TravelMove;
15+
struct Velocity;
1316

1417
class TravelRoute : public ContinuousExtruderMoveSequence
1518
{

src/operation_transformation/DirectTravelMoveGenerator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "operation_transformation/DirectTravelMoveGenerator.h"
55

6+
#include "PrintFeatureType.h"
67
#include "print_operation/TravelMove.h"
78
#include "print_operation/TravelRoute.h"
89

src/operation_transformation/InsertOperationsProcessor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace cura
1010

1111
void InsertOperationsProcessor::process(PrintOperationSequence* operation)
1212
{
13-
std::vector<std::shared_ptr<PrintOperation>>& child_operations = operation->getOperations();
13+
std::vector<std::shared_ptr<PrintOperation>> child_operations = operation->getOperations();
1414
for (size_t index_first = 0; index_first < child_operations.size(); ++index_first)
1515
{
1616
const std::shared_ptr<PrintOperation>& operation_first = child_operations[index_first];
@@ -35,6 +35,8 @@ void InsertOperationsProcessor::process(PrintOperationSequence* operation)
3535
}
3636
}
3737
}
38+
39+
operation->setOperations(child_operations);
3840
}
3941

4042
} // namespace cura

src/operation_transformation/LayerPlanTravelMovesInserter.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,39 @@ void LayerPlanTravelMovesInserter::process(LayerPlan* layer_plan)
3131

3232
void LayerPlanTravelMovesInserter::appendTravelsMovesBetweenChildren(const std::shared_ptr<PrintOperationSequence>& sequence, const SpeedDerivatives& speed)
3333
{
34-
std::vector<std::shared_ptr<PrintOperation>>& child_operations = sequence->getOperations();
35-
for (size_t index_first = 0; index_first < child_operations.size(); ++index_first)
34+
std::vector<std::shared_ptr<PrintOperation>> child_operations = sequence->getOperations();
35+
for (size_t index_first = 0; index_first < child_operations.size() - 1; ++index_first)
3636
{
3737
const std::shared_ptr<PrintOperation>& operation_first = child_operations[index_first];
3838
std::optional<Point3LL> first_end_position = operation_first->findEndPosition();
39-
if (first_end_position.has_value())
39+
if (! first_end_position.has_value())
4040
{
41-
for (size_t index_second = index_first + 1; index_second < child_operations.size(); ++index_second)
41+
continue;
42+
}
43+
44+
for (size_t index_second = index_first + 1; index_second < child_operations.size(); ++index_second)
45+
{
46+
const std::shared_ptr<PrintOperation>& operation_second = child_operations[index_second];
47+
std::optional<Point3LL> second_start_position = operation_second->findStartPosition();
48+
if (! second_start_position.has_value())
4249
{
43-
const std::shared_ptr<PrintOperation>& operation_second = child_operations[index_second];
44-
std::optional<Point3LL> second_start_position = operation_second->findStartPosition();
45-
if (second_start_position.has_value())
46-
{
47-
if (const std::shared_ptr<PrintOperation> travel_move = makeTravelMove(first_end_position.value(), second_start_position.value(), speed))
48-
{
49-
child_operations.insert(std::next(child_operations.begin(), index_second), travel_move);
50-
index_first = index_second;
51-
}
52-
else
53-
{
54-
index_first = index_second - 1;
55-
}
56-
break;
57-
}
50+
continue;
5851
}
52+
53+
if (const std::shared_ptr<PrintOperation> travel_move = makeTravelRoute(first_end_position.value(), second_start_position.value(), speed))
54+
{
55+
child_operations.insert(std::next(child_operations.begin(), index_second), travel_move);
56+
index_first = index_second;
57+
}
58+
else
59+
{
60+
index_first = index_second - 1;
61+
}
62+
break;
5963
}
6064
}
65+
66+
sequence->setOperations(child_operations);
6167
}
6268

6369
void LayerPlanTravelMovesInserter::appendTravelMovesRecursively(const std::shared_ptr<PrintOperation>& operation, const SpeedDerivatives& speed)
@@ -73,7 +79,7 @@ void LayerPlanTravelMovesInserter::appendTravelMovesRecursively(const std::share
7379
}
7480
}
7581

76-
const std::shared_ptr<PrintOperation> LayerPlanTravelMovesInserter::makeTravelMove(const Point3LL& start_position, const Point3LL& end_position, const SpeedDerivatives& speed)
82+
const std::shared_ptr<PrintOperation> LayerPlanTravelMovesInserter::makeTravelRoute(const Point3LL& start_position, const Point3LL& end_position, const SpeedDerivatives& speed)
7783
{
7884
if (end_position != start_position)
7985
{

src/print_operation/ContinuousExtruderMoveSequence.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void ContinuousExtruderMoveSequence::reorderToEndWith(const std::shared_ptr<Extr
2828
{
2929
if (closed_)
3030
{
31-
std::vector<std::shared_ptr<PrintOperation>>& operations = getOperations();
31+
std::vector<std::shared_ptr<PrintOperation>> operations = getOperations();
3232
if (operations.size() > 1)
3333
{
3434
auto iterator = std::find(operations.begin(), operations.end(), extruder_move);
@@ -44,6 +44,8 @@ void ContinuousExtruderMoveSequence::reorderToEndWith(const std::shared_ptr<Extr
4444
{
4545
spdlog::warn("Unable to change sequence ordering because the given move is not part of the sequence");
4646
}
47+
48+
setOperations(operations);
4749
}
4850
}
4951
else
@@ -80,7 +82,8 @@ void ContinuousExtruderMoveSequence::reverse()
8082
}
8183
}
8284

83-
std::reverse(getOperations().begin(), getOperations().end());
85+
std::reverse(operations.begin(), operations.end());
86+
setOperations(operations);
8487
}
8588
}
8689

src/print_operation/PrintOperation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ std::shared_ptr<PrintOperationSequence>
4646

4747
return parent_ptr->findParent(search_function, warn_not_found);
4848
}
49-
else if (warn_not_found)
49+
50+
if (warn_not_found)
5051
{
5152
spdlog::warn("Couldn't find appropriate parent");
5253
}

0 commit comments

Comments
 (0)