[AdgLDim] Avoid arrange() on the same data
[adg.git] / adg / adg-path.c
blob94ded28085cd4c96d6ff016070ab83f475fe35c0
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"
65 #include <math.h>
68 static void finalize (GObject *object);
69 static void changed (AdgModel *model);
70 static CpmlPath * get_cpml_path (AdgTrail *trail);
71 static CpmlPath * read_cpml_path (AdgPath *path);
72 static void append_primitive (AdgPath *path,
73 AdgPrimitive *primitive);
74 static gint needed_pairs (CpmlPrimitiveType type,
75 gboolean cp_is_valid);
76 static void clear_operation (AdgPath *path);
77 static gboolean append_operation (AdgPath *path,
78 AdgOperator operator,
79 ...);
80 static void do_operation (AdgPath *path,
81 cairo_path_data_t
82 *path_data);
83 static void do_chamfer (AdgPath *path,
84 CpmlPrimitive *current);
85 static void do_fillet (AdgPath *path,
86 CpmlPrimitive *current);
87 static gboolean is_convex (const CpmlPrimitive
88 *primitive1,
89 const CpmlPrimitive
90 *primitive2);
93 G_DEFINE_TYPE(AdgPath, adg_path, ADG_TYPE_TRAIL);
96 static void
97 adg_path_class_init(AdgPathClass *klass)
99 GObjectClass *gobject_class;
100 AdgModelClass *model_class;
101 AdgTrailClass *trail_class;
103 gobject_class = (GObjectClass *) klass;
104 model_class = (AdgModelClass *) klass;
105 trail_class = (AdgTrailClass *) klass;
107 g_type_class_add_private(klass, sizeof(AdgPathPrivate));
109 gobject_class->finalize = finalize;
111 model_class->changed = changed;
113 trail_class->get_cpml_path = get_cpml_path;
116 static void
117 adg_path_init(AdgPath *path)
119 AdgPathPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(path, ADG_TYPE_PATH,
120 AdgPathPrivate);
122 data->cp_is_valid = FALSE;
123 data->path = g_array_new(FALSE, FALSE, sizeof(cairo_path_data_t));
124 data->operation.operator = ADG_OPERATOR_NONE;
126 path->data = data;
129 static void
130 finalize(GObject *object)
132 AdgPath *path;
133 AdgPathPrivate *data;
134 GObjectClass *object_class;
136 path = (AdgPath *) object;
137 data = path->data;
138 object_class = (GObjectClass *) adg_path_parent_class;
140 g_array_free(data->path, TRUE);
141 clear_operation(path);
143 if (object_class->finalize != NULL)
144 object_class->finalize(object);
149 * adg_path_new:
151 * Creates a new path model. The path should be constructed
152 * programmatically by using the methods provided by #AdgPath.
154 * Returns: the newly created path model
156 AdgPath *
157 adg_path_new(void)
159 return g_object_new(ADG_TYPE_PATH, NULL);
163 * adg_path_get_current_point:
164 * @path: an #AdgPath
165 * @x: where to store the x coordinate of the current point
166 * @y: where to store the y coordinate of the current point
168 * Gets the current point of @path, which is conceptually the
169 * final point reached by the path so far.
171 * If there is no defined current point, @x and @y will both be set
172 * to 0 and a warning will be triggered. It is possible to check this
173 * in advance with adg_path_has_current_point().
175 * Most #AdgPath methods alter the current point and most of them
176 * expect a current point to be defined otherwise will fail triggering
177 * a warning. Check the description of every method for specific details.
179 void
180 adg_path_get_current_point(AdgPath *path, gdouble *x, gdouble *y)
182 AdgPathPrivate *data;
184 g_return_if_fail(ADG_IS_PATH(path));
186 data = path->data;
188 if (data->cp_is_valid) {
189 *x = data->cp.x;
190 *y = data->cp.y;
191 } else {
192 *x = *y = 0.;
193 g_return_if_reached();
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_clear:
220 * @path: an #AdgPath
222 * Releases the internal memory hold by @path and resets its status,
223 * so that after this call @path contains an empty path.
225 void
226 adg_path_clear(AdgPath *path)
228 AdgPathPrivate *data;
230 g_return_if_fail(ADG_IS_PATH(path));
232 data = path->data;
234 g_array_set_size(data->path, 0);
235 clear_operation(path);
236 adg_trail_clear_cairo_path((AdgTrail *) path);
241 * adg_path_append:
242 * @path: an #AdgPath
243 * @type: a #cairo_data_type_t value
244 * @...: point data, specified as #AdgPair pointers
246 * Generic method to append a primitive to @path. The number of #AdgPair
247 * structs depends on @type: there is no way with this function to
248 * reserve more cairo_path_data_t structs than what is needed by the
249 * primitive.
251 * This function accepts also the special %CAIRO_PATH_ARC_TO primitive.
253 * If @path has no current point while the requested primitive needs it,
254 * a warning message will be triggered without other effect.
256 void
257 adg_path_append(AdgPath *path, CpmlPrimitiveType type, ...)
259 va_list var_args;
261 va_start(var_args, type);
262 adg_path_append_valist(path, type, var_args);
263 va_end(var_args);
267 * adg_path_append_valist:
268 * @path: an #AdgPath
269 * @type: a #cairo_data_type_t value
270 * @var_args: point data, specified as #AdgPair pointers
272 * va_list version of adg_path_append().
274 void
275 adg_path_append_valist(AdgPath *path, CpmlPrimitiveType type, va_list var_args)
277 AdgPathPrivate *data;
278 AdgPrimitive primitive;
279 gint length, cnt;
280 cairo_path_data_t org;
281 cairo_path_data_t *path_data;
283 g_return_if_fail(ADG_IS_PATH(path));
285 data = path->data;
286 length = needed_pairs(type, data->cp_is_valid);
287 if (length == 0)
288 return;
290 /* Set a copy of the current point as the primitive origin */
291 cpml_pair_to_cairo(&data->cp, &org);
292 primitive.org = &org;
294 /* Build the cairo_path_data_t array */
295 primitive.data = path_data = g_new(cairo_path_data_t, length);
297 path_data->header.type = type;
298 path_data->header.length = length;
300 for (cnt = 1; cnt < length; ++ cnt) {
301 ++ path_data;
302 cpml_pair_to_cairo(va_arg(var_args, AdgPair *), path_data);
305 /* Terminate the creation of the temporary primitive */
306 primitive.segment = NULL;
308 /* Append this primitive to @path */
309 append_primitive(path, &primitive);
311 g_free(primitive.data);
315 * adg_path_append_primitive:
316 * @path: an #AdgPath
317 * @primitive: the #AdgPrimitive to append
319 * Appends @primitive to @path. The primitive to add is considered the
320 * continuation of the current path so the <structfield>org</structfield>
321 * component of @primitive is not used. Anyway the current poins is
322 * checked against it: they must be equal or the function will fail
323 * without further processing.
325 void
326 adg_path_append_primitive(AdgPath *path, const AdgPrimitive *primitive)
328 AdgPathPrivate *data;
329 AdgPrimitive *primitive_dup;
331 g_return_if_fail(ADG_IS_PATH(path));
332 g_return_if_fail(primitive != NULL);
334 data = path->data;
336 g_return_if_fail(primitive->org->point.x == data->cp.x &&
337 primitive->org->point.y == data->cp.y);
339 /* The primitive data could be modified by pending operations:
340 * work on a copy */
341 primitive_dup = adg_primitive_deep_dup(primitive);
343 append_primitive(path, primitive_dup);
345 g_free(primitive_dup);
349 * adg_path_append_segment:
350 * @path: an #AdgPath
351 * @segment: the #AdgSegment to append
353 * Appends @segment to @path.
355 void
356 adg_path_append_segment(AdgPath *path, const AdgSegment *segment)
358 AdgPathPrivate *data;
360 g_return_if_fail(ADG_IS_PATH(path));
361 g_return_if_fail(segment != NULL);
363 data = path->data;
365 adg_trail_clear_cairo_path((AdgTrail *) path);
366 data->path = g_array_append_vals(data->path,
367 segment->data, segment->num_data);
371 * adg_path_append_cairo_path:
372 * @path: an #AdgPath
373 * @cairo_path: the #cairo_path_t path to append
375 * Appends a whole cairo path to @path.
377 void
378 adg_path_append_cairo_path(AdgPath *path, const cairo_path_t *cairo_path)
380 AdgPathPrivate *data;
382 g_return_if_fail(ADG_IS_PATH(path));
384 data = path->data;
386 adg_trail_clear_cairo_path((AdgTrail *) path);
387 data->path = g_array_append_vals(data->path,
388 cairo_path->data, cairo_path->num_data);
392 * adg_path_move_to:
393 * @path: an #AdgPath
394 * @x: the new x coordinate
395 * @y: the new y coordinate
397 * Begins a new segment. After this call the current point will be (@x, @y).
399 void
400 adg_path_move_to(AdgPath *path, gdouble x, gdouble y)
402 AdgPair p;
404 p.x = x;
405 p.y = y;
407 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p);
411 * adg_path_line_to:
412 * @path: an #AdgPath
413 * @x: the new x coordinate
414 * @y: the new y coordinate
416 * Adds a line to @path from the current point to position (@x, @y).
417 * After this call the current point will be (@x, @y).
419 * If @path has no current point before this call, this function will
420 * trigger a warning without other effect.
422 void
423 adg_path_line_to(AdgPath *path, gdouble x, gdouble y)
425 AdgPair p;
427 p.x = x;
428 p.y = y;
430 adg_path_append(path, CAIRO_PATH_LINE_TO, &p);
434 * adg_path_arc_to:
435 * @path: an #AdgPath
436 * @x1: the x coordinate of an intermediate point
437 * @y1: the y coordinate of an intermediate point
438 * @x2: the x coordinate of the end of the arc
439 * @y2: the y coordinate of the end of the arc
441 * Adds an arc to the path from the current point to (@x2, @y2),
442 * passing throught (@x1, @y1). After this call the current point
443 * will be (@x2, @y2).
445 * If @path has no current point before this call, this function will
446 * trigger a warning without other effect.
448 void
449 adg_path_arc_to(AdgPath *path, gdouble x1, gdouble y1, gdouble x2, gdouble y2)
451 AdgPair p[2];
453 p[0].x = x1;
454 p[0].y = y1;
455 p[1].x = x2;
456 p[1].y = y2;
458 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[0], &p[1]);
462 * adg_path_curve_to:
463 * @path: an #AdgPath
464 * @x1: the x coordinate of the first control point
465 * @y1: the y coordinate of the first control point
466 * @x2: the x coordinate of the second control point
467 * @y2: the y coordinate of the second control point
468 * @x3: the x coordinate of the end of the curve
469 * @y3: the y coordinate of the end of the curve
471 * Adds a cubic Bézier curve to the path from the current point to
472 * position (@x3, @y3), using (@x1, @y1) and (@x2, @y2) as the
473 * control points. After this call the current point will be (@x3, @y3).
475 * If @path has no current point before this call, this function will
476 * trigger a warning without other effect.
478 void
479 adg_path_curve_to(AdgPath *path, gdouble x1, gdouble y1,
480 gdouble x2, gdouble y2, gdouble x3, gdouble y3)
482 AdgPair p[3];
484 p[0].x = x1;
485 p[0].y = y1;
486 p[1].x = x2;
487 p[1].y = y2;
488 p[2].x = x3;
489 p[2].y = y3;
491 adg_path_append(path, CAIRO_PATH_CURVE_TO, &p[0], &p[1], &p[2]);
495 * adg_path_close:
496 * @path: an #AdgPath
498 * Adds a line segment to the path from the current point to the
499 * beginning of the current segment, (the most recent point passed
500 * to an adg_path_move_to()), and closes this segment.
501 * After this call the current point will be unset.
503 * The behavior of adg_path_close() is distinct from simply calling
504 * adg_line_to() with the coordinates of the segment starting point.
505 * When a closed segment is stroked, there are no caps on the ends.
506 * Instead, there is a line join connecting the final and initial
507 * primitive of the segment.
509 * If @path has no current point before this call, this function will
510 * trigger a warning without other effect.
512 void
513 adg_path_close(AdgPath *path)
515 adg_path_append(path, CAIRO_PATH_CLOSE_PATH);
519 * adg_path_arc
520 * @path: an #AdgPath
521 * @xc: x position of the center of the arc
522 * @yc: y position of the center of the arc
523 * @r: the radius of the arc
524 * @start: the start angle, in radians
525 * @end: the end angle, in radians
527 * A more usual way to add an arc to @path. After this call, the current
528 * point will be the computed end point of the arc. The arc will be
529 * rendered in increasing angle, accordling to @start and @end. This means
530 * if @start is less than @end, the arc will be rendered in clockwise
531 * direction (accordling to the default cairo coordinate system) while if
532 * @start is greather than @end, the arc will be rendered in couterclockwise
533 * direction.
535 * By explicitely setting the whole arc data, the start point could be
536 * different from the current point. In this case, if @path has no
537 * current point before the call a %CAIRO_PATH_MOVE_TO to the start
538 * point of the arc will be automatically prepended to the arc.
539 * If @path has a current point, a %CAIRO_PATH_LINE_TO to the start
540 * point of the arc will be used instead of the moveto.
542 void
543 adg_path_arc(AdgPath *path, gdouble xc, gdouble yc, gdouble r,
544 gdouble start, gdouble end)
546 AdgPathPrivate *data;
547 AdgPair center, p[3];
549 g_return_if_fail(ADG_IS_PATH(path));
551 data = path->data;
552 center.x = xc;
553 center.y = yc;
555 cpml_vector_from_angle(&p[0], start);
556 cpml_vector_from_angle(&p[1], (end-start) / 2);
557 cpml_vector_from_angle(&p[2], end);
559 cpml_vector_set_length(&p[0], r);
560 cpml_vector_set_length(&p[1], r);
561 cpml_vector_set_length(&p[2], r);
563 cpml_pair_add(&p[0], &center);
564 cpml_pair_add(&p[1], &center);
565 cpml_pair_add(&p[2], &center);
567 if (!data->cp_is_valid)
568 adg_path_append(path, CAIRO_PATH_MOVE_TO, &p[0]);
569 else if (p[0].x != data->cp.x || p[0].y != data->cp.y)
570 adg_path_append(path, CAIRO_PATH_LINE_TO, &p[0]);
572 adg_path_append(path, CAIRO_PATH_ARC_TO, &p[1], &p[2]);
576 * adg_path_chamfer
577 * @path: an #AdgPath
578 * @delta1: the distance from the intersection point of the current primitive
579 * @delta2: the distance from the intersection point of the next primitive
581 * A binary operator that generates a chamfer between two primitives.
582 * The first primitive involved is the current primitive, the second will
583 * be the next primitive appended to @path after this call. The second
584 * primitive is required: if the chamfer operation is not properly
585 * terminated (by not providing the second primitive), any API accessing
586 * the path in reading mode will raise a warning.
588 * The chamfer operation requires two lengths: @delta1 specifies the
589 * "quantity" to trim on the first primitive while @delta2 is the same
590 * applied on the second primitive. The term "quantity" means the length
591 * of the portion to cut out from the original primitive (that is the
592 * primitive as would be without the chamfer).
594 void
595 adg_path_chamfer(AdgPath *path, gdouble delta1, gdouble delta2)
597 g_return_if_fail(ADG_IS_PATH(path));
599 if (!append_operation(path, ADG_OPERATOR_CHAMFER, delta1, delta2))
600 return;
604 * adg_path_fillet:
605 * @path: an #AdgPath
606 * @radius: the radius of the fillet
609 * A binary operator that joins to primitives with an arc.
610 * The first primitive involved is the current primitive, the second will
611 * be the next primitive appended to @path after this call. The second
612 * primitive is required: if the fillet operation is not properly
613 * terminated (by not providing the second primitive), any API accessing
614 * the path in reading mode will raise a warning.
616 void
617 adg_path_fillet(AdgPath *path, gdouble radius)
619 g_return_if_fail(ADG_IS_PATH(path));
621 if (!append_operation(path, ADG_OPERATOR_FILLET, radius))
622 return;
626 static void
627 changed(AdgModel *model)
629 AdgModelClass *model_class = (AdgModelClass *) adg_path_parent_class;
631 adg_path_clear((AdgPath *) model);
633 if (model_class->changed != NULL)
634 model_class->changed(model);
637 static CpmlPath *
638 get_cpml_path(AdgTrail *trail)
640 adg_trail_clear_cairo_path(trail);
642 return read_cpml_path((AdgPath *) trail);
645 static CpmlPath *
646 read_cpml_path(AdgPath *path)
648 AdgPathPrivate *data;
649 CpmlPath *cpml_path;
651 data = path->data;
652 cpml_path = &data->cpml_path;
654 cpml_path->status = CAIRO_STATUS_SUCCESS;
655 cpml_path->data = (cairo_path_data_t *) data->path->data;
656 cpml_path->num_data = data->path->len;
658 return cpml_path;
661 static void
662 append_primitive(AdgPath *path, AdgPrimitive *current)
664 AdgPathPrivate *data;
665 cairo_path_data_t *path_data;
666 int length;
668 data = path->data;
669 path_data = current->data;
670 length = path_data[0].header.length;
672 /* Execute any pending operation */
673 do_operation(path, path_data);
675 /* Append the path data to the internal path array */
676 data->path = g_array_append_vals(data->path, path_data, length);
678 /* Set path data to point to the recently appended cairo_path_data_t
679 * primitive: the first struct is the header */
680 path_data = (cairo_path_data_t *) data->path->data +
681 data->path->len - length;
683 /* Set the last primitive for subsequent binary operations */
684 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
685 data->last.segment = NULL;
686 data->last.data = path_data;
688 /* Save the last point as the current point, if applicable */
689 data->cp_is_valid = length > 1;
690 if (length > 1)
691 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
693 /* Invalidate cairo_path: should be recomputed */
694 adg_trail_clear_cairo_path((AdgTrail *) path);
697 static gint
698 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
700 switch (type) {
702 case CAIRO_PATH_CLOSE_PATH:
703 g_return_val_if_fail(cp_is_valid, 0);
704 return 1;
706 case CAIRO_PATH_MOVE_TO:
707 return 2;
709 case CAIRO_PATH_LINE_TO:
710 g_return_val_if_fail(cp_is_valid, 0);
711 return 2;
713 case CAIRO_PATH_ARC_TO:
714 g_return_val_if_fail(cp_is_valid, 0);
715 return 3;
717 case CAIRO_PATH_CURVE_TO:
718 g_return_val_if_fail(cp_is_valid, 0);
719 return 4;
721 default:
722 g_return_val_if_reached(0);
725 return 0;
728 static void
729 clear_operation(AdgPath *path)
731 AdgPathPrivate *data;
732 AdgOperation *operation;
734 data = path->data;
735 operation = &data->operation;
737 if (operation->operator == ADG_OPERATOR_NONE)
738 return;
740 g_warning("An operation is still active while clearing the path "
741 "(operator `%d')", operation->operator);
742 operation->operator = ADG_OPERATOR_NONE;
745 static gboolean
746 append_operation(AdgPath *path, AdgOperator operator, ...)
748 AdgPathPrivate *data;
749 AdgOperation *operation;
750 va_list var_args;
752 data = path->data;
754 if (!data->cp_is_valid) {
755 g_warning("Operation requested but path has no current primitive "
756 "(operator `%d')", operator);
757 return FALSE;
760 operation = &data->operation;
761 if (operation->operator != ADG_OPERATOR_NONE) {
762 g_warning("Operation requested but another operation is yet active"
763 "(operators: new `%d', old `%d')",
764 operator, operation->operator);
765 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
766 "operators can and may cohexist. As an example, a "
767 "fillet followed by a polar chamfer should be done.");
768 return FALSE;
771 va_start(var_args, operator);
773 switch (operator) {
775 case ADG_OPERATOR_CHAMFER:
776 operation->data.chamfer.delta1 = va_arg(var_args, double);
777 operation->data.chamfer.delta2 = va_arg(var_args, double);
778 break;
780 case ADG_OPERATOR_FILLET:
781 operation->data.fillet.radius = va_arg(var_args, double);
782 break;
784 case ADG_OPERATOR_NONE:
785 va_end(var_args);
786 return TRUE;
788 default:
789 g_warning("Operation not recognized (operator `%d')", operator);
790 va_end(var_args);
791 return FALSE;
794 operation->operator = operator;
795 va_end(var_args);
797 return TRUE;
800 static void
801 do_operation(AdgPath *path, cairo_path_data_t *path_data)
803 AdgPathPrivate *data;
804 AdgOperator operator;
805 CpmlSegment segment;
806 CpmlPrimitive current;
807 cairo_path_data_t current_org;
809 data = path->data;
810 operator = data->operation.operator;
811 cpml_segment_from_cairo(&segment, read_cpml_path(path));
813 /* Construct the current primitive, that is the primitive to be inserted.
814 * Its org is a copy of the end point of the last primitive: it can be
815 * modified without affecting anything else. It is expected the operation
816 * functions will add to @path the primitives required but NOT to add
817 * @current, as this one will be inserted automatically. */
818 current.segment = &segment;
819 current.org = &current_org;
820 current.data = path_data;
821 cpml_pair_to_cairo(&data->cp, &current_org);
823 switch (operator) {
825 case ADG_OPERATOR_NONE:
826 return;
828 case ADG_OPERATOR_CHAMFER:
829 do_chamfer(path, &current);
830 break;
832 case ADG_OPERATOR_FILLET:
833 do_fillet(path, &current);
834 break;
836 default:
837 g_warning("Operation not implemented (operator `%d')", operator);
838 return;
842 static void
843 do_chamfer(AdgPath *path, CpmlPrimitive *current)
845 AdgPathPrivate *data;
846 CpmlPrimitive *last;
847 gdouble delta1, delta2;
848 gdouble len1, len2;
849 AdgPair pair;
850 cairo_path_data_t line[2];
852 data = path->data;
853 last = &data->last;
854 delta1 = data->operation.data.chamfer.delta1;
855 len1 = cpml_primitive_length(last);
857 if (delta1 >= len1) {
858 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
859 delta1, len1);
860 return;
863 delta2 = data->operation.data.chamfer.delta2;
864 len2 = cpml_primitive_length(current);
866 if (delta2 >= len2) {
867 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
868 delta2, len2);
869 return;
872 /* Change the end point of the last primitive */
873 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
874 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
876 /* Change the start point of the current primitive */
877 cpml_primitive_pair_at(current, &pair, delta2 / len2);
878 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
880 /* Add the chamfer line */
881 line[0].header.type = CAIRO_PATH_LINE_TO;
882 line[0].header.length = 2;
883 line[1].point.x = pair.x;
884 line[1].point.y = pair.y;
885 data->path = g_array_append_vals(data->path, line, 2);
887 data->operation.operator = ADG_OPERATOR_NONE;
890 static void
891 do_fillet(AdgPath *path, CpmlPrimitive *current)
893 AdgPathPrivate *data;
894 CpmlPrimitive *last, *current_dup, *last_dup;
895 gdouble radius, offset, pos;
896 AdgPair center, vector, p[3];
897 cairo_path_data_t arc[3];
899 data = path->data;
900 last = &data->last;
901 current_dup = adg_primitive_deep_dup(current);
903 /* Force current_dup to point to the original segment so a
904 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
905 current_dup->segment = current->segment;
907 last_dup = adg_primitive_deep_dup(last);
908 radius = data->operation.data.fillet.radius;
909 offset = is_convex(last_dup, current_dup) ? -radius : radius;
911 /* Find the center of the fillet from the intersection between
912 * the last and current primitives offseted by radius */
913 cpml_primitive_offset(current_dup, offset);
914 cpml_primitive_offset(last_dup, offset);
915 if (cpml_primitive_intersection(current_dup, last_dup,
916 &center, 1) == 0) {
917 g_warning("Fillet not applicable (radius = %lf)", radius);
918 g_free(current_dup);
919 g_free(last_dup);
920 return;
923 /* Compute the start point of the fillet */
924 pos = cpml_primitive_near_pos(last_dup, &center);
925 cpml_primitive_vector_at(last_dup, &vector, pos);
926 cpml_vector_set_length(&vector, offset);
927 cpml_vector_normal(&vector);
928 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
930 /* Compute the mid point of the fillet */
931 cpml_pair_from_cairo(&vector, current->org);
932 cpml_pair_sub(&vector, &center);
933 cpml_vector_set_length(&vector, radius);
934 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
936 /* Compute the end point of the fillet */
937 pos = cpml_primitive_near_pos(current_dup, &center);
938 cpml_primitive_vector_at(current_dup, &vector, pos);
939 cpml_vector_set_length(&vector, offset);
940 cpml_vector_normal(&vector);
941 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
943 g_free(current_dup);
944 g_free(last_dup);
946 /* Modify the end point of the last primitive */
947 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
949 /* Add the fillet arc */
950 arc[0].header.type = CAIRO_PATH_ARC_TO;
951 arc[0].header.length = 3;
952 cpml_pair_to_cairo(&p[1], &arc[1]);
953 cpml_pair_to_cairo(&p[2], &arc[2]);
954 data->path = g_array_append_vals(data->path, arc, 3);
956 data->operation.operator = ADG_OPERATOR_NONE;
959 static gboolean
960 is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
962 CpmlVector v1, v2;
963 gdouble angle1, angle2;
965 cpml_primitive_vector_at(primitive1, &v1, -1);
966 cpml_primitive_vector_at(primitive2, &v2, 0);
968 /* Probably there is a smarter way to get this without trygonometry */
969 angle1 = cpml_vector_angle(&v1);
970 angle2 = cpml_vector_angle(&v2);
972 if (angle1 > angle2)
973 angle1 -= M_PI*2;
975 return angle2-angle1 > M_PI;