summaryrefslogtreecommitdiff
path: root/test/spline-decomposition.c
blob: ea8f26f23c8a31643fa59e4cabf14e86d871c30c (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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright 2008 Chris Wilson
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Chris Wilson not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Chris Wilson makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairo-test.h"

typedef struct _point {
    double x,y;
} point_t;

typedef struct _knots {
    point_t a,b,c,d;
} knots_t;

static knots_t knots[5] = {
    { {0, 0}, {0, 100}, {100, 100}, {100, 0} },
    { {0, 0}, {75, 100}, {25, 100}, {100, 0} },
    { {0, 0}, {100, 100}, {0, 100}, {100, 0} },
    { {0, 0}, {150, 100}, {-50, 100}, {100, 0} },
    { {0, 0}, {100, 200}, {0, -100}, {100, 100} },
};

#ifdef REFERENCE
static void
_lerp_half (const point_t *a, const point_t *b, point_t *result)
{
    result->x = .5 * (a->x + b->x);
    result->y = .5 * (a->y + b->y);
}

static void
_de_casteljau (knots_t *k1, knots_t *k2)
{
    point_t ab, bc, cd;
    point_t abbc, bccd;
    point_t final;

    _lerp_half (&k1->a, &k1->b, &ab);
    _lerp_half (&k1->b, &k1->c, &bc);
    _lerp_half (&k1->c, &k1->d, &cd);
    _lerp_half (&ab, &bc, &abbc);
    _lerp_half (&bc, &cd, &bccd);
    _lerp_half (&abbc, &bccd, &final);

    k2->a = final;
    k2->b = bccd;
    k2->c = cd;
    k2->d = k1->d;

    k1->b = ab;
    k1->c = abbc;
    k1->d = final;
}

static double
_spline_error_squared (const knots_t *knots)
{
    double bdx, bdy, berr;
    double cdx, cdy, cerr;
    double dx, dy, v;

    /* Intersection point (px):
     *	    px = p1 + u(p2 - p1)
     *	    (p - px) ∙ (p2 - p1) = 0
     * Thus:
     *	    u = ((p - p1) ∙ (p2 - p1)) / ∥p2 - p1∥²;
     */
    bdx = knots->b.x - knots->a.x;
    bdy = knots->b.y - knots->a.y;

    cdx = knots->c.x - knots->a.x;
    cdy = knots->c.y - knots->a.y;

    dx = knots->d.x - knots->a.x;
    dy = knots->d.y - knots->a.y;
    v = dx * dx + dy * dy;
    if (v != 0.) {
	double u;

	u = bdx * dx + bdy * dy;
	if (u <= 0) {
	    /* bdx -= 0;
	     * bdy -= 0;
	     */
	} else if (u >= v) {
	    bdx -= dx;
	    bdy -= dy;
	} else {
	    bdx -= u/v * dx;
	    bdy -= u/v * dy;
	}

	u = cdx * dx + cdy * dy;
	if (u <= 0) {
	    /* cdx -= 0;
	     * cdy -= 0;
	     */
	} else if (u >= v) {
	    cdx -= dx;
	    cdy -= dy;
	} else {
	    cdx -= u/v * dx;
	    cdy -= u/v * dy;
	}
    }

    berr = bdx * bdx + bdy * bdy;
    cerr = cdx * cdx + cdy * cdy;
    if (berr > cerr)
	return berr * v;
    else
	return cerr * v;
}

static void
_offset_line_to (cairo_t *cr,
		 const point_t *p0,
		 const point_t *p1,
		 const point_t *p2,
		 const point_t *p3,
		 double offset)
{
    double dx, dy, v;

    dx = p1->x - p0->x;
    dy = p1->y - p0->y;
     v = hypot (dx, dy);
     if (v == 0) {
	 dx = p2->x - p0->x;
	 dy = p2->y - p0->y;
	 v = hypot (dx, dy);
	 if (v == 0) {
	     dx = p3->x - p0->x;
	     dy = p3->y - p0->y;
	     v = hypot (dx, dy);
	 }
     }

     if (v == 0) {
	 cairo_line_to (cr, p0->x, p0->y);
     } else
	 cairo_line_to (cr, p0->x - offset * dy / v, p0->y + offset * dx / v);
}

static void
_spline_decompose_into (knots_t *k1,
			double tolerance_squared,
			double offset,
			cairo_t *cr)
{
    knots_t k2;

    if (_spline_error_squared (k1) < tolerance_squared) {
	_offset_line_to (cr, &k1->a, &k1->b, &k1->c, &k1->d, offset);
	return;
    }

    _de_casteljau (k1, &k2);

    _spline_decompose_into (k1, tolerance_squared, offset, cr);
    _spline_decompose_into (&k2, tolerance_squared, offset, cr);
}

static void
_spline_decompose (const knots_t *knots,
		   double tolerance, double offset,
		   cairo_t *cr)
{
    knots_t k;

    k = *knots;
    _spline_decompose_into (&k, tolerance * tolerance, offset, cr);

    _offset_line_to (cr, &knots->d, &knots->c, &knots->b, &knots->a, -offset);
}

static void
_knots_reverse (knots_t *knots)
{
    point_t tmp;

    tmp = knots->a;
    knots->a = knots->d;
    knots->d = tmp;

    tmp = knots->b;
    knots->b = knots->c;
    knots->c = tmp;
}

static void
thick_splines (cairo_t *cr, double offset)
{
    knots_t k;

    cairo_save (cr);
    cairo_translate (cr, 15, 15);

    k = knots[0];

    cairo_new_path (cr);
    _spline_decompose (&k, .1, offset, cr);
    _knots_reverse (&k);
    _spline_decompose (&k, .1, offset, cr);
    cairo_close_path (cr);
    cairo_fill (cr);

    cairo_translate (cr, 130, 0);

    k = knots[1];

    cairo_new_path (cr);
    _spline_decompose (&k, .1, offset, cr);
    _knots_reverse (&k);
    _spline_decompose (&k, .1, offset, cr);
    cairo_close_path (cr);
    cairo_fill (cr);

    cairo_translate (cr, 130, 0);

    k = knots[2];

    cairo_new_path (cr);
    _spline_decompose (&k, .1, offset, cr);
    _knots_reverse (&k);
    _spline_decompose (&k, .1, offset, cr);
    cairo_close_path (cr);
    cairo_fill (cr);

    cairo_translate (cr, -130 - 65, 130);

    k = knots[3];

    cairo_new_path (cr);
    _spline_decompose (&k, .1, offset, cr);
    _knots_reverse (&k);
    _spline_decompose (&k, .1, offset, cr);
    cairo_close_path (cr);
    cairo_fill (cr);

    cairo_translate (cr, 130, 0);

    k = knots[4];

    cairo_new_path (cr);
    _spline_decompose (&k, .1, offset, cr);
    _knots_reverse (&k);
    _spline_decompose (&k, .1, offset, cr);
    cairo_close_path (cr);
    cairo_fill (cr);
    cairo_restore (cr);
}

static void
thin_splines (cairo_t *cr)
{
    cairo_save (cr);
    cairo_translate (cr, 15, 15);

    cairo_new_path (cr);
    _spline_decompose (&knots[0], .1, 0, cr);
    cairo_stroke (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    _spline_decompose (&knots[1], .1, 0, cr);
    cairo_stroke (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    _spline_decompose (&knots[2], .1, 0, cr);
    cairo_stroke (cr);

    cairo_translate (cr, -130 - 65, 130);

    cairo_new_path (cr);
    _spline_decompose (&knots[3], .1, 0, cr);
    cairo_stroke (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    _spline_decompose (&knots[4], .1, 0, cr);
    cairo_stroke (cr);
    cairo_restore (cr);
}
#endif

static void
draw_bbox (cairo_t *cr, double x0, double y0, double x1, double y1)
{
    cairo_rectangle (cr,
		     floor (x0) + .5, floor (y0) + .5,
		     ceil (x1) - floor (x0), ceil (y1) - floor (y0));
    cairo_stroke (cr);
}

static void
stroke_splines (cairo_t *cr)
{
    double stroke_x0, stroke_x1, stroke_y0, stroke_y1;
    double path_x0, path_x1, path_y0, path_y1;

    cairo_save (cr);
    cairo_translate (cr, 15, 15);

    cairo_new_path (cr);
    cairo_move_to (cr,
		   knots[0].a.x, knots[0].a.y);
    cairo_curve_to (cr,
		    knots[0].b.x, knots[0].b.y,
		    knots[0].c.x, knots[0].c.y,
		    knots[0].d.x, knots[0].d.y);
    cairo_stroke_extents (cr, &stroke_x0, &stroke_y0, &stroke_x1, &stroke_y1);
    cairo_path_extents (cr, &path_x0, &path_y0, &path_x1, &path_y1);
    cairo_stroke (cr);

    cairo_save (cr); {
	cairo_set_line_width (cr, 1);
	cairo_set_source_rgb (cr, 1, 0, 0);
	draw_bbox (cr, stroke_x0, stroke_y0, stroke_x1, stroke_y1);
	cairo_set_source_rgb (cr, 0, 0, 1);
	draw_bbox (cr, path_x0, path_y0, path_x1, path_y1);
    } cairo_restore (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    cairo_move_to (cr,
		   knots[1].a.x, knots[1].a.y);
    cairo_curve_to (cr,
		    knots[1].b.x, knots[1].b.y,
		    knots[1].c.x, knots[1].c.y,
		    knots[1].d.x, knots[1].d.y);
    cairo_stroke_extents (cr, &stroke_x0, &stroke_y0, &stroke_x1, &stroke_y1);
    cairo_path_extents (cr, &path_x0, &path_y0, &path_x1, &path_y1);
    cairo_stroke (cr);

    cairo_save (cr); {
	cairo_set_line_width (cr, 1);
	cairo_set_source_rgb (cr, 1, 0, 0);
	draw_bbox (cr, stroke_x0, stroke_y0, stroke_x1, stroke_y1);
	cairo_set_source_rgb (cr, 0, 0, 1);
	draw_bbox (cr, path_x0, path_y0, path_x1, path_y1);
    } cairo_restore (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    cairo_move_to (cr,
		   knots[2].a.x, knots[2].a.y);
    cairo_curve_to (cr,
		    knots[2].b.x, knots[2].b.y,
		    knots[2].c.x, knots[2].c.y,
		    knots[2].d.x, knots[2].d.y);
    cairo_stroke_extents (cr, &stroke_x0, &stroke_y0, &stroke_x1, &stroke_y1);
    cairo_path_extents (cr, &path_x0, &path_y0, &path_x1, &path_y1);
    cairo_stroke (cr);

    cairo_save (cr); {
	cairo_set_line_width (cr, 1);
	cairo_set_source_rgb (cr, 1, 0, 0);
	draw_bbox (cr, stroke_x0, stroke_y0, stroke_x1, stroke_y1);
	cairo_set_source_rgb (cr, 0, 0, 1);
	draw_bbox (cr, path_x0, path_y0, path_x1, path_y1);
    } cairo_restore (cr);

    cairo_translate (cr, -130 - 65, 130);

    cairo_new_path (cr);
    cairo_move_to (cr,
		   knots[3].a.x, knots[3].a.y);
    cairo_curve_to (cr,
		    knots[3].b.x, knots[3].b.y,
		    knots[3].c.x, knots[3].c.y,
		    knots[3].d.x, knots[3].d.y);
    cairo_stroke_extents (cr, &stroke_x0, &stroke_y0, &stroke_x1, &stroke_y1);
    cairo_path_extents (cr, &path_x0, &path_y0, &path_x1, &path_y1);
    cairo_stroke (cr);

    cairo_save (cr); {
	cairo_set_line_width (cr, 1);
	cairo_set_source_rgb (cr, 1, 0, 0);
	draw_bbox (cr, stroke_x0, stroke_y0, stroke_x1, stroke_y1);
	cairo_set_source_rgb (cr, 0, 0, 1);
	draw_bbox (cr, path_x0, path_y0, path_x1, path_y1);
    } cairo_restore (cr);

    cairo_translate (cr, 130, 0);

    cairo_new_path (cr);
    cairo_move_to (cr,
		   knots[4].a.x, knots[4].a.y);
    cairo_curve_to (cr,
		    knots[4].b.x, knots[4].b.y,
		    knots[4].c.x, knots[4].c.y,
		    knots[4].d.x, knots[4].d.y);
    cairo_stroke_extents (cr, &stroke_x0, &stroke_y0, &stroke_x1, &stroke_y1);
    cairo_path_extents (cr, &path_x0, &path_y0, &path_x1, &path_y1);
    cairo_stroke (cr);

    cairo_save (cr); {
	cairo_set_line_width (cr, 1);
	cairo_set_source_rgb (cr, 1, 0, 0);
	draw_bbox (cr, stroke_x0, stroke_y0, stroke_x1, stroke_y1);
	cairo_set_source_rgb (cr, 0, 0, 1);
	draw_bbox (cr, path_x0, path_y0, path_x1, path_y1);
    } cairo_restore (cr);

    cairo_restore (cr);
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    cairo_set_source_rgb (cr, 1, 1, 1);
    cairo_paint (cr);

#ifdef REFERENCE
    cairo_set_source_rgb (cr, 0, 0, 0);
    thick_splines (cr, 5);

    cairo_set_source_rgb (cr, 1, 1, 1);
    thin_splines (cr);
#endif

    /*
     * Use a high tolerance to reduce dependence upon algorithm used for
     * spline decomposition.
     */
    cairo_set_tolerance (cr, 0.001);

    cairo_set_line_width (cr, 10);
    cairo_set_source_rgb (cr, 0, 0, 0);
    stroke_splines (cr);
    cairo_set_line_width (cr, 2);
    cairo_set_source_rgb (cr, 1, 1, 1);
    stroke_splines (cr);

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (spline_decomposition,
	    "Tests splines with various inflection points",
	    "stroke, spline", /* keywords */
	    NULL, /* requirements */
	    390, 260,
	    NULL, draw)