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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Geometric primitives, like points and lines.
use arrayvec::ArrayVec;
use kurbo::{CubicBez, ParamCurve, ParamCurveExtrema, QuadBez};
use polycool::Quadratic;
use crate::curve::{quadratic_signs, solve_t_for_y, solve_x_for_y};
use crate::num::CheapOrderedFloat;
/// A two-dimensional point.
///
/// Points are sorted by `y` and then by `x`, for the convenience of our sweep-line
/// algorithm (which moves in increasing `y`).
#[cfg_attr(test, derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, PartialEq)]
pub struct Point {
/// Vertical coordinate.
///
/// Although it isn't important for functionality, the documentation and method naming
/// assumes that larger values are down.
pub y: f64,
/// Horizontal component.
///
/// Although it isn't important for functionality, the documentation and method naming
/// assumes that larger values are to the right.
pub x: f64,
}
impl Ord for Point {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(
CheapOrderedFloat::from(self.y),
CheapOrderedFloat::from(self.x),
)
.cmp(&(
CheapOrderedFloat::from(other.y),
CheapOrderedFloat::from(other.x),
))
}
}
impl PartialOrd for Point {
#[inline(always)]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Eq for Point {}
impl std::fmt::Debug for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({:?}, {:?})", self.x, self.y)
}
}
impl Point {
/// Create a new point.
///
/// Note that the `x` coordinate comes first. This might be a tiny bit
/// confusing because we're sorting by `y` coordinate first, but `(x, y)` is
/// the only sane order (prove me wrong).
pub fn new(x: f64, y: f64) -> Self {
debug_assert!(x.is_finite());
debug_assert!(y.is_finite());
Point { x, y }
}
/// Compute an affine combination between `self` and `other`; that is, `(1 - t) * self + t * other`.
///
/// Panics if a NaN is encountered, which might happen if some input is infinite.
pub fn affine(&self, other: &Self, t: f64) -> Self {
Point {
x: (1.0 - t) * self.x + t * other.x,
y: (1.0 - t) * self.y + t * other.y,
}
}
/// Compatibility with `kurbo` points.
pub fn to_kurbo(self) -> kurbo::Point {
kurbo::Point::new(self.x, self.y)
}
/// Compatibility with `kurbo` points.
pub fn from_kurbo(c: kurbo::Point) -> Self {
Self::new(c.x, c.y)
}
}
impl From<(f64, f64)> for Point {
fn from((x, y): (f64, f64)) -> Self {
Self { x, y }
}
}
impl From<Point> for kurbo::Point {
fn from(p: Point) -> Self {
kurbo::Point::new(p.x, p.y)
}
}
impl From<kurbo::Point> for Point {
fn from(p: kurbo::Point) -> Self {
Point::new(p.x, p.y)
}
}
/// A contour segment.
#[derive(Clone, PartialEq, Eq)]
pub struct Segment {
inner: SegmentInner,
}
#[derive(Clone, PartialEq, Eq)]
enum SegmentInner {
Line {
p0: Point,
p1: Point,
},
Quadratic {
p0: Point,
p1: Point,
p2: Point,
},
Cubic {
p0: Point,
p1: Point,
p2: Point,
p3: Point,
},
}
impl std::fmt::Debug for Segment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.inner {
SegmentInner::Line { p0, p1 } => write!(f, "{p0:?} -- {p1:?}"),
SegmentInner::Quadratic { p0, p1, p2 } => write!(f, "{p0:?} -- {p1:?} -- {p2:?}"),
SegmentInner::Cubic { p0, p1, p2, p3 } => {
write!(f, "{p0:?} -- {p1:?} -- {p2:?} -- {p3:?}")
}
}
}
}
/// A y-monotonic piece of a cubic Bezier.
#[derive(Debug)]
pub(crate) struct MonotonicPiece {
pub start_t: f64,
pub end_t: f64,
pub piece: Segment,
pub positively_oriented: bool,
}
pub(crate) fn monotonic_pieces(s: kurbo::PathSeg) -> ArrayVec<MonotonicPiece, 3> {
match s {
kurbo::PathSeg::Line(ell) => {
let mut ret = ArrayVec::new();
let p0: Point = ell.p0.into();
let p1: Point = ell.p1.into();
let (p0, p1, positively_oriented) = if p0 <= p1 {
(p0, p1, true)
} else {
(p1, p0, false)
};
if p0 != p1 {
ret.push(MonotonicPiece {
start_t: 0.0,
end_t: 1.0,
piece: Segment::straight(p0, p1),
positively_oriented,
});
}
ret
}
kurbo::PathSeg::Quad(q) => monotonic_quadratic_pieces(q),
kurbo::PathSeg::Cubic(c) => monotonic_cubic_pieces(c),
}
}
/// This could return an `ArrayVec<_, 2>`, but by returning `ArrayVec<_, 3>` we
/// make it the same return type as `monotonic_cubic_pieces`.
fn monotonic_quadratic_pieces(q: QuadBez) -> ArrayVec<MonotonicPiece, 3> {
fn to_seg(q: QuadBez) -> Option<(Segment, bool)> {
let (p0, mut p1, p2, orient) = if (q.p0.y, q.p0.x) <= (q.p2.y, q.p2.x) {
(q.p0, q.p1, q.p2, true)
} else {
(q.p2, q.p1, q.p0, false)
};
if p0 != p2 {
// Our derivative-solving should have made this cubic monotonic, but
// there might be some numerical issues with the starting and ending
// tangents. Fix them up.
p1.y = p0.y.max(p1.y).min(p2.y);
let seg = Segment::monotonic_quadratic(p0.into(), p1.into(), p2.into());
Some((seg, orient))
} else {
None
}
}
// The derivative is 2(1 - t) (p1 - p0) + 2t (p2 - p1), and we want to solve
// for when the y coordinate of this derivative is zero.
let t = (q.p0.y - q.p1.y) / (q.p2.y - 2.0 * q.p1.y + q.p0.y);
if 0.0 < t && t < 1.0 {
let q0 = q.subsegment(0.0..t);
let q1 = q.subsegment(t..1.0);
if let (Some((piece0, orient0)), Some((piece1, orient1))) = (to_seg(q0), to_seg(q1)) {
let mut ret = ArrayVec::new();
ret.push(MonotonicPiece {
start_t: 0.0,
end_t: t,
piece: piece0,
positively_oriented: orient0,
});
ret.push(MonotonicPiece {
start_t: t,
end_t: 1.0,
piece: piece1,
positively_oriented: orient1,
});
return ret;
}
}
let mut ret = ArrayVec::new();
if let Some((piece, positively_oriented)) = to_seg(q) {
ret.push(MonotonicPiece {
start_t: 0.0,
end_t: 1.0,
piece,
positively_oriented,
});
}
ret
}
pub(crate) fn monotonic_cubic_pieces(cub: CubicBez) -> ArrayVec<MonotonicPiece, 3> {
fn to_seg(c: CubicBez) -> Option<(Segment, bool)> {
let (p0, mut p1, mut p2, p3, orient) = if (c.p0.y, c.p0.x) <= (c.p3.y, c.p3.x) {
(c.p0, c.p1, c.p2, c.p3, true)
} else {
(c.p3, c.p2, c.p1, c.p0, false)
};
if p0 != p3 {
// Our derivative-solving should have made this cubic monotonic, but
// there might be some numerical issues with the starting and ending
// tangents. Fix them up.
p1.y = p0.y.max(p1.y);
p2.y = p3.y.min(p2.y);
let seg = Segment::monotonic_cubic(p0.into(), p1.into(), p2.into(), p3.into());
Some((seg, orient))
} else {
None
}
}
let mut ret = ArrayVec::new();
let q0 = cub.p1.y - cub.p0.y;
let q1 = cub.p2.y - cub.p1.y;
let q2 = cub.p3.y - cub.p2.y;
// Convert to the representation a t^2 + b t + c.
let c = q0;
let b = (q1 - q0) * 2.0;
let a = q0 - q1 * 2.0 + q2;
let q = Quadratic::new([c, b, a]);
let (_, r0, r1) = quadratic_signs(q);
let mut roots: ArrayVec<f64, 2> = ArrayVec::new();
if r0 > 0.0 && r0 < 1.0 {
roots.push(r0);
}
if r1 > r0 && r1 > 0.0 && r1 < 1.0 {
roots.push(r1);
}
let mut last_r = 0.0;
// We could try to remove very short segments, which could easily happen
// if the input curve has an initial or final tangent that's very close
// to horizontal. For now, we only remove segments that are exactly
// zero-length.
for r in roots {
let piece_before = cub.subsegment(last_r..r);
if let Some((piece, positively_oriented)) = to_seg(piece_before) {
ret.push(MonotonicPiece {
piece,
positively_oriented,
start_t: last_r,
end_t: r,
});
last_r = r;
}
}
let piece_before = cub.subsegment(last_r..1.0);
if let Some((piece, positively_oriented)) = to_seg(piece_before) {
ret.push(MonotonicPiece {
piece,
positively_oriented,
start_t: last_r,
end_t: 1.0,
});
} else if let Some(last_piece) = ret.last_mut() {
last_piece.end_t = 1.0;
}
ret
}
impl Segment {
/// Create a new cubic segment that must be increasing in `y`.
pub fn monotonic_cubic(p0: Point, p1: Point, p2: Point, p3: Point) -> Self {
if p3.y == p0.y {
// Ensure that horizontal segments are just represented by straight lines.
// TODO: maybe we should do something about degenerate S-shaped curves?
Self::straight(p0, p3)
} else {
Self {
inner: SegmentInner::Cubic { p0, p1, p2, p3 },
}
}
}
/// Create a new quadratic segment that must be increasing in `y`.
pub fn monotonic_quadratic(p0: Point, p1: Point, p2: Point) -> Self {
if p2.y == p0.y {
// Ensure that horizontal segments are just represented by straight lines.
// TODO: maybe we should do something about degenerate C-shaped curves?
Self::straight(p0, p2)
} else {
Self {
inner: SegmentInner::Quadratic { p0, p1, p2 },
}
}
}
/// Create a new segment that's just a straight line.
///
/// `start` must be less than `end`.
pub fn straight(start: Point, end: Point) -> Self {
debug_assert!(start <= end);
Self {
inner: SegmentInner::Line { p0: start, p1: end },
}
}
/// The starting point (smallest in the sweep-line order) of this segment.
pub fn start(&self) -> Point {
match self.inner {
SegmentInner::Line { p0, .. }
| SegmentInner::Quadratic { p0, .. }
| SegmentInner::Cubic { p0, .. } => p0,
}
}
/// The ending point (largest in the sweep-line order) of this segment.
pub fn end(&self) -> Point {
match self.inner {
SegmentInner::Line { p1, .. } => p1,
SegmentInner::Quadratic { p2, .. } => p2,
SegmentInner::Cubic { p3, .. } => p3,
}
}
/// Compatibility with `kurbo`'s cubics.
pub fn to_kurbo_cubic(&self) -> kurbo::CubicBez {
match self.inner {
SegmentInner::Line { p0: q0, p1: q1 } => {
let p0 = q0.to_kurbo();
let p3 = q1.to_kurbo();
let p1 = p0 + (1. / 3.) * (p3 - p0);
let p2 = p0 + (2. / 3.) * (p3 - p0);
kurbo::CubicBez { p0, p1, p2, p3 }
}
SegmentInner::Quadratic {
p0: q0,
p1: q1,
p2: q2,
} => {
let p0 = q0.to_kurbo();
let p3 = q2.to_kurbo();
let v1 = q1.to_kurbo().to_vec2();
let p1 = ((p0 + 2.0 * v1).to_vec2() / 3.0).to_point();
let p2 = ((p3 + 2.0 * v1).to_vec2() / 3.0).to_point();
kurbo::CubicBez { p0, p1, p2, p3 }
}
SegmentInner::Cubic { p0, p1, p2, p3 } => kurbo::CubicBez {
p0: p0.to_kurbo(),
p1: p1.to_kurbo(),
p2: p2.to_kurbo(),
p3: p3.to_kurbo(),
},
}
}
/// Compatibility with kurbo.
pub fn to_kurbo_seg(&self) -> kurbo::PathSeg {
match self.inner {
SegmentInner::Line { p0, p1 } => kurbo::Line {
p0: p0.to_kurbo(),
p1: p1.to_kurbo(),
}
.into(),
SegmentInner::Quadratic { p0, p1, p2 } => kurbo::QuadBez {
p0: p0.to_kurbo(),
p1: p1.to_kurbo(),
p2: p2.to_kurbo(),
}
.into(),
SegmentInner::Cubic { p0, p1, p2, p3 } => kurbo::CubicBez {
p0: p0.to_kurbo(),
p1: p1.to_kurbo(),
p2: p2.to_kurbo(),
p3: p3.to_kurbo(),
}
.into(),
}
}
/// TODO: maybe From instead?
pub fn from_kurbo_cubic(c: kurbo::CubicBez) -> Self {
Self::monotonic_cubic(
Point::from_kurbo(c.p0),
Point::from_kurbo(c.p1),
Point::from_kurbo(c.p2),
Point::from_kurbo(c.p3),
)
}
/// Is this segment just a straight line?
pub fn is_line(&self) -> bool {
matches!(self.inner, SegmentInner::Line { .. })
}
/// If this segment is a line, returns it.
pub fn as_kurbo_line(&self) -> Option<kurbo::Line> {
match self.inner {
SegmentInner::Line { p0, p1 } => Some(kurbo::Line::new(p0.to_kurbo(), p1.to_kurbo())),
_ => None,
}
}
/// A crude lower bound on our minimum horizontal position.
pub fn min_x(&self) -> f64 {
match self.inner {
SegmentInner::Line { p0, p1 } => p0.x.min(p1.x),
SegmentInner::Quadratic { p0, p1, p2 } => p0.x.min(p1.x).min(p2.x),
SegmentInner::Cubic { p0, p1, p2, p3 } => p0.x.min(p1.x).min(p2.x).min(p3.x),
}
}
/// A crude upper bound on our maximum horizontal position.
pub fn max_x(&self) -> f64 {
match self.inner {
SegmentInner::Line { p0, p1 } => p0.x.max(p1.x),
SegmentInner::Quadratic { p0, p1, p2 } => p0.x.max(p1.x).max(p2.x),
SegmentInner::Cubic { p0, p1, p2, p3 } => p0.x.max(p1.x).max(p2.x).max(p3.x),
}
}
/// Our `x` coordinate at the given `y` coordinate.
///
/// Horizontal segments will return their largest `x` coordinate.
///
/// # Panics
///
/// Panics if `y` is outside the `y` range of this segment.
pub fn at_y(&self, y: f64) -> f64 {
debug_assert!(
(self.start().y..=self.end().y).contains(&y),
"segment {self:?}, y={y:?}"
);
match self.inner {
SegmentInner::Line { p0, p1 } => {
if self.is_horizontal() {
p1.x
} else {
let t = (y - p0.y) / (p1.y - p0.y);
p0.x + t * (p1.x - p0.x)
}
}
// We could have a more optimized implementation for qudratics.
SegmentInner::Quadratic { .. } | SegmentInner::Cubic { .. } => {
solve_x_for_y(self.to_kurbo_cubic(), y)
}
}
}
fn local_bbox(&self, y: f64, eps: f64) -> kurbo::Rect {
let start_y = (y - eps).max(self.start().y);
let end_y = (y + eps).min(self.end().y);
match self.inner {
SegmentInner::Line { .. } => {
let start_x = self.at_y(start_y);
let end_x = self.at_y(end_y);
kurbo::Rect::from_points((start_x, start_y), (end_x, end_y))
}
SegmentInner::Quadratic { .. } | SegmentInner::Cubic { .. } => {
let c = self.to_kurbo_cubic();
let t_min = solve_t_for_y(c, start_y);
let t_max = solve_t_for_y(c, end_y);
c.subsegment(t_min..t_max).bounding_box()
}
}
}
/// Returns a lower bound on the `x` coordinate near `y`.
///
/// More precisely, take a Minkowski sum between this curve
/// and a small square. This returns a lower bound on the `x`
/// coordinate of the resulting set at height `y`.
pub fn lower(&self, y: f64, eps: f64) -> f64 {
self.local_bbox(y, eps).min_x() - eps
}
/// Returns an upper bound on the `x` coordinate near `y`.
///
/// More precisely, take a Minkowski sum between this curve
/// and a small square. This returns a upper bound on the `x`
/// coordinate of the resulting set at height `y`.
pub fn upper(&self, y: f64, eps: f64) -> f64 {
self.local_bbox(y, eps).max_x() + eps
}
/// Returns true if this segment is exactly horizontal.
pub fn is_horizontal(&self) -> bool {
match self.inner {
SegmentInner::Line { p0, p1 } => p0.y == p1.y,
SegmentInner::Cubic { .. } | SegmentInner::Quadratic { .. } => {
debug_assert_ne!(
self.start().y,
self.end().y,
"horizontal segments should be represented by lines"
);
false
}
}
}
}