-
Notifications
You must be signed in to change notification settings - Fork 5
/
path_gen.cs
285 lines (231 loc) · 8.94 KB
/
path_gen.cs
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
using System;
using System.Collections.Generic;
using CamBam.Geom;
using CamBam.CAD;
using Geom;
namespace Matmill
{
public enum Sliced_path_item_type
{
SLICE,
SPIRAL,
CHORD,
SMOOTH_CHORD,
GUIDE,
SLICE_SHORTCUT,
RETURN_TO_BASE,
DEBUG_MEDIAL_AXIS,
}
public class Sliced_path : List<Sliced_path_item>
{
public object Extension;
}
public class Sliced_path_item: Polyline
{
public readonly Sliced_path_item_type Item_type;
public Sliced_path_item(Sliced_path_item_type type) : base()
{
Item_type = type;
}
public Sliced_path_item(Sliced_path_item_type type, int i) : base(i)
{
Item_type = type;
}
public Sliced_path_item(Sliced_path_item_type type, Polyline p) : base(p)
{
Item_type = type;
}
public void Add(Point2F pt)
{
base.Add(new Point3F(pt.X, pt.Y, 0));
}
public void Add(Curve2d curve)
{
foreach (Point2F pt in curve.Points)
this.Add(pt);
}
public void Add(Biarc2d biarc, double tolerance)
{
if (biarc.Seg1 is Arc2F)
this.Add((Arc2F)biarc.Seg1, tolerance);
else
this.Add((Line2F)biarc.Seg1, tolerance);
if (biarc.Seg2 is Arc2F)
this.Add((Arc2F)biarc.Seg2, tolerance);
else
this.Add((Line2F)biarc.Seg2, tolerance);
}
}
class Sliced_path_generator
{
protected double _general_tolerance;
public Sliced_path Path = new Sliced_path();
protected Slice _last_slice = null;
protected void append_slice(Slice slice)
{
// emit segments
for (int segidx = 0; segidx < slice.Segments.Count; segidx++)
{
// connect segments
if (segidx > 0)
{
Sliced_path_item shortcut = new Sliced_path_item(Sliced_path_item_type.SLICE_SHORTCUT);
shortcut.Add(slice.Segments[segidx - 1].P2);
shortcut.Add(slice.Segments[segidx].P1);
Path.Add(shortcut);
}
Sliced_path_item arc = new Sliced_path_item(Sliced_path_item_type.SLICE);
arc.Add(slice.Segments[segidx], _general_tolerance);
Path.Add(arc);
}
_last_slice = slice;
}
protected Sliced_path_item connect_slices(Slice dst, Slice src)
{
Sliced_path_item path = new Sliced_path_item(Sliced_path_item_type.CHORD);
path.Add(src.End);
path.Add(dst.Start);
return path;
}
public void Append_spiral(Point2F start, Point2F end, Vector2d start_tangent, double ted, double tool_r, RotationDirection dir)
{
Sliced_path_item spiral = new Sliced_path_item(Sliced_path_item_type.SPIRAL);
double spacing = Spiral_generator.Calc_reverse_spacing(end.DistanceTo(start), tool_r, ted, _general_tolerance);
foreach (Biarc2d biarc in Spiral_generator.Gen_archimedean_spiral(start, end, start_tangent, spacing, dir))
spiral.Add(biarc, _general_tolerance);
Path.Add(spiral);
}
public void Append_root_slice(Slice slice)
{
append_slice(slice);
}
public virtual void Append_slice(Slice slice, List<Point2F> guide)
{
if (_last_slice == null)
throw new Exception("attempt to install slice without the root slice");
if (guide == null)
{
Path.Add(connect_slices(slice, _last_slice));
}
else
{
Sliced_path_item p = new Sliced_path_item(Sliced_path_item_type.GUIDE);
p.Add(_last_slice.End);
foreach (Point2F pt in guide)
p.Add(pt);
p.Add(slice.Start);
Path.Add(p);
}
append_slice(slice);
}
public void Append_return_to_base(List<Point2F> exit)
{
Sliced_path_item p = new Sliced_path_item(Sliced_path_item_type.RETURN_TO_BASE);
p.Add(_last_slice.End);
foreach (Point2F pt in exit)
p.Add(pt);
Path.Add(p);
}
public void Append_slice_sequence(Slice_sequence sequence)
{
this.Append_root_slice(sequence.Root_slice);
for (int i = 1; i < sequence.Slices.Count; i++)
{
Slice s = sequence.Slices[i];
this.Append_slice(s, s.Guide);
}
}
public Sliced_path_generator(double general_tolerance)
{
_general_tolerance = general_tolerance;
}
}
class Sliced_path_smooth_generator : Sliced_path_generator
{
double _min_arc_len;
private bool is_biarc_inside_ball(Biarc2d biarc, Circle2F ball)
{
if (biarc.Pm.DistanceTo(ball.Center) > ball.Radius)
return false;
Point2F start = biarc.P1;
Point2F end = biarc.P2;
Line2F insects;
if (biarc.Seg1 is Line2F)
insects = ball.LineIntersect((Line2F)biarc.Seg1);
else
insects = ((Arc2F)biarc.Seg1).CircleIntersect(ball);
if ((!insects.p1.IsUndefined) && insects.p1.DistanceTo(start) < _general_tolerance)
insects.p1 = Point2F.Undefined;
if ((!insects.p2.IsUndefined) && insects.p2.DistanceTo(start) < _general_tolerance)
insects.p2 = Point2F.Undefined;
if (!(insects.p1.IsUndefined && insects.p2.IsUndefined))
return false;
if (biarc.Seg2 is Line2F)
insects = ball.LineIntersect((Line2F)biarc.Seg2);
else
insects = ((Arc2F)biarc.Seg2).CircleIntersect(ball);
if ((!insects.p1.IsUndefined) && insects.p1.DistanceTo(end) < _general_tolerance)
insects.p1 = Point2F.Undefined;
if ((!insects.p2.IsUndefined) && insects.p2.DistanceTo(end) < _general_tolerance)
insects.p2 = Point2F.Undefined;
if (!(insects.p1.IsUndefined && insects.p2.IsUndefined))
return false;
return true;
}
private Sliced_path_item connect_slices_with_biarc(Slice dst, Slice src)
{
Point2F start = src.End;
Point2F end = dst.Start;
// unit normals to points
Vector2d vn_start = new Vector2d(src.Center, start).Unit();
Vector2d vn_end = new Vector2d(dst.Center, end).Unit();
// tangents to points
Vector2d vt_start;
Vector2d vt_end;
if (src.Dir == RotationDirection.CW)
vt_start = new Vector2d(vn_start.Y, -vn_start.X);
else
vt_start = new Vector2d(-vn_start.Y, vn_start.X);
if (dst.Dir == RotationDirection.CW)
vt_end = new Vector2d(vn_end.Y, -vn_end.X);
else
vt_end = new Vector2d(-vn_end.Y, vn_end.X);
Biarc2d biarc = new Biarc2d(start, vt_start, end, vt_end);
if (!is_biarc_inside_ball(biarc, src.Ball))
return null;
Sliced_path_item path = new Sliced_path_item(Sliced_path_item_type.SMOOTH_CHORD);
path.Add(biarc, _general_tolerance);
return path;
}
private Sliced_path_item smooth_connect_slices(Slice dst, Slice src)
{
Sliced_path_item path = null;
// do not emit biarcs if distance is too small
if (src.End.DistanceTo(dst.Start) > _min_arc_len)
{
path = connect_slices_with_biarc(dst, src);
if (path == null)
Logger.warn("biarc is outside the slice, replacing with chord");
}
return path;
}
public override void Append_slice(Slice slice, List<Point2F> guide)
{
if (guide == null)
{
Sliced_path_item biarc = smooth_connect_slices(slice, _last_slice);
if (biarc != null)
{
Path.Add(biarc);
append_slice(slice);
return;
}
}
base.Append_slice(slice, guide);
}
public Sliced_path_smooth_generator(double general_tolerance, double min_arc_len) : base(general_tolerance)
{
_min_arc_len = min_arc_len;
}
}
}