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
use crate::segment::{Segment, BoundingRect};
use crate::scalar::{Scalar, NumCast};
use crate::generic_math::{Point, Vector, Rect};
use crate::{QuadraticBezierSegment, CubicBezierSegment};
use std::ops::Range;
use arrayvec::ArrayVec;

use std::f64;

pub(crate) trait MonotonicSegment {
    type Scalar: Scalar;
    fn solve_t_for_x(&self, x: Self::Scalar, t_range: Range<Self::Scalar>, tolerance: Self::Scalar) -> Self::Scalar;
}

/// A x and y monotonic curve segment, for example `Monotonic<QuadraticBezierSegment>`.
#[derive(Copy, Clone, Debug)]
pub struct Monotonic<T> {
    pub(crate) segment: T,
}

impl<T: Segment> Monotonic<T> {
    #[inline]
    pub fn segment(&self) -> &T { &self.segment }
    #[inline]
    pub fn from(&self) -> Point<T::Scalar> { self.segment.from() }
    #[inline]
    pub fn to(&self) -> Point<T::Scalar> { self.segment.to() }
    #[inline]
    pub fn sample(&self, t: T::Scalar) -> Point<T::Scalar> { self.segment.sample(t) }
    #[inline]
    pub fn x(&self, t: T::Scalar) -> T::Scalar { self.segment.x(t) }
    #[inline]
    pub fn y(&self, t: T::Scalar) -> T::Scalar { self.segment.y(t) }
    #[inline]
    pub fn derivative(&self, t: T::Scalar) -> Vector<T::Scalar> { self.segment.derivative(t) }
    #[inline]
    pub fn dx(&self, t: T::Scalar) -> T::Scalar { self.segment.dx(t) }
    #[inline]
    pub fn dy(&self, t: T::Scalar) -> T::Scalar { self.segment.dy(t) }
    #[inline]
    pub fn split_range(&self, t_range: Range<T::Scalar>) -> Self {
        Self { segment: self.segment.split_range(t_range) }
    }
    #[inline]
    pub fn split(&self, t: T::Scalar) -> (Self, Self) {
        let (a, b) = self.segment.split(t);
        (Self { segment: a }, Self { segment: b })
    }
    #[inline]
    pub fn before_split(&self, t: T::Scalar) -> Self {
        Self { segment: self.segment.before_split(t) }
    }
    #[inline]
    pub fn after_split(&self, t: T::Scalar) -> Self {
        Self { segment: self.segment.after_split(t) }
    }
    #[inline]
    pub fn flip(&self) -> Self {
        Self { segment: self.segment.flip() }
    }
    #[inline]
    pub fn approximate_length(&self, tolerance: T::Scalar) -> T::Scalar {
        self.segment.approximate_length(tolerance)
    }
}

impl<T: Segment> Segment for Monotonic<T> { impl_segment!(T::Scalar); }

impl<T: BoundingRect> BoundingRect for Monotonic<T> {
    type Scalar = T::Scalar;
    fn bounding_rect(&self) -> Rect<T::Scalar> {
        // For monotonic segments the fast bounding rect approximation
        // is exact.
        self.segment.fast_bounding_rect()
    }
    fn fast_bounding_rect(&self) -> Rect<T::Scalar> {
        self.segment.fast_bounding_rect()
    }
    fn bounding_range_x(&self) -> (T::Scalar, T::Scalar) {
        self.segment.bounding_range_x()
    }
    fn bounding_range_y(&self) -> (T::Scalar, T::Scalar) {
        self.segment.bounding_range_y()
    }
    fn fast_bounding_range_x(&self) -> (T::Scalar, T::Scalar) {
        self.segment.fast_bounding_range_x()
    }
    fn fast_bounding_range_y(&self) -> (T::Scalar, T::Scalar) {
        self.segment.fast_bounding_range_y()
    }
}

impl<S: Scalar> Monotonic<QuadraticBezierSegment<S>> {
    pub fn solve_t_for_x(&self, x: S) -> S {
        Self::solve_t(
            NumCast::from(self.segment.from.x).unwrap(),
            NumCast::from(self.segment.ctrl.x).unwrap(),
            NumCast::from(self.segment.to.x).unwrap(),
            NumCast::from(x).unwrap(),
        )
    }

    pub fn solve_t_for_y(&self, y: S) -> S {
        Self::solve_t(
            NumCast::from(self.segment.from.y).unwrap(),
            NumCast::from(self.segment.ctrl.y).unwrap(),
            NumCast::from(self.segment.to.y).unwrap(),
            NumCast::from(y).unwrap(),
        )
    }

