[demo] Added "Mapping" page to adg-demo
[adg.git] / cpml / cpml-curve.c
blob6d93439522bc8e3d42ee62fbee3e32386275381b
1 /* CPML - Cairo Path Manipulation Library
2 * Copyright (C) 2008, 2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 /**
21 * SECTION:curve
22 * @title: Bézier curves
23 * @short_description: Bézier cubic curve primitive management
25 * The following functions manipulate %CAIRO_PATH_CURVE_TO #CpmlPrimitive.
26 * No check is made on the primitive struct, so be sure
27 * <structname>CpmlPrimitive</structname> is effectively a Bézier curve
28 * before calling these APIs.
29 **/
31 #include "cpml-curve.h"
32 #include "cpml-pair.h"
34 /**
35 * cpml_curve_type_get_npoints:
37 * Returns the number of point needed to properly specify a curve primitive.
39 * Return value: 4
40 **/
41 int
42 cpml_curve_type_get_npoints(void)
44 return 4;
47 /**
48 * cpml_curve_length:
49 * @curve: the #CpmlPrimitive curve data
51 * Given the @curve primitive, returns the approximated length of
52 * the Bézier curve.
54 * <important>
55 * <title>TODO</title>
56 * <itemizedlist>
57 * <listitem>To be implemented...</listitem>
58 * </itemizedlist>
59 * </important>
61 * Return value: the requested length
62 **/
63 double
64 cpml_curve_length(const CpmlPrimitive *curve)
66 return 0.;
69 /**
70 * cpml_curve_pair_at_time:
71 * @curve: the #CpmlPrimitive curve data
72 * @pair: the destination pair
73 * @t: the "time" value
75 * Given the @curve Bézier cubic, finds the coordinates at time @t
76 * (where 0 is the start and 1 is the end) and stores the result
77 * in @pair. Keep in mind @t is not homogeneous, so 0.5 does not
78 * necessarily means the mid point.
80 * The relation 0 < @t < 1 must be satisfied, as interpolating on
81 * cubic curves is not allowed.
82 **/
83 void
84 cpml_curve_pair_at_time(const CpmlPrimitive *curve, CpmlPair *pair, double t)
86 cairo_path_data_t *p1, *p2, *p3, *p4;
87 double t_2, t_3, t1, t1_2, t1_3;
89 p1 = cpml_primitive_get_point(curve, 0);
90 p2 = cpml_primitive_get_point(curve, 1);
91 p3 = cpml_primitive_get_point(curve, 2);
92 p4 = cpml_primitive_get_point(curve, 3);
94 t_2 = t * t;
95 t_3 = t_2 * t;
96 t1 = 1 - t;
97 t1_2 = t1 * t1;
98 t1_3 = t1_2 * t1;
100 pair->x = t1_3 * p1->point.x + 3 * t1_2 * t * p2->point.x
101 + 3 * t1 * t_2 * p3->point.x + t_3 * p4->point.x;
102 pair->y = t1_3 * p1->point.y + 3 * t1_2 * t * p2->point.y
103 + 3 * t1 * t_2 * p3->point.y + t_3 * p4->point.y;
107 * cpml_curve_pair_at:
108 * @curve: the #CpmlPrimitive curve data
109 * @pair: the destination #AdgPair
110 * @pos: the position value
112 * Given the @curve Bézier cubic, finds the coordinates at position
113 * @pos (where 0 is the start and 1 is the end) and stores the result
114 * in @pair. It is similar to cpml_curve_pair_at_time() but the @pos
115 * value is evenly distribuited, that is 0.5 is exactly the mid point.
116 * If you do not need this feature, use cpml_curve_pair_at_time()
117 * as it is considerable faster.
119 * The relation 0 < @pos < 1 must be satisfied, as interpolating on
120 * cubic curves is not allowed.
122 * <important>
123 * <title>TODO</title>
124 * <itemizedlist>
125 * <listitem>To be implemented...</listitem>
126 * </itemizedlist>
127 * </important>
129 void
130 cpml_curve_pair_at(const CpmlPrimitive *curve, CpmlPair *pair, double pos)
135 * cpml_curve_vector_at_time:
136 * @curve: the #CpmlPrimitive curve data
137 * @vector: the destination vector
138 * @t: the "time" value
140 * Given the @curve Bézier cubic, finds the slope at time @t
141 * (where 0 is the start and 1 is the end) and stores the result
142 * in @vector. Keep in mind @t is not homogeneous, so 0.5
143 * does not necessarily means the mid point.
145 * @t must be inside the range 0 .. 1, as interpolating is not
146 * allowed.
148 void
149 cpml_curve_vector_at_time(const CpmlPrimitive *curve,
150 CpmlVector *vector, double t)
152 cairo_path_data_t *p1, *p2, *p3, *p4;
153 CpmlPair p21, p32, p43;
154 double t1, t1_2, t_2;
156 p1 = cpml_primitive_get_point(curve, 0);
157 p2 = cpml_primitive_get_point(curve, 1);
158 p3 = cpml_primitive_get_point(curve, 2);
159 p4 = cpml_primitive_get_point(curve, 3);
161 p21.x = p2->point.x - p1->point.x;
162 p21.y = p2->point.y - p1->point.y;
163 p32.x = p3->point.x - p2->point.x;
164 p32.y = p3->point.y - p2->point.y;
165 p43.x = p4->point.x - p3->point.x;
166 p43.y = p4->point.y - p3->point.y;
168 t1 = 1 - t;
169 t1_2 = t1 * t1;
170 t_2 = t * t;
172 vector->x = 3 * t1_2 * p21.x + 6 * t1 * t * p32.x + 3 * t_2 * p43.x;
173 vector->y = 3 * t1_2 * p21.y + 6 * t1 * t * p32.y + 3 * t_2 * p43.y;
177 * cpml_curve_vector_at:
178 * @curve: the #CpmlPrimitive curve data
179 * @vector: the destination vector
180 * @pos: the position value
182 * Given the @curve Bézier cubic, finds the slope at position @pos
183 * (where 0 is the start and 1 is the end) and stores the result
184 * in @vector. It is similar to cpml_curve_vector_at_time() but the
185 * @pos value is evenly distribuited, that is 0.5 is exactly the
186 * mid point. If you do not need this feature, use
187 * cpml_curve_vector_at_time() as it is considerable faster.
189 * @pos must be inside the range 0 .. 1, as interpolating is not
190 * allowed.
192 * <important>
193 * <title>TODO</title>
194 * <itemizedlist>
195 * <listitem>To be implemented...</listitem>
196 * </itemizedlist>
197 * </important>
199 void
200 cpml_curve_vector_at(const CpmlPrimitive *curve,
201 CpmlVector *vector, double pos)
206 * cpml_curve_near_pos:
207 * @curve: the #CpmlPrimitive curve data
208 * @pair: the coordinates of the subject point
210 * Returns the pos value of the point on @curve nearest to @pair.
211 * The returned value is always between 0 and 1.
213 * <important>
214 * <title>TODO</title>
215 * <itemizedlist>
216 * <listitem>To be implemented...</listitem>
217 * </itemizedlist>
218 * </important>
220 * Return value: the pos value, always between 0 and 1
222 double
223 cpml_curve_near_pos(const CpmlPrimitive *curve, const CpmlPair *pair)
225 /* TODO */
227 return 0;
231 * cpml_curve_intersection:
232 * @curve: the first curve
233 * @curve2: the second curve
234 * @dest: a vector of #CpmlPair
235 * @max: maximum number of intersections to return
236 * (that is, the size of @dest)
238 * Given two Bézier cubic curves (@curve and @curve2), gets their
239 * intersection points and store the result in @dest. Because two
240 * curves can have 4 intersections, @dest MUST be at least an array
241 * of 4 #CpmlPair.
243 * If @max is 0, the function returns 0 immediately without any
244 * further processing. If @curve and @curve2 are cohincident,
245 * their intersections are not considered.
247 * <important>
248 * <title>TODO</title>
249 * <itemizedlist>
250 * <listitem>To be implemented...</listitem>
251 * </itemizedlist>
252 * </important>
254 * Return value: the number of intersections found (max 4)
255 * or 0 if the primitives do not intersect
258 cpml_curve_intersection(const CpmlPrimitive *curve,
259 const CpmlPrimitive *curve2,
260 CpmlPair *dest, int max)
262 return 0;
266 * cpml_curve_intersection_with_arc:
267 * @curve: a curve
268 * @arc: an arc
269 * @dest: a vector of #CpmlPair
270 * @max: maximum number of intersections to return
271 * (that is, the size of @dest)
273 * Given a Bézier cubic @curve and an @arc, gets their intersection
274 * points and store the result in @dest. Because an arc and a cubic
275 * curve can have up to 4 intersections, @dest MUST be at least an
276 * array of 4 #CpmlPair.
278 * If @max is 0, the function returns 0 immediately without any
279 * further processing.
281 * <important>
282 * <title>TODO</title>
283 * <itemizedlist>
284 * <listitem>To be implemented...</listitem>
285 * </itemizedlist>
286 * </important>
288 * Return value: the number of intersections found (max 4)
289 * or 0 if the primitives do not intersect
292 cpml_curve_intersection_with_arc(const CpmlPrimitive *curve,
293 const CpmlPrimitive *arc,
294 CpmlPair *dest, int max)
296 return 0;
300 * cpml_curve_intersection_with_line:
301 * @curve: a curve
302 * @line: a line
303 * @dest: a vector of #CpmlPair
304 * @max: maximum number of intersections to return
305 * (that is, the size of @dest)
307 * Given a Bézier cubic @curve and a @line, gets their intersection
308 * points and store the result in @dest. Because a line and a cubic
309 * curve can have up to 4 intersections, @dest MUST be at least an
310 * array of 4 #CpmlPair.
312 * If @max is 0, the function returns 0 immediately without any
313 * further processing.
315 * <important>
316 * <title>TODO</title>
317 * <itemizedlist>
318 * <listitem>To be implemented...</listitem>
319 * </itemizedlist>
320 * </important>
322 * Return value: the number of intersections found (max 4)
323 * or 0 if the primitives do not intersect
326 cpml_curve_intersection_with_line(const CpmlPrimitive *curve,
327 const CpmlPrimitive *line,
328 CpmlPair *dest, int max)
330 return 0;
334 * cpml_curve_offset:
335 * @curve: the #CpmlPrimitive curve data
336 * @offset: distance for the computed parallel curve
338 * Given a cubic Bézier primitive in @curve, this function finds
339 * the approximated Bézier curve parallel to @curve at distance
340 * @offset (an offset curve). The four points needed to build the
341 * new curve are returned in the @curve struct.
343 * To solve the offset problem, a custom algorithm is used. First, the
344 * resulting curve MUST have the same slope at the start and end point.
345 * These constraints are not sufficient to resolve the system, so I let
346 * the curve pass thought a given point (pm, known and got from the
347 * original curve) at a given time (m, now hardcoded to 0.5).
349 * Firstly, I define some useful variables:
351 * v0 = unitvector(p[1]-p[0]) * offset;
352 * v3 = unitvector(p[3]-p[2]) * offset;
353 * p0 = p[0] + normal v0;
354 * p3 = p[3] + normal v3.
356 * Now I want the curve to have the specified slopes at the start
357 * and end point. Forcing the same slope at the start point means:
359 * p1 = p0 + k0 v0.
361 * where k0 is an arbitrary factor. Decomposing for x and y components:
363 * p1.x = p0.x + k0 v0.x;
364 * p1.y = p0.y + k0 v0.y.
366 * Doing the same for the end point gives:
368 * p2.x = p3.x + k3 v3.x;
369 * p2.y = p3.y + k3 v3.y.
371 * Now I interpolate the curve by forcing it to pass throught pm
372 * when "time" is m, where 0 < m < 1. The cubic Bézier function is:
374 * C(t) = (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3.
376 * and forcing t=m and C(t) = pm:
378 * pm = (1-m)³p0 + 3m(1-m)²p1 + 3m²(1-m)p2 + m³p3.
380 * (1-m) p1 + m p2 = (pm - (1-m)³p0 - m³p3) / (3m (1-m)).
382 * So the final system is:
384 * p1.x = p0.x + k0 v0.x;
385 * p1.y = p0.y + k0 v0.y;
386 * p2.x = p3.x + k3 v3.x;
387 * p2.y = p3.y + k3 v3.y;
388 * (1-m) p1.x + m p2.x = (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m));
389 * (1-m) p1.y + m p2.y = (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)).
391 * Substituting and resolving for k0 and k3:
393 * (1-m) k0 v0.x + m k3 v3.x =
394 * (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m)) - (1-m) p0.x - m p3.x;
395 * (1-m) k0 v0.y + m k3 v3.y =
396 * (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)) - (1-m) p0.y - m p3.y.
398 * (1-m) k0 v0.x + m k3 v3.x =
399 * (pm.x - (1-m)²(1+2m) p0.x - m²(3-2m) p3.x) / (3m (1-m));
400 * (1-m) k0 v0.y + m k3 v3.y =
401 * (pm.y - (1-m)²(1+2m) p0.y - m²(3-2m) p3.y) / (3m (1-m)).
403 * Let:
405 * pk = (pm - (1-m)²(1+2m) p0 - m²(3-2m) p3) / (3m (1-m)).
407 * gives the following system:
409 * (1-m) k0 v0.x + m k3 v3.x = pk.x;
410 * (1-m) k0 v0.y + m k3 v3.y = pk.y.
412 * Now I should avoid division by 0 troubles. If either v0.x and v3.x
413 * are 0, the first equation will be inconsistent. More in general the
414 * v0.x*v3.y = v3.x*v3.y condition should be avoided. This is the first
415 * case to check, in which case an alternative approach is used. In the
416 * other cases the above system can be used.
418 * If v0.x != 0 I can resolve for k0 and then find k3:
420 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
421 * (pk.x - m k3 v3.x) v0.y / v0.x + m k3 v3.y = pk.y.
423 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
424 * k3 m (v3.y - v3.x v0.y / v0.x) = pk.y - pk.x v0.y / v0.x.
426 * k3 = (pk.y - pk.x v0.y / v0.x) / (m (v3.y - v3.x v0.y / v0.x));
427 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x).
429 * If v3.x != 0 I can resolve for k3 and then find k0:
431 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
432 * (1-m) k0 v0.y + (pk.x - (1-m) k0 v0.x) v3.y / v3.x = pk.y.
434 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
435 * k0 (1-m) (v0.y - k0 v0.x v3.y / v3.x) = pk.y - pk.x v3.y / v3.x.
437 * k0 = (pk.y - pk.x v3.y / v3.x) / ((1-m) (v0.y - v0.x v3.y / v3.x));
438 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x).
440 * <important>
441 * <title>TODO</title>
442 * <itemizedlist>
443 * <listitem>By default, interpolation of the new curve is made by offseting
444 * the mid point: use a better candidate.</listitem>
445 * <listitem>When the equations are inconsistent, the alternative approach
446 * performs very bad if <varname>v0</varname> and
447 * <varname>v3</varname> are opposite or staggered.</listitem>
448 * </itemizedlist>
449 * </important>
451 void
452 cpml_curve_offset(CpmlPrimitive *curve, double offset)
454 double m, mm;
455 CpmlVector v0, v3, vm, vtmp;
456 CpmlPair p0, p1, p2, p3, pm;
458 m = 0.5;
459 mm = 1-m;
461 /* Firstly, convert the curve points from cairo format to cpml format
462 * and store them (temporary) in p0..p3 */
463 cpml_pair_from_cairo(&p0, curve->org);
464 cpml_pair_from_cairo(&p1, &curve->data[1]);
465 cpml_pair_from_cairo(&p2, &curve->data[2]);
466 cpml_pair_from_cairo(&p3, &curve->data[3]);
468 /* v0 = p1-p0 */
469 cpml_pair_sub(cpml_pair_copy(&v0, &p1), &p0);
471 /* v3 = p3-p2 */
472 cpml_pair_sub(cpml_pair_copy(&v3, &p3), &p2);
474 /* pm = point in C(m) offseted the requested @offset distance */
475 cpml_curve_vector_at_time(curve, &vm, m);
476 cpml_vector_set_length(&vm, offset);
477 cpml_vector_normal(&vm);
478 cpml_curve_pair_at_time(curve, &pm, m);
479 cpml_pair_add(&pm, &vm);
481 /* p0 = p0 + normal of v0 of @offset magnitude (exact value) */
482 cpml_vector_set_length(cpml_pair_copy(&vtmp, &v0), offset);
483 cpml_vector_normal(&vtmp);
484 cpml_pair_add(&p0, &vtmp);
486 /* p3 = p3 + normal of v3 of @offset magnitude, as done for p0 */
487 cpml_vector_set_length(cpml_pair_copy(&vtmp, &v3), offset);
488 cpml_vector_normal(&vtmp);
489 cpml_pair_add(&p3, &vtmp);
491 if (v0.x*v3.y == v3.x*v0.y) {
492 /* Inconsistent equations: use the alternative approach */
493 p1.x = p0.x + v0.x + vm.x * 4/3;
494 p1.y = p0.y + v0.y + vm.y * 4/3;
495 p2.x = p3.x - v3.x + vm.x * 4/3;
496 p2.y = p3.y - v3.y + vm.y * 4/3;
497 } else {
498 CpmlPair pk;
499 double k0, k3;
501 pk.x = (pm.x - mm*mm*(1+m+m)*p0.x - m*m*(1+mm+mm)*p3.x) / (3*m*(1-m));
502 pk.y = (pm.y - mm*mm*(1+m+m)*p0.y - m*m*(1+mm+mm)*p3.y) / (3*m*(1-m));
504 if (v0.x != 0) {
505 k3 = (pk.y - pk.x*v0.y / v0.x) / (m*(v3.y - v3.x*v0.y / v0.x));
506 k0 = (pk.x - m*k3*v3.x) / (mm*v0.x);
507 } else {
508 k0 = (pk.y - pk.x*v3.y / v3.x) / (mm*(v0.y - v0.x*v3.y / v3.x));
509 k3 = (pk.x - mm*k0*v0.x) / (m*v3.x);
512 p1.x = p0.x + k0*v0.x;
513 p1.y = p0.y + k0*v0.y;
514 p2.x = p3.x + k3*v3.x;
515 p2.y = p3.y + k3*v3.y;
518 /* Return the new curve in the original array */
519 cpml_pair_to_cairo(&p0, curve->org);
520 cpml_pair_to_cairo(&p1, &curve->data[1]);
521 cpml_pair_to_cairo(&p2, &curve->data[2]);
522 cpml_pair_to_cairo(&p3, &curve->data[3]);