Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-spline.c
Line
Count
Source
1
/* cairo - a vector graphics library with display and print output
2
 *
3
 * Copyright © 2002 University of Southern California
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it either under the terms of the GNU Lesser General Public
7
 * License version 2.1 as published by the Free Software Foundation
8
 * (the "LGPL") or, at your option, under the terms of the Mozilla
9
 * Public License Version 1.1 (the "MPL"). If you do not alter this
10
 * notice, a recipient may use your version of this file under either
11
 * the MPL or the LGPL.
12
 *
13
 * You should have received a copy of the LGPL along with this library
14
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16
 * You should have received a copy of the MPL along with this library
17
 * in the file COPYING-MPL-1.1
18
 *
19
 * The contents of this file are subject to the Mozilla Public License
20
 * Version 1.1 (the "License"); you may not use this file except in
21
 * compliance with the License. You may obtain a copy of the License at
22
 * http://www.mozilla.org/MPL/
23
 *
24
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26
 * the specific language governing rights and limitations.
27
 *
28
 * The Original Code is the cairo graphics library.
29
 *
30
 * The Initial Developer of the Original Code is University of Southern
31
 * California.
32
 *
33
 * Contributor(s):
34
 *  Carl D. Worth <cworth@cworth.org>
35
 */
36
37
#include "cairoint.h"
38
39
#include "cairo-box-inline.h"
40
#include "cairo-slope-private.h"
41
42
cairo_bool_t
43
_cairo_spline_intersects (const cairo_point_t *a,
44
        const cairo_point_t *b,
45
        const cairo_point_t *c,
46
        const cairo_point_t *d,
47
        const cairo_box_t *box)
48
793k
{
49
793k
    cairo_box_t bounds;
50
51
793k
    if (_cairo_box_contains_point (box, a) ||
52
285k
  _cairo_box_contains_point (box, b) ||
53
283k
  _cairo_box_contains_point (box, c) ||
54
279k
  _cairo_box_contains_point (box, d))
55
520k
    {
56
520k
  return TRUE;
57
520k
    }
58
59
272k
    bounds.p2 = bounds.p1 = *a;
60
272k
    _cairo_box_add_point (&bounds, b);
61
272k
    _cairo_box_add_point (&bounds, c);
62
272k
    _cairo_box_add_point (&bounds, d);
63
64
272k
    if (bounds.p2.x <= box->p1.x || bounds.p1.x >= box->p2.x ||
65
15.2k
  bounds.p2.y <= box->p1.y || bounds.p1.y >= box->p2.y)
66
271k
    {
67
271k
  return FALSE;
68
271k
    }
69
70
#if 0 /* worth refining? */
71
    bounds.p2 = bounds.p1 = *a;
72
    _cairo_box_add_curve_to (&bounds, b, c, d);
73
    if (bounds.p2.x <= box->p1.x || bounds.p1.x >= box->p2.x ||
74
  bounds.p2.y <= box->p1.y || bounds.p1.y >= box->p2.y)
75
    {
76
  return FALSE;
77
    }
78
#endif
79
80
1.07k
    return TRUE;
81
272k
}
82
83
cairo_bool_t
84
_cairo_spline_init (cairo_spline_t *spline,
85
        cairo_spline_add_point_func_t add_point_func,
86
        void *closure,
87
        const cairo_point_t *a, const cairo_point_t *b,
88
        const cairo_point_t *c, const cairo_point_t *d)
89
522k
{
90
    /* If both tangents are zero, this is just a straight line */
91
522k
    if (a->x == b->x && a->y == b->y && c->x == d->x && c->y == d->y)
92
0
  return FALSE;
93
94
522k
    spline->add_point_func = add_point_func;
95
522k
    spline->closure = closure;
96
97
522k
    spline->knots.a = *a;
98
522k
    spline->knots.b = *b;
99
522k
    spline->knots.c = *c;
100
522k
    spline->knots.d = *d;
101
102
522k
    if (a->x != b->x || a->y != b->y)
103
350k
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.b);
104
171k
    else if (a->x != c->x || a->y != c->y)
105
171k
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.c);
106
19
    else if (a->x != d->x || a->y != d->y)
107
19
  _cairo_slope_init (&spline->initial_slope, &spline->knots.a, &spline->knots.d);
108
0
    else
109
0
  return FALSE;
110
111
522k
    if (c->x != d->x || c->y != d->y)
112
350k
  _cairo_slope_init (&spline->final_slope, &spline->knots.c, &spline->knots.d);
113
171k
    else if (b->x != d->x || b->y != d->y)
114
171k
  _cairo_slope_init (&spline->final_slope, &spline->knots.b, &spline->knots.d);
115
12
    else
