-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmatmill.cs
210 lines (165 loc) · 9.33 KB
/
matmill.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
using System;
using System.Collections.Generic;
using CamBam.CAD;
using CamBam.Geom;
using Geom;
namespace Matmill
{
public class Pocket_generator
{
private readonly Topographer _topo;
private double _general_tolerance = 0.001;
private double _tool_r = 1.5;
private double _margin = 0;
private double _max_ted = 3.0 * 0.4;
private double _min_ted = 3.0 * 0.1;
private Point2F _startpoint = Point2F.Undefined;
private RotationDirection _dir = RotationDirection.CW;
private bool _should_smooth_chords = false;
private bool _startpoint_is_a_hint = false;
//private bool _should_emit_debug_medial_axis = false;
private double _slice_leadin_angle = 3 * Math.PI / 180;
private double _slice_leadout_angle = 0.5 * Math.PI / 180;
private Vector2d _spiral_tangent = new Vector2d();
public double Tool_d { set { _tool_r = value / 2.0;}}
public double General_tolerance { set { _general_tolerance = value; } }
public double Margin { set { _margin = value; } }
public double Max_ted { set { _max_ted = value; } }
public double Min_ted { set { _min_ted = value; } }
public double Slice_leadin_angle { set { _slice_leadin_angle = value; } }
public double Slice_leadout_angle { set { _slice_leadout_angle = value; } }
public Point2F Startpoint { set { _startpoint = value; } }
public RotationDirection Mill_direction { set { _dir = value; } }
public bool Should_smooth_chords { set { _should_smooth_chords = value; }}
public bool Startpoint_is_a_hint { set { _startpoint_is_a_hint = value; }}
public Vector2F Spiral_tangent { set { _spiral_tangent = new Vector2d(value.X, value.Y); }}
private double _min_passable_mic_radius
{
get { return 0.1 * _tool_r; } // 5 % of tool diameter is seems to be ok
}
private double get_mic_radius(Point2F pt)
{
return _topo.Get_dist_to_wall(pt) - _tool_r - _margin;
}
private Sliced_path generate_path(Slice_sequence sequence)
{
Sliced_path_generator gen;
if (!_should_smooth_chords)
gen = new Sliced_path_generator(_general_tolerance);
else
gen = new Sliced_path_smooth_generator(_general_tolerance, 0.1 * _tool_r);
gen.Append_spiral(sequence.Root_slice.Center, sequence.Root_slice.End, _spiral_tangent, _max_ted, _tool_r, _dir == RotationDirection.Unknown ? RotationDirection.CCW : _dir);
gen.Append_slice_sequence(sequence);
return gen.Path;
}
private double radius_getter(Point2F pt)
{
double radius = get_mic_radius(pt);
if (radius < _min_passable_mic_radius) return 0;
return radius;
}
public Sliced_path run()
{
if (_dir == RotationDirection.Unknown && _should_smooth_chords)
throw new Exception("smooth chords are not allowed for the variable mill direction");
Logger.log("building medial axis");
Branch tree = new Branch(null);
bool is_ok = _topo.Build_medial_tree(tree, _tool_r / 10, _general_tolerance, _startpoint, _min_passable_mic_radius + _tool_r + _margin, _startpoint_is_a_hint);
if (! is_ok)
{
Logger.warn("failed to build tree");
return null;
}
Branch_slicer slicer = new Branch_slicer(_topo.Min, _topo.Max, _general_tolerance);
slicer.Slice_leadin_angle = _slice_leadin_angle;
slicer.Slice_leadout_angle = _slice_leadout_angle;
slicer.Get_radius = radius_getter;
Logger.log("generating slices");
Slice_sequence sequence = slicer.Run(tree, _tool_r, _max_ted, _min_ted, _dir);
Logger.log("generating path");
return generate_path(sequence);
}
public Pocket_generator(Polyline outline, Polyline[] islands)
{
_topo = new Topographer(outline, islands);
}
}
public class Engrave_generator
{
private const double TED_TOLERANCE_PERCENTAGE = 0.001; // 0.1 %
private const double FINAL_ALLOWED_TED_OVERSHOOT_PERCENTAGE = 0.03; // 3 %
private Polyline _poly;
private Topographer _topo;
private double _general_tolerance = 0.001;
private double _tool_r = 1.5;
private double _max_ted = 3.0 * 0.4;
private Point2F _startpoint = Point2F.Undefined;
private RotationDirection _dir = RotationDirection.CW;
private bool _should_smooth_chords = false;
private double _slice_leadin_angle = 3 * Math.PI / 180;
private double _slice_leadout_angle = 0.5 * Math.PI / 180;
private double _slice_radius = 1.5;
private Vector2d _spiral_tangent = new Vector2d();
public Vector2F Spiral_tangent { set { _spiral_tangent = new Vector2d(value.X, value.Y); } }
public double Tool_d { set { _tool_r = value / 2.0;}}
public double General_tolerance { set { _general_tolerance = value; } }
public double Max_ted { set { _max_ted = value; } }
public double Slice_radius { set { _slice_radius = value; } }
public double Slice_leadin_angle { set { _slice_leadin_angle = value; } }
public double Slice_leadout_angle { set { _slice_leadout_angle = value; } }
public RotationDirection Mill_direction { set { _dir = value; } }
public bool Should_smooth_chords { set { _should_smooth_chords = value; }}
private int find_optimal_slice(Slice parent, Point2F pt, ref Slice _candidate)
{
Slice s = new Slice(parent, pt, _slice_radius, RotationDirection.CCW, _tool_r, Point2F.Undefined);
if (s.Placement == Slice_placement.INSIDE_ANOTHER) return 1; // go right
if (s.Placement == Slice_placement.TOO_FAR) return -1; // go left
_candidate = s;
if (s.Max_ted > _max_ted) return -1; // overshoot, go left
if ((_max_ted - s.Max_ted) / _max_ted > TED_TOLERANCE_PERCENTAGE) return 1; // undershoot outside the strict TED tolerance, go right
return 0; // good slice inside the tolerance, stop search
}
// NOTE: funny way to calc it: try a lot of slices on a test line, approaching the best placement with the correct TED
private double calc_optimal_step()
{
Branch branch = new Branch(null);
branch.Add_point(new Point2F(0, 0));
branch.Add_point(new Point2F(0, _tool_r * 2));
Slice slice_a = new Slice(new Point2F(0, 0), _slice_radius, RotationDirection.CCW);
Slice slice_b = null;
double t = 0.0;
branch.Bisect(pt => find_optimal_slice(slice_a, pt, ref slice_b), ref t, _general_tolerance);
return slice_a.Center.DistanceTo(slice_b.Center);
}
private Sliced_path generate_path(Slice_sequence sequence)
{
Sliced_path_generator gen;
if (!_should_smooth_chords)
gen = new Sliced_path_generator(_general_tolerance);
else
gen = new Sliced_path_smooth_generator(_general_tolerance, 0.1 * _tool_r);
gen.Append_spiral(sequence.Root_slice.Center, sequence.Root_slice.End, _spiral_tangent, _max_ted, _tool_r, _dir == RotationDirection.Unknown ? RotationDirection.CCW : _dir);
gen.Append_slice_sequence(sequence);
return gen.Path;
}
public Sliced_path run()
{
if (_dir == RotationDirection.Unknown && _should_smooth_chords)
throw new Exception("smooth chords are not allowed for the variable mill direction");
_topo = new Topographer(_poly, new Polyline[] { });
double step = calc_optimal_step();
List<Point2F> slice_centers = _topo.Get_samples_exact(step);
Bypoint_slicer slicer = new Bypoint_slicer(_topo.Min, _topo.Max, _general_tolerance);
slicer.Slice_leadin_angle = _slice_leadin_angle;
slicer.Slice_leadout_angle = _slice_leadout_angle;
Logger.log("generating slices");
Slice_sequence sequence = slicer.Run(slice_centers, _tool_r, _slice_radius, _dir);
Logger.log("generating path");
return generate_path(sequence);
}
public Engrave_generator(Polyline poly)
{
_poly = poly;
}
}
}