1 /* Libart_LGPL - library of basic graphic primitives
2 * Copyright (C) 1998-2000 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 /* "Unsort" a sorted vector path into an ordinary vector path. */
22 #include <stdio.h> /* for printf - debugging */
25 #include "art_vpath.h"
27 #include "art_vpath_svp.h"
29 typedef struct _ArtVpathSVPEnd ArtVpathSVPEnd;
31 struct _ArtVpathSVPEnd {
33 int which; /* 0 = top, 1 = bottom */
40 art_vpath_svp_point_compare (double x1, double y1, double x2, double y2)
42 if (y1 - EPSILON > y2) return 1;
43 if (y1 + EPSILON < y2) return -1;
44 if (x1 - EPSILON > x2) return 1;
45 if (x1 + EPSILON < x2) return -1;
50 art_vpath_svp_compare (const void *s1, const void *s2)
52 const ArtVpathSVPEnd *e1 = s1;
53 const ArtVpathSVPEnd *e2 = s2;
55 return art_vpath_svp_point_compare (e1->x, e1->y, e2->x, e2->y);
58 /* Convert from sorted vector path representation into regular
59 vector path representation.
61 Status of this routine:
63 Basic correctness: Only works with closed paths.
65 Numerical stability: Not known to work when more than two segments
68 Speed: Should be pretty good.
70 Precision: Does not degrade precision.
74 * art_vpath_from_svp: Convert from svp to vpath form.
75 * @svp: Original #ArtSVP.
77 * Converts the sorted vector path @svp into standard vpath form.
79 * Return value: the newly allocated vpath.
82 art_vpath_from_svp (const ArtSVP *svp)
84 int n_segs = svp->n_segs;
90 int j = 0; /* Quiet compiler */
93 double last_x, last_y;
97 last_x = 0; /* to eliminate "uninitialized" warning */
100 ends = art_new (ArtVpathSVPEnd, n_segs * 2);
101 for (i = 0; i < svp->n_segs; i++)
105 ends[i * 2].seg_num = i;
106 ends[i * 2].which = 0;
107 ends[i * 2].x = svp->segs[i].points[0].x;
108 ends[i * 2].y = svp->segs[i].points[0].y;
110 lastpt = svp->segs[i].n_points - 1;
111 ends[i * 2 + 1].seg_num = i;
112 ends[i * 2 + 1].which = 1;
113 ends[i * 2 + 1].x = svp->segs[i].points[lastpt].x;
114 ends[i * 2 + 1].y = svp->segs[i].points[lastpt].y;
116 qsort (ends, n_segs * 2, sizeof (ArtVpathSVPEnd), art_vpath_svp_compare);
119 n_new_max = 16; /* I suppose we _could_ estimate this from traversing
120 the svp, so we don't have to reallocate */
121 new = art_new (ArtVpath, n_new_max);
123 visited = art_new (int, n_segs);
124 for (i = 0; i < n_segs; i++)
128 for (i = 0; i < n_segs; i++)
132 /* search for the continuation of the existing subpath */
133 /* This could be a binary search (which is why we sorted, above) */
134 for (j = 0; j < n_segs * 2; j++)
136 if (!visited[ends[j].seg_num] &&
137 art_vpath_svp_point_compare (last_x, last_y,
138 ends[j].x, ends[j].y) == 0)
146 /* start a new subpath */
147 for (j = 0; j < n_segs * 2; j++)
148 if (!visited[ends[j].seg_num])
153 printf ("failure\n");
155 seg_num = ends[j].seg_num;
156 n_points = svp->segs[seg_num].n_points;
157 for (k = 0; k < n_points; k++)
159 pt_num = svp->segs[seg_num].dir ? k : n_points - (1 + k);
164 art_vpath_add_point (&new, &n_new, &n_new_max,
166 svp->segs[seg_num].points[pt_num].x,
167 svp->segs[seg_num].points[pt_num].y);
172 art_vpath_add_point (&new, &n_new, &n_new_max,
174 svp->segs[seg_num].points[pt_num].x,
175 svp->segs[seg_num].points[pt_num].y);
176 if (k == n_points - 1)
178 last_x = svp->segs[seg_num].points[pt_num].x;
179 last_y = svp->segs[seg_num].points[pt_num].y;
180 /* to make more robust, check for meeting first_[xy],
186 visited[seg_num] = 1;
189 art_vpath_add_point (&new, &n_new, &n_new_max,