    fn solve_t(from: f64, ctrl: f64, to: f64, x: f64) -> S {
        let a = from - 2.0 * ctrl + to;
        let b = -2.0 * from + 2.0 * ctrl;
        let c = from - x;

        let t = 2.0 * c / (-b - f64::sqrt(b * b - 4.0 * a * c));

        NumCast::from(t.max(0.0).min(1.0)).unwrap()
    }

    #[inline]
    pub fn split_at_x(&self, x: S) -> (Self, Self) {
        self.split(self.solve_t_for_x(x))
    }

    pub fn intersections_t(
        &self, self_t_range: Range<S>,
        other: &Self, other_t_range: Range<S>,
        tolerance: S,
    ) -> ArrayVec<[(S, S);2]> {
        monotonic_segment_intersecions(
            self, self_t_range,
            other, other_t_range,
            tolerance
        )
    }

    pub fn intersections(
        &self, self_t_range: Range<S>,
        other: &Self, other_t_range: Range<S>,
        tolerance: S,
    ) -> ArrayVec<[Point<S>;2]> {
        let intersections = monotonic_segment_intersecions(
            self, self_t_range,
            other, other_t_range,
            tolerance
        );
        let mut result = ArrayVec::new();
        for (t, _) in intersections {
            result.push(self.sample(t));
        }

        result
    }

    pub fn first_intersection_t(
        &self, self_t_range: Range<S>,
        other: &Self, other_t_range: Range<S>,
        tolerance: S,
    ) -> Option<(S, S)> {
        first_monotonic_segment_intersecion(
            self, self_t_range,
            other, other_t_range,
            tolerance
        )
    }

    pub fn first_intersection(
        &self, self_t_range: Range<S>,
        other: &Self, other_t_range: Range<S>,
        tolerance: S,
    ) -> Option<Point<S>> {
        first_monotonic_segment_intersecion(
            self, self_t_range,
            other, other_t_range,
            tolerance
        ).map(|(t, _)|{ self.sample(t) })
    }
}

impl<S: Scalar> MonotonicSegment for Monotonic<QuadraticBezierSegment<S>> {
    type Scalar = S;
    fn solve_t_for_x(&self, x: S, _t_range: Range<S>, _tolerance: S) -> S {
        self.solve_t_for_x(x)
    }
}

impl<S: Scalar> Monotonic<CubicBezierSegment<S>> {
    pub fn solve_t_for_x(&self, x: S, t_range: Range<S>, tolerance: S) -> S {
        debug_assert!(t_range.start <= t_range.end);
        let from = self.x(t_range.start);
        let to = self.x(t_range.end);
        if x <= from {
            return t_range.start;
        }
        if x >= to {
            return t_range.end;
        }

        // Newton's method.
        let mut t = x - from / (to - from);
        for _ in 0..8 {
            let x2 = self.x(t);

            if S::abs(x2 - x) <= tolerance {
                return t
            }

            let dx = self.dx(t);

            if dx <= S::EPSILON {
                break
            }

            t = t - (x2 - x) / dx;
        }

        // Fall back to binary search.
        let mut min = t_range.start;
        let mut max = t_range.end;
        let mut t = S::HALF;

        while min < max {
            let x2 = self.x(t);

            if S::abs(x2 - x) < tolerance {
                return t;
            }

            if x > x2 {
                min = t;
            } else {
                max = t;
            }

            t = (max - min) * S::HALF + min;
        }

        return t;
    }

    #[inline]
    pub fn split_at_x(&self, x: S) -> (Self, Self) {
        // TODO tolerance param.
        self.split(self.solve_t_for_x(x, S::ZERO..S::ONE, S::value(0.001)))
    }
}

impl<S: Scalar> MonotonicSegment for Monotonic<CubicBezierSegment<S>> {
    type Scalar = S;
    fn solve_t_for_x(&self, x: S, t_range: Range<S>, tolerance: S) -> S {
        self.solve_t_for_x(x, t_range, tolerance)
    }
}