116
12
  return FALSE; /* just treat this as a straight-line from a -> d */
117
118
    /* XXX if the initial, final and vector are all equal, this is just a line */
119
120
522k
    return TRUE;
121
522k
}
122
123
static cairo_status_t
124
_cairo_spline_add_point (cairo_spline_t *spline,
125
       const cairo_point_t *point,
126
       const cairo_point_t *knot)
127
1.77M
{
128
1.77M
    cairo_point_t *prev;
129
1.77M
    cairo_slope_t slope;
130
131
1.77M
    prev = &spline->last_point;
132
1.77M
    if (prev->x == point->x && prev->y == point->y)
133
522k
  return CAIRO_STATUS_SUCCESS;
134
135
1.25M
    _cairo_slope_init (&slope, point, knot);
136
137
1.25M
    spline->last_point = *point;
138
1.25M
    return spline->add_point_func (spline->closure, point, &slope);
139
1.77M
}
140
141
static void
142
_lerp_half (const cairo_point_t *a, const cairo_point_t *b, cairo_point_t *result)
143
7.53M
{
144
7.53M
    result->x = a->x + ((b->x - a->x) >> 1);
145
7.53M
    result->y = a->y + ((b->y - a->y) >> 1);
146
7.53M
}
147
148
static void
149
_de_casteljau (cairo_spline_knots_t *s1, cairo_spline_knots_t *s2)
150
1.25M
{
151
1.25M
    cairo_point_t ab, bc, cd;
152
1.25M
    cairo_point_t abbc, bccd;
153
1.25M
    cairo_point_t final;
154
155
1.25M
    _lerp_half (&s1->a, &s1->b, &ab);
156
1.25M
    _lerp_half (&s1->b, &s1->c, &bc);
157
1.25M
    _lerp_half (&s1->c, &s1->d, &cd);
158
1.25M
    _lerp_half (&ab, &bc, &abbc);
159
1.25M
    _lerp_half (&bc, &cd, &bccd);
160
1.25M
    _lerp_half (&abbc, &bccd, &final);
161
162
1.25M
    s2->a = final;
163
1.25M
    s2->b = bccd;
164
1.25M
    s2->c = cd;
165
1.25M
    s2->d = s1->d;
166
167
1.25M
    s1->b = ab;
168
1.25M
    s1->c = abbc;
169
1.25M
    s1->d = final;
170
1.25M
}
171
172
/* Return an upper bound on the error (squared) that could result from
173
 * approximating a spline as a line segment connecting the two endpoints. */
174
static double
175
_cairo_spline_error_squared (const cairo_spline_knots_t *knots)
176
3.03M
{
177
3.03M
    double bdx, bdy, berr;
178
3.03M
    double cdx, cdy, cerr;
179
180
    /* We are going to compute the distance (squared) between each of the b
181
     * and c control points and the segment a-b. The maximum of these two
182
     * distances will be our approximation error. */
183
184
3.03M
    bdx = _cairo_fixed_to_double (knots->b.x - knots->a.x);
185
3.03M
    bdy = _cairo_fixed_to_double (knots->b.y - knots->a.y);
186
187
3.03M
    cdx = _cairo_fixed_to_double (knots->c.x - knots->a.x);
188
3.03M
    cdy = _cairo_fixed_to_double (knots->c.y - knots->a.y);
189
190
3.03M
    if (knots->a.x != knots->d.x || knots->a.y != knots->d.y) {
191
  /* Intersection point (px):
192
   *     px = p1 + u(p2 - p1)
193
   *     (p - px) ∙ (p2 - p1) = 0
194
   * Thus:
195
   *     u = ((p - p1) ∙ (p2 - p1)) / ∥p2 - p1∥²;
196
   */
197
198
3.03M
  double dx, dy, u, v;
199
200
3.03M
  dx = _cairo_fixed_to_double (knots->d.x - knots->a.x);
201
3.03M
  dy = _cairo_fixed_to_double (knots->d.y - knots->a.y);
202
3.03M
   v = dx * dx + dy * dy;
203
204
3.03M
  u = bdx * dx + bdy * dy;
205
3.03M
  if (u <= 0) {
206
      /* bdx -= 0;
207
       * bdy -= 0;
208
       */
209
2.68M
  } else if (u >= v) {
210
465
      bdx -= dx;
211
465
      bdy -= dy;
212
2.68M
  } else {
213
2.68M
      bdx -= u/v * dx;
214
2.68M
      bdy -= u/v * dy;
215
2.68M
  }
216
217
3.03M
  u = cdx * dx + cdy * dy;
218
3.03M
  if (u <= 0) {
219
      /* cdx -= 0;
220
       * cdy -= 0;
221
       */
222
3.03M
  } else if (u >= v) {
223
344k
      cdx -= dx;
224
344k
      cdy -= dy;
225
2.68M
  } else {
226
2.68M
      cdx -= u/v * dx;
227
2.68M
      cdy -= u/v * dy;
228
2.68M
  }
229
3.03M
    }
230
231
3.03M
    berr = bdx * bdx + bdy * bdy;
232
3.03M
    cerr = cdx * cdx + cdy * cdy;
233
3.03M
    if (berr > cerr)
234
1.42M
  return berr;
235
1.60M
    else
236
1.60M
  return cerr;
237
3.03M
}
238
239
static cairo_status_t
240
_cairo_spline_decompose_into (cairo_spline_knots_t *s1,
241
            double tolerance_squared,
242
            cairo_spline_t *result)
