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-combsort-inline.h"
42 #include "cairo-error-private.h"
43 #include "cairo-freelist-private.h"
44 #include "cairo-line-inline.h"
45 #include "cairo-traps-private.h"
47 #define DEBUG_PRINT_STATE 0
48 #define DEBUG_EVENTS 0
51 typedef cairo_point_t cairo_bo_point32_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 _cairo_bo_trap cairo_bo_trap_t;
66 /* A deferred trapezoid of an edge */
67 struct _cairo_bo_trap {
68 cairo_bo_edge_t *right;
72 struct _cairo_bo_edge {
74 cairo_bo_edge_t *prev;
75 cairo_bo_edge_t *next;
76 cairo_bo_edge_t *colinear;
77 cairo_bo_trap_t deferred_trap;
80 /* the parent is always given by index/2 */
81 #define PQ_PARENT_INDEX(i) ((i) >> 1)
82 #define PQ_FIRST_ENTRY 1
84 /* left and right children are index * 2 and (index * 2) +1 respectively */
85 #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
88 CAIRO_BO_EVENT_TYPE_STOP,
89 CAIRO_BO_EVENT_TYPE_INTERSECTION,
90 CAIRO_BO_EVENT_TYPE_START
91 } cairo_bo_event_type_t;
93 typedef struct _cairo_bo_event {
94 cairo_bo_event_type_t type;
98 typedef struct _cairo_bo_start_event {
99 cairo_bo_event_type_t type;
101 cairo_bo_edge_t edge;
102 } cairo_bo_start_event_t;
104 typedef struct _cairo_bo_queue_event {
105 cairo_bo_event_type_t type;
109 } cairo_bo_queue_event_t;
111 typedef struct _pqueue {
114 cairo_bo_event_t **elements;
115 cairo_bo_event_t *elements_embedded[1024];
118 typedef struct _cairo_bo_event_queue {
119 cairo_freepool_t pool;
121 cairo_bo_event_t **start_events;
122 } cairo_bo_event_queue_t;
124 typedef struct _cairo_bo_sweep_line {
125 cairo_bo_edge_t *head;
126 cairo_bo_edge_t *stopped;
128 cairo_bo_edge_t *current_edge;
129 } cairo_bo_sweep_line_t;
133 dump_traps (cairo_traps_t *traps, const char *filename)
139 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
143 if (traps->has_limits) {
144 printf ("%s: limits=(%d, %d, %d, %d)\n",
146 traps->limits.p1.x, traps->limits.p1.y,
147 traps->limits.p2.x, traps->limits.p2.y);
150 _cairo_traps_extents (traps, &extents);
151 printf ("%s: extents=(%d, %d, %d, %d)\n",
153 extents.p1.x, extents.p1.y,
154 extents.p2.x, extents.p2.y);
156 file = fopen (filename, "a");
158 for (n = 0; n < traps->num_traps; n++) {
159 fprintf (file, "%d %d L:(%d, %d), (%d, %d) R:(%d, %d), (%d, %d)\n",
161 traps->traps[n].bottom,
162 traps->traps[n].left.p1.x,
163 traps->traps[n].left.p1.y,
164 traps->traps[n].left.p2.x,
165 traps->traps[n].left.p2.y,
166 traps->traps[n].right.p1.x,
167 traps->traps[n].right.p1.y,
168 traps->traps[n].right.p2.x,
169 traps->traps[n].right.p2.y);
171 fprintf (file, "\n");
177 dump_edges (cairo_bo_start_event_t *events,
179 const char *filename)
184 if (getenv ("CAIRO_DEBUG_TRAPS") == NULL)
187 file = fopen (filename, "a");
189 for (n = 0; n < num_edges; n++) {
190 fprintf (file, "(%d, %d), (%d, %d) %d %d %d\n",
191 events[n].edge.edge.line.p1.x,
192 events[n].edge.edge.line.p1.y,
193 events[n].edge.edge.line.p2.x,
194 events[n].edge.edge.line.p2.y,
195 events[n].edge.edge.top,
196 events[n].edge.edge.bottom,
197 events[n].edge.edge.dir);
199 fprintf (file, "\n");
206 _line_compute_intersection_x_for_y (const cairo_line_t *line,
217 dy = line->p2.y - line->p1.y;
219 x += _cairo_fixed_mul_div_floor (y - line->p1.y,
220 line->p2.x - line->p1.x,
228 _cairo_bo_point32_compare (cairo_bo_point32_t const *a,
229 cairo_bo_point32_t const *b)
240 /* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
241 * slope a is respectively greater than, equal to, or less than the
244 * For each edge, consider the direction vector formed from:
250 * (dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
252 * We then define the slope of each edge as dx/dy, (which is the
253 * inverse of the slope typically used in math instruction). We never
254 * compute a slope directly as the value approaches infinity, but we
255 * can derive a slope comparison without division as follows, (where
256 * the ? represents our compare operator).
258 * 1. slope(a) ? slope(b)
259 * 2. adx/ady ? bdx/bdy
260 * 3. (adx * bdy) ? (bdx * ady)
262 * Note that from step 2 to step 3 there is no change needed in the
263 * sign of the result since both ady and bdy are guaranteed to be
264 * greater than or equal to 0.
266 * When using this slope comparison to sort edges, some care is needed
267 * when interpreting the results. Since the slope compare operates on
268 * distance vectors from top to bottom it gives a correct left to
269 * right sort for edges that have a common top point, (such as two
270 * edges with start events at the same location). On the other hand,
271 * the sense of the result will be exactly reversed for two edges that
272 * have a common stop point.
275 _slope_compare (const cairo_bo_edge_t *a,
276 const cairo_bo_edge_t *b)
278 /* XXX: We're assuming here that dx and dy will still fit in 32
279 * bits. That's not true in general as there could be overflow. We
280 * should prevent that before the tessellation algorithm
283 int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
284 int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
286 /* Since the dy's are all positive by construction we can fast
287 * path several common cases.
290 /* First check for vertical lines. */
296 /* Then where the two edges point in different directions wrt x. */
300 /* Finally we actually need to do the general comparison. */
302 int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
303 int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
304 cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
305 cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
307 return _cairo_int64_cmp (adx_bdy, bdx_ady);
313 * We need to compare the x-coordinate of a line for a particular y wrt to a
314 * given x, without loss of precision.
316 * The x-coordinate along an edge for a given y is:
317 * X = A_x + (Y - A_y) * A_dx / A_dy
319 * So the inequality we wish to test is:
320 * A_x + (Y - A_y) * A_dx / A_dy ∘ X
321 * where ∘ is our inequality operator.
323 * By construction, we know that A_dy (and (Y - A_y)) are
324 * all positive, so we can rearrange it thus without causing a sign change:
325 * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
327 * Given the assumption that all the deltas fit within 32 bits, we can compute
328 * this comparison directly using 64 bit arithmetic.
330 * See the similar discussion for _slope_compare() and
331 * edges_compare_x_for_y_general().
334 edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
342 if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
344 if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
347 adx = a->edge.line.p2.x - a->edge.line.p1.x;
348 dx = x - a->edge.line.p1.x;
352 if (dx == 0 || (adx ^ dx) < 0)
355 dy = y - a->edge.line.p1.y;
356 ady = a->edge.line.p2.y - a->edge.line.p1.y;
358 L = _cairo_int32x32_64_mul (dy, adx);
359 R = _cairo_int32x32_64_mul (dx, ady);
361 return _cairo_int64_cmp (L, R);
365 _cairo_bo_sweep_line_compare_edges (const cairo_bo_sweep_line_t *sweep_line,
366 const cairo_bo_edge_t *a,
367 const cairo_bo_edge_t *b)
371 cmp = cairo_lines_compare_at_y (&a->edge.line,
373 sweep_line->current_y);
377 /* We've got two collinear edges now. */
378 return b->edge.bottom - a->edge.bottom;
381 static inline cairo_int64_t
382 det32_64 (int32_t a, int32_t b,
383 int32_t c, int32_t d)
385 /* det = a * d - b * c */
386 return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
387 _cairo_int32x32_64_mul (b, c));
390 static inline cairo_int128_t
391 det64x32_128 (cairo_int64_t a, int32_t b,
392 cairo_int64_t c, int32_t d)
394 /* det = a * d - b * c */
395 return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
396 _cairo_int64x32_128_mul (c, b));
399 /* Compute the intersection of two lines as defined by two edges. The
400 * result is provided as a coordinate pair of 128-bit integers.
402 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
403 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
406 intersect_lines (cairo_bo_edge_t *a,
408 cairo_bo_intersect_point_t *intersection)
410 cairo_int64_t a_det, b_det;
412 /* XXX: We're assuming here that dx and dy will still fit in 32
413 * bits. That's not true in general as there could be overflow. We
414 * should prevent that before the tessellation algorithm begins.
415 * What we're doing to mitigate this is to perform clamping in
416 * cairo_bo_tessellate_polygon().
418 int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
419 int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
421 int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
422 int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
424 cairo_int64_t den_det;
428 den_det = det32_64 (dx1, dy1, dx2, dy2);
430 /* Q: Can we determine that the lines do not intersect (within range)
431 * much more cheaply than computing the intersection point i.e. by
432 * avoiding the division?
434 * X = ax + t * adx = bx + s * bdx;
435 * Y = ay + t * ady = by + s * bdy;
436 * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
439 * Therefore we can reject any intersection (under the criteria for
440 * valid intersection events) if:
441 * L^R < 0 => t < 0, or
444 * (where top/bottom must at least extend to the line endpoints).
446 * A similar substitution can be performed for s, yielding:
447 * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
449 R = det32_64 (dx2, dy2,
450 b->edge.line.p1.x - a->edge.line.p1.x,
451 b->edge.line.p1.y - a->edge.line.p1.y);
452 if (_cairo_int64_negative (den_det)) {
453 if (_cairo_int64_ge (den_det, R))
456 if (_cairo_int64_le (den_det, R))
460 R = det32_64 (dy1, dx1,
461 a->edge.line.p1.y - b->edge.line.p1.y,
462 a->edge.line.p1.x - b->edge.line.p1.x);
463 if (_cairo_int64_negative (den_det)) {
464 if (_cairo_int64_ge (den_det, R))
467 if (_cairo_int64_le (den_det, R))
471 /* We now know that the two lines should intersect within range. */
473 a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
474 a->edge.line.p2.x, a->edge.line.p2.y);
475 b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
476 b->edge.line.p2.x, b->edge.line.p2.y);
478 /* x = det (a_det, dx1, b_det, dx2) / den_det */
479 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
482 if (_cairo_int64_eq (qr.rem, den_det))
485 intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
487 intersection->x.exactness = EXACT;
488 if (! _cairo_int64_is_zero (qr.rem)) {
489 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
490 qr.rem = _cairo_int64_negate (qr.rem);
491 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
492 if (_cairo_int64_ge (qr.rem, den_det)) {
493 qr.quo = _cairo_int64_add (qr.quo,
494 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
496 intersection->x.exactness = INEXACT;
499 intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo);
501 /* y = det (a_det, dy1, b_det, dy2) / den_det */
502 qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
505 if (_cairo_int64_eq (qr.rem, den_det))
508 intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT;
510 intersection->y.exactness = EXACT;
511 if (! _cairo_int64_is_zero (qr.rem)) {
512 if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem))
513 qr.rem = _cairo_int64_negate (qr.rem);
514 qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2));
515 if (_cairo_int64_ge (qr.rem, den_det)) {
516 qr.quo = _cairo_int64_add (qr.quo,
517 _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1));
519 intersection->y.exactness = INEXACT;
522 intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo);
528 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
531 /* First compare the quotient */
536 /* With quotient identical, if remainder is 0 then compare equal */
537 /* Otherwise, the non-zero remainder makes a > b */
538 return INEXACT == a.exactness;
541 /* Does the given edge contain the given point. The point must already
542 * be known to be contained within the line determined by the edge,
543 * (most likely the point results from an intersection of this edge
546 * If we had exact arithmetic, then this function would simply be a
547 * matter of examining whether the y value of the point lies within
548 * the range of y values of the edge. But since intersection points
549 * are not exact due to being rounded to the nearest integer within
550 * the available precision, we must also examine the x value of the
553 * The definition of "contains" here is that the given intersection
554 * point will be seen by the sweep line after the start event for the
555 * given edge and before the stop event for the edge. See the comments
556 * in the implementation for more details.
559 _cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t *edge,
560 cairo_bo_intersect_point_t *point)
562 int cmp_top, cmp_bottom;
564 /* XXX: When running the actual algorithm, we don't actually need to
565 * compare against edge->top at all here, since any intersection above
566 * top is eliminated early via a slope comparison. We're leaving these
567 * here for now only for the sake of the quadratic-time intersection
568 * finder which needs them.
571 cmp_top = _cairo_bo_intersect_ordinate_32_compare (point->y,
573 cmp_bottom = _cairo_bo_intersect_ordinate_32_compare (point->y,
576 if (cmp_top < 0 || cmp_bottom > 0)
581 if (cmp_top > 0 && cmp_bottom < 0)
586 /* At this stage, the point lies on the same y value as either
587 * edge->top or edge->bottom, so we have to examine the x value in
588 * order to properly determine containment. */
590 /* If the y value of the point is the same as the y value of the
591 * top of the edge, then the x value of the point must be greater
592 * to be considered as inside the edge. Similarly, if the y value
593 * of the point is the same as the y value of the bottom of the
594 * edge, then the x value of the point must be less to be
595 * considered as inside. */
600 top_x = _line_compute_intersection_x_for_y (&edge->edge.line,
602 return _cairo_bo_intersect_ordinate_32_compare (point->x, top_x) > 0;
603 } else { /* cmp_bottom == 0 */
606 bot_x = _line_compute_intersection_x_for_y (&edge->edge.line,
608 return _cairo_bo_intersect_ordinate_32_compare (point->x, bot_x) < 0;
612 /* Compute the intersection of two edges. The result is provided as a
613 * coordinate pair of 128-bit integers.
615 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
616 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
617 * intersection of the lines defined by the edges occurs outside of
618 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
619 * are exactly parallel.
621 * Note that when determining if a candidate intersection is "inside"
622 * an edge, we consider both the infinitesimal shortening and the
623 * infinitesimal tilt rules described by John Hobby. Specifically, if
624 * the intersection is exactly the same as an edge point, it is
625 * effectively outside (no intersection is returned). Also, if the
626 * intersection point has the same
629 _cairo_bo_edge_intersect (cairo_bo_edge_t *a,
631 cairo_bo_point32_t *intersection)
633 cairo_bo_intersect_point_t quorem;
635 if (! intersect_lines (a, b, &quorem))
638 if (! _cairo_bo_edge_contains_intersect_point (a, &quorem))
641 if (! _cairo_bo_edge_contains_intersect_point (b, &quorem))
644 /* Now that we've correctly compared the intersection point and
645 * determined that it lies within the edge, then we know that we
646 * no longer need any more bits of storage for the intersection
647 * than we do for our edge coordinates. We also no longer need the
648 * remainder from the division. */
649 intersection->x = quorem.x.ordinate;
650 intersection->y = quorem.y.ordinate;
656 cairo_bo_event_compare (const cairo_bo_event_t *a,
657 const cairo_bo_event_t *b)
661 cmp = _cairo_bo_point32_compare (&a->point, &b->point);
665 cmp = a->type - b->type;
673 _pqueue_init (pqueue_t *pq)
675 pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
678 pq->elements = pq->elements_embedded;
682 _pqueue_fini (pqueue_t *pq)
684 if (pq->elements != pq->elements_embedded)
688 static cairo_status_t
689 _pqueue_grow (pqueue_t *pq)
691 cairo_bo_event_t **new_elements;
694 if (pq->elements == pq->elements_embedded) {
695 new_elements = _cairo_malloc_ab (pq->max_size,
696 sizeof (cairo_bo_event_t *));
697 if (unlikely (new_elements == NULL))
698 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
700 memcpy (new_elements, pq->elements_embedded,
701 sizeof (pq->elements_embedded));
703 new_elements = _cairo_realloc_ab (pq->elements,
705 sizeof (cairo_bo_event_t *));
706 if (unlikely (new_elements == NULL))
707 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
710 pq->elements = new_elements;
711 return CAIRO_STATUS_SUCCESS;
714 static inline cairo_status_t
715 _pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
717 cairo_bo_event_t **elements;
720 if (unlikely (pq->size + 1 == pq->max_size)) {
721 cairo_status_t status;
723 status = _pqueue_grow (pq);
724 if (unlikely (status))
728 elements = pq->elements;
731 i != PQ_FIRST_ENTRY &&
732 cairo_bo_event_compare (event,
733 elements[parent = PQ_PARENT_INDEX (i)]) < 0;
736 elements[i] = elements[parent];
741 return CAIRO_STATUS_SUCCESS;
745 _pqueue_pop (pqueue_t *pq)
747 cairo_bo_event_t **elements = pq->elements;
748 cairo_bo_event_t *tail;
751 tail = elements[pq->size--];
753 elements[PQ_FIRST_ENTRY] = NULL;
757 for (i = PQ_FIRST_ENTRY;
758 (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
761 if (child != pq->size &&
762 cairo_bo_event_compare (elements[child+1],
763 elements[child]) < 0)
768 if (cairo_bo_event_compare (elements[child], tail) >= 0)
771 elements[i] = elements[child];
776 static inline cairo_status_t
777 _cairo_bo_event_queue_insert (cairo_bo_event_queue_t *queue,
778 cairo_bo_event_type_t type,
781 const cairo_point_t *point)
783 cairo_bo_queue_event_t *event;
785 event = _cairo_freepool_alloc (&queue->pool);
786 if (unlikely (event == NULL))
787 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
792 event->point = *point;
794 return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
798 _cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
799 cairo_bo_event_t *event)
801 _cairo_freepool_free (&queue->pool, event);
804 static cairo_bo_event_t *
805 _cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
807 cairo_bo_event_t *event, *cmp;
809 event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
810 cmp = *event_queue->start_events;
812 (cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
815 event_queue->start_events++;
819 _pqueue_pop (&event_queue->pqueue);
825 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
827 cairo_bo_event_compare)
830 _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
831 cairo_bo_event_t **start_events,
834 event_queue->start_events = start_events;
836 _cairo_freepool_init (&event_queue->pool,
837 sizeof (cairo_bo_queue_event_t));
838 _pqueue_init (&event_queue->pqueue);
839 event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
842 static cairo_status_t
843 _cairo_bo_event_queue_insert_stop (cairo_bo_event_queue_t *event_queue,
844 cairo_bo_edge_t *edge)
846 cairo_bo_point32_t point;
848 point.y = edge->edge.bottom;
849 point.x = _line_compute_intersection_x_for_y (&edge->edge.line,
851 return _cairo_bo_event_queue_insert (event_queue,
852 CAIRO_BO_EVENT_TYPE_STOP,
858 _cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
860 _pqueue_fini (&event_queue->pqueue);
861 _cairo_freepool_fini (&event_queue->pool);
864 static inline cairo_status_t
865 _cairo_bo_event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t *event_queue,
866 cairo_bo_edge_t *left,
867 cairo_bo_edge_t *right)
869 cairo_bo_point32_t intersection;
871 if (MAX (left->edge.line.p1.x, left->edge.line.p2.x) <=
872 MIN (right->edge.line.p1.x, right->edge.line.p2.x))
873 return CAIRO_STATUS_SUCCESS;
875 if (cairo_lines_equal (&left->edge.line, &right->edge.line))
876 return CAIRO_STATUS_SUCCESS;
878 /* The names "left" and "right" here are correct descriptions of
879 * the order of the two edges within the active edge list. So if a
880 * slope comparison also puts left less than right, then we know
881 * that the intersection of these two segments has already
882 * occurred before the current sweep line position. */
883 if (_slope_compare (left, right) <= 0)
884 return CAIRO_STATUS_SUCCESS;
886 if (! _cairo_bo_edge_intersect (left, right, &intersection))
887 return CAIRO_STATUS_SUCCESS;
889 return _cairo_bo_event_queue_insert (event_queue,
890 CAIRO_BO_EVENT_TYPE_INTERSECTION,
896 _cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
898 sweep_line->head = NULL;
899 sweep_line->stopped = NULL;
900 sweep_line->current_y = INT32_MIN;
901 sweep_line->current_edge = NULL;
905 _cairo_bo_sweep_line_insert (cairo_bo_sweep_line_t *sweep_line,
906 cairo_bo_edge_t *edge)
908 if (sweep_line->current_edge != NULL) {
909 cairo_bo_edge_t *prev, *next;
912 cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
913 sweep_line->current_edge,
916 prev = sweep_line->current_edge;
918 while (next != NULL &&
919 _cairo_bo_sweep_line_compare_edges (sweep_line,
922 prev = next, next = prev->next;
930 } else if (cmp > 0) {
931 next = sweep_line->current_edge;
933 while (prev != NULL &&
934 _cairo_bo_sweep_line_compare_edges (sweep_line,
937 next = prev, prev = next->prev;
946 sweep_line->head = edge;
948 prev = sweep_line->current_edge;
950 edge->next = prev->next;
951 if (prev->next != NULL)
952 prev->next->prev = edge;
956 sweep_line->head = edge;
960 sweep_line->current_edge = edge;
964 _cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t *sweep_line,
965 cairo_bo_edge_t *edge)
967 if (edge->prev != NULL)
968 edge->prev->next = edge->next;
970 sweep_line->head = edge->next;
972 if (edge->next != NULL)
973 edge->next->prev = edge->prev;
975 if (sweep_line->current_edge == edge)
976 sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
980 _cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t *sweep_line,
981 cairo_bo_edge_t *left,
982 cairo_bo_edge_t *right)
984 if (left->prev != NULL)
985 left->prev->next = right;
987 sweep_line->head = right;
989 if (right->next != NULL)
990 right->next->prev = left;
992 left->next = right->next;
995 right->prev = left->prev;
999 #if DEBUG_PRINT_STATE
1001 _cairo_bo_edge_print (cairo_bo_edge_t *edge)
1003 printf ("(0x%x, 0x%x)-(0x%x, 0x%x)",
1004 edge->edge.line.p1.x, edge->edge.line.p1.y,
1005 edge->edge.line.p2.x, edge->edge.line.p2.y);
1009 _cairo_bo_event_print (cairo_bo_event_t *event)
1011 switch (event->type) {
1012 case CAIRO_BO_EVENT_TYPE_START:
1015 case CAIRO_BO_EVENT_TYPE_STOP:
1018 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1019 printf ("Intersection: ");
1022 printf ("(%d, %d)\t", event->point.x, event->point.y);
1023 _cairo_bo_edge_print (event->e1);
1024 if (event->type == CAIRO_BO_EVENT_TYPE_INTERSECTION) {
1026 _cairo_bo_edge_print (event->e2);
1032 _cairo_bo_event_queue_print (cairo_bo_event_queue_t *event_queue)
1034 /* XXX: fixme to print the start/stop array too. */
1035 printf ("Event queue:\n");
1039 _cairo_bo_sweep_line_print (cairo_bo_sweep_line_t *sweep_line)
1041 cairo_bool_t first = TRUE;
1042 cairo_bo_edge_t *edge;
1044 printf ("Sweep line from edge list: ");
1046 for (edge = sweep_line->head;
1052 _cairo_bo_edge_print (edge);
1059 print_state (const char *msg,
1060 cairo_bo_event_t *event,
1061 cairo_bo_event_queue_t *event_queue,
1062 cairo_bo_sweep_line_t *sweep_line)
1064 printf ("%s ", msg);
1065 _cairo_bo_event_print (event);
1066 _cairo_bo_event_queue_print (event_queue);
1067 _cairo_bo_sweep_line_print (sweep_line);
1073 static void CAIRO_PRINTF_FORMAT (1, 2)
1074 event_log (const char *fmt, ...)
1078 if (getenv ("CAIRO_DEBUG_EVENTS") == NULL)
1081 file = fopen ("bo-events.txt", "a");
1086 vfprintf (file, fmt, ap);
1094 #define HAS_COLINEAR(a, b) ((cairo_bo_edge_t *)(((uintptr_t)(a))&~1) == (b))
1095 #define IS_COLINEAR(e) (((uintptr_t)(e))&1)
1096 #define MARK_COLINEAR(e, v) ((cairo_bo_edge_t *)(((uintptr_t)(e))|(v)))
1098 static inline cairo_bool_t
1099 edges_colinear (cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
1103 if (HAS_COLINEAR(a->colinear, b))
1104 return IS_COLINEAR(a->colinear);
1106 if (HAS_COLINEAR(b->colinear, a)) {
1107 p = IS_COLINEAR(b->colinear);
1108 a->colinear = MARK_COLINEAR(b, p);
1113 p |= (a->edge.line.p1.x == b->edge.line.p1.x) << 0;
1114 p |= (a->edge.line.p1.y == b->edge.line.p1.y) << 1;
1115 p |= (a->edge.line.p2.x == b->edge.line.p2.x) << 3;
1116 p |= (a->edge.line.p2.y == b->edge.line.p2.y) << 4;
1117 if (p == ((1 << 0) | (1 << 1) | (1 << 3) | (1 << 4))) {
1118 a->colinear = MARK_COLINEAR(b, 1);
1122 if (_slope_compare (a, b)) {
1123 a->colinear = MARK_COLINEAR(b, 0);
1127 /* The choice of y is not truly arbitrary since we must guarantee that it
1128 * is greater than the start of either line.
1131 /* colinear if either end-point are coincident */
1132 p = (((p >> 1) & p) & 5) != 0;
1133 } else if (a->edge.line.p1.y < b->edge.line.p1.y) {
1134 p = edge_compare_for_y_against_x (b,
1136 a->edge.line.p1.x) == 0;
1138 p = edge_compare_for_y_against_x (a,
1140 b->edge.line.p1.x) == 0;
1143 a->colinear = MARK_COLINEAR(b, p);
1147 /* Adds the trapezoid, if any, of the left edge to the #cairo_traps_t */
1149 _cairo_bo_edge_end_trap (cairo_bo_edge_t *left,
1151 cairo_traps_t *traps)
1153 cairo_bo_trap_t *trap = &left->deferred_trap;
1155 /* Only emit (trivial) non-degenerate trapezoids with positive height. */
1156 if (likely (trap->top < bot)) {
1157 _cairo_traps_add_trap (traps,
1159 &left->edge.line, &trap->right->edge.line);
1161 #if DEBUG_PRINT_STATE
1162 printf ("Deferred trap: left=(%x, %x)-(%x,%x) "
1163 "right=(%x,%x)-(%x,%x) top=%x, bot=%x\n",
1164 left->edge.line.p1.x, left->edge.line.p1.y,
1165 left->edge.line.p2.x, left->edge.line.p2.y,
1166 trap->right->edge.line.p1.x, trap->right->edge.line.p1.y,
1167 trap->right->edge.line.p2.x, trap->right->edge.line.p2.y,
1171 event_log ("end trap: %lu %lu %d %d\n",
1183 /* Start a new trapezoid at the given top y coordinate, whose edges
1184 * are `edge' and `edge->next'. If `edge' already has a trapezoid,
1185 * then either add it to the traps in `traps', if the trapezoid's
1186 * right edge differs from `edge->next', or do nothing if the new
1187 * trapezoid would be a continuation of the existing one. */
1189 _cairo_bo_edge_start_or_continue_trap (cairo_bo_edge_t *left,
1190 cairo_bo_edge_t *right,
1192 cairo_traps_t *traps)
1194 if (left->deferred_trap.right == right)
1198 if (left->deferred_trap.right != NULL) {
1199 if (edges_colinear (left->deferred_trap.right, right))
1201 /* continuation on right, so just swap edges */
1202 left->deferred_trap.right = right;
1206 _cairo_bo_edge_end_trap (left, top, traps);
1209 if (! edges_colinear (left, right)) {
1210 left->deferred_trap.top = top;
1211 left->deferred_trap.right = right;
1214 event_log ("begin trap: %lu %lu %d\n",
1223 _active_edges_to_traps (cairo_bo_edge_t *pos,
1226 cairo_traps_t *traps)
1228 cairo_bo_edge_t *left;
1232 #if DEBUG_PRINT_STATE
1233 printf ("Processing active edges for %x\n", top);
1238 while (pos != NULL) {
1239 if (pos != left && pos->deferred_trap.right) {
1240 /* XXX It shouldn't be possible to here with 2 deferred traps
1241 * on colinear edges... See bug-bo-rictoz.
1243 if (left->deferred_trap.right == NULL &&
1244 edges_colinear (left, pos))
1246 /* continuation on left */
1247 left->deferred_trap = pos->deferred_trap;
1248 pos->deferred_trap.right = NULL;
1252 _cairo_bo_edge_end_trap (pos, top, traps);
1256 in_out += pos->edge.dir;
1257 if ((in_out & mask) == 0) {
1258 /* skip co-linear edges */
1259 if (pos->next == NULL || ! edges_colinear (pos, pos->next)) {
1260 _cairo_bo_edge_start_or_continue_trap (left, pos, top, traps);
1269 /* Execute a single pass of the Bentley-Ottmann algorithm on edges,
1270 * generating trapezoids according to the fill_rule and appending them
1272 static cairo_status_t
1273 _cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_event_t **start_events,
1276 cairo_traps_t *traps,
1277 int *num_intersections)
1279 cairo_status_t status;
1280 int intersection_count = 0;
1281 cairo_bo_event_queue_t event_queue;
1282 cairo_bo_sweep_line_t sweep_line;
1283 cairo_bo_event_t *event;
1284 cairo_bo_edge_t *left, *right;
1285 cairo_bo_edge_t *e1, *e2;
1287 /* convert the fill_rule into a winding mask */
1288 if (fill_rule == CAIRO_FILL_RULE_WINDING)
1289 fill_rule = (unsigned) -1;
1297 for (i = 0; i < num_events; i++) {
1298 cairo_bo_start_event_t *event =
1299 ((cairo_bo_start_event_t **) start_events)[i];
1300 event_log ("edge: %lu (%d, %d) (%d, %d) (%d, %d) %d\n",
1301 (long) &events[i].edge,
1302 event->edge.edge.line.p1.x,
1303 event->edge.edge.line.p1.y,
1304 event->edge.edge.line.p2.x,
1305 event->edge.edge.line.p2.y,
1308 event->edge.edge.dir);
1313 _cairo_bo_event_queue_init (&event_queue, start_events, num_events);
1314 _cairo_bo_sweep_line_init (&sweep_line);
1316 while ((event = _cairo_bo_event_dequeue (&event_queue))) {
1317 if (event->point.y != sweep_line.current_y) {
1318 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1319 if (e1->deferred_trap.right != NULL) {
1320 _cairo_bo_edge_end_trap (e1,
1325 sweep_line.stopped = NULL;
1327 _active_edges_to_traps (sweep_line.head,
1328 sweep_line.current_y,
1331 sweep_line.current_y = event->point.y;
1335 event_log ("event: %d (%ld, %ld) %lu, %lu\n",
1337 (long) event->point.x,
1338 (long) event->point.y,
1343 switch (event->type) {
1344 case CAIRO_BO_EVENT_TYPE_START:
1345 e1 = &((cairo_bo_start_event_t *) event)->edge;
1347 _cairo_bo_sweep_line_insert (&sweep_line, e1);
1349 status = _cairo_bo_event_queue_insert_stop (&event_queue, e1);
1350 if (unlikely (status))
1353 /* check to see if this is a continuation of a stopped edge */
1354 /* XXX change to an infinitesimal lengthening rule */
1355 for (left = sweep_line.stopped; left; left = left->next) {
1356 if (e1->edge.top <= left->edge.bottom &&
1357 edges_colinear (e1, left))
1359 e1->deferred_trap = left->deferred_trap;
1360 if (left->prev != NULL)
1361 left->prev = left->next;
1363 sweep_line.stopped = left->next;
1364 if (left->next != NULL)
1365 left->next->prev = left->prev;
1374 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
1375 if (unlikely (status))
1379 if (right != NULL) {
1380 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1381 if (unlikely (status))
1387 case CAIRO_BO_EVENT_TYPE_STOP:
1388 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1389 _cairo_bo_event_queue_delete (&event_queue, event);
1394 _cairo_bo_sweep_line_delete (&sweep_line, e1);
1396 /* first, check to see if we have a continuation via a fresh edge */
1397 if (e1->deferred_trap.right != NULL) {
1398 e1->next = sweep_line.stopped;
1399 if (sweep_line.stopped != NULL)
1400 sweep_line.stopped->prev = e1;
1401 sweep_line.stopped = e1;
1405 if (left != NULL && right != NULL) {
1406 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
1407 if (unlikely (status))
1413 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1414 e1 = ((cairo_bo_queue_event_t *) event)->e1;
1415 e2 = ((cairo_bo_queue_event_t *) event)->e2;
1416 _cairo_bo_event_queue_delete (&event_queue, event);
1418 /* skip this intersection if its edges are not adjacent */
1422 intersection_count++;
1427 _cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
1429 /* after the swap e2 is left of e1 */
1432 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
1433 if (unlikely (status))
1437 if (right != NULL) {
1438 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
1439 if (unlikely (status))
1447 *num_intersections = intersection_count;
1448 for (e1 = sweep_line.stopped; e1; e1 = e1->next) {
1449 if (e1->deferred_trap.right != NULL) {
1450 _cairo_bo_edge_end_trap (e1, e1->edge.bottom, traps);
1453 status = traps->status;
1455 _cairo_bo_event_queue_fini (&event_queue);
1465 _cairo_bentley_ottmann_tessellate_polygon (cairo_traps_t *traps,
1466 const cairo_polygon_t *polygon,
1467 cairo_fill_rule_t fill_rule)
1470 cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
1471 cairo_bo_start_event_t *events;
1472 cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
1473 cairo_bo_event_t **event_ptrs;
1474 cairo_bo_start_event_t *stack_event_y[64];
1475 cairo_bo_start_event_t **event_y = NULL;
1476 int i, num_events, y, ymin, ymax;
1477 cairo_status_t status;
1479 num_events = polygon->num_edges;
1480 if (unlikely (0 == num_events))
1481 return CAIRO_STATUS_SUCCESS;
1483 if (polygon->num_limits) {
1484 ymin = _cairo_fixed_integer_floor (polygon->limit.p1.y);
1485 ymax = _cairo_fixed_integer_ceil (polygon->limit.p2.y) - ymin;
1488 event_y = _cairo_malloc_ab(sizeof (cairo_bo_event_t*), ymax);
1489 if (unlikely (event_y == NULL))
1490 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1492 event_y = stack_event_y;
1494 memset (event_y, 0, ymax * sizeof(cairo_bo_event_t *));
1497 events = stack_events;
1498 event_ptrs = stack_event_ptrs;
1499 if (num_events > ARRAY_LENGTH (stack_events)) {
1500 events = _cairo_malloc_ab_plus_c (num_events,
1501 sizeof (cairo_bo_start_event_t) +
1502 sizeof (cairo_bo_event_t *),
1503 sizeof (cairo_bo_event_t *));
1504 if (unlikely (events == NULL)) {
1505 if (event_y != stack_event_y)
1507 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1510 event_ptrs = (cairo_bo_event_t **) (events + num_events);
1513 for (i = 0; i < num_events; i++) {
1514 events[i].type = CAIRO_BO_EVENT_TYPE_START;
1515 events[i].point.y = polygon->edges[i].top;
1517 _line_compute_intersection_x_for_y (&polygon->edges[i].line,
1520 events[i].edge.edge = polygon->edges[i];
1521 events[i].edge.deferred_trap.right = NULL;
1522 events[i].edge.prev = NULL;
1523 events[i].edge.next = NULL;
1524 events[i].edge.colinear = NULL;
1527 y = _cairo_fixed_integer_floor (events[i].point.y) - ymin;
1528 events[i].edge.next = (cairo_bo_edge_t *) event_y[y];
1529 event_y[y] = (cairo_bo_start_event_t *) &events[i];
1531 event_ptrs[i] = (cairo_bo_event_t *) &events[i];
1535 for (y = i = 0; y < ymax && i < num_events; y++) {
1536 cairo_bo_start_event_t *e;
1538 for (e = event_y[y]; e; e = (cairo_bo_start_event_t *)e->edge.next)
1539 event_ptrs[i++] = (cairo_bo_event_t *) e;
1541 _cairo_bo_event_queue_sort (event_ptrs+j, i-j);
1543 if (event_y != stack_event_y)
1546 _cairo_bo_event_queue_sort (event_ptrs, i);
1547 event_ptrs[i] = NULL;
1550 dump_edges (events, num_events, "bo-polygon-edges.txt");
1553 /* XXX: This would be the convenient place to throw in multiple
1554 * passes of the Bentley-Ottmann algorithm. It would merely
1555 * require storing the results of each pass into a temporary
1557 status = _cairo_bentley_ottmann_tessellate_bo_edges (event_ptrs, num_events,
1561 dump_traps (traps, "bo-polygon-out.txt");
1564 if (events != stack_events)
1571 _cairo_bentley_ottmann_tessellate_traps (cairo_traps_t *traps,
1572 cairo_fill_rule_t fill_rule)
1574 cairo_status_t status;
1575 cairo_polygon_t polygon;
1578 if (unlikely (0 == traps->num_traps))
1579 return CAIRO_STATUS_SUCCESS;
1582 dump_traps (traps, "bo-traps-in.txt");
1585 _cairo_polygon_init (&polygon, traps->limits, traps->num_limits);
1587 for (i = 0; i < traps->num_traps; i++) {
1588 status = _cairo_polygon_add_line (&polygon,
1589 &traps->traps[i].left,
1590 traps->traps[i].top,
1591 traps->traps[i].bottom,
1593 if (unlikely (status))
1596 status = _cairo_polygon_add_line (&polygon,
1597 &traps->traps[i].right,
1598 traps->traps[i].top,
1599 traps->traps[i].bottom,
1601 if (unlikely (status))
1605 _cairo_traps_clear (traps);
1606 status = _cairo_bentley_ottmann_tessellate_polygon (traps,
1611 dump_traps (traps, "bo-traps-out.txt");
1615 _cairo_polygon_fini (&polygon);
1622 edges_have_an_intersection_quadratic (cairo_bo_edge_t *edges,
1627 cairo_bo_edge_t *a, *b;
1628 cairo_bo_point32_t intersection;
1630 /* We must not be given any upside-down edges. */
1631 for (i = 0; i < num_edges; i++) {
1632 assert (_cairo_bo_point32_compare (&edges[i].top, &edges[i].bottom) < 0);
1633 edges[i].line.p1.x <<= CAIRO_BO_GUARD_BITS;
1634 edges[i].line.p1.y <<= CAIRO_BO_GUARD_BITS;
1635 edges[i].line.p2.x <<= CAIRO_BO_GUARD_BITS;
1636 edges[i].line.p2.y <<= CAIRO_BO_GUARD_BITS;
1639 for (i = 0; i < num_edges; i++) {
1640 for (j = 0; j < num_edges; j++) {
1647 if (! _cairo_bo_edge_intersect (a, b, &intersection))
1650 printf ("Found intersection (%d,%d) between (%d,%d)-(%d,%d) and (%d,%d)-(%d,%d)\n",
1653 a->line.p1.x, a->line.p1.y,
1654 a->line.p2.x, a->line.p2.y,
1655 b->line.p1.x, b->line.p1.y,
1656 b->line.p2.x, b->line.p2.y);
1664 #define TEST_MAX_EDGES 10
1666 typedef struct test {
1668 const char *description;
1670 cairo_bo_edge_t edges[TEST_MAX_EDGES];
1677 "3 edges all intersecting very close to each other",
1680 { { 4, 2}, {0, 0}, { 9, 9}, NULL, NULL },
1681 { { 7, 2}, {0, 0}, { 2, 3}, NULL, NULL },
1682 { { 5, 2}, {0, 0}, { 1, 7}, NULL, NULL }
1686 "inconsistent data",
1687 "Derived from random testing---was leading to skip list and edge list disagreeing.",
1690 { { 2, 3}, {0, 0}, { 8, 9}, NULL, NULL },
1691 { { 2, 3}, {0, 0}, { 6, 7}, NULL, NULL }
1696 "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?",
1699 { { 6, 2}, {0, 0}, { 6, 5}, NULL, NULL },
1700 { { 3, 5}, {0, 0}, { 5, 6}, NULL, NULL },
1701 { { 9, 2}, {0, 0}, { 5, 6}, NULL, NULL },
1705 "minimal-intersection",
1706 "Intersection of a two from among the smallest possible edges.",
1709 { { 0, 0}, {0, 0}, { 1, 1}, NULL, NULL },
1710 { { 1, 0}, {0, 0}, { 0, 1}, NULL, NULL }
1715 "A simple intersection of two edges at an integer (2,2).",
1718 { { 1, 1}, {0, 0}, { 3, 3}, NULL, NULL },
1719 { { 2, 1}, {0, 0}, { 2, 3}, NULL, NULL }
1723 "bend-to-horizontal",
1724 "With intersection truncation one edge bends to horizontal",
1727 { { 9, 1}, {0, 0}, {3, 7}, NULL, NULL },
1728 { { 3, 5}, {0, 0}, {9, 9}, NULL, NULL }
1736 "An intersection that occurs at the endpoint of a segment.",
1738 { { 4, 6}, { 5, 6}, NULL, { { NULL }} },
1739 { { 4, 5}, { 5, 7}, NULL, { { NULL }} },
1740 { { 0, 0}, { 0, 0}, NULL, { { NULL }} },
1744 name = "overlapping",
1745 desc = "Parallel segments that share an endpoint, with different slopes.",
1747 { top = { x = 2, y = 0}, bottom = { x = 1, y = 1}},
1748 { top = { x = 2, y = 0}, bottom = { x = 0, y = 2}},
1749 { top = { x = 0, y = 3}, bottom = { x = 1, y = 3}},
1750 { top = { x = 0, y = 3}, bottom = { x = 2, y = 3}},
1751 { top = { x = 0, y = 4}, bottom = { x = 0, y = 6}},
1752 { top = { x = 0, y = 5}, bottom = { x = 0, y = 6}}
1756 name = "hobby_stage_3",
1757 desc = "A particularly tricky part of the 3rd stage of the 'hobby' test below.",
1759 { top = { x = -1, y = -2}, bottom = { x = 4, y = 2}},
1760 { top = { x = 5, y = 3}, bottom = { x = 9, y = 5}},
1761 { top = { x = 5, y = 3}, bottom = { x = 6, y = 3}},
1766 desc = "Example from John Hobby's paper. Requires 3 passes of the iterative algorithm.",
1768 { top = { x = 0, y = 0}, bottom = { x = 9, y = 5}},
1769 { top = { x = 0, y = 0}, bottom = { x = 13, y = 6}},
1770 { top = { x = -1, y = -2}, bottom = { x = 9, y = 5}}
1775 desc = "Edges with same start/stop points but different slopes",
1777 { top = { x = 4, y = 1}, bottom = { x = 6, y = 3}},
1778 { top = { x = 4, y = 1}, bottom = { x = 2, y = 3}},
1779 { top = { x = 2, y = 4}, bottom = { x = 4, y = 6}},
1780 { top = { x = 6, y = 4}, bottom = { x = 4, y = 6}}
1784 name = "horizontal",
1785 desc = "Test of a horizontal edge",
1787 { top = { x = 1, y = 1}, bottom = { x = 6, y = 6}},
1788 { top = { x = 2, y = 3}, bottom = { x = 5, y = 3}}
1793 desc = "Test of a vertical edge",
1795 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
1796 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
1801 desc = "Two overlapping edges with the same slope",
1803 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
1804 { top = { x = 5, y = 2}, bottom = { x = 5, y = 6}},
1805 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
1810 desc = "Several segments with a common intersection point",
1812 { top = { x = 1, y = 2}, bottom = { x = 5, y = 4} },
1813 { top = { x = 1, y = 1}, bottom = { x = 5, y = 5} },
1814 { top = { x = 2, y = 1}, bottom = { x = 4, y = 5} },
1815 { top = { x = 4, y = 1}, bottom = { x = 2, y = 5} },
1816 { top = { x = 5, y = 1}, bottom = { x = 1, y = 5} },
1817 { top = { x = 5, y = 2}, bottom = { x = 1, y = 4} }
1824 run_test (const char *test_name,
1825 cairo_bo_edge_t *test_edges,
1828 int i, intersections, passes;
1829 cairo_bo_edge_t *edges;
1830 cairo_array_t intersected_edges;
1832 printf ("Testing: %s\n", test_name);
1834 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
1836 intersections = _cairo_bentley_ottmann_intersect_edges (test_edges, num_edges, &intersected_edges);
1838 printf ("Pass 1 found %d intersections:\n", intersections);
1841 /* XXX: Multi-pass Bentley-Ottmmann. Preferable would be to add a
1842 * pass of Hobby's tolerance-square algorithm instead. */
1844 while (intersections) {
1845 int num_edges = _cairo_array_num_elements (&intersected_edges);
1847 edges = _cairo_malloc_ab (num_edges, sizeof (cairo_bo_edge_t));
1848 assert (edges != NULL);
1849 memcpy (edges, _cairo_array_index (&intersected_edges, 0), num_edges * sizeof (cairo_bo_edge_t));
1850 _cairo_array_fini (&intersected_edges);
1851 _cairo_array_init (&intersected_edges, sizeof (cairo_bo_edge_t));
1852 intersections = _cairo_bentley_ottmann_intersect_edges (edges, num_edges, &intersected_edges);
1856 printf ("Pass %d found %d remaining intersections:\n", passes, intersections);
1859 for (i = 0; i < passes; i++)
1861 printf ("No remainining intersections found after pass %d\n", passes);
1865 if (edges_have_an_intersection_quadratic (_cairo_array_index (&intersected_edges, 0),
1866 _cairo_array_num_elements (&intersected_edges)))
1867 printf ("*** FAIL ***\n");
1871 _cairo_array_fini (&intersected_edges);
1876 #define MAX_RANDOM 300
1881 char random_name[] = "random-XX";
1882 cairo_bo_edge_t random_edges[MAX_RANDOM], *edge;
1883 unsigned int i, num_random;
1886 for (i = 0; i < ARRAY_LENGTH (tests); i++) {
1888 run_test (test->name, test->edges, test->num_edges);
1891 for (num_random = 0; num_random < MAX_RANDOM; num_random++) {
1893 for (i = 0; i < num_random; i++) {
1895 edge = &random_edges[i];
1896 edge->line.p1.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
1897 edge->line.p1.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
1898 edge->line.p2.x = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
1899 edge->line.p2.y = (int32_t) (10.0 * (rand() / (RAND_MAX + 1.0)));
1900 if (edge->line.p1.y > edge->line.p2.y) {
1901 int32_t tmp = edge->line.p1.y;
1902 edge->line.p1.y = edge->line.p2.y;
1903 edge->line.p2.y = tmp;
1905 } while (edge->line.p1.y == edge->line.p2.y);
1908 sprintf (random_name, "random-%02d", num_random);
1910 run_test (random_name, random_edges, num_random);