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"
44 #include "cairo-traps-private.h"
46 #define DEBUG_PRINT_STATE 0
47 #define DEBUG_EVENTS 0
50 typedef cairo_point_t cairo_bo_point32_t;
52 typedef struct _cairo_bo_intersect_ordinate {
54 enum { EXACT, INEXACT } exactness;
55 } cairo_bo_intersect_ordinate_t;
57 typedef struct _cairo_bo_intersect_point {
58 cairo_bo_intersect_ordinate_t x;
59 cairo_bo_intersect_ordinate_t y;
60 } cairo_bo_intersect_point_t;
62 typedef struct _cairo_bo_edge cairo_bo_edge_t;
63 typedef struct _cairo_bo_trap cairo_bo_trap_t;
65 /* A deferred trapezoid of an edge */
66 struct _cairo_bo_trap {
67 cairo_bo_edge_t *right;
71 struct _cairo_bo_edge {
73 cairo_bo_edge_t *prev;
74 cairo_bo_edge_t *next;
75 cairo_bo_edge_t *colinear;
76 cairo_bo_trap_t deferred_trap;
79 /* the parent is always given by index/2 */
80 #define PQ_PARENT_INDEX(i) ((i) >> 1)
81 #define PQ_FIRST_ENTRY 1
83 /* left and right children are index * 2 and (index * 2) +1 respectively */
84 #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
87 CAIRO_BO_EVENT_TYPE_STOP,
88 CAIRO_BO_EVENT_TYPE_INTERSECTION,
89 CAIRO_BO_EVENT_TYPE_START
90 } cairo_bo_event_type_t;
92 typedef struct _cairo_bo_event {
93 cairo_bo_event_type_t type;
97 typedef struct _cairo_bo_start_event {
98 cairo_bo_event_type_t type;
100 cairo_bo_edge_t edge;
101 } cairo_bo_start_event_t;
103 typedef struct _cairo_bo_queue_event {
104 cairo_bo_event_type_t type;
108 } cairo_bo_queue_event_t;
110 typedef struct _pqueue {
113 cairo_bo_event_t **elements;
114 cairo_bo_event_t *elements_embedded[1024];
117 typedef struct _cairo_bo_event_queue {
118 cairo_freepool_t pool;
120 cairo_bo_event_t **start_events;
121 } cairo_bo_event_queue_t;
123 typedef struct _cairo_bo_sweep_line {
124 cairo_bo_edge_t *head;
125 cairo_bo_edge_t *stopped;
127 cairo_bo_edge_t *current_edge;
128 } cairo_bo_sweep_line_t;
132 dump_traps (cairo_traps_t *traps, const char *filename)
138 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
142 if (traps->has_limits) {
143 printf ("%s: limits=(%d, %d, %d, %d)\n",
145 traps->limits.p1.x, traps->limits.p1.y,
146 traps->limits.p2.x, traps->limits.p2.y);
149 _cairo_traps_extents (traps, &extents);
150 printf ("%s: extents=(%d, %d, %d, %d)\n",
152 extents.p1.x, extents.p1.y,
153 extents.p2.x, extents.p2.y);
155 file = fopen (filename, "a");
157 for (n = 0; n < traps->num_traps; n++) {
158 fprintf (file, "%d %d L:(%d, %d), (%d, %d) R:(%d, %d), (%d, %d)\n",
160 traps->traps[n].bottom,
161 traps->traps[n].left.p1.x,
162 traps->traps[n].left.p1.y,
163 traps->traps[n].left.p2.x,
164 traps->traps[n].left.p2.y,
165 traps->traps[n].right.p1.x,
166 traps->traps[n].right.p1.y,
167 traps->traps[n].right.p2.x,
168 traps->traps[n].right.p2.y);
170 fprintf (file, "\n");
176 dump_edges (cairo_bo_start_event_t *events,
178 const char *filename)
183 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
186 file = fopen (filename, "a");
188 for (n = 0; n < num_edges; n++) {
189 fprintf (file, "(%d, %d), (%d, %d) %d %d %d\n",
190 events[n].edge.edge.line.p1.x,
191 events[n].edge.edge.line.p1.y,
192 events[n].edge.edge.line.p2.x,
193 events[n].edge.edge.line.p2.y,
194 events[n].edge.edge.top,
195 events[n].edge.edge.bottom,
196 events[n].edge.edge.dir);
198 fprintf (file, "\n");
205 _line_compute_intersection_x_for_y (const cairo_line_t *line,
216 dy = line->p2.y - line->p1.y;
218 x += _cairo_fixed_mul_div_floor (y - line->p1.y,
219 line->p2.x - line->p1.x,
227 _cairo_bo_point32_compare (cairo_bo_point32_t const *a,
228 cairo_bo_point32_t const *b)
239 /* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
240 * slope a is respectively greater than, equal to, or less than the
243 * For each edge, consider the direction vector formed from:
249 * (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
251 * We then define the slope of each edge as dx/dy, (which is the
252 * inverse of the slope typically used in math instruction). We never
253 * compute a slope directly as the value approaches infinity, but we
254 * can derive a slope comparison without division as follows, (where
255 * the ? represents our compare operator).
257 * 1. slope(a) ? slope(b)
258 * 2. adx/ady ? bdx/bdy
259 * 3. (adx * bdy) ? (bdx * ady)
261 * Note that from step 2 to step 3 there is no change needed in the
262 * sign of the result since both ady and bdy are guaranteed to be
263 * greater than or equal to 0.
265 * When using this slope comparison to sort edges, some care is needed
266 * when interpreting the results. Since the slope compare operates on
267 * distance vectors from top to bottom it gives a correct left to
268 * right sort for edges that have a common top point, (such as two
269 * edges with start events at the same location). On the other hand,
270 * the sense of the result will be exactly reversed for two edges that
271 * have a common stop point.
274 _slope_compare (const cairo_bo_edge_t *a,
275 const cairo_bo_edge_t *b)
277 /* XXX: We're assuming here that dx and dy will still fit in 32
278 * bits. That's not true in general as there could be overflow. We
279 * should prevent that before the tessellation algorithm
282 int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
283 int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
285 /* Since the dy's are all positive by construction we can fast
286 * path several common cases.
289 /* First check for vertical lines. */
295 /* Then where the two edges point in different directions wrt x. */
299 /* Finally we actually need to do the general comparison. */
301 int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
302 int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
303 cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
304 cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
306 return _cairo_int64_cmp (adx_bdy, bdx_ady);
311 * We need to compare the x-coordinates of a pair of lines for a particular y,
312 * without loss of precision.
314 * The x-coordinate along an edge for a given y is:
315 * X = A_x + (Y - A_y) * A_dx / A_dy
317 * So the inequality we wish to test is:
318 * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
319 * where ∘ is our inequality operator.
321 * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
322 * all positive, so we can rearrange it thus without causing a sign change:
323 * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
324 * - (Y - A_y) * A_dx * B_dy
326 * Given the assumption that all the deltas fit within 32 bits, we can compute
327 * this comparison directly using 128 bit arithmetic. For certain, but common,
328 * input we can reduce this down to a single 32 bit compare by inspecting the
331 * (And put the burden of the work on developing fast 128 bit ops, which are
332 * required throughout the tessellator.)
334 * See the similar discussion for _slope_compare().
337 edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
338 const cairo_bo_edge_t *b,
341 /* XXX: We're assuming here that dx and dy will still fit in 32
342 * bits. That's not true in general as there could be overflow. We
343 * should prevent that before the tessellation algorithm
353 HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
355 HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
356 HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
357 HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
358 } have_dx_adx_bdx = HAVE_ALL;
360 /* don't bother solving for abscissa if the edges' bounding boxes
361 * can be used to order them. */
365 if (a->edge.line.p1.x < a->edge.line.p2.x) {
366 amin = a->edge.line.p1.x;
367 amax = a->edge.line.p2.x;
369 amin = a->edge.line.p2.x;
370 amax = a->edge.line.p1.x;
372 if (b->edge.line.p1.x < b->edge.line.p2.x) {
373 bmin = b->edge.line.p1.x;
374 bmax = b->edge.line.p2.x;
376 bmin = b->edge.line.p2.x;
377 bmax = b->edge.line.p1.x;
379 if (amax < bmin) return -1;
380 if (amin > bmax) return +1;
383 ady = a->edge.line.p2.y - a->edge.line.p1.y;
384 adx = a->edge.line.p2.x - a->edge.line.p1.x;
386 have_dx_adx_bdx &= ~HAVE_ADX;
388 bdy = b->edge.line.p2.y - b->edge.line.p1.y;
389 bdx = b->edge.line.p2.x - b->edge.line.p1.x;
391 have_dx_adx_bdx &= ~HAVE_BDX;
393 dx = a->edge.line.p1.x - b->edge.line.p1.x;
395 have_dx_adx_bdx &= ~HAVE_DX;
397 #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
398 #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
399 #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
400 switch (have_dx_adx_bdx) {
405 /* A_dy * B_dy * (A_x - B_x) ∘ 0 */
406 return dx; /* ady * bdy is positive definite */
408 /* 0 ∘ - (Y - A_y) * A_dx * B_dy */
409 return adx; /* bdy * (y - a->top.y) is positive definite */
411 /* 0 ∘ (Y - B_y) * B_dx * A_dy */
412 return -bdx; /* ady * (y - b->top.y) is positive definite */
414 /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
415 if ((adx ^ bdx) < 0) {
417 } else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
418 cairo_int64_t adx_bdy, bdx_ady;
420 /* ∴ A_dx * B_dy ∘ B_dx * A_dy */
422 adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
423 bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
425 return _cairo_int64_cmp (adx_bdy, bdx_ady);
427 return _cairo_int128_cmp (A, B);
429 /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
430 if ((-adx ^ dx) < 0) {
433 cairo_int64_t ady_dx, dy_adx;
435 ady_dx = _cairo_int32x32_64_mul (ady, dx);
436 dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
438 return _cairo_int64_cmp (ady_dx, dy_adx);
441 /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
442 if ((bdx ^ dx) < 0) {
445 cairo_int64_t bdy_dx, dy_bdx;
447 bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
448 dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
450 return _cairo_int64_cmp (bdy_dx, dy_bdx);
453 /* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
454 return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
462 * We need to compare the x-coordinate of a line for a particular y wrt to a
463 * given x, without loss of precision.
465 * The x-coordinate along an edge for a given y is:
466 * X = A_x + (Y - A_y) * A_dx / A_dy
468 * So the inequality we wish to test is:
469 * A_x + (Y - A_y) * A_dx / A_dy ∘ X
470 * where ∘ is our inequality operator.
472 * By construction, we know that A_dy (and (Y - A_y)) are
473 * all positive, so we can rearrange it thus without causing a sign change:
474 * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
476 * Given the assumption that all the deltas fit within 32 bits, we can compute
477 * this comparison directly using 64 bit arithmetic.
479 * See the similar discussion for _slope_compare() and
480 * edges_compare_x_for_y_general().
483 edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
491 if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
493 if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
496 adx = a->edge.line.p2.x - a->edge.line.p1.x;
497 dx = x - a->edge.line.p1.x;
501 if (dx == 0 || (adx ^ dx) < 0)
504 dy = y - a->edge.line.p1.y;
505 ady = a->edge.line.p2.y - a->edge.line.p1.y;
507 L = _cairo_int32x32_64_mul (dy, adx);
508 R = _cairo_int32x32_64_mul (dx, ady);
510 return _cairo_int64_cmp (L, R);
514 edges_compare_x_for_y (const cairo_bo_edge_t *a,
515 const cairo_bo_edge_t *b,
518 /* If the sweep-line is currently on an end-point of a line,
519 * then we know its precise x value (and considering that we often need to
520 * compare events at end-points, this happens frequently enough to warrant
527 HAVE_BOTH = HAVE_AX | HAVE_BX
528 } have_ax_bx = HAVE_BOTH;
531 if (y == a->edge.line.p1.y)
532 ax = a->edge.line.p1.x;
533 else if (y == a->edge.line.p2.y)
534 ax = a->edge.line.p2.x;
536 have_ax_bx &= ~HAVE_AX;
538 if (y == b->edge.line.p1.y)
539 bx = b->edge.line.p1.x;
540 else if (y == b->edge.line.p2.y)
541 bx = b->edge.line.p2.x;
543 have_ax_bx &= ~HAVE_BX;
545 switch (have_ax_bx) {
548 return edges_compare_x_for_y_general (a, b, y);
550 return -edge_compare_for_y_against_x (b, y, ax);
552 return edge_compare_for_y_against_x (a, y, bx);
559 _line_equal (const cairo_line_t *a, const cairo_line_t *b)
561 return a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
562 a->p2.x == b->p2.x && a->p2.y == b->p2.y;
566 _cairo_bo_sweep_line_compare_edges (const cairo_bo_sweep_line_t *sweep_line,
567 const cairo_bo_edge_t *a,
568 const cairo_bo_edge_t *b)
572 /* compare the edges if not identical */
573 if (! _line_equal (&a->edge.line, &b->edge.line)) {
574 if (MAX (a->edge.line.p1.x, a->edge.line.p2.x) <
575 MIN (b->edge.line.p1.x, b->edge.line.p2.x))
577 else if (MIN (a->edge.line.p1.x, a->edge.line.p2.x) >
578 MAX (b->edge.line.p1.x, b->edge.line.p2.x))
581 cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
585 /* The two edges intersect exactly at y, so fall back on slope
586 * comparison. We know that this compare_edges function will be
587 * called only when starting a new edge, (not when stopping an
588 * edge), so we don't have to worry about conditionally inverting
589 * the sense of _slope_compare. */
590 cmp = _slope_compare (a, b);
595 /* We've got two collinear edges now. */
596 return b->edge.bottom - a->edge.bottom;
599 static inline cairo_int64_t
600 det32_64 (int32_t a, int32_t b,
601 int32_t c, int32_t d)
603 /* det = a * d - b * c */
604 return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
605 _cairo_int32x32_64_mul (b, c));
608 static inline cairo_int128_t
609 det64x32_128 (cairo_int64_t a, int32_t b,
610 cairo_int64_t c, int32_t d)
612 /* det = a * d - b * c */
613 return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
614 _cairo_int64x32_128_mul (c, b));
617 /* Compute the intersection of two lines as defined by two edges. The
618 * result is provided as a coordinate pair of 128-bit integers.
620 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
621 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
624 intersect_lines (cairo_bo_edge_t *a,
626 cairo_bo_intersect_point_t *intersection)
628 cairo_int64_t a_det, b_det;
630 /* XXX: We're assuming here that dx and dy will still fit in 32
631 * bits. That's not true in general as there could be overflow. We
632 * should prevent that before the tessellation algorithm begins.
633 * What we're doing to mitigate this is to perform clamping in
634 * cairo_bo_tessellate_polygon().
636 int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
637 int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
639 int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
640 int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
642 cairo_int64_t den_det;
646 den_det = det32_64 (dx1, dy1, dx2, dy2);
648 /* Q: Can we determine that the lines do not intersect (within range)
649 * much more cheaply than computing the intersection point i.e. by
650 * avoiding the division?
652 * X = ax + t * adx = bx + s * bdx;
653 * Y = ay + t * ady = by + s * bdy;
654 * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
657 * Therefore we can reject any intersection (under the criteria for
658 * valid intersection events) if:
659 * L^R < 0 => t < 0, or
662 * (where top/bottom must at least extend to the line endpoints).
664 * A similar substitution can be performed for s, yielding:
665 * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
667 R = det32_64 (dx2, dy2,
668 b->edge.line.p1.x - a->edge.line.p1.x,
669 b->edge.line.p1.y - a->edge.line.p1.y);
670 if (_cairo_int64_negative (den_det)) {
671 if (_cairo_int64_ge (den_det, R))
674 if (_cairo_int64_le (den_det, R))
678 R = det32_64 (dy1, dx1,
679 a->edge.line.p1.y - b->edge.line.p1.y,
680 a->edge.line.p1.x - b->edge.line.p1.x);
681 if (_cairo_int64_negative (den_det)) {
682 if (_cairo_int64_ge (den_det, R))
685 if (_cairo_int64_le (den_det, R))
689 /* We now know that the two lines should intersect within range. */
691 a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
692 a->edge.line.p2.x, a->edge.line.p2.y);
693 b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
694 b->edge.line.p2.x, b->edge.line.p2.y);
696 /* x = det (a_det, dx1, b_det, dx2) / den_det */
697 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
700 if (_cairo_int64_eq (qr.rem, den_det))
703 intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
705 intersection->x.exactness = EXACT;
706 if (! _cairo_int64_is_zero (qr.rem)) {
707 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
708 qr.rem = _cairo_int64_negate (qr.rem);
709 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
710 if (_cairo_int64_ge (qr.rem, den_det)) {
711 qr.quo = _cairo_int64_add (qr.quo,
712 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
714 intersection->x.exactness = INEXACT;
717 intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo);
719 /* y = det (a_det, dy1, b_det, dy2) / den_det */
720 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
723 if (_cairo_int64_eq (qr.rem, den_det))
726 intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
728 intersection->y.exactness = EXACT;
729 if (! _cairo_int64_is_zero (qr.rem)) {
730 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
731 qr.rem = _cairo_int64_negate (qr.rem);
732 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
733 if (_cairo_int64_ge (qr.rem, den_det)) {
734 qr.quo = _cairo_int64_add (qr.quo,
735 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
737 intersection->y.exactness = INEXACT;
740 intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo);
746 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
749 /* First compare the quotient */
754 /* With quotient identical, if remainder is 0 then compare equal */
755 /* Otherwise, the non-zero remainder makes a > b */
756 return INEXACT == a.exactness;
759 /* Does the given edge contain the given point. The point must already
760 * be known to be contained within the line determined by the edge,
761 * (most likely the point results from an intersection of this edge
764 * If we had exact arithmetic, then this function would simply be a
765 * matter of examining whether the y value of the point lies within
766 * the range of y values of the edge. But since intersection points
767 * are not exact due to being rounded to the nearest integer within
768 * the available precision, we must also examine the x value of the
771 * The definition of "contains" here is that the given intersection
772 * point will be seen by the sweep line after the start event for the
773 * given edge and before the stop event for the edge. See the comments
774 * in the implementation for more details.
777 _cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
778 cairo_bo_intersect_point_t *point)
780 int cmp_top, cmp_bottom;
782 /* XXX: When running the actual algorithm, we don't actually need to
783 * compare against edge->top at all here, since any intersection above
784 * top is eliminated early via a slope comparison. We're leaving these
785 * here for now only for the sake of the quadratic-time intersection
786 * finder which needs them.
789 cmp_top = _cairo_bo_intersect_ordinate_32_compare (point->y,
791 cmp_bottom = _cairo_bo_intersect_ordinate_32_compare (point->y,
794 if (cmp_top < 0 || cmp_bottom > 0)
799 if (cmp_top > 0 && cmp_bottom < 0)
804 /* At this stage, the point lies on the same y value as either
805 * edge->top or edge->bottom, so we have to examine the x value in
806 * order to properly determine containment. */
808 /* If the y value of the point is the same as the y value of the
809 * top of the edge, then the x value of the point must be greater
810 * to be considered as inside the edge. Similarly, if the y value
811 * of the point is the same as the y value of the bottom of the
812 * edge, then the x value of the point must be less to be
813 * considered as inside. */
818 top_x = _line_compute_intersection_x_for_y (&edge->edge.line,
820 return _cairo_bo_intersect_ordinate_32_compare (point->x, top_x) > 0;
821 } else { /* cmp_bottom == 0 */
824 bot_x = _line_compute_intersection_x_for_y (&edge->edge.line,
826 return _cairo_bo_intersect_ordinate_32_compare (point->x, bot_x) < 0;
830 /* Compute the intersection of two edges. The result is provided as a
831 * coordinate pair of 128-bit integers.
833 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
834 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
835 * intersection of the lines defined by the edges occurs outside of
836 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
837 * are exactly parallel.
839 * Note that when determining if a candidate intersection is "inside"
840 * an edge, we consider both the infinitesimal shortening and the
841 * infinitesimal tilt rules described by John Hobby. Specifically, if
842 * the intersection is exactly the same as an edge point, it is
843 * effectively outside (no intersection is returned). Also, if the
844 * intersection point has the same
847 _cairo_bo_edge_intersect (cairo_bo_edge_t *a,
849 cairo_bo_point32_t *intersection)
851 cairo_bo_intersect_point_t quorem;
853 if (! intersect_lines (a, b, &quorem))
856 if (! _cairo_bo_edge_contains_intersect_point (a, &quorem))
859 if (! _cairo_bo_edge_contains_intersect_point (b, &quorem))
862 /* Now that we've correctly compared the intersection point and
863 * determined that it lies within the edge, then we know that we
864 * no longer need any more bits of storage for the intersection
865 * than we do for our edge coordinates. We also no longer need the
866 * remainder from the division. */
867 intersection->x = quorem.x.ordinate;
868 intersection->y = quorem.y.ordinate;
874 cairo_bo_event_compare (const cairo_bo_event_t *a,
875 const cairo_bo_event_t *b)
879 cmp = _cairo_bo_point32_compare (&a->point, &b->point);
883 cmp = a->type - b->type;
891 _pqueue_init (pqueue_t *pq)
893 pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
896 pq->elements = pq->elements_embedded;
900 _pqueue_fini (pqueue_t *pq)
902 if (pq->elements != pq->elements_embedded)
906 static cairo_status_t
907 _pqueue_grow (pqueue_t *pq)
909 cairo_bo_event_t **new_elements;
912 if (pq->elements == pq->elements_embedded) {
913 new_elements = _cairo_malloc_ab (pq->max_size,
914 sizeof (cairo_bo_event_t *));
915 if (unlikely (new_elements == NULL))
916 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
918 memcpy (new_elements, pq->elements_embedded,
919 sizeof (pq->elements_embedded));
921 new_elements = _cairo_realloc_ab (pq->elements,
923 sizeof (cairo_bo_event_t *));
924 if (unlikely (new_elements == NULL))
925 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
928 pq->elements = new_elements;
929 return CAIRO_STATUS_SUCCESS;
932 static inline cairo_status_t
933 _pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
935 cairo_bo_event_t **elements;
938 if (unlikely (pq->size + 1 == pq->max_size)) {
939 cairo_status_t status;
941 status = _pqueue_grow (pq);
942 if (unlikely (status))
946 elements = pq->elements;
949 i != PQ_FIRST_ENTRY &&
950 cairo_bo_event_compare (event,
951 elements[parent = PQ_PARENT_INDEX (i)]) < 0;
954 elements[i] = elements[parent];
959 return CAIRO_STATUS_SUCCESS;
963 _pqueue_pop (pqueue_t *pq)
965 cairo_bo_event_t **elements = pq->elements;
966 cairo_bo_event_t *tail;
969 tail = elements[pq->size--];
971 elements[PQ_FIRST_ENTRY] = NULL;
975 for (i = PQ_FIRST_ENTRY;
976 (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
979 if (child != pq->size &&
980 cairo_bo_event_compare (elements[child+1],
981 elements[child]) < 0)
986 if (cairo_bo_event_compare (elements[child], tail) >= 0)
989 elements[i] = elements[child];
994 static inline cairo_status_t
995 _cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
996 cairo_bo_event_type_t type,
999 const cairo_point_t *point)
1001 cairo_bo_queue_event_t *event;
1003 event = _cairo_freepool_alloc (&queue->pool);
1004 if (unlikely (event == NULL))
1005 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1010 event->point = *point;
1012 return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
1016 _cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
1017 cairo_bo_event_t *event)
1019 _cairo_freepool_free (&queue->pool, event);
1022 static cairo_bo_event_t *
1023 _cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
1025 cairo_bo_event_t *event, *cmp;
1027 event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
1028 cmp = *event_queue->start_events;
1029 if (event == NULL ||
1030 (cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
1033 event_queue->start_events++;
1037 _pqueue_pop (&event_queue->pqueue);
1043 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
1045 cairo_bo_event_compare)
1048 _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
1049 cairo_bo_event_t **start_events,
1052 event_queue->start_events = start_events;
1054 _cairo_freepool_init (&event_queue->pool,
1055 sizeof (cairo_bo_queue_event_t));
1056 _pqueue_init (&event_queue->pqueue);
1057 event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
1060 static cairo_status_t
1061 _cairo_bo_event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
1062 cairo_bo_edge_t *edge)
1064 cairo_bo_point32_t point;
1066 point.y = edge->edge.bottom;
1067 point.x = _line_compute_intersection_x_for_y (&edge->edge.line,
1069 return _cairo_bo_event_queue_insert (event_queue,
1070 CAIRO_BO_EVENT_TYPE_STOP,
1076 _cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
1078 _pqueue_fini (&event_queue->pqueue);
1079 _cairo_freepool_fini (&event_queue->pool);
1082 static inline cairo_status_t
1083 _cairo_bo_event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
1084 cairo_bo_edge_t *left,
1085 cairo_bo_edge_t *right)
1087 cairo_bo_point32_t intersection;
1089 if (MAX (left->edge.line.p1.x, left->edge.line.p2.x) <=
1090 MIN (right->edge.line.p1.x, right->edge.line.p2.x))
1091 return CAIRO_STATUS_SUCCESS;
1093 if (_line_equal (&left->edge.line, &right->edge.line))
1094 return CAIRO_STATUS_SUCCESS;
1096 /* The names "left" and "right" here are correct descriptions of
1097 * the order of the two edges within the active edge list. So if a
1098 * slope comparison also puts left less than right, then we know
1099 * that the intersection of these two segments has already
1100 * occurred before the current sweep line position. */
1101 if (_slope_compare (left, right) <= 0)
1102 return CAIRO_STATUS_SUCCESS;
1104 if (! _cairo_bo_edge_intersect (left, right, &intersection))
1105 return CAIRO_STATUS_SUCCESS;
1107 return _cairo_bo_event_queue_insert (event_queue,
1108 CAIRO_BO_EVENT_TYPE_INTERSECTION,
1114 _cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
1116 sweep_line->head = NULL;
1117 sweep_line->stopped = NULL;
1118 sweep_line->current_y = INT32_MIN;
1119 sweep_line->current_edge = NULL;
1123 _cairo_bo_sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
1124 cairo_bo_edge_t *edge)
1126 if (sweep_line->current_edge != NULL) {
1127 cairo_bo_edge_t *prev, *next;
1130 cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
1131 sweep_line->current_edge,
1134 prev = sweep_line->current_edge;
1136 while (next != NULL &&
1137 _cairo_bo_sweep_line_compare_edges (sweep_line,
1140 prev = next, next = prev->next;
1148 } else if (cmp > 0) {
1149 next = sweep_line->current_edge;
1151 while (prev != NULL &&
1152 _cairo_bo_sweep_line_compare_edges (sweep_line,
1155 next = prev, prev = next->prev;
1164 sweep_line->head = edge;
1166 prev = sweep_line->current_edge;
1168 edge->next = prev->next;
1169 if (prev->next != NULL)
1170 prev->next->prev = edge;
1174 sweep_line->head = edge;
1178 sweep_line->current_edge = edge;
1182 _cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
1183 cairo_bo_edge_t *edge)
1185 if (edge->prev != NULL)
1186 edge->prev->next = edge->next;
1188 sweep_line->head = edge->next;
1190 if (edge->next != NULL)
1191 edge->next->prev = edge->prev;
1193 if (sweep_line->current_edge == edge)
1194 sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
1198 _cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
1199 cairo_bo_edge_t *left,
1200 cairo_bo_edge_t *right)
1202 if (left->prev != NULL)
1203 left->prev->next = right;
1205 sweep_line->head = right;
1207 if (right->next != NULL)
1208 right->next->prev = left;
1210 left->next = right->next;
1213 right->prev = left->prev;
1217 #if DEBUG_PRINT_STATE
1219 _cairo_bo_edge_print (cairo_bo_edge_t *edge)
1221 printf ("(0x%x, 0x%x)-(0x%x, 0x%x)",
1222 edge->edge.line.p1.x, edge->edge.line.p1.y,
1223 edge->edge.line.p2.x, edge->edge.line.p2.y);
1227 _cairo_bo_event_print (cairo_bo_event_t *event)
1229 switch (event->type) {
1230 case CAIRO_BO_EVENT_TYPE_START:
1233 case CAIRO_BO_EVENT_TYPE_STOP:
1236 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1237 printf ("Intersection: ");
1240 printf ("(%d, %d)\t", event->point.x, event->point.y);
1241 _cairo_bo_edge_print (event->e1);
1242 if (event->type == CAIRO_BO_EVENT_TYPE_INTERSECTION) {
1244 _cairo_bo_edge_print (event->e2);
1250 _cairo_bo_event_queue_print (cairo_bo_event_queue_t *event_queue)
1252 /* XXX: fixme to print the start/stop array too. */
1253 printf ("Event queue:\n");
1257 _cairo_bo_sweep_line_print (cairo_bo_sweep_line_t *sweep_line)
1259 cairo_bool_t first = TRUE;
1260 cairo_bo_edge_t *edge;
1262 printf ("Sweep line from edge list: ");
1264 for (edge = sweep_line->head;
1270 _cairo_bo_edge_print (edge);
1277 print_state (const char *msg,
1278 cairo_bo_event_t *event,
1279 cairo_bo_event_queue_t *event_queue,
1280 cairo_bo_sweep_line_t *sweep_line)
1282 printf ("%s ", msg);
1283 _cairo_bo_event_print (event);
1284 _cairo_bo_event_queue_print (event_queue);
1285 _cairo_bo_sweep_line_print (sweep_line);
1291 static void CAIRO_PRINTF_FORMAT (1, 2)
1292 event_log (const char *fmt, ...)
1296 if (getenv ("CAIRO_DEBUG_EVENTS") == NULL)
1299 file = fopen ("bo-events.txt", "a");
1304 vfprintf (file, fmt, ap);
1312 #define HAS_COLINEAR(a, b) ((cairo_bo_edge_t *)(((uintptr_t)(a))&~1) == (b))
1313 #define IS_COLINEAR(e) (((uintptr_t)(e))&1)
1314 #define MARK_COLINEAR(e, v) ((cairo_bo_edge_t *)(((uintptr_t)(e))|(v)))
1316 static inline cairo_bool_t
1317 edges_colinear (cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
1321 if (HAS_COLINEAR(a->colinear, b))
1322 return IS_COLINEAR(a->colinear);
1324 if (HAS_COLINEAR(b->colinear, a)) {
1325 p = IS_COLINEAR(b->colinear);
1326 a->colinear = MARK_COLINEAR(b, p);
1331 p |= (a->edge.line.p1.x == b->edge.line.p1.x) << 0;
1332 p |= (a->edge.line.p1.y == b->edge.line.p1.y) << 1;
1333 p |= (a->edge.line.p2.x == b->edge.line.p2.x) << 3;
1334 p |= (a->edge.line.p2.y == b->edge.line.p2.y) << 4;
1335 if (p == ((1 << 0) | (1 << 1) | (1 << 3) | (1 << 4))) {
1336 a->colinear = MARK_COLINEAR(b, 1);
1340 if (_slope_compare (a, b)) {
1341 a->colinear = MARK_COLINEAR(b, 0);
1345 /* The choice of y is not truly arbitrary since we must guarantee that it
1346 * is greater than the start of either line.
1349 /* colinear if either end-point are coincident */
1350 p = (((p >> 1) & p) & 5) != 0;
1351 } else if (a->edge.line.p1.y < b->edge.line.p1.y) {
1352 p = edge_compare_for_y_against_x (b,
1354 a->edge.line.p1.x) == 0;
1356 p = edge_compare_for_y_against_x (a,
1358 b->edge.line.p1.x) == 0;
1361 a->colinear = MARK_COLINEAR(b, p);
1365 /* Adds the trapezoid, if any, of the left edge to the #cairo_traps_t */
1367 _cairo_bo_edge_end_trap (cairo_bo_edge_t *left,
1369 cairo_traps_t *traps)
1371 cairo_bo_trap_t *trap = &left->deferred_trap;
1373 /* Only emit (trivial) non-degenerate trapezoids with positive height. */
1374 if (likely (trap->top < bot)) {
1375 _cairo_traps_add_trap (traps,
1377 &left->edge.line, &trap->right->edge.line);
1379 #if DEBUG_PRINT_STATE
1380 printf ("Deferred trap: left=(%x, %x)-(%x,%x) "
1381 "right=(%x,%x)-(%x,%x) top=%x, bot=%x\n",
1382 left->edge.line.p1.x, left->edge.line.p1.y,
1383 left->edge.line.p2.x, left->edge.line.p2.y,
1384 trap->right->edge.line.p1.x, trap->right->edge.line.p1.y,
1385 trap->right->edge.line.p2.x, trap->right->edge.line.p2.y,
1389 event_log ("end trap: %lu %lu %d %d\n",
1401 /* Start a new trapezoid at the given top y coordinate, whose edges
1402 * are `edge' and `edge->next'. If `edge' already has a trapezoid,
1403 * then either add it to the traps in `traps', if the trapezoid's
1404 * right edge differs from `edge->next', or do nothing if the new
1405 * trapezoid would be a continuation of the existing one. */
1407 _cairo_bo_edge_start_or_continue_trap (cairo_bo_edge_t *left,
1408 cairo_bo_edge_t *right,
1410 cairo_traps_t *traps)
1412 if (left->deferred_trap.right == right)
1416 if (left->deferred_trap.right != NULL) {
1417 if (edges_colinear (left->deferred_trap.right, right))
1419 /* continuation on right, so just swap edges */
1420 left->deferred_trap.right = right;
1424 _cairo_bo_edge_end_trap (left, top, traps);
1427 if (! edges_colinear (left, right)) {
1428 left->deferred_trap.top = top;
1429 left->deferred_trap.right = right;
1432 event_log ("begin trap: %lu %lu %d\n",
1441 _active_edges_to_traps (cairo_bo_edge_t *pos,
1444 cairo_traps_t *traps)
1446 cairo_bo_edge_t *left;
1450 #if DEBUG_PRINT_STATE
1451 printf ("Processing active edges for %x\n", top);
1456 while (pos != NULL) {
1457 if (pos != left && pos->deferred_trap.right) {
1458 /* XXX It shouldn't be possible to here with 2 deferred traps
1459 * on colinear edges... See bug-bo-rictoz.
1461 if (left->deferred_trap.right == NULL &&
1462 edges_colinear (left, pos))
1464 /* continuation on left */
1465 left->deferred_trap = pos->deferred_trap;
1466 pos->deferred_trap.right = NULL;
1470 _cairo_bo_edge_end_trap (pos, top, traps);
1474 in_out += pos->edge.dir;
1475 if ((in_out & mask) == 0) {
1476 /* skip co-linear edges */
1477 if (pos->next == NULL || ! edges_colinear (pos, pos->next)) {
1478 _cairo_bo_edge_start_or_continue_trap (left, pos, top, traps);
1487 /* Execute a single pass of the Bentley-Ottmann algorithm on edges,
1488 * generating trapezoids according to the fill_rule and appending them
1490 static cairo_status_t
1491 _cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_event_t **start_events,
1494 cairo_traps_t *traps,
1495 int *num_intersections)
1497 cairo_status_t status;
1498 int intersection_count = 0;
1499 cairo_bo_event_queue_t event_queue;
1500 cairo_bo_sweep_line_t sweep_line;
1501 cairo_bo_event_t *event;
1502 cairo_bo_edge_t *left, *right;
1503 cairo_bo_edge_t *e1, *e2;
1505 /* convert the fill_rule into a winding mask */
1506 if (fill_rule == CAIRO_FILL_RULE_WINDING)
1507 fill_rule = (unsigned) -1;
1515 for (i = 0; i < num_events; i++) {
1516 cairo_bo_start_event_t *event =
1517 ((cairo_bo_start_event_t **) start_events)[i];
1518 event_log ("edge: %lu (%d, %d) (%d, %d) (%d, %d) %d\n",
1519 (long) &events[i].edge,
1520 event->edge.edge.line.p1.x,
1521 event->edge.edge.line.p1.y,
1522 event->edge.edge.line.p2.x,
1523 event->edge.edge.line.p2.y,
1526 event->edge.edge.dir);
1531 _cairo_bo_event_queue_init (&event_queue, start_events, num_events);
1532 _cairo_bo_sweep_line_init (&sweep_line);
1534 while ((event = _cairo_bo_event_dequeue (&event_queue))) {
1535 if (event->point.y != sweep_line.current_y) {
1536 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1537 if (e1->deferred_trap.right != NULL) {
1538 _cairo_bo_edge_end_trap (e1,
1543 sweep_line.stopped = NULL;
1545 _active_edges_to_traps (sweep_line.head,
1546 sweep_line.current_y,
1549 sweep_line.current_y = event->point.y;
1553 event_log ("event: %d (%ld, %ld) %lu, %lu\n",
1555 (long) event->point.x,
1556 (long) event->point.y,
1561 switch (event->type) {
1562 case CAIRO_BO_EVENT_TYPE_START:
1563 e1 = &((cairo_bo_start_event_t *) event)->edge;
1565 _cairo_bo_sweep_line_insert (&sweep_line, e1);
1567 status = _cairo_bo_event_queue_insert_stop (&event_queue, e1);
1568 if (unlikely (status))
1571 /* check to see if this is a continuation of a stopped edge */
1572 /* XXX change to an infinitesimal lengthening rule */
1573 for (left = sweep_line.stopped; left; left = left->next) {
1574 if (e1->edge.top <= left->edge.bottom &&
1575 edges_colinear (e1, left))
1577 e1->deferred_trap = left->deferred_trap;
1578 if (left->prev != NULL)
1579 left->prev = left->next;
1581 sweep_line.stopped = left->next;
1582 if (left->next != NULL)
1583 left->next->prev = left->prev;
1592 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
1593 if (unlikely (status))
1597 if (right != NULL) {
1598 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1599 if (unlikely (status))
1605 case CAIRO_BO_EVENT_TYPE_STOP:
1606 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1607 _cairo_bo_event_queue_delete (&event_queue, event);
1612 _cairo_bo_sweep_line_delete (&sweep_line, e1);
1614 /* first, check to see if we have a continuation via a fresh edge */
1615 if (e1->deferred_trap.right != NULL) {
1616 e1->next = sweep_line.stopped;
1617 if (sweep_line.stopped != NULL)
1618 sweep_line.stopped->prev = e1;
1619 sweep_line.stopped = e1;
1623 if (left != NULL && right != NULL) {
1624 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
1625 if (unlikely (status))
1631 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1632 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1633 e2 = ((cairo_bo_queue_event_t *) event)->e2;
1634 _cairo_bo_event_queue_delete (&event_queue, event);
1636 /* skip this intersection if its edges are not adjacent */
1640 intersection_count++;
1645 _cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
1647 /* after the swap e2 is left of e1 */
1650 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
1651 if (unlikely (status))
1655 if (right != NULL) {
1656 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1657 if (unlikely (status))
1665 *num_intersections = intersection_count;
1666 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1667 if (e1->deferred_trap.right != NULL) {
1668 _cairo_bo_edge_end_trap (e1, e1->edge.bottom, traps);
1671 status = traps->status;
1673 _cairo_bo_event_queue_fini (&event_queue);
1683 _cairo_bentley_ottmann_tessellate_polygon (cairo_traps_t *traps,
1684 const cairo_polygon_t *polygon,
1685 cairo_fill_rule_t fill_rule)
1688 cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
1689 cairo_bo_start_event_t *events;
1690 cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
1691 cairo_bo_event_t **event_ptrs;
1692 cairo_bo_start_event_t *stack_event_y[64];
1693 cairo_bo_start_event_t **event_y = NULL;
1694 int i, num_events, y, ymin = 0, ymax = 0;
1695 cairo_status_t status;
1697 num_events = polygon->num_edges;
1698 if (unlikely (0 == num_events))
1699 return CAIRO_STATUS_SUCCESS;
1701 if (polygon->num_limits) {
1702 ymin = _cairo_fixed_integer_floor (polygon->limit.p1.y);
1703 ymax = _cairo_fixed_integer_ceil (polygon->limit.p2.y) - ymin;
1706 event_y = _cairo_malloc_ab(sizeof (cairo_bo_event_t*), ymax);
1707 if (unlikely (event_y == NULL))
1708 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1710 event_y = stack_event_y;
1712 memset (event_y, 0, ymax * sizeof(cairo_bo_event_t *));
1715 events = stack_events;
1716 event_ptrs = stack_event_ptrs;
1717 if (num_events > ARRAY_LENGTH (stack_events)) {
1718 events = _cairo_malloc_ab_plus_c (num_events,
1719 sizeof (cairo_bo_start_event_t) +
1720 sizeof (cairo_bo_event_t *),
1721 sizeof (cairo_bo_event_t *));
1722 if (unlikely (events == NULL)) {
1723 if (event_y != stack_event_y)
1725 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1728 event_ptrs = (cairo_bo_event_t **) (events + num_events);
1731 for (i = 0; i < num_events; i++) {
1732 events[i].type = CAIRO_BO_EVENT_TYPE_START;
1733 events[i].point.y = polygon->edges[i].top;
1735 _line_compute_intersection_x_for_y (&polygon->edges[i].line,
1738 events[i].edge.edge = polygon->edges[i];
1739 events[i].edge.deferred_trap.right = NULL;
1740 events[i].edge.prev = NULL;
1741 events[i].edge.next = NULL;
1742 events[i].edge.colinear = NULL;
1745 y = _cairo_fixed_integer_floor (events[i].point.y) - ymin;
1746 events[i].edge.next = (cairo_bo_edge_t *) event_y[y];
1747 event_y[y] = (cairo_bo_start_event_t *) &events[i];
1749 event_ptrs[i] = (cairo_bo_event_t *) &events[i];
1753 for (y = i = 0; y < ymax && i < num_events; y++) {
1754 cairo_bo_start_event_t *e;
1756 for (e = event_y[y]; e; e = (cairo_bo_start_event_t *)e->edge.next)
1757 event_ptrs[i++] = (cairo_bo_event_t *) e;
1759 _cairo_bo_event_queue_sort (event_ptrs+j, i-j);
1761 if (event_y != stack_event_y)
1764 _cairo_bo_event_queue_sort (event_ptrs, i);
1765 event_ptrs[i] = NULL;
1768 dump_edges (events, num_events, "bo-polygon-edges.txt");
1771 /* XXX: This would be the convenient place to throw in multiple
1772 * passes of the Bentley-Ottmann algorithm. It would merely
1773 * require storing the results of each pass into a temporary
1775 status = _cairo_bentley_ottmann_tessellate_bo_edges (event_ptrs, num_events,
1779 dump_traps (traps, "bo-polygon-out.txt");
1782 if (events != stack_events)
1789 _cairo_bentley_ottmann_tessellate_traps (cairo_traps_t *traps,
1790 cairo_fill_rule_t fill_rule)
1792 cairo_status_t status;
1793 cairo_polygon_t polygon;
1796 if (unlikely (0 == traps->num_traps))
1797 return CAIRO_STATUS_SUCCESS;
1800 dump_traps (traps, "bo-traps-in.txt");
1803 _cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
1805 for (i = 0; i < traps->num_traps; i++) {
1806 status = _cairo_polygon_add_line (&polygon,
1807 &traps->traps[i].left,
1808 traps->traps[i].top,
1809 traps->traps[i].bottom,
1811 if (unlikely (status))
1814 status = _cairo_polygon_add_line (&polygon,
1815 &traps->traps[i].right,
1816 traps->traps[i].top,
1817 traps->traps[i].bottom,
1819 if (unlikely (status))
1823 _cairo_traps_clear (traps);
1824 status = _cairo_bentley_ottmann_tessellate_polygon (traps,
1829 dump_traps (traps, "bo-traps-out.txt");
1833 _cairo_polygon_fini (&polygon);
1840 edges_have_an_intersection_quadratic (cairo_bo_edge_t *edges,
1845 cairo_bo_edge_t *a, *b;
1846 cairo_bo_point32_t intersection;
1848 /* We must not be given any upside-down edges. */
1849 for (i = 0; i < num_edges; i++) {
1850 assert (_cairo_bo_point32_compare (&edges[i].top, &edges[i].bottom) < 0);
1851 edges[i].line.p1.x <<= CAIRO_BO_GUARD_BITS;
1852 edges[i].line.p1.y <<= CAIRO_BO_GUARD_BITS;
1853 edges[i].line.p2.x <<= CAIRO_BO_GUARD_BITS;
1854 edges[i].line.p2.y <<= CAIRO_BO_GUARD_BITS;
1857 for (i = 0; i < num_edges; i++) {
1858 for (j = 0; j < num_edges; j++) {
1865 if (! _cairo_bo_edge_intersect (a, b, &intersection))
1868 printf ("Found intersection (%d,%d) between (%d,%d)-(%d,%d) and (%d,%d)-(%d,%d)\n",
1871 a->line.p1.x, a->line.p1.y,
1872 a->line.p2.x, a->line.p2.y,
1873 b->line.p1.x, b->line.p1.y,
1874 b->line.p2.x, b->line.p2.y);
1882 #define TEST_MAX_EDGES 10
1884 typedef struct test {
1886 const char *description;
1888 cairo_bo_edge_t edges[TEST_MAX_EDGES];
1895 "3 edges all intersecting very close to each other",
1898 { { 4, 2}, {0, 0}, { 9, 9}, NULL, NULL },
1899 { { 7, 2}, {0, 0}, { 2, 3}, NULL, NULL },
1900 { { 5, 2}, {0, 0}, { 1, 7}, NULL, NULL }
1904 "inconsistent data",
1905 "Derived from random testing---was leading to skip list and edge list disagreeing.",
1908 { { 2, 3}, {0, 0}, { 8, 9}, NULL, NULL },
1909 { { 2, 3}, {0, 0}, { 6, 7}, NULL, NULL }
1914 "A test derived from random testing that leads to an inconsistent sort --- looks like we just can't attempt to validate the sweep line with edge_compare?",
1917 { { 6, 2}, {0, 0}, { 6, 5}, NULL, NULL },
1918 { { 3, 5}, {0, 0}, { 5, 6}, NULL, NULL },
1919 { { 9, 2}, {0, 0}, { 5, 6}, NULL, NULL },
1923 "minimal-intersection",
1924 "Intersection of a two from among the smallest possible edges.",
1927 { { 0, 0}, {0, 0}, { 1, 1}, NULL, NULL },
1928 { { 1, 0}, {0, 0}, { 0, 1}, NULL, NULL }
1933 "A simple intersection of two edges at an integer (2,2).",
1936 { { 1, 1}, {0, 0}, { 3, 3}, NULL, NULL },
1937 { { 2, 1}, {0, 0}, { 2, 3}, NULL, NULL }
1941 "bend-to-horizontal",
1942 "With intersection truncation one edge bends to horizontal",
1945 { { 9, 1}, {0, 0}, {3, 7}, NULL, NULL },
1946 { { 3, 5}, {0, 0}, {9, 9}, NULL, NULL }
1954 "An intersection that occurs at the endpoint of a segment.",
1956 { { 4, 6}, { 5, 6}, NULL, { { NULL }} },
1957 { { 4, 5}, { 5, 7}, NULL, { { NULL }} },
1958 { { 0, 0}, { 0, 0}, NULL, { { NULL }} },
1962 name = "overlapping",
1963 desc = "Parallel segments that share an endpoint, with different slopes.",
1965 { top = { x = 2, y = 0}, bottom = { x = 1, y = 1}},
1966 { top = { x = 2, y = 0}, bottom = { x = 0, y = 2}},
1967 { top = { x = 0, y = 3}, bottom = { x = 1, y = 3}},
1968 { top = { x = 0, y = 3}, bottom = { x = 2, y = 3}},
1969 { top = { x = 0, y = 4}, bottom = { x = 0, y = 6}},
1970 { top = { x = 0, y = 5}, bottom = { x = 0, y = 6}}
1974 name = "hobby_stage_3",
1975 desc = "A particularly tricky part of the 3rd stage of the 'hobby' test below.",
1977 { top = { x = -1, y = -2}, bottom = { x = 4, y = 2}},
1978 { top = { x = 5, y = 3}, bottom = { x = 9, y = 5}},
1979 { top = { x = 5, y = 3}, bottom = { x = 6, y = 3}},
1984 desc = "Example from John Hobby's paper. Requires 3 passes of the iterative algorithm.",
1986 { top = { x = 0, y = 0}, bottom = { x = 9, y = 5}},
1987 { top = { x = 0, y = 0}, bottom = { x = 13, y = 6}},
1988 { top = { x = -1, y = -2}, bottom = { x = 9, y = 5}}
1993 desc = "Edges with same start/stop points but different slopes",
1995 { top = { x = 4, y = 1}, bottom = { x = 6, y = 3}},
1996 { top = { x = 4, y = 1}, bottom = { x = 2, y = 3}},
1997 { top = { x = 2, y = 4}, bottom = { x = 4, y = 6}},
1998 { top = { x = 6, y = 4}, bottom = { x = 4, y = 6}}
2002 name = "horizontal",
2003 desc = "Test of a horizontal edge",
2005 { top = { x = 1, y = 1}, bottom = { x = 6, y = 6}},
2006 { top = { x = 2, y = 3}, bottom = { x = 5, y = 3}}
2011 desc = "Test of a vertical edge",
2013 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
2014 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
2019 desc = "Two overlapping edges with the same slope",
2021 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
2022 { top = { x = 5, y = 2}, bottom = { x = 5, y = 6}},
2023 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
2028 desc = "Several segments with a common intersection point",
2030 { top = { x = 1, y = 2}, bottom = { x = 5, y = 4} },
2031 { top = { x = 1, y = 1}, bottom = { x = 5, y = 5} },
2032 { top = { x = 2, y = 1}, bottom = { x = 4, y = 5} },
2033 { top = { x = 4, y = 1}, bottom = { x = 2, y = 5} },
2034 { top = { x = 5, y = 1}, bottom = { x = 1, y = 5} },
2035 { top = { x = 5, y = 2}, bottom = { x = 1, y = 4} }
2042 run_test (const char *test_name,
2043 cairo_bo_edge_t *test_edges,
2046 int i, intersections, passes;
2047 cairo_bo_edge_t *edges;
2048 cairo_array_t intersected_edges;
2050 printf ("Testing: %s\n", test_name);
2052 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
2054 intersections = _cairo_bentley_ottmann_intersect_edges (test_edges, num_edges, &intersected_edges);
2056 printf ("Pass 1 found %d intersections:\n", intersections);
2059 /* XXX: Multi-pass Bentley-Ottmmann. Preferable would be to add a
2060 * pass of Hobby's tolerance-square algorithm instead. */
2062 while (intersections) {
2063 int num_edges = _cairo_array_num_elements (&intersected_edges);
2065 edges = _cairo_malloc_ab (num_edges, sizeof (cairo_bo_edge_t));
2066 assert (edges != NULL);
2067 memcpy (edges, _cairo_array_index (&intersected_edges, 0), num_edges * sizeof (cairo_bo_edge_t));
2068 _cairo_array_fini (&intersected_edges);
2069 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
2070 intersections = _cairo_bentley_ottmann_intersect_edges (edges, num_edges, &intersected_edges);
2074 printf ("Pass %d found %d remaining intersections:\n", passes, intersections);
2077 for (i = 0; i < passes; i++)
2079 printf ("No remainining intersections found after pass %d\n", passes);
2083 if (edges_have_an_intersection_quadratic (_cairo_array_index (&intersected_edges, 0),
2084 _cairo_array_num_elements (&intersected_edges)))
2085 printf ("*** FAIL ***\n");
2089 _cairo_array_fini (&intersected_edges);
2094 #define MAX_RANDOM 300
2099 char random_name[] = "random-XX";
2100 cairo_bo_edge_t random_edges[MAX_RANDOM], *edge;
2101 unsigned int i, num_random;
2104 for (i = 0; i < ARRAY_LENGTH (tests); i++) {
2106 run_test (test->name, test->edges, test->num_edges);
2109 for (num_random = 0; num_random < MAX_RANDOM; num_random++) {
2111 for (i = 0; i < num_random; i++) {
2113 edge = &random_edges[i];
2114 edge->line.p1.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2115 edge->line.p1.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2116 edge->line.p2.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2117 edge->line.p2.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2118 if (edge->line.p1.y > edge->line.p2.y) {
2119 int32_t tmp = edge->line.p1.y;
2120 edge->line.p1.y = edge->line.p2.y;
2121 edge->line.p2.y = tmp;
2123 } while (edge->line.p1.y == edge->line.p2.y);
2126 sprintf (random_name, "random-%02d", num_random);
2128 run_test (random_name, random_edges, num_random);