243
3.03M
{
244
3.03M
    cairo_spline_knots_t s2;
245
3.03M
    cairo_status_t status;
246
247
3.03M
    if (_cairo_spline_error_squared (s1) < tolerance_squared)
248
1.77M
  return _cairo_spline_add_point (result, &s1->a, &s1->b);
249
250
1.25M
    _de_casteljau (s1, &s2);
251
252
1.25M
    status = _cairo_spline_decompose_into (s1, tolerance_squared, result);
253
1.25M
    if (unlikely (status))
254
0
  return status;
255
256
1.25M
    return _cairo_spline_decompose_into (&s2, tolerance_squared, result);
257
1.25M
}
258
259
cairo_status_t
260
_cairo_spline_decompose (cairo_spline_t *spline, double tolerance)
261
522k
{
262
522k
    cairo_spline_knots_t s1;
263
522k
    cairo_status_t status;
264
265
522k
    s1 = spline->knots;
266
522k
    spline->last_point = s1.a;
267
522k
    status = _cairo_spline_decompose_into (&s1, tolerance * tolerance, spline);
268
522k
    if (unlikely (status))
269
0
  return status;
270
271
522k
    return spline->add_point_func (spline->closure,
272
522k
           &spline->knots.d, &spline->final_slope);
273
522k
}
274
275
/* Note: this function is only good for computing bounds in device space. */
276
cairo_status_t
277
_cairo_spline_bound (cairo_spline_add_point_func_t add_point_func,
278
         void *closure,
279
         const cairo_point_t *p0, const cairo_point_t *p1,
280
         const cairo_point_t *p2, const cairo_point_t *p3)
