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.
25 * @short_description: Manipulation of circular arcs
27 * The following functions manipulate #CAIRO_PATH_ARC_TO #CpmlPrimitive.
28 * No validation is made on the input so use the following methods
29 * only when you are sure the <varname>primitive</varname> argument
30 * is effectively an arc-to.
32 * The arc primitive is defined by 3 points: the first one is the usual
33 * implicit point got from the previous primitive, the second point is
34 * an arbitrary intermediate point laying on the arc and the third point
35 * is the end of the arc. These points identify univocally an arc:
36 * furthermore, the intermediate point also gives the side of
39 * As a special case, when the first point is coincident with the end
40 * point the primitive is considered a circle with diameter defined by
41 * the segment between the first and the intermediate point.
45 * An arc is not a native cairo primitive and should be treated specially.
49 * Using these CPML APIs you are free to use #CAIRO_PATH_ARC_TO whenever
50 * you want but, if you are directly accessing the struct fields, you
51 * are responsible of converting arcs to curves before passing them
52 * to cairo. In other words, do not directly feed #CpmlPath struct to
53 * cairo (throught cairo_append_path() for example) or at least do not
54 * expect it will work.
56 * The conversion is provided by two APIs: cpml_arc_to_cairo() and
57 * cpml_arc_to_curves(). The former directly renders to a cairo context
58 * and is internally used by all the ..._to_cairo() functions when an
59 * arc is met. The latter provided a more powerful (and more complex)
60 * approach as it allows to specify the number of curves to use and do
61 * not need a cairo context.
65 #include "cpml-internal.h"
66 #include "cpml-extents.h"
67 #include "cpml-segment.h"
68 #include "cpml-primitive.h"
69 #include "cpml-primitive-private.h"
75 /* Hardcoded max angle of the arc to be approximated by a Bézier curve:
76 * this influence the arc quality (the default value is got from cairo) */
77 #define ARC_MAX_ANGLE M_PI_2
79 /* Macro to save typing and make put_extents() code cleaner */
80 #define ANGLE_INCLUDED(d) \
81 ((start < (d) && end > (d)) || (start > (d) && end < (d)))
84 static double get_length (const CpmlPrimitive
*arc
);
85 static void put_extents (const CpmlPrimitive
*arc
,
86 CpmlExtents
*extents
);
87 static cairo_bool_t
get_center (const CpmlPair
*p
,
89 static void get_angles (const CpmlPair
*p
,
90 const CpmlPair
*center
,
93 static void arc_to_curve (CpmlPrimitive
*curve
,
94 const CpmlPair
*center
,
100 const _CpmlPrimitiveClass
*
101 _cpml_arc_get_class(void)
103 static _CpmlPrimitiveClass
*p_class
= NULL
;
105 if (p_class
== NULL
) {
106 static _CpmlPrimitiveClass class_data
= {
117 p_class
= &class_data
;
126 * @arc: the #CpmlPrimitive arc data
127 * @center: where to store the center coordinates (can be %NULL)
128 * @r: where to store the radius (can be %NULL)
129 * @start: where to store the starting angle (can be %NULL)
130 * @end: where to store the ending angle (can be %NULL)
132 * Given an @arc, this function calculates and returns its basic data.
133 * Any pointer can be %NULL, in which case the requested info is not
134 * returned. This function can fail (when the three points lay on a
135 * straight line, for example) in which case 0 is returned and no
136 * data can be considered valid.
138 * The radius @r can be 0 when the three points are coincidents: a
139 * circle with radius 0 is considered a valid path.
141 * When the start and end angle are returned, together with their
142 * values these angles implicitely gives another important information:
145 * If @start < @end the arc must be rendered with increasing angle
146 * value (clockwise direction using the ordinary cairo coordinate
147 * system) while if @start > @end the arc must be rendered in reverse
148 * order (that is counterclockwise in the cairo world). This is the
149 * reason the angle values are returned in the range
150 * { -M_PI < value < 3*M_PI } inclusive instead of the usual
151 * { -M_PI < value < M_PI } range.
153 * Returns: 1 if the function worked succesfully, 0 on errors
156 cpml_arc_info(const CpmlPrimitive
*arc
, CpmlPair
*center
,
157 double *r
, double *start
, double *end
)
159 CpmlPair p
[3], l_center
;
161 cpml_pair_from_cairo(&p
[0], arc
->org
);
162 cpml_pair_from_cairo(&p
[1], &arc
->data
[1]);
163 cpml_pair_from_cairo(&p
[2], &arc
->data
[2]);
165 if (!get_center(p
, &l_center
))
172 *r
= cpml_pair_distance(&p
[0], &l_center
);
174 if (start
!= NULL
|| end
!= NULL
) {
175 double l_start
, l_end
;
177 get_angles(p
, &l_center
, &l_start
, &l_end
);
189 * cpml_arc_put_pair_at:
190 * @arc: the #CpmlPrimitive arc data
191 * @pos: the position value
192 * @pair: the destination #CpmlPair
194 * Given an @arc, finds the coordinates at position @pos (where 0 is
195 * the start and 1 is the end) and stores the result in @pair.
197 * @pos can also be outside the 0..1 limit, as interpolating on an
198 * arc is quite trivial.
201 cpml_arc_put_pair_at(const CpmlPrimitive
*arc
, double pos
, CpmlPair
*pair
)
204 cpml_pair_from_cairo(pair
, arc
->org
);
205 } else if (pos
== 1.) {
206 cpml_pair_from_cairo(pair
, &arc
->data
[2]);
209 double r
, start
, end
, angle
;
211 if (!cpml_arc_info(arc
, ¢er
, &r
, &start
, &end
))
214 angle
= (end
-start
)*pos
+ start
;
215 cpml_vector_from_angle(pair
, angle
);
216 cpml_vector_set_length(pair
, r
);
217 cpml_pair_add(pair
, ¢er
);
222 * cpml_arc_put_vector_at:
223 * @arc: the #CpmlPrimitive arc data
224 * @pos: the position value
225 * @vector: the destination vector
227 * Given an @arc, finds the slope at position @pos (where 0 is
228 * the start and 1 is the end) and stores the result in @vector.
230 * @pos can also be outside the 0..1 limit, as interpolating on an
231 * arc is quite trivial.
234 cpml_arc_put_vector_at(const CpmlPrimitive
*arc
, double pos
,
237 double start
, end
, angle
;
239 if (!cpml_arc_info(arc
, NULL
, NULL
, &start
, &end
))
242 angle
= (end
-start
)*pos
+ start
;
243 cpml_vector_from_angle(vector
, angle
);
244 cpml_vector_normal(vector
);
247 cpml_pair_negate(vector
);
251 * cpml_arc_get_closest_pos:
252 * @arc: the #CpmlPrimitive arc data
253 * @pair: the coordinates of the subject point
255 * Returns the pos value of the point on @arc nearest to @pair.
256 * The returned value is always between 0 and 1.
259 * <title>TODO</title>
261 * <listitem>To be implemented...</listitem>
265 * Returns: the pos value, always between 0 and 1
268 cpml_arc_get_closest_pos(const CpmlPrimitive
*arc
, const CpmlPair
*pair
)
276 * cpml_arc_put_intersections:
277 * @arc: the first arc
278 * @arc2: the second arc
279 * @max: maximum number of intersections to return
280 * (that is, the size of @dest)
281 * @dest: a vector of #CpmlPair
283 * Given two arcs (@arc and @arc2), gets their intersection points
284 * and store the result in @dest. Keep in mind two arcs can have
285 * up to 2 intersections.
287 * If @max is 0, the function returns 0 immediately without any
288 * further processing. If @arc and @arc2 are cohincident (same
289 * center and same radius), their intersections are not considered.
292 * <title>TODO</title>
294 * <listitem>To be implemented...</listitem>
298 * Returns: the number of intersections found (max 2)
299 * or 0 if the primitives do not intersect
302 cpml_arc_put_intersections(const CpmlPrimitive
*arc
, const CpmlPrimitive
*arc2
,
303 int max
, CpmlPair
*dest
)
309 * cpml_arc_put_intersections_with_line:
312 * @max: maximum number of intersections to return
313 * (that is, the size of @dest)
314 * @dest: a vector of #CpmlPair
316 * Given an @arc and a @line, gets their intersection points
317 * and store the result in @dest. Keep in mind an arc and a
318 * line can have up to 2 intersections.
320 * If @max is 0, the function returns 0 immediately without any
321 * further processing.
324 * <title>TODO</title>
326 * <listitem>To be implemented...</listitem>
330 * Returns: the number of intersections found (max 2)
331 * or 0 if the primitives do not intersect
334 cpml_arc_put_intersections_with_line(const CpmlPrimitive
*arc
,
335 const CpmlPrimitive
*line
,
336 int max
, CpmlPair
*dest
)
343 * @arc: the #CpmlPrimitive arc data
344 * @offset: distance for the computed parallel arc
346 * Given an @arc, this function computes the parallel arc at
347 * distance @offset. The three points needed to build the
348 * new arc are returned in the @arc data (substituting the
352 cpml_arc_offset(CpmlPrimitive
*arc
, double offset
)
354 CpmlPair p
[3], center
;
357 cpml_pair_from_cairo(&p
[0], arc
->org
);
358 cpml_pair_from_cairo(&p
[1], &arc
->data
[1]);
359 cpml_pair_from_cairo(&p
[2], &arc
->data
[2]);
361 if (!get_center(p
, ¢er
))
364 r
= cpml_pair_distance(&p
[0], ¢er
) + offset
;
366 /* Offset the three points by calculating their vector from the center,
367 * setting the new radius as length and readding the center */
368 cpml_pair_sub(&p
[0], ¢er
);
369 cpml_pair_sub(&p
[1], ¢er
);
370 cpml_pair_sub(&p
[2], ¢er
);
372 cpml_vector_set_length(&p
[0], r
);
373 cpml_vector_set_length(&p
[1], r
);
374 cpml_vector_set_length(&p
[2], r
);
376 cpml_pair_add(&p
[0], ¢er
);
377 cpml_pair_add(&p
[1], ¢er
);
378 cpml_pair_add(&p
[2], ¢er
);
380 cpml_pair_to_cairo(&p
[0], arc
->org
);
381 cpml_pair_to_cairo(&p
[1], &arc
->data
[1]);
382 cpml_pair_to_cairo(&p
[2], &arc
->data
[2]);
387 * @arc: the #CpmlPrimitive arc data
388 * @cr: the destination cairo context
390 * Renders @arc to the @cr cairo context. As cairo does not support
391 * arcs natively, it is approximated using one or more Bézier curves.
393 * The number of curves used is dependent from the angle of the arc.
394 * Anyway, this function uses internally the hardcoded %M_PI_2 value
395 * as threshold value. This means the maximum arc approximated by a
396 * single curve will be a quarter of a circle and, consequently, a
397 * whole circle will be approximated by 4 Bézier curves.
400 cpml_arc_to_cairo(const CpmlPrimitive
*arc
, cairo_t
*cr
)
403 double r
, start
, end
;
407 cairo_path_data_t data
[4];
409 if (!cpml_arc_info(arc
, ¢er
, &r
, &start
, &end
))
412 n_curves
= ceil(fabs(end
-start
) / ARC_MAX_ANGLE
);
413 step
= (end
-start
) / (double) n_curves
;
416 for (angle
= start
; n_curves
--; angle
+= step
) {
417 arc_to_curve(&curve
, ¢er
, r
, angle
, angle
+step
);
419 curve
.data
[1].point
.x
, curve
.data
[1].point
.y
,
420 curve
.data
[2].point
.x
, curve
.data
[2].point
.y
,
421 curve
.data
[3].point
.x
, curve
.data
[3].point
.y
);
426 * cpml_arc_to_curves:
427 * @arc: the #CpmlPrimitive arc data
428 * @segment: the destination #CpmlSegment
429 * @n_curves: number of Bézier to use
431 * Converts @arc to a serie of @n_curves Bézier curves and puts them
432 * inside @segment. Obviously, @segment must have enough space to
433 * contain at least @n_curves curves.
435 * This function works in a similar way as cpml_arc_to_cairo() but
436 * has two important differences: it does not need a cairo context
437 * and the number of curves to be generated is explicitely defined.
438 * The latter difference allows a more specific error control from
439 * the application: in the file src/cairo-arc.c, found in the cairo
440 * tarball (at least in cairo-1.9.1), there is a table showing the
441 * magnitude of error of this curve approximation algorithm.
444 cpml_arc_to_curves(const CpmlPrimitive
*arc
, CpmlSegment
*segment
,
448 double r
, start
, end
;
452 if (!cpml_arc_info(arc
, ¢er
, &r
, &start
, &end
))
455 step
= (end
-start
) / (double) n_curves
;
456 segment
->num_data
= n_curves
*4;
457 curve
.segment
= segment
;
458 curve
.data
= segment
->data
;
460 for (angle
= start
; n_curves
--; angle
+= step
) {
461 arc_to_curve(&curve
, ¢er
, r
, angle
, angle
+step
);
468 get_length(const CpmlPrimitive
*arc
)
470 double r
, start
, end
, delta
;
472 if (!cpml_arc_info(arc
, NULL
, &r
, &start
, &end
) || start
== end
)
483 put_extents(const CpmlPrimitive
*arc
, CpmlExtents
*extents
)
485 double r
, start
, end
;
486 CpmlPair center
, pair
;
488 extents
->is_defined
= 0;
490 if (!cpml_arc_info(arc
, ¢er
, &r
, &start
, &end
))
493 /* Add the right quadrant point if needed */
494 if (ANGLE_INCLUDED(0) || ANGLE_INCLUDED(M_PI
* 2)) {
495 pair
.x
= center
.x
+ r
;
497 cpml_extents_pair_add(extents
, &pair
);
500 /* Add the bottom quadrant point if needed */
501 if (ANGLE_INCLUDED(M_PI_2
) || ANGLE_INCLUDED(M_PI_2
* 5)) {
503 pair
.y
= center
.y
+ r
;
504 cpml_extents_pair_add(extents
, &pair
);
507 /* Add the left quadrant point if needed */
508 if (ANGLE_INCLUDED(M_PI
)) {
509 pair
.x
= center
.x
- r
;
511 cpml_extents_pair_add(extents
, &pair
);
514 /* Add the top quadrant point if needed */
515 if (ANGLE_INCLUDED(M_PI_2
* 3) || ANGLE_INCLUDED(-M_PI_2
)) {
517 pair
.y
= center
.y
- r
;
518 cpml_extents_pair_add(extents
, &pair
);
521 /* Add the start point */
522 cpml_pair_from_cairo(&pair
, cpml_primitive_get_point(arc
, 0));
523 cpml_extents_pair_add(extents
, &pair
);
525 /* Add the end point */
526 cpml_pair_from_cairo(&pair
, cpml_primitive_get_point(arc
, -1));
527 cpml_extents_pair_add(extents
, &pair
);
532 get_center(const CpmlPair
*p
, CpmlPair
*dest
)
537 /* When p[0] == p[2], p[0]..p[1] is considered the diameter of a circle */
538 if (p
[0].x
== p
[2].x
&& p
[0].y
== p
[2].y
) {
539 dest
->x
= (p
[0].x
+ p
[1].x
) / 2;
540 dest
->y
= (p
[0].y
+ p
[1].y
) / 2;
544 /* Translate the 3 points of -p0, to simplify the formula */
545 cpml_pair_copy(&b
, &p
[1]);
546 cpml_pair_sub(&b
, &p
[0]);
547 cpml_pair_copy(&c
, &p
[2]);
548 cpml_pair_sub(&c
, &p
[0]);
550 /* Check for division by 0, that is the case where the 3 given points
551 * are laying on a straight line and there is no fitting circle */
552 d
= (b
.x
*c
.y
- b
.y
*c
.x
) * 2;
556 b2
= b
.x
*b
.x
+ b
.y
*b
.y
;
557 c2
= c
.x
*c
.x
+ c
.y
*c
.y
;
559 dest
->x
= (c
.y
*b2
- b
.y
*c2
) / d
+ p
[0].x
;
560 dest
->y
= (b
.x
*c2
- c
.x
*b2
) / d
+ p
[0].y
;
566 get_angles(const CpmlPair
*p
, const CpmlPair
*center
,
567 double *start
, double *end
)
572 /* Calculate the starting angle */
573 cpml_pair_copy(&vector
, &p
[0]);
574 cpml_pair_sub(&vector
, center
);
575 *start
= cpml_vector_angle(&vector
);
577 if (p
[0].x
== p
[2].x
&& p
[0].y
== p
[2].y
) {
578 /* When p[0] and p[2] are cohincidents, p[0]..p[1] is the diameter
579 * of a circle: return by convention start=start end=start+2PI */
580 *end
= *start
+ M_PI
*2;
582 /* Calculate the mid and end angle: cpml_vector_angle()
583 * returns an angle between -M_PI and M_PI */
584 cpml_pair_copy(&vector
, &p
[1]);
585 cpml_pair_sub(&vector
, center
);
586 mid
= cpml_vector_angle(&vector
);
587 cpml_pair_copy(&vector
, &p
[2]);
588 cpml_pair_sub(&vector
, center
);
589 *end
= cpml_vector_angle(&vector
);
592 /* If the middle angle is outside the start..end range,
593 * the arc should be reversed (that is, start must
594 * be greather than end) */
595 if (mid
< *start
|| mid
> *end
)
598 /* Here the arc is reversed: if the middle angle is
599 * outside the end..start range, the arc should be
600 * re-reversed to get a straight arc (that is, end
601 * must be greather than start) */
602 if (mid
< *end
|| mid
> *start
)
609 arc_to_curve(CpmlPrimitive
*curve
, const CpmlPair
*center
,
610 double r
, double start
, double end
)
612 double r_sin1
, r_cos1
;
613 double r_sin2
, r_cos2
;
616 r_sin1
= r
*sin(start
);
617 r_cos1
= r
*cos(start
);
621 h
= 4./3. * tan((end
-start
) / 4.);
623 curve
->data
[0].header
.type
= CAIRO_PATH_CURVE_TO
;
624 curve
->data
[0].header
.length
= 4;
625 curve
->data
[1].point
.x
= center
->x
+ r_cos1
- h
*r_sin1
;
626 curve
->data
[1].point
.y
= center
->y
+ r_sin1
+ h
*r_cos1
;
627 curve
->data
[2].point
.x
= center
->x
+ r_cos2
+ h
*r_sin2
;
628 curve
->data
[2].point
.y
= center
->y
+ r_sin2
- h
*r_cos2
;
629 curve
->data
[3].point
.x
= center
->x
+ r_cos2
;
630 curve
->data
[3].point
.y
= center
->y
+ r_sin2
;