[gimp] libgimpmath: add various GimpMatrix2 functions



commit edfc837e7db353393e64d89adaebac580acbed79
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/libgimpmath3-sections.txt |  3 ++
 libgimpmath/gimpmatrix.c                         | 60 ++++++++++++++++++++++++
 libgimpmath/gimpmatrix.h                         |  9 ++++
 3 files changed, 72 insertions(+)
---
diff --git a/devel-docs/libgimpmath/libgimpmath3-sections.txt 
b/devel-docs/libgimpmath/libgimpmath3-sections.txt
index 378311c2e6..420e8c027b 100644
--- a/devel-docs/libgimpmath/libgimpmath3-sections.txt
+++ b/devel-docs/libgimpmath/libgimpmath3-sections.txt
@@ -20,6 +20,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 836978ab1c..c3f0b5a1cf 100644
--- a/libgimpmath/gimpmatrix.c
+++ b/libgimpmath/gimpmatrix.c
@@ -244,6 +244,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]