[gimp/gimp-2-10] libgimpmath: add various GimpMatrix2 functions



commit d546e3b8137042da365cd324a3c73642376e5c17
Author: Ell <ell_se yahoo com>
Date:   Mon Jan 6 16:10:32 2020 +0200

    libgimpmath: add various GimpMatrix2 functions
    
    Add gimp_matrix2_{determinant,invert,transform_point}().

 devel-docs/libgimpmath/libgimpmath-sections.txt |  3 ++
 libgimpmath/gimpmatrix.c                        | 60 +++++++++++++++++++++++++
 libgimpmath/gimpmatrix.h                        |  9 ++++
 3 files changed, 72 insertions(+)
---
diff --git a/devel-docs/libgimpmath/libgimpmath-sections.txt b/devel-docs/libgimpmath/libgimpmath-sections.txt
index 2001c0360d..70a1b245ed 100644
--- a/devel-docs/libgimpmath/libgimpmath-sections.txt
+++ b/devel-docs/libgimpmath/libgimpmath-sections.txt
@@ -26,6 +26,9 @@ GimpMatrix3
 GimpMatrix4
 gimp_matrix2_identity
 gimp_matrix2_mult
+gimp_matrix2_determinant
+gimp_matrix2_invert
+gimp_matrix2_transform_point
 gimp_matrix3_identity
 gimp_matrix3_mult
 gimp_matrix3_translate
diff --git a/libgimpmath/gimpmatrix.c b/libgimpmath/gimpmatrix.c
index 4e058ded48..3fc477c7c1 100644
--- a/libgimpmath/gimpmatrix.c
+++ b/libgimpmath/gimpmatrix.c
@@ -264,6 +264,66 @@ gimp_matrix2_mult (const GimpMatrix2 *matrix1,
   *matrix2 = tmp;
 }
 
+/**
+ * gimp_matrix2_determinant:
+ * @matrix: The input matrix.
+ *
+ * Calculates the determinant of the given matrix.
+ *
+ * Returns: The determinant.
+ */
+
+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.
+ */
+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.
+ * @newx: The transformed X coordinate.
+ * @newy: The transformed Y coordinate.
+ *
+ * Transforms a point in 2D as specified by the transformation matrix.
+ */
+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;
+}
+
 
 static GimpMatrix3 * matrix3_copy                  (const GimpMatrix3 *matrix);
 
diff --git a/libgimpmath/gimpmatrix.h b/libgimpmath/gimpmatrix.h
index 7d5e95e9c9..6acc32c9a7 100644
--- a/libgimpmath/gimpmatrix.h
+++ b/libgimpmath/gimpmatrix.h
@@ -57,6 +57,15 @@ void          gimp_matrix2_identity        (GimpMatrix2       *matrix);
 void          gimp_matrix2_mult            (const GimpMatrix2 *matrix1,
                                             GimpMatrix2       *matrix2);
 
+gdouble       gimp_matrix2_determinant     (const GimpMatrix2 *matrix);
+void          gimp_matrix2_invert          (GimpMatrix2       *matrix);
+
+void          gimp_matrix2_transform_point (const GimpMatrix2 *matrix,
+                                            gdouble            x,
+                                            gdouble            y,
+                                            gdouble           *newx,
+                                            gdouble           *newy);
+
 
 /*****************/
 /*  GimpMatrix3  */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]