[demo] Updated adg-demo to use named pairs where possible
[adg.git] / adg / adg-path.c
blob67ca3e1e5c49e293c66b7a8f8bb5cf267e640584
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 * @x: the new x coordinate
374 * @y: the new y coordinate
376 * Begins a new segment. After this call the current point will be (@x, @y).
378 void
379 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
381 AdgPair p;
383 p.x = x;
384 p.y = y;
386 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
390 * adg_path_line_to:
391 * @path: an #AdgPath
392 * @x: the new x coordinate
393 * @y: the new y coordinate
395 * Adds a line to @path from the current point to position (@x, @y).
396 * After this call the current point will be (@x, @y).
398 * If @path has no current point before this call, this function will
399 * trigger a warning without other effect.
401 void
402 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
404 AdgPair p;
406 p.x = x;
407 p.y = y;
409 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
413 * adg_path_arc_to:
414 * @path: an #AdgPath
415 * @x1: the x coordinate of an intermediate point
416 * @y1: the y coordinate of an intermediate point
417 * @x2: the x coordinate of the end of the arc
418 * @y2: the y coordinate of the end of the arc
420 * Adds an arc to the path from the current point to (@x2, @y2),
421 * passing throught (@x1, @y1). After this call the current point
422 * will be (@x2, @y2).
424 * If @path has no current point before this call, this function will
425 * trigger a warning without other effect.
427 void
428 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
430 AdgPair p[2];
432 p[0].x = x1;
433 p[0].y = y1;
434 p[1].x = x2;
435 p[1].y = y2;
437 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
441 * adg_path_curve_to:
442 * @path: an #AdgPath
443 * @x1: the x coordinate of the first control point
444 * @y1: the y coordinate of the first control point
445 * @x2: the x coordinate of the second control point
446 * @y2: the y coordinate of the second control point
447 * @x3: the x coordinate of the end of the curve
448 * @y3: the y coordinate of the end of the curve
450 * Adds a cubic Bézier curve to the path from the current point to
451 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
452 * control points. After this call the current point will be (@x3, @y3).
454 * If @path has no current point before this call, this function will
455 * trigger a warning without other effect.
457 void
458 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
459 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
461 AdgPair p[3];
463 p[0].x = x1;
464 p[0].y = y1;
465 p[1].x = x2;
466 p[1].y = y2;
467 p[2].x = x3;
468 p[2].y = y3;
470 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
474 * adg_path_close:
475 * @path: an #AdgPath
477 * Adds a line segment to the path from the current point to the
478 * beginning of the current segment, (the most recent point passed
479 * to an adg_path_move_to()), and closes this segment.
480 * After this call the current point will be unset.
482 * The behavior of adg_path_close() is distinct from simply calling
483 * adg_line_to() with the coordinates of the segment starting point.
484 * When a closed segment is stroked, there are no caps on the ends.
485 * Instead, there is a line join connecting the final and initial
486 * primitive of the segment.
488 * If @path has no current point before this call, this function will
489 * trigger a warning without other effect.
491 void
492 adg_path_close(AdgPath *path)
494 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
498 * adg_path_arc
499 * @path: an #AdgPath
500 * @xc: x position of the center of the arc
501 * @yc: y position of the center of the arc
502 * @r: the radius of the arc
503 * @start: the start angle, in radians
504 * @end: the end angle, in radians
506 * A more usual way to add an arc to @path. After this call, the current
507 * point will be the computed end point of the arc. The arc will be
508 * rendered in increasing angle, accordling to @start and @end. This means
509 * if @start is less than @end, the arc will be rendered in clockwise
510 * direction (accordling to the default cairo coordinate system) while if
511 * @start is greather than @end, the arc will be rendered in couterclockwise
512 * direction.
514 * By explicitely setting the whole arc data, the start point could be
515 * different from the current point. In this case, if @path has no
516 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
517 * point of the arc will be automatically prepended to the arc.
518 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
519 * point of the arc will be used instead of the moveto.
521 void
522 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
523 gdouble start, gdouble end)
525 AdgPathPrivate *data;
526 AdgPair center, p[3];
528 g_return_if_fail(ADG_IS_PATH(path));
530 data = path->data;
531 center.x = xc;
532 center.y = yc;
534 cpml_vector_from_angle(&p[0], start);
535 cpml_vector_from_angle(&p[1], (end-start) / 2);
536 cpml_vector_from_angle(&p[2], end);
538 cpml_vector_set_length(&p[0], r);
539 cpml_vector_set_length(&p[1], r);
540 cpml_vector_set_length(&p[2], r);
542 cpml_pair_add(&p[0], &center);
543 cpml_pair_add(&p[1], &center);
544 cpml_pair_add(&p[2], &center);
546 if (!data->cp_is_valid)
547 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
548 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
549 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
551 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
555 * adg_path_chamfer
556 * @path: an #AdgPath
557 * @delta1: the distance from the intersection point of the current primitive
558 * @delta2: the distance from the intersection point of the next primitive
560 * A binary action that generates a chamfer between two primitives.
561 * The first primitive involved is the current primitive, the second will
562 * be the next primitive appended to @path after this call. The second
563 * primitive is required: if the chamfer operation is not properly
564 * terminated (by not providing the second primitive), any API accessing
565 * the path in reading mode will raise a warning.
567 * The chamfer operation requires two lengths: @delta1 specifies the
568 * "quantity" to trim on the first primitive while @delta2 is the same
569 * applied on the second primitive. The term "quantity" means the length
570 * of the portion to cut out from the original primitive (that is the
571 * primitive as would be without the chamfer).
573 void
574 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
576 g_return_if_fail(ADG_IS_PATH(path));
578 if (!append_operation(path, ADG_ACTION_CHAMFER, delta1, delta2))
579 return;
583 * adg_path_fillet:
584 * @path: an #AdgPath
585 * @radius: the radius of the fillet
587 * A binary action that joins to primitives with an arc.
588 * The first primitive involved is the current primitive, the second will
589 * be the next primitive appended to @path after this call. The second
590 * primitive is required: if the fillet operation is not properly
591 * terminated (by not providing the second primitive), any API accessing
592 * the path in reading mode will raise a warning.
594 void
595 adg_path_fillet(AdgPath *path, gdouble radius)
597 g_return_if_fail(ADG_IS_PATH(path));
599 if (!append_operation(path, ADG_ACTION_FILLET, radius))
600 return;
604 * adg_path_last_primitive:
605 * @path: an #AdgPath
607 * Gets the last primitive appended to @path. The returned struct
608 * is owned by @path and should not be freed or modified.
610 * Returns: a pointer to the last appended primitive or %NULL on errors
612 const AdgPrimitive *
613 adg_path_last_primitive(AdgPath *path)
615 AdgPathPrivate *data;
617 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
619 data = path->data;
621 return &data->last;
625 * adg_path_over_primitive:
626 * @path: an #AdgPath
628 * Gets the primitive before the last one appended to @path. The
629 * "over" term comes from forth, where the %OVER operator works
630 * on the stack in the same way as adg_path_over_primitive() works
631 * on @path. The returned struct is owned by @path and should not
632 * be freed or modified.
634 * Returns: a pointer to the primitive before the last appended one
635 * or %NULL on errors
637 const AdgPrimitive *
638 adg_path_over_primitive(AdgPath *path)
640 AdgPathPrivate *data;
642 g_return_val_if_fail(ADG_IS_PATH(path), NULL);
644 data = path->data;
646 return &data->over;
650 static void
651 clear(AdgModel *model)
653 AdgPath *path;
654 AdgPathPrivate *data;
656 path = (AdgPath *) model;
657 data = path->data;
659 g_array_set_size(data->cpml.array, 0);
660 clear_operation(path);
661 clear_parent(model);
664 static void
665 clear_parent(AdgModel *model)
667 if (PARENT_MODEL_CLASS->clear != NULL)
668 PARENT_MODEL_CLASS->clear(model);
671 static void
672 changed(AdgModel *model)
674 clear_parent(model);
676 if (PARENT_MODEL_CLASS->changed != NULL)
677 PARENT_MODEL_CLASS->changed(model);
680 static CpmlPath *
681 get_cpml_path(AdgTrail *trail)
683 clear_parent((AdgModel *) trail);
684 return read_cpml_path((AdgPath *) trail);
687 static CpmlPath *
688 read_cpml_path(AdgPath *path)
690 AdgPathPrivate *data = path->data;
692 /* Always regenerate the CpmlPath as it is a trivial operation */
693 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
694 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
695 data->cpml.path.num_data = (data->cpml.array)->len;
697 return &data->cpml.path;
700 static void
701 append_primitive(AdgPath *path, AdgPrimitive *current)
703 AdgPathPrivate *data;
704 cairo_path_data_t *path_data;
705 int length;
707 data = path->data;
708 path_data = current->data;
709 length = path_data[0].header.length;
711 /* Execute any pending operation */
712 do_operation(path, path_data);
714 /* Append the path data to the internal path array */
715 data->cpml.array = g_array_append_vals(data->cpml.array,
716 path_data, length);
718 /* Set path data to point to the recently appended cairo_path_data_t
719 * primitive: the first struct is the header */
720 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
721 (data->cpml.array)->len - length;
723 /* Store the over primitive */
724 memcpy(&data->over, &data->last, sizeof(AdgPrimitive));
726 /* Set the last primitive for subsequent binary operations */
727 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
728 data->last.segment = NULL;
729 data->last.data = path_data;
731 /* Save the last point as the current point, if applicable */
732 data->cp_is_valid = length > 1;
733 if (length > 1)
734 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
736 /* Invalidate cairo_path: should be recomputed */
737 clear_parent((AdgModel *) path);
740 static gint
741 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
743 switch (type) {
745 case CAIRO_PATH_CLOSE_PATH:
746 g_return_val_if_fail(cp_is_valid, 0);
747 return 1;
749 case CAIRO_PATH_MOVE_TO:
750 return 2;
752 case CAIRO_PATH_LINE_TO:
753 g_return_val_if_fail(cp_is_valid, 0);
754 return 2;
756 case CAIRO_PATH_ARC_TO:
757 g_return_val_if_fail(cp_is_valid, 0);
758 return 3;
760 case CAIRO_PATH_CURVE_TO:
761 g_return_val_if_fail(cp_is_valid, 0);
762 return 4;
764 default:
765 g_return_val_if_reached(0);
768 return 0;
771 static void
772 clear_operation(AdgPath *path)
774 AdgPathPrivate *data;
775 AdgOperation *operation;
777 data = path->data;
778 operation = &data->operation;
780 if (operation->action == ADG_ACTION_NONE)
781 return;
783 g_warning("An operation is still active while clearing the path "
784 "(action `%d')", operation->action);
785 operation->action = ADG_ACTION_NONE;
788 static gboolean
789 append_operation(AdgPath *path, AdgAction action, ...)
791 AdgPathPrivate *data;
792 AdgOperation *operation;
793 va_list var_args;
795 data = path->data;
797 if (!data->cp_is_valid) {
798 g_warning("Operation requested but path has no current primitive "
799 "(action `%d')", action);
800 return FALSE;
803 operation = &data->operation;
804 if (operation->action != ADG_ACTION_NONE) {
805 g_warning("Operation requested but another operation is yet active"
806 "(operators: new `%d', old `%d')",
807 action, operation->action);
808 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
809 "operators can and may cohexist. As an example, a "
810 "fillet followed by a polar chamfer should be done.");
811 return FALSE;
814 va_start(var_args, action);
816 switch (action) {
818 case ADG_ACTION_CHAMFER:
819 operation->data.chamfer.delta1 = va_arg(var_args, double);
820 operation->data.chamfer.delta2 = va_arg(var_args, double);
821 break;
823 case ADG_ACTION_FILLET:
824 operation->data.fillet.radius = va_arg(var_args, double);
825 break;
827 case ADG_ACTION_NONE:
828 va_end(var_args);
829 return TRUE;
831 default:
832 g_warning("Operation not recognized (action `%d')", action);
833 va_end(var_args);
834 return FALSE;
837 operation->action = action;
838 va_end(var_args);
840 return TRUE;
843 static void
844 do_operation(AdgPath *path, cairo_path_data_t *path_data)
846 AdgPathPrivate *data;
847 AdgAction action;
848 AdgSegment segment;
849 AdgPrimitive current;
850 cairo_path_data_t current_org;
852 data = path->data;
853 action = data->operation.action;
854 cpml_segment_from_cairo(&segment, read_cpml_path(path));
856 /* Construct the current primitive, that is the primitive to be inserted.
857 * Its org is a copy of the end point of the last primitive: it can be
858 * modified without affecting anything else. It is expected the operation
859 * functions will add to @path the primitives required but NOT to add
860 * @current, as this one will be inserted automatically. */
861 current.segment = &segment;
862 current.org = &current_org;
863 current.data = path_data;
864 cpml_pair_to_cairo(&data->cp, &current_org);
866 switch (action) {
868 case ADG_ACTION_NONE:
869 return;
871 case ADG_ACTION_CHAMFER:
872 do_chamfer(path, &current);
873 break;
875 case ADG_ACTION_FILLET:
876 do_fillet(path, &current);
877 break;
879 default:
880 g_warning("Operation not implemented (action `%d')", action);
881 return;
885 static void
886 do_chamfer(AdgPath *path, AdgPrimitive *current)
888 AdgPathPrivate *data;
889 AdgPrimitive *last;
890 gdouble delta1, delta2;
891 gdouble len1, len2;
892 AdgPair pair;
894 data = path->data;
895 last = &data->last;
896 delta1 = data->operation.data.chamfer.delta1;
897 len1 = cpml_primitive_length(last);
899 if (delta1 >= len1) {
900 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
901 delta1, len1);
902 return;
905 delta2 = data->operation.data.chamfer.delta2;
906 len2 = cpml_primitive_length(current);
908 if (delta2 >= len2) {
909 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
910 delta2, len2);
911 return;
914 /* Change the end point of the last primitive */
915 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
916 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
918 /* Change the start point of the current primitive */
919 cpml_primitive_pair_at(current, &pair, delta2 / len2);
920 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
922 /* Add the chamfer line */
923 data->operation.action = ADG_ACTION_NONE;
924 adg_path_append(path, CAIRO_PATH_LINE_TO, &pair);
927 static void
928 do_fillet(AdgPath *path, AdgPrimitive *current)
930 AdgPathPrivate *data;
931 AdgPrimitive *last, *current_dup, *last_dup;
932 gdouble radius, offset, pos;
933 AdgPair center, vector, p[3];
935 data = path->data;
936 last = &data->last;
937 current_dup = adg_primitive_deep_dup(current);
939 /* Force current_dup to point to the original segment so a
940 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
941 current_dup->segment = current->segment;
943 last_dup = adg_primitive_deep_dup(last);
944 radius = data->operation.data.fillet.radius;
945 offset = is_convex(last_dup, current_dup) ? -radius : radius;
947 /* Find the center of the fillet from the intersection between
948 * the last and current primitives offseted by radius */
949 cpml_primitive_offset(current_dup, offset);
950 cpml_primitive_offset(last_dup, offset);
951 if (cpml_primitive_intersection(current_dup, last_dup,
952 &center, 1) == 0) {
953 g_warning("Fillet not applicable (radius = %lf)", radius);
954 g_free(current_dup);
955 g_free(last_dup);
956 return;
959 /* Compute the start point of the fillet */
960 pos = cpml_primitive_near_pos(last_dup, &center);
961 cpml_primitive_vector_at(last_dup, &vector, pos);
962 cpml_vector_set_length(&vector, offset);
963 cpml_vector_normal(&vector);
964 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
966 /* Compute the mid point of the fillet */
967 cpml_pair_from_cairo(&vector, current->org);
968 cpml_pair_sub(&vector, &center);
969 cpml_vector_set_length(&vector, radius);
970 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
972 /* Compute the end point of the fillet */
973 pos = cpml_primitive_near_pos(current_dup, &center);
974 cpml_primitive_vector_at(current_dup, &vector, pos);
975 cpml_vector_set_length(&vector, offset);
976 cpml_vector_normal(&vector);
977 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
979 g_free(current_dup);
980 g_free(last_dup);
982 /* Change the end point of the last primitive */
983 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
985 /* Change the start point of the current primitive */
986 cpml_pair_to_cairo(&p[2], cpml_primitive_get_point(current, 0));
988 /* Add the fillet arc */
989 data->operation.action = ADG_ACTION_NONE;
990 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
993 static gboolean
994 is_convex(const AdgPrimitive *primitive1, const AdgPrimitive *primitive2)
996 CpmlVector v1, v2;
997 gdouble angle1, angle2;
999 cpml_primitive_vector_at(primitive1, &v1, -1);
1000 cpml_primitive_vector_at(primitive2, &v2, 0);
1002 /* Probably there is a smarter way to get this without trygonometry */
1003 angle1 = cpml_vector_angle(&v1);
1004 angle2 = cpml_vector_angle(&v2);
1006 if (angle1 > angle2)
1007 angle1 -= M_PI*2;
1009 return angle2-angle1 > M_PI;