1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,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. *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
23 * @short_description: The basic model representing a generic path
25 * The #AdgPath model is a virtual path: in a few words, it is a
26 * simple conceptual #cairo_path_t struct. This class implements
27 * methods to manipulate the underlying cairo path.
29 * Although some of the provided methods are clearly based on the
30 * original cairo path manipulation API, their behavior could be
31 * sligthly different. This is intentional, because the ADG provides
32 * additional path manipulation algorithms, sometime quite complex,
33 * and a more restrictive filter on the path quality is required.
34 * Also, the ADG is designed to be used by technicians while cairo
35 * targets a broader range of developers.
37 * As an example, following the rule of the less surprise, some
38 * cairo functions guess the current point when it is not defined,
39 * while the #AdgPath methods trigger a warning without other effect.
40 * Furthermore, after a cairo_path_close_path() call a MOVE_TO
41 * primitive to the starting point of the segment is automatically
42 * added by cairo while in the ADG, after an adg_path_close(), the
43 * current point is simply unset.
47 #include "adg-path-private.h"
48 #include "adg-primitive.h"
54 static void finalize (GObject
*object
);
55 static void changed (AdgModel
*model
);
56 static void clear_cairo_path (AdgPath
*path
);
57 static cairo_path_t
* get_cairo_path (AdgPath
*path
);
58 static cairo_path_t
* get_cpml_path (AdgPath
*path
);
59 static GArray
* arc_to_curves (GArray
*array
,
60 const cairo_path_data_t
62 static void append_primitive (AdgPath
*path
,
63 AdgPrimitive
*primitive
);
64 static gint
needed_pairs (CpmlPrimitiveType type
,
65 gboolean cp_is_valid
);
66 static void clear_operation (AdgPath
*path
);
67 static gboolean
append_operation (AdgPath
*path
,
70 static void do_operation (AdgPath
*path
,
73 static void do_chamfer (AdgPath
*path
,
74 CpmlPrimitive
*current
);
75 static void do_fillet (AdgPath
*path
,
76 CpmlPrimitive
*current
);
77 static gboolean
is_convex (const CpmlPrimitive
83 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_MODEL
);
87 adg_path_class_init(AdgPathClass
*klass
)
89 GObjectClass
*gobject_class
;
90 AdgModelClass
*model_class
;
92 gobject_class
= (GObjectClass
*) klass
;
93 model_class
= (AdgModelClass
*) klass
;
95 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
97 gobject_class
->finalize
= finalize
;
99 model_class
->changed
= changed
;
103 adg_path_init(AdgPath
*path
)
105 AdgPathPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
108 priv
->cp_is_valid
= FALSE
;
109 priv
->path
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
110 priv
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
111 priv
->cairo_path
.data
= NULL
;
112 priv
->cairo_path
.num_data
= 0;
113 priv
->operation
.operator = ADG_OPERATOR_NONE
;
119 finalize(GObject
*object
)
122 GObjectClass
*object_class
;
124 path
= (AdgPath
*) object
;
125 object_class
= (GObjectClass
*) adg_path_parent_class
;
127 g_array_free(path
->priv
->path
, TRUE
);
128 clear_cairo_path(path
);
129 clear_operation(path
);
131 if (object_class
->finalize
!= NULL
)
132 object_class
->finalize(object
);
139 * Creates a new path model. The path must be constructed in the @callback
140 * function: AdgPath will cache and reuse the cairo_copy_path() returned by
141 * the cairo context after the @callback call.
143 * Return value: the new model
148 return (AdgModel
*) g_object_new(ADG_TYPE_PATH
, NULL
);
153 * adg_path_get_cairo_path:
156 * Gets a pointer to the cairo path structure of @path. The return value
157 * is owned by @path and must be considered read-only.
159 * This function also converts %CAIRO_PATH_ARC_TO primitives, not
160 * recognized by cairo, into approximated Bézier curves. The conversion
161 * is cached so any furter request is O(1). This cache is cleared
162 * whenever @path is modified (by adding a new primitive or by calling
166 * <title>TODO</title>
168 * <listitem>Actually, the arcs are approximated to Bézier using the
169 * hardcoded max angle of PI/2. This should be customizable
170 * by adding, for instance, a property to the #AdgPath class
171 * with a default value of PI/2.</listitem>
175 * Return value: a pointer to the internal cairo path or %NULL on errors
178 adg_path_get_cairo_path(AdgPath
*path
)
180 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
182 return get_cairo_path(path
);
186 * adg_path_get_cpml_path:
189 * Gets a pointer to the cairo path structure of @path. The return
190 * value is owned by @path and must not be freed.
192 * This function is similar to adg_path_get_cairo_path() but with
193 * two important differences: firstly the arc primitives are not
194 * expanded to Bézier curves and secondly the returned path is
195 * not read-only. This means it is allowed to modify the returned
196 * path as long as its size is retained and its data contains a
199 * Keep in mind any changes to @path makes the value returned by
200 * this function useless, as it is likely to contain plain garbage.
202 * Return value: a pointer to the internal cpml path or %NULL on errors
205 adg_path_get_cpml_path(AdgPath
*path
)
207 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
209 clear_cairo_path(path
);
211 return get_cpml_path(path
);
215 * adg_path_get_current_point:
217 * @x: return value for x coordinate of the current point
218 * @y: return value for y coordinate of the current point
220 * Gets the current point of @path, which is conceptually the
221 * final point reached by the path so far.
223 * If there is no defined current point, @x and @y will both be set
224 * to 0 and a warning will be triggered. It is possible to check this
225 * in advance with adg_path_has_current_point().
227 * Most #AdgPath methods alter the current point and most of them
228 * expect a current point to be defined otherwise will fail triggering
229 * a warning. Check the description of every method for specific details.
232 adg_path_get_current_point(AdgPath
*path
, gdouble
*x
, gdouble
*y
)
234 g_return_if_fail(ADG_IS_PATH(path
));
236 if (path
->priv
->cp_is_valid
) {
237 *x
= path
->priv
->cp
.x
;
238 *y
= path
->priv
->cp
.y
;
241 g_return_if_reached();
246 * adg_path_has_current_point:
249 * Returns whether a current point is defined on @path.
250 * See adg_path_get_current_point() for details on the current point.
252 * Return value: whether a current point is defined
255 adg_path_has_current_point(AdgPath
*path
)
257 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
259 return path
->priv
->cp_is_valid
;
266 * Releases the internal memory hold by @path and resets its status,
267 * so that after this call @path contains an empty path.
270 adg_path_clear(AdgPath
*path
)
272 g_return_if_fail(ADG_IS_PATH(path
));
274 g_array_set_size(path
->priv
->path
, 0);
275 clear_cairo_path(path
);
276 clear_operation(path
);
283 * @type: a #cairo_data_type_t value
284 * @...: point data, specified as #AdgPair pointers
286 * Generic method to append a primitive to @path. The number of #AdgPair
287 * structs depends on @type: there is no way with this function to
288 * reserve more cairo_path_data_t structs than what is needed by the
291 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
293 * If @path has no current point while the requested primitive needs it,
294 * a warning message will be triggered without other effect.
297 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
301 va_start(var_args
, type
);
302 adg_path_append_valist(path
, type
, var_args
);
307 * adg_path_append_valist:
309 * @type: a #cairo_data_type_t value
310 * @var_args: point data, specified as #AdgPair pointers
312 * va_list version of adg_path_append().
315 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
317 AdgPrimitive primitive
;
319 cairo_path_data_t org
;
320 cairo_path_data_t
*data
;
322 g_return_if_fail(ADG_IS_PATH(path
));
324 length
= needed_pairs(type
, path
->priv
->cp_is_valid
);
328 /* Set a copy of the current point as the primitive origin */
329 cpml_pair_to_cairo(&path
->priv
->cp
, &org
);
330 primitive
.org
= &org
;
332 /* Build the cairo_path_data_t array */
333 primitive
.data
= data
= g_new(cairo_path_data_t
, length
);
335 data
->header
.type
= type
;
336 data
->header
.length
= length
;
338 for (cnt
= 1; cnt
< length
; ++ cnt
) {
340 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), data
);
343 /* Terminate the creation of the temporary primitive */
344 primitive
.segment
= NULL
;
346 /* Append this primitive to @path */
347 append_primitive(path
, &primitive
);
349 g_free(primitive
.data
);
353 * adg_path_append_primitive:
355 * @primitive: the #AdgPrimitive to append
357 * Appends @primitive to @path. The primitive to add is considered the
358 * continuation of the current path so the <structfield>org</structfield>
359 * component of @primitive is not used. Anyway the current poins is
360 * checked against it: they must be equal or the function will fail
361 * without further processing.
364 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
366 AdgPrimitive
*primitive_dup
;
368 g_return_if_fail(ADG_IS_PATH(path
));
369 g_return_if_fail(primitive
!= NULL
);
370 g_return_if_fail(primitive
->org
->point
.x
== path
->priv
->cp
.x
&&
371 primitive
->org
->point
.y
== path
->priv
->cp
.y
);
373 /* The primitive data could be modified by pending operations:
375 primitive_dup
= adg_primitive_deep_dup(primitive
);
377 append_primitive(path
, primitive_dup
);
379 g_free(primitive_dup
);
383 * adg_path_append_segment:
385 * @segment: the #AdgSegment to append
387 * Appends @segment to @path.
390 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
392 g_return_if_fail(ADG_IS_PATH(path
));
393 g_return_if_fail(segment
!= NULL
);
395 clear_cairo_path(path
);
396 path
->priv
->path
= g_array_append_vals(path
->priv
->path
,
402 * adg_path_append_cairo_path:
404 * @cairo_path: the #cairo_path_t path to append
406 * Appends a whole cairo path to @path.
409 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
411 g_return_if_fail(ADG_IS_PATH(path
));
413 clear_cairo_path(path
);
414 path
->priv
->path
= g_array_append_vals(path
->priv
->path
,
416 cairo_path
->num_data
);
422 * @x: the new x coordinate
423 * @y: the new y coordinate
425 * Begins a new segment. After this call the current point will be (@x, @y).
428 adg_path_move_to(AdgPath
*path
, gdouble x
, gdouble y
)
435 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
441 * @x: the new x coordinate
442 * @y: the new y coordinate
444 * Adds a line to @path from the current point to position (@x, @y).
445 * After this call the current point will be (@x, @y).
447 * If @path has no current point before this call, this function will
448 * trigger a warning without other effect.
451 adg_path_line_to(AdgPath
*path
, gdouble x
, gdouble y
)
458 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
464 * @x1: the x coordinate of an intermediate point
465 * @y1: the y coordinate of an intermediate point
466 * @x2: the x coordinate of the end of the arc
467 * @y2: the y coordinate of the end of the arc
469 * Adds an arc to the path from the current point to (@x2, @y2),
470 * passing throught (@x1, @y1). After this call the current point
471 * will be (@x2, @y2).
473 * If @path has no current point before this call, this function will
474 * trigger a warning without other effect.
477 adg_path_arc_to(AdgPath
*path
, gdouble x1
, gdouble y1
, gdouble x2
, gdouble y2
)
486 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
492 * @x1: the x coordinate of the first control point
493 * @y1: the y coordinate of the first control point
494 * @x2: the x coordinate of the second control point
495 * @y2: the y coordinate of the second control point
496 * @x3: the x coordinate of the end of the curve
497 * @y3: the y coordinate of the end of the curve
499 * Adds a cubic Bézier curve to the path from the current point to
500 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
501 * control points. After this call the current point will be (@x3, @y3).
503 * If @path has no current point before this call, this function will
504 * trigger a warning without other effect.
507 adg_path_curve_to(AdgPath
*path
, gdouble x1
, gdouble y1
,
508 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
519 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
526 * Adds a line segment to the path from the current point to the
527 * beginning of the current segment, (the most recent point passed
528 * to an adg_path_move_to()), and closes this segment.
529 * After this call the current point will be unset.
531 * The behavior of adg_path_close() is distinct from simply calling
532 * adg_line_to() with the coordinates of the segment starting point.
533 * When a closed segment is stroked, there are no caps on the ends.
534 * Instead, there is a line join connecting the final and initial
535 * primitive of the segment.
537 * If @path has no current point before this call, this function will
538 * trigger a warning without other effect.
541 adg_path_close(AdgPath
*path
)
543 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
549 * @xc: x position of the center of the arc
550 * @yc: y position of the center of the arc
551 * @r: the radius of the arc
552 * @start: the start angle, in radians
553 * @end: the end angle, in radians
555 * A more usual way to add an arc to @path. After this call, the current
556 * point will be the computed end point of the arc. The arc will be
557 * rendered in increasing angle, accordling to @start and @end. This means
558 * if @start is less than @end, the arc will be rendered in clockwise
559 * direction (accordling to the default cairo coordinate system) while if
560 * @start is greather than @end, the arc will be rendered in couterclockwise
563 * By explicitely setting the whole arc data, the start point could be
564 * different from the current point. In this case, if @path has no
565 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
566 * point of the arc will be automatically prepended to the arc.
567 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
568 * point of the arc will be used instead of the moveto.
571 adg_path_arc(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
572 gdouble start
, gdouble end
)
574 AdgPair center
, p
[3];
576 g_return_if_fail(ADG_IS_PATH(path
));
581 cpml_vector_from_angle(&p
[0], start
, r
);
582 cpml_vector_from_angle(&p
[1], (end
-start
) / 2, r
);
583 cpml_vector_from_angle(&p
[2], end
, r
);
585 cpml_pair_add(&p
[0], ¢er
);
586 cpml_pair_add(&p
[1], ¢er
);
587 cpml_pair_add(&p
[2], ¢er
);
589 if (!path
->priv
->cp_is_valid
)
590 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
591 else if (p
[0].x
!= path
->priv
->cp
.x
|| p
[0].y
!= path
->priv
->cp
.y
)
592 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
594 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
600 * @delta1: the distance from the intersection point of the current primitive
601 * @delta2: the distance from the intersection point of the next primitive
603 * A binary operator that generates a chamfer between two primitives.
604 * The first primitive involved is the current primitive, the second will
605 * be the next primitive appended to @path after this call. The second
606 * primitive is required: if the chamfer operation is not properly
607 * terminated (by not providing the second primitive), any API accessing
608 * the path in reading mode will raise a warning.
610 * The chamfer operation requires two lengths: @delta1 specifies the
611 * "quantity" to trim on the first primitive while @delta2 is the same
612 * applied on the second primitive. The term "quantity" means the length
613 * of the portion to cut out from the original primitive (that is the
614 * primitive as would be without the chamfer).
617 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
619 g_return_if_fail(ADG_IS_PATH(path
));
621 if (!append_operation(path
, ADG_OPERATOR_CHAMFER
, delta1
, delta2
))
628 * @radius: the radius of the fillet
631 * A binary operator that joins to primitives with an arc.
632 * The first primitive involved is the current primitive, the second will
633 * be the next primitive appended to @path after this call. The second
634 * primitive is required: if the fillet operation is not properly
635 * terminated (by not providing the second primitive), any API accessing
636 * the path in reading mode will raise a warning.
639 adg_path_fillet(AdgPath
*path
, gdouble radius
)
641 g_return_if_fail(ADG_IS_PATH(path
));
643 if (!append_operation(path
, ADG_OPERATOR_FILLET
, radius
))
652 * Dumps the data content of @path to stdout in a human readable format.
655 adg_path_dump(AdgPath
*path
)
658 cairo_path_t
*cairo_path
;
660 g_return_if_fail(ADG_IS_PATH(path
));
662 cairo_path
= get_cairo_path(path
);
664 g_return_if_fail(cairo_path
!= NULL
);
666 if (!cpml_segment_from_cairo(&segment
, cairo_path
)) {
667 g_warning("Invalid path data to dump!\n");
670 cpml_segment_dump(&segment
);
671 } while (cpml_segment_next(&segment
));
677 changed(AdgModel
*model
)
679 AdgModelClass
*model_class
= (AdgModelClass
*) adg_path_parent_class
;
681 adg_path_clear((AdgPath
*) model
);
683 if (model_class
->changed
!= NULL
)
684 model_class
->changed(model
);
688 clear_cairo_path(AdgPath
*path
)
690 cairo_path_t
*cairo_path
= &path
->priv
->cairo_path
;
692 if (cairo_path
->data
== NULL
)
695 g_free(cairo_path
->data
);
697 cairo_path
->status
= CAIRO_STATUS_INVALID_PATH_DATA
;
698 cairo_path
->data
= NULL
;
699 cairo_path
->num_data
= 0;
702 static cairo_path_t
*
703 get_cairo_path(AdgPath
*path
)
705 cairo_path_t
*cairo_path
;
708 const cairo_path_data_t
*p_src
;
711 /* Check for cached result */
712 cairo_path
= &path
->priv
->cairo_path
;
713 if (cairo_path
->data
!= NULL
)
716 src
= path
->priv
->path
;
717 dst
= g_array_sized_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
), src
->len
);
719 /* Cycle the path and convert arcs to Bézier curves */
720 for (i
= 0; i
< src
->len
; i
+= p_src
->header
.length
) {
721 p_src
= (const cairo_path_data_t
*) src
->data
+ i
;
723 if (p_src
->header
.type
== CAIRO_PATH_ARC_TO
)
724 dst
= arc_to_curves(dst
, p_src
);
726 dst
= g_array_append_vals(dst
, p_src
, p_src
->header
.length
);
729 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
730 cairo_path
->num_data
= dst
->len
;
731 cairo_path
->data
= (cairo_path_data_t
*) g_array_free(dst
, FALSE
);
736 static cairo_path_t
*
737 get_cpml_path(AdgPath
*path
)
739 cairo_path_t
*cpml_path
= &path
->priv
->cpml_path
;
741 cpml_path
->status
= CAIRO_STATUS_SUCCESS
;
742 cpml_path
->data
= (cairo_path_data_t
*) path
->priv
->path
->data
;
743 cpml_path
->num_data
= path
->priv
->path
->len
;
749 arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
)
754 /* Build the arc primitive: the arc origin is supposed to be the previous
755 * point (src-1): this means a primitive must exist before the arc */
757 arc
.org
= (cairo_path_data_t
*) (src
-1);
758 arc
.data
= (cairo_path_data_t
*) src
;
760 if (cpml_arc_info(&arc
, NULL
, NULL
, &start
, &end
)) {
763 cairo_path_data_t
*curves
;
765 n_curves
= ceil(fabs(end
-start
) / M_PI_2
);
766 curves
= g_new(cairo_path_data_t
, n_curves
* 4);
767 segment
.data
= curves
;
768 cpml_arc_to_curves(&arc
, &segment
, n_curves
);
770 array
= g_array_append_vals(array
, curves
, n_curves
* 4);
779 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
781 AdgPathPrivate
*priv
= path
->priv
;
782 cairo_path_data_t
*data
;
786 data
= current
->data
;
787 length
= data
[0].header
.length
;
789 /* Execute any pending operation */
790 do_operation(path
, data
);
792 /* Append the cairo data to the internal path array */
793 priv
->path
= g_array_append_vals(priv
->path
, data
, length
);
795 /* Set data to point to the recently appended cairo_path_data_t
796 * primitive: the first struct is the header */
797 data
= (cairo_path_data_t
*) priv
->path
->data
+ priv
->path
->len
- length
;
799 /* Set the last primitive for subsequent binary operations */
800 priv
->last
.org
= priv
->cp_is_valid
? data
- 1 : NULL
;
801 priv
->last
.segment
= NULL
;
802 priv
->last
.data
= data
;
804 /* Save the last point as the current point, if applicable */
805 priv
->cp_is_valid
= length
> 1;
807 cpml_pair_from_cairo(&priv
->cp
, &data
[length
-1]);
809 /* Invalidate cairo_path: should be recomputed */
810 clear_cairo_path(path
);
814 needed_pairs(CpmlPrimitiveType type
, gboolean cp_is_valid
)
818 case CAIRO_PATH_CLOSE_PATH
:
819 g_return_val_if_fail(cp_is_valid
, 0);
822 case CAIRO_PATH_MOVE_TO
:
825 case CAIRO_PATH_LINE_TO
:
826 g_return_val_if_fail(cp_is_valid
, 0);
829 case CAIRO_PATH_ARC_TO
:
830 g_return_val_if_fail(cp_is_valid
, 0);
833 case CAIRO_PATH_CURVE_TO
:
834 g_return_val_if_fail(cp_is_valid
, 0);
838 g_return_val_if_reached(0);
845 clear_operation(AdgPath
*path
)
847 AdgOperation
*operation
= &path
->priv
->operation
;
849 if (operation
->operator == ADG_OPERATOR_NONE
)
852 g_warning("An operation is still active while clearing the path "
853 "(operator `%d')", operation
->operator);
854 operation
->operator = ADG_OPERATOR_NONE
;
858 append_operation(AdgPath
*path
, AdgOperator
operator, ...)
860 AdgOperation
*operation
;
863 if (!path
->priv
->cp_is_valid
) {
864 g_warning("Operation requested but path has no current primitive "
865 "(operator `%d')", operator);
869 operation
= &path
->priv
->operation
;
870 if (operation
->operator != ADG_OPERATOR_NONE
) {
871 /* TODO: this is a rude semplification, as a lot of operators can
872 * and may cohexist. As an example, a fillet followed by a
873 * polar chamfer is not difficult to compute */
874 g_warning("Operation requested but another operation is yet active"
875 "(operators: new `%d', old `%d')",
876 operator, operation
->operator);
880 va_start(var_args
, operator);
884 case ADG_OPERATOR_CHAMFER
:
885 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
886 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
889 case ADG_OPERATOR_FILLET
:
890 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
893 case ADG_OPERATOR_NONE
:
898 g_warning("Operation not recognized (operator `%d')", operator);
903 operation
->operator = operator;
910 do_operation(AdgPath
*path
, cairo_path_data_t
*data
)
912 AdgPathPrivate
*priv
;
913 AdgOperator
operator;
915 CpmlPrimitive current
;
916 cairo_path_data_t current_org
;
919 operator = priv
->operation
.operator;
920 cpml_segment_from_cairo(&segment
, get_cpml_path(path
));
922 /* Construct the current primitive, that is the primitive to be inserted.
923 * Its org is a copy of the end point of the last primitive: it can be
924 * modified without affecting anything else. It is expected the operation
925 * functions will add to @path the primitives required but NOT to add
926 * @current, as this one will be inserted automatically. */
927 current
.segment
= &segment
;
928 current
.org
= ¤t_org
;
930 cpml_pair_to_cairo(&priv
->cp
, ¤t_org
);
934 case ADG_OPERATOR_NONE
:
937 case ADG_OPERATOR_CHAMFER
:
938 do_chamfer(path
, ¤t
);
941 case ADG_OPERATOR_FILLET
:
942 do_fillet(path
, ¤t
);
946 g_warning("Operation not implemented (operator `%d')", operator);
952 do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
954 AdgPathPrivate
*priv
;
956 gdouble delta1
, delta2
;
959 cairo_path_data_t line
[2];
963 delta1
= priv
->operation
.data
.chamfer
.delta1
;
964 len1
= cpml_primitive_length(last
);
966 if (delta1
>= len1
) {
967 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
972 delta2
= priv
->operation
.data
.chamfer
.delta2
;
973 len2
= cpml_primitive_length(current
);
975 if (delta2
>= len2
) {
976 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
981 /* Change the end point of the last primitive */
982 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
983 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
985 /* Change the start point of the current primitive */
986 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
987 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
989 /* Add the chamfer line */
990 line
[0].header
.type
= CAIRO_PATH_LINE_TO
;
991 line
[0].header
.length
= 2;
992 line
[1].point
.x
= pair
.x
;
993 line
[1].point
.y
= pair
.y
;
994 priv
->path
= g_array_append_vals(priv
->path
, line
, 2);
996 priv
->operation
.operator = ADG_OPERATOR_NONE
;
1000 do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1002 AdgPathPrivate
*priv
;
1003 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1004 gdouble radius
, offset
, pos
;
1005 AdgPair center
, vector
, p
[3];
1006 cairo_path_data_t arc
[3];
1010 current_dup
= adg_primitive_deep_dup(current
);
1012 /* Force current_dup to point to the original segment so a
1013 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1014 current_dup
->segment
= current
->segment
;
1016 last_dup
= adg_primitive_deep_dup(last
);
1017 radius
= priv
->operation
.data
.fillet
.radius
;
1018 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1020 /* Find the center of the fillet from the intersection between
1021 * the last and current primitives offseted by radius */
1022 cpml_primitive_offset(current_dup
, offset
);
1023 cpml_primitive_offset(last_dup
, offset
);
1024 if (cpml_primitive_intersection(current_dup
, last_dup
,
1026 g_warning("Fillet not applicable (radius = %lf)", radius
);
1027 g_free(current_dup
);
1032 /* Compute the start point of the fillet */
1033 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1034 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1035 cpml_vector_set_length(&vector
, offset
);
1036 cpml_vector_normal(&vector
);
1037 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1039 /* Compute the mid point of the fillet */
1040 cpml_pair_from_cairo(&vector
, current
->org
);
1041 cpml_pair_sub(&vector
, ¢er
);
1042 cpml_vector_set_length(&vector
, radius
);
1043 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1045 /* Compute the end point of the fillet */
1046 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1047 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1048 cpml_vector_set_length(&vector
, offset
);
1049 cpml_vector_normal(&vector
);
1050 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1052 g_free(current_dup
);
1055 /* Modify the end point of the last primitive */
1056 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1058 /* Add the fillet arc */
1059 arc
[0].header
.type
= CAIRO_PATH_ARC_TO
;
1060 arc
[0].header
.length
= 3;
1061 cpml_pair_to_cairo(&p
[1], &arc
[1]);
1062 cpml_pair_to_cairo(&p
[2], &arc
[2]);
1063 priv
->path
= g_array_append_vals(priv
->path
, arc
, 3);
1065 priv
->operation
.operator = ADG_OPERATOR_NONE
;
1069 is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1072 gdouble angle1
, angle2
;
1074 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1075 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1077 /* Probably there is a smarter way to get this without trygonometry */
1078 angle1
= cpml_vector_angle(&v1
);
1079 angle2
= cpml_vector_angle(&v2
);
1081 if (angle1
> angle2
)
1084 return angle2
-angle1
> M_PI
;