Debian Codegraph @

converter/other/pamtosvg/curve.c

raw symbols SCIP
1 /* curve.c: operations on the lists of pixels and lists of curves.
2
3 The code was partially derived from limn.
4
5 Copyright (C) 1992 Free Software Foundation, Inc.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include <assert.h>
22
23 #include "mallocvar.h"
24
25 #include "logreport.h"
26 #include "curve.h"
27
28
29 static Point
30 realCoordFromInt(pm_pixelcoord const int_coord) {
31 /*----------------------------------------------------------------------------
32 Turn an integer point into a real one.
33 -----------------------------------------------------------------------------*/
34 Point real_coord;
35
36 real_coord.x = int_coord.col;
37 real_coord.y = int_coord.row;
38 real_coord.z = 0.0;
39
40 return real_coord;
41 }
42
43
44
45 Curve *
46 curve_new(void) {
47 /*----------------------------------------------------------------------------
48 A new, entirely empty curve.
49 -----------------------------------------------------------------------------*/
50 Curve * curveP;
51
52 MALLOCVAR_NOFAIL(curveP);
53
54 curveP->pointList = NULL;
55 CURVE_LENGTH(curveP) = 0;
56 CURVE_CYCLIC(curveP) = false;
57 PREVIOUS_CURVE(curveP) = NULL;
58 NEXT_CURVE(curveP) = NULL;
59
60 return curveP;
61 }
62
63
64
65 Curve *
66 curve_copyMost(Curve * const oldCurveP) {
67 /*----------------------------------------------------------------------------
68 New curve that is the same as *curveP, except without any points.
69
70 Don't copy the points or tangents, but copy everything else.
71 -----------------------------------------------------------------------------*/
72 Curve * curveP = curve_new();
73
74 CURVE_CYCLIC(curveP) = CURVE_CYCLIC(oldCurveP);
75 PREVIOUS_CURVE(curveP) = PREVIOUS_CURVE(oldCurveP);
76 NEXT_CURVE(curveP) = NEXT_CURVE(oldCurveP);
77
78 return curveP;
79 }
80
81
82
83 void
84 curve_move(Curve * const dstP,
85 Curve * const srcP) {
86 /*----------------------------------------------------------------------------
87 Move ownership of dynamically allocated memory from source to destination;
88 destroy source.
89 -----------------------------------------------------------------------------*/
90 if (CURVE_LENGTH(dstP) > 0)
91 free(dstP->pointList);
92
93 *dstP = *srcP;
94
95 free(srcP);
96 }
97
98
99
100 void
101 curve_free(Curve * const curveP) {
102
103 /* The length of CURVE will be zero if we ended up not being able to fit
104 it (which in turn implies a problem elsewhere in the program, but at
105 any rate, we shouldn't try here to free the nonexistent curve).
106 */
107
108 if (CURVE_LENGTH(curveP) > 0)
109 free(curveP->pointList);
110
111 free(curveP);
112 }
113
114
115
116 void
117 curve_appendPoint(Curve * const curveP,
118 Point const coord) {
119 /*----------------------------------------------------------------------------
120 Like `append_pixel', for a point in real coordinates.
121 -----------------------------------------------------------------------------*/
122 CURVE_LENGTH(curveP)++;
123 REALLOCARRAY_NOFAIL(curveP->pointList, CURVE_LENGTH(curveP));
124 LAST_CURVE_POINT(curveP) = coord;
125 /* The t value does not need to be set. */
126 }
127
128
129
130 void
131 curve_appendPixel(Curve * const curveP,
132 pm_pixelcoord const coord) {
133 /*----------------------------------------------------------------------------
134 Append the point 'coord' to the end of *curveP's list.
135 -----------------------------------------------------------------------------*/
136 curve_appendPoint(curveP, realCoordFromInt(coord));
137 }
138
139
140
141 void
142 curve_setDistance(Curve * const curveP) {
143 /*----------------------------------------------------------------------------
144 Fill in the distance values in *curveP.
145
146 The distance value for point P on a curve is the distance P is along the
147 curve from the initial point, normalized so the entire curve is length 1.0
148 (i.e. t of the initial point is 0.0; t of the final point is 1.0).
149
150 There are a lot of curves that pass through the points indicated by
151 *curveP, but for practical computation of t, we just take the piecewise
152 linear locus that runs through all of them. That means we can just step
153 through *curveP, adding up the distance from one point to the next to get
154 the t value for each point.
155
156 This is the "chord-length parameterization" method, which is described in
157 Plass & Stone.
158 -----------------------------------------------------------------------------*/
159 unsigned int p;
160
161 LOG("\nAssigning initial t values:\n ");
162
163 /* Algorithm: We do a pass through the curve establishing how far each
164 point is along the curve in absolute terms, and then another pass
165 to normalize those distances to the fraction of the curve (i.e.
166 if the curve is 5 units long and Point P is 2 units in, we record
167 2 units for Point P in the first pass, and then compute 0.2 as the
168 fraction of the curve length in the second pass.
169
170 In the first pass, we abuse the distance property of the CurvePoint
171 object to remember the unnormalized distances.
172 */
173
174 CURVE_DIST(curveP, 0) = 0.0;
175
176 for (p = 1; p < CURVE_LENGTH(curveP); ++p) {
177 Point const point = CURVE_POINT(curveP, p);
178 Point const previous_p = CURVE_POINT(curveP, p - 1);
179 float const d = point_distance(point, previous_p);
180 CURVE_DIST(curveP, p) = CURVE_DIST(curveP, p - 1) + d;
181 }
182
183 assert(LAST_CURVE_DIST(curveP) != 0.0);
184
185 /* Normalize to a curve length of 1.0 */
186
187 for (p = 1; p < CURVE_LENGTH(curveP); ++p)
188 CURVE_DIST(curveP, p) =
189 CURVE_DIST(curveP, p) / LAST_CURVE_DIST(curveP);
190
191 curve_logEntire(curveP);
192 }
193
194
195
196 #define NUM_TO_PRINT 3
197
198 #define LOG_CURVE_POINT(c, p, printDistance) \
199 do \
200 { \
201 LOG2 ("(%.3f,%.3f)", CURVE_POINT (c, p).x, CURVE_POINT (c, p).y); \
202 if (printDistance) \
203 LOG1 ("/%.2f", CURVE_DIST(c, p)); \
204 } \
205 while (0)
206
207
208
209 void
210 curve_log(Curve * const curveP,
211 bool const printDistance) {
212 /*----------------------------------------------------------------------------
213 Print a curve in human-readable form. It turns out we never care
214 about most of the points on the curve, and so it is pointless to
215 print them all out umpteen times. What matters is that we have some
216 from the end and some from the beginning.
217 -----------------------------------------------------------------------------*/
218 if (!log_file)
219 return;
220
221 LOG1("curve id = %lx:\n", (unsigned long) curveP);
222 LOG1(" length = %u.\n", CURVE_LENGTH(curveP));
223 if (CURVE_CYCLIC(curveP))
224 LOG(" cyclic.\n");
225
226 LOG(" ");
227
228 /* If the curve is short enough, don't use ellipses. */
229 if (CURVE_LENGTH(curveP) <= NUM_TO_PRINT * 2) {
230 unsigned int thisPoint;
231
232 for (thisPoint = 0; thisPoint < CURVE_LENGTH(curveP); ++thisPoint) {
233 LOG_CURVE_POINT(curveP, thisPoint, printDistance);
234 LOG(" ");
235
236 if (thisPoint != CURVE_LENGTH(curveP) - 1
237 && (thisPoint + 1) % NUM_TO_PRINT == 0)
238 LOG("\n ");
239 }
240 } else {
241 unsigned int thisPoint;
242 for (thisPoint = 0;
243 thisPoint < NUM_TO_PRINT && thisPoint < CURVE_LENGTH(curveP);
244 ++thisPoint) {
245 LOG_CURVE_POINT(curveP, thisPoint, printDistance);
246 LOG(" ");
247 }
248
249 LOG("...\n ...");
250
251 for (thisPoint = CURVE_LENGTH(curveP) - NUM_TO_PRINT;
252 thisPoint < CURVE_LENGTH(curveP);
253 ++thisPoint) {
254 LOG(" ");
255 LOG_CURVE_POINT(curveP, thisPoint, printDistance);
256 }
257 }
258 LOG(".\n");
259 }
260
261
262
263 void
264 curve_logEntire(Curve * const curveP) {
265 /*----------------------------------------------------------------------------
266 Like `log_curve', but write the whole thing.
267 -----------------------------------------------------------------------------*/
268 unsigned int thisPoint;
269
270 if (!log_file)
271 return;
272
273 LOG1("curve id = %lx:\n", (unsigned long) curveP);
274 LOG1(" length = %u.\n", CURVE_LENGTH(curveP));
275 if (CURVE_CYCLIC(curveP))
276 LOG(" cyclic.\n");
277
278 LOG(" ");
279
280 for (thisPoint = 0; thisPoint < CURVE_LENGTH(curveP); ++thisPoint) {
281 LOG(" ");
282 LOG_CURVE_POINT(curveP, thisPoint, true);
283 /* Compiler warning `Condition is always true' can be ignored */
284 }
285
286 LOG(".\n");
287 }
288
289
290
291 CurveList
292 curve_newList(void) {
293 /*----------------------------------------------------------------------------
294 A new initialized but empty curve list.
295 -----------------------------------------------------------------------------*/
296 CurveList curveList;
297
298 curveList.length = 0;
299 curveList.data = NULL;
300
301 return curveList;
302 }
303
304
305
306 void
307 curve_freeList(CurveList * const curveListP) {
308 /*----------------------------------------------------------------------------
309 Free curve list and all the curves it contains.
310 -----------------------------------------------------------------------------*/
311 unsigned int thisCurve;
312
313 for (thisCurve = 0; thisCurve < curveListP->length; ++thisCurve)
314 curve_free(curveListP->data[thisCurve]);
315
316 /* If the character was empty, it won't have any curves. */
317 if (curveListP->data != NULL)
318 free(curveListP->data);
319 }
320
321
322
323 void
324 curve_appendList(CurveList * const curveListP,
325 Curve * const curveP) {
326 /*----------------------------------------------------------------------------
327 Add an element to a curve list.
328 -----------------------------------------------------------------------------*/
329 ++curveListP->length;
330 REALLOCARRAY_NOFAIL(curveListP->data, curveListP->length);
331 curveListP->data[curveListP->length - 1] = curveP;
332 }
333
334
335
336 CurveListArray
337 curve_newListArray(void) {
338 /*----------------------------------------------------------------------------
339 An initialized but empty curve list array.
340 -----------------------------------------------------------------------------*/
341 CurveListArray curveListArray;
342
343 CURVE_LIST_ARRAY_LENGTH(curveListArray) = 0;
344 curveListArray.data = NULL;
345
346 return curveListArray;
347 }
348
349
350
351 void
352 curve_freeListArray(const CurveListArray * const curveListArrayP,
353 at_progress_func notify_progress,
354 void * const clientData) {
355 /*----------------------------------------------------------------------------
356 Free all the curve lists curveListArray contains.
357 -----------------------------------------------------------------------------*/
358 unsigned int thisList;
359
360 for (thisList = 0;
361 thisList < CURVE_LIST_ARRAY_LENGTH(*curveListArrayP);
362 ++thisList) {
363
364 if (notify_progress)
365 notify_progress(((float)thisList)/
366 (CURVE_LIST_ARRAY_LENGTH(*curveListArrayP) *
367 (float)3.0)+(float)0.666 ,
368 clientData);
369 curve_freeList(&CURVE_LIST_ARRAY_ELT(*curveListArrayP, thisList));
370 }
371
372 /* If the character was empty, it won't have any curves. */
373 if (curveListArrayP->data)
374 free(curveListArrayP->data);
375 }
376
377
378
379 void
380 curve_appendArray(CurveListArray * const curveListArrayP,
381 CurveList const curveList) {
382 /*----------------------------------------------------------------------------
383 Add an element to *curveListArrayP.
384 -----------------------------------------------------------------------------*/
385 CURVE_LIST_ARRAY_LENGTH(*curveListArrayP)++;
386 REALLOCARRAY_NOFAIL(curveListArrayP->data,
387 CURVE_LIST_ARRAY_LENGTH(*curveListArrayP));
388 LAST_CURVE_LIST_ARRAY_ELT(*curveListArrayP) = curveList;
389 }
390
391
392