gimp/app/path_bezier.c

278 lines
6.5 KiB
C
Raw Normal View History

/* The GIMP -- an image manipulation program
*
* This file Copyright (C) 1999 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 <gtk/gtk.h>
#include "libgimpmath/gimpmath.h"
#include "core/core-types.h"
#include "tools/tools-types.h"
#include "path_curves.h"
#include "path_bezier.h"
#define HANDLE_HALFWIDTH 3
#define HANDLE_WIDTH 6
/*
* This function is to get a set of npoints different coordinates for
* the range from start to end (each in the range from 0 to 1 and
* start < end.
* returns the number of created coords. Make sure that the points-
* Array is allocated.
*/
guint
path_bezier_get_points (PathSegment *segment,
gfloat *points,
guint npoints,
gdouble start,
gdouble end)
{
return 0;
}
void
path_bezier_get_point (PathSegment *segment,
gdouble pos,
gdouble *x,
gdouble *y)
{
PathBezierData *data = (PathBezierData *) segment->data;
if (segment->next) {
*x = (1-pos) * (1-pos) * (1-pos) * segment->x
+ 3 * pos * (1-pos) * (1-pos) * (segment->x + data->x1)
+ 3 * pos * pos * (1-pos) * (segment->next->x + data->x2)
+ pos * pos * pos * (segment->next->x);
*y = (1-pos) * (1-pos) * (1-pos) * segment->y
+ 3 * pos * (1-pos) * (1-pos) * (segment->y + data->y1)
+ 3 * pos * pos * (1-pos) * (segment->next->y + data->y2)
+ pos * pos * pos * (segment->next->y);
}
#ifdef PATH_TOOL_DEBUG
else fprintf (stderr, "FIXME: path_bezier_get_point called with endpoint-segment!!!\n");
#endif
return;
}
void
path_bezier_draw_handles (GimpDrawTool *tool,
PathSegment *segment)
{
#if 0
PathTool *path_tool = (PathTool *) (tool->private);
PathBezierData *data = (PathBezierData *) segment->data;
Cleanup weekend... 2001-10-29 Michael Natterer <mitch@gimp.org> Cleanup weekend... * app/app_procs.c: pass "no_interface" to gimp_new(). * app/core/gimp.[ch]: added "gboolean no_interface" and the load_procs and save_procs GSLists. * app/core/gimptoolinfo.[ch]: added a "Gimp" pointer to the GimpToolInfo object so more functions find their context without accessing the global "the_gimp" variable. * app/display/display-types.h: removed the GDisplay -> GimpDisplay typedef. * app/display/gimpdisplay.c: look at gimp->no_interface, don't include "appenv.h". * app/file/file-open.[ch] * app/file/file-save.[ch]: don't use "the_gimp" any more. Instead, pass around lots of "Gimp" pointers. Removed the global load_procs and save_procs variables here. Use access() to find out whether a file is readable/writable, removed the manual voodoo and it's Win32 wrappers. Added an optional (can be NULL) "PlunInProcDef" parameter to file_save(), removed file_save_with_proc(). * app/gui/menus.c: Use the unused "gpointer data" parameter of the GtkItemFactory callbacks to pass a "Gimp" pointer to all of them. This reduces the usage of the global "the_gimp" hack to zero in app/gui/... yeah. * app/gui/channels-commands.c * app/gui/edit-commands.c * app/gui/file-commands.c * app/gui/image-commands.c * app/gui/layers-commands.c * app/gui/palettes-commands.c * app/gui/select-commands.c * app/gui/test-commands.c * app/gui/tools-commands.c * app/gui/view-commands.c: use the passed "Gimp" pointer. * app/gui/color-area.[ch] * app/gui/convert-dialog.c * app/gui/dialogs-constructors.c * app/gui/file-new-dialog.[ch] * app/gui/file-open-dialog.[ch] * app/gui/file-save-dialog.[ch] * app/gui/gui.c * app/gui/info-window.[ch] * app/gui/module-browser.[ch] * app/gui/palette-editor.c * app/gui/palette-import-dialog.[ch] * app/gui/paths-dialog.c * app/gui/preferences-dialog.[ch] * app/gui/resize-dialog.[ch] * app/gui/tool-options-dialog.[ch] * app/gui/toolbox.c: pass around lots more "Gimp" and "GimpContext" pointers and don't use "the_gimp" any more. * app/tools/gimptool.h: added a pointer to the corresponding GimpToolInfo object (which in turn has a pointer to a Gimp). * app/tools/tool_manager.[ch]: set the pointer after creating the tool object. Removed tool_manager_get_info_by_tool() as there is a tool->tool_info pointer now. * app/tools/gimpbezierselecttool.c * app/tools/gimpblendtool.c * app/tools/gimpbrightnesscontrasttool.c * app/tools/gimpclonetool.c * app/tools/gimpcolorbalancetool.c * app/tools/gimpcolorpickertool.c * app/tools/gimpconvolvetool.c * app/tools/gimpcroptool.c * app/tools/gimpcurvestool.c * app/tools/gimpdodgeburntool.c * app/tools/gimpdrawtool.c * app/tools/gimpeditselectiontool.c * app/tools/gimpellipseselecttool.c * app/tools/gimperasertool.c * app/tools/gimpfliptool.c * app/tools/gimphistogramtool.c * app/tools/gimphuesaturationtool.c * app/tools/gimpinktool.c * app/tools/gimplevelstool.c * app/tools/gimpmagnifytool.c * app/tools/gimpmeasuretool.c * app/tools/gimpmovetool.c * app/tools/gimppainttool.c * app/tools/gimppathtool.c * app/tools/gimpperspectivetool.c * app/tools/gimpposterizetool.c * app/tools/gimprectselecttool.c * app/tools/gimprotatetool.c * app/tools/gimpscaletool.c * app/tools/gimpsheartool.c * app/tools/gimptexttool.c * app/tools/gimpthresholdtool.c * app/tools/path_tool.c * app/tools/xinput_airbrush.c: s/GDisplay/GimpDisplay/g. Use tool->tool_info and tool_info->gimp in some places to get rid of using "the_gimp". Removing the remaining ones involves changing the tool options system and is scheduled next... * app/widgets/gimpdnd.c * app/widgets/gimpdocumentview.c: pass a "Gimp" pointer to all file_open_*() functions. * app/gdisplay_color.[ch] * app/gdisplay_color_ui.[ch] * app/image_map.[ch] * app/nav_window.[ch] * app/path.c * app/path_bezier.c * app/path_transform.h * app/qmask.[ch]: s/GDisplay/GimpDisplay/g * tools/pdbgen/pdb/fileops.pdb: load_procs and save_procs are members of the "Gimp" object now. * tools/pdbgen/pdb/plug_in.pdb: use gimp->no_interface, don't include "appenv.h". * app/pdb/fileops_cmds.c * app/pdb/plug_in_cmds.c: regenerated.
2001-10-29 19:47:11 +08:00
GimpDisplay * gdisp = tool->gdisp;
gint sx, sy, hx, hy;
if (segment->next) {
if (segment->flags & SEGMENT_ACTIVE) {
gdisplay_transform_coords (gdisp,
(gint) (segment->x), (gint) (segment->y), &sx, &sy, FALSE);
gdisplay_transform_coords (gdisp,
(gint) (segment->x + data->x1),
(gint) (segment->y + data->y1), &hx, &hy, FALSE);
gdk_draw_line (path_tool->core->win,
path_tool->core->gc, hx, hy, sx, sy);
gdk_draw_rectangle (path_tool->core->win, path_tool->core->gc, 0,
hx - HANDLE_HALFWIDTH, hy - HANDLE_HALFWIDTH,
HANDLE_WIDTH, HANDLE_WIDTH);
}
if (segment->next->flags & SEGMENT_ACTIVE) {
gdisplay_transform_coords (gdisp,
(gint) (segment->next->x), (gint) (segment->next->y), &sx, &sy, FALSE);
gdisplay_transform_coords (gdisp,
(gint) (segment->next->x + data->x2),
(gint) (segment->next->y + data->y2), &hx, &hy, FALSE);
gdk_draw_line (path_tool->core->win,
path_tool->core->gc, hx, hy, sx, sy);
gdk_draw_rectangle (path_tool->core->win, path_tool->core->gc, 0,
hx - HANDLE_HALFWIDTH, hy - HANDLE_HALFWIDTH,
HANDLE_WIDTH, HANDLE_WIDTH);
}
}
#endif
}
void
path_bezier_draw_segment (GimpDrawTool *tool,
PathSegment *segment)
{
return;
}
gdouble
path_bezier_on_segment (PathSegment *segment,
gint x,
gint y,
gint halfwidth,
gint *distance)
{
return -1;
}
void
path_bezier_drag_segment (PathSegment *segment,
gdouble pos,
gdouble dx,
gdouble dy)
{
PathBezierData *data = (PathBezierData *) segment->data;
gdouble feel_good;
if (pos <= 0.5)
feel_good = (pow(2 * pos, 3)) / 2;
else
feel_good = (1 - pow((1-pos)*2, 3)) / 2 + 0.5;
data->x1 += (dx / (3*pos*(1-pos)*(1-pos)))*(1-feel_good);
data->y1 += (dy / (3*pos*(1-pos)*(1-pos)))*(1-feel_good);
data->x2 += (dx / (3*pos*pos*(1-pos)))*feel_good;
data->y2 += (dy / (3*pos*pos*(1-pos)))*feel_good;
return;
}
gint
path_bezier_on_handles (PathSegment *segment,
gdouble x,
gdouble y,
gdouble halfwidth)
{
PathBezierData *data = (PathBezierData *) segment->data;
if (segment->flags & SEGMENT_ACTIVE &&
fabs(segment->x + data->x1 - x) <= halfwidth &&
fabs(segment->y + data->y1 - y) <= halfwidth)
return 1;
if (segment->next && segment->next->flags & SEGMENT_ACTIVE &&
fabs(segment->next->x + data->x2 - x) <= halfwidth &&
fabs(segment->next->y + data->y2 - y) <= halfwidth)
return 2;
return 0;
}
void
path_bezier_drag_handles (PathSegment *segment,
gdouble dx,
gdouble dy,
gint handle_id)
{
PathBezierData *data = (PathBezierData *) segment->data;
if (handle_id == 1) {
data->x1 += dx;
data->y1 += dy;
} else {
data->x2 += dx;
data->y2 += dy;
}
}
PathSegment *
path_bezier_insert_anchor (PathSegment *segment,
gdouble position)
{
return NULL;
}
void
path_bezier_update_segment (PathSegment *segment)
{
return;
}
void
path_bezier_flip_segment (PathSegment *segment)
{
PathBezierData *data = (PathBezierData *) segment->data;
gdouble swap;
swap = data->x1;
data->x1 = data->x2;
data->x2 = swap;
swap = data->y1;
data->y1 = data->y2;
data->y2 = swap;
return;
}
void
path_bezier_init_segment (PathSegment *segment)
{
PathBezierData *data, *neardata;
data = g_new(PathBezierData, 1);
data->x1 = 0;
data->y1 = 0;
data->x2 = 0;
data->y2 = 0;
if (segment->prev && segment->prev->type == SEGMENT_BEZIER) {
neardata = (PathBezierData *) segment->prev->data;
data->x1 = - neardata->x2;
data->y1 = - neardata->y2;
}
if (segment->next && segment->next->type == SEGMENT_BEZIER) {
neardata = (PathBezierData *) segment->next->data;
data->x2 = - neardata->x1;
data->y2 = - neardata->y1;
}
#ifdef PATH_TOOL_DEBUG
if (segment->data)
fprintf(stderr, "Warning: path_bezier_init_segment called with already initialized segment\n");
#endif
segment->data = data;
return;
}
void
path_bezier_cleanup_segment (PathSegment *segment)
{
g_free(segment->data);
segment->data = NULL;
return;
}