[gimp] libgimpmath: add various GimpMatrix4 functions



commit e3bc877a426cf7982043e255e2589c3e46b3d3d9
Author: Ell <ell_se yahoo com>
Date:   Mon Jan 6 16:12:11 2020 +0200

    libgimpmath: add various GimpMatrix4 functions
    
    Add gimp_matrix4_{identity,mult,transform_point}().

 devel-docs/libgimpmath/libgimpmath3-sections.txt |  3 +
 libgimpmath/gimpmatrix.c                         | 91 ++++++++++++++++++++++++
 libgimpmath/gimpmatrix.h                         | 12 ++++
 3 files changed, 106 insertions(+)
---
diff --git a/devel-docs/libgimpmath/libgimpmath3-sections.txt 
b/devel-docs/libgimpmath/libgimpmath3-sections.txt
index 420e8c027b..f7a978de44 100644
--- a/devel-docs/libgimpmath/libgimpmath3-sections.txt
+++ b/devel-docs/libgimpmath/libgimpmath3-sections.txt
@@ -38,7 +38,10 @@ gimp_matrix3_is_identity
 gimp_matrix3_is_diagonal
 gimp_matrix3_is_affine
 gimp_matrix3_is_simple
+gimp_matrix4_identity
+gimp_matrix4_mult
 gimp_matrix4_to_deg
+gimp_matrix4_transform_point
 GIMP_TYPE_MATRIX2
 GIMP_TYPE_PARAM_MATRIX2
 GIMP_IS_PARAM_SPEC_MATRIX2
diff --git a/libgimpmath/gimpmatrix.c b/libgimpmath/gimpmatrix.c
index c3f0b5a1cf..7a4422df9f 100644
--- a/libgimpmath/gimpmatrix.c
+++ b/libgimpmath/gimpmatrix.c
@@ -885,6 +885,50 @@ gimp_matrix3_is_simple (const GimpMatrix3 *matrix)
   return TRUE;
 }
 
+/**
+ * gimp_matrix4_identity:
+ * @matrix: A matrix.
+ *
+ * Sets the matrix to the identity matrix.
+ */
+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:
+ * @matrix1: The first input matrix.
+ * @matrix2: The second input matrix which will be overwritten by the result.
+ *
+ * Multiplies two matrices and puts the result into the second one.
+ */
+void
+gimp_matrix4_mult (const GimpMatrix4 *matrix1,
+                   GimpMatrix4       *matrix2)
+{
+  GimpMatrix4 result = {};
+  gint        i, j, k;
+
+  for (i = 0; i < 4; i++)
+    {
+      for (j = 0; j < 4; j++)
+        {
+          for (k = 0; k < 4; k++)
+            result.coeff[i][j] += matrix1->coeff[i][k] * matrix2->coeff[k][j];
+        }
+    }
+
+  *matrix2 = result;
+}
+
 /**
  * gimp_matrix4_to_deg:
  * @matrix:
@@ -904,3 +948,50 @@ gimp_matrix4_to_deg (const GimpMatrix4 *matrix,
   *b = 180 * (asin (matrix->coeff[2][0]) / G_PI_2);
   *c = 180 * (asin (matrix->coeff[2][1]) / G_PI_2);
 }
+
+/**
+ * gimp_matrix4_transform_point:
+ * @matrix: The transformation matrix.
+ * @x: The source X coordinate.
+ * @y: The source Y coordinate.
+ * @z: The source Z coordinate.
+ * @newx: The transformed X coordinate.
+ * @newy: The transformed Y coordinate.
+ * @newz: The transformed Z coordinate.
+ *
+ * Transforms a point in 3D as specified by the transformation matrix.
+ */
+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;
+}
diff --git a/libgimpmath/gimpmatrix.h b/libgimpmath/gimpmatrix.h
index 6acc32c9a7..75fc908429 100644
--- a/libgimpmath/gimpmatrix.h
+++ b/libgimpmath/gimpmatrix.h
@@ -131,11 +131,23 @@ void          gimp_matrix3_transform_point (const GimpMatrix3 *matrix,
 /*  GimpMatrix4  */
 /*****************/
 
+void          gimp_matrix4_identity        (GimpMatrix4       *matrix);
+void          gimp_matrix4_mult            (const GimpMatrix4 *matrix1,
+                                            GimpMatrix4       *matrix2);
+
 void          gimp_matrix4_to_deg          (const GimpMatrix4 *matrix,
                                             gdouble           *a,
                                             gdouble           *b,
                                             gdouble           *c);
 
+gdouble       gimp_matrix4_transform_point (const GimpMatrix4 *matrix,
+                                            gdouble            x,
+                                            gdouble            y,
+                                            gdouble            z,
+                                            gdouble           *newx,
+                                            gdouble           *newy,
+                                            gdouble           *newz);
+
 
 G_END_DECLS
 


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