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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/* Copyright (C) 2001-2017 Peter Selinger.
This file is part of Potrace. It is free software and it is covered
by the GNU General Public License. See the file COPYING for details. */
/* The DXF backend of Potrace. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "main.h"
#include "backend_dxf.h"
#include "potracelib.h"
#include "lists.h"
#include "auxiliary.h"
#include "trans.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/* ---------------------------------------------------------------------- */
/* auxiliary linear algebra functions */
/* subtract two vectors */
static dpoint_t sub(dpoint_t v, dpoint_t w) {
dpoint_t r;
r.x = v.x - w.x;
r.y = v.y - w.y;
return r;
}
/* inner product */
static double iprod(dpoint_t v, dpoint_t w) {
return v.x * w.x + v.y * w.y;
}
/* cross product */
static double xprod(dpoint_t v, dpoint_t w) {
return v.x * w.y - v.y * w.x;
}
/* calculate the DXF polyline "bulge" value corresponding to the angle
between two vectors. In case of "infinity" return 0.0. */
static double bulge(dpoint_t v, dpoint_t w) {
double v2, w2, vw, vxw, nvw;
v2 = iprod(v, v);
w2 = iprod(w, w);
vw = iprod(v, w);
vxw = xprod(v, w);
nvw = sqrt(v2 * w2);
if (vxw == 0.0) {
return 0.0;
}
return (nvw - vw) / vxw;
}
/* ---------------------------------------------------------------------- */
/* DXF output synthesis */
/* print with group code: the low-level DXF file format */
static int ship(FILE *fout, int gc, const char *fmt, ...) {
va_list args;
int r;
int c;
r = fprintf(fout, "%3d\n", gc);
if (r < 0) {
return r;
}
c = r;
va_start(args, fmt);
r = vfprintf(fout, fmt, args);
va_end(args);
if (r < 0) {
return r;
}
c += r;
r = fprintf(fout, "\n");
if (r < 0) {
return r;
}
c += r;
return c;
}
/* output the start of a polyline */
static void ship_polyline(FILE *fout, const char *layer, int closed) {
ship(fout, 0, "POLYLINE");
ship(fout, 8, "%s", layer);
ship(fout, 66, "%d", 1);
ship(fout, 70, "%d", closed ? 1 : 0);
}
/* output a vertex */
static void ship_vertex(FILE *fout, const char *layer, dpoint_t v, double bulge) {
ship(fout, 0, "VERTEX");
ship(fout, 8, "%s", layer);
ship(fout, 10, "%f", v.x);
ship(fout, 20, "%f", v.y);
ship(fout, 42, "%f", bulge);
}
/* output the end of a polyline */
static void ship_seqend(FILE *fout) {
ship(fout, 0, "SEQEND");
}
/* output a comment */
static void ship_comment(FILE *fout, const char *comment) {
ship(fout, 999, "%s", comment);
}
/* output the start of a section */
static void ship_section(FILE *fout, const char *name) {
ship(fout, 0, "SECTION");
ship(fout, 2, "%s", name);
}
static void ship_endsec(FILE *fout) {
ship(fout, 0, "ENDSEC");
}
static void ship_eof(FILE *fout) {
ship(fout, 0, "EOF");
}
/* ---------------------------------------------------------------------- */
/* Simulated quadratic and bezier curves */
/* Output vertices (with bulges) corresponding to a smooth pair of
circular arcs from A to B, tangent to AC at A and to CB at
B. Segments are meant to be concatenated, so don't output the final
vertex. */
static void pseudo_quad(FILE *fout, const char *layer, dpoint_t A, dpoint_t C, dpoint_t B) {
dpoint_t v, w;
double v2, w2, vw, vxw, nvw;
double a, b, c, y;
dpoint_t G;
double bulge1, bulge2;
v = sub(A, C);
w = sub(B, C);
v2 = iprod(v, v);
w2 = iprod(w, w);
vw = iprod(v, w);
vxw = xprod(v, w);
nvw = sqrt(v2 * w2);
a = v2 + 2*vw + w2;
b = v2 + 2*nvw + w2;
c = 4*nvw;
if (vxw == 0 || a == 0) {
goto degenerate;
}
/* assert: a,b,c >= 0, b*b - a*c >= 0, and 0 <= b - sqrt(b*b - a*c) <= a */
y = (b - sqrt(b*b - a*c)) / a;
G = interval(y, C, interval(0.5, A, B));
bulge1 = bulge(sub(A,G), v);
bulge2 = bulge(w, sub(B,G));
ship_vertex(fout, layer, A, -bulge1);
ship_vertex(fout, layer, G, -bulge2);
return;
degenerate:
ship_vertex(fout, layer, A, 0);
return;
}
/* produce a smooth from A to D, tangent to AB at A and to CD at D.
This is similar to a Bezier curve, except that our curve will be
made up of up to 4 circular arcs. This is particularly intended for
the case when AD and BC are parallel. Like arcs(), don't output the
final vertex. */
static void pseudo_bezier(FILE *fout, const char *layer, dpoint_t A, dpoint_t B, dpoint_t C, dpoint_t D) {
dpoint_t E = interval(0.75, A, B);
dpoint_t G = interval(0.75, D, C);
dpoint_t F = interval(0.5, E, G);
pseudo_quad(fout, layer, A, E, F);
pseudo_quad(fout, layer, F, G, D);
return;
}
/* ---------------------------------------------------------------------- */
/* functions for converting a path to a DXF polyline */
/* do one path. */
static int dxf_path(FILE *fout, const char *layer, potrace_curve_t *curve, trans_t t) {
int i;
dpoint_t *c, *c1;
int n = curve->n;
ship_polyline(fout, layer, 1);
for (i=0; i<n; i++) {
c = curve->c[i];
c1 = curve->c[mod(i-1,n)];
switch (curve->tag[i]) {
case POTRACE_CORNER:
ship_vertex(fout, layer, trans(c1[2], t), 0);
ship_vertex(fout, layer, trans(c[1], t), 0);
break;
case POTRACE_CURVETO:
pseudo_bezier(fout, layer, trans(c1[2], t), trans(c[0], t), trans(c[1], t), trans(c[2], t));
break;
}
}
ship_seqend(fout);
return 0;
}
/* ---------------------------------------------------------------------- */
/* Backend. */
/* public interface for DXF */
int page_dxf(FILE *fout, potrace_path_t *plist, imginfo_t *imginfo) {
potrace_path_t *p;
trans_t t;
const char *layer = "0";
/* set up the coordinate transform (rotation) */
t.bb[0] = imginfo->trans.bb[0]+imginfo->lmar+imginfo->rmar;
t.bb[1] = imginfo->trans.bb[1]+imginfo->tmar+imginfo->bmar;
t.orig[0] = imginfo->trans.orig[0]+imginfo->lmar;
t.orig[1] = imginfo->trans.orig[1]+imginfo->bmar;
t.x[0] = imginfo->trans.x[0];
t.x[1] = imginfo->trans.x[1];
t.y[0] = imginfo->trans.y[0];
t.y[1] = imginfo->trans.y[1];
ship_comment(fout, "DXF data, created by " POTRACE " " VERSION ", written by Peter Selinger 2001-2017");
/* header section */
ship_section(fout, "HEADER");
/* variables */
ship(fout, 9, "$ACADVER");
ship(fout, 1, "AC1006");
ship(fout, 9, "$EXTMIN");
ship(fout, 10, "%f", 0.0);
ship(fout, 20, "%f", 0.0);
ship(fout, 30, "%f", 0.0);
ship(fout, 9, "$EXTMAX");
ship(fout, 10, "%f", t.bb[0]);
ship(fout, 20, "%f", t.bb[1]);
ship(fout, 30, "%f", 0.0);
ship_endsec(fout);
/* entities section */
ship_section(fout, "ENTITIES");
/* write paths */
list_forall (p, plist) {
dxf_path(fout, layer, &p->curve, t);
}
ship_endsec(fout);
ship_eof(fout);
fflush(fout);
return 0;
}
|