[ADG] Removed ADG_MESSAGE() macro
[adg.git] / adg / adg-path.c
blob18b49f5a92e5deeb1dd9723616d7939a357d1338
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.
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 <math.h>
65 #define PARENT_OBJECT_CLASS ((GObjectClass *) adg_path_parent_class)
66 #define PARENT_MODEL_CLASS ((AdgModelClass *) adg_path_parent_class)
69 static void finalize (GObject *object);
70 static void clear (AdgModel *model);
71 static void clear_parent (AdgModel *model);
72 static void changed (AdgModel *model);
73 static CpmlPath * get_cpml_path (AdgTrail *trail);
74 static CpmlPath * read_cpml_path (AdgPath *path);
75 static void append_primitive (AdgPath *path,
76 AdgPrimitive *primitive);
77 static gint needed_pairs (CpmlPrimitiveType type);
78 static void clear_operation (AdgPath *path);
79 static gboolean append_operation (AdgPath *path,
80 AdgAction action,
81 ...);
82 static void do_operation (AdgPath *path,
83 cairo_path_data_t
84 *path_data);
85 static void do_action (AdgPath *path,
86 AdgAction action,
87 AdgPrimitive *primitive);
88 static void do_chamfer (AdgPath *path,
89 AdgPrimitive *current);
90 static void do_fillet (AdgPath *path,
91 AdgPrimitive *current);
92 static gboolean is_convex (const AdgPrimitive
93 *primitive1,
94 const AdgPrimitive
95 *primitive2);
96 static const gchar * action_name (AdgAction action);
97 static void get_named_pair (const gchar *name,
98 AdgPair *pair,
99 gpointer user_data);
100 static void dup_reverse_named_pairs (AdgModel *model,
101 const AdgMatrix*matrix);
104 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
107 static void
108 adg_path_class_init(AdgPathClass *klass)
110 GObjectClass *gobject_class;
111 AdgModelClass *model_class;
112 AdgTrailClass *trail_class;
114 gobject_class = (GObjectClass *) klass;
115 model_class = (AdgModelClass *) klass;
116 trail_class = (AdgTrailClass *) klass;
118 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
120 gobject_class->finalize = finalize;
122 model_class->clear = clear;
123 model_class->changed = changed;
125 trail_class->get_cpml_path = get_cpml_path;
128 static void
129 adg_path_init(AdgPath *path)
131 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
132 AdgPathPrivate);
134 data->cp_is_valid = FALSE;
135 data->cpml.array = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
136 data->operation.action = ADG_ACTION_NONE;
138 path->data = data;
141 static void
142 finalize(GObject *object)
144 AdgPath *path;
145 AdgPathPrivate *data;
147 path = (AdgPath *) object;
148 data = path->data;
150 g_array_free(data->cpml.array, TRUE);
151 clear_operation(path);
153 if (PARENT_OBJECT_CLASS->finalize)
154 PARENT_OBJECT_CLASS->finalize(object);
159 * adg_path_new:
161 * Creates a new path model. The path should be constructed
162 * programmatically by using the methods provided by #AdgPath.
164 * Returns: the newly created path model
166 AdgPath *
167 adg_path_new(void)
169 return g_object_new(ADG_TYPE_PATH, NULL);
173 * adg_path_get_current_point:
174 * @path: an #AdgPath
176 * Gets the current point of @path, which is conceptually the
177 * final point reached by the path so far.
179 * If there is no defined current point, %NULL is returned.
180 * It is possible to check this in advance with
181 * adg_path_has_current_point().
183 * Most #AdgPath methods alter the current point and most of them
184 * expect a current point to be defined otherwise will fail triggering
185 * a warning. Check the description of every method for specific details.
187 * Returns: the current point or %NULL on no current point set or errors
189 const AdgPair *
190 adg_path_get_current_point(AdgPath *path)
192 AdgPathPrivate *data;
194 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
196 data = path->data;
198 if (!data->cp_is_valid)
199 return NULL;
201 return &data->cp;
205 * adg_path_has_current_point:
206 * @path: an #AdgPath
208 * Returns whether a current point is defined on @path.
209 * See adg_path_get_current_point() for details on the current point.
211 * Returns: whether a current point is defined
213 gboolean
214 adg_path_has_current_point(AdgPath *path)
216 AdgPathPrivate *data;
218 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
220 data = path->data;
222 return data->cp_is_valid;
226 * adg_path_last_primitive:
227 * @path: an #AdgPath
229 * Gets the last primitive appended to @path. The returned struct
230 * is owned by @path and should not be freed or modified.
232 * Returns: a pointer to the last appended primitive or %NULL on errors
234 const AdgPrimitive *
235 adg_path_last_primitive(AdgPath *path)
237 AdgPathPrivate *data;
239 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
241 data = path->data;
243 return &data->last;
247 * adg_path_over_primitive:
248 * @path: an #AdgPath
250 * Gets the primitive before the last one appended to @path. The
251 * "over" term comes from forth, where the %OVER operator works
252 * on the stack in the same way as adg_path_over_primitive() works
253 * on @path. The returned struct is owned by @path and should not
254 * be freed or modified.
256 * Returns: a pointer to the primitive before the last appended one
257 * or %NULL on errors
259 const AdgPrimitive *
260 adg_path_over_primitive(AdgPath *path)
262 AdgPathPrivate *data;
264 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
266 data = path->data;
268 return &data->over;
272 * adg_path_append:
273 * @path: an #AdgPath
274 * @type: a #cairo_data_type_t value
275 * @...: point data, specified as #AdgPair pointers
277 * Generic method to append a primitive to @path. The number of #AdgPair
278 * structs depends on @type: there is no way with this function to
279 * reserve more cairo_path_data_t structs than what is needed by the
280 * primitive.
282 * This function accepts also the special #CPML_ARC primitive.
284 * If @path has no current point while the requested primitive needs it,
285 * a warning message will be triggered without other effect.
287 void
288 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
290 va_list var_args;
292 va_start(var_args, type);
293 adg_path_append_valist(path, type, var_args);
294 va_end(var_args);
298 * adg_path_append_valist:
299 * @path: an #AdgPath
300 * @type: a #cairo_data_type_t value
301 * @var_args: point data, specified as #AdgPair pointers
303 * va_list version of adg_path_append().
305 void
306 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
308 AdgPathPrivate *data;
309 AdgPrimitive primitive;
310 gint length, cnt;
311 cairo_path_data_t org;
312 cairo_path_data_t *path_data;
313 const AdgPair *pair;
315 g_return_if_fail(ADG_IS_PATH(path));
317 data = path->data;
318 length = needed_pairs(type);
319 if (length == 0)
320 return;
322 /* Set a copy of the current point as the primitive origin */
323 cpml_pair_to_cairo(&data->cp, &org);
324 primitive.org = &org;
326 /* Build the cairo_path_data_t array */
327 primitive.data = path_data = g_new(cairo_path_data_t, length);
329 path_data->header.type = type;
330 path_data->header.length = length;
332 for (cnt = 1; cnt < length; ++ cnt) {
333 pair = va_arg(var_args, AdgPair *);
334 if (pair == NULL) {
335 g_free(primitive.data);
336 g_warning(_("%s: null pair caught while parsing arguments"),
337 G_STRLOC);
338 return;
341 ++ path_data;
342 cpml_pair_to_cairo(pair, path_data);
345 /* Terminate the creation of the temporary primitive */
346 primitive.segment = NULL;
348 /* Append this primitive to @path */
349 append_primitive(path, &primitive);
351 g_free(primitive.data);
355 * adg_path_append_primitive:
356 * @path: an #AdgPath
357 * @primitive: the #AdgPrimitive to append
359 * Appends @primitive to @path. The primitive to add is considered the
360 * continuation of the current path so the <structfield>org</structfield>
361 * component of @primitive is not used. Anyway the current point is
362 * checked against it: they must be equal or the function will fail
363 * without further processing.
365 void
366 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
368 AdgPathPrivate *data;
369 AdgPrimitive *primitive_dup;
371 g_return_if_fail(ADG_IS_PATH(path));
372 g_return_if_fail(primitive != NULL);
373 g_return_if_fail(primitive->org != NULL);
375 data = path->data;
377 g_return_if_fail(primitive->org->point.x == data->cp.x &&
378 primitive->org->point.y == data->cp.y);
380 /* The primitive data could be modified by pending operations:
381 * work on a copy */
382 primitive_dup = adg_primitive_deep_dup(primitive);
384 append_primitive(path, primitive_dup);
386 g_free(primitive_dup);
390 * adg_path_append_segment:
391 * @path: an #AdgPath
392 * @segment: the #AdgSegment to append
394 * Appends @segment to @path.
396 void
397 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
399 AdgPathPrivate *data;
401 g_return_if_fail(ADG_IS_PATH(path));
402 g_return_if_fail(segment != NULL);
404 data = path->data;
406 clear_parent((AdgModel *) path);
407 data->cpml.array = g_array_append_vals(data->cpml.array,
408 segment->data, segment->num_data);
412 * adg_path_append_cpml_path:
413 * @path: an #AdgPath
414 * @cpml_path: the #cairo_path_t path to append
416 * Appends a whole #CpmlPath to @path. #CpmlPath is a superset of
417 * #cairo_path_t, so this function can be feeded with both.
419 void
420 adg_path_append_cpml_path(AdgPath *path, const CpmlPath *cpml_path)
422 AdgPathPrivate *data;
424 g_return_if_fail(ADG_IS_PATH(path));
425 g_return_if_fail(cpml_path != NULL);
427 data = path->data;
429 clear_parent((AdgModel *) path);
430 data->cpml.array = g_array_append_vals(data->cpml.array,
431 cpml_path->data,
432 cpml_path->num_data);
436 * adg_path_move_to:
437 * @path: an #AdgPath
438 * @pair: the destination coordinates
440 * Begins a new segment. After this call the current point will be @pair.
442 void
443 adg_path_move_to(AdgPath *path, const AdgPair *pair)
445 adg_path_append(path, CPML_MOVE, pair);
449 * adg_path_move_to_explicit:
450 * @path: an #AdgPath
451 * @x: the new x coordinate
452 * @y: the new y coordinate
454 * Convenient function to call adg_path_move_to() using explicit
455 * coordinates instead of #AdgPair.
457 void
458 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
460 AdgPair p;
462 p.x = x;
463 p.y = y;
465 adg_path_append(path, CPML_MOVE, &p);
469 * adg_path_line_to:
470 * @path: an #AdgPath
471 * @pair: the destination coordinates
473 * Adds a line to @path from the current point to @pair. After this
474 * call the current point will be @pair.
476 * If @path has no current point before this call, this function will
477 * trigger a warning without other effect.
479 void
480 adg_path_line_to(AdgPath *path, const AdgPair *pair)
482 adg_path_append(path, CPML_LINE, pair);
486 * adg_path_line_to_explicit:
487 * @path: an #AdgPath
488 * @x: the new x coordinate
489 * @y: the new y coordinate
491 * Convenient function to call adg_path_line_to() using explicit
492 * coordinates instead of #AdgPair.
494 void
495 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
497 AdgPair p;
499 p.x = x;
500 p.y = y;
502 adg_path_append(path, CPML_LINE, &p);
506 * adg_path_arc_to:
507 * @path: an #AdgPath
508 * @throught: an arbitrary point on the arc
509 * @pair: the destination coordinates
511 * Adds an arc to the path from the current point to @pair, passing
512 * throught @throught. After this call the current point will be @pair.
514 * If @path has no current point before this call, this function will
515 * trigger a warning without other effect.
517 void
518 adg_path_arc_to(AdgPath *path, const AdgPair *throught, const AdgPair *pair)
520 adg_path_append(path, CPML_ARC, throught, pair);
524 * adg_path_arc_to_explicit:
525 * @path: an #AdgPath
526 * @x1: the x coordinate of an intermediate point
527 * @y1: the y coordinate of an intermediate point
528 * @x2: the x coordinate of the end of the arc
529 * @y2: the y coordinate of the end of the arc
531 * Convenient function to call adg_path_arc_to() using explicit
532 * coordinates instead of #AdgPair.
534 void
535 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
536 gdouble x2, gdouble y2)
538 AdgPair p[2];
540 p[0].x = x1;
541 p[0].y = y1;
542 p[1].x = x2;
543 p[1].y = y2;
545 adg_path_append(path, CPML_ARC, &p[0], &p[1]);
549 * adg_path_curve_to:
550 * @path: an #AdgPath
551 * @control1: the first control point of the curve
552 * @control2: the second control point of the curve
553 * @pair: the destination coordinates
555 * Adds a cubic Bézier curve to the path from the current point to
556 * position @pair, using @control1 and @control2 as control points.
557 * After this call the current point will be @pair.
559 * If @path has no current point before this call, this function will
560 * trigger a warning without other effect.
562 void
563 adg_path_curve_to(AdgPath *path, const AdgPair *control1,
564 const AdgPair *control2, const AdgPair *pair)
566 adg_path_append(path, CPML_CURVE, control1, control2, pair);
570 * adg_path_curve_to_explicit:
571 * @path: an #AdgPath
572 * @x1: the x coordinate of the first control point
573 * @y1: the y coordinate of the first control point
574 * @x2: the x coordinate of the second control point
575 * @y2: the y coordinate of the second control point
576 * @x3: the x coordinate of the end of the curve
577 * @y3: the y coordinate of the end of the curve
579 * Convenient function to call adg_path_curve_to() using explicit
580 * coordinates instead of #AdgPair.
582 void
583 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
584 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
586 AdgPair p[3];
588 p[0].x = x1;
589 p[0].y = y1;
590 p[1].x = x2;
591 p[1].y = y2;
592 p[2].x = x3;
593 p[2].y = y3;
595 adg_path_append(path, CPML_CURVE, &p[0], &p[1], &p[2]);
599 * adg_path_close:
600 * @path: an #AdgPath
602 * Adds a line segment to the path from the current point to the
603 * beginning of the current segment, (the most recent point passed
604 * to an adg_path_move_to()), and closes this segment.
605 * After this call the current point will be unset.
607 * The behavior of adg_path_close() is distinct from simply calling
608 * adg_line_to() with the coordinates of the segment starting point.
609 * When a closed segment is stroked, there are no caps on the ends.
610 * Instead, there is a line join connecting the final and initial
611 * primitive of the segment.
613 * If @path has no current point before this call, this function will
614 * trigger a warning without other effect.
616 void
617 adg_path_close(AdgPath *path)
619 adg_path_append(path, CPML_CLOSE);
623 * adg_path_arc:
624 * @path: an #AdgPath
625 * @center: coordinates of the center of the arc
626 * @r: the radius of the arc
627 * @start: the start angle, in radians
628 * @end: the end angle, in radians
630 * A more usual way to add an arc to @path. After this call, the current
631 * point will be the computed end point of the arc. The arc will be
632 * rendered in increasing angle, accordling to @start and @end. This means
633 * if @start is less than @end, the arc will be rendered in clockwise
634 * direction (accordling to the default cairo coordinate system) while if
635 * @start is greather than @end, the arc will be rendered in couterclockwise
636 * direction.
638 * By explicitely setting the whole arc data, the start point could be
639 * different from the current point. In this case, if @path has no
640 * current point before the call a #CPML_MOVE to the start point of
641 * the arc will be automatically prepended to the arc. If @path has a
642 * current point, a #CPML_LINE to the start point of the arc will be
643 * used instead of the "move to" primitive.
645 void
646 adg_path_arc(AdgPath *path, const AdgPair *center, gdouble r,
647 gdouble start, gdouble end)
649 AdgPathPrivate *data;
650 AdgPair p[3];
652 g_return_if_fail(ADG_IS_PATH(path));
653 g_return_if_fail(center != NULL);
655 data = path->data;
656 cpml_vector_from_angle(&p[0], start);
657 cpml_vector_from_angle(&p[1], (end-start) / 2);
658 cpml_vector_from_angle(&p[2], end);
660 cpml_vector_set_length(&p[0], r);
661 cpml_vector_set_length(&p[1], r);
662 cpml_vector_set_length(&p[2], r);
664 p[0].x += center->x;
665 p[0].y += center->y;
666 p[1].x += center->x;
667 p[1].y += center->y;
668 p[2].x += center->x;
669 p[2].y += center->y;
671 if (!data->cp_is_valid)
672 adg_path_append(path, CPML_MOVE, &p[0]);
673 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
674 adg_path_append(path, CPML_LINE, &p[0]);
676 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
680 * adg_path_arc_explicit:
681 * @path: an #AdgPath
682 * @xc: x position of the center of the arc
683 * @yc: y position of the center of the arc
684 * @r: the radius of the arc
685 * @start: the start angle, in radians
686 * @end: the end angle, in radians
688 * Convenient function to call adg_path_arc() using explicit
689 * coordinates instead of #AdgPair.
691 void
692 adg_path_arc_explicit(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
693 gdouble start, gdouble end)
695 AdgPair center;
697 center.x = xc;
698 center.y = yc;
700 adg_path_arc(path, &center, r, start, end);
704 * adg_path_chamfer
705 * @path: an #AdgPath
706 * @delta1: the distance from the intersection point of the current primitive
707 * @delta2: the distance from the intersection point of the next primitive
709 * A binary action that generates a chamfer between two primitives.
710 * The first primitive involved is the current primitive, the second will
711 * be the next primitive appended to @path after this call. The second
712 * primitive is required: if the chamfer operation is not properly
713 * terminated (by not providing the second primitive), any API accessing
714 * the path in reading mode will raise a warning.
716 * An exception is a chamfer after a #CPML_CLOSE primitive. In this case,
717 * the second primitive is not required: the current close path is used
718 * as first operand while the first primitive of the current segment is
719 * used as second operand.
721 * The chamfer operation requires two lengths: @delta1 specifies the
722 * "quantity" to trim on the first primitive while @delta2 is the same
723 * applied on the second primitive. The term "quantity" means the length
724 * of the portion to cut out from the original primitive (that is the
725 * primitive as would be without the chamfer).
727 void
728 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
730 g_return_if_fail(ADG_IS_PATH(path));
732 if (!append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
733 return;
737 * adg_path_fillet:
738 * @path: an #AdgPath
739 * @radius: the radius of the fillet
741 * A binary action that joins to primitives with an arc.
742 * The first primitive involved is the current primitive, the second will
743 * be the next primitive appended to @path after this call. The second
744 * primitive is required: if the fillet operation is not properly
745 * terminated (by not providing the second primitive), any API accessing
746 * the path in reading mode will raise a warning.
748 * An exception is a fillet after a #CPML_CLOSE primitive. In this case,
749 * the second primitive is not required: the current close path is used
750 * as first operand while the first primitive of the current segment is
751 * used as second operand.
753 void
754 adg_path_fillet(AdgPath *path, gdouble radius)
756 g_return_if_fail(ADG_IS_PATH(path));
758 if (!append_operation(path, ADG_ACTION_FILLET, radius))
759 return;
763 * adg_path_reflect:
764 * @path: an #AdgPath
765 * @vector: the slope of the axis
767 * Reflects the first segment or @path around the axis passing
768 * throught (0, 0) and with a @vector slope. The internal segment
769 * is duplicated and the proper transformation (computed from
770 * @vector) to mirror the segment is applied on all its points.
771 * The result is then reversed with cpml_segment_reverse() and
772 * appended to the original path with adg_path_append_segment().
774 * For convenience, if @vector is %NULL the path is reversed
775 * around the x axis (y=0).
777 void
778 adg_path_reflect(AdgPath *path, const CpmlVector *vector)
780 AdgModel *model;
781 AdgMatrix matrix;
782 AdgSegment segment, *dup_segment;
784 g_return_if_fail(ADG_IS_PATH(path));
785 g_return_if_fail(vector == NULL || vector->x != 0 || vector->y != 0);
787 model = (AdgModel *) path;
789 if (vector == NULL) {
790 cairo_matrix_init_scale(&matrix, 1, -1);
791 } else {
792 CpmlVector slope;
793 gdouble cos2angle, sin2angle;
795 cpml_pair_copy(&slope, vector);
796 cpml_vector_set_length(&slope, 1);
798 if (slope.x == 0 && slope.y == 0) {
799 g_warning(_("%s: the axis of the reflection is not known"),
800 G_STRLOC);
801 return;
804 sin2angle = 2. * vector->x * vector->y;
805 cos2angle = 2. * vector->x * vector->x - 1;
807 cairo_matrix_init(&matrix, cos2angle, sin2angle,
808 sin2angle, -cos2angle, 0, 0);
811 if (!adg_trail_put_segment((AdgTrail *) path, 1, &segment))
812 return;
814 /* No need to reverse an empty segment */
815 if (segment.num_data == 0 || segment.num_data == 0)
816 return;
818 dup_segment = adg_segment_deep_dup(&segment);
819 if (dup_segment == NULL)
820 return;
822 cpml_segment_reverse(dup_segment);
823 cpml_segment_transform(dup_segment, &matrix);
824 dup_segment->data[0].header.type = CPML_LINE;
826 adg_path_append_segment(path, dup_segment);
828 g_free(dup_segment);
830 dup_reverse_named_pairs(model, &matrix);
834 static void
835 clear(AdgModel *model)
837 AdgPath *path;
838 AdgPathPrivate *data;
840 path = (AdgPath *) model;
841 data = path->data;
843 g_array_set_size(data->cpml.array, 0);
844 clear_operation(path);
845 clear_parent(model);
848 static void
849 clear_parent(AdgModel *model)
851 if (PARENT_MODEL_CLASS->clear)
852 PARENT_MODEL_CLASS->clear(model);
855 static void
856 changed(AdgModel *model)
858 clear_parent(model);
860 if (PARENT_MODEL_CLASS->changed)
861 PARENT_MODEL_CLASS->changed(model);
864 static CpmlPath *
865 get_cpml_path(AdgTrail *trail)
867 clear_parent((AdgModel *) trail);
868 return read_cpml_path((AdgPath *) trail);
871 static CpmlPath *
872 read_cpml_path(AdgPath *path)
874 AdgPathPrivate *data = path->data;
876 /* Always regenerate the CpmlPath as it is a trivial operation */
877 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
878 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
879 data->cpml.path.num_data = (data->cpml.array)->len;
881 return &data->cpml.path;
884 static void
885 append_primitive(AdgPath *path, AdgPrimitive *current)
887 AdgPathPrivate *data;
888 cairo_path_data_t *path_data;
889 int length;
891 data = path->data;
892 path_data = current->data;
893 length = path_data[0].header.length;
895 /* Execute any pending operation */
896 do_operation(path, path_data);
898 /* Append the path data to the internal path array */
899 data->cpml.array = g_array_append_vals(data->cpml.array,
900 path_data, length);
902 /* Set path data to point to the recently appended cairo_path_data_t
903 * primitive: the first struct is the header */
904 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
905 (data->cpml.array)->len - length;
907 /* Store the over primitive */
908 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
910 /* Set the last primitive for subsequent binary operations */
911 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
912 data->last.segment = NULL;
913 data->last.data = path_data;
915 /* Save the last point as the current point, if applicable */
916 data->cp_is_valid = length > 1;
917 if (length > 1)
918 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
920 /* Invalidate cairo_path: should be recomputed */
921 clear_parent((AdgModel *) path);
924 static gint
925 needed_pairs(CpmlPrimitiveType type)
927 switch (type) {
928 case CPML_CLOSE:
929 return 1;
930 case CPML_MOVE:
931 return 2;
932 case CPML_LINE:
933 return 2;
934 case CPML_ARC:
935 return 3;
936 case CPML_CURVE:
937 return 4;
938 default:
939 g_return_val_if_reached(0);
942 return 0;
945 static void
946 clear_operation(AdgPath *path)
948 AdgPathPrivate *data;
949 AdgOperation *operation;
951 data = path->data;
952 operation = &data->operation;
954 if (operation->action != ADG_ACTION_NONE) {
955 g_warning(_("%s: a `%s' operation is still active while clearing the path"),
956 G_STRLOC, action_name(operation->action));
957 operation->action = ADG_ACTION_NONE;
961 static gboolean
962 append_operation(AdgPath *path, AdgAction action, ...)
964 AdgPathPrivate *data;
965 AdgOperation *operation;
966 va_list var_args;
968 data = path->data;
970 if (data->last.data == NULL) {
971 g_warning(_("%s: requested a `%s' operation on a path without current primitive"),
972 G_STRLOC, action_name(action));
973 return FALSE;
976 operation = &data->operation;
977 if (operation->action != ADG_ACTION_NONE) {
978 g_warning(_("%s: requested a `%s' operation while a `%s' operation is active"),
979 G_STRLOC, action_name(action), action_name(operation->action));
980 /* TODO: this is a rude simplification, as a lot of actions
981 * could be chained up. As an example, a fillet followed by
982 * a polar chamfer is quite common.
984 return FALSE;
987 va_start(var_args, action);
989 switch (action) {
991 case ADG_ACTION_CHAMFER:
992 operation->data.chamfer.delta1 = va_arg(var_args, double);
993 operation->data.chamfer.delta2 = va_arg(var_args, double);
994 break;
996 case ADG_ACTION_FILLET:
997 operation->data.fillet.radius = va_arg(var_args, double);
998 break;
1000 case ADG_ACTION_NONE:
1001 va_end(var_args);
1002 return TRUE;
1004 default:
1005 g_warning(_("%s: `%d' operation not recognized"), G_STRLOC, action);
1006 va_end(var_args);
1007 return FALSE;
1010 operation->action = action;
1011 va_end(var_args);
1013 if (data->last.data[0].header.type == CPML_CLOSE) {
1014 /* Special case: an action with the close primitive should
1015 * be resolved now by changing the close primitive to a
1016 * line-to and using it as second operand and use the first
1017 * primitive of the current segment as first operand */
1018 guint length;
1019 cairo_path_data_t *path_data;
1020 CpmlSegment segment;
1021 CpmlPrimitive current;
1023 length = data->cpml.array->len;
1025 /* Ensure the close path primitive is not the only data */
1026 g_return_val_if_fail(length > 1, FALSE);
1028 /* Allocate one more item once for all to accept the
1029 * conversion from a close to line-to primitive */
1030 data->cpml.array = g_array_set_size(data->cpml.array, length + 1);
1031 path_data = (cairo_path_data_t *) data->cpml.array->data;
1032 --data->cpml.array->len;
1034 /* Set segment and current (the first primitive of segment) */
1035 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1036 while (cpml_segment_next(&segment))
1038 cpml_primitive_from_segment(&current, &segment);
1040 /* Convert close path to a line-to primitive */
1041 ++data->cpml.array->len;
1042 path_data[length - 1].header.type = CPML_LINE;
1043 path_data[length - 1].header.length = 2;
1044 path_data[length] = *current.org;
1046 data->last.segment = &segment;
1047 data->last.org = &path_data[length - 2];
1048 data->last.data = &path_data[length - 1];
1050 do_action(path, action, &current);
1054 return TRUE;
1057 static void
1058 do_operation(AdgPath *path, cairo_path_data_t *path_data)
1060 AdgPathPrivate *data;
1061 AdgAction action;
1062 AdgSegment segment;
1063 AdgPrimitive current;
1064 cairo_path_data_t current_org;
1066 data = path->data;
1067 action = data->operation.action;
1068 cpml_segment_from_cairo(&segment, read_cpml_path(path));
1070 /* Construct the current primitive, that is the primitive to be
1071 * mixed with the last primitive with the specified operation.
1072 * Its org is a copy of the end point of the last primitive: it can be
1073 * modified without affecting anything else. It is expected the operation
1074 * functions will add to @path the primitives required but NOT to add
1075 * @current, as this one will be inserted automatically. */
1076 current.segment = &segment;
1077 current.org = &current_org;
1078 current.data = path_data;
1079 cpml_pair_to_cairo(&data->cp, &current_org);
1081 do_action(path, action, &current);
1084 static void
1085 do_action(AdgPath *path, AdgAction action, AdgPrimitive *primitive)
1087 switch (action) {
1088 case ADG_ACTION_NONE:
1089 return;
1090 case ADG_ACTION_CHAMFER:
1091 do_chamfer(path, primitive);
1092 break;
1093 case ADG_ACTION_FILLET:
1094 do_fillet(path, primitive);
1095 break;
1096 default:
1097 g_return_if_reached();
1101 static void
1102 do_chamfer(AdgPath *path, AdgPrimitive *current)
1104 AdgPathPrivate *data;
1105 AdgPrimitive *last;
1106 gdouble delta1, delta2;
1107 gdouble len1, len2;
1108 AdgPair pair;
1110 data = path->data;
1111 last = &data->last;
1112 delta1 = data->operation.data.chamfer.delta1;
1113 len1 = cpml_primitive_get_length(last);
1115 if (delta1 >= len1) {
1116 g_warning(_("%s: first chamfer delta of `%lf' is greather than the available `%lf' length"),
1117 G_STRLOC, delta1, len1);
1118 return;
1121 delta2 = data->operation.data.chamfer.delta2;
1122 len2 = cpml_primitive_get_length(current);
1124 if (delta2 >= len2) {
1125 g_warning(_("%s: second chamfer delta of `%lf' is greather than the available `%lf' length"),
1126 G_STRLOC, delta1, len1);
1127 return;
1130 /* Change the end point of the last primitive */
1131 cpml_primitive_put_pair_at(last, 1. - delta1 / len1, &pair);
1132 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
1134 /* Change the start point of the current primitive */
1135 cpml_primitive_put_pair_at(current, delta2 / len2, &pair);
1136 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
1138 /* Add the chamfer line */
1139 data->operation.action = ADG_ACTION_NONE;
1140 adg_path_append(path, CPML_LINE, &pair);
1143 static void
1144 do_fillet(AdgPath *path, AdgPrimitive *current)
1146 AdgPathPrivate *data;
1147 AdgPrimitive *last, *current_dup, *last_dup;
1148 gdouble radius, offset, pos;
1149 AdgPair center, vector, p[3];
1151 data = path->data;
1152 last = &data->last;
1153 current_dup = adg_primitive_deep_dup(current);
1154 last_dup = adg_primitive_deep_dup(last);
1155 radius = data->operation.data.fillet.radius;
1156 offset = is_convex(last_dup, current_dup) ? -radius : radius;
1158 /* Find the center of the fillet from the intersection between
1159 * the last and current primitives offseted by radius */
1160 cpml_primitive_offset(current_dup, offset);
1161 cpml_primitive_offset(last_dup, offset);
1162 if (cpml_primitive_put_intersections(current_dup, last_dup, 1, &center) == 0) {
1163 g_warning(_("%s: fillet with radius of `%lf' is not applicable here"),
1164 G_STRLOC, radius);
1165 g_free(current_dup);
1166 g_free(last_dup);
1167 return;
1170 /* Compute the start point of the fillet */
1171 pos = cpml_primitive_get_closest_pos(last_dup, &center);
1172 cpml_primitive_put_vector_at(last_dup, pos, &vector);
1173 cpml_vector_set_length(&vector, offset);
1174 cpml_vector_normal(&vector);
1175 p[0].x = center.x - vector.x;
1176 p[0].y = center.y - vector.y;
1178 /* Compute the mid point of the fillet */
1179 cpml_pair_from_cairo(&vector, current->org);
1180 vector.x -= center.x;
1181 vector.y -= center.y;
1182 cpml_vector_set_length(&vector, radius);
1183 p[1].x = center.x + vector.x;
1184 p[1].y = center.y + vector.y;
1186 /* Compute the end point of the fillet */
1187 pos = cpml_primitive_get_closest_pos(current_dup, &center);
1188 cpml_primitive_put_vector_at(current_dup, pos, &vector);
1189 cpml_vector_set_length(&vector, offset);
1190 cpml_vector_normal(&vector);
1191 p[2].x = center.x - vector.x;
1192 p[2].y = center.y - vector.y;
1194 g_free(current_dup);
1195 g_free(last_dup);
1197 /* Change the end point of the last primitive */
1198 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
1200 /* Change the start point of the current primitive */
1201 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
1203 /* Add the fillet arc */
1204 data->operation.action = ADG_ACTION_NONE;
1205 adg_path_append(path, CPML_ARC, &p[1], &p[2]);
1208 static gboolean
1209 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1211 CpmlVector v1, v2;
1212 gdouble angle1, angle2;
1214 cpml_primitive_put_vector_at(primitive1, -1, &v1);
1215 cpml_primitive_put_vector_at(primitive2, 0, &v2);
1217 /* Probably there is a smarter way to get this without trygonometry */
1218 angle1 = cpml_vector_angle(&v1);
1219 angle2 = cpml_vector_angle(&v2);
1221 if (angle1 > angle2)
1222 angle1 -= M_PI*2;
1224 return angle2-angle1 > M_PI;
1227 static const gchar *
1228 action_name(AdgAction action)
1230 switch (action) {
1231 case ADG_ACTION_NONE:
1232 return "NULL";
1233 case ADG_ACTION_CHAMFER:
1234 return "CHAMFER";
1235 case ADG_ACTION_FILLET:
1236 return "FILLET";
1239 return "undefined";
1242 static void
1243 get_named_pair(const gchar *name, AdgPair *pair, gpointer user_data)
1245 GSList **named_pairs;
1246 AdgNamedPair *named_pair;
1248 named_pairs = user_data;
1250 named_pair = g_new(AdgNamedPair, 1);
1251 named_pair->name = name;
1252 named_pair->pair = *pair;
1254 *named_pairs = g_slist_prepend(*named_pairs, named_pair);
1257 static void
1258 dup_reverse_named_pairs(AdgModel *model, const AdgMatrix *matrix)
1260 AdgNamedPair *old_named_pair;
1261 AdgNamedPair named_pair;
1262 GSList *named_pairs;
1264 /* Populate named_pairs with all the named pairs of model */
1265 named_pairs = NULL;
1266 adg_model_foreach_named_pair(model, get_named_pair, &named_pairs);
1268 /* Readd the pairs applying the reversing transformation matrix to
1269 * their coordinates and prepending a "-" to their name */
1270 while (named_pairs) {
1271 old_named_pair = named_pairs->data;
1273 named_pair.name = g_strdup_printf("-%s", old_named_pair->name);
1274 named_pair.pair = old_named_pair->pair;
1275 cpml_pair_transform(&named_pair.pair, matrix);
1277 adg_model_set_named_pair(model, named_pair.name, &named_pair.pair);
1279 g_free((gpointer) named_pair.name);
1280 named_pairs = g_slist_delete_link(named_pairs, named_pairs);