1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2017 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.
23 * @short_description: A model with the edges of another model
25 * The #AdgEdges can be used to render the edges of a yet existing
26 * #AdgTrail source. It is useful for any part made by revolution,
27 * where the shape is symmetric along a specific axis and thus the
28 * edge lines can be easily computed.
30 * The trail can be set by changing the #AdgEdges:source property
31 * or the relevant APIs. If the trail changes, a recomputation
32 * can be forced by calling the adg_model_clear() method.
34 * The angle of the axis is implied to pass through the (0,0) point
35 * and has an angle of #AdgEdges:axis-angle radiants. The default
36 * is a 0 radiant angle, meaning the y=0 axis is assumed.
44 * All fields are private and should not be used directly.
45 * Use its public methods instead.
51 #include "adg-internal.h"
52 #include "adg-model.h"
53 #include "adg-trail.h"
56 #include "adg-edges.h"
57 #include "adg-edges-private.h"
60 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_edges_parent_class)
61 #define _ADG_OLD_MODEL_CLASS ((AdgModelClass *) adg_edges_parent_class)
64 G_DEFINE_TYPE(AdgEdges
, adg_edges
, ADG_TYPE_TRAIL
)
74 static void _adg_dispose (GObject
*object
);
75 static void _adg_finalize (GObject
*object
);
76 static void _adg_get_property (GObject
*object
,
80 static void _adg_set_property (GObject
*object
,
84 static void _adg_clear (AdgModel
*model
);
85 static cairo_path_t
* _adg_get_cairo_path (AdgTrail
*trail
);
86 static void _adg_unset_source (AdgEdges
*edges
);
87 static void _adg_clear_cairo_path (AdgEdges
*edges
);
88 static GSList
* _adg_get_vertices (GSList
*vertices
,
91 static GSList
* _adg_optimize_vertices (GSList
*vertices
);
92 static GArray
* _adg_path_build (const GSList
*vertices
);
93 static void _adg_path_transform (GArray
*path_data
,
94 const cairo_matrix_t
*map
);
98 adg_edges_class_init(AdgEdgesClass
*klass
)
100 GObjectClass
*gobject_class
;
101 AdgModelClass
*model_class
;
102 AdgTrailClass
*trail_class
;
105 gobject_class
= (GObjectClass
*) klass
;
106 model_class
= (AdgModelClass
*) klass
;
107 trail_class
= (AdgTrailClass
*) klass
;
109 g_type_class_add_private(klass
, sizeof(AdgEdgesPrivate
));
111 gobject_class
->dispose
= _adg_dispose
;
112 gobject_class
->finalize
= _adg_finalize
;
113 gobject_class
->get_property
= _adg_get_property
;
114 gobject_class
->set_property
= _adg_set_property
;
116 model_class
->clear
= _adg_clear
;
118 trail_class
->get_cairo_path
= _adg_get_cairo_path
;
120 param
= g_param_spec_object("source",
122 P_("The source from which the edges should be computed from"),
124 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
);
125 g_object_class_install_property(gobject_class
, PROP_SOURCE
, param
);
127 param
= g_param_spec_double("axis-angle",
129 P_("The angle of the axis of the source trail: it is implied this axis passes through (0,0)"),
132 g_object_class_install_property(gobject_class
, PROP_AXIS_ANGLE
, param
);
134 param
= g_param_spec_double("critical-angle",
135 P_("Critical Angle"),
136 P_("The angle that defines which corner generates an edge (if the corner angle is greater than this critical angle) and which edge is ignored"),
139 g_object_class_install_property(gobject_class
, PROP_CRITICAL_ANGLE
, param
);
143 adg_edges_init(AdgEdges
*edges
)
145 AdgEdgesPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(edges
, ADG_TYPE_EDGES
,
149 data
->critical_angle
= G_PI
/ 45;
150 data
->axis_angle
= 0;
152 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
153 data
->cairo
.array
= NULL
;
159 _adg_dispose(GObject
*object
)
161 AdgEdges
*edges
= (AdgEdges
*) object
;
163 adg_edges_set_source(edges
, NULL
);
165 if (_ADG_OLD_OBJECT_CLASS
->dispose
!= NULL
)
166 _ADG_OLD_OBJECT_CLASS
->dispose(object
);
170 _adg_finalize(GObject
*object
)
172 _adg_clear_cairo_path((AdgEdges
*) object
);
174 if (_ADG_OLD_OBJECT_CLASS
->finalize
!= NULL
)
175 _ADG_OLD_OBJECT_CLASS
->finalize(object
);
179 _adg_get_property(GObject
*object
, guint prop_id
,
180 GValue
*value
, GParamSpec
*pspec
)
183 AdgEdgesPrivate
*data
;
185 edges
= (AdgEdges
*) object
;
190 g_value_set_object(value
, data
->source
);
192 case PROP_AXIS_ANGLE
:
193 g_value_set_double(value
, data
->axis_angle
);
195 case PROP_CRITICAL_ANGLE
:
196 g_value_set_double(value
, data
->critical_angle
);
199 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
205 _adg_set_property(GObject
*object
, guint prop_id
,
206 const GValue
*value
, GParamSpec
*pspec
)
209 AdgEdgesPrivate
*data
;
210 gpointer tmp_pointer
;
213 edges
= (AdgEdges
*) object
;
218 tmp_pointer
= data
->source
;
219 data
->source
= g_value_get_object(value
);
221 if (tmp_pointer
!= data
->source
) {
223 g_object_weak_ref((GObject
*) data
->source
,
224 (GWeakNotify
) _adg_unset_source
, object
);
226 g_object_weak_unref((GObject
*) tmp_pointer
,
227 (GWeakNotify
) _adg_unset_source
, object
);
230 _adg_clear((AdgModel
*) object
);
232 case PROP_AXIS_ANGLE
:
233 tmp_double
= g_value_get_double(value
);
234 if (data
->axis_angle
!= tmp_double
) {
235 data
->axis_angle
= tmp_double
;
236 _adg_clear_cairo_path(edges
);
239 case PROP_CRITICAL_ANGLE
:
240 tmp_double
= g_value_get_double(value
);
241 if (data
->critical_angle
!= tmp_double
) {
242 data
->critical_angle
= tmp_double
;
243 _adg_clear_cairo_path(edges
);
247 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, prop_id
, pspec
);
256 * Creates a new undefined model to keep track of the edges of
257 * another model. You should at least set the referred #AdgTrail
258 * with adg_edges_set_source().
260 * Returns: the newly created edges model
267 return g_object_new(ADG_TYPE_EDGES
, NULL
);
271 * adg_edges_new_with_source:
272 * @source: (transfer none): the new source #AdgTrail
274 * Creates a new edges model explicitely specifying the source trail.
275 * The returned object will own a weak reference on @source.
277 * Returns: the newly created edges model
282 adg_edges_new_with_source(AdgTrail
*source
)
284 return g_object_new(ADG_TYPE_EDGES
, "source", source
, NULL
);
288 * adg_edges_get_source:
289 * @edges: an #AdgEdges
291 * Gets the source #AdgTrail of this @edges model.
292 * The returned object is owned by @edges and should not be
295 * Returns: (transfer none): the requested #AdgTrail or <constant>NULL</constant> on errors.
300 adg_edges_get_source(AdgEdges
*edges
)
302 AdgEdgesPrivate
*data
;
304 g_return_val_if_fail(ADG_IS_EDGES(edges
), NULL
);
312 * adg_edges_set_source:
313 * @edges: an #AdgEdges
314 * @source: (transfer none): the new source #AdgTrail
316 * Sets @source as the source trail for @edges.
317 * After the call, @edges will own a weak reference on @source.
322 adg_edges_set_source(AdgEdges
*edges
, AdgTrail
*source
)
324 g_return_if_fail(ADG_IS_EDGES(edges
));
325 g_object_set(edges
, "source", source
, NULL
);
329 * adg_edges_set_axis_angle:
330 * @edges: an #AdgEdges
331 * @angle: the new angle (in radians)
333 * Sets the axis angle of @edges to @angle, basically setting
334 * the #AdgEdges:axis-angle property. All the resulting edge
335 * lines will be normal to this axis.
337 * It is implied the axis will pass through the (0,0) point,
338 * so the underlying trail should be constructed accordingly.
343 adg_edges_set_axis_angle(AdgEdges
*edges
, gdouble angle
)
345 g_return_if_fail(ADG_IS_EDGES(edges
));
346 g_object_set(edges
, "axis-angle", angle
, NULL
);
350 * adg_edges_get_axis_angle:
351 * @edges: an #AdgEdges
353 * Gets the angle of the supposed axis of @edges. Refer to
354 * adg_edges_set_axis_angle() for details of what this parameter
357 * Returns: the value (in radians) of the axis angle
362 adg_edges_get_axis_angle(AdgEdges
*edges
)
364 AdgEdgesPrivate
*data
;
366 g_return_val_if_fail(ADG_IS_EDGES(edges
), 0);
369 return data
->axis_angle
;
373 * adg_edges_set_critical_angle:
374 * @edges: an #AdgEdges
375 * @angle: the new angle (in radians)
377 * Sets the critical angle of @edges to @angle, basically setting
378 * the #AdgEdges:critical-angle property.
380 * The critical angle defines what corner should generate an edge and
381 * what not. Typical values are close to 0, being 0 the lowest angle
382 * where every corner generates an edge.
387 adg_edges_set_critical_angle(AdgEdges
*edges
, gdouble angle
)
389 g_return_if_fail(ADG_IS_EDGES(edges
));
390 g_object_set(edges
, "critical-angle", angle
, NULL
);
394 * adg_edges_get_critical_angle:
395 * @edges: an #AdgEdges
397 * Gets the current critical angle of @edges. Refer to
398 * adg_edges_set_critical_angle() for details of what this parameter
401 * Returns: the value (in radians) of the critical angle
406 adg_edges_get_critical_angle(AdgEdges
*edges
)
408 AdgEdgesPrivate
*data
;
410 g_return_val_if_fail(ADG_IS_EDGES(edges
), 0);
413 return data
->critical_angle
;
418 _adg_clear(AdgModel
*model
)
420 _adg_clear_cairo_path((AdgEdges
*) model
);
422 if (_ADG_OLD_MODEL_CLASS
->clear
!= NULL
)
423 _ADG_OLD_MODEL_CLASS
->clear(model
);
426 static cairo_path_t
*
427 _adg_get_cairo_path(AdgTrail
*trail
)
430 AdgEdgesPrivate
*data
;
436 edges
= (AdgEdges
*) trail
;
439 /* Check for cached path */
440 if (data
->cairo
.path
.status
== CAIRO_STATUS_SUCCESS
)
441 return &data
->cairo
.path
;
443 _adg_clear_cairo_path((AdgEdges
*) trail
);
445 if (data
->source
!= NULL
) {
448 /* The threshold is squared because the _adg_get_vertices()
449 * function uses cpml_pair_squared_distance() against the
450 * two vectors of every corner to avoid sqrt()ing everything */
451 threshold
= sin(data
->critical_angle
);
452 threshold
*= threshold
* 2;
455 for (n
= 1; adg_trail_put_segment(data
->source
, n
, &segment
); ++ n
) {
456 vertices
= _adg_get_vertices(vertices
, &segment
, threshold
);
459 /* Rotate all the vertices so the axis will always be on y=0:
460 * this is mainly needed to not complicate the _adg_path_build()
461 * code which assumes the y=0 axis is in effect */
462 cairo_matrix_init_rotate(&map
, -data
->axis_angle
);
463 g_slist_foreach(vertices
, (GFunc
) cpml_pair_transform
, &map
);
465 vertices
= _adg_optimize_vertices(vertices
);
466 data
->cairo
.array
= _adg_path_build(vertices
);
468 g_slist_foreach(vertices
, (GFunc
) g_free
, NULL
);
469 g_slist_free(vertices
);
471 /* Reapply the inverse of the previous transformation to
472 * move the vertices to their original positions */
473 cairo_matrix_invert(&map
);
474 _adg_path_transform(data
->cairo
.array
, &map
);
476 data
->cairo
.path
.status
= CAIRO_STATUS_SUCCESS
;
477 data
->cairo
.path
.data
= (cairo_path_data_t
*) (data
->cairo
.array
)->data
;
478 data
->cairo
.path
.num_data
= (data
->cairo
.array
)->len
;
481 return &data
->cairo
.path
;
485 _adg_unset_source(AdgEdges
*edges
)
487 g_object_set(edges
, "source", NULL
, NULL
);
491 _adg_clear_cairo_path(AdgEdges
*edges
)
493 AdgEdgesPrivate
*data
= edges
->data
;
495 if (data
->cairo
.array
!= NULL
) {
496 g_array_free(data
->cairo
.array
, TRUE
);
497 data
->cairo
.array
= NULL
;
500 data
->cairo
.path
.status
= CAIRO_STATUS_INVALID_PATH_DATA
;
501 data
->cairo
.path
.data
= NULL
;
502 data
->cairo
.path
.num_data
= 0;
507 * @vertices: a #GSList
508 * @segment: a #CpmlSegment
509 * @threshold: a theshold value
511 * Collects a list of #CpmlPair corners where the angle has a minimum
512 * threshold incidence of @threshold. The threshold is considered as
513 * the squared distance between the two unit vectors, the one before
514 * and the one after every corner.
516 * Returns: the original #GSList with new vertices appended.
521 _adg_get_vertices(GSList
*vertices
, CpmlSegment
*segment
, gdouble threshold
)
523 CpmlPrimitive primitive
;
527 cpml_primitive_from_segment(&primitive
, segment
);
528 /* The first vector starts undefined, so it will always be
529 * included (the squared distance between any vector and an
530 * undefined vector will always be greater than threshold) */
534 cpml_vector_set_length(&old
, 1);
535 cpml_primitive_put_vector_at(&primitive
, 0, &new);
536 cpml_vector_set_length(&new, 1);
538 /* Vertical vectors are always added, as they represent
539 * a vertical side and could be filleted, thus skipping
540 * the edge detection */
542 cpml_pair_squared_distance(&old
, &new) > threshold
) {
543 cpml_primitive_put_pair_at(&primitive
, 0, &pair
);
544 vertices
= g_slist_append(vertices
, cpml_pair_dup(&pair
));
547 cpml_primitive_put_vector_at(&primitive
, 1, &old
);
548 } while (cpml_primitive_next(&primitive
));
553 /* Removes adjacent vertices lying on the same edge */
555 _adg_optimize_vertices(GSList
*vertices
)
557 GSList
*vertex
, *old_vertex
;
558 CpmlPair
*pair
, *old_pair
;
560 /* Check for empty list */
561 if (vertices
== NULL
)
564 old_vertex
= vertices
;
566 while ((vertex
= old_vertex
->next
) != NULL
) {
568 old_pair
= old_vertex
->data
;
570 if (pair
->x
!= old_pair
->x
) {
575 if (old_pair
->y
< pair
->y
) {
576 /* Preserve the old vertex and remove the current one */
578 vertices
= g_slist_delete_link(vertices
, vertex
);
580 /* Preserve the current vertex and remove the old one */
582 vertices
= g_slist_delete_link(vertices
, old_vertex
);
591 _adg_path_build(const GSList
*vertices
)
593 cairo_path_data_t line
[4];
595 const GSList
*vertex
, *vertex2
;
596 const CpmlPair
*pair
, *pair2
;
598 line
[0].header
.type
= CPML_MOVE
;
599 line
[0].header
.length
= 2;
600 line
[2].header
.type
= CPML_LINE
;
601 line
[2].header
.length
= 2;
603 array
= g_array_new(FALSE
, FALSE
, sizeof(cairo_path_data_t
));
606 while (vertex
!= NULL
) {
608 vertex
= vertex
->next
;
611 while (vertex2
!= NULL
) {
612 pair2
= vertex2
->data
;
614 if (pair
->x
== pair2
->x
) {
615 /* Opposite vertex found: append a line in the path
616 * and quit from this loop */
617 cpml_pair_to_cairo(pair
, &line
[1]);
618 cpml_pair_to_cairo(pair2
, &line
[3]);
619 array
= g_array_append_vals(array
, line
, G_N_ELEMENTS(line
));
623 vertex2
= vertex2
->next
;
631 _adg_path_transform(GArray
*path_data
, const cairo_matrix_t
*map
)
634 cairo_path_data_t
*data
;
636 /* Only the odd items are transformed: the even ones are either
637 * header items, CPML_MOVE and CPML_LINE alternatively */
638 for (n
= 1; n
< path_data
->len
; n
+= 2) {
639 data
= &g_array_index(path_data
, cairo_path_data_t
, n
);
640 cairo_matrix_transform_point(map
, &data
->point
.x
, &data
->point
.y
);