281
9.71k
{
282
9.71k
    double x0, x1, x2, x3;
283
9.71k
    double y0, y1, y2, y3;
284
9.71k
    double a, b, c;
285
9.71k
    double t[4];
286
9.71k
    int t_num = 0, i;
287
9.71k
    cairo_status_t status;
288
289
9.71k
    x0 = _cairo_fixed_to_double (p0->x);
290
9.71k
    y0 = _cairo_fixed_to_double (p0->y);
291
9.71k
    x1 = _cairo_fixed_to_double (p1->x);
292
9.71k
    y1 = _cairo_fixed_to_double (p1->y);
293
9.71k
    x2 = _cairo_fixed_to_double (p2->x);
294
9.71k
    y2 = _cairo_fixed_to_double (p2->y);
295
9.71k
    x3 = _cairo_fixed_to_double (p3->x);
296
9.71k
    y3 = _cairo_fixed_to_double (p3->y);
297
298
    /* The spline can be written as a polynomial of the four points:
299
     *
300
     *   (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3
301
     *
302
     * for 0≤t≤1.  Now, the X and Y components of the spline follow the
303
     * same polynomial but with x and y replaced for p.  To find the
304
     * bounds of the spline, we just need to find the X and Y bounds.
305
     * To find the bound, we take the derivative and equal it to zero,
306
     * and solve to find the t's that give the extreme points.
307
     *
308
     * Here is the derivative of the curve, sorted on t:
309
     *
310
     *   3t²(-p0+3p1-3p2+p3) + 2t(3p0-6p1+3p2) -3p0+3p1
311
     *
312
     * Let:
313
     *
314
     *   a = -p0+3p1-3p2+p3
315
     *   b =  p0-2p1+p2
316
     *   c = -p0+p1
317
     *
318
     * Gives:
319
     *
320
     *   a.t² + 2b.t + c = 0
321
     *
322
     * With:
323
     *
324
     *   delta = b*b - a*c
325
     *
326
     * the extreme points are at -c/2b if a is zero, at (-b±√delta)/a if
327
     * delta is positive, and at -b/a if delta is zero.
328
     */
329
330
9.71k
#define ADD(t0) \
331
26.4k
    { \
332
26.4k
  double _t0 = (t0); \
333
26.4k
  if (0 < _t0 && _t0 < 1) \
334
26.4k
      t[t_num++] = _t0; \
335
26.4k
    }
336
337
9.71k
#define FIND_EXTREMES(a,b,c) \
338
19.4k
    { \
339
19.4k
  if (a == 0) { \
340
9.37k
      if (b != 0) \
341
9.37k
    ADD (-c / (2*b)); \
342
10.0k
  } else { \
343
10.0k
      double b2 = b * b; \
344
10.0k
      double delta = b2 - a * c; \
345
10.0k
      if (delta > 0) { \
346
8.93k
    cairo_bool_t feasible; \
347
8.93k
    double _2ab = 2 * a * b; \
348
    /* We are only interested in solutions t that satisfy 0<t<1 \
349
     * here.  We do some checks to avoid sqrt if the solutions \
350
     * are not in that range.  The checks can be derived from: \
351
     * \
352
     *   0 < (-b±√delta)/a < 1 \
353
     */ \
354
8.93k
    if (_2ab >= 0) \
355
8.93k
        feasible = delta > b2 && delta < a*a + b2 + _2ab; \
356
8.93k
    else if (-b / a >= 1) \
357
6.40k
        feasible = delta < b2 && delta > a*a + b2 + _2ab; \
358
6.40k
    else \
359
6.40k
        feasible = delta < b2 || delta < a*a + b2 + _2ab; \
360
8.93k
          \
361
8.93k
    if (unlikely (feasible)) { \
362
8.53k
        double sqrt_delta = sqrt (delta); \
363
8.53k
        ADD ((-b - sqrt_delta) / a); \
364
8.53k
        ADD ((-b + sqrt_delta) / a); \
365
8.53k
    } \
366
8.93k
      } else if (delta == 0) { \
367
6
    ADD (-b / a); \
368
6
      } \
369
10.0k
  } \
370
19.4k
    }
371
372
    /* Find X extremes */
373
9.71k
    a = -x0 + 3*x1 - 3*x2 + x3;
374
9.71k
    b =  x0 - 2*x1 + x2;
375
9.71k
    c = -x0 + x1;
376
9.71k
    FIND_EXTREMES (a, b, c);
377
378
    /* Find Y extremes */
379
9.71k
    a = -y0 + 3*y1 - 3*y2 + y3;
380
9.71k
    b =  y0 - 2*y1 + y2;
381
9.71k
    c = -y0 + y1;
382
9.71k
    FIND_EXTREMES (a, b, c);
383
384
9.71k
    status = add_point_func (closure, p0, NULL);
385
9.71k
    if (unlikely (status))
386
0
  return status;
387
388
30.3k
    for (i = 0; i < t_num; i++) {
389
20.6k
  cairo_point_t p;
390
20.6k
  double x, y;
391
20.6k
        double t_1_0, t_0_1;
392
20.6k
        double t_2_0, t_0_2;
393
20.6k
        double t_3_0, t_2_1_3, t_1_2_3, t_0_3;
394
395
20.6k
        t_1_0 = t[i];          /*      t  */
396
20.6k
        t_0_1 = 1 - t_1_0;     /* (1 - t) */
397
398
20.6k
        t_2_0 = t_1_0 * t_1_0; /*      t  *      t  */
399
20.6k
        t_0_2 = t_0_1 * t_0_1; /* (1 - t) * (1 - t) */
400
401
20.6k
        t_3_0   = t_2_0 * t_1_0;     /*      t  *      t  *      t      */
402
20.6k
        t_2_1_3 = t_2_0 * t_0_1 * 3; /*      t  *      t  * (1 - t) * 3 */
403
20.6k
        t_1_2_3 = t_1_0 * t_0_2 * 3; /*      t  * (1 - t) * (1 - t) * 3 */
404
20.6k
        t_0_3   = t_0_1 * t_0_2;     /* (1 - t) * (1 - t) * (1 - t)     */
405
406
        /* Bezier polynomial */
407
20.6k
        x = x0 * t_0_3
408
20.6k
          + x1 * t_1_2_3
409
20.6k
          + x2 * t_2_1_3
410
20.6k
          + x3 * t_3_0;
411
20.6k
        y = y0 * t_0_3
412
20.6k
          + y1 * t_1_2_3
413
20.6k
          + y2 * t_2_1_3
414
20.6k
          + y3 * t_3_0;
415
416
20.6k
  p.x = _cairo_fixed_from_double (x);
417
20.6k
  p.y = _cairo_fixed_from_double (y);
418
20.6k
  status = add_point_func (closure, &p, NULL);
419
20.6k
  if (unlikely (status))
420
0
      return status;
421
20.6k
    }
422
423
9.71k
    return add_point_func (closure, p3, NULL);
424
9.71k
}