[CpmlCurve] Added stub for cpml_curve_length()
[adg.git] / cpml / cpml-curve.c
blob9ce0b7bbc859d0bd32565b6c0820110efce0a6e2
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_intersection:
207 * @curve: the first curve
208 * @curve2: the second curve
209 * @dest: a vector of at least 4 #CpmlPair
211 * Given two Bézier cubic curves (@curve and @curve2), gets their
212 * intersection points and store the result in @dest. Because two
213 * curves can have 4 intersections, @dest MUST be at least an array
214 * of 4 #CpmlPair.
216 * <important>
217 * <title>TODO</title>
218 * <itemizedlist>
219 * <listitem>To be implemented...</listitem>
220 * </itemizedlist>
221 * </important>
223 * Return value: the number of intersections (max 4)
226 cpml_curve_intersection(const CpmlPrimitive *curve,
227 const CpmlPrimitive *curve2, CpmlPair *dest)
229 return 0;
233 * cpml_curve_intersection_with_arc:
234 * @curve: a curve
235 * @arc: an arc
236 * @dest: a vector of at least 4 #CpmlPair
238 * Given a Bézier cubic @curve and an @arc, gets their intersection
239 * points and store the result in @dest. Because an arc and a cubic
240 * curve can have up to 4 intersections, @dest MUST be at least an
241 * array of 4 #CpmlPair.
243 * <important>
244 * <title>TODO</title>
245 * <itemizedlist>
246 * <listitem>To be implemented...</listitem>
247 * </itemizedlist>
248 * </important>
250 * Return value: the number of intersections (max 4)
253 cpml_curve_intersection_with_arc(const CpmlPrimitive *curve,
254 const CpmlPrimitive *arc, CpmlPair *dest)
256 return 0;
260 * cpml_curve_intersection_with_line:
261 * @curve: a curve
262 * @line: a line
263 * @dest: a vector of at least 4 #CpmlPair
265 * Given a Bézier cubic @curve and a @line, gets their intersection
266 * points and store the result in @dest. Because a line and a cubic
267 * curve can have up to 4 intersections, @dest MUST be at least an
268 * array of 4 #CpmlPair.
270 * <important>
271 * <title>TODO</title>
272 * <itemizedlist>
273 * <listitem>To be implemented...</listitem>
274 * </itemizedlist>
275 * </important>
277 * Return value: the number of intersections (max 4)
280 cpml_curve_intersection_with_line(const CpmlPrimitive *curve,
281 const CpmlPrimitive *line, CpmlPair *dest)
283 return 0;
287 * cpml_curve_offset:
288 * @curve: the #CpmlPrimitive curve data
289 * @offset: distance for the computed parallel curve
291 * Given a cubic Bézier primitive in @curve, this function finds
292 * the approximated Bézier curve parallel to @curve at distance
293 * @offset (an offset curve). The four points needed to build the
294 * new curve are returned in the @curve struct.
296 * To solve the offset problem, a custom algorithm is used. First, the
297 * resulting curve MUST have the same slope at the start and end point.
298 * These constraints are not sufficient to resolve the system, so I let
299 * the curve pass thought a given point (pm, known and got from the
300 * original curve) at a given time (m, now hardcoded to 0.5).
302 * Firstly, I define some useful variables:
304 * v0 = unitvector(p[1]-p[0]) * offset;
305 * v3 = unitvector(p[3]-p[2]) * offset;
306 * p0 = p[0] + normal v0;
307 * p3 = p[3] + normal v3.
309 * Now I want the curve to have the specified slopes at the start
310 * and end point. Forcing the same slope at the start point means:
312 * p1 = p0 + k0 v0.
314 * where k0 is an arbitrary factor. Decomposing for x and y components:
316 * p1.x = p0.x + k0 v0.x;
317 * p1.y = p0.y + k0 v0.y.
319 * Doing the same for the end point gives:
321 * p2.x = p3.x + k3 v3.x;
322 * p2.y = p3.y + k3 v3.y.
324 * Now I interpolate the curve by forcing it to pass throught pm
325 * when "time" is m, where 0 < m < 1. The cubic Bézier function is:
327 * C(t) = (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3.
329 * and forcing t=m and C(t) = pm:
331 * pm = (1-m)³p0 + 3m(1-m)²p1 + 3m²(1-m)p2 + m³p3.
333 * (1-m) p1 + m p2 = (pm - (1-m)³p0 - m³p3) / (3m (1-m)).
335 * So the final system is:
337 * p1.x = p0.x + k0 v0.x;
338 * p1.y = p0.y + k0 v0.y;
339 * p2.x = p3.x + k3 v3.x;
340 * p2.y = p3.y + k3 v3.y;
341 * (1-m) p1.x + m p2.x = (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m));
342 * (1-m) p1.y + m p2.y = (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)).
344 * Substituting and resolving for k0 and k3:
346 * (1-m) k0 v0.x + m k3 v3.x =
347 * (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m)) - (1-m) p0.x - m p3.x;
348 * (1-m) k0 v0.y + m k3 v3.y =
349 * (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)) - (1-m) p0.y - m p3.y.
351 * (1-m) k0 v0.x + m k3 v3.x =
352 * (pm.x - (1-m)²(1+2m) p0.x - m²(3-2m) p3.x) / (3m (1-m));
353 * (1-m) k0 v0.y + m k3 v3.y =
354 * (pm.y - (1-m)²(1+2m) p0.y - m²(3-2m) p3.y) / (3m (1-m)).
356 * Let:
358 * pk = (pm - (1-m)²(1+2m) p0 - m²(3-2m) p3) / (3m (1-m)).
360 * gives the following system:
362 * (1-m) k0 v0.x + m k3 v3.x = pk.x;
363 * (1-m) k0 v0.y + m k3 v3.y = pk.y.
365 * Now I should avoid division by 0 troubles. If either v0.x and v3.x
366 * are 0, the first equation will be inconsistent. More in general the
367 * v0.x*v3.y = v3.x*v3.y condition should be avoided. This is the first
368 * case to check, in which case an alternative approach is used. In the
369 * other cases the above system can be used.
371 * If v0.x != 0 I can resolve for k0 and then find k3:
373 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
374 * (pk.x - m k3 v3.x) v0.y / v0.x + m k3 v3.y = pk.y.
376 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
377 * k3 m (v3.y - v3.x v0.y / v0.x) = pk.y - pk.x v0.y / v0.x.
379 * k3 = (pk.y - pk.x v0.y / v0.x) / (m (v3.y - v3.x v0.y / v0.x));
380 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x).
382 * If v3.x != 0 I can resolve for k3 and then find k0:
384 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
385 * (1-m) k0 v0.y + (pk.x - (1-m) k0 v0.x) v3.y / v3.x = pk.y.
387 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
388 * k0 (1-m) (v0.y - k0 v0.x v3.y / v3.x) = pk.y - pk.x v3.y / v3.x.
390 * k0 = (pk.y - pk.x v3.y / v3.x) / ((1-m) (v0.y - v0.x v3.y / v3.x));
391 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x).
393 * <important>
394 * <title>TODO</title>
395 * <itemizedlist>
396 * <listitem>By default, interpolation of the new curve is made by offseting
397 * the mid point: use a better candidate.</listitem>
398 * <listitem>When the equations are inconsistent, the alternative approach
399 * performs very bad if <varname>v0</varname> and
400 * <varname>v3</varname> are opposite or staggered.</listitem>
401 * </itemizedlist>
402 * </important>
404 void
405 cpml_curve_offset(CpmlPrimitive *curve, double offset)
407 double m, mm;
408 CpmlVector v0, v3, vm, vtmp;
409 CpmlPair p0, p1, p2, p3, pm;
411 m = 0.5;
412 mm = 1-m;
414 /* Firstly, convert the curve points from cairo format to cpml format
415 * and store them (temporary) in p0..p3 */
416 cpml_pair_from_cairo(&p0, curve->org);
417 cpml_pair_from_cairo(&p1, &curve->data[1]);
418 cpml_pair_from_cairo(&p2, &curve->data[2]);
419 cpml_pair_from_cairo(&p3, &curve->data[3]);
421 /* v0 = p1-p0 */
422 cpml_pair_sub(cpml_pair_copy(&v0, &p1), &p0);
424 /* v3 = p3-p2 */
425 cpml_pair_sub(cpml_pair_copy(&v3, &p3), &p2);
427 /* pm = point in C(m) offseted the requested @offset distance */
428 cpml_curve_vector_at_time(curve, &vm, m);
429 cpml_vector_set_length(&vm, offset);
430 cpml_vector_normal(&vm);
431 cpml_curve_pair_at_time(curve, &pm, m);
432 cpml_pair_add(&pm, &vm);
434 /* p0 = p0 + normal of v0 of @offset magnitude (exact value) */
435 cpml_vector_set_length(cpml_pair_copy(&vtmp, &v0), offset);
436 cpml_vector_normal(&vtmp);
437 cpml_pair_add(&p0, &vtmp);
439 /* p3 = p3 + normal of v3 of @offset magnitude, as done for p0 */
440 cpml_vector_set_length(cpml_pair_copy(&vtmp, &v3), offset);
441 cpml_vector_normal(&vtmp);
442 cpml_pair_add(&p3, &vtmp);
444 if (v0.x*v3.y == v3.x*v0.y) {
445 /* Inconsistent equations: use the alternative approach */
446 p1.x = p0.x + v0.x + vm.x * 4/3;
447 p1.y = p0.y + v0.y + vm.y * 4/3;
448 p2.x = p3.x - v3.x + vm.x * 4/3;
449 p2.y = p3.y - v3.y + vm.y * 4/3;
450 } else {
451 CpmlPair pk;
452 double k0, k3;
454 pk.x = (pm.x - mm*mm*(1+m+m)*p0.x - m*m*(1+mm+mm)*p3.x) / (3*m*(1-m));
455 pk.y = (pm.y - mm*mm*(1+m+m)*p0.y - m*m*(1+mm+mm)*p3.y) / (3*m*(1-m));
457 if (v0.x != 0) {
458 k3 = (pk.y - pk.x*v0.y / v0.x) / (m*(v3.y - v3.x*v0.y / v0.x));
459 k0 = (pk.x - m*k3*v3.x) / (mm*v0.x);
460 } else {
461 k0 = (pk.y - pk.x*v3.y / v3.x) / (mm*(v0.y - v0.x*v3.y / v3.x));
462 k3 = (pk.x - mm*k0*v0.x) / (m*v3.x);
465 p1.x = p0.x + k0*v0.x;
466 p1.y = p0.y + k0*v0.y;
467 p2.x = p3.x + k3*v3.x;
468 p2.y = p3.y + k3*v3.y;
471 /* Return the new curve in the original array */
472 cpml_pair_to_cairo(&p0, curve->org);
473 cpml_pair_to_cairo(&p1, &curve->data[1]);
474 cpml_pair_to_cairo(&p2, &curve->data[2]);
475 cpml_pair_to_cairo(&p3, &curve->data[3]);