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.
22 * @Section_Id:CpmlCurve
24 * @short_description: Bézier cubic curve primitive management
26 * The following functions manipulate %CAIRO_PATH_CURVE_TO #CpmlPrimitive.
27 * No validation is made on the input so use the following methods
28 * only when you are sure the <varname>primitive</varname> argument
29 * is effectively a cubic Bézier curve.
33 #include "cpml-curve.h"
34 #include "cpml-pair.h"
38 * cpml_curve_type_get_npoints:
40 * Returns the number of point needed to properly specify a curve primitive.
45 cpml_curve_type_get_npoints(void)
52 * @curve: the #CpmlPrimitive curve data
54 * Given the @curve primitive, returns the approximated length of
60 * <listitem>To be implemented...</listitem>
64 * Return value: the requested length
67 cpml_curve_length(const CpmlPrimitive
*curve
)
74 * @curve: the #CpmlPrimitive curve data
75 * @extents: where to store the extents
77 * Given a @curve primitive, returns its boundary box in @extents.
80 cpml_curve_extents(const CpmlPrimitive
*curve
, CpmlExtents
*extents
)
82 CpmlPair p1
, p2
, p3
, p4
;
84 cpml_pair_from_cairo(&p1
, cpml_primitive_get_point(curve
, 0));
85 cpml_pair_from_cairo(&p2
, cpml_primitive_get_point(curve
, 1));
86 cpml_pair_from_cairo(&p3
, cpml_primitive_get_point(curve
, 2));
87 cpml_pair_from_cairo(&p4
, cpml_primitive_get_point(curve
, 3));
89 cpml_extents_pair_add(extents
, &p1
);
90 cpml_extents_pair_add(extents
, &p2
);
91 cpml_extents_pair_add(extents
, &p3
);
92 cpml_extents_pair_add(extents
, &p4
);
96 * cpml_curve_pair_at_time:
97 * @curve: the #CpmlPrimitive curve data
98 * @pair: the destination pair
99 * @t: the "time" value
101 * Given the @curve Bézier cubic, finds the coordinates at time @t
102 * (where 0 is the start and 1 is the end) and stores the result
103 * in @pair. Keep in mind @t is not homogeneous, so 0.5 does not
104 * necessarily means the mid point.
106 * The relation 0 < @t < 1 must be satisfied, as interpolating on
107 * cubic curves is not allowed.
110 cpml_curve_pair_at_time(const CpmlPrimitive
*curve
, CpmlPair
*pair
, double t
)
112 cairo_path_data_t
*p1
, *p2
, *p3
, *p4
;
113 double t_2
, t_3
, t1
, t1_2
, t1_3
;
115 p1
= cpml_primitive_get_point(curve
, 0);
116 p2
= cpml_primitive_get_point(curve
, 1);
117 p3
= cpml_primitive_get_point(curve
, 2);
118 p4
= cpml_primitive_get_point(curve
, 3);
126 pair
->x
= t1_3
* p1
->point
.x
+ 3 * t1_2
* t
* p2
->point
.x
127 + 3 * t1
* t_2
* p3
->point
.x
+ t_3
* p4
->point
.x
;
128 pair
->y
= t1_3
* p1
->point
.y
+ 3 * t1_2
* t
* p2
->point
.y
129 + 3 * t1
* t_2
* p3
->point
.y
+ t_3
* p4
->point
.y
;
133 * cpml_curve_pair_at:
134 * @curve: the #CpmlPrimitive curve data
135 * @pair: the destination #CpmlPair
136 * @pos: the position value
138 * Given the @curve Bézier cubic, finds the coordinates at position
139 * @pos (where 0 is the start and 1 is the end) and stores the result
140 * in @pair. It is similar to cpml_curve_pair_at_time() but the @pos
141 * value is evenly distribuited, that is 0.5 is exactly the mid point.
142 * If you do not need this feature, use cpml_curve_pair_at_time()
143 * as it is considerable faster.
145 * The relation 0 < @pos < 1 must be satisfied, as interpolating on
146 * cubic curves is not allowed.
149 * <title>TODO</title>
151 * <listitem>To be implemented...</listitem>
156 cpml_curve_pair_at(const CpmlPrimitive
*curve
, CpmlPair
*pair
, double pos
)
161 * cpml_curve_vector_at_time:
162 * @curve: the #CpmlPrimitive curve data
163 * @vector: the destination vector
164 * @t: the "time" value
166 * Given the @curve Bézier cubic, finds the slope at time @t
167 * (where 0 is the start and 1 is the end) and stores the result
168 * in @vector. Keep in mind @t is not homogeneous, so 0.5
169 * does not necessarily means the mid point.
171 * @t must be inside the range 0 .. 1, as interpolating is not
175 cpml_curve_vector_at_time(const CpmlPrimitive
*curve
,
176 CpmlVector
*vector
, double t
)
178 cairo_path_data_t
*p1
, *p2
, *p3
, *p4
;
179 CpmlPair p21
, p32
, p43
;
180 double t1
, t1_2
, t_2
;
182 p1
= cpml_primitive_get_point(curve
, 0);
183 p2
= cpml_primitive_get_point(curve
, 1);
184 p3
= cpml_primitive_get_point(curve
, 2);
185 p4
= cpml_primitive_get_point(curve
, 3);
187 p21
.x
= p2
->point
.x
- p1
->point
.x
;
188 p21
.y
= p2
->point
.y
- p1
->point
.y
;
189 p32
.x
= p3
->point
.x
- p2
->point
.x
;
190 p32
.y
= p3
->point
.y
- p2
->point
.y
;
191 p43
.x
= p4
->point
.x
- p3
->point
.x
;
192 p43
.y
= p4
->point
.y
- p3
->point
.y
;
198 vector
->x
= 3 * t1_2
* p21
.x
+ 6 * t1
* t
* p32
.x
+ 3 * t_2
* p43
.x
;
199 vector
->y
= 3 * t1_2
* p21
.y
+ 6 * t1
* t
* p32
.y
+ 3 * t_2
* p43
.y
;
203 * cpml_curve_vector_at:
204 * @curve: the #CpmlPrimitive curve data
205 * @vector: the destination vector
206 * @pos: the position value
208 * Given the @curve Bézier cubic, finds the slope at position @pos
209 * (where 0 is the start and 1 is the end) and stores the result
210 * in @vector. It is similar to cpml_curve_vector_at_time() but the
211 * @pos value is evenly distribuited, that is 0.5 is exactly the
212 * mid point. If you do not need this feature, use
213 * cpml_curve_vector_at_time() as it is considerable faster.
215 * @pos must be inside the range 0 .. 1, as interpolating is not
219 * <title>TODO</title>
221 * <listitem>To be implemented...</listitem>
226 cpml_curve_vector_at(const CpmlPrimitive
*curve
,
227 CpmlVector
*vector
, double pos
)
232 * cpml_curve_near_pos:
233 * @curve: the #CpmlPrimitive curve data
234 * @pair: the coordinates of the subject point
236 * Returns the pos value of the point on @curve nearest to @pair.
237 * The returned value is always between 0 and 1.
240 * <title>TODO</title>
242 * <listitem>To be implemented...</listitem>
246 * Return value: the pos value, always between 0 and 1
249 cpml_curve_near_pos(const CpmlPrimitive
*curve
, const CpmlPair
*pair
)
257 * cpml_curve_intersection:
258 * @curve: the first curve
259 * @curve2: the second curve
260 * @dest: a vector of #CpmlPair
261 * @max: maximum number of intersections to return
262 * (that is, the size of @dest)
264 * Given two Bézier cubic curves (@curve and @curve2), gets their
265 * intersection points and store the result in @dest. Because two
266 * curves can have 4 intersections, @dest MUST be at least an array
269 * If @max is 0, the function returns 0 immediately without any
270 * further processing. If @curve and @curve2 are cohincident,
271 * their intersections are not considered.
274 * <title>TODO</title>
276 * <listitem>To be implemented...</listitem>
280 * Return value: the number of intersections found (max 4)
281 * or 0 if the primitives do not intersect
284 cpml_curve_intersection(const CpmlPrimitive
*curve
,
285 const CpmlPrimitive
*curve2
,
286 CpmlPair
*dest
, int max
)
292 * cpml_curve_intersection_with_arc:
295 * @dest: a vector of #CpmlPair
296 * @max: maximum number of intersections to return
297 * (that is, the size of @dest)
299 * Given a Bézier cubic @curve and an @arc, gets their intersection
300 * points and store the result in @dest. Because an arc and a cubic
301 * curve can have up to 4 intersections, @dest MUST be at least an
302 * array of 4 #CpmlPair.
304 * If @max is 0, the function returns 0 immediately without any
305 * further processing.
308 * <title>TODO</title>
310 * <listitem>To be implemented...</listitem>
314 * Return value: the number of intersections found (max 4)
315 * or 0 if the primitives do not intersect
318 cpml_curve_intersection_with_arc(const CpmlPrimitive
*curve
,
319 const CpmlPrimitive
*arc
,
320 CpmlPair
*dest
, int max
)
326 * cpml_curve_intersection_with_line:
329 * @dest: a vector of #CpmlPair
330 * @max: maximum number of intersections to return
331 * (that is, the size of @dest)
333 * Given a Bézier cubic @curve and a @line, gets their intersection
334 * points and store the result in @dest. Because a line and a cubic
335 * curve can have up to 4 intersections, @dest MUST be at least an
336 * array of 4 #CpmlPair.
338 * If @max is 0, the function returns 0 immediately without any
339 * further processing.
342 * <title>TODO</title>
344 * <listitem>To be implemented...</listitem>
348 * Return value: the number of intersections found (max 4)
349 * or 0 if the primitives do not intersect
352 cpml_curve_intersection_with_line(const CpmlPrimitive
*curve
,
353 const CpmlPrimitive
*line
,
354 CpmlPair
*dest
, int max
)
361 * @curve: the #CpmlPrimitive curve data
362 * @offset: distance for the computed parallel curve
364 * Given a cubic Bézier primitive in @curve, this function finds
365 * the approximated Bézier curve parallel to @curve at distance
366 * @offset (an offset curve). The four points needed to build the
367 * new curve are returned in the @curve struct.
369 * To solve the offset problem, a custom algorithm is used. First, the
370 * resulting curve MUST have the same slope at the start and end point.
371 * These constraints are not sufficient to resolve the system, so I let
372 * the curve pass thought a given point (pm, known and got from the
373 * original curve) at a given time (m, now hardcoded to 0.5).
375 * Firstly, I define some useful variables:
377 * v0 = unitvector(p[1]-p[0]) * offset;
378 * v3 = unitvector(p[3]-p[2]) * offset;
379 * p0 = p[0] + normal v0;
380 * p3 = p[3] + normal v3.
382 * Now I want the curve to have the specified slopes at the start
383 * and end point. Forcing the same slope at the start point means:
387 * where k0 is an arbitrary factor. Decomposing for x and y components:
389 * p1.x = p0.x + k0 v0.x;
390 * p1.y = p0.y + k0 v0.y.
392 * Doing the same for the end point gives:
394 * p2.x = p3.x + k3 v3.x;
395 * p2.y = p3.y + k3 v3.y.
397 * Now I interpolate the curve by forcing it to pass throught pm
398 * when "time" is m, where 0 < m < 1. The cubic Bézier function is:
400 * C(t) = (1-t)³p0 + 3t(1-t)²p1 + 3t²(1-t)p2 + t³p3.
402 * and forcing t=m and C(t) = pm:
404 * pm = (1-m)³p0 + 3m(1-m)²p1 + 3m²(1-m)p2 + m³p3.
406 * (1-m) p1 + m p2 = (pm - (1-m)³p0 - m³p3) / (3m (1-m)).
408 * So the final system is:
410 * p1.x = p0.x + k0 v0.x;
411 * p1.y = p0.y + k0 v0.y;
412 * p2.x = p3.x + k3 v3.x;
413 * p2.y = p3.y + k3 v3.y;
414 * (1-m) p1.x + m p2.x = (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m));
415 * (1-m) p1.y + m p2.y = (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)).
417 * Substituting and resolving for k0 and k3:
419 * (1-m) k0 v0.x + m k3 v3.x =
420 * (pm.x - (1-m)³p0.x - m³p3.x) / (3m (1-m)) - (1-m) p0.x - m p3.x;
421 * (1-m) k0 v0.y + m k3 v3.y =
422 * (pm.y - (1-m)³p0.y - m³p3.y) / (3m (1-m)) - (1-m) p0.y - m p3.y.
424 * (1-m) k0 v0.x + m k3 v3.x =
425 * (pm.x - (1-m)²(1+2m) p0.x - m²(3-2m) p3.x) / (3m (1-m));
426 * (1-m) k0 v0.y + m k3 v3.y =
427 * (pm.y - (1-m)²(1+2m) p0.y - m²(3-2m) p3.y) / (3m (1-m)).
431 * pk = (pm - (1-m)²(1+2m) p0 - m²(3-2m) p3) / (3m (1-m)).
433 * gives the following system:
435 * (1-m) k0 v0.x + m k3 v3.x = pk.x;
436 * (1-m) k0 v0.y + m k3 v3.y = pk.y.
438 * Now I should avoid division by 0 troubles. If either v0.x and v3.x
439 * are 0, the first equation will be inconsistent. More in general the
440 * v0.x*v3.y = v3.x*v3.y condition should be avoided. This is the first
441 * case to check, in which case an alternative approach is used. In the
442 * other cases the above system can be used.
444 * If v0.x != 0 I can resolve for k0 and then find k3:
446 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
447 * (pk.x - m k3 v3.x) v0.y / v0.x + m k3 v3.y = pk.y.
449 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x);
450 * k3 m (v3.y - v3.x v0.y / v0.x) = pk.y - pk.x v0.y / v0.x.
452 * k3 = (pk.y - pk.x v0.y / v0.x) / (m (v3.y - v3.x v0.y / v0.x));
453 * k0 = (pk.x - m k3 v3.x) / ((1-m) v0.x).
455 * If v3.x != 0 I can resolve for k3 and then find k0:
457 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
458 * (1-m) k0 v0.y + (pk.x - (1-m) k0 v0.x) v3.y / v3.x = pk.y.
460 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x);
461 * k0 (1-m) (v0.y - k0 v0.x v3.y / v3.x) = pk.y - pk.x v3.y / v3.x.
463 * k0 = (pk.y - pk.x v3.y / v3.x) / ((1-m) (v0.y - v0.x v3.y / v3.x));
464 * k3 = (pk.x - (1-m) k0 v0.x) / (m v3.x).
467 * <title>TODO</title>
469 * <listitem>By default, interpolation of the new curve is made by offseting
470 * the mid point: use a better candidate.</listitem>
471 * <listitem>When the equations are inconsistent, the alternative approach
472 * performs very bad if <varname>v0</varname> and
473 * <varname>v3</varname> are opposite or staggered.</listitem>
478 cpml_curve_offset(CpmlPrimitive
*curve
, double offset
)
481 CpmlVector v0
, v3
, vm
, vtmp
;
482 CpmlPair p0
, p1
, p2
, p3
, pm
;
487 /* Firstly, convert the curve points from cairo format to cpml format
488 * and store them (temporary) in p0..p3 */
489 cpml_pair_from_cairo(&p0
, curve
->org
);
490 cpml_pair_from_cairo(&p1
, &curve
->data
[1]);
491 cpml_pair_from_cairo(&p2
, &curve
->data
[2]);
492 cpml_pair_from_cairo(&p3
, &curve
->data
[3]);
495 cpml_pair_sub(cpml_pair_copy(&v0
, &p1
), &p0
);
498 cpml_pair_sub(cpml_pair_copy(&v3
, &p3
), &p2
);
500 /* pm = point in C(m) offseted the requested @offset distance */
501 cpml_curve_vector_at_time(curve
, &vm
, m
);
502 cpml_vector_set_length(&vm
, offset
);
503 cpml_vector_normal(&vm
);
504 cpml_curve_pair_at_time(curve
, &pm
, m
);
505 cpml_pair_add(&pm
, &vm
);
507 /* p0 = p0 + normal of v0 of @offset magnitude (exact value) */
508 cpml_vector_set_length(cpml_pair_copy(&vtmp
, &v0
), offset
);
509 cpml_vector_normal(&vtmp
);
510 cpml_pair_add(&p0
, &vtmp
);
512 /* p3 = p3 + normal of v3 of @offset magnitude, as done for p0 */
513 cpml_vector_set_length(cpml_pair_copy(&vtmp
, &v3
), offset
);
514 cpml_vector_normal(&vtmp
);
515 cpml_pair_add(&p3
, &vtmp
);
517 if (v0
.x
*v3
.y
== v3
.x
*v0
.y
) {
518 /* Inconsistent equations: use the alternative approach */
519 p1
.x
= p0
.x
+ v0
.x
+ vm
.x
* 4/3;
520 p1
.y
= p0
.y
+ v0
.y
+ vm
.y
* 4/3;
521 p2
.x
= p3
.x
- v3
.x
+ vm
.x
* 4/3;
522 p2
.y
= p3
.y
- v3
.y
+ vm
.y
* 4/3;
527 pk
.x
= (pm
.x
- mm
*mm
*(1+m
+m
)*p0
.x
- m
*m
*(1+mm
+mm
)*p3
.x
) / (3*m
*(1-m
));
528 pk
.y
= (pm
.y
- mm
*mm
*(1+m
+m
)*p0
.y
- m
*m
*(1+mm
+mm
)*p3
.y
) / (3*m
*(1-m
));
531 k3
= (pk
.y
- pk
.x
*v0
.y
/ v0
.x
) / (m
*(v3
.y
- v3
.x
*v0
.y
/ v0
.x
));
532 k0
= (pk
.x
- m
*k3
*v3
.x
) / (mm
*v0
.x
);
534 k0
= (pk
.y
- pk
.x
*v3
.y
/ v3
.x
) / (mm
*(v0
.y
- v0
.x
*v3
.y
/ v3
.x
));
535 k3
= (pk
.x
- mm
*k0
*v0
.x
) / (m
*v3
.x
);
538 p1
.x
= p0
.x
+ k0
*v0
.x
;
539 p1
.y
= p0
.y
+ k0
*v0
.y
;
540 p2
.x
= p3
.x
+ k3
*v3
.x
;
541 p2
.y
= p3
.y
+ k3
*v3
.y
;
544 /* Return the new curve in the original array */
545 cpml_pair_to_cairo(&p0
, curve
->org
);
546 cpml_pair_to_cairo(&p1
, &curve
->data
[1]);
547 cpml_pair_to_cairo(&p2
, &curve
->data
[2]);
548 cpml_pair_to_cairo(&p3
, &curve
->data
[3]);