[AdgPath] Return the current point as a const AdgPair *
[adg.git] / adg / adg-path.c
blobcc4be795bc4298cf3e462ed5c6d46c0e76611435
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 AdgOperator operator,
83 ...);
84 static void do_operation (AdgPath *path,
85 cairo_path_data_t
86 *path_data);
87 static void do_chamfer (AdgPath *path,
88 CpmlPrimitive *current);
89 static void do_fillet (AdgPath *path,
90 CpmlPrimitive *current);
91 static gboolean is_convex (const CpmlPrimitive
92 *primitive1,
93 const CpmlPrimitive
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.operator = ADG_OPERATOR_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 operator 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_OPERATOR_CHAMFER, delta1, delta2))
579 return;
583 * adg_path_fillet:
584 * @path: an #AdgPath
585 * @radius: the radius of the fillet
588 * A binary operator that joins to primitives with an arc.
589 * The first primitive involved is the current primitive, the second will
590 * be the next primitive appended to @path after this call. The second
591 * primitive is required: if the fillet operation is not properly
592 * terminated (by not providing the second primitive), any API accessing
593 * the path in reading mode will raise a warning.
595 void
596 adg_path_fillet(AdgPath *path, gdouble radius)
598 g_return_if_fail(ADG_IS_PATH(path));
600 if (!append_operation(path, ADG_OPERATOR_FILLET, radius))
601 return;
605 static void
606 clear(AdgModel *model)
608 AdgPath *path;
609 AdgPathPrivate *data;
611 path = (AdgPath *) model;
612 data = path->data;
614 g_array_set_size(data->cpml.array, 0);
615 clear_operation(path);
616 clear_parent(model);
619 static void
620 clear_parent(AdgModel *model)
622 if (PARENT_MODEL_CLASS->clear != NULL)
623 PARENT_MODEL_CLASS->clear(model);
626 static void
627 changed(AdgModel *model)
629 clear_parent(model);
631 if (PARENT_MODEL_CLASS->changed != NULL)
632 PARENT_MODEL_CLASS->changed(model);
635 static CpmlPath *
636 get_cpml_path(AdgTrail *trail)
638 clear_parent((AdgModel *) trail);
639 return read_cpml_path((AdgPath *) trail);
642 static CpmlPath *
643 read_cpml_path(AdgPath *path)
645 AdgPathPrivate *data = path->data;
647 /* Always regenerate the CpmlPath as it is a trivial operation */
648 data->cpml.path.status = CAIRO_STATUS_SUCCESS;
649 data->cpml.path.data = (cairo_path_data_t *) (data->cpml.array)->data;
650 data->cpml.path.num_data = (data->cpml.array)->len;
652 return &data->cpml.path;
655 static void
656 append_primitive(AdgPath *path, AdgPrimitive *current)
658 AdgPathPrivate *data;
659 cairo_path_data_t *path_data;
660 int length;
662 data = path->data;
663 path_data = current->data;
664 length = path_data[0].header.length;
666 /* Execute any pending operation */
667 do_operation(path, path_data);
669 /* Append the path data to the internal path array */
670 data->cpml.array = g_array_append_vals(data->cpml.array,
671 path_data, length);
673 /* Set path data to point to the recently appended cairo_path_data_t
674 * primitive: the first struct is the header */
675 path_data = (cairo_path_data_t *) (data->cpml.array)->data +
676 (data->cpml.array)->len - length;
678 /* Set the last primitive for subsequent binary operations */
679 data->last.org = data->cp_is_valid ? path_data - 1 : NULL;
680 data->last.segment = NULL;
681 data->last.data = path_data;
683 /* Save the last point as the current point, if applicable */
684 data->cp_is_valid = length > 1;
685 if (length > 1)
686 cpml_pair_from_cairo(&data->cp, &path_data[length-1]);
688 /* Invalidate cairo_path: should be recomputed */
689 clear_parent((AdgModel *) path);
692 static gint
693 needed_pairs(CpmlPrimitiveType type, gboolean cp_is_valid)
695 switch (type) {
697 case CAIRO_PATH_CLOSE_PATH:
698 g_return_val_if_fail(cp_is_valid, 0);
699 return 1;
701 case CAIRO_PATH_MOVE_TO:
702 return 2;
704 case CAIRO_PATH_LINE_TO:
705 g_return_val_if_fail(cp_is_valid, 0);
706 return 2;
708 case CAIRO_PATH_ARC_TO:
709 g_return_val_if_fail(cp_is_valid, 0);
710 return 3;
712 case CAIRO_PATH_CURVE_TO:
713 g_return_val_if_fail(cp_is_valid, 0);
714 return 4;
716 default:
717 g_return_val_if_reached(0);
720 return 0;
723 static void
724 clear_operation(AdgPath *path)
726 AdgPathPrivate *data;
727 AdgOperation *operation;
729 data = path->data;
730 operation = &data->operation;
732 if (operation->operator == ADG_OPERATOR_NONE)
733 return;
735 g_warning("An operation is still active while clearing the path "
736 "(operator `%d')", operation->operator);
737 operation->operator = ADG_OPERATOR_NONE;
740 static gboolean
741 append_operation(AdgPath *path, AdgOperator operator, ...)
743 AdgPathPrivate *data;
744 AdgOperation *operation;
745 va_list var_args;
747 data = path->data;
749 if (!data->cp_is_valid) {
750 g_warning("Operation requested but path has no current primitive "
751 "(operator `%d')", operator);
752 return FALSE;
755 operation = &data->operation;
756 if (operation->operator != ADG_OPERATOR_NONE) {
757 g_warning("Operation requested but another operation is yet active"
758 "(operators: new `%d', old `%d')",
759 operator, operation->operator);
760 ADG_MESSAGE("TODO: this is a rude simplification, as a lot of "
761 "operators can and may cohexist. As an example, a "
762 "fillet followed by a polar chamfer should be done.");
763 return FALSE;
766 va_start(var_args, operator);
768 switch (operator) {
770 case ADG_OPERATOR_CHAMFER:
771 operation->data.chamfer.delta1 = va_arg(var_args, double);
772 operation->data.chamfer.delta2 = va_arg(var_args, double);
773 break;
775 case ADG_OPERATOR_FILLET:
776 operation->data.fillet.radius = va_arg(var_args, double);
777 break;
779 case ADG_OPERATOR_NONE:
780 va_end(var_args);
781 return TRUE;
783 default:
784 g_warning("Operation not recognized (operator `%d')", operator);
785 va_end(var_args);
786 return FALSE;
789 operation->operator = operator;
790 va_end(var_args);
792 return TRUE;
795 static void
796 do_operation(AdgPath *path, cairo_path_data_t *path_data)
798 AdgPathPrivate *data;
799 AdgOperator operator;
800 CpmlSegment segment;
801 CpmlPrimitive current;
802 cairo_path_data_t current_org;
804 data = path->data;
805 operator = data->operation.operator;
806 cpml_segment_from_cairo(&segment, read_cpml_path(path));
808 /* Construct the current primitive, that is the primitive to be inserted.
809 * Its org is a copy of the end point of the last primitive: it can be
810 * modified without affecting anything else. It is expected the operation
811 * functions will add to @path the primitives required but NOT to add
812 * @current, as this one will be inserted automatically. */
813 current.segment = &segment;
814 current.org = &current_org;
815 current.data = path_data;
816 cpml_pair_to_cairo(&data->cp, &current_org);
818 switch (operator) {
820 case ADG_OPERATOR_NONE:
821 return;
823 case ADG_OPERATOR_CHAMFER:
824 do_chamfer(path, &current);
825 break;
827 case ADG_OPERATOR_FILLET:
828 do_fillet(path, &current);
829 break;
831 default:
832 g_warning("Operation not implemented (operator `%d')", operator);
833 return;
837 static void
838 do_chamfer(AdgPath *path, CpmlPrimitive *current)
840 AdgPathPrivate *data;
841 CpmlPrimitive *last;
842 gdouble delta1, delta2;
843 gdouble len1, len2;
844 AdgPair pair;
845 cairo_path_data_t line[2];
847 data = path->data;
848 last = &data->last;
849 delta1 = data->operation.data.chamfer.delta1;
850 len1 = cpml_primitive_length(last);
852 if (delta1 >= len1) {
853 g_warning("Chamfer too big for the last primitive (%lf >= %lf)",
854 delta1, len1);
855 return;
858 delta2 = data->operation.data.chamfer.delta2;
859 len2 = cpml_primitive_length(current);
861 if (delta2 >= len2) {
862 g_warning("Chamfer too big for the current primitive (%lf >= %lf)",
863 delta2, len2);
864 return;
867 /* Change the end point of the last primitive */
868 cpml_primitive_pair_at(last, &pair, 1. - delta1 / len1);
869 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(last, -1));
871 /* Change the start point of the current primitive */
872 cpml_primitive_pair_at(current, &pair, delta2 / len2);
873 cpml_pair_to_cairo(&pair, cpml_primitive_get_point(current, 0));
875 /* Add the chamfer line */
876 line[0].header.type = CAIRO_PATH_LINE_TO;
877 line[0].header.length = 2;
878 line[1].point.x = pair.x;
879 line[1].point.y = pair.y;
880 data->cpml.array = g_array_append_vals(data->cpml.array, line, 2);
882 data->operation.operator = ADG_OPERATOR_NONE;
885 static void
886 do_fillet(AdgPath *path, CpmlPrimitive *current)
888 AdgPathPrivate *data;
889 CpmlPrimitive *last, *current_dup, *last_dup;
890 gdouble radius, offset, pos;
891 AdgPair center, vector, p[3];
892 cairo_path_data_t arc[3];
894 data = path->data;
895 last = &data->last;
896 current_dup = adg_primitive_deep_dup(current);
898 /* Force current_dup to point to the original segment so a
899 * CAIRO_PATH_CLOSE_PATH primitive will work as expected */
900 current_dup->segment = current->segment;
902 last_dup = adg_primitive_deep_dup(last);
903 radius = data->operation.data.fillet.radius;
904 offset = is_convex(last_dup, current_dup) ? -radius : radius;
906 /* Find the center of the fillet from the intersection between
907 * the last and current primitives offseted by radius */
908 cpml_primitive_offset(current_dup, offset);
909 cpml_primitive_offset(last_dup, offset);
910 if (cpml_primitive_intersection(current_dup, last_dup,
911 &center, 1) == 0) {
912 g_warning("Fillet not applicable (radius = %lf)", radius);
913 g_free(current_dup);
914 g_free(last_dup);
915 return;
918 /* Compute the start point of the fillet */
919 pos = cpml_primitive_near_pos(last_dup, &center);
920 cpml_primitive_vector_at(last_dup, &vector, pos);
921 cpml_vector_set_length(&vector, offset);
922 cpml_vector_normal(&vector);
923 cpml_pair_sub(cpml_pair_copy(&p[0], &center), &vector);
925 /* Compute the mid point of the fillet */
926 cpml_pair_from_cairo(&vector, current->org);
927 cpml_pair_sub(&vector, &center);
928 cpml_vector_set_length(&vector, radius);
929 cpml_pair_add(cpml_pair_copy(&p[1], &center), &vector);
931 /* Compute the end point of the fillet */
932 pos = cpml_primitive_near_pos(current_dup, &center);
933 cpml_primitive_vector_at(current_dup, &vector, pos);
934 cpml_vector_set_length(&vector, offset);
935 cpml_vector_normal(&vector);
936 cpml_pair_sub(cpml_pair_copy(&p[2], &center), &vector);
938 g_free(current_dup);
939 g_free(last_dup);
941 /* Modify the end point of the last primitive */
942 cpml_pair_to_cairo(&p[0], cpml_primitive_get_point(last, -1));
944 /* Add the fillet arc */
945 arc[0].header.type = CAIRO_PATH_ARC_TO;
946 arc[0].header.length = 3;
947 cpml_pair_to_cairo(&p[1], &arc[1]);
948 cpml_pair_to_cairo(&p[2], &arc[2]);
949 data->cpml.array = g_array_append_vals(data->cpml.array, arc, 3);
951 data->operation.operator = ADG_OPERATOR_NONE;
954 static gboolean
955 is_convex(const CpmlPrimitive *primitive1, const CpmlPrimitive *primitive2)
957 CpmlVector v1, v2;
958 gdouble angle1, angle2;
960 cpml_primitive_vector_at(primitive1, &v1, -1);
961 cpml_primitive_vector_at(primitive2, &v2, 0);
963 /* Probably there is a smarter way to get this without trygonometry */
964 angle1 = cpml_vector_angle(&v1);
965 angle2 = cpml_vector_angle(&v2);
967 if (angle1 > angle2)
968 angle1 -= M_PI*2;
970 return angle2-angle1 > M_PI;