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
use std::iter;
use fj_math::{Circle, Point, Scalar, Sign};
use crate::geometry::path::{GlobalPath, SurfacePath};
use super::{Approx, Tolerance};
impl Approx for (SurfacePath, RangeOnPath) {
type Approximation = Vec<(Point<1>, Point<2>)>;
type Cache = ();
fn approx_with_cache(
self,
tolerance: impl Into<Tolerance>,
(): &mut Self::Cache,
) -> Self::Approximation {
let (path, range) = self;
match path {
SurfacePath::Circle(circle) => {
approx_circle(&circle, range, tolerance.into())
}
SurfacePath::Line(_) => vec![],
}
}
}
impl Approx for (GlobalPath, RangeOnPath) {
type Approximation = Vec<(Point<1>, Point<3>)>;
type Cache = ();
fn approx_with_cache(
self,
tolerance: impl Into<Tolerance>,
(): &mut Self::Cache,
) -> Self::Approximation {
let (path, range) = self;
match path {
GlobalPath::Circle(circle) => {
approx_circle(&circle, range, tolerance.into())
}
GlobalPath::Line(_) => vec![],
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct RangeOnPath {
pub boundary: [Point<1>; 2],
}
impl RangeOnPath {
pub fn reverse(self) -> Self {
let [a, b] = self.boundary;
Self { boundary: [b, a] }
}
}
impl<T> From<[T; 2]> for RangeOnPath
where
T: Into<Point<1>>,
{
fn from(boundary: [T; 2]) -> Self {
let boundary = boundary.map(Into::into);
Self { boundary }
}
}
fn approx_circle<const D: usize>(
circle: &Circle<D>,
range: impl Into<RangeOnPath>,
tolerance: Tolerance,
) -> Vec<(Point<1>, Point<D>)> {
let range = range.into();
let params = PathApproxParams::for_circle(circle, tolerance);
let mut points = Vec::new();
for point_curve in params.points(range) {
let point_global = circle.point_from_circle_coords(point_curve);
points.push((point_curve, point_global));
}
points
}
struct PathApproxParams {
increment: Scalar,
}
impl PathApproxParams {
pub fn for_circle<const D: usize>(
circle: &Circle<D>,
tolerance: impl Into<Tolerance>,
) -> Self {
let radius = circle.a().magnitude();
let num_vertices_to_approx_full_circle = Scalar::max(
Scalar::PI
/ (Scalar::ONE - (tolerance.into().inner() / radius)).acos(),
3.,
)
.ceil();
let increment = Scalar::TAU / num_vertices_to_approx_full_circle;
Self { increment }
}
pub fn increment(&self) -> Scalar {
self.increment
}
pub fn points(
&self,
range: impl Into<RangeOnPath>,
) -> impl Iterator<Item = Point<1>> + '_ {
let range = range.into();
let [a, b] = range.boundary.map(|point| point.t / self.increment());
let direction = (b - a).sign();
let [min, max] = if a < b { [a, b] } else { [b, a] };
let min = min.floor() + 1.;
let max = max.ceil() - 1.;
let [start, end] = match direction {
Sign::Negative => [max, min],
Sign::Positive | Sign::Zero => [min, max],
};
let mut i = start;
iter::from_fn(move || {
let is_finished = match direction {
Sign::Negative => i < end,
Sign::Positive | Sign::Zero => i > end,
};
if is_finished {
return None;
}
let t = self.increment() * i;
i += direction.to_scalar();
Some(Point::from([t]))
})
}
}
#[cfg(test)]
mod tests {
use std::f64::consts::TAU;
use fj_math::{Circle, Point, Scalar};
use crate::algorithms::approx::{path::RangeOnPath, Tolerance};
use super::PathApproxParams;
#[test]
fn increment_for_circle() {
test_increment(1., 0.5, 3.);
test_increment(1., 0.1, 7.);
test_increment(1., 0.01, 23.);
fn test_increment(
radius: impl Into<Scalar>,
tolerance: impl Into<Tolerance>,
expected_num_vertices: impl Into<Scalar>,
) {
let circle = Circle::from_center_and_radius([0., 0.], radius);
let params = PathApproxParams::for_circle(&circle, tolerance);
let expected_increment = Scalar::TAU / expected_num_vertices;
assert_eq!(params.increment(), expected_increment);
}
}
#[test]
fn points_for_circle() {
let empty: [Scalar; 0] = [];
test_path([[0.], [0.]], empty);
test_path([[0.], [TAU]], [1., 2., 3.]);
test_path([[1.], [TAU]], [1., 2., 3.]);
test_path([[0.], [TAU - 1.]], [1., 2., 3.]);
test_path([[2.], [TAU]], [2., 3.]);
test_path([[0.], [TAU - 2.]], [1., 2.]);
test_path([[TAU], [0.]], [3., 2., 1.]);
test_path([[TAU], [1.]], [3., 2., 1.]);
test_path([[TAU - 1.], [0.]], [3., 2., 1.]);
test_path([[TAU], [2.]], [3., 2.]);
test_path([[TAU - 2.], [0.]], [2., 1.]);
fn test_path(
range: impl Into<RangeOnPath>,
expected_coords: impl IntoIterator<Item = impl Into<Scalar>>,
) {
let radius = 1.;
let tolerance = 0.375;
let circle = Circle::from_center_and_radius([0., 0.], radius);
let params = PathApproxParams::for_circle(&circle, tolerance);
let points = params.points(range).collect::<Vec<_>>();
let expected_points = expected_coords
.into_iter()
.map(|i| Point::from([params.increment() * i]))
.collect::<Vec<_>>();
assert_eq!(points, expected_points);
}
}
}