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_trap_t deferred_trap;
78 /* the parent is always given by index/2 */
79 #define PQ_PARENT_INDEX(i) ((i) >> 1)
80 #define PQ_FIRST_ENTRY 1
82 /* left and right children are index * 2 and (index * 2) +1 respectively */
83 #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
86 CAIRO_BO_EVENT_TYPE_STOP,
87 CAIRO_BO_EVENT_TYPE_INTERSECTION,
88 CAIRO_BO_EVENT_TYPE_START
89 } cairo_bo_event_type_t;
91 typedef struct _cairo_bo_event {
92 cairo_bo_event_type_t type;
96 typedef struct _cairo_bo_start_event {
97 cairo_bo_event_type_t type;
100 } cairo_bo_start_event_t;
102 typedef struct _cairo_bo_queue_event {
103 cairo_bo_event_type_t type;
107 } cairo_bo_queue_event_t;
109 typedef struct _pqueue {
112 cairo_bo_event_t **elements;
113 cairo_bo_event_t *elements_embedded[1024];
116 typedef struct _cairo_bo_event_queue {
117 cairo_freepool_t pool;
119 cairo_bo_event_t **start_events;
120 } cairo_bo_event_queue_t;
122 typedef struct _cairo_bo_sweep_line {
123 cairo_bo_edge_t *head;
124 cairo_bo_edge_t *stopped;
126 cairo_bo_edge_t *current_edge;
127 } cairo_bo_sweep_line_t;
131 dump_traps (cairo_traps_t *traps, const char *filename)
137 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
141 if (traps->has_limits) {
142 printf ("%s: limits=(%d, %d, %d, %d)\n",
144 traps->limits.p1.x, traps->limits.p1.y,
145 traps->limits.p2.x, traps->limits.p2.y);
148 _cairo_traps_extents (traps, &extents);
149 printf ("%s: extents=(%d, %d, %d, %d)\n",
151 extents.p1.x, extents.p1.y,
152 extents.p2.x, extents.p2.y);
154 file = fopen (filename, "a");
156 for (n = 0; n < traps->num_traps; n++) {
157 fprintf (file, "%d %d L:(%d, %d), (%d, %d) R:(%d, %d), (%d, %d)\n",
159 traps->traps[n].bottom,
160 traps->traps[n].left.p1.x,
161 traps->traps[n].left.p1.y,
162 traps->traps[n].left.p2.x,
163 traps->traps[n].left.p2.y,
164 traps->traps[n].right.p1.x,
165 traps->traps[n].right.p1.y,
166 traps->traps[n].right.p2.x,
167 traps->traps[n].right.p2.y);
169 fprintf (file, "\n");
175 dump_edges (cairo_bo_start_event_t *events,
177 const char *filename)
182 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
185 file = fopen (filename, "a");
187 for (n = 0; n < num_edges; n++) {
188 fprintf (file, "(%d, %d), (%d, %d) %d %d %d\n",
189 events[n].edge.edge.line.p1.x,
190 events[n].edge.edge.line.p1.y,
191 events[n].edge.edge.line.p2.x,
192 events[n].edge.edge.line.p2.y,
193 events[n].edge.edge.top,
194 events[n].edge.edge.bottom,
195 events[n].edge.edge.dir);
197 fprintf (file, "\n");
204 _line_compute_intersection_x_for_y (const cairo_line_t *line,
215 dy = line->p2.y - line->p1.y;
217 x += _cairo_fixed_mul_div_floor (y - line->p1.y,
218 line->p2.x - line->p1.x,
226 _cairo_bo_point32_compare (cairo_bo_point32_t const *a,
227 cairo_bo_point32_t const *b)
238 /* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
239 * slope a is respectively greater than, equal to, or less than the
242 * For each edge, consider the direction vector formed from:
248 * (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
250 * We then define the slope of each edge as dx/dy, (which is the
251 * inverse of the slope typically used in math instruction). We never
252 * compute a slope directly as the value approaches infinity, but we
253 * can derive a slope comparison without division as follows, (where
254 * the ? represents our compare operator).
256 * 1. slope(a) ? slope(b)
257 * 2. adx/ady ? bdx/bdy
258 * 3. (adx * bdy) ? (bdx * ady)
260 * Note that from step 2 to step 3 there is no change needed in the
261 * sign of the result since both ady and bdy are guaranteed to be
262 * greater than or equal to 0.
264 * When using this slope comparison to sort edges, some care is needed
265 * when interpreting the results. Since the slope compare operates on
266 * distance vectors from top to bottom it gives a correct left to
267 * right sort for edges that have a common top point, (such as two
268 * edges with start events at the same location). On the other hand,
269 * the sense of the result will be exactly reversed for two edges that
270 * have a common stop point.
273 _slope_compare (const cairo_bo_edge_t *a,
274 const cairo_bo_edge_t *b)
276 /* XXX: We're assuming here that dx and dy will still fit in 32
277 * bits. That's not true in general as there could be overflow. We
278 * should prevent that before the tessellation algorithm
281 int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
282 int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
284 /* Since the dy's are all positive by construction we can fast
285 * path several common cases.
288 /* First check for vertical lines. */
294 /* Then where the two edges point in different directions wrt x. */
298 /* Finally we actually need to do the general comparison. */
300 int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
301 int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
302 cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
303 cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
305 return _cairo_int64_cmp (adx_bdy, bdx_ady);
310 * We need to compare the x-coordinates of a pair of lines for a particular y,
311 * without loss of precision.
313 * The x-coordinate along an edge for a given y is:
314 * X = A_x + (Y - A_y) * A_dx / A_dy
316 * So the inequality we wish to test is:
317 * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
318 * where ∘ is our inequality operator.
320 * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
321 * all positive, so we can rearrange it thus without causing a sign change:
322 * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
323 * - (Y - A_y) * A_dx * B_dy
325 * Given the assumption that all the deltas fit within 32 bits, we can compute
326 * this comparison directly using 128 bit arithmetic. For certain, but common,
327 * input we can reduce this down to a single 32 bit compare by inspecting the
330 * (And put the burden of the work on developing fast 128 bit ops, which are
331 * required throughout the tessellator.)
333 * See the similar discussion for _slope_compare().
336 edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
337 const cairo_bo_edge_t *b,
340 /* XXX: We're assuming here that dx and dy will still fit in 32
341 * bits. That's not true in general as there could be overflow. We
342 * should prevent that before the tessellation algorithm
352 HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
354 HAVE_DX_BDX = HAVE_DX | HAVE_BDX,
355 HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
356 HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX
357 } have_dx_adx_bdx = HAVE_ALL;
359 /* don't bother solving for abscissa if the edges' bounding boxes
360 * can be used to order them. */
364 if (a->edge.line.p1.x < a->edge.line.p2.x) {
365 amin = a->edge.line.p1.x;
366 amax = a->edge.line.p2.x;
368 amin = a->edge.line.p2.x;
369 amax = a->edge.line.p1.x;
371 if (b->edge.line.p1.x < b->edge.line.p2.x) {
372 bmin = b->edge.line.p1.x;
373 bmax = b->edge.line.p2.x;
375 bmin = b->edge.line.p2.x;
376 bmax = b->edge.line.p1.x;
378 if (amax < bmin) return -1;
379 if (amin > bmax) return +1;
382 ady = a->edge.line.p2.y - a->edge.line.p1.y;
383 adx = a->edge.line.p2.x - a->edge.line.p1.x;
385 have_dx_adx_bdx &= ~HAVE_ADX;
387 bdy = b->edge.line.p2.y - b->edge.line.p1.y;
388 bdx = b->edge.line.p2.x - b->edge.line.p1.x;
390 have_dx_adx_bdx &= ~HAVE_BDX;
392 dx = a->edge.line.p1.x - b->edge.line.p1.x;
394 have_dx_adx_bdx &= ~HAVE_DX;
396 #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
397 #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
398 #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
399 switch (have_dx_adx_bdx) {
404 /* A_dy * B_dy * (A_x - B_x) ∘ 0 */
405 return dx; /* ady * bdy is positive definite */
407 /* 0 ∘ - (Y - A_y) * A_dx * B_dy */
408 return adx; /* bdy * (y - a->top.y) is positive definite */
410 /* 0 ∘ (Y - B_y) * B_dx * A_dy */
411 return -bdx; /* ady * (y - b->top.y) is positive definite */
413 /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
414 if ((adx ^ bdx) < 0) {
416 } else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
417 cairo_int64_t adx_bdy, bdx_ady;
419 /* ∴ A_dx * B_dy ∘ B_dx * A_dy */
421 adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
422 bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
424 return _cairo_int64_cmp (adx_bdy, bdx_ady);
426 return _cairo_int128_cmp (A, B);
428 /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
429 if ((-adx ^ dx) < 0) {
432 cairo_int64_t ady_dx, dy_adx;
434 ady_dx = _cairo_int32x32_64_mul (ady, dx);
435 dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
437 return _cairo_int64_cmp (ady_dx, dy_adx);
440 /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
441 if ((bdx ^ dx) < 0) {
444 cairo_int64_t bdy_dx, dy_bdx;
446 bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
447 dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
449 return _cairo_int64_cmp (bdy_dx, dy_bdx);
452 /* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
453 return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
461 * We need to compare the x-coordinate of a line for a particular y wrt to a
462 * given x, without loss of precision.
464 * The x-coordinate along an edge for a given y is:
465 * X = A_x + (Y - A_y) * A_dx / A_dy
467 * So the inequality we wish to test is:
468 * A_x + (Y - A_y) * A_dx / A_dy ∘ X
469 * where ∘ is our inequality operator.
471 * By construction, we know that A_dy (and (Y - A_y)) are
472 * all positive, so we can rearrange it thus without causing a sign change:
473 * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
475 * Given the assumption that all the deltas fit within 32 bits, we can compute
476 * this comparison directly using 64 bit arithmetic.
478 * See the similar discussion for _slope_compare() and
479 * edges_compare_x_for_y_general().
482 edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
490 if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
492 if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
495 adx = a->edge.line.p2.x - a->edge.line.p1.x;
496 dx = x - a->edge.line.p1.x;
500 if (dx == 0 || (adx ^ dx) < 0)
503 dy = y - a->edge.line.p1.y;
504 ady = a->edge.line.p2.y - a->edge.line.p1.y;
506 L = _cairo_int32x32_64_mul (dy, adx);
507 R = _cairo_int32x32_64_mul (dx, ady);
509 return _cairo_int64_cmp (L, R);
513 edges_compare_x_for_y (const cairo_bo_edge_t *a,
514 const cairo_bo_edge_t *b,
517 /* If the sweep-line is currently on an end-point of a line,
518 * then we know its precise x value (and considering that we often need to
519 * compare events at end-points, this happens frequently enough to warrant
526 HAVE_BOTH = HAVE_AX | HAVE_BX
527 } have_ax_bx = HAVE_BOTH;
530 if (y == a->edge.line.p1.y)
531 ax = a->edge.line.p1.x;
532 else if (y == a->edge.line.p2.y)
533 ax = a->edge.line.p2.x;
535 have_ax_bx &= ~HAVE_AX;
537 if (y == b->edge.line.p1.y)
538 bx = b->edge.line.p1.x;
539 else if (y == b->edge.line.p2.y)
540 bx = b->edge.line.p2.x;
542 have_ax_bx &= ~HAVE_BX;
544 switch (have_ax_bx) {
547 return edges_compare_x_for_y_general (a, b, y);
549 return -edge_compare_for_y_against_x (b, y, ax);
551 return edge_compare_for_y_against_x (a, y, bx);
558 _line_equal (const cairo_line_t *a, const cairo_line_t *b)
560 return a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
561 a->p2.x == b->p2.x && a->p2.y == b->p2.y;
565 _cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t *sweep_line,
566 const cairo_bo_edge_t *a,
567 const cairo_bo_edge_t *b)
571 /* compare the edges if not identical */
572 if (! _line_equal (&a->edge.line, &b->edge.line)) {
573 if (MAX (a->edge.line.p1.x, a->edge.line.p2.x) <
574 MIN (b->edge.line.p1.x, b->edge.line.p2.x))
576 else if (MIN (a->edge.line.p1.x, a->edge.line.p2.x) >
577 MAX (b->edge.line.p1.x, b->edge.line.p2.x))
580 cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
584 /* The two edges intersect exactly at y, so fall back on slope
585 * comparison. We know that this compare_edges function will be
586 * called only when starting a new edge, (not when stopping an
587 * edge), so we don't have to worry about conditionally inverting
588 * the sense of _slope_compare. */
589 cmp = _slope_compare (a, b);
594 /* We've got two collinear edges now. */
595 return b->edge.bottom - a->edge.bottom;
598 static inline cairo_int64_t
599 det32_64 (int32_t a, int32_t b,
600 int32_t c, int32_t d)
602 /* det = a * d - b * c */
603 return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
604 _cairo_int32x32_64_mul (b, c));
607 static inline cairo_int128_t
608 det64x32_128 (cairo_int64_t a, int32_t b,
609 cairo_int64_t c, int32_t d)
611 /* det = a * d - b * c */
612 return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
613 _cairo_int64x32_128_mul (c, b));
616 /* Compute the intersection of two lines as defined by two edges. The
617 * result is provided as a coordinate pair of 128-bit integers.
619 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
620 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
623 intersect_lines (cairo_bo_edge_t *a,
625 cairo_bo_intersect_point_t *intersection)
627 cairo_int64_t a_det, b_det;
629 /* XXX: We're assuming here that dx and dy will still fit in 32
630 * bits. That's not true in general as there could be overflow. We
631 * should prevent that before the tessellation algorithm begins.
632 * What we're doing to mitigate this is to perform clamping in
633 * cairo_bo_tessellate_polygon().
635 int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
636 int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
638 int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
639 int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
641 cairo_int64_t den_det;
645 den_det = det32_64 (dx1, dy1, dx2, dy2);
647 /* Q: Can we determine that the lines do not intersect (within range)
648 * much more cheaply than computing the intersection point i.e. by
649 * avoiding the division?
651 * X = ax + t * adx = bx + s * bdx;
652 * Y = ay + t * ady = by + s * bdy;
653 * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
656 * Therefore we can reject any intersection (under the criteria for
657 * valid intersection events) if:
658 * L^R < 0 => t < 0, or
661 * (where top/bottom must at least extend to the line endpoints).
663 * A similar substitution can be performed for s, yielding:
664 * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
666 R = det32_64 (dx2, dy2,
667 b->edge.line.p1.x - a->edge.line.p1.x,
668 b->edge.line.p1.y - a->edge.line.p1.y);
669 if (_cairo_int64_negative (den_det)) {
670 if (_cairo_int64_ge (den_det, R))
673 if (_cairo_int64_le (den_det, R))
677 R = det32_64 (dy1, dx1,
678 a->edge.line.p1.y - b->edge.line.p1.y,
679 a->edge.line.p1.x - b->edge.line.p1.x);
680 if (_cairo_int64_negative (den_det)) {
681 if (_cairo_int64_ge (den_det, R))
684 if (_cairo_int64_le (den_det, R))
688 /* We now know that the two lines should intersect within range. */
690 a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
691 a->edge.line.p2.x, a->edge.line.p2.y);
692 b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
693 b->edge.line.p2.x, b->edge.line.p2.y);
695 /* x = det (a_det, dx1, b_det, dx2) / den_det */
696 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
699 if (_cairo_int64_eq (qr.rem, den_det))
702 intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
704 intersection->x.exactness = EXACT;
705 if (! _cairo_int64_is_zero (qr.rem)) {
706 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
707 qr.rem = _cairo_int64_negate (qr.rem);
708 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
709 if (_cairo_int64_ge (qr.rem, den_det)) {
710 qr.quo = _cairo_int64_add (qr.quo,
711 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
713 intersection->x.exactness = INEXACT;
716 intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo);
718 /* y = det (a_det, dy1, b_det, dy2) / den_det */
719 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
722 if (_cairo_int64_eq (qr.rem, den_det))
725 intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
727 intersection->y.exactness = EXACT;
728 if (! _cairo_int64_is_zero (qr.rem)) {
729 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
730 qr.rem = _cairo_int64_negate (qr.rem);
731 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
732 if (_cairo_int64_ge (qr.rem, den_det)) {
733 qr.quo = _cairo_int64_add (qr.quo,
734 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
736 intersection->y.exactness = INEXACT;
739 intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo);
745 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
748 /* First compare the quotient */
753 /* With quotient identical, if remainder is 0 then compare equal */
754 /* Otherwise, the non-zero remainder makes a > b */
755 return INEXACT == a.exactness;
758 /* Does the given edge contain the given point. The point must already
759 * be known to be contained within the line determined by the edge,
760 * (most likely the point results from an intersection of this edge
763 * If we had exact arithmetic, then this function would simply be a
764 * matter of examining whether the y value of the point lies within
765 * the range of y values of the edge. But since intersection points
766 * are not exact due to being rounded to the nearest integer within
767 * the available precision, we must also examine the x value of the
770 * The definition of "contains" here is that the given intersection
771 * point will be seen by the sweep line after the start event for the
772 * given edge and before the stop event for the edge. See the comments
773 * in the implementation for more details.
776 _cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
777 cairo_bo_intersect_point_t *point)
779 int cmp_top, cmp_bottom;
781 /* XXX: When running the actual algorithm, we don't actually need to
782 * compare against edge->top at all here, since any intersection above
783 * top is eliminated early via a slope comparison. We're leaving these
784 * here for now only for the sake of the quadratic-time intersection
785 * finder which needs them.
788 cmp_top = _cairo_bo_intersect_ordinate_32_compare (point->y,
790 cmp_bottom = _cairo_bo_intersect_ordinate_32_compare (point->y,
793 if (cmp_top < 0 || cmp_bottom > 0)
798 if (cmp_top > 0 && cmp_bottom < 0)
803 /* At this stage, the point lies on the same y value as either
804 * edge->top or edge->bottom, so we have to examine the x value in
805 * order to properly determine containment. */
807 /* If the y value of the point is the same as the y value of the
808 * top of the edge, then the x value of the point must be greater
809 * to be considered as inside the edge. Similarly, if the y value
810 * of the point is the same as the y value of the bottom of the
811 * edge, then the x value of the point must be less to be
812 * considered as inside. */
817 top_x = _line_compute_intersection_x_for_y (&edge->edge.line,
819 return _cairo_bo_intersect_ordinate_32_compare (point->x, top_x) > 0;
820 } else { /* cmp_bottom == 0 */
823 bot_x = _line_compute_intersection_x_for_y (&edge->edge.line,
825 return _cairo_bo_intersect_ordinate_32_compare (point->x, bot_x) < 0;
829 /* Compute the intersection of two edges. The result is provided as a
830 * coordinate pair of 128-bit integers.
832 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
833 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
834 * intersection of the lines defined by the edges occurs outside of
835 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
836 * are exactly parallel.
838 * Note that when determining if a candidate intersection is "inside"
839 * an edge, we consider both the infinitesimal shortening and the
840 * infinitesimal tilt rules described by John Hobby. Specifically, if
841 * the intersection is exactly the same as an edge point, it is
842 * effectively outside (no intersection is returned). Also, if the
843 * intersection point has the same
846 _cairo_bo_edge_intersect (cairo_bo_edge_t *a,
848 cairo_bo_point32_t *intersection)
850 cairo_bo_intersect_point_t quorem;
852 if (! intersect_lines (a, b, &quorem))
855 if (! _cairo_bo_edge_contains_intersect_point (a, &quorem))
858 if (! _cairo_bo_edge_contains_intersect_point (b, &quorem))
861 /* Now that we've correctly compared the intersection point and
862 * determined that it lies within the edge, then we know that we
863 * no longer need any more bits of storage for the intersection
864 * than we do for our edge coordinates. We also no longer need the
865 * remainder from the division. */
866 intersection->x = quorem.x.ordinate;
867 intersection->y = quorem.y.ordinate;
873 cairo_bo_event_compare (const cairo_bo_event_t *a,
874 const cairo_bo_event_t *b)
878 cmp = _cairo_bo_point32_compare (&a->point, &b->point);
882 cmp = a->type - b->type;
890 _pqueue_init (pqueue_t *pq)
892 pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
895 pq->elements = pq->elements_embedded;
899 _pqueue_fini (pqueue_t *pq)
901 if (pq->elements != pq->elements_embedded)
905 static cairo_status_t
906 _pqueue_grow (pqueue_t *pq)
908 cairo_bo_event_t **new_elements;
911 if (pq->elements == pq->elements_embedded) {
912 new_elements = _cairo_malloc_ab (pq->max_size,
913 sizeof (cairo_bo_event_t *));
914 if (unlikely (new_elements == NULL))
915 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
917 memcpy (new_elements, pq->elements_embedded,
918 sizeof (pq->elements_embedded));
920 new_elements = _cairo_realloc_ab (pq->elements,
922 sizeof (cairo_bo_event_t *));
923 if (unlikely (new_elements == NULL))
924 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
927 pq->elements = new_elements;
928 return CAIRO_STATUS_SUCCESS;
931 static inline cairo_status_t
932 _pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
934 cairo_bo_event_t **elements;
937 if (unlikely (pq->size + 1 == pq->max_size)) {
938 cairo_status_t status;
940 status = _pqueue_grow (pq);
941 if (unlikely (status))
945 elements = pq->elements;
948 i != PQ_FIRST_ENTRY &&
949 cairo_bo_event_compare (event,
950 elements[parent = PQ_PARENT_INDEX (i)]) < 0;
953 elements[i] = elements[parent];
958 return CAIRO_STATUS_SUCCESS;
962 _pqueue_pop (pqueue_t *pq)
964 cairo_bo_event_t **elements = pq->elements;
965 cairo_bo_event_t *tail;
968 tail = elements[pq->size--];
970 elements[PQ_FIRST_ENTRY] = NULL;
974 for (i = PQ_FIRST_ENTRY;
975 (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
978 if (child != pq->size &&
979 cairo_bo_event_compare (elements[child+1],
980 elements[child]) < 0)
985 if (cairo_bo_event_compare (elements[child], tail) >= 0)
988 elements[i] = elements[child];
993 static inline cairo_status_t
994 _cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
995 cairo_bo_event_type_t type,
998 const cairo_point_t *point)
1000 cairo_bo_queue_event_t *event;
1002 event = _cairo_freepool_alloc (&queue->pool);
1003 if (unlikely (event == NULL))
1004 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1009 event->point = *point;
1011 return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
1015 _cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
1016 cairo_bo_event_t *event)
1018 _cairo_freepool_free (&queue->pool, event);
1021 static cairo_bo_event_t *
1022 _cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
1024 cairo_bo_event_t *event, *cmp;
1026 event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
1027 cmp = *event_queue->start_events;
1028 if (event == NULL ||
1029 (cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
1032 event_queue->start_events++;
1036 _pqueue_pop (&event_queue->pqueue);
1042 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
1044 cairo_bo_event_compare)
1047 _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
1048 cairo_bo_event_t **start_events,
1051 event_queue->start_events = start_events;
1053 _cairo_freepool_init (&event_queue->pool,
1054 sizeof (cairo_bo_queue_event_t));
1055 _pqueue_init (&event_queue->pqueue);
1056 event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
1059 static cairo_status_t
1060 _cairo_bo_event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
1061 cairo_bo_edge_t *edge)
1063 cairo_bo_point32_t point;
1065 point.y = edge->edge.bottom;
1066 point.x = _line_compute_intersection_x_for_y (&edge->edge.line,
1068 return _cairo_bo_event_queue_insert (event_queue,
1069 CAIRO_BO_EVENT_TYPE_STOP,
1075 _cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
1077 _pqueue_fini (&event_queue->pqueue);
1078 _cairo_freepool_fini (&event_queue->pool);
1081 static inline cairo_status_t
1082 _cairo_bo_event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
1083 cairo_bo_edge_t *left,
1084 cairo_bo_edge_t *right)
1086 cairo_bo_point32_t intersection;
1088 if (MAX (left->edge.line.p1.x, left->edge.line.p2.x) <=
1089 MIN (right->edge.line.p1.x, right->edge.line.p2.x))
1090 return CAIRO_STATUS_SUCCESS;
1092 if (_line_equal (&left->edge.line, &right->edge.line))
1093 return CAIRO_STATUS_SUCCESS;
1095 /* The names "left" and "right" here are correct descriptions of
1096 * the order of the two edges within the active edge list. So if a
1097 * slope comparison also puts left less than right, then we know
1098 * that the intersection of these two segments has already
1099 * occurred before the current sweep line position. */
1100 if (_slope_compare (left, right) <= 0)
1101 return CAIRO_STATUS_SUCCESS;
1103 if (! _cairo_bo_edge_intersect (left, right, &intersection))
1104 return CAIRO_STATUS_SUCCESS;
1106 return _cairo_bo_event_queue_insert (event_queue,
1107 CAIRO_BO_EVENT_TYPE_INTERSECTION,
1113 _cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
1115 sweep_line->head = NULL;
1116 sweep_line->stopped = NULL;
1117 sweep_line->current_y = INT32_MIN;
1118 sweep_line->current_edge = NULL;
1121 static cairo_status_t
1122 _cairo_bo_sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
1123 cairo_bo_edge_t *edge)
1125 if (sweep_line->current_edge != NULL) {
1126 cairo_bo_edge_t *prev, *next;
1129 cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
1130 sweep_line->current_edge,
1133 prev = sweep_line->current_edge;
1135 while (next != NULL &&
1136 _cairo_bo_sweep_line_compare_edges (sweep_line,
1139 prev = next, next = prev->next;
1147 } else if (cmp > 0) {
1148 next = sweep_line->current_edge;
1150 while (prev != NULL &&
1151 _cairo_bo_sweep_line_compare_edges (sweep_line,
1154 next = prev, prev = next->prev;
1163 sweep_line->head = edge;
1165 prev = sweep_line->current_edge;
1167 edge->next = prev->next;
1168 if (prev->next != NULL)
1169 prev->next->prev = edge;
1173 sweep_line->head = edge;
1177 sweep_line->current_edge = edge;
1179 return CAIRO_STATUS_SUCCESS;
1183 _cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
1184 cairo_bo_edge_t *edge)
1186 if (edge->prev != NULL)
1187 edge->prev->next = edge->next;
1189 sweep_line->head = edge->next;
1191 if (edge->next != NULL)
1192 edge->next->prev = edge->prev;
1194 if (sweep_line->current_edge == edge)
1195 sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
1199 _cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
1200 cairo_bo_edge_t *left,
1201 cairo_bo_edge_t *right)
1203 if (left->prev != NULL)
1204 left->prev->next = right;
1206 sweep_line->head = right;
1208 if (right->next != NULL)
1209 right->next->prev = left;
1211 left->next = right->next;
1214 right->prev = left->prev;
1218 #if DEBUG_PRINT_STATE
1220 _cairo_bo_edge_print (cairo_bo_edge_t *edge)
1222 printf ("(0x%x, 0x%x)-(0x%x, 0x%x)",
1223 edge->edge.line.p1.x, edge->edge.line.p1.y,
1224 edge->edge.line.p2.x, edge->edge.line.p2.y);
1228 _cairo_bo_event_print (cairo_bo_event_t *event)
1230 switch (event->type) {
1231 case CAIRO_BO_EVENT_TYPE_START:
1234 case CAIRO_BO_EVENT_TYPE_STOP:
1237 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1238 printf ("Intersection: ");
1241 printf ("(%d, %d)\t", event->point.x, event->point.y);
1242 _cairo_bo_edge_print (event->e1);
1243 if (event->type == CAIRO_BO_EVENT_TYPE_INTERSECTION) {
1245 _cairo_bo_edge_print (event->e2);
1251 _cairo_bo_event_queue_print (cairo_bo_event_queue_t *event_queue)
1253 /* XXX: fixme to print the start/stop array too. */
1254 printf ("Event queue:\n");
1258 _cairo_bo_sweep_line_print (cairo_bo_sweep_line_t *sweep_line)
1260 cairo_bool_t first = TRUE;
1261 cairo_bo_edge_t *edge;
1263 printf ("Sweep line from edge list: ");
1265 for (edge = sweep_line->head;
1271 _cairo_bo_edge_print (edge);
1278 print_state (const char *msg,
1279 cairo_bo_event_t *event,
1280 cairo_bo_event_queue_t *event_queue,
1281 cairo_bo_sweep_line_t *sweep_line)
1283 printf ("%s ", msg);
1284 _cairo_bo_event_print (event);
1285 _cairo_bo_event_queue_print (event_queue);
1286 _cairo_bo_sweep_line_print (sweep_line);
1292 static void CAIRO_PRINTF_FORMAT (1, 2)
1293 event_log (const char *fmt, ...)
1297 if (getenv ("CAIRO_DEBUG_EVENTS") == NULL)
1300 file = fopen ("bo-events.txt", "a");
1305 vfprintf (file, fmt, ap);
1313 static inline cairo_bool_t
1314 edges_colinear (const cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
1319 p |= (a->edge.line.p1.x == b->edge.line.p1.x) << 0;
1320 p |= (a->edge.line.p1.y == b->edge.line.p1.y) << 1;
1321 p |= (a->edge.line.p2.x == b->edge.line.p2.x) << 3;
1322 p |= (a->edge.line.p2.y == b->edge.line.p2.y) << 4;
1323 if (p == ((1 << 0) | (1 << 1) | (1 << 3) | (1 << 4)))
1326 if (_slope_compare (a, b))
1329 /* The choice of y is not truly arbitrary since we must guarantee that it
1330 * is greater than the start of either line.
1333 /* colinear if either end-point are coincident */
1334 return ((p >> 1) & p) != 0;
1335 } else if (a->edge.line.p1.y < b->edge.line.p1.y) {
1336 return edge_compare_for_y_against_x (b,
1338 a->edge.line.p1.x) == 0;
1340 return edge_compare_for_y_against_x (a,
1342 b->edge.line.p1.x) == 0;
1346 /* Adds the trapezoid, if any, of the left edge to the #cairo_traps_t */
1347 static cairo_status_t
1348 _cairo_bo_edge_end_trap (cairo_bo_edge_t *left,
1350 cairo_traps_t *traps)
1352 cairo_bo_trap_t *trap = &left->deferred_trap;
1354 /* Only emit (trivial) non-degenerate trapezoids with positive height. */
1355 if (likely (trap->top < bot)) {
1356 _cairo_traps_add_trap (traps,
1358 &left->edge.line, &trap->right->edge.line);
1360 #if DEBUG_PRINT_STATE
1361 printf ("Deferred trap: left=(%x, %x)-(%x,%x) "
1362 "right=(%x,%x)-(%x,%x) top=%x, bot=%x\n",
1363 left->edge.line.p1.x, left->edge.line.p1.y,
1364 left->edge.line.p2.x, left->edge.line.p2.y,
1365 trap->right->edge.line.p1.x, trap->right->edge.line.p1.y,
1366 trap->right->edge.line.p2.x, trap->right->edge.line.p2.y,
1370 event_log ("end trap: %lu %lu %d %d\n",
1380 return _cairo_traps_status (traps);
1384 /* Start a new trapezoid at the given top y coordinate, whose edges
1385 * are `edge' and `edge->next'. If `edge' already has a trapezoid,
1386 * then either add it to the traps in `traps', if the trapezoid's
1387 * right edge differs from `edge->next', or do nothing if the new
1388 * trapezoid would be a continuation of the existing one. */
1389 static inline cairo_status_t
1390 _cairo_bo_edge_start_or_continue_trap (cairo_bo_edge_t *left,
1391 cairo_bo_edge_t *right,
1393 cairo_traps_t *traps)
1395 cairo_status_t status;
1397 if (left->deferred_trap.right == right)
1398 return CAIRO_STATUS_SUCCESS;
1401 if (left->deferred_trap.right != NULL) {
1402 if (edges_colinear (left->deferred_trap.right, right))
1404 /* continuation on right, so just swap edges */
1405 left->deferred_trap.right = right;
1406 return CAIRO_STATUS_SUCCESS;
1409 status = _cairo_bo_edge_end_trap (left, top, traps);
1410 if (unlikely (status))
1414 if (! edges_colinear (left, right)) {
1415 left->deferred_trap.top = top;
1416 left->deferred_trap.right = right;
1419 event_log ("begin trap: %lu %lu %d\n",
1426 return CAIRO_STATUS_SUCCESS;
1429 static inline cairo_status_t
1430 _active_edges_to_traps (cairo_bo_edge_t *pos,
1433 cairo_traps_t *traps)
1435 cairo_bo_edge_t *left;
1436 cairo_status_t status;
1440 #if DEBUG_PRINT_STATE
1441 printf ("Processing active edges for %x\n", top);
1446 while (pos != NULL) {
1447 if (pos != left && pos->deferred_trap.right) {
1448 /* XXX It shouldn't be possible to here with 2 deferred traps
1449 * on colinear edges... See bug-bo-rictoz.
1451 if (left->deferred_trap.right == NULL &&
1452 edges_colinear (left, pos))
1454 /* continuation on left */
1455 left->deferred_trap = pos->deferred_trap;
1456 pos->deferred_trap.right = NULL;
1460 status = _cairo_bo_edge_end_trap (pos, top, traps);
1461 if (unlikely (status))
1466 in_out += pos->edge.dir;
1467 if ((in_out & mask) == 0) {
1468 /* skip co-linear edges */
1469 if (pos->next == NULL || ! edges_colinear (pos, pos->next)) {
1470 status = _cairo_bo_edge_start_or_continue_trap (left, pos,
1472 if (unlikely (status))
1482 return CAIRO_STATUS_SUCCESS;
1486 /* Execute a single pass of the Bentley-Ottmann algorithm on edges,
1487 * generating trapezoids according to the fill_rule and appending them
1489 static cairo_status_t
1490 _cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_event_t **start_events,
1493 cairo_traps_t *traps,
1494 int *num_intersections)
1496 cairo_status_t status = CAIRO_STATUS_SUCCESS; /* silence compiler */
1497 int intersection_count = 0;
1498 cairo_bo_event_queue_t event_queue;
1499 cairo_bo_sweep_line_t sweep_line;
1500 cairo_bo_event_t *event;
1501 cairo_bo_edge_t *left, *right;
1502 cairo_bo_edge_t *e1, *e2;
1504 /* convert the fill_rule into a winding mask */
1505 if (fill_rule == CAIRO_FILL_RULE_WINDING)
1506 fill_rule = (unsigned) -1;
1514 for (i = 0; i < num_events; i++) {
1515 cairo_bo_start_event_t *event =
1516 ((cairo_bo_start_event_t **) start_events)[i];
1517 event_log ("edge: %lu (%d, %d) (%d, %d) (%d, %d) %d\n",
1518 (long) &events[i].edge,
1519 event->edge.edge.line.p1.x,
1520 event->edge.edge.line.p1.y,
1521 event->edge.edge.line.p2.x,
1522 event->edge.edge.line.p2.y,
1525 event->edge.edge.dir);
1530 _cairo_bo_event_queue_init (&event_queue, start_events, num_events);
1531 _cairo_bo_sweep_line_init (&sweep_line);
1533 while ((event = _cairo_bo_event_dequeue (&event_queue))) {
1534 if (event->point.y != sweep_line.current_y) {
1535 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1536 if (e1->deferred_trap.right != NULL) {
1537 status = _cairo_bo_edge_end_trap (e1,
1540 if (unlikely (status))
1544 sweep_line.stopped = NULL;
1546 status = _active_edges_to_traps (sweep_line.head,
1547 sweep_line.current_y,
1549 if (unlikely (status))
1552 sweep_line.current_y = event->point.y;
1556 event_log ("event: %d (%ld, %ld) %lu, %lu\n",
1558 (long) event->point.x,
1559 (long) event->point.y,
1564 switch (event->type) {
1565 case CAIRO_BO_EVENT_TYPE_START:
1566 e1 = &((cairo_bo_start_event_t *) event)->edge;
1568 status = _cairo_bo_sweep_line_insert (&sweep_line, e1);
1569 if (unlikely (status))
1572 status = _cairo_bo_event_queue_insert_stop (&event_queue, e1);
1573 if (unlikely (status))
1576 /* check to see if this is a continuation of a stopped edge */
1577 /* XXX change to an infinitesimal lengthening rule */
1578 for (left = sweep_line.stopped; left; left = left->next) {
1579 if (e1->edge.top <= left->edge.bottom &&
1580 edges_colinear (e1, left))
1582 e1->deferred_trap = left->deferred_trap;
1583 if (left->prev != NULL)
1584 left->prev = left->next;
1586 sweep_line.stopped = left->next;
1587 if (left->next != NULL)
1588 left->next->prev = left->prev;
1597 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
1598 if (unlikely (status))
1602 if (right != NULL) {
1603 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1604 if (unlikely (status))
1610 case CAIRO_BO_EVENT_TYPE_STOP:
1611 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1612 _cairo_bo_event_queue_delete (&event_queue, event);
1617 _cairo_bo_sweep_line_delete (&sweep_line, e1);
1619 /* first, check to see if we have a continuation via a fresh edge */
1620 if (e1->deferred_trap.right != NULL) {
1621 e1->next = sweep_line.stopped;
1622 if (sweep_line.stopped != NULL)
1623 sweep_line.stopped->prev = e1;
1624 sweep_line.stopped = e1;
1628 if (left != NULL && right != NULL) {
1629 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
1630 if (unlikely (status))
1636 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1637 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1638 e2 = ((cairo_bo_queue_event_t *) event)->e2;
1639 _cairo_bo_event_queue_delete (&event_queue, event);
1641 /* skip this intersection if its edges are not adjacent */
1645 intersection_count++;
1650 _cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
1652 /* after the swap e2 is left of e1 */
1655 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
1656 if (unlikely (status))
1660 if (right != NULL) {
1661 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1662 if (unlikely (status))
1670 *num_intersections = intersection_count;
1671 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1672 if (e1->deferred_trap.right != NULL) {
1673 status = _cairo_bo_edge_end_trap (e1, e1->edge.bottom, traps);
1674 if (unlikely (status))
1679 _cairo_bo_event_queue_fini (&event_queue);
1689 _cairo_bentley_ottmann_tessellate_polygon (cairo_traps_t *traps,
1690 const cairo_polygon_t *polygon,
1691 cairo_fill_rule_t fill_rule)
1694 cairo_status_t status;
1695 cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
1696 cairo_bo_start_event_t *events;
1697 cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
1698 cairo_bo_event_t **event_ptrs;
1699 cairo_bo_start_event_t *stack_event_y[64];
1700 cairo_bo_start_event_t **event_y = NULL;
1701 int i, num_events, y, ymin, ymax;
1703 num_events = polygon->num_edges;
1704 if (unlikely (0 == num_events))
1705 return CAIRO_STATUS_SUCCESS;
1707 if (polygon->num_limits) {
1708 ymin = _cairo_fixed_integer_floor (polygon->limit.p1.y);
1709 ymax = _cairo_fixed_integer_ceil (polygon->limit.p2.y) - ymin;
1712 event_y = _cairo_malloc_ab(sizeof (cairo_bo_event_t*), ymax);
1714 event_y = stack_event_y;
1715 memset (event_y, 0, ymax * sizeof(cairo_bo_event_t *));
1718 events = stack_events;
1719 event_ptrs = stack_event_ptrs;
1720 if (num_events > ARRAY_LENGTH (stack_events)) {
1721 events = _cairo_malloc_ab_plus_c (num_events,
1722 sizeof (cairo_bo_start_event_t) +
1723 sizeof (cairo_bo_event_t *),
1724 sizeof (cairo_bo_event_t *));
1725 if (unlikely (events == NULL))
1726 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;
1744 y = _cairo_fixed_integer_floor (events[i].point.y) - ymin;
1745 events[i].edge.next = (cairo_bo_edge_t *) event_y[y];
1746 event_y[y] = (cairo_bo_start_event_t *) &events[i];
1748 event_ptrs[i] = (cairo_bo_event_t *) &events[i];
1752 for (y = i = 0; y < ymax && i < num_events; y++) {
1753 cairo_bo_start_event_t *e;
1755 for (e = event_y[y]; e; e = (cairo_bo_start_event_t *)e->edge.next)
1756 event_ptrs[i++] = (cairo_bo_event_t *) e;
1758 _cairo_bo_event_queue_sort (event_ptrs+j, i-j);
1760 if (event_y != stack_event_y)
1763 _cairo_bo_event_queue_sort (event_ptrs, i);
1764 event_ptrs[i] = NULL;
1767 dump_edges (events, num_events, "bo-polygon-edges.txt");
1770 /* XXX: This would be the convenient place to throw in multiple
1771 * passes of the Bentley-Ottmann algorithm. It would merely
1772 * require storing the results of each pass into a temporary
1774 status = _cairo_bentley_ottmann_tessellate_bo_edges (event_ptrs, num_events,
1778 dump_traps (traps, "bo-polygon-out.txt");
1781 if (events != stack_events)
1788 _cairo_bentley_ottmann_tessellate_traps (cairo_traps_t *traps,
1789 cairo_fill_rule_t fill_rule)
1791 cairo_status_t status;
1792 cairo_polygon_t polygon;
1795 if (unlikely (0 == traps->num_traps))
1796 return CAIRO_STATUS_SUCCESS;
1799 dump_traps (traps, "bo-traps-in.txt");
1802 _cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
1804 for (i = 0; i < traps->num_traps; i++) {
1805 status = _cairo_polygon_add_line (&polygon,
1806 &traps->traps[i].left,
1807 traps->traps[i].top,
1808 traps->traps[i].bottom,
1810 if (unlikely (status))
1813 status = _cairo_polygon_add_line (&polygon,
1814 &traps->traps[i].right,
1815 traps->traps[i].top,
1816 traps->traps[i].bottom,
1818 if (unlikely (status))
1822 _cairo_traps_clear (traps);
1823 status = _cairo_bentley_ottmann_tessellate_polygon (traps,
1828 dump_traps (traps, "bo-traps-out.txt");
1832 _cairo_polygon_fini (&polygon);
1839 edges_have_an_intersection_quadratic (cairo_bo_edge_t *edges,
1844 cairo_bo_edge_t *a, *b;
1845 cairo_bo_point32_t intersection;
1847 /* We must not be given any upside-down edges. */
1848 for (i = 0; i < num_edges; i++) {
1849 assert (_cairo_bo_point32_compare (&edges[i].top, &edges[i].bottom) < 0);
1850 edges[i].line.p1.x <<= CAIRO_BO_GUARD_BITS;
1851 edges[i].line.p1.y <<= CAIRO_BO_GUARD_BITS;
1852 edges[i].line.p2.x <<= CAIRO_BO_GUARD_BITS;
1853 edges[i].line.p2.y <<= CAIRO_BO_GUARD_BITS;
1856 for (i = 0; i < num_edges; i++) {
1857 for (j = 0; j < num_edges; j++) {
1864 if (! _cairo_bo_edge_intersect (a, b, &intersection))
1867 printf ("Found intersection (%d,%d) between (%d,%d)-(%d,%d) and (%d,%d)-(%d,%d)\n",
1870 a->line.p1.x, a->line.p1.y,
1871 a->line.p2.x, a->line.p2.y,
1872 b->line.p1.x, b->line.p1.y,
1873 b->line.p2.x, b->line.p2.y);
1881 #define TEST_MAX_EDGES 10
1883 typedef struct test {
1885 const char *description;
1887 cairo_bo_edge_t edges[TEST_MAX_EDGES];
1894 "3 edges all intersecting very close to each other",
1897 { { 4, 2}, {0, 0}, { 9, 9}, NULL, NULL },
1898 { { 7, 2}, {0, 0}, { 2, 3}, NULL, NULL },
1899 { { 5, 2}, {0, 0}, { 1, 7}, NULL, NULL }
1903 "inconsistent data",
1904 "Derived from random testing---was leading to skip list and edge list disagreeing.",
1907 { { 2, 3}, {0, 0}, { 8, 9}, NULL, NULL },
1908 { { 2, 3}, {0, 0}, { 6, 7}, NULL, NULL }
1913 "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?",
1916 { { 6, 2}, {0, 0}, { 6, 5}, NULL, NULL },
1917 { { 3, 5}, {0, 0}, { 5, 6}, NULL, NULL },
1918 { { 9, 2}, {0, 0}, { 5, 6}, NULL, NULL },
1922 "minimal-intersection",
1923 "Intersection of a two from among the smallest possible edges.",
1926 { { 0, 0}, {0, 0}, { 1, 1}, NULL, NULL },
1927 { { 1, 0}, {0, 0}, { 0, 1}, NULL, NULL }
1932 "A simple intersection of two edges at an integer (2,2).",
1935 { { 1, 1}, {0, 0}, { 3, 3}, NULL, NULL },
1936 { { 2, 1}, {0, 0}, { 2, 3}, NULL, NULL }
1940 "bend-to-horizontal",
1941 "With intersection truncation one edge bends to horizontal",
1944 { { 9, 1}, {0, 0}, {3, 7}, NULL, NULL },
1945 { { 3, 5}, {0, 0}, {9, 9}, NULL, NULL }
1953 "An intersection that occurs at the endpoint of a segment.",
1955 { { 4, 6}, { 5, 6}, NULL, { { NULL }} },
1956 { { 4, 5}, { 5, 7}, NULL, { { NULL }} },
1957 { { 0, 0}, { 0, 0}, NULL, { { NULL }} },
1961 name = "overlapping",
1962 desc = "Parallel segments that share an endpoint, with different slopes.",
1964 { top = { x = 2, y = 0}, bottom = { x = 1, y = 1}},
1965 { top = { x = 2, y = 0}, bottom = { x = 0, y = 2}},
1966 { top = { x = 0, y = 3}, bottom = { x = 1, y = 3}},
1967 { top = { x = 0, y = 3}, bottom = { x = 2, y = 3}},
1968 { top = { x = 0, y = 4}, bottom = { x = 0, y = 6}},
1969 { top = { x = 0, y = 5}, bottom = { x = 0, y = 6}}
1973 name = "hobby_stage_3",
1974 desc = "A particularly tricky part of the 3rd stage of the 'hobby' test below.",
1976 { top = { x = -1, y = -2}, bottom = { x = 4, y = 2}},
1977 { top = { x = 5, y = 3}, bottom = { x = 9, y = 5}},
1978 { top = { x = 5, y = 3}, bottom = { x = 6, y = 3}},
1983 desc = "Example from John Hobby's paper. Requires 3 passes of the iterative algorithm.",
1985 { top = { x = 0, y = 0}, bottom = { x = 9, y = 5}},
1986 { top = { x = 0, y = 0}, bottom = { x = 13, y = 6}},
1987 { top = { x = -1, y = -2}, bottom = { x = 9, y = 5}}
1992 desc = "Edges with same start/stop points but different slopes",
1994 { top = { x = 4, y = 1}, bottom = { x = 6, y = 3}},
1995 { top = { x = 4, y = 1}, bottom = { x = 2, y = 3}},
1996 { top = { x = 2, y = 4}, bottom = { x = 4, y = 6}},
1997 { top = { x = 6, y = 4}, bottom = { x = 4, y = 6}}
2001 name = "horizontal",
2002 desc = "Test of a horizontal edge",
2004 { top = { x = 1, y = 1}, bottom = { x = 6, y = 6}},
2005 { top = { x = 2, y = 3}, bottom = { x = 5, y = 3}}
2010 desc = "Test of a vertical edge",
2012 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
2013 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
2018 desc = "Two overlapping edges with the same slope",
2020 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
2021 { top = { x = 5, y = 2}, bottom = { x = 5, y = 6}},
2022 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
2027 desc = "Several segments with a common intersection point",
2029 { top = { x = 1, y = 2}, bottom = { x = 5, y = 4} },
2030 { top = { x = 1, y = 1}, bottom = { x = 5, y = 5} },
2031 { top = { x = 2, y = 1}, bottom = { x = 4, y = 5} },
2032 { top = { x = 4, y = 1}, bottom = { x = 2, y = 5} },
2033 { top = { x = 5, y = 1}, bottom = { x = 1, y = 5} },
2034 { top = { x = 5, y = 2}, bottom = { x = 1, y = 4} }
2041 run_test (const char *test_name,
2042 cairo_bo_edge_t *test_edges,
2045 int i, intersections, passes;
2046 cairo_bo_edge_t *edges;
2047 cairo_array_t intersected_edges;
2049 printf ("Testing: %s\n", test_name);
2051 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
2053 intersections = _cairo_bentley_ottmann_intersect_edges (test_edges, num_edges, &intersected_edges);
2055 printf ("Pass 1 found %d intersections:\n", intersections);
2058 /* XXX: Multi-pass Bentley-Ottmmann. Preferable would be to add a
2059 * pass of Hobby's tolerance-square algorithm instead. */
2061 while (intersections) {
2062 int num_edges = _cairo_array_num_elements (&intersected_edges);
2064 edges = _cairo_malloc_ab (num_edges, sizeof (cairo_bo_edge_t));
2065 assert (edges != NULL);
2066 memcpy (edges, _cairo_array_index (&intersected_edges, 0), num_edges * sizeof (cairo_bo_edge_t));
2067 _cairo_array_fini (&intersected_edges);
2068 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
2069 intersections = _cairo_bentley_ottmann_intersect_edges (edges, num_edges, &intersected_edges);
2073 printf ("Pass %d found %d remaining intersections:\n", passes, intersections);
2076 for (i = 0; i < passes; i++)
2078 printf ("No remainining intersections found after pass %d\n", passes);
2082 if (edges_have_an_intersection_quadratic (_cairo_array_index (&intersected_edges, 0),
2083 _cairo_array_num_elements (&intersected_edges)))
2084 printf ("*** FAIL ***\n");
2088 _cairo_array_fini (&intersected_edges);
2093 #define MAX_RANDOM 300
2098 char random_name[] = "random-XX";
2099 cairo_bo_edge_t random_edges[MAX_RANDOM], *edge;
2100 unsigned int i, num_random;
2103 for (i = 0; i < ARRAY_LENGTH (tests); i++) {
2105 run_test (test->name, test->edges, test->num_edges);
2108 for (num_random = 0; num_random < MAX_RANDOM; num_random++) {
2110 for (i = 0; i < num_random; i++) {
2112 edge = &random_edges[i];
2113 edge->line.p1.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2114 edge->line.p1.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2115 edge->line.p2.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2116 edge->line.p2.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
2117 if (edge->line.p1.y > edge->line.p2.y) {
2118 int32_t tmp = edge->line.p1.y;
2119 edge->line.p1.y = edge->line.p2.y;
2120 edge->line.p2.y = tmp;
2122 } while (edge->line.p1.y == edge->line.p2.y);
2125 sprintf (random_name, "random-%02d", num_random);
2127 run_test (random_name, random_edges, num_random);