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
// use std::ops::Range;
// use crate::{utils::{close_enough, diff_of_prod}, bezierq::BezierQ, Point};
// #[doc(hidden)]
// #[derive(Clone, Copy, Debug, PartialEq)]
// pub(crate) struct BezierC {
// pub p0: Point,
// pub p1: Point,
// pub p2: Point,
// pub p3: Point,
// }
// #[doc(hidden)]
// #[derive(Debug, PartialEq)]
// pub(crate) enum BezierConfig {
// NoLoop,
// Cusip(f64, f64), // (t, u) parameters around the cusip point
// Loop(f64, f64), // (t, u) parameters of self-intersection
// }
// impl BezierC {
// /// Create a new cubic Bézier segment.
// #[inline(always)]
// pub(crate) fn new<P: Into<Point>>(p0: P, p1: P, p2: P, p3: P) -> BezierC {
// BezierC {
// p0: p0.into(),
// p1: p1.into(),
// p2: p2.into(),
// p3: p3.into(),
// }
// }
// }
// // #00013
// #[doc(hidden)]
// const CUSIP_THRESHOLD: f64 = 1e-4; // threshold for cusip detection
// impl BezierC {
// pub(crate) fn self_intersection(&self) -> BezierConfig {
// // For cubic curves containing a loop, compute the parameters t, u where
// // B(t) = B(u), i.e. the parameters of self-intersection.
// // The method name is both a phonetic abbreviation ("c"elf "x")
// // and an ASCII depiction of a loopy curve.
// // As Bézier curve geometry is invariant under affine transformations,
// // we can restrict ourselves to some canonical form of the curve.
// // Then, consider the coordinate polynomials
// // x(t) = at³+bt²+ct+p
// // y(t) = dt³+et²+ft+q
// // and parameters of self-intersection as λ and μ. By definition,
// // x(λ) - x(μ) = a(λ³-μ³)+b(λ²-μ²)+c(λ-μ) = 0
// // y(λ) - y(μ) = d(λ³-μ³)+e(λ²-μ²)+f(λ-μ) = 0
// // Dividing by the trivial solution λ = μ and expanding we get
// // a(λ²+λμ+μ²)+b(λ+μ)+c = 0
// // d(λ²+λμ+μ²)+e(λ+μ)+f = 0
// // In the canonical form chosen here
// // (https://pomax.github.io/bezierinfo/#canonical) we have
// // (x-3)(λ²+λμ+μ²)+3(λ+μ) = 0
// // y(λ²+λμ+μ²)-3(λ+μ)+3 = 0
// // whereby eliminating λ²+λμ+μ² gives (-3-3y/(x-3))(λ+μ) + 3 = 0
// // or λ+μ = (x-3)/(x+y-3), followed by λμ = (λ+μ)²+3/(x+y-3).
// // λ and μ can now be found by Viète's formulas.
// let vx = self.p2 - self.p1;
// let vy = self.p1 - self.p0;
// let vz = self.p3 - self.p0;
// let a = vx.x;
// let b = vy.x;
// let c = vx.y;
// let d = vy.y;
// let z1 = vz.x;
// let z2 = vz.y;
// let res = solve2x2(a, b, c, d, z1, z2);
// if res.is_none() {
// // No solution found, the curve is not self-intersecting.
// return BezierConfig::NoLoop;
// }
// let (x, y) = res.unwrap();
// // Check if the solution is within the bounds of the Bezier curve
// // Comparisons come from the Canonical form of the cubic Bezier curve
// // https://pomax.github.io/BezierInfo-2/#canonical
// if (x > 1.0)
// || (4.0 * y > (x + 1.0) * (3.0 - x))
// || (x > 0.0 && (2.0 * y + x < (3.0 * x * (4.0 - x)).sqrt())
// || (3.0 * y < x * (3.0 - x)))
// {
// // https://github.com/linebender/kurbo/blob/main/kurbo/src/cubicbez.rs#L442 detectCusip() for comparison
// if close_enough(4.0 * y, (x + 1.0) * (3.0 - x), CUSIP_THRESHOLD) {
// // TODO: adjust tolerance
// let rs = (x - 3.0) / (x + y - 3.0);
// // let rp = rs * rs + 3.0 / (x + y - 3.0);
// // let zz = rs * rs - 4.0 * rp; // close to zero
// let x1 = rs / 2.0;
// // Special case: cusip, where the curve touches itself at a single point
// return BezierConfig::Cusip(x1 - CUSIP_THRESHOLD, x1 + CUSIP_THRESHOLD);
// }
// return BezierConfig::NoLoop;
// }
// let rs = (x - 3.0) / (x + y - 3.0);
// let rp = rs * rs + 3.0 / (x + y - 3.0);
// let x1 = (rs - (rs * rs - 4.0 * rp).sqrt()) / 2.0;
// // sorted
// if x1 < rp / x1 {
// return BezierConfig::Loop(x1, rp / x1);
// } else {
// return BezierConfig::Loop(rp / x1, x1);
// }
// }
// }
// // Solution by inverting a 2x2 matrix equation Ax = b
// // https://www.google.com/search?q=rust+Solve+a+linear+2x2+matrix+equation
// fn solve2x2(a: f64, b: f64, c: f64, d: f64, z1: f64, z2: f64) -> Option<(f64, f64)> {
// // Solve a linear 2x2 matrix equation
// //let det = a * d - b * c;
// let det = diff_of_prod(a, d, b, c);
// if close_enough(det, 0.0, 1e-10) {
// return None; // No solution
// }
// // Inverse of matrix
// let ainv = [d / det, -b / det, -c / det, a / det];
// // solution to the matrix equation \(Ax=b\) is \(x=A^{-1}b\).
// let x = ainv[0] * z1 + ainv[1] * z2;
// let y = ainv[2] * z1 + ainv[3] * z2;
// Some((x, y))
// }
// #[cfg(test)]
// mod test_bezier {
// use super::*;
// #[test]
// fn test_selfintersect_01() {
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(11.0, 10.0),
// Point::new(-1.0, 10.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(
// res,
// BezierConfig::Loop(0.3194212203713461, 0.6805787796286539)
// );
// }
// #[test]
// fn test_nointersect_02() {
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(7.0, 10.0),
// Point::new(3.0, 10.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::NoLoop);
// }
// #[test]
// fn test_selfintersect_03a() {
// // edge case, loop
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(10.0, 10.0),
// Point::new(0.0, 10.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::Loop(0.5, 0.5));
// }
// #[test]
// fn test_selfintersect_03b() {
// // edge case, cusip
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(10.0, 10.0),
// Point::new(0.0 + 1e-6, 10.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(
// res,
// BezierConfig::Cusip(0.5 - CUSIP_THRESHOLD, 0.5 + CUSIP_THRESHOLD)
// );
// }
// #[test]
// fn test_selfintersect_04() {
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(10.0, 10.0),
// Point::new(1e-10, 10.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(
// res,
// BezierConfig::Cusip(0.5 - CUSIP_THRESHOLD, 0.5 + CUSIP_THRESHOLD)
// );
// }
// #[test]
// fn test_selfintersect_05() {
// // edge case, cusip
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(13.0 + 1.0 / 3.0, 0.0),
// Point::new(0.0, 4.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(
// res,
// BezierConfig::Loop(0.6666666666666666, 0.6666666666666666)
// );
// }
// #[test]
// fn test_nointersect_06() {
// // edge case
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(20.0, -4.0),
// Point::new(0.0, 4.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::NoLoop);
// }
// #[test]
// fn test_nointersect_zerodet_07() {
// // edge case
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(-2.0, 0.0),
// Point::new(12.0, 0.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::NoLoop);
// }
// #[test]
// fn test_nointersect_zerodet_08() {
// // edge case
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(-2.0, 0.0),
// Point::new(8.0, 0.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::NoLoop);
// }
// #[test]
// fn test_nointersect_zerodet_09() {
// // edge case
// let b = BezierC::new(
// Point::new(2.0, 0.0),
// Point::new(12.0, 0.0),
// Point::new(8.0, 0.0),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// assert_eq!(res, BezierConfig::NoLoop);
// }
// #[test]
// fn test_cusip_10() {
// // edge case
// let b = BezierC::new(
// Point::new(0.0, 0.0),
// Point::new(20.0, -10.0),
// // Point::new(0.0, 12.07106),
// Point::new(0.0, 12.071),
// Point::new(10.0, 0.0),
// );
// let res = b.self_intersection();
// let x = 0.7734605377276669;
// assert_eq!(
// res,
// BezierConfig::Cusip(x - CUSIP_THRESHOLD, x + CUSIP_THRESHOLD)
// );
// }
// }
// impl BezierC {
// #[inline]
// pub fn eval(&self, t: f64) -> Point {
// let mt = 1.0 - t;
// let v = self.p0 * (mt * mt * mt)
// + (self.p1 * (mt * mt * 3.0)
// + (self.p2 * (mt * 3.0) + self.p3 * t) * t)
// * t;
// v
// }
// #[inline]
// pub fn deriv(&self) -> BezierQ {
// BezierQ::new(
// (self.p1 - self.p0) * 3.0,
// (self.p2 - self.p1) * 3.0,
// (self.p3 - self.p2) * 3.0,
// )
// }
// pub fn subsegment(&self, range: Range<f64>) -> BezierC {
// let (t0, t1) = (range.start, range.end);
// let p0 = self.eval(t0);
// let p3 = self.eval(t1);
// let d = self.deriv();
// let scale = (t1 - t0) * (1.0 / 3.0);
// let p1 = p0 + d.eval(t0) * scale;
// let p2 = p3 - d.eval(t1) * scale;
// BezierC { p0, p1, p2, p3 }
// }
// }