2 * Copyright © 2004 Carl Worth
3 * Copyright © 2006 Red Hat, Inc.
4 * Copyright © 2008 Chris Wilson
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Carl Worth
34 * Carl D. Worth <cworth@cworth.org>
35 * Chris Wilson <chris@chris-wilson.co.uk>
38 /* Provide definitions for standalone compilation */
41 #include "cairo-error-private.h"
42 #include "cairo-freelist-private.h"
43 #include "cairo-combsort-inline.h"
45 typedef cairo_point_t cairo_bo_point32_t;
47 typedef struct _cairo_bo_intersect_ordinate {
49 enum { EXACT, INEXACT } exactness;
50 } cairo_bo_intersect_ordinate_t;
52 typedef struct _cairo_bo_intersect_point {
53 cairo_bo_intersect_ordinate_t x;
54 cairo_bo_intersect_ordinate_t y;
55 } cairo_bo_intersect_point_t;
57 typedef struct _cairo_bo_edge cairo_bo_edge_t;
59 typedef struct _cairo_bo_deferred {
60 cairo_bo_edge_t *other;
62 } cairo_bo_deferred_t;
64 struct _cairo_bo_edge {
67 cairo_bo_edge_t *prev;
68 cairo_bo_edge_t *next;
69 cairo_bo_deferred_t deferred;
72 /* the parent is always given by index/2 */
73 #define PQ_PARENT_INDEX(i) ((i) >> 1)
74 #define PQ_FIRST_ENTRY 1
76 /* left and right children are index * 2 and (index * 2) +1 respectively */
77 #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
80 CAIRO_BO_EVENT_TYPE_STOP,
81 CAIRO_BO_EVENT_TYPE_INTERSECTION,
82 CAIRO_BO_EVENT_TYPE_START
83 } cairo_bo_event_type_t;
85 typedef struct _cairo_bo_event {
86 cairo_bo_event_type_t type;
90 typedef struct _cairo_bo_start_event {
91 cairo_bo_event_type_t type;
94 } cairo_bo_start_event_t;
96 typedef struct _cairo_bo_queue_event {
97 cairo_bo_event_type_t type;
101 } cairo_bo_queue_event_t;
103 typedef struct _pqueue {
106 cairo_bo_event_t **elements;
107 cairo_bo_event_t *elements_embedded[1024];
110 typedef struct _cairo_bo_event_queue {
111 cairo_freepool_t pool;
113 cairo_bo_event_t **start_events;
114 } cairo_bo_event_queue_t;
116 typedef struct _cairo_bo_sweep_line {
117 cairo_bo_edge_t *head;
119 cairo_bo_edge_t *current_edge;
120 } cairo_bo_sweep_line_t;
123 _line_compute_intersection_x_for_y (const cairo_line_t *line,
134 dy = line->p2.y - line->p1.y;
136 x += _cairo_fixed_mul_div_floor (y - line->p1.y,
137 line->p2.x - line->p1.x,
145 _cairo_bo_point32_compare (cairo_bo_point32_t const *a,
146 cairo_bo_point32_t const *b)
157 /* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
158 * slope a is respectively greater than, equal to, or less than the
161 * For each edge, consider the direction vector formed from:
167 * (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
169 * We then define the slope of each edge as dx/dy, (which is the
170 * inverse of the slope typically used in math instruction). We never
171 * compute a slope directly as the value approaches infinity, but we
172 * can derive a slope comparison without division as follows, (where
173 * the ? represents our compare operator).
175 * 1. slope(a) ? slope(b)
176 * 2. adx/ady ? bdx/bdy
177 * 3. (adx * bdy) ? (bdx * ady)
179 * Note that from step 2 to step 3 there is no change needed in the
180 * sign of the result since both ady and bdy are guaranteed to be
181 * greater than or equal to 0.
183 * When using this slope comparison to sort edges, some care is needed
184 * when interpreting the results. Since the slope compare operates on
185 * distance vectors from top to bottom it gives a correct left to
186 * right sort for edges that have a common top point, (such as two
187 * edges with start events at the same location). On the other hand,
188 * the sense of the result will be exactly reversed for two edges that
189 * have a common stop point.
192 _slope_compare (const cairo_bo_edge_t *a,
193 const cairo_bo_edge_t *b)
195 /* XXX: We're assuming here that dx and dy will still fit in 32
196 * bits. That's not true in general as there could be overflow. We
197 * should prevent that before the tessellation algorithm
200 int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
201 int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
203 /* Since the dy's are all positive by construction we can fast
204 * path several common cases.
207 /* First check for vertical lines. */
213 /* Then where the two edges point in different directions wrt x. */
217 /* Finally we actually need to do the general comparison. */
219 int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
220 int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
221 cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
222 cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
224 return _cairo_int64_cmp (adx_bdy, bdx_ady);
229 * We need to compare the x-coordinates of a pair of lines for a particular y,
230 * without loss of precision.
232 * The x-coordinate along an edge for a given y is:
233 * X = A_x + (Y - A_y) * A_dx / A_dy
235 * So the inequality we wish to test is:
236 * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
237 * where ∘ is our inequality operator.
239 * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
240 * all positive, so we can rearrange it thus without causing a sign change:
241 * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
242 * - (Y - A_y) * A_dx * B_dy
244 * Given the assumption that all the deltas fit within 32 bits, we can compute
245 * this comparison directly using 128 bit arithmetic. For certain, but common,
246 * input we can reduce this down to a single 32 bit compare by inspecting the
249 * (And put the burden of the work on developing fast 128 bit ops, which are
250 * required throughout the tessellator.)
252 * See the similar discussion for _slope_compare().
255 edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
256 const cairo_bo_edge_t *b,
259 /* XXX: We're assuming here that dx and dy will still fit in 32
260 * bits. That's not true in general as there could be overflow. We
261 * should prevent that before the tessellation algorithm
271 HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
273 HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
274 HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
275 HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
276 } have_dx_adx_bdx = HAVE_ALL;
278 /* don't bother solving for abscissa if the edges' bounding boxes
279 * can be used to order them. */
283 if (a->edge.line.p1.x < a->edge.line.p2.x) {
284 amin = a->edge.line.p1.x;
285 amax = a->edge.line.p2.x;
287 amin = a->edge.line.p2.x;
288 amax = a->edge.line.p1.x;
290 if (b->edge.line.p1.x < b->edge.line.p2.x) {
291 bmin = b->edge.line.p1.x;
292 bmax = b->edge.line.p2.x;
294 bmin = b->edge.line.p2.x;
295 bmax = b->edge.line.p1.x;
297 if (amax < bmin) return -1;
298 if (amin > bmax) return +1;
301 ady = a->edge.line.p2.y - a->edge.line.p1.y;
302 adx = a->edge.line.p2.x - a->edge.line.p1.x;
304 have_dx_adx_bdx &= ~HAVE_ADX;
306 bdy = b->edge.line.p2.y - b->edge.line.p1.y;
307 bdx = b->edge.line.p2.x - b->edge.line.p1.x;
309 have_dx_adx_bdx &= ~HAVE_BDX;
311 dx = a->edge.line.p1.x - b->edge.line.p1.x;
313 have_dx_adx_bdx &= ~HAVE_DX;
315 #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
316 #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
317 #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
318 switch (have_dx_adx_bdx) {
323 /* A_dy * B_dy * (A_x - B_x) ∘ 0 */
324 return dx; /* ady * bdy is positive definite */
326 /* 0 ∘ - (Y - A_y) * A_dx * B_dy */
327 return adx; /* bdy * (y - a->top.y) is positive definite */
329 /* 0 ∘ (Y - B_y) * B_dx * A_dy */
330 return -bdx; /* ady * (y - b->top.y) is positive definite */
332 /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
333 if ((adx ^ bdx) < 0) {
335 } else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
336 cairo_int64_t adx_bdy, bdx_ady;
338 /* ∴ A_dx * B_dy ∘ B_dx * A_dy */
340 adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
341 bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
343 return _cairo_int64_cmp (adx_bdy, bdx_ady);
345 return _cairo_int128_cmp (A, B);
347 /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
348 if ((-adx ^ dx) < 0) {
351 cairo_int64_t ady_dx, dy_adx;
353 ady_dx = _cairo_int32x32_64_mul (ady, dx);
354 dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
356 return _cairo_int64_cmp (ady_dx, dy_adx);
359 /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
360 if ((bdx ^ dx) < 0) {
363 cairo_int64_t bdy_dx, dy_bdx;
365 bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
366 dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
368 return _cairo_int64_cmp (bdy_dx, dy_bdx);
371 /* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
372 return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
380 * We need to compare the x-coordinate of a line for a particular y wrt to a
381 * given x, without loss of precision.
383 * The x-coordinate along an edge for a given y is:
384 * X = A_x + (Y - A_y) * A_dx / A_dy
386 * So the inequality we wish to test is:
387 * A_x + (Y - A_y) * A_dx / A_dy ∘ X
388 * where ∘ is our inequality operator.
390 * By construction, we know that A_dy (and (Y - A_y)) are
391 * all positive, so we can rearrange it thus without causing a sign change:
392 * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
394 * Given the assumption that all the deltas fit within 32 bits, we can compute
395 * this comparison directly using 64 bit arithmetic.
397 * See the similar discussion for _slope_compare() and
398 * edges_compare_x_for_y_general().
401 edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
409 if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
411 if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
414 adx = a->edge.line.p2.x - a->edge.line.p1.x;
415 dx = x - a->edge.line.p1.x;
419 if (dx == 0 || (adx ^ dx) < 0)
422 dy = y - a->edge.line.p1.y;
423 ady = a->edge.line.p2.y - a->edge.line.p1.y;
425 L = _cairo_int32x32_64_mul (dy, adx);
426 R = _cairo_int32x32_64_mul (dx, ady);
428 return _cairo_int64_cmp (L, R);
432 edges_compare_x_for_y (const cairo_bo_edge_t *a,
433 const cairo_bo_edge_t *b,
436 /* If the sweep-line is currently on an end-point of a line,
437 * then we know its precise x value (and considering that we often need to
438 * compare events at end-points, this happens frequently enough to warrant
445 HAVE_BOTH = HAVE_AX | HAVE_BX
446 } have_ax_bx = HAVE_BOTH;
447 int32_t ax = 0, bx = 0;
449 if (y == a->edge.line.p1.y)
450 ax = a->edge.line.p1.x;
451 else if (y == a->edge.line.p2.y)
452 ax = a->edge.line.p2.x;
454 have_ax_bx &= ~HAVE_AX;
456 if (y == b->edge.line.p1.y)
457 bx = b->edge.line.p1.x;
458 else if (y == b->edge.line.p2.y)
459 bx = b->edge.line.p2.x;
461 have_ax_bx &= ~HAVE_BX;
463 switch (have_ax_bx) {
466 return edges_compare_x_for_y_general (a, b, y);
468 return -edge_compare_for_y_against_x (b, y, ax);
470 return edge_compare_for_y_against_x (a, y, bx);
477 _line_equal (const cairo_line_t *a, const cairo_line_t *b)
479 return a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
480 a->p2.x == b->p2.x && a->p2.y == b->p2.y;
484 _cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t *sweep_line,
485 const cairo_bo_edge_t *a,
486 const cairo_bo_edge_t *b)
490 /* compare the edges if not identical */
491 if (! _line_equal (&a->edge.line, &b->edge.line)) {
492 cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
496 /* The two edges intersect exactly at y, so fall back on slope
497 * comparison. We know that this compare_edges function will be
498 * called only when starting a new edge, (not when stopping an
499 * edge), so we don't have to worry about conditionally inverting
500 * the sense of _slope_compare. */
501 cmp = _slope_compare (a, b);
506 /* We've got two collinear edges now. */
507 return b->edge.bottom - a->edge.bottom;
510 static inline cairo_int64_t
511 det32_64 (int32_t a, int32_t b,
512 int32_t c, int32_t d)
514 /* det = a * d - b * c */
515 return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
516 _cairo_int32x32_64_mul (b, c));
519 static inline cairo_int128_t
520 det64x32_128 (cairo_int64_t a, int32_t b,
521 cairo_int64_t c, int32_t d)
523 /* det = a * d - b * c */
524 return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
525 _cairo_int64x32_128_mul (c, b));
528 /* Compute the intersection of two lines as defined by two edges. The
529 * result is provided as a coordinate pair of 128-bit integers.
531 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
532 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
535 intersect_lines (cairo_bo_edge_t *a,
537 cairo_bo_intersect_point_t *intersection)
539 cairo_int64_t a_det, b_det;
541 /* XXX: We're assuming here that dx and dy will still fit in 32
542 * bits. That's not true in general as there could be overflow. We
543 * should prevent that before the tessellation algorithm begins.
544 * What we're doing to mitigate this is to perform clamping in
545 * cairo_bo_tessellate_polygon().
547 int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
548 int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
550 int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
551 int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
553 cairo_int64_t den_det;
557 den_det = det32_64 (dx1, dy1, dx2, dy2);
559 /* Q: Can we determine that the lines do not intersect (within range)
560 * much more cheaply than computing the intersection point i.e. by
561 * avoiding the division?
563 * X = ax + t * adx = bx + s * bdx;
564 * Y = ay + t * ady = by + s * bdy;
565 * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
568 * Therefore we can reject any intersection (under the criteria for
569 * valid intersection events) if:
570 * L^R < 0 => t < 0, or
573 * (where top/bottom must at least extend to the line endpoints).
575 * A similar substitution can be performed for s, yielding:
576 * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
578 R = det32_64 (dx2, dy2,
579 b->edge.line.p1.x - a->edge.line.p1.x,
580 b->edge.line.p1.y - a->edge.line.p1.y);
581 if (_cairo_int64_negative (den_det)) {
582 if (_cairo_int64_ge (den_det, R))
585 if (_cairo_int64_le (den_det, R))
589 R = det32_64 (dy1, dx1,
590 a->edge.line.p1.y - b->edge.line.p1.y,
591 a->edge.line.p1.x - b->edge.line.p1.x);
592 if (_cairo_int64_negative (den_det)) {
593 if (_cairo_int64_ge (den_det, R))
596 if (_cairo_int64_le (den_det, R))
600 /* We now know that the two lines should intersect within range. */
602 a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
603 a->edge.line.p2.x, a->edge.line.p2.y);
604 b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
605 b->edge.line.p2.x, b->edge.line.p2.y);
607 /* x = det (a_det, dx1, b_det, dx2) / den_det */
608 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
611 if (_cairo_int64_eq (qr.rem, den_det))
614 intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
616 intersection->x.exactness = EXACT;
617 if (! _cairo_int64_is_zero (qr.rem)) {
618 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
619 qr.rem = _cairo_int64_negate (qr.rem);
620 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
621 if (_cairo_int64_ge (qr.rem, den_det)) {
622 qr.quo = _cairo_int64_add (qr.quo,
623 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
625 intersection->x.exactness = INEXACT;
628 intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo);
630 /* y = det (a_det, dy1, b_det, dy2) / den_det */
631 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
634 if (_cairo_int64_eq (qr.rem, den_det))
637 intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
639 intersection->y.exactness = EXACT;
640 if (! _cairo_int64_is_zero (qr.rem)) {
641 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
642 qr.rem = _cairo_int64_negate (qr.rem);
643 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
644 if (_cairo_int64_ge (qr.rem, den_det)) {
645 qr.quo = _cairo_int64_add (qr.quo,
646 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
648 intersection->y.exactness = INEXACT;
651 intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo);
657 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
660 /* First compare the quotient */
665 /* With quotient identical, if remainder is 0 then compare equal */
666 /* Otherwise, the non-zero remainder makes a > b */
667 return INEXACT == a.exactness;
670 /* Does the given edge contain the given point. The point must already
671 * be known to be contained within the line determined by the edge,
672 * (most likely the point results from an intersection of this edge
675 * If we had exact arithmetic, then this function would simply be a
676 * matter of examining whether the y value of the point lies within
677 * the range of y values of the edge. But since intersection points
678 * are not exact due to being rounded to the nearest integer within
679 * the available precision, we must also examine the x value of the
682 * The definition of "contains" here is that the given intersection
683 * point will be seen by the sweep line after the start event for the
684 * given edge and before the stop event for the edge. See the comments
685 * in the implementation for more details.
688 _cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
689 cairo_bo_intersect_point_t *point)
691 int cmp_top, cmp_bottom;
693 /* XXX: When running the actual algorithm, we don't actually need to
694 * compare against edge->top at all here, since any intersection above
695 * top is eliminated early via a slope comparison. We're leaving these
696 * here for now only for the sake of the quadratic-time intersection
697 * finder which needs them.
700 cmp_top = _cairo_bo_intersect_ordinate_32_compare (point->y,
702 cmp_bottom = _cairo_bo_intersect_ordinate_32_compare (point->y,
705 if (cmp_top < 0 || cmp_bottom > 0)
710 if (cmp_top > 0 && cmp_bottom < 0)
715 /* At this stage, the point lies on the same y value as either
716 * edge->top or edge->bottom, so we have to examine the x value in
717 * order to properly determine containment. */
719 /* If the y value of the point is the same as the y value of the
720 * top of the edge, then the x value of the point must be greater
721 * to be considered as inside the edge. Similarly, if the y value
722 * of the point is the same as the y value of the bottom of the
723 * edge, then the x value of the point must be less to be
724 * considered as inside. */
729 top_x = _line_compute_intersection_x_for_y (&edge->edge.line,
731 return _cairo_bo_intersect_ordinate_32_compare (point->x, top_x) > 0;
732 } else { /* cmp_bottom == 0 */
735 bot_x = _line_compute_intersection_x_for_y (&edge->edge.line,
737 return _cairo_bo_intersect_ordinate_32_compare (point->x, bot_x) < 0;
741 /* Compute the intersection of two edges. The result is provided as a
742 * coordinate pair of 128-bit integers.
744 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
745 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
746 * intersection of the lines defined by the edges occurs outside of
747 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
748 * are exactly parallel.
750 * Note that when determining if a candidate intersection is "inside"
751 * an edge, we consider both the infinitesimal shortening and the
752 * infinitesimal tilt rules described by John Hobby. Specifically, if
753 * the intersection is exactly the same as an edge point, it is
754 * effectively outside (no intersection is returned). Also, if the
755 * intersection point has the same
758 _cairo_bo_edge_intersect (cairo_bo_edge_t *a,
760 cairo_bo_point32_t *intersection)
762 cairo_bo_intersect_point_t quorem;
764 if (! intersect_lines (a, b, &quorem))
767 if (! _cairo_bo_edge_contains_intersect_point (a, &quorem))
770 if (! _cairo_bo_edge_contains_intersect_point (b, &quorem))
773 /* Now that we've correctly compared the intersection point and
774 * determined that it lies within the edge, then we know that we
775 * no longer need any more bits of storage for the intersection
776 * than we do for our edge coordinates. We also no longer need the
777 * remainder from the division. */
778 intersection->x = quorem.x.ordinate;
779 intersection->y = quorem.y.ordinate;
785 cairo_bo_event_compare (const cairo_bo_event_t *a,
786 const cairo_bo_event_t *b)
790 cmp = _cairo_bo_point32_compare (&a->point, &b->point);
794 cmp = a->type - b->type;
802 _pqueue_init (pqueue_t *pq)
804 pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
807 pq->elements = pq->elements_embedded;
811 _pqueue_fini (pqueue_t *pq)
813 if (pq->elements != pq->elements_embedded)
817 static cairo_status_t
818 _pqueue_grow (pqueue_t *pq)
820 cairo_bo_event_t **new_elements;
823 if (pq->elements == pq->elements_embedded) {
824 new_elements = _cairo_malloc_ab (pq->max_size,
825 sizeof (cairo_bo_event_t *));
826 if (unlikely (new_elements == NULL))
827 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
829 memcpy (new_elements, pq->elements_embedded,
830 sizeof (pq->elements_embedded));
832 new_elements = _cairo_realloc_ab (pq->elements,
834 sizeof (cairo_bo_event_t *));
835 if (unlikely (new_elements == NULL))
836 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
839 pq->elements = new_elements;
840 return CAIRO_STATUS_SUCCESS;
843 static inline cairo_status_t
844 _pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
846 cairo_bo_event_t **elements;
849 if (unlikely (pq->size + 1 == pq->max_size)) {
850 cairo_status_t status;
852 status = _pqueue_grow (pq);
853 if (unlikely (status))
857 elements = pq->elements;
860 i != PQ_FIRST_ENTRY &&
861 cairo_bo_event_compare (event,
862 elements[parent = PQ_PARENT_INDEX (i)]) < 0;
865 elements[i] = elements[parent];
870 return CAIRO_STATUS_SUCCESS;
874 _pqueue_pop (pqueue_t *pq)
876 cairo_bo_event_t **elements = pq->elements;
877 cairo_bo_event_t *tail;
880 tail = elements[pq->size--];
882 elements[PQ_FIRST_ENTRY] = NULL;
886 for (i = PQ_FIRST_ENTRY;
887 (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
890 if (child != pq->size &&
891 cairo_bo_event_compare (elements[child+1],
892 elements[child]) < 0)
897 if (cairo_bo_event_compare (elements[child], tail) >= 0)
900 elements[i] = elements[child];
905 static inline cairo_status_t
906 _cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
907 cairo_bo_event_type_t type,
910 const cairo_point_t *point)
912 cairo_bo_queue_event_t *event;
914 event = _cairo_freepool_alloc (&queue->pool);
915 if (unlikely (event == NULL))
916 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
921 event->point = *point;
923 return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
927 _cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
928 cairo_bo_event_t *event)
930 _cairo_freepool_free (&queue->pool, event);
933 static cairo_bo_event_t *
934 _cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
936 cairo_bo_event_t *event, *cmp;
938 event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
939 cmp = *event_queue->start_events;
941 (cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
944 event_queue->start_events++;
948 _pqueue_pop (&event_queue->pqueue);
954 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
956 cairo_bo_event_compare)
959 _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
960 cairo_bo_event_t **start_events,
963 _cairo_bo_event_queue_sort (start_events, num_events);
964 start_events[num_events] = NULL;
966 event_queue->start_events = start_events;
968 _cairo_freepool_init (&event_queue->pool,
969 sizeof (cairo_bo_queue_event_t));
970 _pqueue_init (&event_queue->pqueue);
971 event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
974 static cairo_status_t
975 event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
976 cairo_bo_edge_t *edge)
978 cairo_bo_point32_t point;
980 point.y = edge->edge.bottom;
981 point.x = _line_compute_intersection_x_for_y (&edge->edge.line,
983 return _cairo_bo_event_queue_insert (event_queue,
984 CAIRO_BO_EVENT_TYPE_STOP,
990 _cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
992 _pqueue_fini (&event_queue->pqueue);
993 _cairo_freepool_fini (&event_queue->pool);
996 static inline cairo_status_t
997 event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
998 cairo_bo_edge_t *left,
999 cairo_bo_edge_t *right)
1001 cairo_bo_point32_t intersection;
1003 if (_line_equal (&left->edge.line, &right->edge.line))
1004 return CAIRO_STATUS_SUCCESS;
1006 /* The names "left" and "right" here are correct descriptions of
1007 * the order of the two edges within the active edge list. So if a
1008 * slope comparison also puts left less than right, then we know
1009 * that the intersection of these two segments has already
1010 * occurred before the current sweep line position. */
1011 if (_slope_compare (left, right) <= 0)
1012 return CAIRO_STATUS_SUCCESS;
1014 if (! _cairo_bo_edge_intersect (left, right, &intersection))
1015 return CAIRO_STATUS_SUCCESS;
1017 return _cairo_bo_event_queue_insert (event_queue,
1018 CAIRO_BO_EVENT_TYPE_INTERSECTION,
1024 _cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
1026 sweep_line->head = NULL;
1027 sweep_line->current_y = INT32_MIN;
1028 sweep_line->current_edge = NULL;
1031 static cairo_status_t
1032 sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
1033 cairo_bo_edge_t *edge)
1035 if (sweep_line->current_edge != NULL) {
1036 cairo_bo_edge_t *prev, *next;
1039 cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
1040 sweep_line->current_edge,
1043 prev = sweep_line->current_edge;
1045 while (next != NULL &&
1046 _cairo_bo_sweep_line_compare_edges (sweep_line,
1049 prev = next, next = prev->next;
1057 } else if (cmp > 0) {
1058 next = sweep_line->current_edge;
1060 while (prev != NULL &&
1061 _cairo_bo_sweep_line_compare_edges (sweep_line,
1064 next = prev, prev = next->prev;
1073 sweep_line->head = edge;
1075 prev = sweep_line->current_edge;
1077 edge->next = prev->next;
1078 if (prev->next != NULL)
1079 prev->next->prev = edge;
1083 sweep_line->head = edge;
1086 sweep_line->current_edge = edge;
1088 return CAIRO_STATUS_SUCCESS;
1092 _cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
1093 cairo_bo_edge_t *edge)
1095 if (edge->prev != NULL)
1096 edge->prev->next = edge->next;
1098 sweep_line->head = edge->next;
1100 if (edge->next != NULL)
1101 edge->next->prev = edge->prev;
1103 if (sweep_line->current_edge == edge)
1104 sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
1108 _cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
1109 cairo_bo_edge_t *left,
1110 cairo_bo_edge_t *right)
1112 if (left->prev != NULL)
1113 left->prev->next = right;
1115 sweep_line->head = right;
1117 if (right->next != NULL)
1118 right->next->prev = left;
1120 left->next = right->next;
1123 right->prev = left->prev;
1127 static inline cairo_bool_t
1128 edges_colinear (const cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
1130 if (_line_equal (&a->edge.line, &b->edge.line))
1133 if (_slope_compare (a, b))
1136 /* The choice of y is not truly arbitrary since we must guarantee that it
1137 * is greater than the start of either line.
1139 if (a->edge.line.p1.y == b->edge.line.p1.y) {
1140 return a->edge.line.p1.x == b->edge.line.p1.x;
1141 } else if (a->edge.line.p1.y < b->edge.line.p1.y) {
1142 return edge_compare_for_y_against_x (b,
1144 a->edge.line.p1.x) == 0;
1146 return edge_compare_for_y_against_x (a,
1148 b->edge.line.p1.x) == 0;
1153 edges_end (cairo_bo_edge_t *left,
1155 cairo_polygon_t *polygon)
1157 cairo_bo_deferred_t *l = &left->deferred;
1158 cairo_bo_edge_t *right = l->other;
1160 assert(right->deferred.other == NULL);
1161 if (likely (l->top < bot)) {
1162 _cairo_polygon_add_line (polygon, &left->edge.line, l->top, bot, 1);
1163 _cairo_polygon_add_line (polygon, &right->edge.line, l->top, bot, -1);
1170 edges_start_or_continue (cairo_bo_edge_t *left,
1171 cairo_bo_edge_t *right,
1173 cairo_polygon_t *polygon)
1175 assert (right->deferred.other == NULL);
1177 if (left->deferred.other == right)
1180 if (left->deferred.other != NULL) {
1181 if (right != NULL && edges_colinear (left->deferred.other, right)) {
1182 cairo_bo_edge_t *old = left->deferred.other;
1184 /* continuation on right, extend right to cover both */
1185 assert (old->deferred.other == NULL);
1186 assert (old->edge.line.p2.y > old->edge.line.p1.y);
1188 if (old->edge.line.p1.y < right->edge.line.p1.y)
1189 right->edge.line.p1 = old->edge.line.p1;
1190 if (old->edge.line.p2.y > right->edge.line.p2.y)
1191 right->edge.line.p2 = old->edge.line.p2;
1192 left->deferred.other = right;
1196 edges_end (left, top, polygon);
1199 if (right != NULL && ! edges_colinear (left, right)) {
1200 left->deferred.top = top;
1201 left->deferred.other = right;
1205 #define is_zero(w) ((w)[0] == 0 || (w)[1] == 0)
1208 active_edges (cairo_bo_edge_t *left,
1210 cairo_polygon_t *polygon)
1212 cairo_bo_edge_t *right;
1213 int winding[2] = {0, 0};
1215 /* Yes, this is naive. Consider this a placeholder. */
1217 while (left != NULL) {
1218 assert (is_zero (winding));
1221 winding[left->a_or_b] += left->edge.dir;
1222 if (! is_zero (winding))
1225 if unlikely ((left->deferred.other))
1226 edges_end (left, top, polygon);
1235 if unlikely ((right->deferred.other))
1236 edges_end (right, top, polygon);
1238 winding[right->a_or_b] += right->edge.dir;
1239 if (is_zero (winding)) {
1240 if (right->next == NULL ||
1241 ! edges_colinear (right, right->next))
1245 right = right->next;
1248 edges_start_or_continue (left, right, top, polygon);
1254 static cairo_status_t
1255 intersection_sweep (cairo_bo_event_t **start_events,
1257 cairo_polygon_t *polygon)
1259 cairo_status_t status = CAIRO_STATUS_SUCCESS; /* silence compiler */
1260 cairo_bo_event_queue_t event_queue;
1261 cairo_bo_sweep_line_t sweep_line;
1262 cairo_bo_event_t *event;
1263 cairo_bo_edge_t *left, *right;
1264 cairo_bo_edge_t *e1, *e2;
1266 _cairo_bo_event_queue_init (&event_queue, start_events, num_events);
1267 _cairo_bo_sweep_line_init (&sweep_line);
1269 while ((event = _cairo_bo_event_dequeue (&event_queue))) {
1270 if (event->point.y != sweep_line.current_y) {
1271 active_edges (sweep_line.head,
1272 sweep_line.current_y,
1274 sweep_line.current_y = event->point.y;
1277 switch (event->type) {
1278 case CAIRO_BO_EVENT_TYPE_START:
1279 e1 = &((cairo_bo_start_event_t *) event)->edge;
1281 status = sweep_line_insert (&sweep_line, e1);
1282 if (unlikely (status))
1285 status = event_queue_insert_stop (&event_queue, e1);
1286 if (unlikely (status))
1293 status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
1294 if (unlikely (status))
1298 if (right != NULL) {
1299 status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1300 if (unlikely (status))
1306 case CAIRO_BO_EVENT_TYPE_STOP:
1307 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1308 _cairo_bo_event_queue_delete (&event_queue, event);
1310 if (e1->deferred.other)
1311 edges_end (e1, sweep_line.current_y, polygon);
1316 _cairo_bo_sweep_line_delete (&sweep_line, e1);
1318 if (left != NULL && right != NULL) {
1319 status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
1320 if (unlikely (status))
1326 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1327 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1328 e2 = ((cairo_bo_queue_event_t *) event)->e2;
1329 _cairo_bo_event_queue_delete (&event_queue, event);
1331 /* skip this intersection if its edges are not adjacent */
1335 if (e1->deferred.other)
1336 edges_end (e1, sweep_line.current_y, polygon);
1337 if (e2->deferred.other)
1338 edges_end (e2, sweep_line.current_y, polygon);
1343 _cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
1345 /* after the swap e2 is left of e1 */
1348 status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
1349 if (unlikely (status))
1353 if (right != NULL) {
1354 status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1355 if (unlikely (status))
1364 _cairo_bo_event_queue_fini (&event_queue);
1370 _cairo_polygon_intersect (cairo_polygon_t *a, int winding_a,
1371 cairo_polygon_t *b, int winding_b)
1373 cairo_status_t status;
1374 cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
1375 cairo_bo_start_event_t *events;
1376 cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
1377 cairo_bo_event_t **event_ptrs;
1382 if (winding_a != CAIRO_FILL_RULE_WINDING) {
1383 status = _cairo_polygon_reduce (a, winding_a);
1384 if (unlikely (status))
1388 if (winding_b != CAIRO_FILL_RULE_WINDING) {
1389 status = _cairo_polygon_reduce (b, winding_b);
1390 if (unlikely (status))
1394 if (unlikely (0 == a->num_edges))
1395 return CAIRO_STATUS_SUCCESS;
1397 if (unlikely (0 == b->num_edges)) {
1399 return CAIRO_STATUS_SUCCESS;
1402 events = stack_events;
1403 event_ptrs = stack_event_ptrs;
1404 num_events = a->num_edges + b->num_edges;
1405 if (num_events > ARRAY_LENGTH (stack_events)) {
1406 events = _cairo_malloc_ab_plus_c (num_events,
1407 sizeof (cairo_bo_start_event_t) +
1408 sizeof (cairo_bo_event_t *),
1409 sizeof (cairo_bo_event_t *));
1410 if (unlikely (events == NULL))
1411 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1413 event_ptrs = (cairo_bo_event_t **) (events + num_events);
1417 for (i = 0; i < a->num_edges; i++) {
1418 event_ptrs[j] = (cairo_bo_event_t *) &events[j];
1420 events[j].type = CAIRO_BO_EVENT_TYPE_START;
1421 events[j].point.y = a->edges[i].top;
1423 _line_compute_intersection_x_for_y (&a->edges[i].line,
1426 events[j].edge.a_or_b = 0;
1427 events[j].edge.edge = a->edges[i];
1428 events[j].edge.deferred.other = NULL;
1429 events[j].edge.prev = NULL;
1430 events[j].edge.next = NULL;
1434 for (i = 0; i < b->num_edges; i++) {
1435 event_ptrs[j] = (cairo_bo_event_t *) &events[j];
1437 events[j].type = CAIRO_BO_EVENT_TYPE_START;
1438 events[j].point.y = b->edges[i].top;
1440 _line_compute_intersection_x_for_y (&b->edges[i].line,
1443 events[j].edge.a_or_b = 1;
1444 events[j].edge.edge = b->edges[i];
1445 events[j].edge.deferred.other = NULL;
1446 events[j].edge.prev = NULL;
1447 events[j].edge.next = NULL;
1450 assert (j == num_events);
1454 FILE *file = fopen ("clip_a.txt", "w");
1455 _cairo_debug_print_polygon (file, a);
1459 FILE *file = fopen ("clip_b.txt", "w");
1460 _cairo_debug_print_polygon (file, b);
1466 status = intersection_sweep (event_ptrs, num_events, a);
1467 if (events != stack_events)
1472 FILE *file = fopen ("clip_result.txt", "w");
1473 _cairo_debug_print_polygon (file, a);
1482 _cairo_polygon_intersect_with_boxes (cairo_polygon_t *polygon,
1483 cairo_fill_rule_t *winding,
1488 cairo_status_t status;
1491 if (num_boxes == 0) {
1492 polygon->num_edges = 0;
1493 return CAIRO_STATUS_SUCCESS;
1496 for (n = 0; n < num_boxes; n++) {
1497 if (polygon->extents.p1.x >= boxes[n].p1.x &&
1498 polygon->extents.p2.x <= boxes[n].p2.x &&
1499 polygon->extents.p1.y >= boxes[n].p1.y &&
1500 polygon->extents.p2.y <= boxes[n].p2.y)
1502 return CAIRO_STATUS_SUCCESS;
1506 _cairo_polygon_init (&b, NULL, 0);
1507 for (n = 0; n < num_boxes; n++) {
1508 if (boxes[n].p2.x > polygon->extents.p1.x &&
1509 boxes[n].p1.x < polygon->extents.p2.x &&
1510 boxes[n].p2.y > polygon->extents.p1.y &&
1511 boxes[n].p1.y < polygon->extents.p2.y)
1513 cairo_point_t p1, p2;
1515 p1.y = boxes[n].p1.y;
1516 p2.y = boxes[n].p2.y;
1518 p2.x = p1.x = boxes[n].p1.x;
1519 _cairo_polygon_add_external_edge (&b, &p1, &p2);
1521 p2.x = p1.x = boxes[n].p2.x;
1522 _cairo_polygon_add_external_edge (&b, &p2, &p1);
1526 status = _cairo_polygon_intersect (polygon, *winding,
1527 &b, CAIRO_FILL_RULE_WINDING);
1528 _cairo_polygon_fini (&b);
1530 *winding = CAIRO_FILL_RULE_WINDING;