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 represents a virtual #CpmlPath: this class
26 * implements methods to create the path and provides additional
27 * operations specific to technical drawings.
29 * #AdgPath overrides the get_cpml_path() method of the parent
30 * #AdgTrail class, avoiding the need of an #AdgTrailCallback.
31 * The path is constructed programmaticaly: keep in mind any
32 * method that modifies the path will invalidate the #CpmlPath
33 * returned by adg_trail_get_cpml_path().
35 * Although some of the provided methods are clearly based on the
36 * original cairo path manipulation API, their behavior could be
37 * sligthly different. This is intentional, because the ADG provides
38 * additional path manipulation algorithms, sometime quite complex,
39 * and a more restrictive filter on the path quality is required.
40 * Also, the ADG is designed to be used by technicians while cairo
41 * targets a broader range of developers.
43 * As an example, following the rule of the less surprise, some
44 * cairo functions guess the current point when it is not defined,
45 * while the #AdgPath methods trigger a warning without other effect.
46 * Furthermore, after cairo_path_close_path() a %CAIRO_PATH_MOVE_TO
47 * primitive to the starting point of the segment is automatically
48 * added by cairo; in ADG, after an adg_path_close() the current
49 * point is simply unset.
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
66 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
67 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
70 static void finalize (GObject
*object
);
71 static void clear (AdgModel
*model
);
72 static void clear_parent (AdgModel
*model
);
73 static void changed (AdgModel
*model
);
74 static CpmlPath
* get_cpml_path (AdgTrail
*trail
);
75 static CpmlPath
* read_cpml_path (AdgPath
*path
);
76 static void append_primitive (AdgPath
*path
,
77 AdgPrimitive
*primitive
);
78 static gint
needed_pairs (CpmlPrimitiveType type
,
79 gboolean cp_is_valid
);
80 static void clear_operation (AdgPath
*path
);
81 static gboolean
append_operation (AdgPath
*path
,
84 static void do_operation (AdgPath
*path
,
87 static void do_chamfer (AdgPath
*path
,
88 AdgPrimitive
*current
);
89 static void do_fillet (AdgPath
*path
,
90 AdgPrimitive
*current
);
91 static gboolean
is_convex (const AdgPrimitive
97 G_DEFINE_TYPE(AdgPath
, adg_path
, ADG_TYPE_TRAIL
);
101 adg_path_class_init(AdgPathClass
*klass
)
103 GObjectClass
*gobject_class
;
104 AdgModelClass
*model_class
;
105 AdgTrailClass
*trail_class
;
107 gobject_class
= (GObjectClass
*) klass
;
108 model_class
= (AdgModelClass
*) klass
;
109 trail_class
= (AdgTrailClass
*) klass
;
111 g_type_class_add_private(klass
, sizeof(AdgPathPrivate
));
113 gobject_class
->finalize
= finalize
;
115 model_class
->clear
= clear
;
116 model_class
->changed
= changed
;
118 trail_class
->get_cpml_path
= get_cpml_path
;
122 adg_path_init(AdgPath
*path
)
124 AdgPathPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(path
, ADG_TYPE_PATH
,
127 data
->cp_is_valid
= FALSE
;
128 data
->cpml
.array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
129 data
->operation
.action
= ADG_ACTION_NONE
;
135 finalize(GObject
*object
)
138 AdgPathPrivate
*data
;
140 path
= (AdgPath
*) object
;
143 g_array_free(data
->cpml
.array
, TRUE
);
144 clear_operation(path
);
146 if (PARENT_OBJECT_CLASS
->finalize
!= NULL
)
147 PARENT_OBJECT_CLASS
->finalize(object
);
154 * Creates a new path model. The path should be constructed
155 * programmatically by using the methods provided by #AdgPath.
157 * Returns: the newly created path model
162 return g_object_new(ADG_TYPE_PATH
, NULL
);
166 * adg_path_current_point:
169 * Gets the current point of @path, which is conceptually the
170 * final point reached by the path so far.
172 * If there is no defined current point, %NULL is returned.
173 * It is possible to check this in advance with
174 * adg_path_has_current_point().
176 * Most #AdgPath methods alter the current point and most of them
177 * expect a current point to be defined otherwise will fail triggering
178 * a warning. Check the description of every method for specific details.
180 * Returns: the current point or %NULL on no current point set or errors
183 adg_path_current_point(AdgPath
*path
)
185 AdgPathPrivate
*data
;
187 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
191 if (!data
->cp_is_valid
)
198 * adg_path_has_current_point:
201 * Returns whether a current point is defined on @path.
202 * See adg_path_get_current_point() for details on the current point.
204 * Returns: whether a current point is defined
207 adg_path_has_current_point(AdgPath
*path
)
209 AdgPathPrivate
*data
;
211 g_return_val_if_fail(ADG_IS_PATH(path
), FALSE
);
215 return data
->cp_is_valid
;
221 * @type: a #cairo_data_type_t value
222 * @...: point data, specified as #AdgPair pointers
224 * Generic method to append a primitive to @path. The number of #AdgPair
225 * structs depends on @type: there is no way with this function to
226 * reserve more cairo_path_data_t structs than what is needed by the
229 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
231 * If @path has no current point while the requested primitive needs it,
232 * a warning message will be triggered without other effect.
235 adg_path_append(AdgPath
*path
, CpmlPrimitiveType type
, ...)
239 va_start(var_args
, type
);
240 adg_path_append_valist(path
, type
, var_args
);
245 * adg_path_append_valist:
247 * @type: a #cairo_data_type_t value
248 * @var_args: point data, specified as #AdgPair pointers
250 * va_list version of adg_path_append().
253 adg_path_append_valist(AdgPath
*path
, CpmlPrimitiveType type
, va_list var_args
)
255 AdgPathPrivate
*data
;
256 AdgPrimitive primitive
;
258 cairo_path_data_t org
;
259 cairo_path_data_t
*path_data
;
261 g_return_if_fail(ADG_IS_PATH(path
));
264 length
= needed_pairs(type
, data
->cp_is_valid
);
268 /* Set a copy of the current point as the primitive origin */
269 cpml_pair_to_cairo(&data
->cp
, &org
);
270 primitive
.org
= &org
;
272 /* Build the cairo_path_data_t array */
273 primitive
.data
= path_data
= g_new(cairo_path_data_t
, length
);
275 path_data
->header
.type
= type
;
276 path_data
->header
.length
= length
;
278 for (cnt
= 1; cnt
< length
; ++ cnt
) {
280 cpml_pair_to_cairo(va_arg(var_args
, AdgPair
*), path_data
);
283 /* Terminate the creation of the temporary primitive */
284 primitive
.segment
= NULL
;
286 /* Append this primitive to @path */
287 append_primitive(path
, &primitive
);
289 g_free(primitive
.data
);
293 * adg_path_append_primitive:
295 * @primitive: the #AdgPrimitive to append
297 * Appends @primitive to @path. The primitive to add is considered the
298 * continuation of the current path so the <structfield>org</structfield>
299 * component of @primitive is not used. Anyway the current poins is
300 * checked against it: they must be equal or the function will fail
301 * without further processing.
304 adg_path_append_primitive(AdgPath
*path
, const AdgPrimitive
*primitive
)
306 AdgPathPrivate
*data
;
307 AdgPrimitive
*primitive_dup
;
309 g_return_if_fail(ADG_IS_PATH(path
));
310 g_return_if_fail(primitive
!= NULL
);
314 g_return_if_fail(primitive
->org
->point
.x
== data
->cp
.x
&&
315 primitive
->org
->point
.y
== data
->cp
.y
);
317 /* The primitive data could be modified by pending operations:
319 primitive_dup
= adg_primitive_deep_dup(primitive
);
321 append_primitive(path
, primitive_dup
);
323 g_free(primitive_dup
);
327 * adg_path_append_segment:
329 * @segment: the #AdgSegment to append
331 * Appends @segment to @path.
334 adg_path_append_segment(AdgPath
*path
, const AdgSegment
*segment
)
336 AdgPathPrivate
*data
;
338 g_return_if_fail(ADG_IS_PATH(path
));
339 g_return_if_fail(segment
!= NULL
);
343 clear_parent((AdgModel
*) path
);
344 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
345 segment
->data
, segment
->num_data
);
349 * adg_path_append_cairo_path:
351 * @cairo_path: the #cairo_path_t path to append
353 * Appends a whole cairo path to @path.
356 adg_path_append_cairo_path(AdgPath
*path
, const cairo_path_t
*cairo_path
)
358 AdgPathPrivate
*data
;
360 g_return_if_fail(ADG_IS_PATH(path
));
364 clear_parent((AdgModel
*) path
);
365 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
367 cairo_path
->num_data
);
373 * @pair: the destination coordinates
375 * Begins a new segment. After this call the current point will be @pair.
378 adg_path_move_to(AdgPath
*path
, const AdgPair
*pair
)
380 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, pair
);
384 * adg_path_move_to_explicit:
386 * @x: the new x coordinate
387 * @y: the new y coordinate
389 * Convenient function to call adg_path_move_to() using explicit
390 * coordinates instead of #AdgPair.
393 adg_path_move_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
400 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
);
406 * @pair: the destination coordinates
408 * Adds a line to @path from the current point to @pair. After this
409 * call the current point will be @pair.
411 * If @path has no current point before this call, this function will
412 * trigger a warning without other effect.
415 adg_path_line_to(AdgPath
*path
, const AdgPair
*pair
)
417 adg_path_append(path
, CAIRO_PATH_LINE_TO
, pair
);
421 * adg_path_line_to_explicit:
423 * @x: the new x coordinate
424 * @y: the new y coordinate
426 * Convenient function to call adg_path_line_to() using explicit
427 * coordinates instead of #AdgPair.
430 adg_path_line_to_explicit(AdgPath
*path
, gdouble x
, gdouble y
)
437 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
);
443 * @throught: an arbitrary point on the arc
444 * @pair: the destination coordinates
446 * Adds an arc to the path from the current point to @pair, passing
447 * throught @throught. After this call the current point will be @pair.
449 * If @path has no current point before this call, this function will
450 * trigger a warning without other effect.
453 adg_path_arc_to(AdgPath
*path
, const AdgPair
*throught
, const AdgPair
*pair
)
455 adg_path_append(path
, CAIRO_PATH_ARC_TO
, throught
, pair
);
459 * adg_path_arc_to_explicit:
461 * @x1: the x coordinate of an intermediate point
462 * @y1: the y coordinate of an intermediate point
463 * @x2: the x coordinate of the end of the arc
464 * @y2: the y coordinate of the end of the arc
466 * Convenient function to call adg_path_arc_to() using explicit
467 * coordinates instead of #AdgPair.
470 adg_path_arc_to_explicit(AdgPath
*path
, gdouble x1
, gdouble y1
,
471 gdouble x2
, gdouble y2
)
480 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[0], &p
[1]);
486 * @control1: the first control point of the curve
487 * @control2: the second control point of the curve
488 * @pair: the destination coordinates
490 * Adds a cubic Bézier curve to the path from the current point to
491 * position @pair, using @control1 and @control2 as control points.
492 * After this call the current point will be @pair.
494 * If @path has no current point before this call, this function will
495 * trigger a warning without other effect.
498 adg_path_curve_to(AdgPath
*path
, const AdgPair
*control1
,
499 const AdgPair
*control2
, const AdgPair
*pair
)
501 adg_path_append(path
, CAIRO_PATH_CURVE_TO
, control1
, control2
, pair
);
505 * adg_path_curve_to_explicit:
507 * @x1: the x coordinate of the first control point
508 * @y1: the y coordinate of the first control point
509 * @x2: the x coordinate of the second control point
510 * @y2: the y coordinate of the second control point
511 * @x3: the x coordinate of the end of the curve
512 * @y3: the y coordinate of the end of the curve
514 * Convenient function to call adg_path_curve_to() using explicit
515 * coordinates instead of #AdgPair.
518 adg_path_curve_to_explicit(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 AdgPathPrivate
*data
;
586 AdgPair center
, p
[3];
588 g_return_if_fail(ADG_IS_PATH(path
));
594 cpml_vector_from_angle(&p
[0], start
);
595 cpml_vector_from_angle(&p
[1], (end
-start
) / 2);
596 cpml_vector_from_angle(&p
[2], end
);
598 cpml_vector_set_length(&p
[0], r
);
599 cpml_vector_set_length(&p
[1], r
);
600 cpml_vector_set_length(&p
[2], r
);
602 cpml_pair_add(&p
[0], ¢er
);
603 cpml_pair_add(&p
[1], ¢er
);
604 cpml_pair_add(&p
[2], ¢er
);
606 if (!data
->cp_is_valid
)
607 adg_path_append(path
, CAIRO_PATH_MOVE_TO
, &p
[0]);
608 else if (p
[0].x
!= data
->cp
.x
|| p
[0].y
!= data
->cp
.y
)
609 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &p
[0]);
611 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
617 * @delta1: the distance from the intersection point of the current primitive
618 * @delta2: the distance from the intersection point of the next primitive
620 * A binary action that generates a chamfer between two primitives.
621 * The first primitive involved is the current primitive, the second will
622 * be the next primitive appended to @path after this call. The second
623 * primitive is required: if the chamfer operation is not properly
624 * terminated (by not providing the second primitive), any API accessing
625 * the path in reading mode will raise a warning.
627 * The chamfer operation requires two lengths: @delta1 specifies the
628 * "quantity" to trim on the first primitive while @delta2 is the same
629 * applied on the second primitive. The term "quantity" means the length
630 * of the portion to cut out from the original primitive (that is the
631 * primitive as would be without the chamfer).
634 adg_path_chamfer(AdgPath
*path
, gdouble delta1
, gdouble delta2
)
636 g_return_if_fail(ADG_IS_PATH(path
));
638 if (!append_operation(path
, ADG_ACTION_CHAMFER
, delta1
, delta2
))
645 * @radius: the radius of the fillet
647 * A binary action that joins to primitives with an arc.
648 * The first primitive involved is the current primitive, the second will
649 * be the next primitive appended to @path after this call. The second
650 * primitive is required: if the fillet operation is not properly
651 * terminated (by not providing the second primitive), any API accessing
652 * the path in reading mode will raise a warning.
655 adg_path_fillet(AdgPath
*path
, gdouble radius
)
657 g_return_if_fail(ADG_IS_PATH(path
));
659 if (!append_operation(path
, ADG_ACTION_FILLET
, radius
))
664 * adg_path_last_primitive:
667 * Gets the last primitive appended to @path. The returned struct
668 * is owned by @path and should not be freed or modified.
670 * Returns: a pointer to the last appended primitive or %NULL on errors
673 adg_path_last_primitive(AdgPath
*path
)
675 AdgPathPrivate
*data
;
677 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
685 * adg_path_over_primitive:
688 * Gets the primitive before the last one appended to @path. The
689 * "over" term comes from forth, where the %OVER operator works
690 * on the stack in the same way as adg_path_over_primitive() works
691 * on @path. The returned struct is owned by @path and should not
692 * be freed or modified.
694 * Returns: a pointer to the primitive before the last appended one
698 adg_path_over_primitive(AdgPath
*path
)
700 AdgPathPrivate
*data
;
702 g_return_val_if_fail(ADG_IS_PATH(path
), NULL
);
711 clear(AdgModel
*model
)
714 AdgPathPrivate
*data
;
716 path
= (AdgPath
*) model
;
719 g_array_set_size(data
->cpml
.array
, 0);
720 clear_operation(path
);
725 clear_parent(AdgModel
*model
)
727 if (PARENT_MODEL_CLASS
->clear
!= NULL
)
728 PARENT_MODEL_CLASS
->clear(model
);
732 changed(AdgModel
*model
)
736 if (PARENT_MODEL_CLASS
->changed
!= NULL
)
737 PARENT_MODEL_CLASS
->changed(model
);
741 get_cpml_path(AdgTrail
*trail
)
743 clear_parent((AdgModel
*) trail
);
744 return read_cpml_path((AdgPath
*) trail
);
748 read_cpml_path(AdgPath
*path
)
750 AdgPathPrivate
*data
= path
->data
;
752 /* Always regenerate the CpmlPath as it is a trivial operation */
753 data
->cpml
.path
.status
= CAIRO_STATUS_SUCCESS
;
754 data
->cpml
.path
.data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
;
755 data
->cpml
.path
.num_data
= (data
->cpml
.array
)->len
;
757 return &data
->cpml
.path
;
761 append_primitive(AdgPath
*path
, AdgPrimitive
*current
)
763 AdgPathPrivate
*data
;
764 cairo_path_data_t
*path_data
;
768 path_data
= current
->data
;
769 length
= path_data
[0].header
.length
;
771 /* Execute any pending operation */
772 do_operation(path
, path_data
);
774 /* Append the path data to the internal path array */
775 data
->cpml
.array
= g_array_append_vals(data
->cpml
.array
,
778 /* Set path data to point to the recently appended cairo_path_data_t
779 * primitive: the first struct is the header */
780 path_data
= (cairo_path_data_t
*) (data
->cpml
.array
)->data
+
781 (data
->cpml
.array
)->len
- length
;
783 /* Store the over primitive */
784 memcpy(&data
->over
, &data
->last
, sizeof(AdgPrimitive
));
786 /* Set the last primitive for subsequent binary operations */
787 data
->last
.org
= data
->cp_is_valid
? path_data
- 1 : NULL
;
788 data
->last
.segment
= NULL
;
789 data
->last
.data
= path_data
;
791 /* Save the last point as the current point, if applicable */
792 data
->cp_is_valid
= length
> 1;
794 cpml_pair_from_cairo(&data
->cp
, &path_data
[length
-1]);
796 /* Invalidate cairo_path: should be recomputed */
797 clear_parent((AdgModel
*) path
);
801 needed_pairs(CpmlPrimitiveType type
, gboolean cp_is_valid
)
805 case CAIRO_PATH_CLOSE_PATH
:
806 g_return_val_if_fail(cp_is_valid
, 0);
809 case CAIRO_PATH_MOVE_TO
:
812 case CAIRO_PATH_LINE_TO
:
813 g_return_val_if_fail(cp_is_valid
, 0);
816 case CAIRO_PATH_ARC_TO
:
817 g_return_val_if_fail(cp_is_valid
, 0);
820 case CAIRO_PATH_CURVE_TO
:
821 g_return_val_if_fail(cp_is_valid
, 0);
825 g_return_val_if_reached(0);
832 clear_operation(AdgPath
*path
)
834 AdgPathPrivate
*data
;
835 AdgOperation
*operation
;
838 operation
= &data
->operation
;
840 if (operation
->action
== ADG_ACTION_NONE
)
843 g_warning("An operation is still active while clearing the path "
844 "(action `%d')", operation
->action
);
845 operation
->action
= ADG_ACTION_NONE
;
849 append_operation(AdgPath
*path
, AdgAction action
, ...)
851 AdgPathPrivate
*data
;
852 AdgOperation
*operation
;
857 if (!data
->cp_is_valid
) {
858 g_warning("Operation requested but path has no current primitive "
859 "(action `%d')", action
);
863 operation
= &data
->operation
;
864 if (operation
->action
!= ADG_ACTION_NONE
) {
865 g_warning("Operation requested but another operation is yet active"
866 "(operators: new `%d', old `%d')",
867 action
, operation
->action
);
868 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
869 "operators can and may cohexist. As an example, a "
870 "fillet followed by a polar chamfer should be done.");
874 va_start(var_args
, action
);
878 case ADG_ACTION_CHAMFER
:
879 operation
->data
.chamfer
.delta1
= va_arg(var_args
, double);
880 operation
->data
.chamfer
.delta2
= va_arg(var_args
, double);
883 case ADG_ACTION_FILLET
:
884 operation
->data
.fillet
.radius
= va_arg(var_args
, double);
887 case ADG_ACTION_NONE
:
892 g_warning("Operation not recognized (action `%d')", action
);
897 operation
->action
= action
;
904 do_operation(AdgPath
*path
, cairo_path_data_t
*path_data
)
906 AdgPathPrivate
*data
;
909 AdgPrimitive current
;
910 cairo_path_data_t current_org
;
913 action
= data
->operation
.action
;
914 cpml_segment_from_cairo(&segment
, read_cpml_path(path
));
916 /* Construct the current primitive, that is the primitive to be inserted.
917 * Its org is a copy of the end point of the last primitive: it can be
918 * modified without affecting anything else. It is expected the operation
919 * functions will add to @path the primitives required but NOT to add
920 * @current, as this one will be inserted automatically. */
921 current
.segment
= &segment
;
922 current
.org
= ¤t_org
;
923 current
.data
= path_data
;
924 cpml_pair_to_cairo(&data
->cp
, ¤t_org
);
928 case ADG_ACTION_NONE
:
931 case ADG_ACTION_CHAMFER
:
932 do_chamfer(path
, ¤t
);
935 case ADG_ACTION_FILLET
:
936 do_fillet(path
, ¤t
);
940 g_warning("Operation not implemented (action `%d')", action
);
946 do_chamfer(AdgPath
*path
, AdgPrimitive
*current
)
948 AdgPathPrivate
*data
;
950 gdouble delta1
, delta2
;
956 delta1
= data
->operation
.data
.chamfer
.delta1
;
957 len1
= cpml_primitive_length(last
);
959 if (delta1
>= len1
) {
960 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
965 delta2
= data
->operation
.data
.chamfer
.delta2
;
966 len2
= cpml_primitive_length(current
);
968 if (delta2
>= len2
) {
969 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
974 /* Change the end point of the last primitive */
975 cpml_primitive_pair_at(last
, &pair
, 1. - delta1
/ len1
);
976 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(last
, -1));
978 /* Change the start point of the current primitive */
979 cpml_primitive_pair_at(current
, &pair
, delta2
/ len2
);
980 cpml_pair_to_cairo(&pair
, cpml_primitive_get_point(current
, 0));
982 /* Add the chamfer line */
983 data
->operation
.action
= ADG_ACTION_NONE
;
984 adg_path_append(path
, CAIRO_PATH_LINE_TO
, &pair
);
988 do_fillet(AdgPath
*path
, AdgPrimitive
*current
)
990 AdgPathPrivate
*data
;
991 AdgPrimitive
*last
, *current_dup
, *last_dup
;
992 gdouble radius
, offset
, pos
;
993 AdgPair center
, vector
, p
[3];
997 current_dup
= adg_primitive_deep_dup(current
);
999 /* Force current_dup to point to the original segment so a
1000 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
1001 current_dup
->segment
= current
->segment
;
1003 last_dup
= adg_primitive_deep_dup(last
);
1004 radius
= data
->operation
.data
.fillet
.radius
;
1005 offset
= is_convex(last_dup
, current_dup
) ? -radius
: radius
;
1007 /* Find the center of the fillet from the intersection between
1008 * the last and current primitives offseted by radius */
1009 cpml_primitive_offset(current_dup
, offset
);
1010 cpml_primitive_offset(last_dup
, offset
);
1011 if (cpml_primitive_intersection(current_dup
, last_dup
,
1013 g_warning("Fillet not applicable (radius = %lf)", radius
);
1014 g_free(current_dup
);
1019 /* Compute the start point of the fillet */
1020 pos
= cpml_primitive_near_pos(last_dup
, ¢er
);
1021 cpml_primitive_vector_at(last_dup
, &vector
, pos
);
1022 cpml_vector_set_length(&vector
, offset
);
1023 cpml_vector_normal(&vector
);
1024 cpml_pair_sub(cpml_pair_copy(&p
[0], ¢er
), &vector
);
1026 /* Compute the mid point of the fillet */
1027 cpml_pair_from_cairo(&vector
, current
->org
);
1028 cpml_pair_sub(&vector
, ¢er
);
1029 cpml_vector_set_length(&vector
, radius
);
1030 cpml_pair_add(cpml_pair_copy(&p
[1], ¢er
), &vector
);
1032 /* Compute the end point of the fillet */
1033 pos
= cpml_primitive_near_pos(current_dup
, ¢er
);
1034 cpml_primitive_vector_at(current_dup
, &vector
, pos
);
1035 cpml_vector_set_length(&vector
, offset
);
1036 cpml_vector_normal(&vector
);
1037 cpml_pair_sub(cpml_pair_copy(&p
[2], ¢er
), &vector
);
1039 g_free(current_dup
);
1042 /* Change the end point of the last primitive */
1043 cpml_pair_to_cairo(&p
[0], cpml_primitive_get_point(last
, -1));
1045 /* Change the start point of the current primitive */
1046 cpml_pair_to_cairo(&p
[2], cpml_primitive_get_point(current
, 0));
1048 /* Add the fillet arc */
1049 data
->operation
.action
= ADG_ACTION_NONE
;
1050 adg_path_append(path
, CAIRO_PATH_ARC_TO
, &p
[1], &p
[2]);
1054 is_convex(const AdgPrimitive
*primitive1
, const AdgPrimitive
*primitive2
)
1057 gdouble angle1
, angle2
;
1059 cpml_primitive_vector_at(primitive1
, &v1
, -1);
1060 cpml_primitive_vector_at(primitive2
, &v2
, 0);
1062 /* Probably there is a smarter way to get this without trygonometry */
1063 angle1
= cpml_vector_angle(&v1
);
1064 angle2
= cpml_vector_angle(&v2
);
1066 if (angle1
> angle2
)
1069 return angle2
-angle1
> M_PI
;