-
-
Notifications
You must be signed in to change notification settings - Fork 893
/
Copy pathLinesSet.cpp
358 lines (323 loc) · 11.8 KB
/
LinesSet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (c) 2024 UltiMaker
// CuraEngine is released under the terms of the AGPLv3 or higher.
#include "geometry/LinesSet.h"
#include <cassert>
#include <numeric>
#include "geometry/ClosedLinesSet.h"
#include "geometry/OpenLinesSet.h"
#include "geometry/OpenPolyline.h"
#include "geometry/Polygon.h"
#include "geometry/Shape.h"
namespace cura
{
template<class LineType>
bool LinesSet<LineType>::checkAdd(const LineType& line, CheckNonEmptyParam check_non_empty)
{
switch (check_non_empty)
{
case CheckNonEmptyParam::EvenIfEmpty:
return true;
case CheckNonEmptyParam::OnlyIfNotEmpty:
return ! line.empty();
case CheckNonEmptyParam::OnlyIfValid:
return line.isValid();
}
return false;
}
template<class LineType>
void LinesSet<LineType>::push_back(const LineType& line, CheckNonEmptyParam check_non_empty)
{
if (checkAdd(line, check_non_empty))
{
lines_.push_back(line);
}
}
template<class LineType>
void LinesSet<LineType>::push_back(LineType&& line, CheckNonEmptyParam check_non_empty)
{
if (checkAdd(line, check_non_empty))
{
lines_.push_back(std::move(line));
}
}
template<class LineType>
template<class OtherLineType>
void LinesSet<LineType>::push_back(LinesSet<OtherLineType>&& lines_set)
{
reserve(size() + lines_set.size());
for (OtherLineType& line : lines_set)
{
emplace_back(std::move(line));
}
}
template<class LineType>
size_t LinesSet<LineType>::pointCount() const
{
return std::accumulate(
lines_.begin(),
lines_.end(),
0ULL,
[](size_t total, const LineType& line)
{
return total + line.size();
});
}
template<>
void OpenLinesSet::addSegment(const Point2LL& from, const Point2LL& to)
{
lines_.emplace_back(std::initializer_list<Point2LL>{ from, to });
}
template<class LineType>
void LinesSet<LineType>::removeAt(size_t index)
{
if (lines_.size() == 1)
{
lines_.clear();
}
else if (lines_.size() > 1)
{
assert(index < lines_.size());
if (index < lines_.size() - 1)
{
lines_[index] = std::move(lines_.back());
}
lines_.resize(lines_.size() - 1);
}
}
template<class LineType>
void LinesSet<LineType>::splitIntoSegments(OpenLinesSet& result) const
{
for (const LineType& line : lines_)
{
line.splitIntoSegments(result);
}
}
template<class LineType>
OpenLinesSet LinesSet<LineType>::splitIntoSegments() const
{
OpenLinesSet result;
for (const LineType& line : lines_)
{
line.splitIntoSegments(result);
}
return result;
}
template<class LineType>
coord_t LinesSet<LineType>::length() const
{
return std::accumulate(
lines_.begin(),
lines_.end(),
0LL,
[](coord_t total, const LineType& line)
{
return total += line.length();
});
}
template<class LineType>
Shape LinesSet<LineType>::createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const
{
return offset(outer_offset).difference(offset(-inner_offset));
}
template<class LineType>
void LinesSet<LineType>::translate(const Point2LL& delta)
{
if (delta.X != 0 || delta.Y != 0)
{
for (LineType& line : getLines())
{
line.translate(delta);
}
}
}
template<>
Shape LinesSet<ClosedPolyline>::offset(coord_t distance, ClipperLib::JoinType join_type, double miter_limit) const
{
if (empty())
{
return {};
}
if (distance == 0)
{
Shape result;
for (const ClosedPolyline& line : getLines())
{
result.emplace_back(line.getPoints(), line.isExplicitelyClosed());
}
return result;
}
ClipperLib::Paths ret;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);
addPaths(clipper, join_type, ClipperLib::etClosedLine);
clipper.MiterLimit = miter_limit;
clipper.Execute(ret, static_cast<double>(distance));
return Shape{ std::move(ret) };
}
template<>
Shape LinesSet<Polygon>::offset(coord_t distance, ClipperLib::JoinType join_type, double miter_limit) const
{
if (empty())
{
return {};
}
if (distance == 0)
{
return { getLines() };
}
ClipperLib::Paths ret;
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);
Shape(getLines()).unionPolygons().addPaths(clipper, join_type, ClipperLib::etClosedPolygon);
clipper.MiterLimit = miter_limit;
clipper.Execute(ret, static_cast<double>(distance));
return Shape{ std::move(ret) };
}
template<>
Shape OpenLinesSet::offset(coord_t distance, ClipperLib::JoinType join_type, double miter_limit) const
{
if (empty() || distance == 0)
{
return {};
}
ClipperLib::ClipperOffset clipper(miter_limit, 10.0);
const ClipperLib::EndType end_type{ join_type == ClipperLib::jtMiter ? ClipperLib::etOpenSquare : ClipperLib::etOpenRound };
addPaths(clipper, join_type, end_type);
clipper.MiterLimit = miter_limit;
ClipperLib::Paths result_paths;
clipper.Execute(result_paths, static_cast<double>(distance));
return Shape{ std::move(result_paths) };
}
template<class LineType>
void LinesSet<LineType>::removeDegenerateVerts()
{
for (size_t poly_idx = 0; poly_idx < lines_.size(); poly_idx++)
{
LineType& poly = lines_[poly_idx];
const bool for_polyline = (dynamic_cast<OpenPolyline*>(&poly) != nullptr);
ClipperLib::Path result;
auto is_degenerate = [](const Point2LL& last, const Point2LL& now, const Point2LL& next)
{
Point2LL last_line = now - last;
Point2LL next_line = next - now;
return dot(last_line, next_line) == -1 * vSize(last_line) * vSize(next_line);
};
// With polylines, skip the first and last vertex.
const size_t start_vertex = for_polyline ? 1 : 0;
const size_t end_vertex = for_polyline ? poly.size() - 1 : poly.size();
for (size_t i = 0; i < start_vertex; ++i)
{
result.push_back(poly[i]); // Add everything before the start vertex.
}
bool is_changed = false;
for (size_t idx = start_vertex; idx < end_vertex; idx++)
{
const Point2LL& last = (result.size() == 0) ? poly.back() : result.back();
if (idx + 1 >= poly.size() && result.size() == 0)
{
break;
}
const Point2LL& next = (idx + 1 >= poly.size()) ? result[0] : poly[idx + 1];
if (is_degenerate(last, poly[idx], next))
{ // lines are in the opposite direction
// don't add vert to the result
is_changed = true;
while (result.size() > 1 && is_degenerate(result[result.size() - 2], result.back(), next))
{
result.pop_back();
}
}
else
{
result.push_back(poly[idx]);
}
}
for (size_t i = end_vertex; i < poly.size(); ++i)
{
result.push_back(poly[i]); // Add everything after the end vertex.
}
if (is_changed)
{
if (for_polyline || result.size() > 2)
{
poly.setPoints(std::move(result));
}
else
{
removeAt(poly_idx);
poly_idx--; // effectively the next iteration has the same poly_idx (referring to a new poly which is not yet processed)
}
}
}
}
template<class LineType>
template<class OtherLineLine>
void LinesSet<LineType>::addPath(ClipperLib::Clipper& clipper, const OtherLineLine& line, ClipperLib::PolyType poly_typ) const
{
// In this context, the "Closed" argument means "Is a surface" so it should be only
// true for actual filled polygons. Closed polylines are to be treated as lines here.
if constexpr (std::is_same<OtherLineLine, Polygon>::value)
{
clipper.AddPath(line.getPoints(), poly_typ, true);
}
else
{
clipper.AddPath(line.getPoints(), poly_typ, false);
}
}
template<class LineType>
void LinesSet<LineType>::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType poly_typ) const
{
for (const LineType& line : getLines())
{
addPath(clipper, line, poly_typ);
}
}
template<class LineType>
void LinesSet<LineType>::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType joint_type, ClipperLib::EndType endType) const
{
for (const LineType& line : getLines())
{
clipper.AddPath(line.getPoints(), joint_type, endType);
}
}
template size_t OpenLinesSet::pointCount() const;
template void OpenLinesSet::removeAt(size_t index);
template void OpenLinesSet::splitIntoSegments(OpenLinesSet& result) const;
template OpenLinesSet OpenLinesSet::splitIntoSegments() const;
template coord_t OpenLinesSet::length() const;
template Shape OpenLinesSet::createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void OpenLinesSet::translate(const Point2LL& delta);
template void OpenLinesSet::removeDegenerateVerts();
template void OpenLinesSet::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void OpenLinesSet::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void OpenLinesSet::push_back(const OpenPolyline& line, CheckNonEmptyParam checkNonEmpty);
template void OpenLinesSet::push_back(OpenPolyline&& line, CheckNonEmptyParam checkNonEmpty);
template void OpenLinesSet::push_back(OpenLinesSet&& lines_set);
template size_t ClosedLinesSet::pointCount() const;
template void ClosedLinesSet::removeAt(size_t index);
template void ClosedLinesSet::splitIntoSegments(OpenLinesSet& result) const;
template OpenLinesSet ClosedLinesSet::splitIntoSegments() const;
template coord_t ClosedLinesSet::length() const;
template Shape ClosedLinesSet::createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void ClosedLinesSet::translate(const Point2LL& delta);
template void ClosedLinesSet::removeDegenerateVerts();
template void ClosedLinesSet::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void ClosedLinesSet::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void ClosedLinesSet::push_back(const ClosedPolyline& line, CheckNonEmptyParam checkNonEmpty);
template void ClosedLinesSet::push_back(ClosedPolyline&& line, CheckNonEmptyParam checkNonEmpty);
template void ClosedLinesSet::push_back(ClosedLinesSet&& lines_set);
template void ClosedLinesSet::push_back(LinesSet<Polygon>&& lines_set);
template size_t LinesSet<Polygon>::pointCount() const;
template void LinesSet<Polygon>::removeAt(size_t index);
template void LinesSet<Polygon>::splitIntoSegments(OpenLinesSet& result) const;
template OpenLinesSet LinesSet<Polygon>::splitIntoSegments() const;
template coord_t LinesSet<Polygon>::length() const;
template Shape LinesSet<Polygon>::createTubeShape(const coord_t inner_offset, const coord_t outer_offset) const;
template void LinesSet<Polygon>::translate(const Point2LL& delta);
template void LinesSet<Polygon>::removeDegenerateVerts();
template void LinesSet<Polygon>::addPaths(ClipperLib::Clipper& clipper, ClipperLib::PolyType PolyTyp) const;
template void LinesSet<Polygon>::addPaths(ClipperLib::ClipperOffset& clipper, ClipperLib::JoinType jointType, ClipperLib::EndType endType) const;
template void LinesSet<Polygon>::push_back(const Polygon& line, CheckNonEmptyParam checkNonEmpty);
template void LinesSet<Polygon>::push_back(Polygon&& line, CheckNonEmptyParam checkNonEmpty);
template void LinesSet<Polygon>::push_back(LinesSet<Polygon>&& lines_set);
template void LinesSet<Polygon>::addPath(ClipperLib::Clipper& clipper, const Polygon& line, ClipperLib::PolyType poly_typ) const;
} // namespace cura