Changes.
[cairo/gpu.git] / src / cairo-bentley-ottmann.c
blob1d59d70386280d89e209f430ef4c8f87ab2636ff
1 /*
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
32 * Contributor(s):
33 * Carl D. Worth <cworth@cworth.org>
36 /* Provide definitions for standalone compilation */
37 #include "cairoint.h"
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 {
49 cairo_int128_t x;
50 cairo_int128_t y;
51 } cairo_bo_point128_t;
53 typedef struct _cairo_bo_intersect_ordinate {
54 int32_t 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;
71 int32_t top;
74 struct _cairo_bo_traps {
75 cairo_traps_t *traps;
76 cairo_freelist_t freelist;
78 /* These form the closed bounding box of the original input
79 * points. */
80 cairo_fixed_t xmin;
81 cairo_fixed_t ymin;
82 cairo_fixed_t xmax;
83 cairo_fixed_t ymax;
86 struct _cairo_bo_edge {
87 cairo_bo_point32_t top;
88 cairo_bo_point32_t middle;
89 cairo_bo_point32_t bottom;
90 int dir;
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;
99 skip_elt_t elt;
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)
105 typedef enum {
106 CAIRO_BO_STATUS_INTERSECTION,
107 CAIRO_BO_STATUS_PARALLEL,
108 CAIRO_BO_STATUS_NO_INTERSECTION
109 } cairo_bo_status_t;
111 typedef enum {
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;
119 cairo_bo_edge_t *e1;
120 cairo_bo_edge_t *e2;
121 cairo_bo_point32_t point;
122 skip_elt_t elt;
123 } cairo_bo_event_t;
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;
139 int32_t current_y;
140 } cairo_bo_sweep_line_t;
143 static inline int
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;
148 if (cmp) return cmp;
149 return a->x - b->x;
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
154 * slope of b.
156 * For each edge, consider the direction vector formed from:
158 * top -> bottom
160 * which is:
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.
186 static int
187 _slope_compare (cairo_bo_edge_t *a,
188 cairo_bo_edge_t *b)
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
193 * begins.
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. */
203 if (adx == 0)
204 return -bdx;
205 if (bdx == 0)
206 return adx;
208 /* Then where the two edges point in different directions wrt x. */
209 if ((adx ^ bdx) < 0)
210 return adx;
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
242 * deltas.
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().
249 static int
250 edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
251 const cairo_bo_edge_t *b,
252 int32_t y)
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
257 * begins.
259 int32_t dx;
260 int32_t adx, ady;
261 int32_t bdx, bdy;
262 enum {
263 HAVE_NONE = 0x0,
264 HAVE_DX = 0x1,
265 HAVE_ADX = 0x2,
266 HAVE_DX_ADX = HAVE_DX | HAVE_ADX,
267 HAVE_BDX = 0x4,
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;
275 if (adx == 0)
276 have_dx_adx_bdx &= ~HAVE_ADX;
278 bdy = b->bottom.y - b->top.y;
279 bdx = b->bottom.x - b->top.x;
280 if (bdx == 0)
281 have_dx_adx_bdx &= ~HAVE_BDX;
283 dx = a->top.x - b->top.x;
284 if (dx == 0)
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) {
291 default:
292 case HAVE_NONE:
293 return 0;
294 case HAVE_DX:
295 /* A_dy * B_dy * (A_x - B_x) ∘ 0 */
296 return dx; /* ady * bdy is positive definite */
297 case HAVE_ADX:
298 /* 0 ∘ - (Y - A_y) * A_dx * B_dy */
299 return adx; /* bdy * (y - a->top.y) is positive definite */
300 case HAVE_BDX:
301 /* 0 ∘ (Y - B_y) * B_dx * A_dy */
302 return -bdx; /* ady * (y - b->top.y) is positive definite */
303 case HAVE_ADX_BDX:
304 /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
305 if ((adx ^ bdx) < 0) {
306 return adx;
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);
316 } else
317 return _cairo_int128_cmp (A, B);
318 case HAVE_DX_ADX:
319 /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
320 if ((-adx ^ dx) < 0) {
321 return dx;
322 } else {
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);
330 case HAVE_DX_BDX:
331 /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
332 if ((bdx ^ dx) < 0) {
333 return dx;
334 } else {
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);
342 case HAVE_ALL:
343 return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
345 #undef B
346 #undef A
347 #undef L
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().
371 static int
372 edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
373 int32_t y,
374 int32_t x)
376 int32_t adx, ady;
377 int32_t dx, dy;
378 cairo_int64_t L, R;
380 adx = a->bottom.x - a->top.x;
381 dx = x - a->top.x;
383 if (adx == 0)
384 return -dx;
385 if ((adx ^ dx) < 0)
386 return adx;
388 dy = y - a->top.y;
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);
397 static int
398 edges_compare_x_for_y (const cairo_bo_edge_t *a,
399 const cairo_bo_edge_t *b,
400 int32_t y)
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
405 * special casing).
407 enum {
408 HAVE_NEITHER = 0x0,
409 HAVE_AX = 0x1,
410 HAVE_BX = 0x2,
411 HAVE_BOTH = HAVE_AX | HAVE_BX
412 } have_ax_bx = HAVE_BOTH;
413 int32_t ax, bx;
415 if (y == a->top.y)
416 ax = a->top.x;
417 else if (y == a->bottom.y)
418 ax = a->bottom.x;
419 else
420 have_ax_bx &= ~HAVE_AX;
422 if (y == b->top.y)
423 bx = b->top.x;
424 else if (y == b->bottom.y)
425 bx = b->bottom.x;
426 else
427 have_ax_bx &= ~HAVE_BX;
429 switch (have_ax_bx) {
430 default:
431 case HAVE_NEITHER:
432 return edges_compare_x_for_y_general (a, b, y);
433 case HAVE_AX:
434 return -edge_compare_for_y_against_x (b, y, ax);
435 case HAVE_BX:
436 return edge_compare_for_y_against_x (a, y, bx);
437 case HAVE_BOTH:
438 return ax - bx;
442 static int
443 _cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t *sweep_line,
444 cairo_bo_edge_t *a,
445 cairo_bo_edge_t *b)
447 int cmp;
449 if (a == b)
450 return 0;
452 /* don't bother solving for abscissa if the edges' bounding boxes
453 * can be used to order them. */
455 int32_t amin, amax;
456 int32_t bmin, bmax;
457 if (a->middle.x < a->bottom.x) {
458 amin = a->middle.x;
459 amax = a->bottom.x;
460 } else {
461 amin = a->bottom.x;
462 amax = a->middle.x;
464 if (b->middle.x < b->bottom.x) {
465 bmin = b->middle.x;
466 bmax = b->bottom.x;
467 } else {
468 bmin = b->bottom.x;
469 bmax = b->middle.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);
476 if (cmp)
477 return cmp;
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);
485 if (cmp)
486 return cmp;
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);
493 if (cmp)
494 return cmp;
496 cmp = _cairo_bo_point32_compare (&a->bottom, &b->bottom);
497 if (cmp)
498 return cmp;
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
503 * move. */
504 if (a > b)
505 return 1;
506 else
507 return -1;
510 static int
511 _sweep_line_elt_compare (void *list,
512 void *a,
513 void *b)
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,
520 edge_elt_a->edge,
521 edge_elt_b->edge);
524 static inline int
525 cairo_bo_event_compare (cairo_bo_event_t const *a,
526 cairo_bo_event_t const *b)
528 int cmp;
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);
537 if (cmp)
538 return cmp;
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)
547 return -1;
548 if (a->type == CAIRO_BO_EVENT_TYPE_START)
549 return 1;
551 if (b->type == CAIRO_BO_EVENT_TYPE_STOP)
552 return 1;
553 if (b->type == CAIRO_BO_EVENT_TYPE_START)
554 return -1;
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
560 * shortening rule.
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);
568 if (cmp) {
569 if (a->type == CAIRO_BO_EVENT_TYPE_START)
570 return cmp;
571 else
572 return - cmp;
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,
579 &a->e1->bottom);
580 if (cmp)
581 return cmp;
583 else if (a->type == CAIRO_BO_EVENT_TYPE_STOP) {
584 cmp = _cairo_bo_point32_compare (&a->e1->top,
585 &b->e1->top);
586 if (cmp)
587 return cmp;
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);
595 if (cmp)
596 return cmp;
597 cmp = _cairo_bo_point32_compare (&a->e2->bottom, &b->e2->bottom);
598 if (cmp)
599 return cmp;
600 cmp = _cairo_bo_point32_compare (&a->e1->top, &b->e1->top);
601 if (cmp)
602 return cmp;
603 cmp = _cairo_bo_point32_compare (&a->e1->bottom, &b->e1->bottom);
604 if (cmp)
605 return cmp;
608 /* Discrimination based on the edge pointers. */
609 if (a->e1 < b->e1)
610 return -1;
611 if (a->e1 > b->e1)
612 return +1;
613 if (a->e2 < b->e2)
614 return -1;
615 if (a->e2 > b->e2)
616 return +1;
617 return 0;
620 static int
621 cairo_bo_event_compare_abstract (void *list,
622 void *a,
623 void *b)
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);
631 static int
632 cairo_bo_event_compare_pointers (const cairo_bo_event_t *a,
633 const cairo_bo_event_t *b)
635 int cmp;
637 if (a == b)
638 return 0;
639 cmp = cairo_bo_event_compare (a, b);
640 if (cmp)
641 return cmp;
643 return a - b;
646 static inline cairo_int64_t
647 det32_64 (int32_t a,
648 int32_t b,
649 int32_t c,
650 int32_t d)
652 cairo_int64_t ad;
653 cairo_int64_t bc;
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,
664 int32_t b,
665 cairo_int64_t c,
666 int32_t d)
668 cairo_int128_t ad;
669 cairo_int128_t bc;
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,
686 cairo_bo_edge_t *b,
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;
704 cairo_int64_t R;
705 cairo_quorem64_t qr;
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)
718 * => t * L = R
720 * Therefore we can reject any intersection (under the criteria for
721 * valid intersection events) if:
722 * L^R < 0 => t < 0, or
723 * L<R => t > 1
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;
738 } else {
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;
751 } else {
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,
765 b_det, dx2),
766 den_det);
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,
774 b_det, dy2),
775 den_det);
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;
784 static int
785 _cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t a,
786 int32_t b)
788 /* First compare the quotient */
789 if (a.ordinate > b)
790 return +1;
791 if (a.ordinate < b)
792 return -1;
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
801 * with another).
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
808 * point.
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.
815 static cairo_bool_t
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)
833 return FALSE;
836 if (cmp_top > 0 && cmp_bottom < 0)
838 return TRUE;
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. */
852 if (cmp_top == 0)
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,
876 cairo_bo_edge_t *b,
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);
883 if (status)
884 return status;
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;
903 static void
904 _cairo_bo_event_init (cairo_bo_event_t *event,
905 cairo_bo_event_type_t type,
906 cairo_bo_edge_t *e1,
907 cairo_bo_edge_t *e2,
908 cairo_bo_point32_t point)
910 event->type = type;
911 event->e1 = e1;
912 event->e2 = e2;
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);
925 return status;
928 static void
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)
945 return intersection;
947 if (intersection == NULL ||
948 cairo_bo_event_compare (startstop, intersection) <= 0)
950 event_queue->sorted_startstop_event_ptrs++;
951 return startstop;
954 return intersection;
957 CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
958 cairo_bo_event_t *,
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,
964 int num_edges)
966 int i;
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
973 * elt? */
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,
994 &edges[i], NULL,
995 edges[i].top);
997 _cairo_bo_event_init (&events[2*i+1],
998 CAIRO_BO_EVENT_TYPE_STOP,
999 &edges[i], NULL,
1000 edges[i].bottom);
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;
1013 static void
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,
1050 left, right,
1051 intersection);
1053 return _cairo_bo_event_queue_insert (event_queue, &event);
1056 static void
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;
1068 static void
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];
1088 if (next_elt)
1089 prev_of_next = & (SKIP_ELT_TO_EDGE (next_elt)->prev);
1090 else
1091 prev_of_next = &sweep_line->tail;
1093 if (*prev_of_next)
1094 next_of_prev = &(*prev_of_next)->next;
1095 else
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;
1108 static void
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;
1118 if (edge->prev)
1119 left_next = &edge->prev->next;
1121 right_prev = &sweep_line->tail;
1122 if (edge->next)
1123 right_prev = &edge->next->prev;
1125 *left_next = edge->next;
1126 *right_prev = edge->prev;
1129 static void
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;
1152 if (left->prev)
1153 before_left = &left->prev->next;
1154 *before_left = right;
1156 after_right = &sweep_line->tail;
1157 if (right->next)
1158 after_right = &right->next->prev;
1159 *after_right = left;
1161 left->next = right->next;
1162 right->next = left;
1164 right->prev = left->prev;
1165 left->prev = right;
1168 #if DEBUG_PRINT_STATE
1169 static void
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);
1177 static void
1178 _cairo_bo_event_print (cairo_bo_event_t *event)
1180 switch (event->type) {
1181 case CAIRO_BO_EVENT_TYPE_START:
1182 printf ("Start: ");
1183 break;
1184 case CAIRO_BO_EVENT_TYPE_STOP:
1185 printf ("Stop: ");
1186 break;
1187 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1188 printf ("Intersection: ");
1189 break;
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) {
1194 printf (" X ");
1195 _cairo_bo_edge_print (event->e2);
1197 printf ("\n");
1200 static void
1201 _cairo_bo_event_queue_print (cairo_bo_event_queue_t *event_queue)
1203 skip_elt_t *elt;
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];
1211 elt;
1212 elt = elt->next[0])
1214 event = SKIP_ELT_TO_EVENT (elt);
1215 _cairo_bo_event_print (event);
1219 static void
1220 _cairo_bo_sweep_line_print (cairo_bo_sweep_line_t *sweep_line)
1222 cairo_bool_t first = TRUE;
1223 skip_elt_t *elt;
1224 cairo_bo_edge_t *edge;
1226 printf ("Sweep line (reversed): ");
1228 for (edge = sweep_line->tail;
1229 edge;
1230 edge = edge->prev)
1232 if (!first)
1233 printf (", ");
1234 _cairo_bo_edge_print (edge);
1235 first = FALSE;
1237 printf ("\n");
1240 printf ("Sweep line from edge list: ");
1241 first = TRUE;
1242 for (edge = sweep_line->head;
1243 edge;
1244 edge = edge->next)
1246 if (!first)
1247 printf (", ");
1248 _cairo_bo_edge_print (edge);
1249 first = FALSE;
1251 printf ("\n");
1253 printf ("Sweep line from skip list: ");
1254 first = TRUE;
1255 for (elt = sweep_line->active_edges.chains[0];
1256 elt;
1257 elt = elt->next[0])
1259 if (!first)
1260 printf (", ");
1261 _cairo_bo_edge_print (SKIP_ELT_TO_EDGE (elt));
1262 first = FALSE;
1264 printf ("\n");
1267 static void
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);
1275 printf ("\n");
1277 #endif
1279 /* Adds the trapezoid, if any, of the left edge to the #cairo_traps_t
1280 * of bo_traps. */
1281 static cairo_status_t
1282 _cairo_bo_edge_end_trap (cairo_bo_edge_t *left,
1283 int32_t bot,
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;
1290 if (!trap)
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;
1300 fixed_bot = bot;
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;
1308 fixed_top += ymin;
1309 fixed_bot += 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,
1339 trap->top, bot);
1340 #endif
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,
1357 int32_t top,
1358 cairo_bo_traps_t *bo_traps)
1360 cairo_status_t status;
1361 cairo_bo_trap_t *trap = edge->deferred_trap;
1363 if (trap) {
1364 if (trap->right == edge->next) return CAIRO_STATUS_SUCCESS;
1365 status = _cairo_bo_edge_end_trap (edge, top, bo_traps);
1366 if (status)
1367 return status;
1370 if (edge->next) {
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;
1376 trap->top = top;
1378 return CAIRO_STATUS_SUCCESS;
1381 static void
1382 _cairo_bo_traps_init (cairo_bo_traps_t *bo_traps,
1383 cairo_traps_t *traps,
1384 cairo_fixed_t xmin,
1385 cairo_fixed_t ymin,
1386 cairo_fixed_t xmax,
1387 cairo_fixed_t ymax)
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;
1397 static void
1398 _cairo_bo_traps_fini (cairo_bo_traps_t *bo_traps)
1400 _cairo_freelist_fini (&bo_traps->freelist);
1403 #if DEBUG_VALIDATE
1404 static void
1405 _cairo_bo_sweep_line_validate (cairo_bo_sweep_line_t *sweep_line)
1407 cairo_bo_edge_t *edge;
1408 skip_elt_t *elt;
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];
1415 edge && elt;
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");
1420 abort ();
1424 if (edge || elt) {
1425 fprintf (stderr, "*** Error: Sweep line fails to validate: One list ran out before the other.\n");
1426 abort ();
1429 #endif
1432 static cairo_status_t
1433 _active_edges_to_traps (cairo_bo_edge_t *head,
1434 int32_t top,
1435 cairo_fill_rule_t fill_rule,
1436 cairo_bo_traps_t *bo_traps)
1438 cairo_status_t status;
1439 int in_out = 0;
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;
1445 if (in_out == 0) {
1446 status = _cairo_bo_edge_end_trap (edge, top, bo_traps);
1447 if (status)
1448 return status;
1449 continue;
1451 } else {
1452 in_out++;
1453 if ((in_out & 1) == 0) {
1454 status = _cairo_bo_edge_end_trap (edge, top, bo_traps);
1455 if (status)
1456 return status;
1457 continue;
1461 status = _cairo_bo_edge_start_or_continue_trap (edge, top, bo_traps);
1462 if (status)
1463 return status;
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
1471 * to traps. */
1472 static cairo_status_t
1473 _cairo_bentley_ottmann_tessellate_bo_edges (cairo_bo_edge_t *edges,
1474 int num_edges,
1475 cairo_fill_rule_t fill_rule,
1476 cairo_traps_t *traps,
1477 cairo_fixed_t xmin,
1478 cairo_fixed_t ymin,
1479 cairo_fixed_t xmax,
1480 cairo_fixed_t ymax,
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;
1493 if (num_edges == 0)
1494 return CAIRO_STATUS_SUCCESS;
1496 status = _cairo_bo_event_queue_init (&event_queue, edges, num_edges);
1497 if (status)
1498 return status;
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);
1506 #endif
1508 while (1)
1510 event = _cairo_bo_event_dequeue (&event_queue);
1511 if (!event)
1512 break;
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);
1518 if (status)
1519 goto unwind;
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:
1530 edge = event->e1;
1532 status = _cairo_bo_sweep_line_insert (&sweep_line, edge);
1533 if (status)
1534 goto unwind;
1535 /* Cache the insert position for use in pass 2.
1536 event->e2 = Sortlist::prev (sweep_line, edge);
1539 left = edge->prev;
1540 right = edge->next;
1542 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, edge);
1543 if (status)
1544 goto unwind;
1546 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, edge, right);
1547 if (status)
1548 goto unwind;
1550 #if DEBUG_PRINT_STATE
1551 print_state ("After processing start", &event_queue, &sweep_line);
1552 #endif
1553 break;
1555 case CAIRO_BO_EVENT_TYPE_STOP:
1556 edge = event->e1;
1558 left = edge->prev;
1559 right = edge->next;
1561 _cairo_bo_sweep_line_delete (&sweep_line, edge);
1563 status = _cairo_bo_edge_end_trap (edge, edge->bottom.y, &bo_traps);
1564 if (status)
1565 goto unwind;
1567 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
1568 if (status)
1569 goto unwind;
1571 #if DEBUG_PRINT_STATE
1572 print_state ("After processing stop", &event_queue, &sweep_line);
1573 #endif
1574 break;
1576 case CAIRO_BO_EVENT_TYPE_INTERSECTION:
1577 edge1 = event->e1;
1578 edge2 = event->e2;
1580 /* skip this intersection if its edges are not adjacent */
1581 if (edge2 != edge1->next)
1582 break;
1584 intersection_count++;
1586 edge1->middle = event->point;
1587 edge2->middle = event->point;
1589 left = edge1->prev;
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,
1597 left, edge2);
1598 if (status)
1599 goto unwind;
1601 status = _cairo_bo_event_queue_insert_if_intersect_below_current_y (&event_queue,
1602 edge1, right);
1603 if (status)
1604 goto unwind;
1606 #if DEBUG_PRINT_STATE
1607 print_state ("After processing intersection", &event_queue, &sweep_line);
1608 #endif
1609 break;
1611 #if DEBUG_VALIDATE
1612 _cairo_bo_sweep_line_validate (&sweep_line);
1613 #endif
1616 *num_intersections = intersection_count;
1617 unwind:
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,
1621 &bo_traps);
1622 if (!status)
1623 status = status2;
1625 _cairo_bo_traps_fini (&bo_traps);
1626 _cairo_bo_sweep_line_fini (&sweep_line);
1627 _cairo_bo_event_queue_fini (&event_queue);
1628 return status;
1631 static void
1632 update_minmax(cairo_fixed_t *inout_min,
1633 cairo_fixed_t *inout_max,
1634 cairo_fixed_t v)
1636 if (v < *inout_min)
1637 *inout_min = v;
1638 if (v > *inout_max)
1639 *inout_max = v;
1642 cairo_status_t
1643 _cairo_bentley_ottmann_tessellate_polygon (cairo_traps_t *traps,
1644 const cairo_polygon_t *polygon,
1645 cairo_fill_rule_t fill_rule)
1647 int intersections;
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;
1655 cairo_box_t limit;
1656 cairo_bool_t has_limits;
1657 int num_bo_edges;
1658 int i;
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. */
1706 if (has_limits) {
1707 /* Strictly above or below the limits? */
1708 if (bot.y <= limit.p1.y || top.y >= limit.p2.y)
1709 continue;
1712 /* Offset coordinates into the non-negative range. */
1713 top.x -= xmin;
1714 top.y -= ymin;
1715 bot.x -= xmin;
1716 bot.y -= ymin;
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
1728 * those. */
1729 continue;
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;
1741 edge->prev = NULL;
1742 edge->next = NULL;
1743 edge->sweep_line_elt = NULL;
1745 num_bo_edges++;
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
1751 * cairo_traps_t. */
1752 status = _cairo_bentley_ottmann_tessellate_bo_edges (edges, num_bo_edges,
1753 fill_rule, traps,
1754 xmin, ymin, xmax, ymax,
1755 &intersections);
1757 if (edges != stack_edges)
1758 free (edges);
1760 return status;
1763 #if 0
1764 static cairo_bool_t
1765 edges_have_an_intersection_quadratic (cairo_bo_edge_t *edges,
1766 int num_edges)
1769 int i, j;
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++) {
1785 if (i == j)
1786 continue;
1788 a = &edges[i];
1789 b = &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)
1795 continue;
1798 printf ("Found intersection (%d,%d) between (%d,%d)-(%d,%d) and (%d,%d)-(%d,%d)\n",
1799 intersection.x,
1800 intersection.y,
1801 a->top.x, a->top.y,
1802 a->bottom.x, a->bottom.y,
1803 b->top.x, b->top.y,
1804 b->bottom.x, b->bottom.y);
1806 return TRUE;
1809 return FALSE;
1812 #define TEST_MAX_EDGES 10
1814 typedef struct test {
1815 const char *name;
1816 const char *description;
1817 int num_edges;
1818 cairo_bo_edge_t edges[TEST_MAX_EDGES];
1819 } test_t;
1821 static test_t
1822 tests[] = {
1824 "3 near misses",
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 }
1843 "failed sort",
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 }
1862 "simple",
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 }
1883 "endpoint",
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.",
1894 edges = {
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.",
1906 edges = {
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}},
1913 name = "hobby",
1914 desc = "Example from John Hobby's paper. Requires 3 passes of the iterative algorithm.",
1915 edges = {
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}}
1922 name = "slope",
1923 desc = "Edges with same start/stop points but different slopes",
1924 edges = {
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",
1934 edges = {
1935 { top = { x = 1, y = 1}, bottom = { x = 6, y = 6}},
1936 { top = { x = 2, y = 3}, bottom = { x = 5, y = 3}}
1940 name = "vertical",
1941 desc = "Test of a vertical edge",
1942 edges = {
1943 { top = { x = 5, y = 1}, bottom = { x = 5, y = 7}},
1944 { top = { x = 2, y = 4}, bottom = { x = 8, y = 5}}
1948 name = "congruent",
1949 desc = "Two overlapping edges with the same slope",
1950 edges = {
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}}
1957 name = "multi",
1958 desc = "Several segments with a common intersection point",
1959 edges = {
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} }
1971 static int
1972 run_test (const char *test_name,
1973 cairo_bo_edge_t *test_edges,
1974 int num_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);
1985 if (intersections)
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. */
1991 passes = 1;
1992 while (intersections) {
1993 int num_edges = _cairo_array_num_elements (&intersected_edges);
1994 passes++;
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);
2001 free (edges);
2003 if (intersections){
2004 printf ("Pass %d found %d remaining intersections:\n", passes, intersections);
2005 } else {
2006 if (passes > 3)
2007 for (i = 0; i < passes; i++)
2008 printf ("*");
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");
2016 else
2017 printf ("PASS\n");
2019 _cairo_array_fini (&intersected_edges);
2021 return 0;
2024 #define MAX_RANDOM 300
2027 main (void)
2029 char random_name[] = "random-XX";
2030 cairo_bo_edge_t random_edges[MAX_RANDOM], *edge;
2031 unsigned int i, num_random;
2032 test_t *test;
2034 for (i = 0; i < ARRAY_LENGTH (tests); i++) {
2035 test = &tests[i];
2036 run_test (test->name, test->edges, test->num_edges);
2039 for (num_random = 0; num_random < MAX_RANDOM; num_random++) {
2040 srand (0);
2041 for (i = 0; i < num_random; i++) {
2042 do {
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);
2061 return 0;
2063 #endif