2003-09-12 21:23:01 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
2000-02-26 11:41:06 +08:00
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpmatrix.c
|
2006-01-12 08:50:44 +08:00
|
|
|
* Copyright (C) 1998 Jay Cox <jaycox@gimp.org>
|
1998-10-14 10:54:02 +08:00
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
1999-11-18 05:13:50 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-10-14 10:54:02 +08:00
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
1998-10-14 10:54:02 +08:00
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
1999-11-18 05:13:50 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
1998-10-14 10:54:02 +08:00
|
|
|
*/
|
|
|
|
|
2002-05-14 07:30:23 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-01-22 08:43:31 +08:00
|
|
|
#include <glib-object.h>
|
2000-05-31 07:38:46 +08:00
|
|
|
|
2000-02-15 00:29:41 +08:00
|
|
|
#include "gimpmath.h"
|
1999-07-09 22:51:01 +08:00
|
|
|
|
2000-05-31 07:38:46 +08:00
|
|
|
|
2010-07-02 20:41:35 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpmatrix
|
|
|
|
* @title: GimpMatrix
|
|
|
|
* @short_description: Utilities to set up and manipulate 3x3
|
|
|
|
* transformation matrices.
|
|
|
|
* @see_also: #GimpVector2, #GimpVector3, #GimpVector4
|
|
|
|
*
|
|
|
|
* When doing image manipulation you will often need 3x3
|
|
|
|
* transformation matrices that define translation, rotation, scaling,
|
|
|
|
* shearing and arbitrary perspective transformations using a 3x3
|
|
|
|
* matrix. Here you'll find a set of utility functions to set up those
|
|
|
|
* matrices and to perform basic matrix manipulations and tests.
|
|
|
|
*
|
|
|
|
* Each matrix class has a 2 dimensional gdouble coeff member. The
|
|
|
|
* element for row r and column c of the matrix is coeff[r][c].
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
1999-07-09 22:51:01 +08:00
|
|
|
#define EPSILON 1e-6
|
1998-10-14 10:54:02 +08:00
|
|
|
|
2003-07-08 02:36:00 +08:00
|
|
|
|
2005-01-22 08:43:31 +08:00
|
|
|
static GimpMatrix2 * matrix2_copy (const GimpMatrix2 *matrix);
|
|
|
|
|
2019-07-17 13:04:59 +08:00
|
|
|
G_DEFINE_BOXED_TYPE (GimpMatrix2, gimp_matrix2, matrix2_copy, g_free)
|
2005-01-22 08:43:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GIMP_TYPE_PARAM_MATRIX2
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GIMP_PARAM_SPEC_MATRIX2(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_MATRIX2, GimpParamSpecMatrix2))
|
|
|
|
|
|
|
|
static void gimp_param_matrix2_class_init (GParamSpecClass *class);
|
|
|
|
static void gimp_param_matrix2_init (GParamSpec *pspec);
|
|
|
|
static void gimp_param_matrix2_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value);
|
|
|
|
static gint gimp_param_matrix2_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2);
|
|
|
|
|
|
|
|
typedef struct _GimpParamSpecMatrix2 GimpParamSpecMatrix2;
|
|
|
|
|
|
|
|
struct _GimpParamSpecMatrix2
|
|
|
|
{
|
|
|
|
GParamSpecBoxed parent_instance;
|
|
|
|
|
|
|
|
GimpMatrix2 default_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_matrix2_get_type:
|
|
|
|
*
|
|
|
|
* Reveals the object type
|
|
|
|
*
|
|
|
|
* Returns: the #GType for a GimpMatrix2 object
|
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2005-01-22 08:43:31 +08:00
|
|
|
**/
|
|
|
|
GType
|
|
|
|
gimp_param_matrix2_get_type (void)
|
|
|
|
{
|
|
|
|
static GType spec_type = 0;
|
|
|
|
|
|
|
|
if (!spec_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo type_info =
|
|
|
|
{
|
|
|
|
sizeof (GParamSpecClass),
|
|
|
|
NULL, NULL,
|
|
|
|
(GClassInitFunc) gimp_param_matrix2_class_init,
|
|
|
|
NULL, NULL,
|
|
|
|
sizeof (GimpParamSpecMatrix2),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gimp_param_matrix2_init
|
|
|
|
};
|
|
|
|
|
|
|
|
spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
|
|
|
|
"GimpParamMatrix2",
|
|
|
|
&type_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix2_class_init (GParamSpecClass *class)
|
|
|
|
{
|
|
|
|
class->value_type = GIMP_TYPE_MATRIX2;
|
|
|
|
class->value_set_default = gimp_param_matrix2_set_default;
|
|
|
|
class->values_cmp = gimp_param_matrix2_values_cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix2_init (GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);
|
|
|
|
|
|
|
|
gimp_matrix2_identity (&cspec->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix2_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix2 *cspec = GIMP_PARAM_SPEC_MATRIX2 (pspec);
|
|
|
|
|
|
|
|
g_value_set_static_boxed (value, &cspec->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gimp_param_matrix2_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2)
|
|
|
|
{
|
|
|
|
GimpMatrix2 *matrix1;
|
|
|
|
GimpMatrix2 *matrix2;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
matrix1 = value1->data[0].v_pointer;
|
|
|
|
matrix2 = value2->data[0].v_pointer;
|
|
|
|
|
|
|
|
/* try to return at least *something*, it's useless anyway... */
|
|
|
|
|
|
|
|
if (! matrix1)
|
|
|
|
return matrix2 != NULL ? -1 : 0;
|
|
|
|
else if (! matrix2)
|
|
|
|
return matrix1 != NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
for (j = 0; j < 2; j++)
|
|
|
|
if (matrix1->coeff[i][j] != matrix2->coeff[i][j])
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_spec_matrix2:
|
|
|
|
* @name: Canonical name of the param
|
|
|
|
* @nick: Nickname of the param
|
2016-06-26 04:54:10 +08:00
|
|
|
* @blurb: Brief description of param.
|
2005-01-22 08:43:31 +08:00
|
|
|
* @default_value: Value to use if none is assigned.
|
|
|
|
* @flags: a combination of #GParamFlags
|
|
|
|
*
|
|
|
|
* Creates a param spec to hold a #GimpMatrix2 value.
|
|
|
|
* See g_param_spec_internal() for more information.
|
|
|
|
*
|
2019-08-01 07:52:07 +08:00
|
|
|
* Returns: (transfer full): a newly allocated #GParamSpec instance
|
2005-01-22 08:43:31 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2005-01-22 08:43:31 +08:00
|
|
|
**/
|
|
|
|
GParamSpec *
|
|
|
|
gimp_param_spec_matrix2 (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
const GimpMatrix2 *default_value,
|
|
|
|
GParamFlags flags)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix2 *cspec;
|
|
|
|
|
|
|
|
g_return_val_if_fail (default_value != NULL, NULL);
|
|
|
|
|
|
|
|
cspec = g_param_spec_internal (GIMP_TYPE_PARAM_MATRIX2,
|
|
|
|
name, nick, blurb, flags);
|
|
|
|
|
|
|
|
cspec->default_value = *default_value;
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (cspec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GimpMatrix2 *
|
|
|
|
matrix2_copy (const GimpMatrix2 *matrix)
|
|
|
|
{
|
2021-08-26 23:18:32 +08:00
|
|
|
return (GimpMatrix2 *) g_memdup2 (matrix, sizeof (GimpMatrix2));
|
2005-01-22 08:43:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-08 00:22:45 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix2_identity:
|
|
|
|
* @matrix: A matrix.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2003-07-08 00:22:45 +08:00
|
|
|
* Sets the matrix to the identity matrix.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_matrix2_identity (GimpMatrix2 *matrix)
|
|
|
|
{
|
|
|
|
static const GimpMatrix2 identity = { { { 1.0, 0.0 },
|
|
|
|
{ 0.0, 1.0 } } };
|
|
|
|
|
|
|
|
*matrix = identity;
|
|
|
|
}
|
|
|
|
|
2003-07-08 02:36:00 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix2_mult:
|
2023-03-25 13:06:38 +08:00
|
|
|
* @left: The first input matrix.
|
|
|
|
* @right: The second input matrix which will be overwritten by the result.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2003-07-08 02:36:00 +08:00
|
|
|
* Multiplies two matrices and puts the result into the second one.
|
|
|
|
*/
|
|
|
|
void
|
2023-03-25 13:06:38 +08:00
|
|
|
gimp_matrix2_mult (const GimpMatrix2 *left,
|
|
|
|
GimpMatrix2 *right)
|
2003-07-08 02:36:00 +08:00
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
gdouble r00 = right->coeff[0][0];
|
|
|
|
gdouble r01 = right->coeff[0][1];
|
2003-07-08 02:36:00 +08:00
|
|
|
|
2023-03-25 13:06:38 +08:00
|
|
|
right->coeff[0][0] = (left->coeff[0][0] * r00 +
|
|
|
|
left->coeff[0][1] * right->coeff[1][0]);
|
|
|
|
right->coeff[0][1] = (left->coeff[0][0] * r01 +
|
|
|
|
left->coeff[0][1] * right->coeff[1][1]);
|
|
|
|
right->coeff[1][0] = (left->coeff[1][0] * r00 +
|
|
|
|
left->coeff[1][1] * right->coeff[1][0]);
|
|
|
|
right->coeff[1][1] = (left->coeff[1][0] * r01 +
|
|
|
|
left->coeff[1][1] * right->coeff[1][1]);
|
2003-07-08 02:36:00 +08:00
|
|
|
}
|
|
|
|
|
2020-01-06 22:10:32 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix2_determinant:
|
|
|
|
* @matrix: The input matrix.
|
|
|
|
*
|
|
|
|
* Calculates the determinant of the given matrix.
|
|
|
|
*
|
|
|
|
* Returns: The determinant.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:10:32 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
gdouble
|
|
|
|
gimp_matrix2_determinant (const GimpMatrix2 *matrix)
|
|
|
|
{
|
|
|
|
return matrix->coeff[0][0] * matrix->coeff[1][1] -
|
|
|
|
matrix->coeff[0][1] * matrix->coeff[1][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_matrix2_invert:
|
|
|
|
* @matrix: The matrix that is to be inverted.
|
|
|
|
*
|
|
|
|
* Inverts the given matrix.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:10:32 +08:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_matrix2_invert (GimpMatrix2 *matrix)
|
|
|
|
{
|
|
|
|
gdouble det = gimp_matrix2_determinant (matrix);
|
|
|
|
gdouble temp;
|
|
|
|
|
|
|
|
if (fabs (det) <= EPSILON)
|
|
|
|
return;
|
|
|
|
|
|
|
|
temp = matrix->coeff[0][0];
|
|
|
|
|
|
|
|
matrix->coeff[0][0] = matrix->coeff[1][1] / det;
|
|
|
|
matrix->coeff[0][1] /= -det;
|
|
|
|
matrix->coeff[1][0] /= -det;
|
|
|
|
matrix->coeff[1][1] = temp / det;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_matrix2_transform_point:
|
|
|
|
* @matrix: The transformation matrix.
|
|
|
|
* @x: The source X coordinate.
|
|
|
|
* @y: The source Y coordinate.
|
2020-05-04 08:10:55 +08:00
|
|
|
* @newx: (out): The transformed X coordinate.
|
|
|
|
* @newy: (out): The transformed Y coordinate.
|
2020-01-06 22:10:32 +08:00
|
|
|
*
|
|
|
|
* Transforms a point in 2D as specified by the transformation matrix.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:10:32 +08:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_matrix2_transform_point (const GimpMatrix2 *matrix,
|
|
|
|
gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gdouble *newx,
|
|
|
|
gdouble *newy)
|
|
|
|
{
|
|
|
|
*newx = matrix->coeff[0][0] * x + matrix->coeff[0][1] * y;
|
|
|
|
*newy = matrix->coeff[1][0] * x + matrix->coeff[1][1] * y;
|
|
|
|
}
|
|
|
|
|
2011-03-28 03:13:31 +08:00
|
|
|
|
|
|
|
static GimpMatrix3 * matrix3_copy (const GimpMatrix3 *matrix);
|
|
|
|
|
2019-07-17 13:04:59 +08:00
|
|
|
G_DEFINE_BOXED_TYPE (GimpMatrix3, gimp_matrix3, matrix3_copy, g_free)
|
2011-03-28 03:13:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GIMP_TYPE_PARAM_MATRIX3
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define GIMP_PARAM_SPEC_MATRIX3(pspec) (G_TYPE_CHECK_INSTANCE_CAST ((pspec), GIMP_TYPE_PARAM_MATRIX3, GimpParamSpecMatrix3))
|
|
|
|
|
|
|
|
static void gimp_param_matrix3_class_init (GParamSpecClass *class);
|
|
|
|
static void gimp_param_matrix3_init (GParamSpec *pspec);
|
|
|
|
static void gimp_param_matrix3_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value);
|
|
|
|
static gint gimp_param_matrix3_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2);
|
|
|
|
|
|
|
|
typedef struct _GimpParamSpecMatrix3 GimpParamSpecMatrix3;
|
|
|
|
|
|
|
|
struct _GimpParamSpecMatrix3
|
|
|
|
{
|
|
|
|
GParamSpecBoxed parent_instance;
|
|
|
|
|
|
|
|
GimpMatrix3 default_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_matrix3_get_type:
|
|
|
|
*
|
|
|
|
* Reveals the object type
|
|
|
|
*
|
|
|
|
* Returns: the #GType for a GimpMatrix3 object
|
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.8
|
2011-03-28 03:13:31 +08:00
|
|
|
**/
|
|
|
|
GType
|
|
|
|
gimp_param_matrix3_get_type (void)
|
|
|
|
{
|
|
|
|
static GType spec_type = 0;
|
|
|
|
|
|
|
|
if (!spec_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo type_info =
|
|
|
|
{
|
|
|
|
sizeof (GParamSpecClass),
|
|
|
|
NULL, NULL,
|
|
|
|
(GClassInitFunc) gimp_param_matrix3_class_init,
|
|
|
|
NULL, NULL,
|
|
|
|
sizeof (GimpParamSpecMatrix3),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gimp_param_matrix3_init
|
|
|
|
};
|
|
|
|
|
|
|
|
spec_type = g_type_register_static (G_TYPE_PARAM_BOXED,
|
|
|
|
"GimpParamMatrix3",
|
|
|
|
&type_info, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix3_class_init (GParamSpecClass *class)
|
|
|
|
{
|
|
|
|
class->value_type = GIMP_TYPE_MATRIX3;
|
|
|
|
class->value_set_default = gimp_param_matrix3_set_default;
|
|
|
|
class->values_cmp = gimp_param_matrix3_values_cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix3_init (GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix3 *cspec = GIMP_PARAM_SPEC_MATRIX3 (pspec);
|
|
|
|
|
|
|
|
gimp_matrix3_identity (&cspec->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_param_matrix3_set_default (GParamSpec *pspec,
|
|
|
|
GValue *value)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix3 *cspec = GIMP_PARAM_SPEC_MATRIX3 (pspec);
|
|
|
|
|
|
|
|
g_value_set_static_boxed (value, &cspec->default_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gimp_param_matrix3_values_cmp (GParamSpec *pspec,
|
|
|
|
const GValue *value1,
|
|
|
|
const GValue *value2)
|
|
|
|
{
|
|
|
|
GimpMatrix3 *matrix1;
|
|
|
|
GimpMatrix3 *matrix2;
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
matrix1 = value1->data[0].v_pointer;
|
|
|
|
matrix2 = value2->data[0].v_pointer;
|
|
|
|
|
|
|
|
/* try to return at least *something*, it's useless anyway... */
|
|
|
|
|
|
|
|
if (! matrix1)
|
|
|
|
return matrix2 != NULL ? -1 : 0;
|
|
|
|
else if (! matrix2)
|
|
|
|
return matrix1 != NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
if (matrix1->coeff[i][j] != matrix2->coeff[i][j])
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_param_spec_matrix3:
|
|
|
|
* @name: Canonical name of the param
|
|
|
|
* @nick: Nickname of the param
|
2016-06-26 04:54:10 +08:00
|
|
|
* @blurb: Brief description of param.
|
2011-03-28 03:13:31 +08:00
|
|
|
* @default_value: Value to use if none is assigned.
|
|
|
|
* @flags: a combination of #GParamFlags
|
|
|
|
*
|
|
|
|
* Creates a param spec to hold a #GimpMatrix3 value.
|
|
|
|
* See g_param_spec_internal() for more information.
|
|
|
|
*
|
2019-08-01 07:52:07 +08:00
|
|
|
* Returns: (transfer full): a newly allocated #GParamSpec instance
|
2011-03-28 03:13:31 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.8
|
2011-03-28 03:13:31 +08:00
|
|
|
**/
|
|
|
|
GParamSpec *
|
|
|
|
gimp_param_spec_matrix3 (const gchar *name,
|
|
|
|
const gchar *nick,
|
|
|
|
const gchar *blurb,
|
|
|
|
const GimpMatrix3 *default_value,
|
|
|
|
GParamFlags flags)
|
|
|
|
{
|
|
|
|
GimpParamSpecMatrix3 *cspec;
|
|
|
|
|
|
|
|
cspec = g_param_spec_internal (GIMP_TYPE_PARAM_MATRIX3,
|
|
|
|
name, nick, blurb, flags);
|
|
|
|
|
|
|
|
if (default_value)
|
|
|
|
cspec->default_value = *default_value;
|
|
|
|
|
|
|
|
return G_PARAM_SPEC (cspec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GimpMatrix3 *
|
|
|
|
matrix3_copy (const GimpMatrix3 *matrix)
|
|
|
|
{
|
2021-08-26 23:18:32 +08:00
|
|
|
return (GimpMatrix3 *) g_memdup2 (matrix, sizeof (GimpMatrix3));
|
2011-03-28 03:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-08 02:36:00 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix3_identity:
|
|
|
|
* @matrix: A matrix.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2003-07-08 02:36:00 +08:00
|
|
|
* Sets the matrix to the identity matrix.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_matrix3_identity (GimpMatrix3 *matrix)
|
|
|
|
{
|
|
|
|
static const GimpMatrix3 identity = { { { 1.0, 0.0, 0.0 },
|
|
|
|
{ 0.0, 1.0, 0.0 },
|
|
|
|
{ 0.0, 0.0, 1.0 } } };
|
|
|
|
|
|
|
|
*matrix = identity;
|
|
|
|
}
|
2000-02-07 06:45:55 +08:00
|
|
|
|
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_transform_point:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The transformation matrix.
|
|
|
|
* @x: The source X coordinate.
|
|
|
|
* @y: The source Y coordinate.
|
|
|
|
* @newx: The transformed X coordinate.
|
|
|
|
* @newy: The transformed Y coordinate.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Transforms a point in 2D as specified by the transformation matrix.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_transform_point (const GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gdouble *newx,
|
|
|
|
gdouble *newy)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
gdouble w;
|
2000-02-07 06:45:55 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
w = matrix->coeff[2][0] * x + matrix->coeff[2][1] * y + matrix->coeff[2][2];
|
1998-10-14 10:54:02 +08:00
|
|
|
|
|
|
|
if (w == 0.0)
|
|
|
|
w = 1.0;
|
|
|
|
else
|
|
|
|
w = 1.0/w;
|
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
*newx = (matrix->coeff[0][0] * x +
|
|
|
|
matrix->coeff[0][1] * y +
|
|
|
|
matrix->coeff[0][2]) * w;
|
|
|
|
*newy = (matrix->coeff[1][0] * x +
|
|
|
|
matrix->coeff[1][1] * y +
|
|
|
|
matrix->coeff[1][2]) * w;
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_mult:
|
2023-03-25 13:06:38 +08:00
|
|
|
* @left: The first input matrix.
|
|
|
|
* @right: The second input matrix which will be overwritten by the result.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Multiplies two matrices and puts the result into the second one.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2023-03-25 13:06:38 +08:00
|
|
|
gimp_matrix3_mult (const GimpMatrix3 *left,
|
|
|
|
GimpMatrix3 *right)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gdouble li0, li1, li2;
|
|
|
|
gdouble r00 = right->coeff[0][0];
|
|
|
|
gdouble r01 = right->coeff[0][1];
|
|
|
|
gdouble r02 = right->coeff[0][2];
|
|
|
|
gdouble r10 = right->coeff[1][0];
|
|
|
|
gdouble r11 = right->coeff[1][1];
|
|
|
|
gdouble r12 = right->coeff[1][2];
|
|
|
|
gdouble r20 = right->coeff[2][0];
|
|
|
|
gdouble r21 = right->coeff[2][1];
|
|
|
|
gdouble r22 = right->coeff[2][2];
|
1998-10-14 10:54:02 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
li0 = left->coeff[i][0];
|
|
|
|
li1 = left->coeff[i][1];
|
|
|
|
li2 = left->coeff[i][2];
|
|
|
|
right->coeff[i][0] = li0 * r00 + li1 * r10 + li2 * r20;
|
|
|
|
right->coeff[i][1] = li0 * r01 + li1 * r11 + li2 * r21;
|
|
|
|
right->coeff[i][2] = li0 * r02 + li1 * r12 + li2 * r22;
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_translate:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be translated.
|
|
|
|
* @x: Translation in X direction.
|
|
|
|
* @y: Translation in Y direction.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Translates the matrix by x and y.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_translate (GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2000-02-07 06:45:55 +08:00
|
|
|
gdouble g, h, i;
|
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
g = matrix->coeff[2][0];
|
|
|
|
h = matrix->coeff[2][1];
|
|
|
|
i = matrix->coeff[2][2];
|
2000-02-07 06:45:55 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
matrix->coeff[0][0] += x * g;
|
|
|
|
matrix->coeff[0][1] += x * h;
|
|
|
|
matrix->coeff[0][2] += x * i;
|
|
|
|
matrix->coeff[1][0] += y * g;
|
|
|
|
matrix->coeff[1][1] += y * h;
|
|
|
|
matrix->coeff[1][2] += y * i;
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_scale:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be scaled.
|
|
|
|
* @x: X scale factor.
|
|
|
|
* @y: Y scale factor.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
|
|
|
* Scales the matrix by x and y
|
2000-02-07 06:45:55 +08:00
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_scale (GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
matrix->coeff[0][0] *= x;
|
|
|
|
matrix->coeff[0][1] *= x;
|
|
|
|
matrix->coeff[0][2] *= x;
|
1998-10-14 10:54:02 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
matrix->coeff[1][0] *= y;
|
|
|
|
matrix->coeff[1][1] *= y;
|
|
|
|
matrix->coeff[1][2] *= y;
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_rotate:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be rotated.
|
|
|
|
* @theta: The angle of rotation (in radians).
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Rotates the matrix by theta degrees.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_rotate (GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble theta)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2000-02-07 06:45:55 +08:00
|
|
|
gdouble t1, t2;
|
|
|
|
gdouble cost, sint;
|
1998-10-14 10:54:02 +08:00
|
|
|
|
2000-02-15 00:29:41 +08:00
|
|
|
cost = cos (theta);
|
|
|
|
sint = sin (theta);
|
2003-09-12 21:23:01 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
t1 = matrix->coeff[0][0];
|
|
|
|
t2 = matrix->coeff[1][0];
|
|
|
|
matrix->coeff[0][0] = cost * t1 - sint * t2;
|
|
|
|
matrix->coeff[1][0] = sint * t1 + cost * t2;
|
|
|
|
|
|
|
|
t1 = matrix->coeff[0][1];
|
|
|
|
t2 = matrix->coeff[1][1];
|
|
|
|
matrix->coeff[0][1] = cost * t1 - sint * t2;
|
|
|
|
matrix->coeff[1][1] = sint * t1 + cost * t2;
|
|
|
|
|
|
|
|
t1 = matrix->coeff[0][2];
|
|
|
|
t2 = matrix->coeff[1][2];
|
|
|
|
matrix->coeff[0][2] = cost * t1 - sint * t2;
|
|
|
|
matrix->coeff[1][2] = sint * t1 + cost * t2;
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_xshear:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be sheared.
|
|
|
|
* @amount: X shear amount.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Shears the matrix in the X direction.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_xshear (GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble amount)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
matrix->coeff[0][0] += amount * matrix->coeff[1][0];
|
|
|
|
matrix->coeff[0][1] += amount * matrix->coeff[1][1];
|
|
|
|
matrix->coeff[0][2] += amount * matrix->coeff[1][2];
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_yshear:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be sheared.
|
|
|
|
* @amount: Y shear amount.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Shears the matrix in the Y direction.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-09-12 21:23:01 +08:00
|
|
|
gimp_matrix3_yshear (GimpMatrix3 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble amount)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
matrix->coeff[1][0] += amount * matrix->coeff[0][0];
|
|
|
|
matrix->coeff[1][1] += amount * matrix->coeff[0][1];
|
|
|
|
matrix->coeff[1][2] += amount * matrix->coeff[0][2];
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2003-09-12 21:23:01 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix3_affine:
|
|
|
|
* @matrix: The input matrix.
|
2010-07-02 20:48:24 +08:00
|
|
|
* @a: the 'a' coefficient
|
|
|
|
* @b: the 'b' coefficient
|
|
|
|
* @c: the 'c' coefficient
|
|
|
|
* @d: the 'd' coefficient
|
|
|
|
* @e: the 'e' coefficient
|
|
|
|
* @f: the 'f' coefficient
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
|
|
|
* Applies the affine transformation given by six values to @matrix.
|
|
|
|
* The six values form define an affine transformation matrix as
|
|
|
|
* illustrated below:
|
|
|
|
*
|
|
|
|
* ( a c e )
|
|
|
|
* ( b d f )
|
|
|
|
* ( 0 0 1 )
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_matrix3_affine (GimpMatrix3 *matrix,
|
|
|
|
gdouble a,
|
|
|
|
gdouble b,
|
|
|
|
gdouble c,
|
|
|
|
gdouble d,
|
|
|
|
gdouble e,
|
|
|
|
gdouble f)
|
|
|
|
{
|
|
|
|
GimpMatrix3 affine;
|
|
|
|
|
|
|
|
affine.coeff[0][0] = a;
|
|
|
|
affine.coeff[1][0] = b;
|
|
|
|
affine.coeff[2][0] = 0.0;
|
|
|
|
|
|
|
|
affine.coeff[0][1] = c;
|
|
|
|
affine.coeff[1][1] = d;
|
|
|
|
affine.coeff[2][1] = 0.0;
|
|
|
|
|
|
|
|
affine.coeff[0][2] = e;
|
|
|
|
affine.coeff[1][2] = f;
|
|
|
|
affine.coeff[2][2] = 1.0;
|
|
|
|
|
|
|
|
gimp_matrix3_mult (&affine, matrix);
|
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_determinant:
|
2003-09-12 21:23:01 +08:00
|
|
|
* @matrix: The input matrix.
|
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Calculates the determinant of the given matrix.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Returns: The determinant.
|
|
|
|
*/
|
|
|
|
gdouble
|
2003-07-07 21:50:48 +08:00
|
|
|
gimp_matrix3_determinant (const GimpMatrix3 *matrix)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
gdouble m01 = matrix->coeff[0][1];
|
|
|
|
gdouble m02 = matrix->coeff[0][2];
|
|
|
|
gdouble m11 = matrix->coeff[1][1];
|
|
|
|
gdouble m12 = matrix->coeff[1][2];
|
|
|
|
gdouble m21 = matrix->coeff[2][1];
|
|
|
|
gdouble m22 = matrix->coeff[2][2];
|
|
|
|
return matrix->coeff[0][0] * (m11 * m22 - m12 * m21)
|
|
|
|
- matrix->coeff[1][0] * (m01 * m22 - m02 * m21)
|
|
|
|
+ matrix->coeff[2][0] * (m01 * m12 - m02 * m11);
|
1998-10-14 10:54:02 +08:00
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_invert:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be inverted.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Inverts the given matrix.
|
|
|
|
*/
|
1998-10-14 10:54:02 +08:00
|
|
|
void
|
2003-07-07 21:50:48 +08:00
|
|
|
gimp_matrix3_invert (GimpMatrix3 *matrix)
|
1998-10-14 10:54:02 +08:00
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
gdouble det;
|
|
|
|
gdouble m00 = matrix->coeff[0][0];
|
|
|
|
gdouble m01 = matrix->coeff[0][1];
|
|
|
|
gdouble m02 = matrix->coeff[0][2];
|
|
|
|
gdouble m10 = matrix->coeff[1][0];
|
|
|
|
gdouble m11 = matrix->coeff[1][1];
|
|
|
|
gdouble m12 = matrix->coeff[1][2];
|
|
|
|
gdouble m20 = matrix->coeff[2][0];
|
|
|
|
gdouble m21 = matrix->coeff[2][1];
|
|
|
|
gdouble m22 = matrix->coeff[2][2];
|
|
|
|
|
|
|
|
/* To avoid redundant access to the coefficients, inline the determinant
|
|
|
|
* formula.
|
|
|
|
*
|
|
|
|
* See: https://gitlab.gnome.org/GNOME/gimp/-/merge_requests/880#note_1727051
|
|
|
|
*/
|
|
|
|
det = m00 * (m11 * m22 - m12 * m21)
|
|
|
|
- m10 * (m01 * m22 - m02 * m21)
|
|
|
|
+ m20 * (m01 * m12 - m02 * m11);
|
1998-10-14 10:54:02 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
if (det == 0.0)
|
1998-10-14 10:54:02 +08:00
|
|
|
return;
|
2000-02-07 06:45:55 +08:00
|
|
|
|
2003-07-07 21:50:48 +08:00
|
|
|
det = 1.0 / det;
|
1998-10-14 10:54:02 +08:00
|
|
|
|
2023-03-25 13:06:38 +08:00
|
|
|
matrix->coeff[0][0] = (m11 * m22 - m12 * m21) * det;
|
|
|
|
matrix->coeff[1][0] = - (m10 * m22 - m12 * m20) * det;
|
|
|
|
matrix->coeff[2][0] = (m10 * m21 - m11 * m20) * det;
|
|
|
|
matrix->coeff[0][1] = - (m01 * m22 - m02 * m21) * det;
|
|
|
|
matrix->coeff[1][1] = (m00 * m22 - m02 * m20) * det;
|
|
|
|
matrix->coeff[2][1] = - (m00 * m21 - m01 * m20) * det;
|
|
|
|
matrix->coeff[0][2] = (m01 * m12 - m02 * m11) * det;
|
|
|
|
matrix->coeff[1][2] = - (m00 * m12 - m02 * m10) * det;
|
|
|
|
matrix->coeff[2][2] = (m00 * m11 - m01 * m10) * det;
|
1999-07-09 22:51:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* functions to test for matrix properties */
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2005-03-20 08:09:29 +08:00
|
|
|
* gimp_matrix3_is_identity:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be tested.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2005-03-20 08:09:29 +08:00
|
|
|
* Checks if the given matrix is the identity matrix.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2005-03-20 08:09:29 +08:00
|
|
|
* Returns: %TRUE if the matrix is the identity matrix, %FALSE otherwise
|
2000-02-07 06:45:55 +08:00
|
|
|
*/
|
|
|
|
gboolean
|
2005-03-20 08:09:29 +08:00
|
|
|
gimp_matrix3_is_identity (const GimpMatrix3 *matrix)
|
1999-07-09 22:51:01 +08:00
|
|
|
{
|
2000-02-07 06:45:55 +08:00
|
|
|
gint i, j;
|
1999-07-09 22:51:01 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < 3; j++)
|
2003-09-28 07:02:39 +08:00
|
|
|
{
|
2005-03-20 08:09:29 +08:00
|
|
|
if (i == j)
|
|
|
|
{
|
|
|
|
if (fabs (matrix->coeff[i][j] - 1.0) > EPSILON)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (fabs (matrix->coeff[i][j]) > EPSILON)
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-09-28 07:02:39 +08:00
|
|
|
}
|
1999-07-09 22:51:01 +08:00
|
|
|
}
|
2000-02-07 06:45:55 +08:00
|
|
|
|
1999-07-09 22:51:01 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-02-07 06:45:55 +08:00
|
|
|
/**
|
2005-03-20 08:09:29 +08:00
|
|
|
* gimp_matrix3_is_diagonal:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be tested.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2005-03-20 08:09:29 +08:00
|
|
|
* Checks if the given matrix is diagonal.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2005-03-20 08:09:29 +08:00
|
|
|
* Returns: %TRUE if the matrix is diagonal, %FALSE otherwise
|
2000-02-07 06:45:55 +08:00
|
|
|
*/
|
|
|
|
gboolean
|
2005-03-20 08:09:29 +08:00
|
|
|
gimp_matrix3_is_diagonal (const GimpMatrix3 *matrix)
|
1999-07-09 22:51:01 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
gint i, j;
|
1999-07-09 22:51:01 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < 3; j++)
|
2003-09-28 07:02:39 +08:00
|
|
|
{
|
2005-03-20 08:09:29 +08:00
|
|
|
if (i != j && fabs (matrix->coeff[i][j]) > EPSILON)
|
|
|
|
return FALSE;
|
2003-09-28 07:02:39 +08:00
|
|
|
}
|
1999-07-09 22:51:01 +08:00
|
|
|
}
|
2000-02-07 06:45:55 +08:00
|
|
|
|
1999-07-09 22:51:01 +08:00
|
|
|
return TRUE;
|
1998-11-11 06:50:34 +08:00
|
|
|
}
|
1999-07-09 22:51:01 +08:00
|
|
|
|
2005-03-20 08:09:29 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix3_is_affine:
|
|
|
|
* @matrix: The matrix that is to be tested.
|
|
|
|
*
|
|
|
|
* Checks if the given matrix defines an affine transformation.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE if the matrix defines an affine transformation,
|
|
|
|
* %FALSE otherwise
|
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
1999-07-09 22:51:01 +08:00
|
|
|
*/
|
2005-03-20 08:09:29 +08:00
|
|
|
gboolean
|
|
|
|
gimp_matrix3_is_affine (const GimpMatrix3 *matrix)
|
|
|
|
{
|
|
|
|
return (fabs (matrix->coeff[2][0]) < EPSILON &&
|
|
|
|
fabs (matrix->coeff[2][1]) < EPSILON &&
|
|
|
|
fabs (matrix->coeff[2][2] - 1.0) < EPSILON);
|
|
|
|
}
|
2000-02-07 06:45:55 +08:00
|
|
|
|
|
|
|
/**
|
2000-02-15 00:29:41 +08:00
|
|
|
* gimp_matrix3_is_simple:
|
2000-02-07 06:45:55 +08:00
|
|
|
* @matrix: The matrix that is to be tested.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2000-02-07 06:45:55 +08:00
|
|
|
* Checks if we'll need to interpolate when applying this matrix as
|
|
|
|
* a transformation.
|
2003-09-12 21:23:01 +08:00
|
|
|
*
|
2005-03-20 08:09:29 +08:00
|
|
|
* Returns: %TRUE if all entries of the upper left 2x2 matrix are
|
|
|
|
* either 0 or 1, %FALSE otherwise
|
2000-02-07 06:45:55 +08:00
|
|
|
*/
|
|
|
|
gboolean
|
2003-07-07 21:50:48 +08:00
|
|
|
gimp_matrix3_is_simple (const GimpMatrix3 *matrix)
|
1999-07-09 22:51:01 +08:00
|
|
|
{
|
2000-02-07 06:45:55 +08:00
|
|
|
gdouble absm;
|
2003-07-07 21:50:48 +08:00
|
|
|
gint i, j;
|
1999-07-09 22:51:01 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < 2; j++)
|
2003-09-28 07:02:39 +08:00
|
|
|
{
|
|
|
|
absm = fabs (matrix->coeff[i][j]);
|
|
|
|
if (absm > EPSILON && fabs (absm - 1.0) > EPSILON)
|
|
|
|
return FALSE;
|
|
|
|
}
|
1999-07-09 22:51:01 +08:00
|
|
|
}
|
2000-02-07 06:45:55 +08:00
|
|
|
|
1999-07-09 22:51:01 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
2000-02-15 00:29:41 +08:00
|
|
|
|
2020-01-16 05:40:09 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix3_equal:
|
|
|
|
* @matrix1: The first matrix
|
|
|
|
* @matrix2: The second matrix
|
|
|
|
*
|
|
|
|
* Checks if two matrices are equal.
|
|
|
|
*
|
|
|
|
* Returns: %TRUE the matrices are equal, %FALSE otherwise
|
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gimp_matrix3_equal (const GimpMatrix3 *matrix1,
|
|
|
|
const GimpMatrix3 *matrix2)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
if (fabs (matrix1->coeff[i][j] - matrix2->coeff[i][j]) > EPSILON)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-06 22:12:11 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix4_identity:
|
|
|
|
* @matrix: A matrix.
|
|
|
|
*
|
|
|
|
* Sets the matrix to the identity matrix.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:12:11 +08:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_matrix4_identity (GimpMatrix4 *matrix)
|
|
|
|
{
|
|
|
|
gint i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
matrix->coeff[i][j] = i == j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_matrix4_mult:
|
2023-03-25 13:06:38 +08:00
|
|
|
* @left: The first input matrix.
|
|
|
|
* @right: The second input matrix which will be overwritten by the result.
|
2020-01-06 22:12:11 +08:00
|
|
|
*
|
|
|
|
* Multiplies two matrices and puts the result into the second one.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:12:11 +08:00
|
|
|
*/
|
|
|
|
void
|
2023-03-25 13:06:38 +08:00
|
|
|
gimp_matrix4_mult (const GimpMatrix4 *left,
|
|
|
|
GimpMatrix4 *right)
|
2020-01-06 22:12:11 +08:00
|
|
|
{
|
|
|
|
GimpMatrix4 result = {};
|
2023-03-25 13:06:38 +08:00
|
|
|
gint i, j;
|
|
|
|
gdouble li0, li1, li2, li3;
|
2020-01-06 22:12:11 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
li0 = left->coeff[i][0];
|
|
|
|
li1 = left->coeff[i][1];
|
|
|
|
li2 = left->coeff[i][2];
|
|
|
|
li3 = left->coeff[i][3];
|
2020-01-06 22:12:11 +08:00
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
{
|
2023-03-25 13:06:38 +08:00
|
|
|
result.coeff[i][j] = li0 * right->coeff[0][j]
|
|
|
|
+ li1 * right->coeff[1][j]
|
|
|
|
+ li2 * right->coeff[2][j]
|
|
|
|
+ li3 * right->coeff[3][j];
|
2020-01-06 22:12:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-25 13:06:38 +08:00
|
|
|
*right = result;
|
2020-01-06 22:12:11 +08:00
|
|
|
}
|
|
|
|
|
2003-11-15 23:30:34 +08:00
|
|
|
/**
|
|
|
|
* gimp_matrix4_to_deg:
|
|
|
|
* @matrix:
|
2020-05-04 08:10:55 +08:00
|
|
|
* @a: (out):
|
|
|
|
* @b: (out):
|
|
|
|
* @c: (out):
|
2003-11-15 23:30:34 +08:00
|
|
|
*
|
|
|
|
**/
|
2000-02-15 00:29:41 +08:00
|
|
|
void
|
2003-07-07 21:50:48 +08:00
|
|
|
gimp_matrix4_to_deg (const GimpMatrix4 *matrix,
|
2003-09-28 07:02:39 +08:00
|
|
|
gdouble *a,
|
|
|
|
gdouble *b,
|
|
|
|
gdouble *c)
|
2000-02-15 00:29:41 +08:00
|
|
|
{
|
2003-07-07 21:50:48 +08:00
|
|
|
*a = 180 * (asin (matrix->coeff[1][0]) / G_PI_2);
|
|
|
|
*b = 180 * (asin (matrix->coeff[2][0]) / G_PI_2);
|
|
|
|
*c = 180 * (asin (matrix->coeff[2][1]) / G_PI_2);
|
2000-02-15 00:29:41 +08:00
|
|
|
}
|
2020-01-06 22:12:11 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_matrix4_transform_point:
|
|
|
|
* @matrix: The transformation matrix.
|
|
|
|
* @x: The source X coordinate.
|
|
|
|
* @y: The source Y coordinate.
|
|
|
|
* @z: The source Z coordinate.
|
2020-05-04 08:10:55 +08:00
|
|
|
* @newx: (out): The transformed X coordinate.
|
|
|
|
* @newy: (out): The transformed Y coordinate.
|
|
|
|
* @newz: (out): The transformed Z coordinate.
|
2020-01-06 22:12:11 +08:00
|
|
|
*
|
|
|
|
* Transforms a point in 3D as specified by the transformation matrix.
|
2020-01-07 16:00:01 +08:00
|
|
|
*
|
|
|
|
* Returns: The transformed W coordinate.
|
2020-01-16 00:20:43 +08:00
|
|
|
*
|
|
|
|
* Since: 2.10.16
|
2020-01-06 22:12:11 +08:00
|
|
|
*/
|
|
|
|
gdouble
|
|
|
|
gimp_matrix4_transform_point (const GimpMatrix4 *matrix,
|
|
|
|
gdouble x,
|
|
|
|
gdouble y,
|
|
|
|
gdouble z,
|
|
|
|
gdouble *newx,
|
|
|
|
gdouble *newy,
|
|
|
|
gdouble *newz)
|
|
|
|
{
|
|
|
|
gdouble neww;
|
|
|
|
|
|
|
|
*newx = matrix->coeff[0][0] * x +
|
|
|
|
matrix->coeff[0][1] * y +
|
|
|
|
matrix->coeff[0][2] * z +
|
|
|
|
matrix->coeff[0][3];
|
|
|
|
*newy = matrix->coeff[1][0] * x +
|
|
|
|
matrix->coeff[1][1] * y +
|
|
|
|
matrix->coeff[1][2] * z +
|
|
|
|
matrix->coeff[1][3];
|
|
|
|
*newz = matrix->coeff[2][0] * x +
|
|
|
|
matrix->coeff[2][1] * y +
|
|
|
|
matrix->coeff[2][2] * z +
|
|
|
|
matrix->coeff[2][3];
|
|
|
|
neww = matrix->coeff[3][0] * x +
|
|
|
|
matrix->coeff[3][1] * y +
|
|
|
|
matrix->coeff[3][2] * z +
|
|
|
|
matrix->coeff[3][3];
|
|
|
|
|
|
|
|
*newx /= neww;
|
|
|
|
*newy /= neww;
|
|
|
|
*newz /= neww;
|
|
|
|
|
|
|
|
return neww;
|
|
|
|
}
|