The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_vpath_svp.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998-2000 Raph Levien
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /* "Unsort" a sorted vector path into an ordinary vector path. */
21
22 #include <stdio.h> /* for printf - debugging */
23 #include "art_misc.h"
24
25 #include "art_vpath.h"
26 #include "art_svp.h"
27 #include "art_vpath_svp.h"
28
29 typedef struct _ArtVpathSVPEnd ArtVpathSVPEnd;
30
31 struct _ArtVpathSVPEnd {
32   int seg_num;
33   int which; /* 0 = top, 1 = bottom */
34   double x, y;
35 };
36
37 #define EPSILON 1e-6
38
39 static int
40 art_vpath_svp_point_compare (double x1, double y1, double x2, double y2)
41 {
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;
46   return 0;
47 }
48
49 static int
50 art_vpath_svp_compare (const void *s1, const void *s2)
51 {
52   const ArtVpathSVPEnd *e1 = s1;
53   const ArtVpathSVPEnd *e2 = s2;
54
55   return art_vpath_svp_point_compare (e1->x, e1->y, e2->x, e2->y);
56 }
57
58 /* Convert from sorted vector path representation into regular
59    vector path representation.
60
61    Status of this routine:
62
63    Basic correctness: Only works with closed paths.
64
65    Numerical stability: Not known to work when more than two segments
66    meet at a point.
67
68    Speed: Should be pretty good.
69
70    Precision: Does not degrade precision.
71
72 */
73 /**
74  * art_vpath_from_svp: Convert from svp to vpath form.
75  * @svp: Original #ArtSVP.
76  *
77  * Converts the sorted vector path @svp into standard vpath form.
78  *
79  * Return value: the newly allocated vpath.
80  **/
81 ArtVpath *
82 art_vpath_from_svp (const ArtSVP *svp)
83 {
84   int n_segs = svp->n_segs;
85   ArtVpathSVPEnd *ends;
86   ArtVpath *new;
87   int *visited;
88   int n_new, n_new_max;
89   int i, k;
90   int j = 0; /* Quiet compiler */
91   int seg_num;
92   int first;
93   double last_x, last_y;
94   int n_points;
95   int pt_num;
96
97   last_x = 0; /* to eliminate "uninitialized" warning */
98   last_y = 0;
99
100   ends = art_new (ArtVpathSVPEnd, n_segs * 2);
101   for (i = 0; i < svp->n_segs; i++)
102     {
103       int lastpt;
104
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;
109
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;
115     }
116   qsort (ends, n_segs * 2, sizeof (ArtVpathSVPEnd), art_vpath_svp_compare);
117
118   n_new = 0;
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);
122
123   visited = art_new (int, n_segs);
124   for (i = 0; i < n_segs; i++)
125     visited[i] = 0;
126
127   first = 1;
128   for (i = 0; i < n_segs; i++)
129     {
130       if (!first)
131         {
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++)
135             {
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)
139                 break;
140             }
141           if (j == n_segs * 2)
142             first = 1;
143         }
144       if (first)
145         {
146           /* start a new subpath */
147           for (j = 0; j < n_segs * 2; j++)
148             if (!visited[ends[j].seg_num])
149               break;
150         }
151       if (j == n_segs * 2)
152         {
153           printf ("failure\n");
154         }
155       seg_num = ends[j].seg_num;
156       n_points = svp->segs[seg_num].n_points;
157       for (k = 0; k < n_points; k++)
158         {
159           pt_num = svp->segs[seg_num].dir ? k : n_points - (1 + k);
160           if (k == 0)
161             {
162               if (first)
163                 {
164                   art_vpath_add_point (&new, &n_new, &n_new_max,
165                                        ART_MOVETO,
166                                        svp->segs[seg_num].points[pt_num].x,
167                                        svp->segs[seg_num].points[pt_num].y);
168                 }
169             }
170           else
171             {
172               art_vpath_add_point (&new, &n_new, &n_new_max,
173                                    ART_LINETO,
174                                    svp->segs[seg_num].points[pt_num].x,
175                                    svp->segs[seg_num].points[pt_num].y);
176               if (k == n_points - 1)
177                 {
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],
181                      set first if so */
182                 }
183             }
184           first = 0;
185         }
186       visited[seg_num] = 1;
187     }
188
189   art_vpath_add_point (&new, &n_new, &n_new_max,
190                        ART_END, 0, 0);
191   art_free (visited);
192   art_free (ends);
193   return new;
194 }