1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 2001 Raph Levien
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 /* This file contains a testbed implementation of the new intersection
25 #include "art_svp_intersect.h"
27 #include <math.h> /* for sqrt */
29 /* Sanitychecking verifies the main invariant on every priority queue
30 point. Do not use in production, as it slows things down way too
34 /* This can be used in production, to prevent hangs. Eventually, it
35 should not be necessary. */
36 #define CHEAP_SANITYCHECK
42 /* A priority queue - perhaps move to a separate file if it becomes
43 needed somewhere else */
45 #define ART_PRIQ_USE_HEAP
47 typedef struct _ArtPriQ ArtPriQ;
48 typedef struct _ArtPriPoint ArtPriPoint;
65 ArtPriQ *result = art_new (ArtPriQ, 1);
68 result->n_items_max = 16;
69 result->items = art_new (ArtPriPoint *, result->n_items_max);
74 art_pri_free (ArtPriQ *pq)
81 art_pri_empty (ArtPriQ *pq)
83 return pq->n_items == 0;
86 #ifdef ART_PRIQ_USE_HEAP
88 /* This heap implementation is based on Vasek Chvatal's course notes:
89 http://www.cs.rutgers.edu/~chvatal/notes/pq.html#heap */
92 art_pri_bubble_up (ArtPriQ *pq, int vacant, ArtPriPoint *missing)
94 ArtPriPoint **items = pq->items;
97 parent = (vacant - 1) >> 1;
98 while (vacant > 0 && (missing->y < items[parent]->y ||
99 (missing->y == items[parent]->y &&
100 missing->x < items[parent]->x)))
102 items[vacant] = items[parent];
104 parent = (vacant - 1) >> 1;
107 items[vacant] = missing;
111 art_pri_insert (ArtPriQ *pq, ArtPriPoint *point)
113 if (pq->n_items == pq->n_items_max)
114 art_expand (pq->items, ArtPriPoint *, pq->n_items_max);
116 art_pri_bubble_up (pq, pq->n_items++, point);
120 art_pri_sift_down_from_root (ArtPriQ *pq, ArtPriPoint *missing)
122 ArtPriPoint **items = pq->items;
123 int vacant = 0, child = 2;
128 if (items[child - 1]->y < items[child]->y ||
129 (items[child - 1]->y == items[child]->y &&
130 items[child - 1]->x < items[child]->x))
132 items[vacant] = items[child];
134 child = (vacant + 1) << 1;
138 items[vacant] = items[n - 1];
142 art_pri_bubble_up (pq, vacant, missing);
146 art_pri_choose (ArtPriQ *pq)
148 ArtPriPoint *result = pq->items[0];
150 art_pri_sift_down_from_root (pq, pq->items[--pq->n_items]);
156 /* Choose least point in queue */
158 art_pri_choose (ArtPriQ *pq)
162 double best_x, best_y;
166 if (pq->n_items == 0)
169 best_x = pq->items[best]->x;
170 best_y = pq->items[best]->y;
172 for (i = 1; i < pq->n_items; i++)
175 if (y < best_y || (y == best_y && pq->items[i]->x < best_x))
178 best_x = pq->items[best]->x;
182 result = pq->items[best];
183 pq->items[best] = pq->items[--pq->n_items];
188 art_pri_insert (ArtPriQ *pq, ArtPriPoint *point)
190 if (pq->n_items == pq->n_items_max)
191 art_expand (pq->items, ArtPriPoint *, pq->n_items_max);
193 pq->items[pq->n_items++] = point;
200 #include <stdlib.h> /* for rand() */
204 double_rand (double lo, double hi, int quant)
206 int tmp = rand () / (RAND_MAX * (1.0 / quant)) + 0.5;
207 return lo + tmp * ((hi - lo) / quant);
211 * This custom allocator for priority queue points is here so I can
212 * test speed. It doesn't look like it will be that significant, but
213 * if I want a small improvement later, it's something.
216 typedef ArtPriPoint *ArtPriPtPool;
218 static ArtPriPtPool *
219 art_pri_pt_pool_new (void)
221 ArtPriPtPool *result = art_new (ArtPriPtPool, 1);
227 art_pri_pt_alloc (ArtPriPtPool *pool)
229 ArtPriPoint *result = *pool;
231 return art_new (ArtPriPoint, 1);
234 *pool = result->user_data;
240 art_pri_pt_free (ArtPriPtPool *pool, ArtPriPoint *pt)
242 pt->user_data = *pool;
247 art_pri_pt_pool_free (ArtPriPtPool *pool)
249 ArtPriPoint *pt = *pool;
252 ArtPriPoint *next = pt->user_data;
260 main (int argc, char **argv)
262 ArtPriPtPool *pool = art_pri_pt_pool_new ();
265 const int n_iter = 1;
266 const int pq_size = 100;
268 for (j = 0; j < n_iter; j++)
272 for (i = 0; i < pq_size; i++)
274 ArtPriPoint *pt = art_pri_pt_alloc (pool);
275 pt->x = double_rand (0, 1, 100);
276 pt->y = double_rand (0, 1, 100);
277 pt->user_data = (void *)i;
278 art_pri_insert (pq, pt);
281 while (!art_pri_empty (pq))
283 ArtPriPoint *pt = art_pri_choose (pq);
285 printf ("(%g, %g), %d\n", pt->x, pt->y, (int)pt->user_data);
286 art_pri_pt_free (pool, pt);
291 art_pri_pt_pool_free (pool);
295 #else /* TEST_PRIQ */
297 /* A virtual class for an "svp writer". A client of this object creates an
298 SVP by repeatedly calling "add segment" and "add point" methods on it.
301 typedef struct _ArtSvpWriterRewind ArtSvpWriterRewind;
303 /* An implementation of the svp writer virtual class that applies the
306 struct _ArtSvpWriterRewind {
315 art_svp_writer_rewind_add_segment (ArtSvpWriter *self, int wind_left,
316 int delta_wind, double x, double y)
318 ArtSvpWriterRewind *swr = (ArtSvpWriterRewind *)self;
321 art_boolean left_filled, right_filled;
322 int wind_right = wind_left + delta_wind;
324 const int init_n_points_max = 4;
328 case ART_WIND_RULE_NONZERO:
329 left_filled = (wind_left != 0);
330 right_filled = (wind_right != 0);
332 case ART_WIND_RULE_INTERSECT:
333 left_filled = (wind_left > 1);
334 right_filled = (wind_right > 1);
336 case ART_WIND_RULE_ODDEVEN:
337 left_filled = (wind_left & 1);
338 right_filled = (wind_right & 1);
340 case ART_WIND_RULE_POSITIVE:
341 left_filled = (wind_left > 0);
342 right_filled = (wind_right > 0);
345 art_die ("Unknown wind rule %d\n", swr->rule);
347 if (left_filled == right_filled)
349 /* discard segment now */
351 art_dprint ("swr add_segment: %d += %d (%g, %g) --> -1\n",
352 wind_left, delta_wind, x, y);
358 seg_num = svp->n_segs++;
359 if (swr->n_segs_max == seg_num)
361 swr->n_segs_max <<= 1;
362 svp = (ArtSVP *)art_realloc (svp, sizeof(ArtSVP) +
363 (swr->n_segs_max - 1) *
366 swr->n_points_max = art_renew (swr->n_points_max, int,
369 seg = &svp->segs[seg_num];
371 seg->dir = right_filled;
372 swr->n_points_max[seg_num] = init_n_points_max;
377 seg->points = art_new (ArtPoint, init_n_points_max);
378 seg->points[0].x = x;
379 seg->points[0].y = y;
381 art_dprint ("swr add_segment: %d += %d (%g, %g) --> %d(%s)\n",
382 wind_left, delta_wind, x, y, seg_num,
383 seg->dir ? "v" : "^");
389 art_svp_writer_rewind_add_point (ArtSvpWriter *self, int seg_id,
392 ArtSvpWriterRewind *swr = (ArtSvpWriterRewind *)self;
397 art_dprint ("swr add_point: %d (%g, %g)\n", seg_id, x, y);
400 /* omitted segment */
403 seg = &swr->svp->segs[seg_id];
404 n_points = seg->n_points++;
405 if (swr->n_points_max[seg_id] == n_points)
406 art_expand (seg->points, ArtPoint, swr->n_points_max[seg_id]);
407 seg->points[n_points].x = x;
408 seg->points[n_points].y = y;
409 if (x < seg->bbox.x0)
411 if (x > seg->bbox.x1)
417 art_svp_writer_rewind_close_segment (ArtSvpWriter *self, int seg_id)
419 /* Not needed for this simple implementation. A potential future
420 optimization is to merge segments that can be merged safely. */
422 ArtSvpWriterRewind *swr = (ArtSvpWriterRewind *)self;
427 seg = &swr->svp->segs[seg_id];
428 if (seg->n_points < 2)
429 art_warn ("*** closing segment %d with only %d point%s\n",
430 seg_id, seg->n_points, seg->n_points == 1 ? "" : "s");
435 art_dprint ("swr close_segment: %d\n", seg_id);
440 art_svp_writer_rewind_reap (ArtSvpWriter *self)
442 ArtSvpWriterRewind *swr = (ArtSvpWriterRewind *)self;
443 ArtSVP *result = swr->svp;
445 art_free (swr->n_points_max);
451 art_svp_writer_rewind_new (ArtWindRule rule)
453 ArtSvpWriterRewind *result = art_new (ArtSvpWriterRewind, 1);
455 result->super.add_segment = art_svp_writer_rewind_add_segment;
456 result->super.add_point = art_svp_writer_rewind_add_point;
457 result->super.close_segment = art_svp_writer_rewind_close_segment;
460 result->n_segs_max = 16;
461 result->svp = art_alloc (sizeof(ArtSVP) +
462 (result->n_segs_max - 1) * sizeof(ArtSVPSeg));
463 result->svp->n_segs = 0;
464 result->n_points_max = art_new (int, result->n_segs_max);
466 return &result->super;
469 /* Now, data structures for the active list */
471 typedef struct _ArtActiveSeg ArtActiveSeg;
473 /* Note: BNEG is 1 for \ lines, and 0 for /. Thus,
474 x[(flags & BNEG) ^ 1] <= x[flags & BNEG] */
475 #define ART_ACTIVE_FLAGS_BNEG 1
477 /* This flag is set if the segment has been inserted into the active
479 #define ART_ACTIVE_FLAGS_IN_ACTIVE 2
481 /* This flag is set when the segment is to be deleted in the
482 horiz commit process. */
483 #define ART_ACTIVE_FLAGS_DEL 4
485 /* This flag is set if the seg_id is a valid output segment. */
486 #define ART_ACTIVE_FLAGS_OUT 8
488 /* This flag is set if the segment is in the horiz list. */
489 #define ART_ACTIVE_FLAGS_IN_HORIZ 16
491 struct _ArtActiveSeg {
493 int wind_left, delta_wind;
494 ArtActiveSeg *left, *right; /* doubly linked list structure */
496 const ArtSVPSeg *in_seg;
501 double a, b, c; /* line equation; ax+by+c = 0 for the line, a^2 + b^2 = 1,
504 /* bottom point and intersection point stack */
509 /* horiz commit list */
510 ArtActiveSeg *horiz_left, *horiz_right;
512 int horiz_delta_wind;
516 typedef struct _ArtIntersectCtx ArtIntersectCtx;
518 struct _ArtIntersectCtx {
524 ArtActiveSeg *active_head;
527 ArtActiveSeg *horiz_first;
528 ArtActiveSeg *horiz_last;
530 /* segment index of next input segment to be added to pri q */
534 #define EPSILON_A 1e-5 /* Threshold for breaking lines at point insertions */
537 * art_svp_intersect_setup_seg: Set up an active segment from input segment.
538 * @seg: Active segment.
539 * @pri_pt: Priority queue point to initialize.
541 * Sets the x[], a, b, c, flags, and stack fields according to the
542 * line from the current cursor value. Sets the priority queue point
543 * to the bottom point of this line. Also advances the input segment
547 art_svp_intersect_setup_seg (ArtActiveSeg *seg, ArtPriPoint *pri_pt)
549 const ArtSVPSeg *in_seg = seg->in_seg;
550 int in_curs = seg->in_curs++;
551 double x0, y0, x1, y1;
555 x0 = in_seg->points[in_curs].x;
556 y0 = in_seg->points[in_curs].y;
557 x1 = in_seg->points[in_curs + 1].x;
558 y1 = in_seg->points[in_curs + 1].y;
563 r2 = dx * dx + dy * dy;
564 s = r2 == 0 ? 1 : 1 / sqrt (r2);
566 seg->b = b = -dx * s;
567 seg->c = -(a * x0 + b * y0);
568 seg->flags = (seg->flags & ~ART_ACTIVE_FLAGS_BNEG) | (dx > 0);
574 seg->stack[0].x = x1;
575 seg->stack[0].y = y1;
579 * art_svp_intersect_add_horiz: Add point to horizontal list.
580 * @ctx: Intersector context.
581 * @seg: Segment with point to insert into horizontal list.
583 * Inserts @seg into horizontal list, keeping it in ascending horiz_x
586 * Note: the horiz_commit routine processes "clusters" of segs in the
587 * horiz list, all sharing the same horiz_x value. The cluster is
588 * processed in active list order, rather than horiz list order. Thus,
589 * the order of segs in the horiz list sharing the same horiz_x
590 * _should_ be irrelevant. Even so, we use b as a secondary sorting key,
591 * as a "belt and suspenders" defensive coding tactic.
594 art_svp_intersect_add_horiz (ArtIntersectCtx *ctx, ArtActiveSeg *seg)
596 ArtActiveSeg **pp = &ctx->horiz_last;
598 ArtActiveSeg *place_right = NULL;
601 #ifdef CHEAP_SANITYCHECK
602 if (seg->flags & ART_ACTIVE_FLAGS_IN_HORIZ)
604 //art_warn ("*** attempt to put segment in horiz list twice\n");
607 seg->flags |= ART_ACTIVE_FLAGS_IN_HORIZ;
611 art_dprint ("add_horiz %lx, x = %g\n", (unsigned long) seg, seg->horiz_x);
613 for (place = *pp; place != NULL && (place->horiz_x > seg->horiz_x ||
614 (place->horiz_x == seg->horiz_x &&
619 pp = &place->horiz_left;
622 seg->horiz_left = place;
623 seg->horiz_right = place_right;
625 ctx->horiz_first = seg;
627 place->horiz_right = seg;
631 art_svp_intersect_push_pt (ArtIntersectCtx *ctx, ArtActiveSeg *seg,
635 int n_stack = seg->n_stack;
637 if (n_stack == seg->n_stack_max)
638 art_expand (seg->stack, ArtPoint, seg->n_stack_max);
639 seg->stack[n_stack].x = x;
640 seg->stack[n_stack].y = y;
646 pri_pt = art_new (ArtPriPoint, 1);
649 pri_pt->user_data = seg;
650 art_pri_insert (ctx->pq, pri_pt);
659 * art_svp_intersect_break: Break an active segment.
661 * Note: y must be greater than the top point's y, and less than
664 * Return value: x coordinate of break point.
667 art_svp_intersect_break (ArtIntersectCtx *ctx, ArtActiveSeg *seg,
668 double x_ref, double y, ArtBreakFlags break_flags)
670 double x0, y0, x1, y1;
671 const ArtSVPSeg *in_seg = seg->in_seg;
672 int in_curs = seg->in_curs;
675 x0 = in_seg->points[in_curs - 1].x;
676 y0 = in_seg->points[in_curs - 1].y;
677 x1 = in_seg->points[in_curs].x;
678 y1 = in_seg->points[in_curs].y;
679 x = x0 + (x1 - x0) * ((y - y0) / (y1 - y0));
680 if ((break_flags == ART_BREAK_LEFT && x > x_ref) ||
681 (break_flags == ART_BREAK_RIGHT && x < x_ref))
684 art_dprint ("art_svp_intersect_break: limiting x to %f, was %f, %s\n",
685 x_ref, x, break_flags == ART_BREAK_LEFT ? "left" : "right");
690 /* I think we can count on min(x0, x1) <= x <= max(x0, x1) with sane
691 arithmetic, but it might be worthwhile to check just in case. */
694 art_svp_intersect_push_pt (ctx, seg, x, y);
700 art_svp_intersect_add_horiz (ctx, seg);
707 * art_svp_intersect_add_point: Add a point, breaking nearby neighbors.
708 * @ctx: Intersector context.
709 * @x: X coordinate of point to add.
710 * @y: Y coordinate of point to add.
711 * @seg: "nearby" segment, or NULL if leftmost.
713 * Return value: Segment immediately to the left of the new point, or
714 * NULL if the new point is leftmost.
716 static ArtActiveSeg *
717 art_svp_intersect_add_point (ArtIntersectCtx *ctx, double x, double y,
718 ArtActiveSeg *seg, ArtBreakFlags break_flags)
720 ArtActiveSeg *left, *right;
721 double x_min = x, x_max = x;
722 art_boolean left_live, right_live;
725 ArtActiveSeg *test, *result = NULL;
730 right = ctx->active_head;
733 left_live = (break_flags & ART_BREAK_LEFT) && (left != NULL);
734 right_live = (break_flags & ART_BREAK_RIGHT) && (right != NULL);
735 while (left_live || right_live)
739 if (x <= left->x[left->flags & ART_ACTIVE_FLAGS_BNEG] &&
740 /* It may be that one of these conjuncts turns out to be always
741 true. We test both anyway, to be defensive. */
742 y != left->y0 && y < left->y1)
744 d = x_min * left->a + y * left->b + left->c;
747 new_x = art_svp_intersect_break (ctx, left, x_min, y,
752 right_live = (right != NULL);
754 else if (new_x < x_min)
757 left_live = (left != NULL);
760 left_live = ART_FALSE;
763 left_live = ART_FALSE;
767 if (x >= right->x[(right->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1] &&
768 /* It may be that one of these conjuncts turns out to be always
769 true. We test both anyway, to be defensive. */
770 y != right->y0 && y < right->y1)
772 d = x_max * right->a + y * right->b + right->c;
775 new_x = art_svp_intersect_break (ctx, right, x_max, y,
780 left_live = (left != NULL);
782 else if (new_x >= x_max)
784 right = right->right;
785 right_live = (right != NULL);
788 right_live = ART_FALSE;
791 right_live = ART_FALSE;
795 /* Ascending order is guaranteed by break_flags. Thus, we don't need
796 to actually fix up non-ascending pairs. */
798 /* Now, (left, right) defines an interval of segments broken. Sort
799 into ascending x order. */
800 test = left == NULL ? ctx->active_head : left->right;
802 if (test != NULL && test != right)
806 else /* assert y == test->y1, I think */
818 art_warn ("art_svp_intersect_add_point: non-ascending x\n");
827 art_svp_intersect_swap_active (ArtIntersectCtx *ctx,
828 ArtActiveSeg *left_seg, ArtActiveSeg *right_seg)
830 right_seg->left = left_seg->left;
831 if (right_seg->left != NULL)
832 right_seg->left->right = right_seg;
834 ctx->active_head = right_seg;
835 left_seg->right = right_seg->right;
836 if (left_seg->right != NULL)
837 left_seg->right->left = left_seg;
838 left_seg->left = right_seg;
839 right_seg->right = left_seg;
843 * art_svp_intersect_test_cross: Test crossing of a pair of active segments.
844 * @ctx: Intersector context.
845 * @left_seg: Left segment of the pair.
846 * @right_seg: Right segment of the pair.
847 * @break_flags: Flags indicating whether to break neighbors.
849 * Tests crossing of @left_seg and @right_seg. If there is a crossing,
850 * inserts the intersection point into both segments.
852 * Return value: True if the intersection took place at the current
853 * scan line, indicating further iteration is needed.
856 art_svp_intersect_test_cross (ArtIntersectCtx *ctx,
857 ArtActiveSeg *left_seg, ArtActiveSeg *right_seg,
858 ArtBreakFlags break_flags)
860 double left_x0, left_y0, left_x1;
861 double left_y1 = left_seg->y1;
862 double right_y1 = right_seg->y1;
865 const ArtSVPSeg *in_seg;
868 double x, y; /* intersection point */
871 static int count = 0;
873 art_dprint ("art_svp_intersect_test_cross %lx <-> %lx: count=%d\n",
874 (unsigned long)left_seg, (unsigned long)right_seg, count++);
877 if (left_seg->y0 == right_seg->y0 && left_seg->x[0] == right_seg->x[0])
879 /* Top points of left and right segments coincide. This case
880 feels like a bit of duplication - we may want to merge it
881 with the cases below. However, this way, we're sure that this
882 logic makes only localized changes. */
884 if (left_y1 < right_y1)
886 /* Test left (x1, y1) against right segment */
887 double left_x1 = left_seg->x[1];
890 right_seg->x[(right_seg->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1] ||
891 left_y1 == right_seg->y0)
893 d = left_x1 * right_seg->a + left_y1 * right_seg->b + right_seg->c;
896 else if (d < EPSILON_A)
898 /* I'm unsure about the break flags here. */
899 double right_x1 = art_svp_intersect_break (ctx, right_seg,
902 if (left_x1 <= right_x1)
906 else if (left_y1 > right_y1)
908 /* Test right (x1, y1) against left segment */
909 double right_x1 = right_seg->x[1];
911 if (right_x1 > left_seg->x[left_seg->flags & ART_ACTIVE_FLAGS_BNEG] ||
912 right_y1 == left_seg->y0)
914 d = right_x1 * left_seg->a + right_y1 * left_seg->b + left_seg->c;
917 else if (d > -EPSILON_A)
919 /* See above regarding break flags. */
920 double left_x1 = art_svp_intersect_break (ctx, left_seg,
923 if (left_x1 <= right_x1)
927 else /* left_y1 == right_y1 */
929 double left_x1 = left_seg->x[1];
930 double right_x1 = right_seg->x[1];
932 if (left_x1 <= right_x1)
935 art_svp_intersect_swap_active (ctx, left_seg, right_seg);
939 if (left_y1 < right_y1)
941 /* Test left (x1, y1) against right segment */
942 double left_x1 = left_seg->x[1];
945 right_seg->x[(right_seg->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1] ||
946 left_y1 == right_seg->y0)
948 d = left_x1 * right_seg->a + left_y1 * right_seg->b + right_seg->c;
951 else if (d < EPSILON_A)
953 double right_x1 = art_svp_intersect_break (ctx, right_seg,
956 if (left_x1 <= right_x1)
960 else if (left_y1 > right_y1)
962 /* Test right (x1, y1) against left segment */
963 double right_x1 = right_seg->x[1];
965 if (right_x1 > left_seg->x[left_seg->flags & ART_ACTIVE_FLAGS_BNEG] ||
966 right_y1 == left_seg->y0)
968 d = right_x1 * left_seg->a + right_y1 * left_seg->b + left_seg->c;
971 else if (d > -EPSILON_A)
973 double left_x1 = art_svp_intersect_break (ctx, left_seg,
976 if (left_x1 <= right_x1)
980 else /* left_y1 == right_y1 */
982 double left_x1 = left_seg->x[1];
983 double right_x1 = right_seg->x[1];
985 if (left_x1 <= right_x1)
989 /* The segments cross. Find the intersection point. */
991 in_seg = left_seg->in_seg;
992 in_curs = left_seg->in_curs;
993 left_x0 = in_seg->points[in_curs - 1].x;
994 left_y0 = in_seg->points[in_curs - 1].y;
995 left_x1 = in_seg->points[in_curs].x;
996 left_y1 = in_seg->points[in_curs].y;
997 d0 = left_x0 * right_seg->a + left_y0 * right_seg->b + right_seg->c;
998 d1 = left_x1 * right_seg->a + left_y1 * right_seg->b + right_seg->c;
1006 /* Is this division always safe? It could possibly overflow. */
1020 x = left_x0 + t * (left_x1 - left_x0);
1021 y = left_y0 + t * (left_y1 - left_y0);
1025 /* Make sure intersection point is within bounds of right seg. */
1026 if (y < right_seg->y0)
1028 x = right_seg->x[0];
1031 else if (y > right_seg->y1)
1033 x = right_seg->x[1];
1036 else if (x < right_seg->x[(right_seg->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1])
1037 x = right_seg->x[(right_seg->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1];
1038 else if (x > right_seg->x[right_seg->flags & ART_ACTIVE_FLAGS_BNEG])
1039 x = right_seg->x[right_seg->flags & ART_ACTIVE_FLAGS_BNEG];
1041 if (y == left_seg->y0)
1043 if (y != right_seg->y0)
1046 art_dprint ("art_svp_intersect_test_cross: intersection (%g, %g) matches former y0 of %lx, %lx\n",
1047 x, y, (unsigned long)left_seg, (unsigned long)right_seg);
1049 art_svp_intersect_push_pt (ctx, right_seg, x, y);
1050 if ((break_flags & ART_BREAK_RIGHT) && right_seg->right != NULL)
1051 art_svp_intersect_add_point (ctx, x, y, right_seg->right,
1056 /* Intersection takes place at current scan line; process
1057 immediately rather than queueing intersection point into
1059 ArtActiveSeg *winner, *loser;
1061 /* Choose "most vertical" segement */
1062 if (left_seg->a > right_seg->a)
1073 loser->x[0] = winner->x[0];
1074 loser->horiz_x = loser->x[0];
1075 loser->horiz_delta_wind += loser->delta_wind;
1076 winner->horiz_delta_wind -= loser->delta_wind;
1078 art_svp_intersect_swap_active (ctx, left_seg, right_seg);
1082 else if (y == right_seg->y0)
1085 art_dprint ("*** art_svp_intersect_test_cross: intersection (%g, %g) matches latter y0 of %lx, %lx\n",
1086 x, y, (unsigned long)left_seg, (unsigned long)right_seg);
1088 art_svp_intersect_push_pt (ctx, left_seg, x, y);
1089 if ((break_flags & ART_BREAK_LEFT) && left_seg->left != NULL)
1090 art_svp_intersect_add_point (ctx, x, y, left_seg->left,
1096 art_dprint ("Inserting (%g, %g) into %lx, %lx\n",
1097 x, y, (unsigned long)left_seg, (unsigned long)right_seg);
1099 /* Insert the intersection point into both segments. */
1100 art_svp_intersect_push_pt (ctx, left_seg, x, y);
1101 art_svp_intersect_push_pt (ctx, right_seg, x, y);
1102 if ((break_flags & ART_BREAK_LEFT) && left_seg->left != NULL)
1103 art_svp_intersect_add_point (ctx, x, y, left_seg->left, break_flags);
1104 if ((break_flags & ART_BREAK_RIGHT) && right_seg->right != NULL)
1105 art_svp_intersect_add_point (ctx, x, y, right_seg->right, break_flags);
1111 * art_svp_intersect_active_delete: Delete segment from active list.
1112 * @ctx: Intersection context.
1113 * @seg: Segment to delete.
1115 * Deletes @seg from the active list.
1117 static /* todo inline */ void
1118 art_svp_intersect_active_delete (ArtIntersectCtx *ctx, ArtActiveSeg *seg)
1120 ArtActiveSeg *left = seg->left, *right = seg->right;
1123 left->right = right;
1125 ctx->active_head = right;
1131 * art_svp_intersect_active_free: Free an active segment.
1132 * @seg: Segment to delete.
1136 static /* todo inline */ void
1137 art_svp_intersect_active_free (ArtActiveSeg *seg)
1139 art_free (seg->stack);
1141 art_dprint ("Freeing %lx\n", (unsigned long) seg);
1147 * art_svp_intersect_insert_cross: Test crossings of newly inserted line.
1149 * Tests @seg against its left and right neighbors for intersections.
1150 * Precondition: the line in @seg is not purely horizontal.
1153 art_svp_intersect_insert_cross (ArtIntersectCtx *ctx,
1156 ArtActiveSeg *left = seg, *right = seg;
1162 ArtActiveSeg *leftc;
1164 for (leftc = left->left; leftc != NULL; leftc = leftc->left)
1165 if (!(leftc->flags & ART_ACTIVE_FLAGS_DEL))
1167 if (leftc != NULL &&
1168 art_svp_intersect_test_cross (ctx, leftc, left,
1171 if (left == right || right == NULL)
1172 right = left->right;
1179 else if (right != NULL && right->right != NULL)
1181 ArtActiveSeg *rightc;
1183 for (rightc = right->right; rightc != NULL; rightc = rightc->right)
1184 if (!(rightc->flags & ART_ACTIVE_FLAGS_DEL))
1186 if (rightc != NULL &&
1187 art_svp_intersect_test_cross (ctx, right, rightc,
1190 if (left == right || left == NULL)
1204 * art_svp_intersect_horiz: Add horizontal line segment.
1205 * @ctx: Intersector context.
1206 * @seg: Segment on which to add horizontal line.
1207 * @x0: Old x position.
1208 * @x1: New x position.
1210 * Adds a horizontal line from @x0 to @x1, and updates the current
1211 * location of @seg to @x1.
1214 art_svp_intersect_horiz (ArtIntersectCtx *ctx, ArtActiveSeg *seg,
1215 double x0, double x1)
1222 hs = art_new (ArtActiveSeg, 1);
1224 hs->flags = ART_ACTIVE_FLAGS_DEL | (seg->flags & ART_ACTIVE_FLAGS_OUT);
1225 if (seg->flags & ART_ACTIVE_FLAGS_OUT)
1227 ArtSvpWriter *swr = ctx->out;
1229 swr->add_point (swr, seg->seg_id, x0, ctx->y);
1231 hs->seg_id = seg->seg_id;
1233 hs->horiz_delta_wind = seg->delta_wind;
1236 /* Ideally, the (a, b, c) values will never be read. However, there
1237 are probably some tests remaining that don't check for _DEL
1238 before evaluating the line equation. For those, these
1239 initializations will at least prevent a UMR of the values, which
1240 can crash on some platforms. */
1245 seg->horiz_delta_wind -= seg->delta_wind;
1247 art_svp_intersect_add_horiz (ctx, hs);
1252 art_boolean first = ART_TRUE;
1254 for (left = seg->left; left != NULL; left = seg->left)
1256 int left_bneg = left->flags & ART_ACTIVE_FLAGS_BNEG;
1258 if (left->x[left_bneg] <= x1)
1260 if (left->x[left_bneg ^ 1] <= x1 &&
1261 x1 * left->a + ctx->y * left->b + left->c >= 0)
1263 if (left->y0 != ctx->y && left->y1 != ctx->y)
1265 art_svp_intersect_break (ctx, left, x1, ctx->y, ART_BREAK_LEFT);
1268 art_dprint ("x0=%g > x1=%g, swapping %lx, %lx\n",
1269 x0, x1, (unsigned long)left, (unsigned long)seg);
1271 art_svp_intersect_swap_active (ctx, left, seg);
1272 if (first && left->right != NULL)
1274 art_svp_intersect_test_cross (ctx, left, left->right,
1282 ArtActiveSeg *right;
1283 art_boolean first = ART_TRUE;
1285 for (right = seg->right; right != NULL; right = seg->right)
1287 int right_bneg = right->flags & ART_ACTIVE_FLAGS_BNEG;
1289 if (right->x[right_bneg ^ 1] >= x1)
1291 if (right->x[right_bneg] >= x1 &&
1292 x1 * right->a + ctx->y * right->b + right->c <= 0)
1294 if (right->y0 != ctx->y && right->y1 != ctx->y)
1296 art_svp_intersect_break (ctx, right, x1, ctx->y,
1300 art_dprint ("[right]x0=%g < x1=%g, swapping %lx, %lx\n",
1301 x0, x1, (unsigned long)seg, (unsigned long)right);
1303 art_svp_intersect_swap_active (ctx, seg, right);
1304 if (first && right->left != NULL)
1306 art_svp_intersect_test_cross (ctx, right->left, right,
1316 seg->flags &= ~ART_ACTIVE_FLAGS_OUT;
1320 * art_svp_intersect_insert_line: Insert a line into the active list.
1321 * @ctx: Intersector context.
1322 * @seg: Segment containing line to insert.
1324 * Inserts the line into the intersector context, taking care of any
1325 * intersections, and adding the appropriate horizontal points to the
1329 art_svp_intersect_insert_line (ArtIntersectCtx *ctx, ArtActiveSeg *seg)
1331 if (seg->y1 == seg->y0)
1334 art_dprint ("art_svp_intersect_insert_line: %lx is horizontal\n",
1335 (unsigned long)seg);
1337 art_svp_intersect_horiz (ctx, seg, seg->x[0], seg->x[1]);
1341 art_svp_intersect_insert_cross (ctx, seg);
1342 art_svp_intersect_add_horiz (ctx, seg);
1347 art_svp_intersect_process_intersection (ArtIntersectCtx *ctx,
1350 int n_stack = --seg->n_stack;
1351 seg->x[1] = seg->stack[n_stack - 1].x;
1352 seg->y1 = seg->stack[n_stack - 1].y;
1353 seg->x[0] = seg->stack[n_stack].x;
1354 seg->y0 = seg->stack[n_stack].y;
1355 seg->horiz_x = seg->x[0];
1356 art_svp_intersect_insert_line (ctx, seg);
1360 art_svp_intersect_advance_cursor (ArtIntersectCtx *ctx, ArtActiveSeg *seg,
1361 ArtPriPoint *pri_pt)
1363 const ArtSVPSeg *in_seg = seg->in_seg;
1364 int in_curs = seg->in_curs;
1365 ArtSvpWriter *swr = seg->flags & ART_ACTIVE_FLAGS_OUT ? ctx->out : NULL;
1368 swr->add_point (swr, seg->seg_id, seg->x[1], seg->y1);
1369 if (in_curs + 1 == in_seg->n_points)
1371 ArtActiveSeg *left = seg->left, *right = seg->right;
1375 swr->close_segment (swr, seg->seg_id);
1376 seg->flags &= ~ART_ACTIVE_FLAGS_OUT;
1378 seg->flags |= ART_ACTIVE_FLAGS_DEL;
1379 art_svp_intersect_add_horiz (ctx, seg);
1380 art_svp_intersect_active_delete (ctx, seg);
1381 if (left != NULL && right != NULL)
1382 art_svp_intersect_test_cross (ctx, left, right,
1383 ART_BREAK_LEFT | ART_BREAK_RIGHT);
1388 seg->horiz_x = seg->x[1];
1390 art_svp_intersect_setup_seg (seg, pri_pt);
1391 art_pri_insert (ctx->pq, pri_pt);
1392 art_svp_intersect_insert_line (ctx, seg);
1397 art_svp_intersect_add_seg (ArtIntersectCtx *ctx, const ArtSVPSeg *in_seg)
1399 ArtActiveSeg *seg = art_new (ArtActiveSeg, 1);
1402 ArtActiveSeg *beg_range;
1403 ArtActiveSeg *last = NULL;
1404 ArtActiveSeg *left, *right;
1405 ArtPriPoint *pri_pt = art_new (ArtPriPoint, 1);
1408 seg->in_seg = in_seg;
1411 seg->n_stack_max = 4;
1412 seg->stack = art_new (ArtPoint, seg->n_stack_max);
1414 seg->horiz_delta_wind = 0;
1418 pri_pt->user_data = seg;
1419 art_svp_intersect_setup_seg (seg, pri_pt);
1420 art_pri_insert (ctx->pq, pri_pt);
1422 /* Find insertion place for new segment */
1423 /* This is currently a left-to-right scan, but should be replaced
1424 with a binary search as soon as it's validated. */
1426 x0 = in_seg->points[0].x;
1427 y0 = in_seg->points[0].y;
1429 for (test = ctx->active_head; test != NULL; test = test->right)
1432 int test_bneg = test->flags & ART_ACTIVE_FLAGS_BNEG;
1434 if (x0 < test->x[test_bneg])
1436 if (x0 < test->x[test_bneg ^ 1])
1438 d = x0 * test->a + y0 * test->b + test->c;
1445 left = art_svp_intersect_add_point (ctx, x0, y0, last, ART_BREAK_LEFT | ART_BREAK_RIGHT);
1449 right = ctx->active_head;
1450 ctx->active_head = seg;
1454 right = left->right;
1461 seg->delta_wind = in_seg->dir ? 1 : -1;
1464 art_svp_intersect_insert_line (ctx, seg);
1469 art_svp_intersect_sanitycheck_winding (ArtIntersectCtx *ctx)
1472 /* At this point, we seem to be getting false positives, so it's
1473 turned off for now. */
1476 int winding_number = 0;
1478 for (seg = ctx->active_head; seg != NULL; seg = seg->right)
1480 /* Check winding number consistency. */
1481 if (seg->flags & ART_ACTIVE_FLAGS_OUT)
1483 if (winding_number != seg->wind_left)
1484 art_warn ("*** art_svp_intersect_sanitycheck_winding: seg %lx has wind_left of %d, expected %d\n",
1485 (unsigned long) seg, seg->wind_left, winding_number);
1486 winding_number = seg->wind_left + seg->delta_wind;
1489 if (winding_number != 0)
1490 art_warn ("*** art_svp_intersect_sanitycheck_winding: non-balanced winding number %d\n",
1497 * art_svp_intersect_horiz_commit: Commit points in horiz list to output.
1498 * @ctx: Intersection context.
1500 * The main function of the horizontal commit is to output new
1501 * points to the output writer.
1503 * This "commit" pass is also where winding numbers are assigned,
1504 * because doing it here provides much greater tolerance for inputs
1505 * which are not in strict SVP order.
1507 * Each cluster in the horiz_list contains both segments that are in
1508 * the active list (ART_ACTIVE_FLAGS_DEL is false) and that are not,
1509 * and are scheduled to be deleted (ART_ACTIVE_FLAGS_DEL is true). We
1510 * need to deal with both.
1513 art_svp_intersect_horiz_commit (ArtIntersectCtx *ctx)
1516 int winding_number = 0; /* initialization just to avoid warning */
1518 double last_x = 0; /* initialization just to avoid warning */
1521 art_dprint ("art_svp_intersect_horiz_commit: y=%g\n", ctx->y);
1522 for (seg = ctx->horiz_first; seg != NULL; seg = seg->horiz_right)
1523 art_dprint (" %lx: %g %+d\n",
1524 (unsigned long)seg, seg->horiz_x, seg->horiz_delta_wind);
1527 /* Output points to svp writer. */
1528 for (seg = ctx->horiz_first; seg != NULL;)
1530 /* Find a cluster with common horiz_x, */
1532 double x = seg->horiz_x;
1534 /* Generate any horizontal segments. */
1535 if (horiz_wind != 0)
1537 ArtSvpWriter *swr = ctx->out;
1540 seg_id = swr->add_segment (swr, winding_number, horiz_wind,
1542 swr->add_point (swr, seg_id, x, ctx->y);
1543 swr->close_segment (swr, seg_id);
1546 /* Find first active segment in cluster. */
1548 for (curs = seg; curs != NULL && curs->horiz_x == x;
1549 curs = curs->horiz_right)
1550 if (!(curs->flags & ART_ACTIVE_FLAGS_DEL))
1553 if (curs != NULL && curs->horiz_x == x)
1555 /* There exists at least one active segment in this cluster. */
1557 /* Find beginning of cluster. */
1558 for (; curs->left != NULL; curs = curs->left)
1559 if (curs->left->horiz_x != x)
1562 if (curs->left != NULL)
1563 winding_number = curs->left->wind_left + curs->left->delta_wind;
1570 art_dprint (" curs %lx: winding_number = %d += %d\n",
1571 (unsigned long)curs, winding_number, curs->delta_wind);
1573 if (!(curs->flags & ART_ACTIVE_FLAGS_OUT) ||
1574 curs->wind_left != winding_number)
1576 ArtSvpWriter *swr = ctx->out;
1578 if (curs->flags & ART_ACTIVE_FLAGS_OUT)
1580 swr->add_point (swr, curs->seg_id,
1581 curs->horiz_x, ctx->y);
1582 swr->close_segment (swr, curs->seg_id);
1585 curs->seg_id = swr->add_segment (swr, winding_number,
1588 curs->flags |= ART_ACTIVE_FLAGS_OUT;
1590 curs->wind_left = winding_number;
1591 winding_number += curs->delta_wind;
1594 while (curs != NULL && curs->horiz_x == x);
1597 /* Skip past cluster. */
1600 ArtActiveSeg *next = seg->horiz_right;
1602 seg->flags &= ~ART_ACTIVE_FLAGS_IN_HORIZ;
1603 horiz_wind += seg->horiz_delta_wind;
1604 seg->horiz_delta_wind = 0;
1605 if (seg->flags & ART_ACTIVE_FLAGS_DEL)
1607 if (seg->flags & ART_ACTIVE_FLAGS_OUT)
1609 ArtSvpWriter *swr = ctx->out;
1610 swr->close_segment (swr, seg->seg_id);
1612 art_svp_intersect_active_free (seg);
1616 while (seg != NULL && seg->horiz_x == x);
1620 ctx->horiz_first = NULL;
1621 ctx->horiz_last = NULL;
1623 art_svp_intersect_sanitycheck_winding (ctx);
1629 art_svp_intersect_print_active (ArtIntersectCtx *ctx)
1633 art_dprint ("Active list (y = %g):\n", ctx->y);
1634 for (seg = ctx->active_head; seg != NULL; seg = seg->right)
1636 art_dprint (" %lx: (%g, %g)-(%g, %g), (a, b, c) = (%g, %g, %g)\n",
1638 seg->x[0], seg->y0, seg->x[1], seg->y1,
1639 seg->a, seg->b, seg->c);
1646 art_svp_intersect_sanitycheck (ArtIntersectCtx *ctx)
1649 ArtActiveSeg *last = NULL;
1652 for (seg = ctx->active_head; seg != NULL; seg = seg->right)
1654 if (seg->left != last)
1656 art_warn ("*** art_svp_intersect_sanitycheck: last=%lx, seg->left=%lx\n",
1657 (unsigned long)last, (unsigned long)seg->left);
1661 /* pairwise compare with previous seg */
1663 /* First the top. */
1664 if (last->y0 < seg->y0)
1671 /* Then the bottom. */
1672 if (last->y1 < seg->y1)
1675 seg->x[(seg->flags & ART_ACTIVE_FLAGS_BNEG) ^ 1]) ||
1676 last->y1 == seg->y0))
1678 d = last->x[1] * seg->a + last->y1 * seg->b + seg->c;
1679 if (d >= -EPSILON_A)
1680 art_warn ("*** bottom (%g, %g) of %lx is not clear of %lx to right (d = %g)\n",
1681 last->x[1], last->y1, (unsigned long) last,
1682 (unsigned long) seg, d);
1685 else if (last->y1 > seg->y1)
1689 last->x[last->flags & ART_ACTIVE_FLAGS_BNEG]) ||
1690 seg->y1 == last->y0))
1692 d = seg->x[1] * last->a + seg->y1 * last->b + last->c;
1694 art_warn ("*** bottom (%g, %g) of %lx is not clear of %lx to left (d = %g)\n",
1695 seg->x[1], seg->y1, (unsigned long) seg,
1696 (unsigned long) last, d);
1701 if (last->x[1] > seg->x[1])
1702 art_warn ("*** bottoms (%g, %g) of %lx and (%g, %g) of %lx out of order\n",
1703 last->x[1], last->y1, (unsigned long)last,
1704 seg->x[1], seg->y1, (unsigned long)seg);
1713 art_svp_intersector (const ArtSVP *in, ArtSvpWriter *out)
1715 ArtIntersectCtx *ctx;
1717 ArtPriPoint *first_point;
1722 if (in->n_segs == 0)
1725 ctx = art_new (ArtIntersectCtx, 1);
1728 pq = art_pri_new ();
1731 ctx->active_head = NULL;
1733 ctx->horiz_first = NULL;
1734 ctx->horiz_last = NULL;
1737 first_point = art_new (ArtPriPoint, 1);
1738 first_point->x = in->segs[0].points[0].x;
1739 first_point->y = in->segs[0].points[0].y;
1740 first_point->user_data = NULL;
1741 ctx->y = first_point->y;
1742 art_pri_insert (pq, first_point);
1744 while (!art_pri_empty (pq))
1746 ArtPriPoint *pri_point = art_pri_choose (pq);
1747 ArtActiveSeg *seg = (ArtActiveSeg *)pri_point->user_data;
1750 art_dprint ("\nIntersector step %d\n", count++);
1751 art_svp_intersect_print_active (ctx);
1752 art_dprint ("priq choose (%g, %g) %lx\n", pri_point->x, pri_point->y,
1753 (unsigned long)pri_point->user_data);
1756 art_svp_intersect_sanitycheck(ctx);
1759 if (ctx->y != pri_point->y)
1761 art_svp_intersect_horiz_commit (ctx);
1762 ctx->y = pri_point->y;
1767 /* Insert new segment from input */
1768 const ArtSVPSeg *in_seg = &in->segs[ctx->in_curs++];
1769 art_svp_intersect_add_seg (ctx, in_seg);
1770 if (ctx->in_curs < in->n_segs)
1772 const ArtSVPSeg *next_seg = &in->segs[ctx->in_curs];
1773 pri_point->x = next_seg->points[0].x;
1774 pri_point->y = next_seg->points[0].y;
1775 /* user_data is already NULL */
1776 art_pri_insert (pq, pri_point);
1779 art_free (pri_point);
1783 int n_stack = seg->n_stack;
1787 art_svp_intersect_process_intersection (ctx, seg);
1788 art_free (pri_point);
1792 art_svp_intersect_advance_cursor (ctx, seg, pri_point);
1797 art_svp_intersect_horiz_commit (ctx);
1803 #endif /* not TEST_PRIQ */