/* The GIMP -- an image manipulation program * Copyright (C) 1995 Spencer Kimball and Peter Mattis * * gimprectanglestroke.c * Copyright (C) 2004 Simon Budig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "config.h" #include #include "libgimpmath/gimpmath.h" #include "vectors-types.h" #include "core/gimpcoords.h" #include "gimpanchor.h" #include "gimprectanglestroke.h" /* local prototypes */ static void gimp_rectangle_stroke_class_init (GimpRectangleStrokeClass *klass); static void gimp_rectangle_stroke_init (GimpRectangleStroke *rectangle_stroke); static gdouble gimp_rectangle_stroke_nearest_point_get (const GimpStroke *stroke, const GimpCoords *coord, const gdouble precision, GimpCoords *ret_point, GimpAnchor **ret_segment_start, GimpAnchor **ret_segment_end, gdouble *ret_pos); static void gimp_rectangle_stroke_anchor_move_relative (GimpStroke *stroke, GimpAnchor *anchor, const GimpCoords *deltacoord, GimpAnchorFeatureType feature); static void gimp_rectangle_stroke_anchor_move_absolute (GimpStroke *stroke, GimpAnchor *anchor, const GimpCoords *coord, GimpAnchorFeatureType feature); static gboolean gimp_rectangle_stroke_point_is_movable (GimpStroke *stroke, GimpAnchor *predec, gdouble position); static void gimp_rectangle_stroke_point_move_relative (GimpStroke *stroke, GimpAnchor *predec, gdouble position, const GimpCoords *deltacoord, GimpAnchorFeatureType feature); static void gimp_rectangle_stroke_point_move_absolute (GimpStroke *stroke, GimpAnchor *predec, gdouble position, const GimpCoords *coord, GimpAnchorFeatureType feature); static gboolean gimp_rectangle_stroke_anchor_is_insertable (GimpStroke *stroke, GimpAnchor *predec, gdouble position); static gboolean gimp_rectangle_stroke_is_extendable (GimpStroke *stroke, GimpAnchor *neighbor); static GArray * gimp_rectangle_stroke_interpolate (const GimpStroke *stroke, const gdouble precision, gboolean *closed); static void gimp_rectangle_stroke_finalize (GObject *object); /* private variables */ static GimpStrokeClass *parent_class = NULL; GType gimp_rectangle_stroke_get_type (void) { static GType rectangle_stroke_type = 0; if (! rectangle_stroke_type) { static const GTypeInfo rectangle_stroke_info = { sizeof (GimpRectangleStrokeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gimp_rectangle_stroke_class_init, NULL, /* class_finalize */ NULL, /* class_data */ sizeof (GimpRectangleStroke), 0, /* n_preallocs */ (GInstanceInitFunc) gimp_rectangle_stroke_init, }; rectangle_stroke_type = g_type_register_static (GIMP_TYPE_STROKE, "GimpRectangleStroke", &rectangle_stroke_info, 0); } return rectangle_stroke_type; } static void gimp_rectangle_stroke_class_init (GimpRectangleStrokeClass *klass) { GObjectClass *object_class; GimpStrokeClass *stroke_class; object_class = G_OBJECT_CLASS (klass); stroke_class = GIMP_STROKE_CLASS (klass); parent_class = g_type_class_peek_parent (klass); object_class->finalize = gimp_rectangle_stroke_finalize; stroke_class->nearest_point_get = gimp_rectangle_stroke_nearest_point_get; stroke_class->anchor_move_relative = gimp_rectangle_stroke_anchor_move_relative; stroke_class->anchor_move_absolute = gimp_rectangle_stroke_anchor_move_absolute; stroke_class->anchor_is_insertable = gimp_rectangle_stroke_anchor_is_insertable; stroke_class->is_extendable = gimp_rectangle_stroke_is_extendable; stroke_class->interpolate = gimp_rectangle_stroke_interpolate; } static void gimp_rectangle_stroke_init (GimpRectangleStroke *bezier_stroke) { /* pass */ } static void gimp_rectangle_stroke_finalize (GObject *object) { G_OBJECT_CLASS (parent_class)->finalize (object); } /* Rectangle specific functions */ GimpStroke * gimp_rectangle_stroke_new (void) { GimpStroke *stroke; GimpAnchor *anchor; GimpCoords coords; gint count; stroke = g_object_new (GIMP_TYPE_RECTANGLE_STROKE, NULL); coords.x = 30; coords.y = 30; coords.pressure = 1.0; coords.xtilt = 0.5; coords.ytilt = 0.5; coords.wheel = 0.0; stroke->closed = TRUE; for (count = 0; count < 3; count++) { anchor = gimp_anchor_new (GIMP_ANCHOR_ANCHOR, &coords); stroke->anchors = g_list_append (stroke->anchors, anchor); } return stroke; } GimpStroke * gimp_rectangle_stroke_new_from_coords (const GimpCoords *coords, gint n_coords) { GimpRectangleStroke *rectangle_stroke; GimpStroke *stroke; GimpAnchor *anchor; gint count; g_return_val_if_fail (coords != NULL, NULL); g_return_val_if_fail (n_coords == 3, NULL); stroke = gimp_rectangle_stroke_new (); stroke->closed = TRUE; for (count = 0; count < 3; count++) { anchor = gimp_anchor_new (GIMP_ANCHOR_CONTROL, &coords[count]); stroke->anchors = g_list_append (stroke->anchors, anchor); } return stroke; } static gboolean gimp_rectangle_stroke_anchor_is_insertable (GimpStroke *stroke, GimpAnchor *predec, gdouble position) { g_return_val_if_fail (GIMP_IS_RECTANGLE_STROKE (stroke), FALSE); return FALSE; } static gboolean gimp_rectangle_stroke_point_is_movable (GimpStroke *stroke, GimpAnchor *predec, gdouble position) { g_return_val_if_fail (GIMP_IS_RECTANGLE_STROKE (stroke), FALSE); return TRUE; } static gboolean gimp_rectangle_stroke_is_extendable (GimpStroke *stroke, GimpAnchor *neighbor) { return FALSE; } static gdouble gimp_rectangle_stroke_nearest_point_get (const GimpStroke *stroke, const GimpCoords *coord, const gdouble precision, GimpCoords *ret_point, GimpAnchor **ret_segment_start, GimpAnchor **ret_segment_end, gdouble *ret_pos) { return 10000000.0; } static void gimp_rectangle_stroke_anchor_move_relative (GimpStroke *stroke, GimpAnchor *anchor, const GimpCoords *deltacoord, GimpAnchorFeatureType feature) { GimpCoords delta, coord1, coord2; GList *anchor_list; delta = *deltacoord; delta.pressure = 0; delta.xtilt = 0; delta.ytilt = 0; delta.wheel = 0; gimp_coords_add (&(anchor->position), &delta, &coord1); anchor->position = coord1; } static void gimp_rectangle_stroke_anchor_move_absolute (GimpStroke *stroke, GimpAnchor *anchor, const GimpCoords *coord, GimpAnchorFeatureType feature) { GimpCoords deltacoord; gimp_coords_difference (coord, &anchor->position, &deltacoord); gimp_rectangle_stroke_anchor_move_relative (stroke, anchor, &deltacoord, feature); } static GArray * gimp_rectangle_stroke_interpolate (const GimpStroke *stroke, gdouble precision, gboolean *ret_closed) { GArray *ret_coords; GimpAnchor *anchor; GList *anchorlist; GimpCoords rect_coord; GimpCoords x0, x1, x2; g_return_val_if_fail (GIMP_IS_RECTANGLE_STROKE (stroke), NULL); if (!stroke->anchors) { if (ret_closed) *ret_closed = FALSE; return NULL; } if (ret_closed) *ret_closed = TRUE; ret_coords = g_array_new (FALSE, FALSE, sizeof (GimpCoords)); anchorlist = stroke->anchors; x0 = GIMP_ANCHOR (anchorlist->data)->position; anchorlist = g_list_next (anchorlist); x1 = GIMP_ANCHOR (anchorlist->data)->position; anchorlist = g_list_next (anchorlist); x2 = GIMP_ANCHOR (anchorlist->data)->position; gimp_coords_difference (&x0, &x1, &x0); gimp_coords_difference (&x2, &x1, &x2); gimp_coords_mix (1.0, &x1, 1.0, &x0, &rect_coord); gimp_coords_mix (1.0, &rect_coord, 1.0, &x2, &rect_coord); ret_coords = g_array_append_val (ret_coords, rect_coord); gimp_coords_mix (1.0, &x1, 1.0, &x0, &rect_coord); gimp_coords_mix (1.0, &rect_coord, -1.0, &x2, &rect_coord); ret_coords = g_array_append_val (ret_coords, rect_coord); gimp_coords_mix (1.0, &x1, -1.0, &x0, &rect_coord); gimp_coords_mix (1.0, &rect_coord, -1.0, &x2, &rect_coord); ret_coords = g_array_append_val (ret_coords, rect_coord); gimp_coords_mix (1.0, &x1, -1.0, &x0, &rect_coord); gimp_coords_mix (1.0, &rect_coord, 1.0, &x2, &rect_coord); ret_coords = g_array_append_val (ret_coords, rect_coord); gimp_coords_mix (1.0, &x1, 1.0, &x0, &rect_coord); gimp_coords_mix (1.0, &rect_coord, 1.0, &x2, &rect_coord); ret_coords = g_array_append_val (ret_coords, rect_coord); return ret_coords; }