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.
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.
24 * @short_description: The basic model representing a generic path
26 * The #AdgPath model is a virtual path: in a few words, it is a
27 * simple conceptual #cairo_path_t struct. This class implements
28 * methods to manipulate the underlying cairo path.
30 * Although some of the provided methods are clearly based on the
31 * original cairo path manipulation API, their behavior could be
32 * sligthly different. This is intentional, because the ADG provides
33 * additional path manipulation algorithms, sometime quite complex,
34 * and a more restrictive filter on the path quality is required.
35 * Also, the ADG is designed to be used by technicians while cairo
36 * targets a broader range of developers.
38 * As an example, following the rule of the less surprise, some
39 * cairo functions guess the current point when it is not defined,
40 * while the #AdgPath methods trigger a warning without other effect.
41 * Furthermore, after a cairo_path_close_path() call a MOVE_TO
42 * primitive to the starting point of the segment is automatically
43 * added by cairo while in the ADG, after an adg_path_close(), the
44 * current point is simply unset.
48 #include "adg-path-private.h"
49 #include "adg-primitive.h"
55 static void finalize (GObject
*object
);
56 static void changed (AdgModel
*model
);
57 static void clear_cairo_path (AdgPath
*path
);
58 static cairo_path_t
* get_cairo_path (AdgPath
*path
);
59 static cairo_path_t
* get_cpml_path (AdgPath
*path
);
60 static GArray
* arc_to_curves (GArray
*array
,
61 const cairo_path_data_t
63 static void append_primitive_valist (AdgPath
*path
,
64 cairo_path_data_type_t
68 static cairo_path_data_t
*
69 create_nodes_valist (cairo_path_data_type_t
73 static void clear_operation (AdgPath
*path
);
74 static gboolean
append_operation (AdgPath
*path
,
77 static void do_operation (AdgPath
*path
,
80 static void do_chamfer (AdgPath
*path
,
81 CpmlPrimitive
*current
);
82 static void do_fillet (AdgPath
*path
,
83 CpmlPrimitive
*current
);
84 static gboolean
is_convex (const CpmlPrimitive
90 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_MODEL
);
94 adg_path_class_init(AdgPathClass
*klass
)
96 GObjectClass
*gobject_class
;
97 AdgModelClass
*model_class
;
99 gobject_class
= (GObjectClass
*) klass
;
100 model_class
= (AdgModelClass
*) klass
;
102 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
104 gobject_class
->finalize
= finalize
;
106 model_class
->changed
= changed
;
110 adg_path_init(AdgPath
*path
)
112 AdgPathPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
115 priv
->cp_is_valid
= FALSE
;
116 priv
->path
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
117 priv
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
118 priv
->cairo_path
.data
= NULL
;
119 priv
->cairo_path
.num_data
= 0;
120 priv
->operation
.operator = ADG_OPERATOR_NONE
;
126 finalize(GObject
*object
)
129 GObjectClass
*object_class
;
131 path
= (AdgPath
*) object
;
132 object_class
= (GObjectClass
*) adg_path_parent_class
;
134 g_array_free(path
->priv
->path
, TRUE
);
135 clear_cairo_path(path
);
136 clear_operation(path
);
138 if (object_class
->finalize
!= NULL
)
139 object_class
->finalize(object
);
146 * Creates a new path model. The path must be constructed in the @callback
147 * function: AdgPath will cache and reuse the cairo_copy_path() returned by
148 * the cairo context after the @callback call.
150 * Return value: the new model
155 return (AdgModel
*) g_object_new(ADG_TYPE_PATH
, NULL
);
160 * adg_path_get_cairo_path:
163 * Gets a pointer to the cairo path structure of @path. The return value
164 * is owned by @path and must be considered read-only.
166 * This function also converts %CAIRO_PATH_ARC_TO primitives, not
167 * recognized by cairo, into approximated Bézier curves. The conversion
168 * is cached so any furter request is O(1). This cache is cleared
169 * whenever @path is modified (by adding a new primitive or by calling
173 * <title>TODO</title>
175 * <listitem>Actually, the arcs are approximated to Bézier using the
176 * hardcoded max angle of PI/2. This should be customizable
177 * by adding, for instance, a property to the #AdgPath class
178 * with a default value of PI/2.</listitem>
182 * Return value: a pointer to the internal cairo path or %NULL on errors
185 adg_path_get_cairo_path(AdgPath
*path
)
187 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
189 return get_cairo_path(path
);
193 * adg_path_get_cpml_path:
196 * Gets a pointer to the cairo path structure of @path. The return
197 * value is owned by @path and must not be freed.
199 * This function is similar to adg_path_get_cairo_path() but with
200 * two important differences: firstly the arc primitives are not
201 * expanded to Bézier curves and secondly the returned path is
202 * not read-only. This means it is allowed to modify the returned
203 * path as long as its size is retained and its data contains a
206 * Keep in mind any changes to @path makes the value returned by
207 * this function useless, as it is likely to contain plain garbage.
209 * Return value: a pointer to the internal cpml path or %NULL on errors
212 adg_path_get_cpml_path(AdgPath
*path
)
214 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
216 clear_cairo_path(path
);
218 return get_cpml_path(path
);
222 * adg_path_get_current_point:
224 * @x: return value for x coordinate of the current point
225 * @y: return value for y coordinate of the current point
227 * Gets the current point of @path, which is conceptually the
228 * final point reached by the path so far.
230 * If there is no defined current point, @x and @y will both be set
231 * to 0 and a warning will be triggered. It is possible to check this
232 * in advance with adg_path_has_current_point().
234 * Most #AdgPath methods alter the current point and most of them
235 * expect a current point to be defined otherwise will fail triggering
236 * a warning. Check the description of every method for specific details.
239 adg_path_get_current_point(AdgPath
*path
, gdouble
*x
, gdouble
*y
)
241 g_return_if_fail(ADG_IS_PATH(path
));
243 if (path
->priv
->cp_is_valid
) {
244 *x
= path
->priv
->cp
.x
;
245 *y
= path
->priv
->cp
.y
;
248 g_return_if_reached();
253 * adg_path_has_current_point:
256 * Returns whether a current point is defined on @path.
257 * See adg_path_get_current_point() for details on the current point.
259 * Return value: whether a current point is defined
262 adg_path_has_current_point(AdgPath
*path
)
264 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
266 return path
->priv
->cp_is_valid
;
273 * Releases the internal memory hold by @path and resets its status,
274 * so that after this call @path contains an empty path.
277 adg_path_clear(AdgPath
*path
)
279 g_return_if_fail(ADG_IS_PATH(path
));
281 g_array_set_size(path
->priv
->path
, 0);
282 clear_cairo_path(path
);
283 clear_operation(path
);
290 * @type: a #cairo_data_type_t value
291 * @...: point data, specified as #AdgPair pointers
293 * Generic method to append a primitive to @path. The number of #AdgPair
294 * structs depends on @type: there is no way with this function to
295 * reserve more cairo_path_data_t structs than what is needed by the
298 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
300 * If @path has no current point while the requested primitive needs it,
301 * a warning message will be triggered without other effect.
304 adg_path_append(AdgPath
*path
, cairo_path_data_type_t type
, ...)
308 va_start(var_args
, type
);
309 adg_path_append_valist(path
, type
, var_args
);
314 * adg_path_append_valist:
316 * @type: a #cairo_data_type_t value
317 * @var_args: point data, specified as #AdgPair pointers
319 * va_list version of adg_path_append().
322 adg_path_append_valist(AdgPath
*path
, cairo_path_data_type_t type
,
327 g_return_if_fail(ADG_IS_PATH(path
));
331 case CAIRO_PATH_CLOSE_PATH
:
332 g_return_if_fail(path
->priv
->cp_is_valid
);
336 case CAIRO_PATH_MOVE_TO
:
340 case CAIRO_PATH_LINE_TO
:
341 g_return_if_fail(path
->priv
->cp_is_valid
);
345 case CAIRO_PATH_ARC_TO
:
346 g_return_if_fail(path
->priv
->cp_is_valid
);
350 case CAIRO_PATH_CURVE_TO
:
351 g_return_if_fail(path
->priv
->cp_is_valid
);
356 g_assert_not_reached();
360 append_primitive_valist(path
, type
, length
, var_args
);
364 * adg_path_append_primitive:
366 * @primitive: the #AdgPrimitive to append
368 * Appends @primitive to @path. The primitive to add is considered the
369 * continuation of the current path so the <structfield>org</structfield>
370 * component of @primitive is not used. Anyway the current poins is
371 * checked against it: they must be equal or the function will fail
372 * without further processing.
375 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
377 g_return_if_fail(ADG_IS_PATH(path
));
378 g_return_if_fail(primitive
!= NULL
);
379 g_return_if_fail(primitive
->org
->point
.x
!= path
->priv
->cp
.x
||
380 primitive
->org
->point
.y
!= path
->priv
->cp
.y
);
382 /* Check for empty primitive */
383 if (primitive
->data
== NULL
)
386 clear_cairo_path(path
);
387 path
->priv
->path
= g_array_append_vals(path
->priv
->path
,
389 primitive
->data
[0].header
.length
);
393 * adg_path_append_segment:
395 * @segment: the #AdgSegment to append
397 * Appends @segment to @path.
400 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
402 g_return_if_fail(ADG_IS_PATH(path
));
403 g_return_if_fail(segment
!= NULL
);
405 clear_cairo_path(path
);
406 path
->priv
->path
= g_array_append_vals(path
->priv
->path
,
412 * adg_path_append_cairo_path:
414 * @cairo_path: the #cairo_path_t path to append
416 * Appends a whole cairo path to @path.
419 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
421 g_return_if_fail(ADG_IS_PATH(path
));
423 clear_cairo_path(path
);
424 path
->priv
->path
= g_array_append_vals(path
->priv
->path
,
426 cairo_path
->num_data
);
433 * @x: the new x coordinate
434 * @y: the new y coordinate
436 * Begins a new segment. After this call the current point will be (@x, @y).
439 adg_path_move_to(AdgPath
*path
, gdouble x
, gdouble y
)
446 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
452 * @x: the new x coordinate
453 * @y: the new y coordinate
455 * Adds a line to @path from the current point to position (@x, @y).
456 * After this call the current point will be (@x, @y).
458 * If @path has no current point before this call, this function will
459 * trigger a warning without other effect.
462 adg_path_line_to(AdgPath
*path
, gdouble x
, gdouble y
)
469 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
475 * @x1: the x coordinate of an intermediate point
476 * @y1: the y coordinate of an intermediate point
477 * @x2: the x coordinate of the end of the arc
478 * @y2: the y coordinate of the end of the arc
480 * Adds an arc to the path from the current point to (@x2, @y2),
481 * passing throught (@x1, @y1). After this call the current point
482 * will be (@x2, @y2).
484 * If @path has no current point before this call, this function will
485 * trigger a warning without other effect.
488 adg_path_arc_to(AdgPath
*path
, gdouble x1
, gdouble y1
, gdouble x2
, gdouble y2
)
497 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
503 * @x1: the x coordinate of the first control point
504 * @y1: the y coordinate of the first control point
505 * @x2: the x coordinate of the second control point
506 * @y2: the y coordinate of the second control point
507 * @x3: the x coordinate of the end of the curve
508 * @y3: the y coordinate of the end of the curve
510 * Adds a cubic Bézier curve to the path from the current point to
511 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
512 * control points. After this call the current point will be (@x3, @y3).
514 * If @path has no current point before this call, this function will
515 * trigger a warning without other effect.
518 adg_path_curve_to(AdgPath
*path
, gdouble x1
, gdouble y1
,
519 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
530 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
537 * Adds a line segment to the path from the current point to the
538 * beginning of the current segment, (the most recent point passed
539 * to an adg_path_move_to()), and closes this segment.
540 * After this call the current point will be unset.
542 * The behavior of adg_path_close() is distinct from simply calling
543 * adg_line_to() with the coordinates of the segment starting point.
544 * When a closed segment is stroked, there are no caps on the ends.
545 * Instead, there is a line join connecting the final and initial
546 * primitive of the segment.
548 * If @path has no current point before this call, this function will
549 * trigger a warning without other effect.
552 adg_path_close(AdgPath
*path
)
554 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
560 * @xc: x position of the center of the arc
561 * @yc: y position of the center of the arc
562 * @r: the radius of the arc
563 * @start: the start angle, in radians
564 * @end: the end angle, in radians
566 * A more usual way to add an arc to @path. After this call, the current
567 * point will be the computed end point of the arc. The arc will be
568 * rendered in increasing angle, accordling to @start and @end. This means
569 * if @start is less than @end, the arc will be rendered in clockwise
570 * direction (accordling to the default cairo coordinate system) while if
571 * @start is greather than @end, the arc will be rendered in couterclockwise
574 * By explicitely setting the whole arc data, the start point could be
575 * different from the current point. In this case, if @path has no
576 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
577 * point of the arc will be automatically prepended to the arc.
578 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
579 * point of the arc will be used instead of the moveto.
582 adg_path_arc(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
583 gdouble start
, gdouble end
)
585 AdgPair center
, p
[3];
587 g_return_if_fail(ADG_IS_PATH(path
));
592 cpml_vector_from_angle(&p
[0], start
, r
);
593 cpml_vector_from_angle(&p
[1], (end
-start
) / 2, r
);
594 cpml_vector_from_angle(&p
[2], end
, r
);
596 cpml_pair_add(&p
[0], ¢er
);
597 cpml_pair_add(&p
[1], ¢er
);
598 cpml_pair_add(&p
[2], ¢er
);
600 if (!path
->priv
->cp_is_valid
)
601 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
602 else if (p
[0].x
!= path
->priv
->cp
.x
|| p
[0].y
!= path
->priv
->cp
.y
)
603 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
605 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
611 * @delta1: the distance from the intersection point of the current primitive
612 * @delta2: the distance from the intersection point of the next primitive
614 * A binary operator that generates a chamfer between two primitives.
615 * The first primitive involved is the current primitive, the second will
616 * be the next primitive appended to @path after this call. The second
617 * primitive is required: if the chamfer operation is not properly
618 * terminated (by not providing the second primitive), any API accessing
619 * the path in reading mode will raise a warning.
621 * The chamfer operation requires two lengths: @delta1 specifies the
622 * "quantity" to trim on the first primitive while @delta2 is the same
623 * applied on the second primitive. The term "quantity" means the length
624 * of the portion to cut out from the original primitive (that is the
625 * primitive as would be without the chamfer).
628 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
630 g_return_if_fail(ADG_IS_PATH(path
));
632 if (!append_operation(path
, ADG_OPERATOR_CHAMFER
, delta1
, delta2
))
639 * @radius: the radius of the fillet
642 * A binary operator that joins to primitives with an arc.
643 * The first primitive involved is the current primitive, the second will
644 * be the next primitive appended to @path after this call. The second
645 * primitive is required: if the fillet operation is not properly
646 * terminated (by not providing the second primitive), any API accessing
647 * the path in reading mode will raise a warning.
650 adg_path_fillet(AdgPath
*path
, gdouble radius
)
652 g_return_if_fail(ADG_IS_PATH(path
));
654 if (!append_operation(path
, ADG_OPERATOR_FILLET
, radius
))
663 * Dumps the data content of @path to stdout in a human readable format.
666 adg_path_dump(AdgPath
*path
)
669 cairo_path_t
*cairo_path
;
671 g_return_if_fail(ADG_IS_PATH(path
));
673 cairo_path
= get_cairo_path(path
);
675 g_return_if_fail(cairo_path
!= NULL
);
677 if (!cpml_segment_from_cairo(&segment
, cairo_path
)) {
678 g_warning("Invalid path data to dump!\n");
681 cpml_segment_dump(&segment
);
682 } while (cpml_segment_next(&segment
));
688 changed(AdgModel
*model
)
690 AdgModelClass
*model_class
= (AdgModelClass
*) adg_path_parent_class
;
692 adg_path_clear((AdgPath
*) model
);
694 if (model_class
->changed
!= NULL
)
695 model_class
->changed(model
);
699 clear_cairo_path(AdgPath
*path
)
701 cairo_path_t
*cairo_path
= &path
->priv
->cairo_path
;
703 if (cairo_path
->data
== NULL
)
706 g_free(cairo_path
->data
);
708 cairo_path
->status
= CAIRO_STATUS_INVALID_PATH_DATA
;
709 cairo_path
->data
= NULL
;
710 cairo_path
->num_data
= 0;
713 static cairo_path_t
*
714 get_cairo_path(AdgPath
*path
)
716 cairo_path_t
*cairo_path
;
719 const cairo_path_data_t
*p_src
;
722 /* Check for cached result */
723 cairo_path
= &path
->priv
->cairo_path
;
724 if (cairo_path
->data
!= NULL
)
727 src
= path
->priv
->path
;
728 dst
= g_array_sized_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
), src
->len
);
730 /* Cycle the path and convert arcs to Bézier curves */
731 for (i
= 0; i
< src
->len
; i
+= p_src
->header
.length
) {
732 p_src
= (const cairo_path_data_t
*) src
->data
+ i
;
734 if (p_src
->header
.type
== CAIRO_PATH_ARC_TO
)
735 dst
= arc_to_curves(dst
, p_src
);
737 dst
= g_array_append_vals(dst
, p_src
, p_src
->header
.length
);
740 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
741 cairo_path
->num_data
= dst
->len
;
742 cairo_path
->data
= (cairo_path_data_t
*) g_array_free(dst
, FALSE
);
747 static cairo_path_t
*
748 get_cpml_path(AdgPath
*path
)
750 cairo_path_t
*cpml_path
= &path
->priv
->cpml_path
;
752 cpml_path
->status
= CAIRO_STATUS_SUCCESS
;
753 cpml_path
->data
= (cairo_path_data_t
*) path
->priv
->path
->data
;
754 cpml_path
->num_data
= path
->priv
->path
->len
;
760 arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
)
765 /* Build the arc primitive: the arc origin is supposed to be the previous
766 * point (src-1): this means a primitive must exist before the arc */
768 arc
.org
= (cairo_path_data_t
*) (src
-1);
769 arc
.data
= (cairo_path_data_t
*) src
;
771 if (cpml_arc_info(&arc
, NULL
, NULL
, &start
, &end
)) {
774 cairo_path_data_t
*curves
;
776 n_curves
= ceil(fabs(end
-start
) / M_PI_2
);
777 curves
= g_new(cairo_path_data_t
, n_curves
* 4);
778 segment
.data
= curves
;
779 cpml_arc_to_curves(&arc
, &segment
, n_curves
);
781 array
= g_array_append_vals(array
, curves
, n_curves
* 4);
790 append_primitive_valist(AdgPath
*path
, cairo_path_data_type_t type
,
791 int length
, va_list var_args
)
793 AdgPathPrivate
*priv
;
794 cairo_path_data_t
*nodes
;
797 nodes
= create_nodes_valist(type
, length
, var_args
);
799 /* Execute any pending operation */
800 do_operation(path
, nodes
);
802 /* Append the nodes to the internal path array */
803 priv
->path
= g_array_append_vals(priv
->path
, nodes
, length
);
805 nodes
= (cairo_path_data_t
*) priv
->path
->data
+ priv
->path
->len
- length
;
807 /* Set the last primitive for subsequent binary operations */
808 priv
->last
.org
= priv
->cp_is_valid
? nodes
- 1 : NULL
;
809 priv
->last
.segment
= NULL
;
810 priv
->last
.data
= nodes
;
812 /* Save the last point as the current point, if applicable */
813 priv
->cp_is_valid
= length
> 1;
815 cpml_pair_from_cairo(&priv
->cp
, &nodes
[length
-1]);
817 /* Invalidate cairo_path: should be recomputed */
818 clear_cairo_path(path
);
821 static cairo_path_data_t
*
822 create_nodes_valist(cairo_path_data_type_t type
, int length
, va_list var_args
)
824 cairo_path_data_t
*node
, *nodes
;
826 node
= nodes
= g_new(cairo_path_data_t
, length
);
828 /* Append the header item */
829 node
->header
.type
= type
;
830 node
->header
.length
= length
;
832 /* Append the data items (that is, the AdgPair points) */
835 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), node
);
842 clear_operation(AdgPath
*path
)
844 AdgOperation
*operation
= &path
->priv
->operation
;
846 if (operation
->operator == ADG_OPERATOR_NONE
)
849 g_warning("An operation is still active while clearing the path "
850 "(operator `%d')", operation
->operator);
851 operation
->operator = ADG_OPERATOR_NONE
;
855 append_operation(AdgPath
*path
, AdgOperator
operator, ...)
857 AdgOperation
*operation
;
860 if (!path
->priv
->cp_is_valid
) {
861 g_warning("Operation requested but path has no current primitive "
862 "(operator `%d')", operator);
866 operation
= &path
->priv
->operation
;
867 if (operation
->operator != ADG_OPERATOR_NONE
) {
868 /* TODO: this is a rude semplification, as a lot of operators can
869 * and may cohexist. As an example, a fillet followed by a
870 * polar chamfer is not difficult to compute */
871 g_warning("Operation requested but another operation is yet active"
872 "(operators: new `%d', old `%d')",
873 operator, operation
->operator);
877 va_start(var_args
, operator);
881 case ADG_OPERATOR_CHAMFER
:
882 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
883 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
886 case ADG_OPERATOR_FILLET
:
887 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
890 case ADG_OPERATOR_NONE
:
895 g_warning("Operation not recognized (operator `%d')", operator);
900 operation
->operator = operator;
907 do_operation(AdgPath
*path
, cairo_path_data_t
*data
)
909 AdgPathPrivate
*priv
;
910 AdgOperator
operator;
911 CpmlPrimitive current
;
912 cairo_path_data_t current_org
;
915 operator = priv
->operation
.operator;
917 /* Construct the current primitive, that is the primitive to be inserted.
918 * Its org is a copy of the end point of the last primitive: it can be
919 * modified without affecting anything else. It is expected the operation
920 * functions will add to @path the primitives required but NOT to add
921 * @current, as this one will be inserted automatically. */
922 current
.segment
= NULL
;
923 current
.org
= ¤t_org
;
925 cpml_pair_to_cairo(&priv
->cp
, ¤t_org
);
929 case ADG_OPERATOR_NONE
:
932 case ADG_OPERATOR_CHAMFER
:
933 do_chamfer(path
, ¤t
);
936 case ADG_OPERATOR_FILLET
:
937 do_fillet(path
, ¤t
);
941 g_warning("Operation not implemented (operator `%d')", operator);
947 do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
949 AdgPathPrivate
*priv
;
951 gdouble delta1
, delta2
;
954 cairo_path_data_t line
[2];
958 delta1
= priv
->operation
.data
.chamfer
.delta1
;
959 len1
= cpml_primitive_length(last
);
961 if (delta1
>= len1
) {
962 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
967 delta2
= priv
->operation
.data
.chamfer
.delta2
;
968 len2
= cpml_primitive_length(current
);
970 if (delta2
>= len2
) {
971 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
976 /* Change the end point of the last primitive */
977 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
978 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
980 /* Change the start point of the current primitive */
981 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
982 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
984 /* Add the chamfer line */
985 line
[0].header
.type
= CAIRO_PATH_LINE_TO
;
986 line
[0].header
.length
= 2;
987 line
[1].point
.x
= pair
.x
;
988 line
[1].point
.y
= pair
.y
;
989 priv
->path
= g_array_append_vals(priv
->path
, line
, 2);
991 priv
->operation
.operator = ADG_OPERATOR_NONE
;
995 do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
997 AdgPathPrivate
*priv
;
998 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
999 gdouble radius
, offset
, pos
;
1000 AdgPair center
, vector
, p
[3];
1001 cairo_path_data_t arc
[3];
1005 current_dup
= adg_primitive_deep_dup(current
);
1006 last_dup
= adg_primitive_deep_dup(last
);
1007 radius
= priv
->operation
.data
.fillet
.radius
;
1008 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1010 /* Find the center of the fillet from the intersection between
1011 * the last and current primitives offseted by radius */
1012 cpml_primitive_offset(current_dup
, offset
);
1013 cpml_primitive_offset(last_dup
, offset
);
1014 if (cpml_primitive_intersection(current_dup
, last_dup
,
1016 g_warning("Fillet not applicable (radius = %lf)", radius
);
1017 g_free(current_dup
);
1022 /* Compute the start point of the fillet */
1023 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1024 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1025 cpml_vector_set_length(&vector
, offset
);
1026 cpml_vector_normal(&vector
);
1027 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1029 /* Compute the mid point of the fillet */
1030 cpml_pair_from_cairo(&vector
, current
->org
);
1031 cpml_pair_sub(&vector
, ¢er
);
1032 cpml_vector_set_length(&vector
, radius
);
1033 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1035 /* Compute the ent point of the fillet */
1036 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1037 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1038 cpml_vector_set_length(&vector
, offset
);
1039 cpml_vector_normal(&vector
);
1040 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1042 g_free(current_dup
);
1045 /* Modify the end point of the last primitive */
1046 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1048 /* Add the fillet arc */
1049 arc
[0].header
.type
= CAIRO_PATH_ARC_TO
;
1050 arc
[0].header
.length
= 3;
1051 cpml_pair_to_cairo(&p
[1], &arc
[1]);
1052 cpml_pair_to_cairo(&p
[2], &arc
[2]);
1053 priv
->path
= g_array_append_vals(priv
->path
, arc
, 3);
1055 priv
->operation
.operator = ADG_OPERATOR_NONE
;
1059 is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1063 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1064 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1066 /* I think this is a naive (and wrong) approach: test case needed */
1067 return cpml_vector_angle(&v1
) > cpml_vector_angle(&v2
);