2 * Copyright © 2004 Carl Worth
3 * Copyright © 2006 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Carl Worth
33 * Carl D. Worth <cworth@cworth.org>
36 /* Provide definitions for standalone compilation */
39 #include "cairo-skiplist-private.h"
40 #include "cairo-freelist-private.h"
41 #include "cairo-combsort-private.h"
43 #define DEBUG_VALIDATE 0
44 #define DEBUG_PRINT_STATE 0
46 typedef cairo_point_t cairo_bo_point32_t
;
48 typedef struct _cairo_bo_point128
{
51 } cairo_bo_point128_t
;
53 typedef struct _cairo_bo_intersect_ordinate
{
55 enum { EXACT
, INEXACT
} exactness
;
56 } cairo_bo_intersect_ordinate_t
;
58 typedef struct _cairo_bo_intersect_point
{
59 cairo_bo_intersect_ordinate_t x
;
60 cairo_bo_intersect_ordinate_t y
;
61 } cairo_bo_intersect_point_t
;
63 typedef struct _cairo_bo_edge cairo_bo_edge_t
;
64 typedef struct _sweep_line_elt sweep_line_elt_t
;
65 typedef struct _cairo_bo_trap cairo_bo_trap_t
;
66 typedef struct _cairo_bo_traps cairo_bo_traps_t
;
68 /* A deferred trapezoid of an edge. */
69 struct _cairo_bo_trap
{
70 cairo_bo_edge_t
*right
;
74 struct _cairo_bo_traps
{
76 cairo_freelist_t freelist
;
78 /* These form the closed bounding box of the original input
86 struct _cairo_bo_edge
{
87 cairo_bo_point32_t top
;
88 cairo_bo_point32_t middle
;
89 cairo_bo_point32_t bottom
;
91 cairo_bo_edge_t
*prev
;
92 cairo_bo_edge_t
*next
;
93 cairo_bo_trap_t
*deferred_trap
;
94 sweep_line_elt_t
*sweep_line_elt
;
97 struct _sweep_line_elt
{
98 cairo_bo_edge_t
*edge
;
102 #define SKIP_ELT_TO_EDGE_ELT(elt) SKIP_LIST_ELT_TO_DATA (sweep_line_elt_t, (elt))
103 #define SKIP_ELT_TO_EDGE(elt) (SKIP_ELT_TO_EDGE_ELT (elt)->edge)
106 CAIRO_BO_STATUS_INTERSECTION
,
107 CAIRO_BO_STATUS_PARALLEL
,
108 CAIRO_BO_STATUS_NO_INTERSECTION
112 CAIRO_BO_EVENT_TYPE_START
,
113 CAIRO_BO_EVENT_TYPE_STOP
,
114 CAIRO_BO_EVENT_TYPE_INTERSECTION
115 } cairo_bo_event_type_t
;
117 typedef struct _cairo_bo_event
{
118 cairo_bo_event_type_t type
;
121 cairo_bo_point32_t point
;
125 #define SKIP_ELT_TO_EVENT(elt) SKIP_LIST_ELT_TO_DATA (cairo_bo_event_t, (elt))
127 typedef struct _cairo_bo_event_queue
{
128 cairo_skip_list_t intersection_queue
;
130 cairo_bo_event_t
*startstop_events
;
131 cairo_bo_event_t
**sorted_startstop_event_ptrs
;
132 } cairo_bo_event_queue_t
;
134 /* This structure extends #cairo_skip_list_t, which must come first. */
135 typedef struct _cairo_bo_sweep_line
{
136 cairo_skip_list_t active_edges
;
137 cairo_bo_edge_t
*head
;
138 cairo_bo_edge_t
*tail
;
140 } cairo_bo_sweep_line_t
;
144 _cairo_bo_point32_compare (cairo_bo_point32_t
const *a
,
145 cairo_bo_point32_t
const *b
)
147 int cmp
= a
->y
- b
->y
;
152 /* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
153 * slope a is respectively greater than, equal to, or less than the
156 * For each edge, consider the direction vector formed from:
162 * (dx, dy) = (bottom.x - top.x, bottom.y - top.y)
164 * We then define the slope of each edge as dx/dy, (which is the
165 * inverse of the slope typically used in math instruction). We never
166 * compute a slope directly as the value approaches infinity, but we
167 * can derive a slope comparison without division as follows, (where
168 * the ? represents our compare operator).
170 * 1. slope(a) ? slope(b)
171 * 2. adx/ady ? bdx/bdy
172 * 3. (adx * bdy) ? (bdx * ady)
174 * Note that from step 2 to step 3 there is no change needed in the
175 * sign of the result since both ady and bdy are guaranteed to be
176 * greater than or equal to 0.
178 * When using this slope comparison to sort edges, some care is needed
179 * when interpreting the results. Since the slope compare operates on
180 * distance vectors from top to bottom it gives a correct left to
181 * right sort for edges that have a common top point, (such as two
182 * edges with start events at the same location). On the other hand,
183 * the sense of the result will be exactly reversed for two edges that
184 * have a common stop point.
187 _slope_compare (cairo_bo_edge_t
*a
,
190 /* XXX: We're assuming here that dx and dy will still fit in 32
191 * bits. That's not true in general as there could be overflow. We
192 * should prevent that before the tessellation algorithm
195 int32_t adx
= a
->bottom
.x
- a
->top
.x
;
196 int32_t bdx
= b
->bottom
.x
- b
->top
.x
;
198 /* Since the dy's are all positive by construction we can fast
199 * path several common cases.
202 /* First check for vertical lines. */
208 /* Then where the two edges point in different directions wrt x. */
212 /* Finally we actually need to do the general comparison. */
214 int32_t ady
= a
->bottom
.y
- a
->top
.y
;
215 int32_t bdy
= b
->bottom
.y
- b
->top
.y
;
216 cairo_int64_t adx_bdy
= _cairo_int32x32_64_mul (adx
, bdy
);
217 cairo_int64_t bdx_ady
= _cairo_int32x32_64_mul (bdx
, ady
);
219 return _cairo_int64_cmp (adx_bdy
, bdx_ady
);
224 * We need to compare the x-coordinates of a pair of lines for a particular y,
225 * without loss of precision.
227 * The x-coordinate along an edge for a given y is:
228 * X = A_x + (Y - A_y) * A_dx / A_dy
230 * So the inequality we wish to test is:
231 * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
232 * where ∘ is our inequality operator.
234 * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
235 * all positive, so we can rearrange it thus without causing a sign change:
236 * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
237 * - (Y - A_y) * A_dx * B_dy
239 * Given the assumption that all the deltas fit within 32 bits, we can compute
240 * this comparison directly using 128 bit arithmetic. For certain, but common,
241 * input we can reduce this down to a single 32 bit compare by inspecting the
244 * (And put the burden of the work on developing fast 128 bit ops, which are
245 * required throughout the tessellator.)
247 * See the similar discussion for _slope_compare().
250 edges_compare_x_for_y_general (const cairo_bo_edge_t
*a
,
251 const cairo_bo_edge_t
*b
,
254 /* XXX: We're assuming here that dx and dy will still fit in 32
255 * bits. That's not true in general as there could be overflow. We
256 * should prevent that before the tessellation algorithm
266 HAVE_DX_ADX
= HAVE_DX
| HAVE_ADX
,
268 HAVE_DX_BDX
= HAVE_DX
| HAVE_BDX
,
269 HAVE_ADX_BDX
= HAVE_ADX
| HAVE_BDX
,
270 HAVE_ALL
= HAVE_DX
| HAVE_ADX
| HAVE_BDX
271 } have_dx_adx_bdx
= HAVE_ALL
;
273 ady
= a
->bottom
.y
- a
->top
.y
;
274 adx
= a
->bottom
.x
- a
->top
.x
;
276 have_dx_adx_bdx
&= ~HAVE_ADX
;
278 bdy
= b
->bottom
.y
- b
->top
.y
;
279 bdx
= b
->bottom
.x
- b
->top
.x
;
281 have_dx_adx_bdx
&= ~HAVE_BDX
;
283 dx
= a
->top
.x
- b
->top
.x
;
285 have_dx_adx_bdx
&= ~HAVE_DX
;
287 #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
288 #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->top.y)
289 #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->top.y)
290 switch (have_dx_adx_bdx
) {
295 /* A_dy * B_dy * (A_x - B_x) ∘ 0 */
296 return dx
; /* ady * bdy is positive definite */
298 /* 0 ∘ - (Y - A_y) * A_dx * B_dy */
299 return adx
; /* bdy * (y - a->top.y) is positive definite */
301 /* 0 ∘ (Y - B_y) * B_dx * A_dy */
302 return -bdx
; /* ady * (y - b->top.y) is positive definite */
304 /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
305 if ((adx
^ bdx
) < 0) {
307 } else if (a
->top
.y
== b
->top
.y
) { /* common origin */
308 cairo_int64_t adx_bdy
, bdx_ady
;
310 /* ∴ A_dx * B_dy ∘ B_dx * A_dy */
312 adx_bdy
= _cairo_int32x32_64_mul (adx
, bdy
);
313 bdx_ady
= _cairo_int32x32_64_mul (bdx
, ady
);
315 return _cairo_int64_cmp (adx_bdy
, bdx_ady
);
317 return _cairo_int128_cmp (A
, B
);
319 /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
320 if ((-adx
^ dx
) < 0) {
323 cairo_int64_t ady_dx
, dy_adx
;
325 ady_dx
= _cairo_int32x32_64_mul (ady
, dx
);
326 dy_adx
= _cairo_int32x32_64_mul (a
->top
.y
- y
, adx
);
328 return _cairo_int64_cmp (ady_dx
, dy_adx
);
331 /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
332 if ((bdx
^ dx
) < 0) {
335 cairo_int64_t bdy_dx
, dy_bdx
;
337 bdy_dx
= _cairo_int32x32_64_mul (bdy
, dx
);
338 dy_bdx
= _cairo_int32x32_64_mul (y
- b
->top
.y
, bdx
);
340 return _cairo_int64_cmp (bdy_dx
, dy_bdx
);
343 return _cairo_int128_cmp (L
, _cairo_int128_sub (B
, A
));
351 * We need to compare the x-coordinate of a line for a particular y wrt to a
352 * given x, without loss of precision.
354 * The x-coordinate along an edge for a given y is:
355 * X = A_x + (Y - A_y) * A_dx / A_dy
357 * So the inequality we wish to test is:
358 * A_x + (Y - A_y) * A_dx / A_dy ∘ X
359 * where ∘ is our inequality operator.
361 * By construction, we know that A_dy (and (Y - A_y)) are
362 * all positive, so we can rearrange it thus without causing a sign change:
363 * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
365 * Given the assumption that all the deltas fit within 32 bits, we can compute
366 * this comparison directly using 64 bit arithmetic.
368 * See the similar discussion for _slope_compare() and
369 * edges_compare_x_for_y_general().
372 edge_compare_for_y_against_x (const cairo_bo_edge_t
*a
,
380 adx
= a
->bottom
.x
- a
->top
.x
;
389 ady
= a
->bottom
.y
- a
->top
.y
;
391 L
= _cairo_int32x32_64_mul (dy
, adx
);
392 R
= _cairo_int32x32_64_mul (dx
, ady
);
394 return _cairo_int64_cmp (L
, R
);
398 edges_compare_x_for_y (const cairo_bo_edge_t
*a
,
399 const cairo_bo_edge_t
*b
,
402 /* If the sweep-line is currently on an end-point of a line,
403 * then we know its precise x value (and considering that we often need to
404 * compare events at end-points, this happens frequently enough to warrant
411 HAVE_BOTH
= HAVE_AX
| HAVE_BX
412 } have_ax_bx
= HAVE_BOTH
;
417 else if (y
== a
->bottom
.y
)
420 have_ax_bx
&= ~HAVE_AX
;
424 else if (y
== b
->bottom
.y
)
427 have_ax_bx
&= ~HAVE_BX
;
429 switch (have_ax_bx
) {
432 return edges_compare_x_for_y_general (a
, b
, y
);
434 return -edge_compare_for_y_against_x (b
, y
, ax
);
436 return edge_compare_for_y_against_x (a
, y
, bx
);
443 _cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t
*sweep_line
,
452 /* don't bother solving for abscissa if the edges' bounding boxes
453 * can be used to order them. */
457 if (a
->middle
.x
< a
->bottom
.x
) {
464 if (b
->middle
.x
< b
->bottom
.x
) {
471 if (amax
< bmin
) return -1;
472 if (amin
> bmax
) return +1;
475 cmp
= edges_compare_x_for_y (a
, b
, sweep_line
->current_y
);
479 /* The two edges intersect exactly at y, so fall back on slope
480 * comparison. We know that this compare_edges function will be
481 * called only when starting a new edge, (not when stopping an
482 * edge), so we don't have to worry about conditionally inverting
483 * the sense of _slope_compare. */
484 cmp
= _slope_compare (a
, b
);
488 /* We've got two collinear edges now. */
490 /* Since we're dealing with start events, prefer comparing top
491 * edges before bottom edges. */
492 cmp
= _cairo_bo_point32_compare (&a
->top
, &b
->top
);
496 cmp
= _cairo_bo_point32_compare (&a
->bottom
, &b
->bottom
);
500 /* Finally, we've got two identical edges. Let's finally
501 * discriminate by a simple pointer comparison, (which works only
502 * because we "know" the edges are all in a single array and don't
511 _sweep_line_elt_compare (void *list
,
515 cairo_bo_sweep_line_t
*sweep_line
= list
;
516 sweep_line_elt_t
*edge_elt_a
= a
;
517 sweep_line_elt_t
*edge_elt_b
= b
;
519 return _cairo_bo_sweep_line_compare_edges (sweep_line
,
525 cairo_bo_event_compare (cairo_bo_event_t
const *a
,
526 cairo_bo_event_t
const *b
)
530 /* The major motion of the sweep line is vertical (top-to-bottom),
531 * and the minor motion is horizontal (left-to-right), dues to the
532 * infinitesimal tilt rule.
534 * Our point comparison function respects these rules.
536 cmp
= _cairo_bo_point32_compare (&a
->point
, &b
->point
);
540 /* The events share a common point, so further discrimination is
541 * determined by the event type. Due to the infinitesimal
542 * shortening rule, stop events come first, then intersection
543 * events, then start events.
545 if (a
->type
!= b
->type
) {
546 if (a
->type
== CAIRO_BO_EVENT_TYPE_STOP
)
548 if (a
->type
== CAIRO_BO_EVENT_TYPE_START
)
551 if (b
->type
== CAIRO_BO_EVENT_TYPE_STOP
)
553 if (b
->type
== CAIRO_BO_EVENT_TYPE_START
)
557 /* At this stage we are looking at two events of the same type at
558 * the same point. The final sort key is a slope comparison. We
559 * need a different sense for start and stop events based on the
562 * Note: Fortunately, we get to ignore errors in the relative
563 * ordering of intersection events. This means we don't even have
564 * to look at e2 here, nor worry about which sense of the slope
565 * comparison test is used for intersection events.
567 cmp
= _slope_compare (a
->e1
, b
->e1
);
569 if (a
->type
== CAIRO_BO_EVENT_TYPE_START
)
575 /* Next look at the opposite point. This leaves ambiguities only
576 * for identical edges. */
577 if (a
->type
== CAIRO_BO_EVENT_TYPE_START
) {
578 cmp
= _cairo_bo_point32_compare (&b
->e1
->bottom
,
583 else if (a
->type
== CAIRO_BO_EVENT_TYPE_STOP
) {
584 cmp
= _cairo_bo_point32_compare (&a
->e1
->top
,
589 else { /* CAIRO_BO_EVENT_TYPE_INTERSECT */
590 /* For two intersection events at the identical point, we
591 * don't care what order they sort in, but we do care that we
592 * have a stable sort. In particular intersections between
593 * different pairs of edges must never return 0. */
594 cmp
= _cairo_bo_point32_compare (&a
->e2
->top
, &b
->e2
->top
);
597 cmp
= _cairo_bo_point32_compare (&a
->e2
->bottom
, &b
->e2
->bottom
);
600 cmp
= _cairo_bo_point32_compare (&a
->e1
->top
, &b
->e1
->top
);
603 cmp
= _cairo_bo_point32_compare (&a
->e1
->bottom
, &b
->e1
->bottom
);
608 /* Discrimination based on the edge pointers. */
621 cairo_bo_event_compare_abstract (void *list
,
625 cairo_bo_event_t
*event_a
= a
;
626 cairo_bo_event_t
*event_b
= b
;
628 return cairo_bo_event_compare (event_a
, event_b
);
632 cairo_bo_event_compare_pointers (const cairo_bo_event_t
*a
,
633 const cairo_bo_event_t
*b
)
639 cmp
= cairo_bo_event_compare (a
, b
);
646 static inline cairo_int64_t
655 /* det = a * d - b * c */
656 ad
= _cairo_int32x32_64_mul (a
, d
);
657 bc
= _cairo_int32x32_64_mul (b
, c
);
659 return _cairo_int64_sub (ad
, bc
);
662 static inline cairo_int128_t
663 det64x32_128 (cairo_int64_t a
,
671 /* det = a * d - b * c */
672 ad
= _cairo_int64x32_128_mul (a
, d
);
673 bc
= _cairo_int64x32_128_mul (c
, b
);
675 return _cairo_int128_sub (ad
, bc
);
678 /* Compute the intersection of two lines as defined by two edges. The
679 * result is provided as a coordinate pair of 128-bit integers.
681 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
682 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
684 static cairo_bo_status_t
685 intersect_lines (cairo_bo_edge_t
*a
,
687 cairo_bo_intersect_point_t
*intersection
)
689 cairo_int64_t a_det
, b_det
;
691 /* XXX: We're assuming here that dx and dy will still fit in 32
692 * bits. That's not true in general as there could be overflow. We
693 * should prevent that before the tessellation algorithm begins.
694 * What we're doing to mitigate this is to perform clamping in
695 * cairo_bo_tessellate_polygon().
697 int32_t dx1
= a
->top
.x
- a
->bottom
.x
;
698 int32_t dy1
= a
->top
.y
- a
->bottom
.y
;
700 int32_t dx2
= b
->top
.x
- b
->bottom
.x
;
701 int32_t dy2
= b
->top
.y
- b
->bottom
.y
;
703 cairo_int64_t den_det
;
707 den_det
= det32_64 (dx1
, dy1
, dx2
, dy2
);
708 if (_cairo_int64_is_zero (den_det
))
709 return CAIRO_BO_STATUS_PARALLEL
;
711 /* Q: Can we determine that the lines do not intersect (within range)
712 * much more cheaply than computing the intersection point i.e. by
713 * avoiding the division?
715 * X = ax + t * adx = bx + s * bdx;
716 * Y = ay + t * ady = by + s * bdy;
717 * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
720 * Therefore we can reject any intersection (under the criteria for
721 * valid intersection events) if:
722 * L^R < 0 => t < 0, or
725 * (where top/bottom must at least extend to the line endpoints).
727 * A similar substitution can be performed for s, yielding:
728 * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
730 R
= det32_64 (dx2
, dy2
, b
->top
.x
- a
->top
.x
, b
->top
.y
- a
->top
.y
);
731 if (_cairo_int64_is_zero (R
))
732 return CAIRO_BO_STATUS_NO_INTERSECTION
;
733 if (_cairo_int64_negative (den_det
) ^ _cairo_int64_negative (R
))
734 return CAIRO_BO_STATUS_NO_INTERSECTION
;
735 if (_cairo_int64_negative (den_det
)) {
736 if (_cairo_int64_ge (den_det
, R
))
737 return CAIRO_BO_STATUS_NO_INTERSECTION
;
739 if (_cairo_int64_le (den_det
, R
))
740 return CAIRO_BO_STATUS_NO_INTERSECTION
;
743 R
= det32_64 (dy1
, dx1
, a
->top
.y
- b
->top
.y
, a
->top
.x
- b
->top
.x
);
744 if (_cairo_int64_is_zero (R
))
745 return CAIRO_BO_STATUS_NO_INTERSECTION
;
746 if (_cairo_int64_negative (den_det
) ^ _cairo_int64_negative (R
))
747 return CAIRO_BO_STATUS_NO_INTERSECTION
;
748 if (_cairo_int64_negative (den_det
)) {
749 if (_cairo_int64_ge (den_det
, R
))
750 return CAIRO_BO_STATUS_NO_INTERSECTION
;
752 if (_cairo_int64_le (den_det
, R
))
753 return CAIRO_BO_STATUS_NO_INTERSECTION
;
756 /* We now know that the two lines should intersect within range. */
758 a_det
= det32_64 (a
->top
.x
, a
->top
.y
,
759 a
->bottom
.x
, a
->bottom
.y
);
760 b_det
= det32_64 (b
->top
.x
, b
->top
.y
,
761 b
->bottom
.x
, b
->bottom
.y
);
763 /* x = det (a_det, dx1, b_det, dx2) / den_det */
764 qr
= _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det
, dx1
,
767 if (_cairo_int64_eq (qr
.rem
, den_det
))
768 return CAIRO_BO_STATUS_NO_INTERSECTION
;
769 intersection
->x
.ordinate
= _cairo_int64_to_int32 (qr
.quo
);
770 intersection
->x
.exactness
= _cairo_int64_is_zero (qr
.rem
) ? EXACT
: INEXACT
;
772 /* y = det (a_det, dy1, b_det, dy2) / den_det */
773 qr
= _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det
, dy1
,
776 if (_cairo_int64_eq (qr
.rem
, den_det
))
777 return CAIRO_BO_STATUS_NO_INTERSECTION
;
778 intersection
->y
.ordinate
= _cairo_int64_to_int32 (qr
.quo
);
779 intersection
->y
.exactness
= _cairo_int64_is_zero (qr
.rem
) ? EXACT
: INEXACT
;
781 return CAIRO_BO_STATUS_INTERSECTION
;
785 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a
,
788 /* First compare the quotient */
793 /* With quotient identical, if remainder is 0 then compare equal */
794 /* Otherwise, the non-zero remainder makes a > b */
795 return INEXACT
== a
.exactness
;
798 /* Does the given edge contain the given point. The point must already
799 * be known to be contained within the line determined by the edge,
800 * (most likely the point results from an intersection of this edge
803 * If we had exact arithmetic, then this function would simply be a
804 * matter of examining whether the y value of the point lies within
805 * the range of y values of the edge. But since intersection points
806 * are not exact due to being rounded to the nearest integer within
807 * the available precision, we must also examine the x value of the
810 * The definition of "contains" here is that the given intersection
811 * point will be seen by the sweep line after the start event for the
812 * given edge and before the stop event for the edge. See the comments
813 * in the implementation for more details.
816 _cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t
*edge
,
817 cairo_bo_intersect_point_t
*point
)
819 int cmp_top
, cmp_bottom
;
821 /* XXX: When running the actual algorithm, we don't actually need to
822 * compare against edge->top at all here, since any intersection above
823 * top is eliminated early via a slope comparison. We're leaving these
824 * here for now only for the sake of the quadratic-time intersection
825 * finder which needs them.
828 cmp_top
= _cairo_bo_intersect_ordinate_32_compare (point
->y
, edge
->top
.y
);
829 cmp_bottom
= _cairo_bo_intersect_ordinate_32_compare (point
->y
, edge
->bottom
.y
);
831 if (cmp_top
< 0 || cmp_bottom
> 0)
836 if (cmp_top
> 0 && cmp_bottom
< 0)
841 /* At this stage, the point lies on the same y value as either
842 * edge->top or edge->bottom, so we have to examine the x value in
843 * order to properly determine containment. */
845 /* If the y value of the point is the same as the y value of the
846 * top of the edge, then the x value of the point must be greater
847 * to be considered as inside the edge. Similarly, if the y value
848 * of the point is the same as the y value of the bottom of the
849 * edge, then the x value of the point must be less to be
850 * considered as inside. */
853 return (_cairo_bo_intersect_ordinate_32_compare (point
->x
, edge
->top
.x
) > 0);
854 else /* cmp_bottom == 0 */
855 return (_cairo_bo_intersect_ordinate_32_compare (point
->x
, edge
->bottom
.x
) < 0);
858 /* Compute the intersection of two edges. The result is provided as a
859 * coordinate pair of 128-bit integers.
861 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
862 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
863 * intersection of the lines defined by the edges occurs outside of
864 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
865 * are exactly parallel.
867 * Note that when determining if a candidate intersection is "inside"
868 * an edge, we consider both the infinitesimal shortening and the
869 * infinitesimal tilt rules described by John Hobby. Specifically, if
870 * the intersection is exactly the same as an edge point, it is
871 * effectively outside (no intersection is returned). Also, if the
872 * intersection point has the same
874 static cairo_bo_status_t
875 _cairo_bo_edge_intersect (cairo_bo_edge_t
*a
,
877 cairo_bo_point32_t
*intersection
)
879 cairo_bo_status_t status
;
880 cairo_bo_intersect_point_t quorem
;
882 status
= intersect_lines (a
, b
, &quorem
);
886 if (! _cairo_bo_edge_contains_intersect_point (a
, &quorem
))
887 return CAIRO_BO_STATUS_NO_INTERSECTION
;
889 if (! _cairo_bo_edge_contains_intersect_point (b
, &quorem
))
890 return CAIRO_BO_STATUS_NO_INTERSECTION
;
892 /* Now that we've correctly compared the intersection point and
893 * determined that it lies within the edge, then we know that we
894 * no longer need any more bits of storage for the intersection
895 * than we do for our edge coordinates. We also no longer need the
896 * remainder from the division. */
897 intersection
->x
= quorem
.x
.ordinate
;
898 intersection
->y
= quorem
.y
.ordinate
;
900 return CAIRO_BO_STATUS_INTERSECTION
;
904 _cairo_bo_event_init (cairo_bo_event_t
*event
,
905 cairo_bo_event_type_t type
,
908 cairo_bo_point32_t point
)
913 event
->point
= point
;
916 static cairo_status_t
917 _cairo_bo_event_queue_insert (cairo_bo_event_queue_t
*queue
,
918 cairo_bo_event_t
*event
)
920 cairo_status_t status
= CAIRO_STATUS_SUCCESS
;
921 /* Don't insert if there's already an equivalent intersection event in the queue. */
922 if (_cairo_skip_list_insert (&queue
->intersection_queue
, event
,
923 event
->type
== CAIRO_BO_EVENT_TYPE_INTERSECTION
) == NULL
)
924 status
= _cairo_error (CAIRO_STATUS_NO_MEMORY
);
929 _cairo_bo_event_queue_delete (cairo_bo_event_queue_t
*queue
,
930 cairo_bo_event_t
*event
)
932 if (CAIRO_BO_EVENT_TYPE_INTERSECTION
== event
->type
)
933 _cairo_skip_list_delete_given ( &queue
->intersection_queue
, &event
->elt
);
936 static cairo_bo_event_t
*
937 _cairo_bo_event_dequeue (cairo_bo_event_queue_t
*event_queue
)
939 skip_elt_t
*elt
= event_queue
->intersection_queue
.chains
[0];
940 cairo_bo_event_t
*intersection
= elt
? SKIP_ELT_TO_EVENT (elt
) : NULL
;
941 cairo_bo_event_t
*startstop
;
943 startstop
= *event_queue
->sorted_startstop_event_ptrs
;
944 if (startstop
== NULL
)
947 if (intersection
== NULL
||
948 cairo_bo_event_compare (startstop
, intersection
) <= 0)
950 event_queue
->sorted_startstop_event_ptrs
++;
957 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort
,
959 cairo_bo_event_compare_pointers
)
961 static cairo_status_t
962 _cairo_bo_event_queue_init (cairo_bo_event_queue_t
*event_queue
,
963 cairo_bo_edge_t
*edges
,
967 cairo_bo_event_t
*events
, **sorted_event_ptrs
;
968 unsigned num_events
= 2*num_edges
;
970 /* The skip_elt_t field of a cairo_bo_event_t isn't used for start
971 * or stop events, so this allocation is safe. XXX: make the
972 * event type a union so it doesn't always contain the skip
974 events
= _cairo_malloc_ab_plus_c (num_events
,
975 sizeof (cairo_bo_event_t
) +
976 sizeof (cairo_bo_event_t
*),
977 sizeof (cairo_bo_event_t
*));
978 if (unlikely (events
== NULL
))
979 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
981 sorted_event_ptrs
= (cairo_bo_event_t
**) (events
+ num_events
);
982 event_queue
->startstop_events
= events
;
983 event_queue
->sorted_startstop_event_ptrs
= sorted_event_ptrs
;
985 for (i
= 0; i
< num_edges
; i
++) {
986 sorted_event_ptrs
[i
] = &events
[2*i
];
987 sorted_event_ptrs
[i
+num_edges
] = &events
[2*i
+1];
989 /* Initialize "middle" to top */
990 edges
[i
].middle
= edges
[i
].top
;
992 _cairo_bo_event_init (&events
[2*i
],
993 CAIRO_BO_EVENT_TYPE_START
,
997 _cairo_bo_event_init (&events
[2*i
+1],
998 CAIRO_BO_EVENT_TYPE_STOP
,
1003 _cairo_bo_event_queue_sort (sorted_event_ptrs
, num_events
);
1004 event_queue
->sorted_startstop_event_ptrs
[num_events
] = NULL
;
1006 _cairo_skip_list_init (&event_queue
->intersection_queue
,
1007 cairo_bo_event_compare_abstract
,
1008 sizeof (cairo_bo_event_t
));
1010 return CAIRO_STATUS_SUCCESS
;
1014 _cairo_bo_event_queue_fini (cairo_bo_event_queue_t
*event_queue
)
1016 _cairo_skip_list_fini (&event_queue
->intersection_queue
);
1017 if (event_queue
->startstop_events
)
1018 free (event_queue
->startstop_events
);
1021 static cairo_status_t
1022 _cairo_bo_event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t
*event_queue
,
1023 cairo_bo_edge_t
*left
,
1024 cairo_bo_edge_t
*right
)
1026 cairo_bo_status_t status
;
1027 cairo_bo_point32_t intersection
;
1028 cairo_bo_event_t event
;
1030 if (left
== NULL
|| right
== NULL
)
1031 return CAIRO_STATUS_SUCCESS
;
1033 /* The names "left" and "right" here are correct descriptions of
1034 * the order of the two edges within the active edge list. So if a
1035 * slope comparison also puts left less than right, then we know
1036 * that the intersection of these two segments has oalready
1037 * occurred before the current sweep line position. */
1038 if (_slope_compare (left
, right
) < 0)
1039 return CAIRO_STATUS_SUCCESS
;
1041 status
= _cairo_bo_edge_intersect (left
, right
, &intersection
);
1042 if (status
== CAIRO_BO_STATUS_PARALLEL
||
1043 status
== CAIRO_BO_STATUS_NO_INTERSECTION
)
1045 return CAIRO_STATUS_SUCCESS
;
1048 _cairo_bo_event_init (&event
,
1049 CAIRO_BO_EVENT_TYPE_INTERSECTION
,
1053 return _cairo_bo_event_queue_insert (event_queue
, &event
);
1057 _cairo_bo_sweep_line_init (cairo_bo_sweep_line_t
*sweep_line
)
1059 _cairo_skip_list_init (&sweep_line
->active_edges
,
1060 _sweep_line_elt_compare
,
1061 sizeof (sweep_line_elt_t
));
1063 sweep_line
->head
= NULL
;
1064 sweep_line
->tail
= NULL
;
1065 sweep_line
->current_y
= 0;
1069 _cairo_bo_sweep_line_fini (cairo_bo_sweep_line_t
*sweep_line
)
1071 _cairo_skip_list_fini (&sweep_line
->active_edges
);
1074 static cairo_status_t
1075 _cairo_bo_sweep_line_insert (cairo_bo_sweep_line_t
*sweep_line
,
1076 cairo_bo_edge_t
*edge
)
1078 skip_elt_t
*next_elt
;
1079 sweep_line_elt_t
*sweep_line_elt
;
1080 cairo_bo_edge_t
**prev_of_next
, **next_of_prev
;
1082 sweep_line_elt
= _cairo_skip_list_insert (&sweep_line
->active_edges
, &edge
,
1083 1 /* unique inserts*/);
1084 if (unlikely (sweep_line_elt
== NULL
))
1085 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1087 next_elt
= sweep_line_elt
->elt
.next
[0];
1089 prev_of_next
= & (SKIP_ELT_TO_EDGE (next_elt
)->prev
);
1091 prev_of_next
= &sweep_line
->tail
;
1094 next_of_prev
= &(*prev_of_next
)->next
;
1096 next_of_prev
= &sweep_line
->head
;
1098 edge
->prev
= *prev_of_next
;
1099 edge
->next
= *next_of_prev
;
1100 *prev_of_next
= edge
;
1101 *next_of_prev
= edge
;
1103 edge
->sweep_line_elt
= sweep_line_elt
;
1105 return CAIRO_STATUS_SUCCESS
;
1109 _cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t
*sweep_line
,
1110 cairo_bo_edge_t
*edge
)
1112 cairo_bo_edge_t
**left_next
, **right_prev
;
1114 _cairo_skip_list_delete_given (&sweep_line
->active_edges
,
1115 &edge
->sweep_line_elt
->elt
);
1117 left_next
= &sweep_line
->head
;
1119 left_next
= &edge
->prev
->next
;
1121 right_prev
= &sweep_line
->tail
;
1123 right_prev
= &edge
->next
->prev
;
1125 *left_next
= edge
->next
;
1126 *right_prev
= edge
->prev
;
1130 _cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t
*sweep_line
,
1131 cairo_bo_edge_t
*left
,
1132 cairo_bo_edge_t
*right
)
1134 sweep_line_elt_t
*left_elt
, *right_elt
;
1135 cairo_bo_edge_t
**before_left
, **after_right
;
1137 /* Within the skip list we can do the swap simply by swapping the
1138 * pointers to the edge elements and leaving all of the skip list
1139 * elements and pointers unchanged. */
1140 left_elt
= left
->sweep_line_elt
;
1141 right_elt
= SKIP_ELT_TO_EDGE_ELT (left_elt
->elt
.next
[0]);
1143 left_elt
->edge
= right
;
1144 right
->sweep_line_elt
= left_elt
;
1146 right_elt
->edge
= left
;
1147 left
->sweep_line_elt
= right_elt
;
1149 /* Within the doubly-linked list of edges, there's a bit more
1150 * bookkeeping involved with the swap. */
1151 before_left
= &sweep_line
->head
;
1153 before_left
= &left
->prev
->next
;
1154 *before_left
= right
;
1156 after_right
= &sweep_line
->tail
;
1158 after_right
= &right
->next
->prev
;
1159 *after_right
= left
;
1161 left
->next
= right
->next
;
1164 right
->prev
= left
->prev
;
1168 #if DEBUG_PRINT_STATE
1170 _cairo_bo_edge_print (cairo_bo_edge_t
*edge
)
1172 printf ("(0x%x, 0x%x)-(0x%x, 0x%x)",
1173 edge
->top
.x
, edge
->top
.y
,
1174 edge
->bottom
.x
, edge
->bottom
.y
);
1178 _cairo_bo_event_print (cairo_bo_event_t
*event
)
1180 switch (event
->type
) {
1181 case CAIRO_BO_EVENT_TYPE_START
:
1184 case CAIRO_BO_EVENT_TYPE_STOP
:
1187 case CAIRO_BO_EVENT_TYPE_INTERSECTION
:
1188 printf ("Intersection: ");
1191 printf ("(%d, %d)\t", event
->point
.x
, event
->point
.y
);
1192 _cairo_bo_edge_print (event
->e1
);
1193 if (event
->type
== CAIRO_BO_EVENT_TYPE_INTERSECTION
) {
1195 _cairo_bo_edge_print (event
->e2
);
1201 _cairo_bo_event_queue_print (cairo_bo_event_queue_t
*event_queue
)
1204 /* XXX: fixme to print the start/stop array too. */
1205 cairo_skip_list_t
*queue
= &event_queue
->intersection_queue
;
1206 cairo_bo_event_t
*event
;
1208 printf ("Event queue:\n");
1210 for (elt
= queue
->chains
[0];
1214 event
= SKIP_ELT_TO_EVENT (elt
);
1215 _cairo_bo_event_print (event
);
1220 _cairo_bo_sweep_line_print (cairo_bo_sweep_line_t
*sweep_line
)
1222 cairo_bool_t first
= TRUE
;
1224 cairo_bo_edge_t
*edge
;
1226 printf ("Sweep line (reversed): ");
1228 for (edge
= sweep_line
->tail
;
1234 _cairo_bo_edge_print (edge
);
1240 printf ("Sweep line from edge list: ");
1242 for (edge
= sweep_line
->head
;
1248 _cairo_bo_edge_print (edge
);
1253 printf ("Sweep line from skip list: ");
1255 for (elt
= sweep_line
->active_edges
.chains
[0];
1261 _cairo_bo_edge_print (SKIP_ELT_TO_EDGE (elt
));
1268 print_state (const char *msg
,
1269 cairo_bo_event_queue_t
*event_queue
,
1270 cairo_bo_sweep_line_t
*sweep_line
)
1272 printf ("%s\n", msg
);
1273 _cairo_bo_event_queue_print (event_queue
);
1274 _cairo_bo_sweep_line_print (sweep_line
);
1279 /* Adds the trapezoid, if any, of the left edge to the #cairo_traps_t
1281 static cairo_status_t
1282 _cairo_bo_edge_end_trap (cairo_bo_edge_t
*left
,
1284 cairo_bo_traps_t
*bo_traps
)
1286 cairo_fixed_t fixed_top
, fixed_bot
;
1287 cairo_bo_trap_t
*trap
= left
->deferred_trap
;
1288 cairo_bo_edge_t
*right
;
1291 return CAIRO_STATUS_SUCCESS
;
1293 /* If the right edge of the trapezoid stopped earlier than the
1294 * left edge, then cut the trapezoid bottom early. */
1295 right
= trap
->right
;
1296 if (right
->bottom
.y
< bot
)
1297 bot
= right
->bottom
.y
;
1299 fixed_top
= trap
->top
;
1302 /* Only emit trapezoids with positive height. */
1303 if (fixed_top
< fixed_bot
) {
1304 cairo_line_t left_line
;
1305 cairo_line_t right_line
;
1306 cairo_fixed_t xmin
= bo_traps
->xmin
;
1307 cairo_fixed_t ymin
= bo_traps
->ymin
;
1311 left_line
.p1
.x
= left
->top
.x
+ xmin
;
1312 left_line
.p1
.y
= left
->top
.y
+ ymin
;
1313 right_line
.p1
.x
= right
->top
.x
+ xmin
;
1314 right_line
.p1
.y
= right
->top
.y
+ ymin
;
1316 left_line
.p2
.x
= left
->bottom
.x
+ xmin
;
1317 left_line
.p2
.y
= left
->bottom
.y
+ ymin
;
1318 right_line
.p2
.x
= right
->bottom
.x
+ xmin
;
1319 right_line
.p2
.y
= right
->bottom
.y
+ ymin
;
1321 /* Avoid emitting the trapezoid if it is obviously degenerate.
1322 * TODO: need a real collinearity test here for the cases
1323 * where the trapezoid is degenerate, yet the top and bottom
1324 * coordinates aren't equal. */
1325 if (left_line
.p1
.x
!= right_line
.p1
.x
||
1326 left_line
.p1
.y
!= right_line
.p1
.y
||
1327 left_line
.p2
.x
!= right_line
.p2
.x
||
1328 left_line
.p2
.y
!= right_line
.p2
.y
)
1330 _cairo_traps_add_trap (bo_traps
->traps
,
1331 fixed_top
, fixed_bot
,
1332 &left_line
, &right_line
);
1334 #if DEBUG_PRINT_STATE
1335 printf ("Deferred trap: left=(%08x, %08x)-(%08x,%08x) "
1336 "right=(%08x,%08x)-(%08x,%08x) top=%08x, bot=%08x\n",
1337 left
->top
.x
, left
->top
.y
, left
->bottom
.x
, left
->bottom
.y
,
1338 right
->top
.x
, right
->top
.y
, right
->bottom
.x
, right
->bottom
.y
,
1344 _cairo_freelist_free (&bo_traps
->freelist
, trap
);
1345 left
->deferred_trap
= NULL
;
1347 return _cairo_traps_status (bo_traps
->traps
);
1350 /* Start a new trapezoid at the given top y coordinate, whose edges
1351 * are `edge' and `edge->next'. If `edge' already has a trapezoid,
1352 * then either add it to the traps in `bo_traps', if the trapezoid's
1353 * right edge differs from `edge->next', or do nothing if the new
1354 * trapezoid would be a continuation of the existing one. */
1355 static cairo_status_t
1356 _cairo_bo_edge_start_or_continue_trap (cairo_bo_edge_t
*edge
,
1358 cairo_bo_traps_t
*bo_traps
)
1360 cairo_status_t status
;
1361 cairo_bo_trap_t
*trap
= edge
->deferred_trap
;
1364 if (trap
->right
== edge
->next
) return CAIRO_STATUS_SUCCESS
;
1365 status
= _cairo_bo_edge_end_trap (edge
, top
, bo_traps
);
1371 trap
= edge
->deferred_trap
= _cairo_freelist_alloc (&bo_traps
->freelist
);
1372 if (!edge
->deferred_trap
)
1373 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1375 trap
->right
= edge
->next
;
1378 return CAIRO_STATUS_SUCCESS
;
1382 _cairo_bo_traps_init (cairo_bo_traps_t
*bo_traps
,
1383 cairo_traps_t
*traps
,
1389 bo_traps
->traps
= traps
;
1390 _cairo_freelist_init (&bo_traps
->freelist
, sizeof(cairo_bo_trap_t
));
1391 bo_traps
->xmin
= xmin
;
1392 bo_traps
->ymin
= ymin
;
1393 bo_traps
->xmax
= xmax
;
1394 bo_traps
->ymax
= ymax
;
1398 _cairo_bo_traps_fini (cairo_bo_traps_t
*bo_traps
)
1400 _cairo_freelist_fini (&bo_traps
->freelist
);
1405 _cairo_bo_sweep_line_validate (cairo_bo_sweep_line_t
*sweep_line
)
1407 cairo_bo_edge_t
*edge
;
1410 /* March through both the skip list's singly-linked list and the
1411 * sweep line's own list through pointers in the edges themselves
1412 * and make sure they agree at every point. */
1414 for (edge
= sweep_line
->head
, elt
= sweep_line
->active_edges
.chains
[0];
1416 edge
= edge
->next
, elt
= elt
->next
[0])
1418 if (SKIP_ELT_TO_EDGE (elt
) != edge
) {
1419 fprintf (stderr
, "*** Error: Sweep line fails to validate: Inconsistent data in the two lists.\n");
1425 fprintf (stderr
, "*** Error: Sweep line fails to validate: One list ran out before the other.\n");
1432 static cairo_status_t
1433 _active_edges_to_traps (cairo_bo_edge_t
*head
,
1435 cairo_fill_rule_t fill_rule
,
1436 cairo_bo_traps_t
*bo_traps
)
1438 cairo_status_t status
;
1440 cairo_bo_edge_t
*edge
;
1442 for (edge
= head
; edge
; edge
= edge
->next
) {
1443 if (fill_rule
== CAIRO_FILL_RULE_WINDING
) {
1444 in_out
+= edge
->dir
;
1446 status
= _cairo_bo_edge_end_trap (edge
, top
, bo_traps
);
1453 if ((in_out
& 1) == 0) {
1454 status
= _cairo_bo_edge_end_trap (edge
, top
, bo_traps
);
1461 status
= _cairo_bo_edge_start_or_continue_trap (edge
, top
, bo_traps
);
1466 return CAIRO_STATUS_SUCCESS
;
1469 /* Execute a single pass of the Bentley-Ottmann algorithm on edges,
1470 * generating trapezoids according to the fill_rule and appending them
1472 static cairo_status_t
1473 _cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_edge_t
*edges
,
1475 cairo_fill_rule_t fill_rule
,
1476 cairo_traps_t
*traps
,
1481 int *num_intersections
)
1483 cairo_status_t status
;
1484 int intersection_count
= 0;
1485 cairo_bo_event_queue_t event_queue
;
1486 cairo_bo_sweep_line_t sweep_line
;
1487 cairo_bo_traps_t bo_traps
;
1488 cairo_bo_event_t
*event
, event_saved
;
1489 cairo_bo_edge_t
*edge
;
1490 cairo_bo_edge_t
*left
, *right
;
1491 cairo_bo_edge_t
*edge1
, *edge2
;
1494 return CAIRO_STATUS_SUCCESS
;
1496 status
= _cairo_bo_event_queue_init (&event_queue
, edges
, num_edges
);
1500 _cairo_bo_sweep_line_init (&sweep_line
);
1502 _cairo_bo_traps_init (&bo_traps
, traps
, xmin
, ymin
, xmax
, ymax
);
1504 #if DEBUG_PRINT_STATE
1505 print_state ("After initializing", &event_queue
, &sweep_line
);
1510 event
= _cairo_bo_event_dequeue (&event_queue
);
1514 if (event
->point
.y
!= sweep_line
.current_y
) {
1515 status
= _active_edges_to_traps (sweep_line
.head
,
1516 sweep_line
.current_y
,
1517 fill_rule
, &bo_traps
);
1521 sweep_line
.current_y
= event
->point
.y
;
1524 event_saved
= *event
;
1525 _cairo_bo_event_queue_delete (&event_queue
, event
);
1526 event
= &event_saved
;
1528 switch (event
->type
) {
1529 case CAIRO_BO_EVENT_TYPE_START
:
1532 status
= _cairo_bo_sweep_line_insert (&sweep_line
, edge
);
1535 /* Cache the insert position for use in pass 2.
1536 event->e2 = Sortlist::prev (sweep_line, edge);
1542 status
= _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue
, left
, edge
);
1546 status
= _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue
, edge
, right
);
1550 #if DEBUG_PRINT_STATE
1551 print_state ("After processing start", &event_queue
, &sweep_line
);
1555 case CAIRO_BO_EVENT_TYPE_STOP
:
1561 _cairo_bo_sweep_line_delete (&sweep_line
, edge
);
1563 status
= _cairo_bo_edge_end_trap (edge
, edge
->bottom
.y
, &bo_traps
);
1567 status
= _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue
, left
, right
);
1571 #if DEBUG_PRINT_STATE
1572 print_state ("After processing stop", &event_queue
, &sweep_line
);
1576 case CAIRO_BO_EVENT_TYPE_INTERSECTION
:
1580 /* skip this intersection if its edges are not adjacent */
1581 if (edge2
!= edge1
->next
)
1584 intersection_count
++;
1586 edge1
->middle
= event
->point
;
1587 edge2
->middle
= event
->point
;
1590 right
= edge2
->next
;
1592 _cairo_bo_sweep_line_swap (&sweep_line
, edge1
, edge2
);
1594 /* after the swap e2 is left of e1 */
1596 status
= _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue
,
1601 status
= _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue
,
1606 #if DEBUG_PRINT_STATE
1607 print_state ("After processing intersection", &event_queue
, &sweep_line
);
1612 _cairo_bo_sweep_line_validate (&sweep_line
);
1616 *num_intersections
= intersection_count
;
1618 for (edge
= sweep_line
.head
; edge
; edge
= edge
->next
) {
1619 cairo_status_t status2
= _cairo_bo_edge_end_trap (edge
,
1620 sweep_line
.current_y
,
1625 _cairo_bo_traps_fini (&bo_traps
);
1626 _cairo_bo_sweep_line_fini (&sweep_line
);
1627 _cairo_bo_event_queue_fini (&event_queue
);
1632 update_minmax(cairo_fixed_t
*inout_min
,
1633 cairo_fixed_t
*inout_max
,
1643 _cairo_bentley_ottmann_tessellate_polygon (cairo_traps_t
*traps
,
1644 const cairo_polygon_t
*polygon
,
1645 cairo_fill_rule_t fill_rule
)
1648 cairo_status_t status
;
1649 cairo_bo_edge_t stack_edges
[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_edge_t
)];
1650 cairo_bo_edge_t
*edges
;
1651 cairo_fixed_t xmin
= 0x7FFFFFFF;
1652 cairo_fixed_t ymin
= 0x7FFFFFFF;
1653 cairo_fixed_t xmax
= -0x80000000;
1654 cairo_fixed_t ymax
= -0x80000000;
1656 cairo_bool_t has_limits
;
1660 if (0 == polygon
->num_edges
)
1661 return CAIRO_STATUS_SUCCESS
;
1663 if (CAIRO_INJECT_FAULT ())
1664 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1666 has_limits
= _cairo_traps_get_limit (traps
, &limit
);
1668 edges
= stack_edges
;
1669 if (polygon
->num_edges
> ARRAY_LENGTH (stack_edges
)) {
1670 edges
= _cairo_malloc_ab (polygon
->num_edges
, sizeof (cairo_bo_edge_t
));
1671 if (unlikely (edges
== NULL
))
1672 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
1675 /* Figure out the bounding box of the input coordinates and
1676 * validate that we're not given invalid polygon edges. */
1677 for (i
= 0; i
< polygon
->num_edges
; i
++) {
1678 update_minmax (&xmin
, &xmax
, polygon
->edges
[i
].edge
.p1
.x
);
1679 update_minmax (&ymin
, &ymax
, polygon
->edges
[i
].edge
.p1
.y
);
1680 update_minmax (&xmin
, &xmax
, polygon
->edges
[i
].edge
.p2
.x
);
1681 update_minmax (&ymin
, &ymax
, polygon
->edges
[i
].edge
.p2
.y
);
1682 assert (polygon
->edges
[i
].edge
.p1
.y
<= polygon
->edges
[i
].edge
.p2
.y
&&
1683 "BUG: tessellator given upside down or horizontal edges");
1686 /* The tessellation functions currently assume that no line
1687 * segment extends more than 2^31-1 in either dimension. We
1688 * guarantee this by offsetting the internal coordinates to the
1689 * range [0,2^31-1], and clamping to 2^31-1 if a coordinate
1690 * exceeds the range (and yes, this generates an incorrect
1691 * result). First we have to clamp the bounding box itself. */
1692 /* XXX: Rather than changing the input values, a better approach
1693 * would be to detect out-of-bounds input and return a
1694 * CAIRO_STATUS_OVERFLOW value to the user. */
1695 if (xmax
- xmin
< 0)
1696 xmax
= xmin
+ 0x7FFFFFFF;
1697 if (ymax
- ymin
< 0)
1698 ymax
= ymin
+ 0x7FFFFFFF;
1700 for (i
= 0, num_bo_edges
= 0; i
< polygon
->num_edges
; i
++) {
1701 cairo_bo_edge_t
*edge
= &edges
[num_bo_edges
];
1702 cairo_point_t top
= polygon
->edges
[i
].edge
.p1
;
1703 cairo_point_t bot
= polygon
->edges
[i
].edge
.p2
;
1705 /* Discard the edge if it lies outside the limits of traps. */
1707 /* Strictly above or below the limits? */
1708 if (bot
.y
<= limit
.p1
.y
|| top
.y
>= limit
.p2
.y
)
1712 /* Offset coordinates into the non-negative range. */
1718 /* If the coordinates are still negative, then their extent is
1719 * overflowing 2^31-1. We're going to kludge it and clamp the
1720 * coordinates into the clamped bounding box. */
1721 if (top
.x
< 0) top
.x
= xmax
- xmin
;
1722 if (top
.y
< 0) top
.y
= ymax
- ymin
;
1723 if (bot
.x
< 0) bot
.x
= xmax
- xmin
;
1724 if (bot
.y
< 0) bot
.y
= ymax
- ymin
;
1726 if (top
.y
== bot
.y
) {
1727 /* Clamping might have produced horizontal edges. Ignore
1731 assert (top
.y
< bot
.y
&&
1732 "BUG: clamping the input range flipped the "
1733 "orientation of an edge");
1735 edge
->top
.x
= top
.x
;
1736 edge
->top
.y
= top
.y
;
1737 edge
->bottom
.x
= bot
.x
;
1738 edge
->bottom
.y
= bot
.y
;
1739 edge
->dir
= polygon
->edges
[i
].dir
;
1740 edge
->deferred_trap
= NULL
;
1743 edge
->sweep_line_elt
= NULL
;
1748 /* XXX: This would be the convenient place to throw in multiple
1749 * passes of the Bentley-Ottmann algorithm. It would merely
1750 * require storing the results of each pass into a temporary
1752 status
= _cairo_bentley_ottmann_tessellate_bo_edges (edges
, num_bo_edges
,
1754 xmin
, ymin
, xmax
, ymax
,
1757 if (edges
!= stack_edges
)
1765 edges_have_an_intersection_quadratic (cairo_bo_edge_t
*edges
,
1770 cairo_bo_edge_t
*a
, *b
;
1771 cairo_bo_point32_t intersection
;
1772 cairo_bo_status_t status
;
1774 /* We must not be given any upside-down edges. */
1775 for (i
= 0; i
< num_edges
; i
++) {
1776 assert (_cairo_bo_point32_compare (&edges
[i
].top
, &edges
[i
].bottom
) < 0);
1777 edges
[i
].top
.x
<<= CAIRO_BO_GUARD_BITS
;
1778 edges
[i
].top
.y
<<= CAIRO_BO_GUARD_BITS
;
1779 edges
[i
].bottom
.x
<<= CAIRO_BO_GUARD_BITS
;
1780 edges
[i
].bottom
.y
<<= CAIRO_BO_GUARD_BITS
;
1783 for (i
= 0; i
< num_edges
; i
++) {
1784 for (j
= 0; j
< num_edges
; j
++) {
1791 status
= _cairo_bo_edge_intersect (a
, b
, &intersection
);
1792 if (status
== CAIRO_BO_STATUS_PARALLEL
||
1793 status
== CAIRO_BO_STATUS_NO_INTERSECTION
)
1798 printf ("Found intersection (%d,%d) between (%d,%d)-(%d,%d) and (%d,%d)-(%d,%d)\n",
1802 a
->bottom
.x
, a
->bottom
.y
,
1804 b
->bottom
.x
, b
->bottom
.y
);
1812 #define TEST_MAX_EDGES 10
1814 typedef struct test
{
1816 const char *description
;
1818 cairo_bo_edge_t edges
[TEST_MAX_EDGES
];
1825 "3 edges all intersecting very close to each other",
1828 { { 4, 2}, {0, 0}, { 9, 9}, NULL
, NULL
},
1829 { { 7, 2}, {0, 0}, { 2, 3}, NULL
, NULL
},
1830 { { 5, 2}, {0, 0}, { 1, 7}, NULL
, NULL
}
1834 "inconsistent data",
1835 "Derived from random testing---was leading to skip list and edge list disagreeing.",
1838 { { 2, 3}, {0, 0}, { 8, 9}, NULL
, NULL
},
1839 { { 2, 3}, {0, 0}, { 6, 7}, NULL
, NULL
}
1844 "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?",
1847 { { 6, 2}, {0, 0}, { 6, 5}, NULL
, NULL
},
1848 { { 3, 5}, {0, 0}, { 5, 6}, NULL
, NULL
},
1849 { { 9, 2}, {0, 0}, { 5, 6}, NULL
, NULL
},
1853 "minimal-intersection",
1854 "Intersection of a two from among the smallest possible edges.",
1857 { { 0, 0}, {0, 0}, { 1, 1}, NULL
, NULL
},
1858 { { 1, 0}, {0, 0}, { 0, 1}, NULL
, NULL
}
1863 "A simple intersection of two edges at an integer (2,2).",
1866 { { 1, 1}, {0, 0}, { 3, 3}, NULL
, NULL
},
1867 { { 2, 1}, {0, 0}, { 2, 3}, NULL
, NULL
}
1871 "bend-to-horizontal",
1872 "With intersection truncation one edge bends to horizontal",
1875 { { 9, 1}, {0, 0}, {3, 7}, NULL
, NULL
},
1876 { { 3, 5}, {0, 0}, {9, 9}, NULL
, NULL
}
1884 "An intersection that occurs at the endpoint of a segment.",
1886 { { 4, 6}, { 5, 6}, NULL, { { NULL }} },
1887 { { 4, 5}, { 5, 7}, NULL, { { NULL }} },
1888 { { 0, 0}, { 0, 0}, NULL, { { NULL }} },
1892 name = "overlapping",
1893 desc = "Parallel segments that share an endpoint, with different slopes.",
1895 { top = { x = 2, y = 0}, bottom = { x = 1, y = 1}},
1896 { top = { x = 2, y = 0}, bottom = { x = 0, y = 2}},
1897 { top = { x = 0, y = 3}, bottom = { x = 1, y = 3}},
1898 { top = { x = 0, y = 3}, bottom = { x = 2, y = 3}},
1899 { top = { x = 0, y = 4}, bottom = { x = 0, y = 6}},
1900 { top = { x = 0, y = 5}, bottom = { x = 0, y = 6}}
1904 name = "hobby_stage_3",
1905 desc = "A particularly tricky part of the 3rd stage of the 'hobby' test below.",
1907 { top = { x = -1, y = -2}, bottom = { x = 4, y = 2}},
1908 { top = { x = 5, y = 3}, bottom = { x = 9, y = 5}},
1909 { top = { x = 5, y = 3}, bottom = { x = 6, y = 3}},
1914 desc = "Example from John Hobby's paper. Requires 3 passes of the iterative algorithm.",
1916 { top = { x = 0, y = 0}, bottom = { x = 9, y = 5}},
1917 { top = { x = 0, y = 0}, bottom = { x = 13, y = 6}},
1918 { top = { x = -1, y = -2}, bottom = { x = 9, y = 5}}
1923 desc = "Edges with same start/stop points but different slopes",
1925 { top = { x = 4, y = 1}, bottom = { x = 6, y = 3}},
1926 { top = { x = 4, y = 1}, bottom = { x = 2, y = 3}},
1927 { top = { x = 2, y = 4}, bottom = { x = 4, y = 6}},
1928 { top = { x = 6, y = 4}, bottom = { x = 4, y = 6}}
1932 name = "horizontal",
1933 desc = "Test of a horizontal edge",
1935 { top = { x = 1, y = 1}, bottom = { x = 6, y = 6}},
1936 { top = { x = 2, y = 3}, bottom = { x = 5, y = 3}}
1941 desc = "Test of a vertical edge",
1943 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
1944 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
1949 desc = "Two overlapping edges with the same slope",
1951 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
1952 { top = { x = 5, y = 2}, bottom = { x = 5, y = 6}},
1953 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
1958 desc = "Several segments with a common intersection point",
1960 { top = { x = 1, y = 2}, bottom = { x = 5, y = 4} },
1961 { top = { x = 1, y = 1}, bottom = { x = 5, y = 5} },
1962 { top = { x = 2, y = 1}, bottom = { x = 4, y = 5} },
1963 { top = { x = 4, y = 1}, bottom = { x = 2, y = 5} },
1964 { top = { x = 5, y = 1}, bottom = { x = 1, y = 5} },
1965 { top = { x = 5, y = 2}, bottom = { x = 1, y = 4} }
1972 run_test (const char *test_name
,
1973 cairo_bo_edge_t
*test_edges
,
1976 int i
, intersections
, passes
;
1977 cairo_bo_edge_t
*edges
;
1978 cairo_array_t intersected_edges
;
1980 printf ("Testing: %s\n", test_name
);
1982 _cairo_array_init (&intersected_edges
, sizeof (cairo_bo_edge_t
));
1984 intersections
= _cairo_bentley_ottmann_intersect_edges (test_edges
, num_edges
, &intersected_edges
);
1986 printf ("Pass 1 found %d intersections:\n", intersections
);
1989 /* XXX: Multi-pass Bentley-Ottmmann. Preferable would be to add a
1990 * pass of Hobby's tolerance-square algorithm instead. */
1992 while (intersections
) {
1993 int num_edges
= _cairo_array_num_elements (&intersected_edges
);
1995 edges
= _cairo_malloc_ab (num_edges
, sizeof (cairo_bo_edge_t
));
1996 assert (edges
!= NULL
);
1997 memcpy (edges
, _cairo_array_index (&intersected_edges
, 0), num_edges
* sizeof (cairo_bo_edge_t
));
1998 _cairo_array_fini (&intersected_edges
);
1999 _cairo_array_init (&intersected_edges
, sizeof (cairo_bo_edge_t
));
2000 intersections
= _cairo_bentley_ottmann_intersect_edges (edges
, num_edges
, &intersected_edges
);
2004 printf ("Pass %d found %d remaining intersections:\n", passes
, intersections
);
2007 for (i
= 0; i
< passes
; i
++)
2009 printf ("No remainining intersections found after pass %d\n", passes
);
2013 if (edges_have_an_intersection_quadratic (_cairo_array_index (&intersected_edges
, 0),
2014 _cairo_array_num_elements (&intersected_edges
)))
2015 printf ("*** FAIL ***\n");
2019 _cairo_array_fini (&intersected_edges
);
2024 #define MAX_RANDOM 300
2029 char random_name
[] = "random-XX";
2030 cairo_bo_edge_t random_edges
[MAX_RANDOM
], *edge
;
2031 unsigned int i
, num_random
;
2034 for (i
= 0; i
< ARRAY_LENGTH (tests
); i
++) {
2036 run_test (test
->name
, test
->edges
, test
->num_edges
);
2039 for (num_random
= 0; num_random
< MAX_RANDOM
; num_random
++) {
2041 for (i
= 0; i
< num_random
; i
++) {
2043 edge
= &random_edges
[i
];
2044 edge
->top
.x
= (int32_t) (10.0 * (rand() / (RAND_MAX
+ 1.0)));
2045 edge
->top
.y
= (int32_t) (10.0 * (rand() / (RAND_MAX
+ 1.0)));
2046 edge
->bottom
.x
= (int32_t) (10.0 * (rand() / (RAND_MAX
+ 1.0)));
2047 edge
->bottom
.y
= (int32_t) (10.0 * (rand() / (RAND_MAX
+ 1.0)));
2048 if (edge
->top
.y
> edge
->bottom
.y
) {
2049 int32_t tmp
= edge
->top
.y
;
2050 edge
->top
.y
= edge
->bottom
.y
;
2051 edge
->bottom
.y
= tmp
;
2053 } while (edge
->top
.y
== edge
->bottom
.y
);
2056 sprintf (random_name
, "random-%02d", num_random
);
2058 run_test (random_name
, random_edges
, num_random
);