logo
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
//! Bounding rectangle computation for paths.

use crate::geom::{CubicBezierSegment, QuadraticBezierSegment};
use crate::math::{point, Box2D, Point};
use crate::path::PathEvent;
use std::f32;

/// Computes a conservative axis-aligned rectangle that contains the path.
///
/// This bounding rectangle approximation is faster but less precise than
/// [`building_box`](fn.bounding_box.html).
pub fn fast_bounding_box<Iter, Evt>(path: Iter) -> Box2D
where
    Iter: IntoIterator<Item = Evt>,
    Evt: FastBoundingBox,
{
    let mut min = point(f32::MAX, f32::MAX);
    let mut max = point(f32::MIN, f32::MIN);
    for e in path {
        e.min_max(&mut min, &mut max);
    }

    // Return an empty rectangle by default if there was no event in the path.
    if min == point(f32::MAX, f32::MAX) {
        return Box2D::zero();
    }

    Box2D { min, max }
}

#[doc(hidden)]
pub trait FastBoundingBox {
    fn min_max(&self, min: &mut Point, max: &mut Point);
}

impl FastBoundingBox for PathEvent {
    fn min_max(&self, min: &mut Point, max: &mut Point) {
        match self {
            PathEvent::Begin { at } => {
                *min = Point::min(*min, *at);
                *max = Point::max(*max, *at);
            }
            PathEvent::Line { to, .. } => {
                *min = Point::min(*min, *to);
                *max = Point::max(*max, *to);
            }
            PathEvent::Quadratic { ctrl, to, .. } => {
                *min = Point::min(*min, Point::min(*ctrl, *to));
                *max = Point::max(*max, Point::max(*ctrl, *to));
            }
            PathEvent::Cubic {
                ctrl1, ctrl2, to, ..
            } => {
                *min = Point::min(*min, Point::min(*ctrl1, Point::min(*ctrl2, *to)));
                *max = Point::max(*max, Point::max(*ctrl1, Point::max(*ctrl2, *to)));
            }
            PathEvent::End { .. } => {}
        }
    }
}

/// Computes the smallest axis-aligned rectangle that contains the path.
pub fn bounding_box<Iter, Evt>(path: Iter) -> Box2D
where
    Iter: IntoIterator<Item = Evt>,
    Evt: TightBoundingBox,
{
    let mut min = point(f32::MAX, f32::MAX);
    let mut max = point(f32::MIN, f32::MIN);

    for evt in path {
        evt.min_max(&mut min, &mut max);
    }

    // Return an empty rectangle by default if there was no event in the path.
    if min == point(f32::MAX, f32::MAX) {
        return Box2D::zero();
    }

    Box2D { min, max }
}

#[doc(hidden)]
pub trait TightBoundingBox {
    fn min_max(&self, min: &mut Point, max: &mut Point);
}

impl TightBoundingBox for PathEvent {
    fn min_max(&self, min: &mut Point, max: &mut Point) {
        match self {
            PathEvent::Begin { at } => {
                *min = Point::min(*min, *at);
                *max = Point::max(*max, *at);
            }
            PathEvent::Line { to, .. } => {
                *min = Point::min(*min, *to);
                *max = Point::max(*max, *to);
            }
            PathEvent::Quadratic { from, ctrl, to } => {
                let r = QuadraticBezierSegment {
                    from: *from,
                    ctrl: *ctrl,
                    to: *to,
                }
                .bounding_box();
                *min = Point::min(*min, r.min);
                *max = Point::max(*max, r.max);
            }
            PathEvent::Cubic {
                from,
                ctrl1,
                ctrl2,
                to,
            } => {
                let r = CubicBezierSegment {
                    from: *from,
                    ctrl1: *ctrl1,
                    ctrl2: *ctrl2,
                    to: *to,
                }
                .bounding_box();
                *min = Point::min(*min, r.min);
                *max = Point::max(*max, r.max);
            }
            PathEvent::End { .. } => {}
        }
    }
}

#[test]
fn simple_bounding_box() {
    use crate::path::Path;

    let mut builder = Path::builder();
    builder.begin(point(-10.0, -3.0));
    builder.line_to(point(0.0, -12.0));
    builder.quadratic_bezier_to(point(3.0, 4.0), point(5.0, 3.0));
    builder.end(true);
    let path = builder.build();

    assert_eq!(
        fast_bounding_box(&path),
        Box2D {
            min: point(-10.0, -12.0),
            max: point(5.0, 4.0)
        },
    );

    let mut builder = Path::builder();
    builder.begin(point(0.0, 0.0));
    builder.cubic_bezier_to(point(-1.0, 2.0), point(3.0, -4.0), point(1.0, -1.0));
    builder.end(false);
    let path = builder.build();

    assert_eq!(
        fast_bounding_box(path.iter()),
        Box2D {
            min: point(-1.0, -4.0),
            max: point(3.0, 2.0)
        },
    );
}