Skip to content

Commit 197dc7d

Browse files
committed
Use standard subranges features
CURA-12528
1 parent 82d3edf commit 197dc7d

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/unwrap.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,51 +55,42 @@ std::vector<Vector> calculateProjectionNormals(const std::vector<FaceData>& face
5555
return &face_data;
5656
});
5757

58-
using FaceDataIterator = std::vector<const FaceData*>::iterator;
59-
struct FacesRange
60-
{
61-
FaceDataIterator begin;
62-
FaceDataIterator end;
63-
};
64-
6558
// The unprocessed_faces is a sub-range of the faces list, that contains all the faces that have not been assigned to a group yet.
66-
FacesRange unprocessed_faces{ .begin = faces_to_process.begin(), .end = faces_to_process.end() };
59+
auto unprocessed_faces = ranges::subrange(faces_to_process);
6760

6861
while (true)
6962
{
7063
// Get all the faces that belong to the group of the current projection normal,
71-
// by placing them at the beginning of the unprocessed faces
72-
FacesRange current_faces_group_range = unprocessed_faces;
73-
current_faces_group_range.end = std::partition(
74-
unprocessed_faces.begin,
75-
unprocessed_faces.end,
64+
// by placing them at the end of the unprocessed faces
65+
auto current_faces_group = std::ranges::partition(
66+
unprocessed_faces,
7667
[&project_normal, &group_angle_limit_half_cos](const FaceData* face_data)
7768
{
78-
return face_data->normal.dot(*project_normal) > group_angle_limit_half_cos;
69+
return face_data->normal.dot(*project_normal) <= group_angle_limit_half_cos;
7970
});
8071

8172
// All the faces placed to the current group are now no more in the unprocessed faces
82-
unprocessed_faces.begin = current_faces_group_range.end;
73+
unprocessed_faces = ranges::subrange(unprocessed_faces.begin(), current_faces_group.begin());
8374

8475
// Sum all the normals of the current faces group to get the average direction
8576
Vector summed_normals = std::accumulate(
86-
current_faces_group_range.begin,
87-
current_faces_group_range.end,
77+
current_faces_group.begin(),
78+
current_faces_group.end(),
8879
Vector(),
8980
[](const Vector& normal, const FaceData* face_data)
9081
{
9182
return normal + face_data->normal;
9283
});
93-
if (summed_normals.normalize())
84+
if (summed_normals.normalize()) [[likely]]
9485
{
9586
projection_normals.push_back(summed_normals);
9687
}
9788

9889
// For the next iteration, try to find the most different remaining normal from all generated normals
9990
float best_outlier_angle = std::numeric_limits<float>::max();
100-
FaceDataIterator best_outlier_face = faces_to_process.end();
91+
auto best_outlier_face = faces_to_process.end();
10192

102-
for (auto iterator = unprocessed_faces.begin; iterator != unprocessed_faces.end; ++iterator)
93+
for (auto iterator = unprocessed_faces.begin(); iterator != unprocessed_faces.end(); ++iterator)
10394
{
10495
float face_best_angle = std::numeric_limits<float>::lowest();
10596
for (const Vector& projection_normal : projection_normals)
@@ -120,8 +111,9 @@ std::vector<Vector> calculateProjectionNormals(const std::vector<FaceData>& face
120111
project_normal = &(*best_outlier_face)->normal;
121112

122113
// Remove the faces from the unprocessed faces
123-
std::iter_swap(best_outlier_face, unprocessed_faces.begin);
124-
++unprocessed_faces.begin;
114+
const auto last_position = std::prev(unprocessed_faces.end());
115+
std::iter_swap(best_outlier_face, last_position);
116+
unprocessed_faces = ranges::subrange(unprocessed_faces.begin(), last_position);
125117
}
126118
else if (! projection_normals.empty())
127119
{

0 commit comments

Comments
 (0)