/// Return the first intersection point (if any) of two monotonic curve
/// segments.
///
/// Both segments must be monotonically increasing in x.
pub(crate) fn first_monotonic_segment_intersecion<S: Scalar, A, B>(
    a: &A, a_t_range: Range<S>,
    b: &B, b_t_range: Range<S>,
    tolerance: S,
) -> Option<(S, S)>
where
    A: Segment<Scalar=S> + MonotonicSegment<Scalar=S> + BoundingRect<Scalar=S>,
    B: Segment<Scalar=S> + MonotonicSegment<Scalar=S> + BoundingRect<Scalar=S>,
{
    debug_assert!(a.from().x <= a.to().x);
    debug_assert!(b.from().x <= b.to().x);

    // We need to have a stricter tolerance in solve_t_for_x otherwise
    // the error accumulation becomes pretty bad.
    let tx_tolerance = tolerance / S::TEN;

    let (a_min, a_max) = a.split_range(a_t_range).fast_bounding_range_x();
    let (b_min, b_max) = b.split_range(b_t_range).fast_bounding_range_x();

    if a_min > b_max || a_max < b_min {
        return None;
    }

    let mut min_x = S::max(a_min, b_min);
    let mut max_x = S::min(a_max, b_max);

    let mut t_min_a = a.solve_t_for_x(min_x, S::ZERO..S::ONE, tx_tolerance);
    let mut t_max_a = a.solve_t_for_x(max_x, t_min_a..S::ONE, tx_tolerance);
    let mut t_min_b = b.solve_t_for_x(min_x, S::ZERO..S::ONE, tx_tolerance);
    let mut t_max_b = b.solve_t_for_x(max_x, t_min_b..S::ONE, tx_tolerance);

    const MAX_ITERATIONS: u32 = 32;
    for _ in 0..MAX_ITERATIONS {

        let y_max_a = a.y(t_max_a);
        let y_max_b = b.y(t_max_b);
        // It would seem more sensible to use the mid point instead of
        // the max point, but using the mid point means we don't know whether
        // the approximation will be slightly before or slightly after the
        // point.
        // Using the max point ensures that the we return an approximation
        // that is always slightly after the real intersection, which
        // means that if we search for intersections after the one we
        // found, we are not going to converge towards it again.
        if S::abs(y_max_a - y_max_b) < tolerance {
            return Some((t_max_a, t_max_b));
        }

        let mid_x = (min_x + max_x) * S::HALF;
        let t_mid_a = a.solve_t_for_x(mid_x, t_min_a..t_max_a, tx_tolerance);
        let t_mid_b = b.solve_t_for_x(mid_x, t_min_b..t_max_b, tx_tolerance);

        let y_mid_a = a.y(t_mid_a);
        let y_min_a = a.y(t_min_a);

        let y_mid_b = b.y(t_mid_b);
        let y_min_b = b.y(t_min_b);

        let min_sign = S::signum(y_min_a - y_min_b);
        let mid_sign = S::signum(y_mid_a - y_mid_b);
        let max_sign = S::signum(y_max_a - y_max_b);

        if min_sign != mid_sign {
            max_x = mid_x;
            t_max_a = t_mid_a;
            t_max_b = t_mid_b;
        } else if max_sign != mid_sign {
            min_x = mid_x;
            t_min_a = t_mid_a;
            t_min_b = t_mid_b;
        } else {
            // TODO: This is not always correct: if the min, max and mid
            // points are all on the same side, we consider that there is
            // no intersection, but there could be a pair of intersections
            // between the min/max and the mid point.
            break;
        }
    }

    None
}

/// Return the intersection points (if any) of two monotonic curve
/// segments.
///
/// Both segments must be monotonically increasing in x.
pub(crate) fn monotonic_segment_intersecions<S: Scalar, A, B>(
    a: &A, a_t_range: Range<S>,
    b: &B, b_t_range: Range<S>,
    tolerance: S,
) -> ArrayVec<[(S, S); 2]>
where
    A: Segment<Scalar=S> + MonotonicSegment<Scalar=S> + BoundingRect<Scalar=S>,
    B: Segment<Scalar=S> + MonotonicSegment<Scalar=S> + BoundingRect<Scalar=S>,
{
    let (t1, t2) = match first_monotonic_segment_intersecion(
        a, a_t_range.clone(),
        b, b_t_range.clone(),
        tolerance
    ) {
        Some(intersection) => { intersection }
        None => { return ArrayVec::new(); }
    };

    let mut result = ArrayVec::new();
    result.push((t1, t2));

    match first_monotonic_segment_intersecion(
        a, t1..a_t_range.end,
        b, t2..b_t_range.end,
        tolerance
    ) {
        Some(intersection) => { result.push(intersection); }
        None => {}
    }

    result
}

#[test]
fn two_intersections() {
    use crate::QuadraticBezierSegment;
    use crate::math::point;

    let c1 = QuadraticBezierSegment {
        from: point(10.0, 0.0),
        ctrl: point(10.0, 90.0),
        to: point(100.0, 90.0),
    }.assume_monotonic();
    let c2 = QuadraticBezierSegment {
        from: point(0.0, 10.0),
        ctrl: point(90.0, 10.0),
        to: point(90.0, 100.0),
    }.assume_monotonic();

    let intersections = monotonic_segment_intersecions(
        &c1, 0.0..1.0,
        &c2, 0.0..1.0,
        0.001,
    );

    assert_eq!(intersections.len(), 2);
    assert!(intersections[0].0 < 0.1, "{:?} < 0.1", intersections[0].0);
    assert!(intersections[1].1 > 0.9, "{:?} > 0.9", intersections[0].1);
}