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.
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 ADG, after an adg_path_close(), the
43 * current point is simply unset.
49 * All fields are private and should not be used directly.
50 * Use its public methods instead.
55 #include "adg-path-private.h"
56 #include "adg-primitive.h"
62 static void finalize (GObject
*object
);
63 static void changed (AdgModel
*model
);
64 static void clear_cairo_path (AdgPath
*path
);
65 static cairo_path_t
* get_cairo_path (AdgPath
*path
);
66 static cairo_path_t
* get_cpml_path (AdgPath
*path
);
67 static GArray
* arc_to_curves (GArray
*array
,
68 const cairo_path_data_t
70 static void append_primitive (AdgPath
*path
,
71 AdgPrimitive
*primitive
);
72 static gint
needed_pairs (CpmlPrimitiveType type
,
73 gboolean cp_is_valid
);
74 static void clear_operation (AdgPath
*path
);
75 static gboolean
append_operation (AdgPath
*path
,
78 static void do_operation (AdgPath
*path
,
81 static void do_chamfer (AdgPath
*path
,
82 CpmlPrimitive
*current
);
83 static void do_fillet (AdgPath
*path
,
84 CpmlPrimitive
*current
);
85 static gboolean
is_convex (const CpmlPrimitive
91 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_MODEL
);
95 adg_path_class_init(AdgPathClass
*klass
)
97 GObjectClass
*gobject_class
;
98 AdgModelClass
*model_class
;
100 gobject_class
= (GObjectClass
*) klass
;
101 model_class
= (AdgModelClass
*) klass
;
103 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
105 gobject_class
->finalize
= finalize
;
107 model_class
->changed
= changed
;
111 adg_path_init(AdgPath
*path
)
113 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
116 data
->cp_is_valid
= FALSE
;
117 data
->path
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
118 data
->cairo_path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
119 data
->cairo_path
.data
= NULL
;
120 data
->cairo_path
.num_data
= 0;
121 data
->operation
.operator = ADG_OPERATOR_NONE
;
127 finalize(GObject
*object
)
130 AdgPathPrivate
*data
;
131 GObjectClass
*object_class
;
133 path
= (AdgPath
*) object
;
135 object_class
= (GObjectClass
*) adg_path_parent_class
;
137 g_array_free(data
->path
, TRUE
);
138 clear_cairo_path(path
);
139 clear_operation(path
);
141 if (object_class
->finalize
!= NULL
)
142 object_class
->finalize(object
);
149 * Creates a new path model. The path must be constructed in the @callback
150 * function: AdgPath will cache and reuse the cairo_copy_path() returned by
151 * the cairo context after the @callback call.
153 * Returns: the new model
158 return (AdgModel
*) g_object_new(ADG_TYPE_PATH
, NULL
);
163 * adg_path_get_cairo_path:
166 * Gets a pointer to the cairo path structure of @path. The return path
167 * is owned by @path and must be considered read-only.
169 * This function also converts %CAIRO_PATH_ARC_TO primitives, not
170 * recognized by cairo, into approximated Bézier curves. The conversion
171 * is cached so any furter request is O(1). This cache is cleared
172 * whenever @path is modified (by adding a new primitive or by calling
176 * <title>TODO</title>
178 * <listitem>Actually, the arcs are approximated to Bézier using the
179 * hardcoded max angle of PI/2. This should be customizable
180 * by adding, for instance, a property to the #AdgPath class
181 * with a default value of PI/2.</listitem>
185 * Returns: a pointer to the internal cairo path or %NULL on errors
188 adg_path_get_cairo_path(AdgPath
*path
)
190 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
192 return get_cairo_path(path
);
196 * adg_path_get_cpml_path:
199 * Gets a pointer to the cairo path structure of @path. The return
200 * value is owned by @path and must not be freed.
202 * This function is similar to adg_path_get_cairo_path() but with
203 * two important differences: firstly the arc primitives are not
204 * expanded to Bézier curves and secondly the returned path is
205 * not read-only. This means it is allowed to modify the returned
206 * path as long as its size is retained and its data contains a
209 * Keep in mind any changes to @path makes the value returned by
210 * this function useless, as it is likely to contain plain garbage.
212 * Returns: a pointer to the internal cpml path or %NULL on errors
215 adg_path_get_cpml_path(AdgPath
*path
)
217 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
219 clear_cairo_path(path
);
221 return get_cpml_path(path
);
225 * adg_path_get_current_point:
227 * @x: where to store the x coordinate of the current point
228 * @y: where to store the y coordinate of the current point
230 * Gets the current point of @path, which is conceptually the
231 * final point reached by the path so far.
233 * If there is no defined current point, @x and @y will both be set
234 * to 0 and a warning will be triggered. It is possible to check this
235 * in advance with adg_path_has_current_point().
237 * Most #AdgPath methods alter the current point and most of them
238 * expect a current point to be defined otherwise will fail triggering
239 * a warning. Check the description of every method for specific details.
242 adg_path_get_current_point(AdgPath
*path
, gdouble
*x
, gdouble
*y
)
244 AdgPathPrivate
*data
;
246 g_return_if_fail(ADG_IS_PATH(path
));
250 if (data
->cp_is_valid
) {
255 g_return_if_reached();
260 * adg_path_has_current_point:
263 * Returns whether a current point is defined on @path.
264 * See adg_path_get_current_point() for details on the current point.
266 * Returns: whether a current point is defined
269 adg_path_has_current_point(AdgPath
*path
)
271 AdgPathPrivate
*data
;
273 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
277 return data
->cp_is_valid
;
284 * Releases the internal memory hold by @path and resets its status,
285 * so that after this call @path contains an empty path.
288 adg_path_clear(AdgPath
*path
)
290 AdgPathPrivate
*data
;
292 g_return_if_fail(ADG_IS_PATH(path
));
296 g_array_set_size(data
->path
, 0);
297 clear_cairo_path(path
);
298 clear_operation(path
);
305 * @type: a #cairo_data_type_t value
306 * @...: point data, specified as #AdgPair pointers
308 * Generic method to append a primitive to @path. The number of #AdgPair
309 * structs depends on @type: there is no way with this function to
310 * reserve more cairo_path_data_t structs than what is needed by the
313 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
315 * If @path has no current point while the requested primitive needs it,
316 * a warning message will be triggered without other effect.
319 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
323 va_start(var_args
, type
);
324 adg_path_append_valist(path
, type
, var_args
);
329 * adg_path_append_valist:
331 * @type: a #cairo_data_type_t value
332 * @var_args: point data, specified as #AdgPair pointers
334 * va_list version of adg_path_append().
337 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
339 AdgPathPrivate
*data
;
340 AdgPrimitive primitive
;
342 cairo_path_data_t org
;
343 cairo_path_data_t
*path_data
;
345 g_return_if_fail(ADG_IS_PATH(path
));
348 length
= needed_pairs(type
, data
->cp_is_valid
);
352 /* Set a copy of the current point as the primitive origin */
353 cpml_pair_to_cairo(&data
->cp
, &org
);
354 primitive
.org
= &org
;
356 /* Build the cairo_path_data_t array */
357 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
359 path_data
->header
.type
= type
;
360 path_data
->header
.length
= length
;
362 for (cnt
= 1; cnt
< length
; ++ cnt
) {
364 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), path_data
);
367 /* Terminate the creation of the temporary primitive */
368 primitive
.segment
= NULL
;
370 /* Append this primitive to @path */
371 append_primitive(path
, &primitive
);
373 g_free(primitive
.data
);
377 * adg_path_append_primitive:
379 * @primitive: the #AdgPrimitive to append
381 * Appends @primitive to @path. The primitive to add is considered the
382 * continuation of the current path so the <structfield>org</structfield>
383 * component of @primitive is not used. Anyway the current poins is
384 * checked against it: they must be equal or the function will fail
385 * without further processing.
388 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
390 AdgPathPrivate
*data
;
391 AdgPrimitive
*primitive_dup
;
393 g_return_if_fail(ADG_IS_PATH(path
));
394 g_return_if_fail(primitive
!= NULL
);
398 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
399 primitive
->org
->point
.y
== data
->cp
.y
);
401 /* The primitive data could be modified by pending operations:
403 primitive_dup
= adg_primitive_deep_dup(primitive
);
405 append_primitive(path
, primitive_dup
);
407 g_free(primitive_dup
);
411 * adg_path_append_segment:
413 * @segment: the #AdgSegment to append
415 * Appends @segment to @path.
418 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
420 AdgPathPrivate
*data
;
422 g_return_if_fail(ADG_IS_PATH(path
));
423 g_return_if_fail(segment
!= NULL
);
427 clear_cairo_path(path
);
428 data
->path
= g_array_append_vals(data
->path
,
429 segment
->data
, segment
->num_data
);
433 * adg_path_append_cairo_path:
435 * @cairo_path: the #cairo_path_t path to append
437 * Appends a whole cairo path to @path.
440 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
442 AdgPathPrivate
*data
;
444 g_return_if_fail(ADG_IS_PATH(path
));
448 clear_cairo_path(path
);
449 data
->path
= g_array_append_vals(data
->path
,
450 cairo_path
->data
, cairo_path
->num_data
);
456 * @x: the new x coordinate
457 * @y: the new y coordinate
459 * Begins a new segment. After this call the current point will be (@x, @y).
462 adg_path_move_to(AdgPath
*path
, gdouble x
, gdouble y
)
469 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
475 * @x: the new x coordinate
476 * @y: the new y coordinate
478 * Adds a line to @path from the current point to position (@x, @y).
479 * After this call the current point will be (@x, @y).
481 * If @path has no current point before this call, this function will
482 * trigger a warning without other effect.
485 adg_path_line_to(AdgPath
*path
, gdouble x
, gdouble y
)
492 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
498 * @x1: the x coordinate of an intermediate point
499 * @y1: the y coordinate of an intermediate point
500 * @x2: the x coordinate of the end of the arc
501 * @y2: the y coordinate of the end of the arc
503 * Adds an arc to the path from the current point to (@x2, @y2),
504 * passing throught (@x1, @y1). After this call the current point
505 * will be (@x2, @y2).
507 * If @path has no current point before this call, this function will
508 * trigger a warning without other effect.
511 adg_path_arc_to(AdgPath
*path
, gdouble x1
, gdouble y1
, gdouble x2
, gdouble y2
)
520 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
526 * @x1: the x coordinate of the first control point
527 * @y1: the y coordinate of the first control point
528 * @x2: the x coordinate of the second control point
529 * @y2: the y coordinate of the second control point
530 * @x3: the x coordinate of the end of the curve
531 * @y3: the y coordinate of the end of the curve
533 * Adds a cubic Bézier curve to the path from the current point to
534 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
535 * control points. After this call the current point will be (@x3, @y3).
537 * If @path has no current point before this call, this function will
538 * trigger a warning without other effect.
541 adg_path_curve_to(AdgPath
*path
, gdouble x1
, gdouble y1
,
542 gdouble x2
, gdouble y2
, gdouble x3
, gdouble y3
)
553 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, &p
[0], &p
[1], &p
[2]);
560 * Adds a line segment to the path from the current point to the
561 * beginning of the current segment, (the most recent point passed
562 * to an adg_path_move_to()), and closes this segment.
563 * After this call the current point will be unset.
565 * The behavior of adg_path_close() is distinct from simply calling
566 * adg_line_to() with the coordinates of the segment starting point.
567 * When a closed segment is stroked, there are no caps on the ends.
568 * Instead, there is a line join connecting the final and initial
569 * primitive of the segment.
571 * If @path has no current point before this call, this function will
572 * trigger a warning without other effect.
575 adg_path_close(AdgPath
*path
)
577 adg_path_append(path
, CAIRO_PATH_CLOSE_PATH
);
583 * @xc: x position of the center of the arc
584 * @yc: y position of the center of the arc
585 * @r: the radius of the arc
586 * @start: the start angle, in radians
587 * @end: the end angle, in radians
589 * A more usual way to add an arc to @path. After this call, the current
590 * point will be the computed end point of the arc. The arc will be
591 * rendered in increasing angle, accordling to @start and @end. This means
592 * if @start is less than @end, the arc will be rendered in clockwise
593 * direction (accordling to the default cairo coordinate system) while if
594 * @start is greather than @end, the arc will be rendered in couterclockwise
597 * By explicitely setting the whole arc data, the start point could be
598 * different from the current point. In this case, if @path has no
599 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
600 * point of the arc will be automatically prepended to the arc.
601 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
602 * point of the arc will be used instead of the moveto.
605 adg_path_arc(AdgPath
*path
, gdouble xc
, gdouble yc
, gdouble r
,
606 gdouble start
, gdouble end
)
608 AdgPathPrivate
*data
;
609 AdgPair center
, p
[3];
611 g_return_if_fail(ADG_IS_PATH(path
));
617 cpml_vector_from_angle(&p
[0], start
, r
);
618 cpml_vector_from_angle(&p
[1], (end
-start
) / 2, r
);
619 cpml_vector_from_angle(&p
[2], end
, r
);
621 cpml_pair_add(&p
[0], ¢er
);
622 cpml_pair_add(&p
[1], ¢er
);
623 cpml_pair_add(&p
[2], ¢er
);
625 if (!data
->cp_is_valid
)
626 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
627 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
628 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
630 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
636 * @delta1: the distance from the intersection point of the current primitive
637 * @delta2: the distance from the intersection point of the next primitive
639 * A binary operator that generates a chamfer between two primitives.
640 * The first primitive involved is the current primitive, the second will
641 * be the next primitive appended to @path after this call. The second
642 * primitive is required: if the chamfer operation is not properly
643 * terminated (by not providing the second primitive), any API accessing
644 * the path in reading mode will raise a warning.
646 * The chamfer operation requires two lengths: @delta1 specifies the
647 * "quantity" to trim on the first primitive while @delta2 is the same
648 * applied on the second primitive. The term "quantity" means the length
649 * of the portion to cut out from the original primitive (that is the
650 * primitive as would be without the chamfer).
653 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
655 g_return_if_fail(ADG_IS_PATH(path
));
657 if (!append_operation(path
, ADG_OPERATOR_CHAMFER
, delta1
, delta2
))
664 * @radius: the radius of the fillet
667 * A binary operator that joins to primitives with an arc.
668 * The first primitive involved is the current primitive, the second will
669 * be the next primitive appended to @path after this call. The second
670 * primitive is required: if the fillet operation is not properly
671 * terminated (by not providing the second primitive), any API accessing
672 * the path in reading mode will raise a warning.
675 adg_path_fillet(AdgPath
*path
, gdouble radius
)
677 g_return_if_fail(ADG_IS_PATH(path
));
679 if (!append_operation(path
, ADG_OPERATOR_FILLET
, radius
))
688 * Dumps the data content of @path to stdout in a human readable format.
691 adg_path_dump(AdgPath
*path
)
694 cairo_path_t
*cairo_path
;
696 g_return_if_fail(ADG_IS_PATH(path
));
698 cairo_path
= get_cairo_path(path
);
700 g_return_if_fail(cairo_path
!= NULL
);
702 if (!cpml_segment_from_cairo(&segment
, cairo_path
)) {
703 g_warning("Invalid path data to dump!\n");
706 cpml_segment_dump(&segment
);
707 } while (cpml_segment_next(&segment
));
713 changed(AdgModel
*model
)
715 AdgModelClass
*model_class
= (AdgModelClass
*) adg_path_parent_class
;
717 adg_path_clear((AdgPath
*) model
);
719 if (model_class
->changed
!= NULL
)
720 model_class
->changed(model
);
724 clear_cairo_path(AdgPath
*path
)
726 AdgPathPrivate
*data
;
727 cairo_path_t
*cairo_path
;
730 cairo_path
= &data
->cairo_path
;
732 if (cairo_path
->data
== NULL
)
735 g_free(cairo_path
->data
);
737 cairo_path
->status
= CAIRO_STATUS_INVALID_PATH_DATA
;
738 cairo_path
->data
= NULL
;
739 cairo_path
->num_data
= 0;
742 static cairo_path_t
*
743 get_cairo_path(AdgPath
*path
)
745 AdgPathPrivate
*data
;
746 cairo_path_t
*cairo_path
;
749 const cairo_path_data_t
*p_src
;
753 cairo_path
= &data
->cairo_path
;
755 /* Check for cached result */
756 if (cairo_path
->data
!= NULL
)
760 dst
= g_array_sized_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
), src
->len
);
762 /* Cycle the path and convert arcs to Bézier curves */
763 for (i
= 0; i
< src
->len
; i
+= p_src
->header
.length
) {
764 p_src
= (const cairo_path_data_t
*) src
->data
+ i
;
766 if (p_src
->header
.type
== CAIRO_PATH_ARC_TO
)
767 dst
= arc_to_curves(dst
, p_src
);
769 dst
= g_array_append_vals(dst
, p_src
, p_src
->header
.length
);
772 cairo_path
->status
= CAIRO_STATUS_SUCCESS
;
773 cairo_path
->num_data
= dst
->len
;
774 cairo_path
->data
= (cairo_path_data_t
*) g_array_free(dst
, FALSE
);
779 static cairo_path_t
*
780 get_cpml_path(AdgPath
*path
)
782 AdgPathPrivate
*data
;
783 cairo_path_t
*cpml_path
;
786 cpml_path
= &data
->cpml_path
;
788 cpml_path
->status
= CAIRO_STATUS_SUCCESS
;
789 cpml_path
->data
= (cairo_path_data_t
*) data
->path
->data
;
790 cpml_path
->num_data
= data
->path
->len
;
796 arc_to_curves(GArray
*array
, const cairo_path_data_t
*src
)
801 /* Build the arc primitive: the arc origin is supposed to be the previous
802 * point (src-1): this means a primitive must exist before the arc */
804 arc
.org
= (cairo_path_data_t
*) (src
-1);
805 arc
.data
= (cairo_path_data_t
*) src
;
807 if (cpml_arc_info(&arc
, NULL
, NULL
, &start
, &end
)) {
810 cairo_path_data_t
*curves
;
812 n_curves
= ceil(fabs(end
-start
) / M_PI_2
);
813 curves
= g_new(cairo_path_data_t
, n_curves
* 4);
814 segment
.data
= curves
;
815 cpml_arc_to_curves(&arc
, &segment
, n_curves
);
817 array
= g_array_append_vals(array
, curves
, n_curves
* 4);
826 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
828 AdgPathPrivate
*data
;
829 cairo_path_data_t
*path_data
;
833 path_data
= current
->data
;
834 length
= path_data
[0].header
.length
;
836 /* Execute any pending operation */
837 do_operation(path
, path_data
);
839 /* Append the path data to the internal path array */
840 data
->path
= g_array_append_vals(data
->path
, path_data
, length
);
842 /* Set path data to point to the recently appended cairo_path_data_t
843 * primitive: the first struct is the header */
844 path_data
= (cairo_path_data_t
*) data
->path
->data
+
845 data
->path
->len
- length
;
847 /* Set the last primitive for subsequent binary operations */
848 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
849 data
->last
.segment
= NULL
;
850 data
->last
.data
= path_data
;
852 /* Save the last point as the current point, if applicable */
853 data
->cp_is_valid
= length
> 1;
855 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
857 /* Invalidate cairo_path: should be recomputed */
858 clear_cairo_path(path
);
862 needed_pairs(CpmlPrimitiveType type
, gboolean cp_is_valid
)
866 case CAIRO_PATH_CLOSE_PATH
:
867 g_return_val_if_fail(cp_is_valid
, 0);
870 case CAIRO_PATH_MOVE_TO
:
873 case CAIRO_PATH_LINE_TO
:
874 g_return_val_if_fail(cp_is_valid
, 0);
877 case CAIRO_PATH_ARC_TO
:
878 g_return_val_if_fail(cp_is_valid
, 0);
881 case CAIRO_PATH_CURVE_TO
:
882 g_return_val_if_fail(cp_is_valid
, 0);
886 g_return_val_if_reached(0);
893 clear_operation(AdgPath
*path
)
895 AdgPathPrivate
*data
;
896 AdgOperation
*operation
;
899 operation
= &data
->operation
;
901 if (operation
->operator == ADG_OPERATOR_NONE
)
904 g_warning("An operation is still active while clearing the path "
905 "(operator `%d')", operation
->operator);
906 operation
->operator = ADG_OPERATOR_NONE
;
910 append_operation(AdgPath
*path
, AdgOperator
operator, ...)
912 AdgPathPrivate
*data
;
913 AdgOperation
*operation
;
918 if (!data
->cp_is_valid
) {
919 g_warning("Operation requested but path has no current primitive "
920 "(operator `%d')", operator);
924 operation
= &data
->operation
;
925 if (operation
->operator != ADG_OPERATOR_NONE
) {
926 /* TODO: this is a rude semplification, as a lot of operators can
927 * and may cohexist. As an example, a fillet followed by a
928 * polar chamfer is not difficult to compute */
929 g_warning("Operation requested but another operation is yet active"
930 "(operators: new `%d', old `%d')",
931 operator, operation
->operator);
935 va_start(var_args
, operator);
939 case ADG_OPERATOR_CHAMFER
:
940 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
941 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
944 case ADG_OPERATOR_FILLET
:
945 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
948 case ADG_OPERATOR_NONE
:
953 g_warning("Operation not recognized (operator `%d')", operator);
958 operation
->operator = operator;
965 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
967 AdgPathPrivate
*data
;
968 AdgOperator
operator;
970 CpmlPrimitive current
;
971 cairo_path_data_t current_org
;
974 operator = data
->operation
.operator;
975 cpml_segment_from_cairo(&segment
, get_cpml_path(path
));
977 /* Construct the current primitive, that is the primitive to be inserted.
978 * Its org is a copy of the end point of the last primitive: it can be
979 * modified without affecting anything else. It is expected the operation
980 * functions will add to @path the primitives required but NOT to add
981 * @current, as this one will be inserted automatically. */
982 current
.segment
= &segment
;
983 current
.org
= ¤t_org
;
984 current
.data
= path_data
;
985 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
989 case ADG_OPERATOR_NONE
:
992 case ADG_OPERATOR_CHAMFER
:
993 do_chamfer(path
, ¤t
);
996 case ADG_OPERATOR_FILLET
:
997 do_fillet(path
, ¤t
);
1001 g_warning("Operation not implemented (operator `%d')", operator);
1007 do_chamfer(AdgPath
*path
, CpmlPrimitive
*current
)
1009 AdgPathPrivate
*data
;
1010 CpmlPrimitive
*last
;
1011 gdouble delta1
, delta2
;
1014 cairo_path_data_t line
[2];
1018 delta1
= data
->operation
.data
.chamfer
.delta1
;
1019 len1
= cpml_primitive_length(last
);
1021 if (delta1
>= len1
) {
1022 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
1027 delta2
= data
->operation
.data
.chamfer
.delta2
;
1028 len2
= cpml_primitive_length(current
);
1030 if (delta2
>= len2
) {
1031 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
1036 /* Change the end point of the last primitive */
1037 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
1038 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
1040 /* Change the start point of the current primitive */
1041 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
1042 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
1044 /* Add the chamfer line */
1045 line
[0].header
.type
= CAIRO_PATH_LINE_TO
;
1046 line
[0].header
.length
= 2;
1047 line
[1].point
.x
= pair
.x
;
1048 line
[1].point
.y
= pair
.y
;
1049 data
->path
= g_array_append_vals(data
->path
, line
, 2);
1051 data
->operation
.operator = ADG_OPERATOR_NONE
;
1055 do_fillet(AdgPath
*path
, CpmlPrimitive
*current
)
1057 AdgPathPrivate
*data
;
1058 CpmlPrimitive
*last
, *current_dup
, *last_dup
;
1059 gdouble radius
, offset
, pos
;
1060 AdgPair center
, vector
, p
[3];
1061 cairo_path_data_t arc
[3];
1065 current_dup
= adg_primitive_deep_dup(current
);
1067 /* Force current_dup to point to the original segment so a
1068 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1069 current_dup
->segment
= current
->segment
;
1071 last_dup
= adg_primitive_deep_dup(last
);
1072 radius
= data
->operation
.data
.fillet
.radius
;
1073 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1075 /* Find the center of the fillet from the intersection between
1076 * the last and current primitives offseted by radius */
1077 cpml_primitive_offset(current_dup
, offset
);
1078 cpml_primitive_offset(last_dup
, offset
);
1079 if (cpml_primitive_intersection(current_dup
, last_dup
,
1081 g_warning("Fillet not applicable (radius = %lf)", radius
);
1082 g_free(current_dup
);
1087 /* Compute the start point of the fillet */
1088 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1089 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1090 cpml_vector_set_length(&vector
, offset
);
1091 cpml_vector_normal(&vector
);
1092 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1094 /* Compute the mid point of the fillet */
1095 cpml_pair_from_cairo(&vector
, current
->org
);
1096 cpml_pair_sub(&vector
, ¢er
);
1097 cpml_vector_set_length(&vector
, radius
);
1098 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1100 /* Compute the end point of the fillet */
1101 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1102 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1103 cpml_vector_set_length(&vector
, offset
);
1104 cpml_vector_normal(&vector
);
1105 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1107 g_free(current_dup
);
1110 /* Modify the end point of the last primitive */
1111 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1113 /* Add the fillet arc */
1114 arc
[0].header
.type
= CAIRO_PATH_ARC_TO
;
1115 arc
[0].header
.length
= 3;
1116 cpml_pair_to_cairo(&p
[1], &arc
[1]);
1117 cpml_pair_to_cairo(&p
[2], &arc
[2]);
1118 data
->path
= g_array_append_vals(data
->path
, arc
, 3);
1120 data
->operation
.operator = ADG_OPERATOR_NONE
;
1124 is_convex(const CpmlPrimitive
*primitive1
, const CpmlPrimitive
*primitive2
)
1127 gdouble angle1
, angle2
;
1129 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1130 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1132 /* Probably there is a smarter way to get this without trygonometry */
1133 angle1
= cpml_vector_angle(&v1
);
1134 angle2
= cpml_vector_angle(&v2
);
1136 if (angle1
> angle2
)
1139 return angle2
-angle1
> M_PI
;