summaryrefslogtreecommitdiffstats
path: root/art_vpath_svp.c
blob: 000265c37660e67b2566eb3f496d5e7761af2114 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 1998-2000 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* "Unsort" a sorted vector path into an ordinary vector path. */

#include "config.h"
#include "art_vpath_svp.h"

#include <stdio.h> /* for printf - debugging */
#include "art_misc.h"

#include "art_vpath.h"
#include "art_svp.h"

typedef struct _ArtVpathSVPEnd ArtVpathSVPEnd;

struct _ArtVpathSVPEnd {
  int seg_num;
  int which; /* 0 = top, 1 = bottom */
  double x, y;
};

#define EPSILON 1e-6

static int
art_vpath_svp_point_compare (double x1, double y1, double x2, double y2)
{
  if (y1 - EPSILON > y2) return 1;
  if (y1 + EPSILON < y2) return -1;
  if (x1 - EPSILON > x2) return 1;
  if (x1 + EPSILON < x2) return -1;
  return 0;
}

static int
art_vpath_svp_compare (const void *s1, const void *s2)
{
  const ArtVpathSVPEnd *e1 = s1;
  const ArtVpathSVPEnd *e2 = s2;

  return art_vpath_svp_point_compare (e1->x, e1->y, e2->x, e2->y);
}

/* Convert from sorted vector path representation into regular
   vector path representation.

   Status of this routine:

   Basic correctness: Only works with closed paths.

   Numerical stability: Not known to work when more than two segments
   meet at a point.

   Speed: Should be pretty good.

   Precision: Does not degrade precision.

*/
/**
 * art_vpath_from_svp: Convert from svp to vpath form.
 * @svp: Original #ArtSVP.
 *
 * Converts the sorted vector path @svp into standard vpath form.
 *
 * Return value: the newly allocated vpath.
 **/
ArtVpath *
art_vpath_from_svp (const ArtSVP *svp)
{
  int n_segs = svp->n_segs;
  ArtVpathSVPEnd *ends;
  ArtVpath *new;
  int *visited;
  int n_new, n_new_max;
  int i, k;
  int j = 0; /* Quiet compiler */
  int seg_num;
  int first;
  double last_x, last_y;
  int n_points;
  int pt_num;

  last_x = 0; /* to eliminate "uninitialized" warning */
  last_y = 0;

  ends = art_new (ArtVpathSVPEnd, n_segs * 2);
  for (i = 0; i < svp->n_segs; i++)
    {
      int lastpt;

      ends[i * 2].seg_num = i;
      ends[i * 2].which = 0;
      ends[i * 2].x = svp->segs[i].points[0].x;
      ends[i * 2].y = svp->segs[i].points[0].y;

      lastpt = svp->segs[i].n_points - 1;
      ends[i * 2 + 1].seg_num = i;
      ends[i * 2 + 1].which = 1;
      ends[i * 2 + 1].x = svp->segs[i].points[lastpt].x;
      ends[i * 2 + 1].y = svp->segs[i].points[lastpt].y;
    }
  qsort (ends, n_segs * 2, sizeof (ArtVpathSVPEnd), art_vpath_svp_compare);

  n_new = 0;
  n_new_max = 16; /* I suppose we _could_ estimate this from traversing
		     the svp, so we don't have to reallocate */
  new = art_new (ArtVpath, n_new_max);

  visited = art_new (int, n_segs);
  for (i = 0; i < n_segs; i++)
    visited[i] = 0;

  first = 1;
  for (i = 0; i < n_segs; i++)
    {
      if (!first)
	{
	  /* search for the continuation of the existing subpath */
	  /* This could be a binary search (which is why we sorted, above) */
	  for (j = 0; j < n_segs * 2; j++)
	    {
	      if (!visited[ends[j].seg_num] &&
		  art_vpath_svp_point_compare (last_x, last_y,
					       ends[j].x, ends[j].y) == 0)
		break;
	    }
	  if (j == n_segs * 2)
	    first = 1;
	}
      if (first)
	{
	  /* start a new subpath */
	  for (j = 0; j < n_segs * 2; j++)
	    if (!visited[ends[j].seg_num])
	      break;
	}
      if (j == n_segs * 2)
	{
	  printf ("failure\n");
	}
      seg_num = ends[j].seg_num;
      n_points = svp->segs[seg_num].n_points;
      for (k = 0; k < n_points; k++)
	{
	  pt_num = svp->segs[seg_num].dir ? k : n_points - (1 + k);
	  if (k == 0)
	    {
	      if (first)
		{
		  art_vpath_add_point (&new, &n_new, &n_new_max,
				       ART_MOVETO,
				       svp->segs[seg_num].points[pt_num].x,
				       svp->segs[seg_num].points[pt_num].y);
		}
	    }
	  else
	    {
	      art_vpath_add_point (&new, &n_new, &n_new_max,
				   ART_LINETO,
				   svp->segs[seg_num].points[pt_num].x,
				   svp->segs[seg_num].points[pt_num].y);
	      if (k == n_points - 1)
		{
		  last_x = svp->segs[seg_num].points[pt_num].x;
		  last_y = svp->segs[seg_num].points[pt_num].y;
		  /* to make more robust, check for meeting first_[xy],
		     set first if so */
		}
	    }
	  first = 0;
	}
      visited[seg_num] = 1;
    }

  art_vpath_add_point (&new, &n_new, &n_new_max,
		       ART_END, 0, 0);
  art_free (visited);
  art_free (ends);
  return new;
}