[build] Moved project dirs under src/
[adg.git] / src / adg / adg-path.c
blob017f729708a14eaf3ec37aa9f00719cc328a02af
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010 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.
21 /**
22 * SECTION:adg-path
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 #CPML_MOVE primitive
47 * to the starting point of the segment is automatically added by
48 * cairo; in ADG, after an adg_path_close() the current point is unset.
49 **/
51 /**
52 * AdgPath:
54 * All fields are private and should not be used directly.
55 * Use its public methods instead.
56 **/
59 #include "adg-internal.h"
60 #include "adg-path.h"
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
63 #include <string.h>
64 #include <math.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 static void clear_operation (AdgPath *path);
80 static gboolean append_operation (AdgPath *path,
81 AdgAction action,
82 ...);
83 static void do_operation (AdgPath *path,
84 cairo_path_data_t
85 *path_data);
86 static void do_action (AdgPath *path,
87 AdgAction action,
88 AdgPrimitive *primitive);
89 static void do_chamfer (AdgPath *path,
90 AdgPrimitive *current);
91 static void do_fillet (AdgPath *path,
92 AdgPrimitive *current);
93 static gboolean is_convex (const AdgPrimitive
94 *primitive1,
95 const AdgPrimitive
96 *primitive2);
97 static const gchar * action_name (AdgAction action);
98 static void get_named_pair (const gchar *name,
99 AdgPair *pair,
100 gpointer user_data);
101 static void dup_reverse_named_pairs (AdgModel *model,
102 const AdgMatrix*matrix);
105 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
108 static void
109 adg_path_class_init(AdgPathClass *klass)
111 GObjectClass *gobject_class;
112 AdgModelClass *model_class;
113 AdgTrailClass *trail_class;
115 gobject_class = (GObjectClass *) klass;
116 model_class = (AdgModelClass *) klass;
117 trail_class = (AdgTrailClass *) klass;
119 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
121 gobject_class->finalize = finalize;
123 model_class->clear = clear;
124 model_class->changed = changed;
126 trail_class->get_cpml_path = get_cpml_path;
129 static void
130 adg_path_init(AdgPath *path)
132 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
133 AdgPathPrivate);
135 data->cp_is_valid = FALSE;
136 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
137 data->operation.action = ADG_ACTION_NONE;
139 path->data = data;
142 static void
143 finalize(GObject *object)
145 AdgPath *path;
146 AdgPathPrivate *data;
148 path = (AdgPath *) object;
149 data = path->data;
151 g_array_free(data->cpml.array, TRUE);
152 clear_operation(path);
154 if (PARENT_OBJECT_CLASS->finalize)
155 PARENT_OBJECT_CLASS->finalize(object);
160 * adg_path_new:
162 * Creates a new path model. The path should be constructed
163 * programmatically by using the methods provided by #AdgPath.
165 * Returns: the newly created path model
167 AdgPath *
168 adg_path_new(void)
170 return g_object_new(ADG_TYPE_PATH, NULL);
174 * adg_path_get_current_point:
175 * @path: an #AdgPath
177 * Gets the current point of @path, which is conceptually the
178 * final point reached by the path so far.
180 * If there is no defined current point, %NULL is returned.
181 * It is possible to check this in advance with
182 * adg_path_has_current_point().
184 * Most #AdgPath methods alter the current point and most of them
185 * expect a current point to be defined otherwise will fail triggering
186 * a warning. Check the description of every method for specific details.
188 * Returns: the current point or %NULL on no current point set or errors
190 const AdgPair *
191 adg_path_get_current_point(AdgPath *path)
193 AdgPathPrivate *data;
195 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
197 data = path->data;
199 if (!data->cp_is_valid)
200 return NULL;
202 return &data->cp;
206 * adg_path_has_current_point:
207 * @path: an #AdgPath
209 * Returns whether a current point is defined on @path.
210 * See adg_path_get_current_point() for details on the current point.
212 * Returns: whether a current point is defined
214 gboolean
215 adg_path_has_current_point(AdgPath *path)
217 AdgPathPrivate *data;
219 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
221 data = path->data;
223 return data->cp_is_valid;
227 * adg_path_last_primitive:
228 * @path: an #AdgPath
230 * Gets the last primitive appended to @path. The returned struct
231 * is owned by @path and should not be freed or modified.
233 * Returns: a pointer to the last appended primitive or %NULL on errors
235 const AdgPrimitive *
236 adg_path_last_primitive(AdgPath *path)
238 AdgPathPrivate *data;
240 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
242 data = path->data;
244 return &data->last;
248 * adg_path_over_primitive:
249 * @path: an #AdgPath
251 * Gets the primitive before the last one appended to @path. The
252 * "over" term comes from forth, where the %OVER operator works
253 * on the stack in the same way as adg_path_over_primitive() works
254 * on @path. The returned struct is owned by @path and should not
255 * be freed or modified.
257 * Returns: a pointer to the primitive before the last appended one
258 * or %NULL on errors
260 const AdgPrimitive *
261 adg_path_over_primitive(AdgPath *path)
263 AdgPathPrivate *data;
265 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
267 data = path->data;
269 return &data->over;
273 * adg_path_append:
274 * @path: an #AdgPath
275 * @type: a #cairo_data_type_t value
276 * @...: point data, specified as #AdgPair pointers
278 * Generic method to append a primitive to @path. The number of #AdgPair
279 * structs depends on @type: there is no way with this function to
280 * reserve more cairo_path_data_t structs than what is needed by the
281 * primitive.
283 * This function accepts also the special #CPML_ARC primitive.
285 * If @path has no current point while the requested primitive needs it,
286 * a warning message will be triggered without other effect.
288 void
289 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
291 va_list var_args;
293 va_start(var_args, type);
294 adg_path_append_valist(path, type, var_args);
295 va_end(var_args);
299 * adg_path_append_valist:
300 * @path: an #AdgPath
301 * @type: a #cairo_data_type_t value
302 * @var_args: point data, specified as #AdgPair pointers
304 * va_list version of adg_path_append().
306 void
307 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
309 AdgPathPrivate *data;
310 AdgPrimitive primitive;
311 gint length, cnt;
312 cairo_path_data_t org;
313 cairo_path_data_t *path_data;
314 const AdgPair *pair;
316 g_return_if_fail(ADG_IS_PATH(path));
318 data = path->data;
319 length = needed_pairs(type);
320 if (length == 0)
321 return;
323 /* Set a copy of the current point as the primitive origin */
324 cpml_pair_to_cairo(&data->cp, &org);
325 primitive.org = &org;
327 /* Build the cairo_path_data_t array */
328 primitive.data = path_data = g_new(cairo_path_data_t, length);
330 path_data->header.type = type;
331 path_data->header.length = length;
333 for (cnt = 1; cnt < length; ++ cnt) {
334 pair = va_arg(var_args, AdgPair *);
335 if (pair == NULL) {
336 g_free(primitive.data);
337 g_warning(_("%s: null pair caught while parsing arguments"),
338 G_STRLOC);
339 return;
342 ++ path_data;
343 cpml_pair_to_cairo(pair, path_data);
346 /* Terminate the creation of the temporary primitive */
347 primitive.segment = NULL;
349 /* Append this primitive to @path */
350 append_primitive(path, &primitive);
352 g_free(primitive.data);
356 * adg_path_append_primitive:
357 * @path: an #AdgPath
358 * @primitive: the #AdgPrimitive to append
360 * Appends @primitive to @path. The primitive to add is considered the
361 * continuation of the current path so the <structfield>org</structfield>
362 * component of @primitive is not used. Anyway the current point is
363 * checked against it: they must be equal or the function will fail
364 * without further processing.
366 void
367 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
369 AdgPathPrivate *data;
370 AdgPrimitive *primitive_dup;
372 g_return_if_fail(ADG_IS_PATH(path));
373 g_return_if_fail(primitive != NULL);
374 g_return_if_fail(primitive->org != NULL);
376 data = path->data;
378 g_return_if_fail(primitive->org->point.x == data->cp.x &&
379 primitive->org->point.y == data->cp.y);
381 /* The primitive data could be modified by pending operations:
382 * work on a copy */
383 primitive_dup = adg_primitive_deep_dup(primitive);
385 append_primitive(path, primitive_dup);
387 g_free(primitive_dup);
391 * adg_path_append_segment:
392 * @path: an #AdgPath
393 * @segment: the #AdgSegment to append
395 * Appends @segment to @path.
397 void
398 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
400 AdgPathPrivate *data;
402 g_return_if_fail(ADG_IS_PATH(path));
403 g_return_if_fail(segment != NULL);
405 data = path->data;
407 clear_parent((AdgModel *) path);
408 data->cpml.array = g_array_append_vals(data->cpml.array,
409 segment->data, segment->num_data);
413 * adg_path_append_cpml_path:
414 * @path: an #AdgPath
415 * @cpml_path: the #cairo_path_t path to append
417 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
418 * #cairo_path_t, so this function can be feeded with both.
420 void
421 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
423 AdgPathPrivate *data;
425 g_return_if_fail(ADG_IS_PATH(path));
426 g_return_if_fail(cpml_path != NULL);
428 data = path->data;
430 clear_parent((AdgModel *) path);
431 data->cpml.array = g_array_append_vals(data->cpml.array,
432 cpml_path->data,
433 cpml_path->num_data);
437 * adg_path_move_to:
438 * @path: an #AdgPath
439 * @pair: the destination coordinates
441 * Begins a new segment. After this call the current point will be @pair.
443 void
444 adg_path_move_to(AdgPath *path, const AdgPair *pair)
446 adg_path_append(path, CPML_MOVE, pair);
450 * adg_path_move_to_explicit:
451 * @path: an #AdgPath
452 * @x: the new x coordinate
453 * @y: the new y coordinate
455 * Convenient function to call adg_path_move_to() using explicit
456 * coordinates instead of #AdgPair.
458 void
459 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
461 AdgPair p;
463 p.x = x;
464 p.y = y;
466 adg_path_append(path, CPML_MOVE, &p);
470 * adg_path_line_to:
471 * @path: an #AdgPath
472 * @pair: the destination coordinates
474 * Adds a line to @path from the current point to @pair. After this
475 * call the current point will be @pair.
477 * If @path has no current point before this call, this function will
478 * trigger a warning without other effect.
480 void
481 adg_path_line_to(AdgPath *path, const AdgPair *pair)
483 adg_path_append(path, CPML_LINE, pair);
487 * adg_path_line_to_explicit:
488 * @path: an #AdgPath
489 * @x: the new x coordinate
490 * @y: the new y coordinate
492 * Convenient function to call adg_path_line_to() using explicit
493 * coordinates instead of #AdgPair.
495 void
496 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
498 AdgPair p;
500 p.x = x;
501 p.y = y;
503 adg_path_append(path, CPML_LINE, &p);
507 * adg_path_arc_to:
508 * @path: an #AdgPath
509 * @throught: an arbitrary point on the arc
510 * @pair: the destination coordinates
512 * Adds an arc to the path from the current point to @pair, passing
513 * throught @throught. After this call the current point will be @pair.
515 * If @path has no current point before this call, this function will
516 * trigger a warning without other effect.
518 void
519 adg_path_arc_to(AdgPath *path, const AdgPair *throught, const AdgPair *pair)
521 adg_path_append(path, CPML_ARC, throught, pair);
525 * adg_path_arc_to_explicit:
526 * @path: an #AdgPath
527 * @x1: the x coordinate of an intermediate point
528 * @y1: the y coordinate of an intermediate point
529 * @x2: the x coordinate of the end of the arc
530 * @y2: the y coordinate of the end of the arc
532 * Convenient function to call adg_path_arc_to() using explicit
533 * coordinates instead of #AdgPair.
535 void
536 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
537 gdouble x2, gdouble y2)
539 AdgPair p[2];
541 p[0].x = x1;
542 p[0].y = y1;
543 p[1].x = x2;
544 p[1].y = y2;
546 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
550 * adg_path_curve_to:
551 * @path: an #AdgPath
552 * @control1: the first control point of the curve
553 * @control2: the second control point of the curve
554 * @pair: the destination coordinates
556 * Adds a cubic Bézier curve to the path from the current point to
557 * position @pair, using @control1 and @control2 as control points.
558 * After this call the current point will be @pair.
560 * If @path has no current point before this call, this function will
561 * trigger a warning without other effect.
563 void
564 adg_path_curve_to(AdgPath *path, const AdgPair *control1,
565 const AdgPair *control2, const AdgPair *pair)
567 adg_path_append(path, CPML_CURVE, control1, control2, pair);
571 * adg_path_curve_to_explicit:
572 * @path: an #AdgPath
573 * @x1: the x coordinate of the first control point
574 * @y1: the y coordinate of the first control point
575 * @x2: the x coordinate of the second control point
576 * @y2: the y coordinate of the second control point
577 * @x3: the x coordinate of the end of the curve
578 * @y3: the y coordinate of the end of the curve
580 * Convenient function to call adg_path_curve_to() using explicit
581 * coordinates instead of #AdgPair.
583 void
584 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
585 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
587 AdgPair p[3];
589 p[0].x = x1;
590 p[0].y = y1;
591 p[1].x = x2;
592 p[1].y = y2;
593 p[2].x = x3;
594 p[2].y = y3;
596 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
600 * adg_path_close:
601 * @path: an #AdgPath
603 * Adds a line segment to the path from the current point to the
604 * beginning of the current segment, (the most recent point passed
605 * to an adg_path_move_to()), and closes this segment.
606 * After this call the current point will be unset.
608 * The behavior of adg_path_close() is distinct from simply calling
609 * adg_line_to() with the coordinates of the segment starting point.
610 * When a closed segment is stroked, there are no caps on the ends.
611 * Instead, there is a line join connecting the final and initial
612 * primitive of the segment.
614 * If @path has no current point before this call, this function will
615 * trigger a warning without other effect.
617 void
618 adg_path_close(AdgPath *path)
620 adg_path_append(path, CPML_CLOSE);
624 * adg_path_arc:
625 * @path: an #AdgPath
626 * @center: coordinates of the center of the arc
627 * @r: the radius of the arc
628 * @start: the start angle, in radians
629 * @end: the end angle, in radians
631 * A more usual way to add an arc to @path. After this call, the current
632 * point will be the computed end point of the arc. The arc will be
633 * rendered in increasing angle, accordling to @start and @end. This means
634 * if @start is less than @end, the arc will be rendered in clockwise
635 * direction (accordling to the default cairo coordinate system) while if
636 * @start is greather than @end, the arc will be rendered in couterclockwise
637 * direction.
639 * By explicitely setting the whole arc data, the start point could be
640 * different from the current point. In this case, if @path has no
641 * current point before the call a #CPML_MOVE to the start point of
642 * the arc will be automatically prepended to the arc. If @path has a
643 * current point, a #CPML_LINE to the start point of the arc will be
644 * used instead of the "move to" primitive.
646 void
647 adg_path_arc(AdgPath *path, const AdgPair *center, gdouble r,
648 gdouble start, gdouble end)
650 AdgPathPrivate *data;
651 AdgPair p[3];
653 g_return_if_fail(ADG_IS_PATH(path));
654 g_return_if_fail(center != NULL);
656 data = path->data;
657 cpml_vector_from_angle(&p[0], start);
658 cpml_vector_from_angle(&p[1], (end-start) / 2);
659 cpml_vector_from_angle(&p[2], end);
661 cpml_vector_set_length(&p[0], r);
662 cpml_vector_set_length(&p[1], r);
663 cpml_vector_set_length(&p[2], r);
665 p[0].x += center->x;
666 p[0].y += center->y;
667 p[1].x += center->x;
668 p[1].y += center->y;
669 p[2].x += center->x;
670 p[2].y += center->y;
672 if (!data->cp_is_valid)
673 adg_path_append(path, CPML_MOVE, &p[0]);
674 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
675 adg_path_append(path, CPML_LINE, &p[0]);
677 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
681 * adg_path_arc_explicit:
682 * @path: an #AdgPath
683 * @xc: x position of the center of the arc
684 * @yc: y position of the center of the arc
685 * @r: the radius of the arc
686 * @start: the start angle, in radians
687 * @end: the end angle, in radians
689 * Convenient function to call adg_path_arc() using explicit
690 * coordinates instead of #AdgPair.
692 void
693 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
694 gdouble start, gdouble end)
696 AdgPair center;
698 center.x = xc;
699 center.y = yc;
701 adg_path_arc(path, &center, r, start, end);
705 * adg_path_chamfer
706 * @path: an #AdgPath
707 * @delta1: the distance from the intersection point of the current primitive
708 * @delta2: the distance from the intersection point of the next primitive
710 * A binary action that generates a chamfer between two primitives.
711 * The first primitive involved is the current primitive, the second will
712 * be the next primitive appended to @path after this call. The second
713 * primitive is required: if the chamfer operation is not properly
714 * terminated (by not providing the second primitive), any API accessing
715 * the path in reading mode will raise a warning.
717 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
718 * the second primitive is not required: the current close path is used
719 * as first operand while the first primitive of the current segment is
720 * used as second operand.
722 * The chamfer operation requires two lengths: @delta1 specifies the
723 * "quantity" to trim on the first primitive while @delta2 is the same
724 * applied on the second primitive. The term "quantity" means the length
725 * of the portion to cut out from the original primitive (that is the
726 * primitive as would be without the chamfer).
728 void
729 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
731 g_return_if_fail(ADG_IS_PATH(path));
733 if (!append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
734 return;
738 * adg_path_fillet:
739 * @path: an #AdgPath
740 * @radius: the radius of the fillet
742 * A binary action that joins to primitives with an arc.
743 * The first primitive involved is the current primitive, the second will
744 * be the next primitive appended to @path after this call. The second
745 * primitive is required: if the fillet operation is not properly
746 * terminated (by not providing the second primitive), any API accessing
747 * the path in reading mode will raise a warning.
749 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
750 * the second primitive is not required: the current close path is used
751 * as first operand while the first primitive of the current segment is
752 * used as second operand.
754 void
755 adg_path_fillet(AdgPath *path, gdouble radius)
757 g_return_if_fail(ADG_IS_PATH(path));
759 if (!append_operation(path, ADG_ACTION_FILLET, radius))
760 return;
764 * adg_path_reflect:
765 * @path: an #AdgPath
766 * @vector: the slope of the axis
768 * Reflects the first segment or @path around the axis passing
769 * throught (0, 0) and with a @vector slope. The internal segment
770 * is duplicated and the proper transformation (computed from
771 * @vector) to mirror the segment is applied on all its points.
772 * The result is then reversed with cpml_segment_reverse() and
773 * appended to the original path with adg_path_append_segment().
775 * For convenience, if @vector is %NULL the path is reversed
776 * around the x axis (y=0).
778 void
779 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
781 AdgModel *model;
782 AdgMatrix matrix;
783 AdgSegment segment, *dup_segment;
785 g_return_if_fail(ADG_IS_PATH(path));
786 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
788 model = (AdgModel *) path;
790 if (vector == NULL) {
791 cairo_matrix_init_scale(&matrix, 1, -1);
792 } else {
793 CpmlVector slope;
794 gdouble cos2angle, sin2angle;
796 cpml_pair_copy(&slope, vector);
797 cpml_vector_set_length(&slope, 1);
799 if (slope.x == 0 && slope.y == 0) {
800 g_warning(_("%s: the axis of the reflection is not known"),
801 G_STRLOC);
802 return;
805 sin2angle = 2. * vector->x * vector->y;
806 cos2angle = 2. * vector->x * vector->x - 1;
808 cairo_matrix_init(&matrix, cos2angle, sin2angle,
809 sin2angle, -cos2angle, 0, 0);
812 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
813 return;
815 /* No need to reverse an empty segment */
816 if (segment.num_data == 0 || segment.num_data == 0)
817 return;
819 dup_segment = adg_segment_deep_dup(&segment);
820 if (dup_segment == NULL)
821 return;
823 cpml_segment_reverse(dup_segment);
824 cpml_segment_transform(dup_segment, &matrix);
825 dup_segment->data[0].header.type = CPML_LINE;
827 adg_path_append_segment(path, dup_segment);
829 g_free(dup_segment);
831 dup_reverse_named_pairs(model, &matrix);
835 static void
836 clear(AdgModel *model)
838 AdgPath *path;
839 AdgPathPrivate *data;
841 path = (AdgPath *) model;
842 data = path->data;
844 g_array_set_size(data->cpml.array, 0);
845 clear_operation(path);
846 clear_parent(model);
849 static void
850 clear_parent(AdgModel *model)
852 if (PARENT_MODEL_CLASS->clear)
853 PARENT_MODEL_CLASS->clear(model);
856 static void
857 changed(AdgModel *model)
859 clear_parent(model);
861 if (PARENT_MODEL_CLASS->changed)
862 PARENT_MODEL_CLASS->changed(model);
865 static CpmlPath *
866 get_cpml_path(AdgTrail *trail)
868 clear_parent((AdgModel *) trail);
869 return read_cpml_path((AdgPath *) trail);
872 static CpmlPath *
873 read_cpml_path(AdgPath *path)
875 AdgPathPrivate *data = path->data;
877 /* Always regenerate the CpmlPath as it is a trivial operation */
878 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
879 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
880 data->cpml.path.num_data = (data->cpml.array)->len;
882 return &data->cpml.path;
885 static void
886 append_primitive(AdgPath *path, AdgPrimitive *current)
888 AdgPathPrivate *data;
889 cairo_path_data_t *path_data;
890 int length;
892 data = path->data;
893 path_data = current->data;
894 length = path_data[0].header.length;
896 /* Execute any pending operation */
897 do_operation(path, path_data);
899 /* Append the path data to the internal path array */
900 data->cpml.array = g_array_append_vals(data->cpml.array,
901 path_data, length);
903 /* Set path data to point to the recently appended cairo_path_data_t
904 * primitive: the first struct is the header */
905 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
906 (data->cpml.array)->len - length;
908 /* Store the over primitive */
909 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
911 /* Set the last primitive for subsequent binary operations */
912 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
913 data->last.segment = NULL;
914 data->last.data = path_data;
916 /* Save the last point as the current point, if applicable */
917 data->cp_is_valid = length > 1;
918 if (length > 1)
919 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
921 /* Invalidate cairo_path: should be recomputed */
922 clear_parent((AdgModel *) path);
925 static gint
926 needed_pairs(CpmlPrimitiveType type)
928 switch (type) {
929 case CPML_CLOSE:
930 return 1;
931 case CPML_MOVE:
932 return 2;
933 case CPML_LINE:
934 return 2;
935 case CPML_ARC:
936 return 3;
937 case CPML_CURVE:
938 return 4;
939 default:
940 g_return_val_if_reached(0);
943 return 0;
946 static void
947 clear_operation(AdgPath *path)
949 AdgPathPrivate *data;
950 AdgOperation *operation;
952 data = path->data;
953 operation = &data->operation;
955 if (operation->action != ADG_ACTION_NONE) {
956 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
957 G_STRLOC, action_name(operation->action));
958 operation->action = ADG_ACTION_NONE;
962 static gboolean
963 append_operation(AdgPath *path, AdgAction action, ...)
965 AdgPathPrivate *data;
966 AdgOperation *operation;
967 va_list var_args;
969 data = path->data;
971 if (data->last.data == NULL) {
972 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
973 G_STRLOC, action_name(action));
974 return FALSE;
977 operation = &data->operation;
978 if (operation->action != ADG_ACTION_NONE) {
979 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
980 G_STRLOC, action_name(action), action_name(operation->action));
981 /* TODO: this is a rude simplification, as a lot of actions
982 * could be chained up. As an example, a fillet followed by
983 * a polar chamfer is quite common.
985 return FALSE;
988 va_start(var_args, action);
990 switch (action) {
992 case ADG_ACTION_CHAMFER:
993 operation->data.chamfer.delta1 = va_arg(var_args, double);
994 operation->data.chamfer.delta2 = va_arg(var_args, double);
995 break;
997 case ADG_ACTION_FILLET:
998 operation->data.fillet.radius = va_arg(var_args, double);
999 break;
1001 case ADG_ACTION_NONE:
1002 va_end(var_args);
1003 return TRUE;
1005 default:
1006 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
1007 va_end(var_args);
1008 return FALSE;
1011 operation->action = action;
1012 va_end(var_args);
1014 if (data->last.data[0].header.type == CPML_CLOSE) {
1015 /* Special case: an action with the close primitive should
1016 * be resolved now by changing the close primitive to a
1017 * line-to and using it as second operand and use the first
1018 * primitive of the current segment as first operand */
1019 guint length;
1020 cairo_path_data_t *path_data;
1021 CpmlSegment segment;
1022 CpmlPrimitive current;
1024 length = data->cpml.array->len;
1026 /* Ensure the close path primitive is not the only data */
1027 g_return_val_if_fail(length > 1, FALSE);
1029 /* Allocate one more item once for all to accept the
1030 * conversion from a close to line-to primitive */
1031 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1032 path_data = (cairo_path_data_t *) data->cpml.array->data;
1033 --data->cpml.array->len;
1035 /* Set segment and current (the first primitive of segment) */
1036 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1037 while (cpml_segment_next(&segment))
1039 cpml_primitive_from_segment(&current, &segment);
1041 /* Convert close path to a line-to primitive */
1042 ++data->cpml.array->len;
1043 path_data[length - 1].header.type = CPML_LINE;
1044 path_data[length - 1].header.length = 2;
1045 path_data[length] = *current.org;
1047 data->last.segment = &segment;
1048 data->last.org = &path_data[length - 2];
1049 data->last.data = &path_data[length - 1];
1051 do_action(path, action, &current);
1055 return TRUE;
1058 static void
1059 do_operation(AdgPath *path, cairo_path_data_t *path_data)
1061 AdgPathPrivate *data;
1062 AdgAction action;
1063 AdgSegment segment;
1064 AdgPrimitive current;
1065 cairo_path_data_t current_org;
1067 data = path->data;
1068 action = data->operation.action;
1069 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1071 /* Construct the current primitive, that is the primitive to be
1072 * mixed with the last primitive with the specified operation.
1073 * Its org is a copy of the end point of the last primitive: it can be
1074 * modified without affecting anything else. It is expected the operation
1075 * functions will add to @path the primitives required but NOT to add
1076 * @current, as this one will be inserted automatically. */
1077 current.segment = &segment;
1078 current.org = &current_org;
1079 current.data = path_data;
1080 cpml_pair_to_cairo(&data->cp, &current_org);
1082 do_action(path, action, &current);
1085 static void
1086 do_action(AdgPath *path, AdgAction action, AdgPrimitive *primitive)
1088 switch (action) {
1089 case ADG_ACTION_NONE:
1090 return;
1091 case ADG_ACTION_CHAMFER:
1092 do_chamfer(path, primitive);
1093 break;
1094 case ADG_ACTION_FILLET:
1095 do_fillet(path, primitive);
1096 break;
1097 default:
1098 g_return_if_reached();
1102 static void
1103 do_chamfer(AdgPath *path, AdgPrimitive *current)
1105 AdgPathPrivate *data;
1106 AdgPrimitive *last;
1107 gdouble delta1, delta2;
1108 gdouble len1, len2;
1109 AdgPair pair;
1111 data = path->data;
1112 last = &data->last;
1113 delta1 = data->operation.data.chamfer.delta1;
1114 len1 = cpml_primitive_get_length(last);
1116 if (delta1 >= len1) {
1117 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1118 G_STRLOC, delta1, len1);
1119 return;
1122 delta2 = data->operation.data.chamfer.delta2;
1123 len2 = cpml_primitive_get_length(current);
1125 if (delta2 >= len2) {
1126 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1127 G_STRLOC, delta1, len1);
1128 return;
1131 /* Change the end point of the last primitive */
1132 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1133 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1135 /* Change the start point of the current primitive */
1136 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1137 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1139 /* Add the chamfer line */
1140 data->operation.action = ADG_ACTION_NONE;
1141 adg_path_append(path, CPML_LINE, &pair);
1144 static void
1145 do_fillet(AdgPath *path, AdgPrimitive *current)
1147 AdgPathPrivate *data;
1148 AdgPrimitive *last, *current_dup, *last_dup;
1149 gdouble radius, offset, pos;
1150 AdgPair center, vector, p[3];
1152 data = path->data;
1153 last = &data->last;
1154 current_dup = adg_primitive_deep_dup(current);
1155 last_dup = adg_primitive_deep_dup(last);
1156 radius = data->operation.data.fillet.radius;
1157 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1159 /* Find the center of the fillet from the intersection between
1160 * the last and current primitives offseted by radius */
1161 cpml_primitive_offset(current_dup, offset);
1162 cpml_primitive_offset(last_dup, offset);
1163 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1164 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1165 G_STRLOC, radius);
1166 g_free(current_dup);
1167 g_free(last_dup);
1168 return;
1171 /* Compute the start point of the fillet */
1172 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1173 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1174 cpml_vector_set_length(&vector, offset);
1175 cpml_vector_normal(&vector);
1176 p[0].x = center.x - vector.x;
1177 p[0].y = center.y - vector.y;
1179 /* Compute the mid point of the fillet */
1180 cpml_pair_from_cairo(&vector, current->org);
1181 vector.x -= center.x;
1182 vector.y -= center.y;
1183 cpml_vector_set_length(&vector, radius);
1184 p[1].x = center.x + vector.x;
1185 p[1].y = center.y + vector.y;
1187 /* Compute the end point of the fillet */
1188 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1189 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1190 cpml_vector_set_length(&vector, offset);
1191 cpml_vector_normal(&vector);
1192 p[2].x = center.x - vector.x;
1193 p[2].y = center.y - vector.y;
1195 g_free(current_dup);
1196 g_free(last_dup);
1198 /* Change the end point of the last primitive */
1199 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1201 /* Change the start point of the current primitive */
1202 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
1204 /* Add the fillet arc */
1205 data->operation.action = ADG_ACTION_NONE;
1206 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1209 static gboolean
1210 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1212 CpmlVector v1, v2;
1213 gdouble angle1, angle2;
1215 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1216 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1218 /* Probably there is a smarter way to get this without trygonometry */
1219 angle1 = cpml_vector_angle(&v1);
1220 angle2 = cpml_vector_angle(&v2);
1222 if (angle1 > angle2)
1223 angle1 -= M_PI*2;
1225 return angle2-angle1 > M_PI;
1228 static const gchar *
1229 action_name(AdgAction action)
1231 switch (action) {
1232 case ADG_ACTION_NONE:
1233 return "NULL";
1234 case ADG_ACTION_CHAMFER:
1235 return "CHAMFER";
1236 case ADG_ACTION_FILLET:
1237 return "FILLET";
1240 return "undefined";
1243 static void
1244 get_named_pair(const gchar *name, AdgPair *pair, gpointer user_data)
1246 GSList **named_pairs;
1247 AdgNamedPair *named_pair;
1249 named_pairs = user_data;
1251 named_pair = g_new(AdgNamedPair, 1);
1252 named_pair->name = name;
1253 named_pair->pair = *pair;
1255 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1258 static void
1259 dup_reverse_named_pairs(AdgModel *model, const AdgMatrix *matrix)
1261 AdgNamedPair *old_named_pair;
1262 AdgNamedPair named_pair;
1263 GSList *named_pairs;
1265 /* Populate named_pairs with all the named pairs of model */
1266 named_pairs = NULL;
1267 adg_model_foreach_named_pair(model, get_named_pair, &named_pairs);
1269 /* Readd the pairs applying the reversing transformation matrix to
1270 * their coordinates and prepending a "-" to their name */
1271 while (named_pairs) {
1272 old_named_pair = named_pairs->data;
1274 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1275 named_pair.pair = old_named_pair->pair;
1276 cpml_pair_transform(&named_pair.pair, matrix);
1278 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1280 g_free((gpointer) named_pair.name);
1281 named_pairs = g_slist_delete_link(named_pairs, named_pairs);