Commit 835d7ab7 authored by Simon Budig's avatar Simon Budig Committed by Simon Budig

Implemented moveto/lineto/ conicto/curveto-Interface. Moved helper

2003-08-31  Simon Budig  <simon@gimp.org>

	* app/vectors/gimpbezierstroke.[ch]: Implemented moveto/lineto/
	conicto/curveto-Interface.
	Moved helper functions to calculate with GimpCoords into...

	* app/vectors/gimpcoordmath.[ch]: ...these new files.

	* app/vectors/Makefile.am: Changed accordingly.
parent dad6fedb
2003-08-31 Simon Budig <simon@gimp.org>
* app/vectors/gimpbezierstroke.[ch]: Implemented moveto/lineto/
conicto/curveto-Interface.
Moved helper functions to calculate with GimpCoords into...
* app/vectors/gimpcoordmath.[ch]: ...these new files.
* app/vectors/Makefile.am: Changed accordingly.
2003-08-31 Sven Neumann <sven@gimp.org>
* plug-ins/common/bz2.c: make it behave more like the gzip plug-in
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpcoordmath.c
* Copyright (C) 2002 Simon Budig <simon@gimp.org>
*
* 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 <glib-object.h>
#include "vectors-types.h"
#include "gimpcoordmath.h"
#define INPUT_RESOLUTION 256
/* amul * a + bmul * b = ret_val */
void
gimp_bezier_coords_mix (const gdouble amul,
const GimpCoords *a,
const gdouble bmul,
const GimpCoords *b,
GimpCoords *ret_val)
{
if (b)
{
ret_val->x = amul * a->x + bmul * b->x ;
ret_val->y = amul * a->y + bmul * b->y ;
ret_val->pressure = amul * a->pressure + bmul * b->pressure ;
ret_val->xtilt = amul * a->xtilt + bmul * b->xtilt ;
ret_val->ytilt = amul * a->ytilt + bmul * b->ytilt ;
ret_val->wheel = amul * a->wheel + bmul * b->wheel ;
}
else
{
ret_val->x = amul * a->x;
ret_val->y = amul * a->y;
ret_val->pressure = amul * a->pressure;
ret_val->xtilt = amul * a->xtilt;
ret_val->ytilt = amul * a->ytilt;
ret_val->wheel = amul * a->wheel;
}
}
/* (a+b)/2 = ret_average */
void
gimp_bezier_coords_average (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_average)
{
gimp_bezier_coords_mix (0.5, a, 0.5, b, ret_average);
}
/* a + b = ret_add */
void
gimp_bezier_coords_add (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_add)
{
gimp_bezier_coords_mix (1.0, a, 1.0, b, ret_add);
}
/* a - b = ret_difference */
void
gimp_bezier_coords_difference (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_difference)
{
gimp_bezier_coords_mix (1.0, a, -1.0, b, ret_difference);
}
/* a * f = ret_product */
void
gimp_bezier_coords_scale (const gdouble f,
const GimpCoords *a,
GimpCoords *ret_product)
{
gimp_bezier_coords_mix (f, a, 0.0, NULL, ret_product);
}
/* local helper for measuring the scalarproduct of two gimpcoords. */
gdouble
gimp_bezier_coords_scalarprod (const GimpCoords *a,
const GimpCoords *b)
{
return (a->x * b->x +
a->y * b->y +
a->pressure * b->pressure +
a->xtilt * b->xtilt +
a->ytilt * b->ytilt +
a->wheel * b->wheel );
}
/*
* The "lenght" of the gimpcoord.
* Applies a metric that increases the weight on the
* pressure/xtilt/ytilt/wheel to ensure proper interpolation
*/
gdouble
gimp_bezier_coords_length2 (const GimpCoords *a)
{
GimpCoords upscaled_a;
upscaled_a.x = a->x;
upscaled_a.y = a->y;
upscaled_a.pressure = a->pressure * INPUT_RESOLUTION;
upscaled_a.xtilt = a->xtilt * INPUT_RESOLUTION;
upscaled_a.ytilt = a->ytilt * INPUT_RESOLUTION;
upscaled_a.wheel = a->wheel * INPUT_RESOLUTION;
return gimp_bezier_coords_scalarprod (&upscaled_a, &upscaled_a);
}
gdouble
gimp_bezier_coords_length (const GimpCoords *a)
{
return sqrt (gimp_bezier_coords_length2 (a));
}
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpcoordmath.h
* Copyright (C) 2002 Simon Budig <simon@gimp.org>
*
* 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.
*/
void gimp_bezier_coords_mix (const gdouble amul,
const GimpCoords *a,
const gdouble bmul,
const GimpCoords *b,
GimpCoords *ret_val);
void gimp_bezier_coords_average (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_average);
void gimp_bezier_coords_add (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_add);
void gimp_bezier_coords_difference (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *difference);
void gimp_bezier_coords_scale (const gdouble f,
const GimpCoords *a,
GimpCoords *ret_product);
gdouble gimp_bezier_coords_scalarprod (const GimpCoords *a,
const GimpCoords *b);
gdouble gimp_bezier_coords_length (const GimpCoords *a);
gdouble gimp_bezier_coords_length2 (const GimpCoords *a);
......@@ -19,6 +19,8 @@ libappvectors_a_SOURCES = \
gimpanchor.h \
gimpbezierstroke.h \
gimpbezierstroke.c \
gimpcoordmath.h \
gimpcoordmath.c \
gimpstroke.h \
gimpstroke.c \
gimpvectors.c \
......
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpstroke.c
* gimpbezierstroke.c
* Copyright (C) 2002 Simon Budig <simon@gimp.org>
*
* This program is free software; you can redistribute it and/or modify
......@@ -28,8 +28,7 @@
#include "gimpanchor.h"
#include "gimpbezierstroke.h"
#define INPUT_RESOLUTION 256
#include "gimpcoordmath.h"
/* local prototypes */
......@@ -104,23 +103,6 @@ static void gimp_bezier_stroke_finalize (GObject *object);
static GList * gimp_bezier_stroke_get_anchor_listitem (GList *list);
static void gimp_bezier_coords_mix (const gdouble amul,
const GimpCoords *a,
const gdouble bmul,
const GimpCoords *b,
GimpCoords *ret_val);
static void gimp_bezier_coords_average (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_average);
static void gimp_bezier_coords_add (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_add);
static void gimp_bezier_coords_difference (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *difference);
static void gimp_bezier_coords_scale (const gdouble f,
const GimpCoords *a,
GimpCoords *ret_product);
static void gimp_bezier_coords_subdivide (const GimpCoords *beziercoords,
const gdouble precision,
GArray **ret_coords);
......@@ -129,10 +111,6 @@ static void gimp_bezier_coords_subdivide2 (const GimpCoords *beziercoords,
GArray **ret_coords,
gint depth);
static gdouble gimp_bezier_coords_scalarprod (const GimpCoords *a,
const GimpCoords *b);
static gdouble gimp_bezier_coords_length (const GimpCoords *a);
static gdouble gimp_bezier_coords_length2 (const GimpCoords *a);
static gboolean gimp_bezier_coords_is_straight (const GimpCoords *beziercoords,
const gdouble precision);
......@@ -1310,144 +1288,118 @@ gimp_bezier_stroke_interpolate (const GimpStroke *stroke,
return ret_coords;
}
static GList *
gimp_bezier_stroke_get_anchor_listitem (GList *list)
GimpStroke *
gimp_bezier_stroke_new_moveto (const GimpCoords *start)
{
if (!list)
return NULL;
if (GIMP_ANCHOR (list->data)->type == GIMP_ANCHOR_ANCHOR)
return list;
if (list->prev && GIMP_ANCHOR (list->prev->data)->type == GIMP_ANCHOR_ANCHOR)
return list->prev;
if (list->next && GIMP_ANCHOR (list->next->data)->type == GIMP_ANCHOR_ANCHOR)
return list->next;
g_return_val_if_fail (/* bezier stroke inconsistent! */ FALSE, NULL);
return NULL;
}
/* local helper functions for bezier subdivision */
GimpStroke *stroke;
/* amul * a + bmul * b */
stroke = gimp_bezier_stroke_new ();
static void
gimp_bezier_coords_mix (const gdouble amul,
const GimpCoords *a,
const gdouble bmul,
const GimpCoords *b,
GimpCoords *ret_val)
{
if (b)
{
ret_val->x = amul * a->x + bmul * b->x ;
ret_val->y = amul * a->y + bmul * b->y ;
ret_val->pressure = amul * a->pressure + bmul * b->pressure ;
ret_val->xtilt = amul * a->xtilt + bmul * b->xtilt ;
ret_val->ytilt = amul * a->ytilt + bmul * b->ytilt ;
ret_val->wheel = amul * a->wheel + bmul * b->wheel ;
}
else
{
ret_val->x = amul * a->x;
ret_val->y = amul * a->y;
ret_val->pressure = amul * a->pressure;
ret_val->xtilt = amul * a->xtilt;
ret_val->ytilt = amul * a->ytilt;
ret_val->wheel = amul * a->wheel;
}
gimp_bezier_stroke_extend (stroke, start,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, start,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, start,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
return stroke;
}
/* (a+b)/2 */
static void
gimp_bezier_coords_average (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_average)
void
gimp_bezier_stroke_lineto (GimpStroke *stroke,
const GimpCoords *end)
{
gimp_bezier_coords_mix (0.5, a, 0.5, b, ret_average);
g_return_if_fail (GIMP_IS_BEZIER_STROKE (stroke));
g_return_if_fail (stroke->closed == FALSE);
g_return_if_fail (stroke->anchors != NULL);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
}
/* a + b */
static void
gimp_bezier_coords_add (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_add)
void
gimp_bezier_stroke_conicto (GimpStroke *stroke,
const GimpCoords *control,
const GimpCoords *end)
{
gimp_bezier_coords_mix (1.0, a, 1.0, b, ret_add);
}
GimpCoords start, coords;
/* a - b */
g_return_if_fail (GIMP_IS_BEZIER_STROKE (stroke));
g_return_if_fail (stroke->closed == FALSE);
g_return_if_fail (stroke->anchors != NULL);
g_return_if_fail (stroke->anchors->next != NULL);
static void
gimp_bezier_coords_difference (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_difference)
{
gimp_bezier_coords_mix (1.0, a, -1.0, b, ret_difference);
}
start = GIMP_ANCHOR (stroke->anchors->next->data)->position;
gimp_bezier_coords_mix (2.0 / 3.0, control, 1.0 / 3.0, &start, &coords);
/* a * f = ret_product */
GIMP_ANCHOR (stroke->anchors->data)->position = coords;
static void
gimp_bezier_coords_scale (const gdouble f,
const GimpCoords *a,
GimpCoords *ret_product)
{
gimp_bezier_coords_mix (f, a, 0.0, NULL, ret_product);
gimp_bezier_coords_mix (2.0 / 3.0, control, 1.0 / 3.0, end, &coords);
gimp_bezier_stroke_extend (stroke, &coords,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
}
/* local helper for measuring the scalarproduct of two gimpcoords. */
static gdouble
gimp_bezier_coords_scalarprod (const GimpCoords *a,
const GimpCoords *b)
void
gimp_bezier_stroke_cubicto (GimpStroke *stroke,
const GimpCoords *control1,
const GimpCoords *control2,
const GimpCoords *end)
{
return (a->x * b->x +
a->y * b->y +
a->pressure * b->pressure +
a->xtilt * b->xtilt +
a->ytilt * b->ytilt +
a->wheel * b->wheel );
g_return_if_fail (GIMP_IS_BEZIER_STROKE (stroke));
g_return_if_fail (stroke->closed == FALSE);
g_return_if_fail (stroke->anchors != NULL);
GIMP_ANCHOR (stroke->anchors->data)->position = *control1;
gimp_bezier_stroke_extend (stroke, control2,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
gimp_bezier_stroke_extend (stroke, end,
GIMP_ANCHOR (stroke->anchors->data),
EXTEND_SIMPLE);
}
/*
* The "lenght" of the gimpcoord.
* Applies a metric that increases the weight on the
* pressure/xtilt/ytilt/wheel to ensure proper interpolation
*/
/* helper function to get the associated anchor of a listitem */
static gdouble
gimp_bezier_coords_length2 (const GimpCoords *a)
static GList *
gimp_bezier_stroke_get_anchor_listitem (GList *list)
{
GimpCoords upscaled_a;
if (!list)
return NULL;
upscaled_a.x = a->x;
upscaled_a.y = a->y;
upscaled_a.pressure = a->pressure * INPUT_RESOLUTION;
upscaled_a.xtilt = a->xtilt * INPUT_RESOLUTION;
upscaled_a.ytilt = a->ytilt * INPUT_RESOLUTION;
upscaled_a.wheel = a->wheel * INPUT_RESOLUTION;
if (GIMP_ANCHOR (list->data)->type == GIMP_ANCHOR_ANCHOR)
return list;
return gimp_bezier_coords_scalarprod (&upscaled_a, &upscaled_a);
}
if (list->prev && GIMP_ANCHOR (list->prev->data)->type == GIMP_ANCHOR_ANCHOR)
return list->prev;
if (list->next && GIMP_ANCHOR (list->next->data)->type == GIMP_ANCHOR_ANCHOR)
return list->next;
static gdouble
gimp_bezier_coords_length (const GimpCoords *a)
{
return sqrt (gimp_bezier_coords_length2 (a));
g_return_val_if_fail (/* bezier stroke inconsistent! */ FALSE, NULL);
return NULL;
}
......@@ -1522,6 +1474,8 @@ gimp_bezier_coords_is_straight (const GimpCoords *beziercoords,
}
/* local helper functions for bezier subdivision */
static void
gimp_bezier_coords_subdivide (const GimpCoords *beziercoords,
const gdouble precision,
......
......@@ -54,6 +54,17 @@ GimpStroke * gimp_bezier_stroke_new_from_coords (const GimpCoords *coords,
gint n_coords,
gboolean closed);
GimpStroke * gimp_bezier_stroke_new_moveto (const GimpCoords *start);
void gimp_bezier_stroke_lineto (GimpStroke *bez_stroke,
const GimpCoords *end);
void gimp_bezier_stroke_conicto (GimpStroke *bez_stroke,
const GimpCoords *control,
const GimpCoords *end);
void gimp_bezier_stroke_cubicto (GimpStroke *bez_stroke,
const GimpCoords *control1,
const GimpCoords *control2,
const GimpCoords *end);
GimpAnchor * gimp_bezier_stroke_extend (GimpStroke *stroke,
const GimpCoords *coords,
GimpAnchor *neighbor,
......
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpcoordmath.c
* Copyright (C) 2002 Simon Budig <simon@gimp.org>
*
* 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 <glib-object.h>
#include "vectors-types.h"
#include "gimpcoordmath.h"
#define INPUT_RESOLUTION 256
/* amul * a + bmul * b = ret_val */
void
gimp_bezier_coords_mix (const gdouble amul,
const GimpCoords *a,
const gdouble bmul,
const GimpCoords *b,
GimpCoords *ret_val)
{
if (b)
{
ret_val->x = amul * a->x + bmul * b->x ;
ret_val->y = amul * a->y + bmul * b->y ;
ret_val->pressure = amul * a->pressure + bmul * b->pressure ;
ret_val->xtilt = amul * a->xtilt + bmul * b->xtilt ;
ret_val->ytilt = amul * a->ytilt + bmul * b->ytilt ;
ret_val->wheel = amul * a->wheel + bmul * b->wheel ;
}
else
{
ret_val->x = amul * a->x;
ret_val->y = amul * a->y;
ret_val->pressure = amul * a->pressure;
ret_val->xtilt = amul * a->xtilt;
ret_val->ytilt = amul * a->ytilt;
ret_val->wheel = amul * a->wheel;
}
}
/* (a+b)/2 = ret_average */
void
gimp_bezier_coords_average (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_average)
{
gimp_bezier_coords_mix (0.5, a, 0.5, b, ret_average);
}
/* a + b = ret_add */
void
gimp_bezier_coords_add (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_add)
{
gimp_bezier_coords_mix (1.0, a, 1.0, b, ret_add);
}
/* a - b = ret_difference */
void
gimp_bezier_coords_difference (const GimpCoords *a,
const GimpCoords *b,
GimpCoords *ret_difference)
{
gimp_bezier_coords_mix (1.0, a, -1.0, b, ret_difference);
}
/* a * f = ret_product */
void
gimp_bezier_coords_scale (const gdouble f,
const GimpCoords *a,
GimpCoords *ret_product)
{
gimp_bezier_coords_mix (f, a, 0.0, NULL, ret_product);
}
/* local helper for measuring the scalarproduct of two gimpcoords. */
gdouble
gimp_bezier_coords_scalarprod (const GimpCoords *a,
const GimpCoords *b)
{
return (a->x * b->x +
a->y *