[AdgPath] Added _explicit() API version
[adg.git] / adg / adg-path.c
blob3b76c7012c296f259249d324679f6c34f199263d
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 %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.
50 **/
52 /**
53 * AdgPath:
55 * All fields are private and should not be used directly.
56 * Use its public methods instead.
57 **/
60 #include "adg-path.h"
61 #include "adg-path-private.h"
62 #include "adg-primitive.h"
63 #include "adg-intl.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 gboolean cp_is_valid);
80 static void clear_operation (AdgPath *path);
81 static gboolean append_operation (AdgPath *path,
82 AdgAction action,
83 ...);
84 static void do_operation (AdgPath *path,
85 cairo_path_data_t
86 *path_data);
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
92 *primitive1,
93 const AdgPrimitive
94 *primitive2);
97 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
100 static void
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;
121 static void
122 adg_path_init(AdgPath *path)
124 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
125 AdgPathPrivate);
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;
131 path->data = data;
134 static void
135 finalize(GObject *object)
137 AdgPath *path;
138 AdgPathPrivate *data;
140 path = (AdgPath *) object;
141 data = path->data;
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);
152 * adg_path_new:
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
159 AdgPath *
160 adg_path_new(void)
162 return g_object_new(ADG_TYPE_PATH, NULL);
166 * adg_path_current_point:
167 * @path: an #AdgPath
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
182 const AdgPair *
183 adg_path_current_point(AdgPath *path)
185 AdgPathPrivate *data;
187 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
189 data = path->data;
191 if (!data->cp_is_valid)
192 return NULL;
194 return &data->cp;
198 * adg_path_has_current_point:
199 * @path: an #AdgPath
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
206 gboolean
207 adg_path_has_current_point(AdgPath *path)
209 AdgPathPrivate *data;
211 g_return_val_if_fail(ADG_IS_PATH(path), FALSE);
213 data = path->data;
215 return data->cp_is_valid;
219 * adg_path_append:
220 * @path: an #AdgPath
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
227 * primitive.
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.
234 void
235 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
237 va_list var_args;
239 va_start(var_args, type);
240 adg_path_append_valist(path, type, var_args);
241 va_end(var_args);
245 * adg_path_append_valist:
246 * @path: an #AdgPath
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().
252 void
253 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
255 AdgPathPrivate *data;
256 AdgPrimitive primitive;
257 gint length, cnt;
258 cairo_path_data_t org;
259 cairo_path_data_t *path_data;
261 g_return_if_fail(ADG_IS_PATH(path));
263 data = path->data;
264 length = needed_pairs(type, data->cp_is_valid);
265 if (length == 0)
266 return;
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) {
279 ++ path_data;
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:
294 * @path: an #AdgPath
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.
303 void
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);
312 data = path->data;
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:
318 * work on a copy */
319 primitive_dup = adg_primitive_deep_dup(primitive);
321 append_primitive(path, primitive_dup);
323 g_free(primitive_dup);
327 * adg_path_append_segment:
328 * @path: an #AdgPath
329 * @segment: the #AdgSegment to append
331 * Appends @segment to @path.
333 void
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);
341 data = path->data;
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:
350 * @path: an #AdgPath
351 * @cairo_path: the #cairo_path_t path to append
353 * Appends a whole cairo path to @path.
355 void
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));
362 data = path->data;
364 clear_parent((AdgModel *) path);
365 data->cpml.array = g_array_append_vals(data->cpml.array,
366 cairo_path->data,
367 cairo_path->num_data);
371 * adg_path_move_to:
372 * @path: an #AdgPath
373 * @pair: the destination coordinates
375 * Begins a new segment. After this call the current point will be @pair.
377 void
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:
385 * @path: an #AdgPath
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.
392 void
393 adg_path_move_to_explicit(AdgPath *path, gdouble x, gdouble y)
395 AdgPair p;
397 p.x = x;
398 p.y = y;
400 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
404 * adg_path_line_to:
405 * @path: an #AdgPath
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.
414 void
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:
422 * @path: an #AdgPath
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.
429 void
430 adg_path_line_to_explicit(AdgPath *path, gdouble x, gdouble y)
432 AdgPair p;
434 p.x = x;
435 p.y = y;
437 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
441 * adg_path_arc_to:
442 * @path: an #AdgPath
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.
452 void
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:
460 * @path: an #AdgPath
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.
469 void
470 adg_path_arc_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
471 gdouble x2, gdouble y2)
473 AdgPair p[2];
475 p[0].x = x1;
476 p[0].y = y1;
477 p[1].x = x2;
478 p[1].y = y2;
480 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
484 * adg_path_curve_to:
485 * @path: an #AdgPath
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.
497 void
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:
506 * @path: an #AdgPath
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.
517 void
518 adg_path_curve_to_explicit(AdgPath *path, gdouble x1, gdouble y1,
519 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
521 AdgPair p[3];
523 p[0].x = x1;
524 p[0].y = y1;
525 p[1].x = x2;
526 p[1].y = y2;
527 p[2].x = x3;
528 p[2].y = y3;
530 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
534 * adg_path_close:
535 * @path: an #AdgPath
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.
551 void
552 adg_path_close(AdgPath *path)
554 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
558 * adg_path_arc
559 * @path: an #AdgPath
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
572 * direction.
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.
581 void
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));
590 data = path->data;
591 center.x = xc;
592 center.y = yc;
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], &center);
603 cpml_pair_add(&p[1], &center);
604 cpml_pair_add(&p[2], &center);
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]);
615 * adg_path_chamfer
616 * @path: an #AdgPath
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).
633 void
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))
639 return;
643 * adg_path_fillet:
644 * @path: an #AdgPath
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.
654 void
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))
660 return;
664 * adg_path_last_primitive:
665 * @path: an #AdgPath
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
672 const AdgPrimitive *
673 adg_path_last_primitive(AdgPath *path)
675 AdgPathPrivate *data;
677 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
679 data = path->data;
681 return &data->last;
685 * adg_path_over_primitive:
686 * @path: an #AdgPath
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
695 * or %NULL on errors
697 const AdgPrimitive *
698 adg_path_over_primitive(AdgPath *path)
700 AdgPathPrivate *data;
702 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
704 data = path->data;
706 return &data->over;
710 static void
711 clear(AdgModel *model)
713 AdgPath *path;
714 AdgPathPrivate *data;
716 path = (AdgPath *) model;
717 data = path->data;
719 g_array_set_size(data->cpml.array, 0);
720 clear_operation(path);
721 clear_parent(model);
724 static void
725 clear_parent(AdgModel *model)
727 if (PARENT_MODEL_CLASS->clear != NULL)
728 PARENT_MODEL_CLASS->clear(model);
731 static void
732 changed(AdgModel *model)
734 clear_parent(model);
736 if (PARENT_MODEL_CLASS->changed != NULL)
737 PARENT_MODEL_CLASS->changed(model);
740 static CpmlPath *
741 get_cpml_path(AdgTrail *trail)
743 clear_parent((AdgModel *) trail);
744 return read_cpml_path((AdgPath *) trail);
747 static CpmlPath *
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;
760 static void
761 append_primitive(AdgPath *path, AdgPrimitive *current)
763 AdgPathPrivate *data;
764 cairo_path_data_t *path_data;
765 int length;
767 data = 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,
776 path_data, length);
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;
793 if (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);
800 static gint
801 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
803 switch (type) {
805 case CAIRO_PATH_CLOSE_PATH:
806 g_return_val_if_fail(cp_is_valid, 0);
807 return 1;
809 case CAIRO_PATH_MOVE_TO:
810 return 2;
812 case CAIRO_PATH_LINE_TO:
813 g_return_val_if_fail(cp_is_valid, 0);
814 return 2;
816 case CAIRO_PATH_ARC_TO:
817 g_return_val_if_fail(cp_is_valid, 0);
818 return 3;
820 case CAIRO_PATH_CURVE_TO:
821 g_return_val_if_fail(cp_is_valid, 0);
822 return 4;
824 default:
825 g_return_val_if_reached(0);
828 return 0;
831 static void
832 clear_operation(AdgPath *path)
834 AdgPathPrivate *data;
835 AdgOperation *operation;
837 data = path->data;
838 operation = &data->operation;
840 if (operation->action == ADG_ACTION_NONE)
841 return;
843 g_warning("An operation is still active while clearing the path "
844 "(action `%d')", operation->action);
845 operation->action = ADG_ACTION_NONE;
848 static gboolean
849 append_operation(AdgPath *path, AdgAction action, ...)
851 AdgPathPrivate *data;
852 AdgOperation *operation;
853 va_list var_args;
855 data = path->data;
857 if (!data->cp_is_valid) {
858 g_warning("Operation requested but path has no current primitive "
859 "(action `%d')", action);
860 return FALSE;
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.");
871 return FALSE;
874 va_start(var_args, action);
876 switch (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);
881 break;
883 case ADG_ACTION_FILLET:
884 operation->data.fillet.radius = va_arg(var_args, double);
885 break;
887 case ADG_ACTION_NONE:
888 va_end(var_args);
889 return TRUE;
891 default:
892 g_warning("Operation not recognized (action `%d')", action);
893 va_end(var_args);
894 return FALSE;
897 operation->action = action;
898 va_end(var_args);
900 return TRUE;
903 static void
904 do_operation(AdgPath *path, cairo_path_data_t *path_data)
906 AdgPathPrivate *data;
907 AdgAction action;
908 AdgSegment segment;
909 AdgPrimitive current;
910 cairo_path_data_t current_org;
912 data = path->data;
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 = &current_org;
923 current.data = path_data;
924 cpml_pair_to_cairo(&data->cp, &current_org);
926 switch (action) {
928 case ADG_ACTION_NONE:
929 return;
931 case ADG_ACTION_CHAMFER:
932 do_chamfer(path, &current);
933 break;
935 case ADG_ACTION_FILLET:
936 do_fillet(path, &current);
937 break;
939 default:
940 g_warning("Operation not implemented (action `%d')", action);
941 return;
945 static void
946 do_chamfer(AdgPath *path, AdgPrimitive *current)
948 AdgPathPrivate *data;
949 AdgPrimitive *last;
950 gdouble delta1, delta2;
951 gdouble len1, len2;
952 AdgPair pair;
954 data = path->data;
955 last = &data->last;
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)",
961 delta1, len1);
962 return;
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)",
970 delta2, len2);
971 return;
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);
987 static void
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];
995 data = path->data;
996 last = &data->last;
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,
1012 &center, 1) == 0) {
1013 g_warning("Fillet not applicable (radius = %lf)", radius);
1014 g_free(current_dup);
1015 g_free(last_dup);
1016 return;
1019 /* Compute the start point of the fillet */
1020 pos = cpml_primitive_near_pos(last_dup, &center);
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], &center), &vector);
1026 /* Compute the mid point of the fillet */
1027 cpml_pair_from_cairo(&vector, current->org);
1028 cpml_pair_sub(&vector, &center);
1029 cpml_vector_set_length(&vector, radius);
1030 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
1032 /* Compute the end point of the fillet */
1033 pos = cpml_primitive_near_pos(current_dup, &center);
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], &center), &vector);
1039 g_free(current_dup);
1040 g_free(last_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]);
1053 static gboolean
1054 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
1056 CpmlVector v1, v2;
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)
1067 angle1 -= M_PI*2;
1069 return angle2-angle1 > M_PI;