2003-09-01 05:47:26 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2004-05-25 00:45:24 +08:00
|
|
|
* gimpcoords.c
|
2003-09-01 05:47:26 +08:00
|
|
|
* 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>
|
|
|
|
|
2004-05-25 00:45:24 +08:00
|
|
|
#include "core-types.h"
|
|
|
|
|
|
|
|
#include "gimpcoords.h"
|
2003-09-01 05:47:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
#define INPUT_RESOLUTION 256
|
|
|
|
|
2004-05-25 00:45:24 +08:00
|
|
|
|
2003-09-01 05:47:26 +08:00
|
|
|
/* amul * a + bmul * b = ret_val */
|
|
|
|
|
|
|
|
void
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_mix (const gdouble amul,
|
|
|
|
const GimpCoords *a,
|
|
|
|
const gdouble bmul,
|
|
|
|
const GimpCoords *b,
|
|
|
|
GimpCoords *ret_val)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
|
|
|
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
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_average (const GimpCoords *a,
|
|
|
|
const GimpCoords *b,
|
|
|
|
GimpCoords *ret_average)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_mix (0.5, a, 0.5, b, ret_average);
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* a + b = ret_add */
|
|
|
|
|
|
|
|
void
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_add (const GimpCoords *a,
|
|
|
|
const GimpCoords *b,
|
|
|
|
GimpCoords *ret_add)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_mix (1.0, a, 1.0, b, ret_add);
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* a - b = ret_difference */
|
|
|
|
|
|
|
|
void
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_difference (const GimpCoords *a,
|
|
|
|
const GimpCoords *b,
|
|
|
|
GimpCoords *ret_difference)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_mix (1.0, a, -1.0, b, ret_difference);
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* a * f = ret_product */
|
|
|
|
|
|
|
|
void
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_scale (const gdouble f,
|
|
|
|
const GimpCoords *a,
|
|
|
|
GimpCoords *ret_product)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_mix (f, a, 0.0, NULL, ret_product);
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* local helper for measuring the scalarproduct of two gimpcoords. */
|
|
|
|
|
|
|
|
gdouble
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_scalarprod (const GimpCoords *a,
|
|
|
|
const GimpCoords *b)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
|
|
|
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
|
2004-06-09 20:44:13 +08:00
|
|
|
gimp_coords_length_squared (const GimpCoords *a)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2004-05-25 00:45:24 +08:00
|
|
|
return gimp_coords_scalarprod (&upscaled_a, &upscaled_a);
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gdouble
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_length (const GimpCoords *a)
|
2003-09-01 05:47:26 +08:00
|
|
|
{
|
2004-06-09 20:44:13 +08:00
|
|
|
return sqrt (gimp_coords_length_squared (a));
|
2003-09-01 05:47:26 +08:00
|
|
|
}
|
2003-09-01 08:39:47 +08:00
|
|
|
|
2005-10-01 08:49:44 +08:00
|
|
|
/*
|
|
|
|
* Distance via manhattan metric, an upper bound for the eucledian metric.
|
|
|
|
* used for e.g. bezier approximation
|
|
|
|
*/
|
|
|
|
|
|
|
|
gdouble
|
|
|
|
gimp_coords_manhattan_dist (const GimpCoords *a,
|
|
|
|
const GimpCoords *b)
|
|
|
|
{
|
|
|
|
gdouble dist = 0;
|
|
|
|
|
|
|
|
dist += fabs (a->pressure - b->pressure);
|
|
|
|
dist += fabs (a->xtilt - b->xtilt);
|
|
|
|
dist += fabs (a->ytilt - b->ytilt);
|
|
|
|
dist += fabs (a->wheel - b->wheel);
|
|
|
|
|
|
|
|
dist *= INPUT_RESOLUTION;
|
|
|
|
|
|
|
|
dist += fabs (a->x - b->x);
|
|
|
|
dist += fabs (a->y - b->y);
|
|
|
|
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
2003-09-01 08:39:47 +08:00
|
|
|
gboolean
|
2004-05-25 00:45:24 +08:00
|
|
|
gimp_coords_equal (const GimpCoords *a,
|
|
|
|
const GimpCoords *b)
|
2003-09-01 08:39:47 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|