[mutter/gbsneto/graphene-matrix: 50/71] cogl/matrix: Move inverse calculation to cogl-graphene.c




commit cc63dab1f9f904de5bf58e40c68762adbcd661fe
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Sep 11 18:31:07 2020 -0300

    cogl/matrix: Move inverse calculation to cogl-graphene.c
    
    This special precision-bearing calculation will be used in other
    places, so better share them all here.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1439

 clutter/clutter/clutter-actor.c |  3 ++-
 clutter/clutter/clutter-stage.c |  4 ++--
 cogl/cogl/cogl-graphene.c       | 26 +++++++++++++++++++++++
 cogl/cogl/cogl-graphene.h       | 21 +++++++++++++++++++
 cogl/cogl/cogl-matrix-stack.c   |  5 +++--
 cogl/cogl/cogl-matrix.c         | 46 -----------------------------------------
 cogl/cogl/cogl-matrix.h         | 24 ---------------------
 7 files changed, 54 insertions(+), 75 deletions(-)
---
diff --git a/clutter/clutter/clutter-actor.c b/clutter/clutter/clutter-actor.c
index 2b7cf29f3c..95da5d2437 100644
--- a/clutter/clutter/clutter-actor.c
+++ b/clutter/clutter/clutter-actor.c
@@ -3162,7 +3162,8 @@ ensure_valid_actor_transform (ClutterActor *actor)
 
   CLUTTER_ACTOR_GET_CLASS (actor)->apply_transform (actor, &priv->transform);
   priv->has_inverse_transform =
-    cogl_matrix_get_inverse (&priv->transform, &priv->inverse_transform);
+    cogl_graphene_matrix_get_inverse (&priv->transform,
+                                      &priv->inverse_transform);
 
   priv->transform_valid = TRUE;
 }
diff --git a/clutter/clutter/clutter-stage.c b/clutter/clutter/clutter-stage.c
index ff68b40150..dd47c2f733 100644
--- a/clutter/clutter/clutter-stage.c
+++ b/clutter/clutter/clutter-stage.c
@@ -2070,8 +2070,8 @@ clutter_stage_set_perspective (ClutterStage       *stage,
                            priv->perspective.aspect,
                            priv->perspective.z_near,
                            priv->perspective.z_far);
-  cogl_matrix_get_inverse (&priv->projection,
-                           &priv->inverse_projection);
+  cogl_graphene_matrix_get_inverse (&priv->projection,
+                                    &priv->inverse_projection);
 
   _clutter_stage_dirty_projection (stage);
   clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
diff --git a/cogl/cogl/cogl-graphene.c b/cogl/cogl/cogl-graphene.c
index d9bd3bd42c..55be973d64 100644
--- a/cogl/cogl/cogl-graphene.c
+++ b/cogl/cogl/cogl-graphene.c
@@ -274,3 +274,29 @@ cogl_graphene_matrix_project_points (const graphene_matrix_t *matrix,
                          n_points);
     }
 }
+
+#define SCALE 1000.f
+
+gboolean
+cogl_graphene_matrix_get_inverse (const graphene_matrix_t *matrix,
+                                  graphene_matrix_t       *inverse)
+{
+  graphene_matrix_t scaled;
+  graphene_matrix_t m;
+  gboolean invertible;
+
+  graphene_matrix_init_scale (&scaled, SCALE, SCALE, SCALE);
+
+  // Float precision is a limiting factor
+  graphene_matrix_init_from_matrix (&m, matrix);
+  graphene_matrix_multiply (&m, &scaled, &m);
+
+  invertible = graphene_matrix_inverse (&m, inverse);
+
+  if (invertible)
+    graphene_matrix_multiply (&scaled, inverse, inverse);
+  else
+    graphene_matrix_init_identity (inverse);
+
+  return invertible;
+}
diff --git a/cogl/cogl/cogl-graphene.h b/cogl/cogl/cogl-graphene.h
index c355c8f92e..e1173cf860 100644
--- a/cogl/cogl/cogl-graphene.h
+++ b/cogl/cogl/cogl-graphene.h
@@ -162,5 +162,26 @@ cogl_graphene_matrix_project_points (const graphene_matrix_t *matrix,
                                      void                    *points_out,
                                      int                      n_points);
 
+/**
+ * cogl_graphene_matrix_get_inverse:
+ * @matrix: A 4x4 transformation matrix
+ * @inverse: (out): The destination for a 4x4 inverse transformation matrix
+ *
+ * Gets the inverse transform of a given matrix and uses it to initialize
+ * a new #graphene_matrix_t.
+ *
+ * <note>Although the first parameter is annotated as const to indicate
+ * that the transform it represents isn't modified this function may
+ * technically save a copy of the inverse transform within the given
+ * #graphene_matrix_t so that subsequent requests for the inverse transform may
+ * avoid costly inversion calculations.</note>
+ *
+ * Return value: %TRUE if the inverse was successfully calculated or %FALSE
+ *   for degenerate transformations that can't be inverted (in this case the
+ *   @inverse matrix will simply be initialized with the identity matrix)
+ */
+COGL_EXPORT gboolean
+cogl_graphene_matrix_get_inverse (const graphene_matrix_t *matrix,
+                                  graphene_matrix_t       *inverse);
 
 G_END_DECLS
diff --git a/cogl/cogl/cogl-matrix-stack.c b/cogl/cogl/cogl-matrix-stack.c
index a4a2d77f41..0431727375 100644
--- a/cogl/cogl/cogl-matrix-stack.c
+++ b/cogl/cogl/cogl-matrix-stack.c
@@ -34,6 +34,7 @@
 #include "cogl-config.h"
 
 #include "cogl-context-private.h"
+#include "cogl-graphene.h"
 #include "cogl-matrix-stack.h"
 #include "cogl-framebuffer-private.h"
 #include "cogl-object-private.h"
@@ -411,9 +412,9 @@ cogl_matrix_stack_get_inverse (CoglMatrixStack   *stack,
   graphene_matrix_t *internal = cogl_matrix_stack_get (stack, &matrix);
 
   if (internal)
-    return cogl_matrix_get_inverse (internal, inverse);
+    return cogl_graphene_matrix_get_inverse (internal, inverse);
   else
-    return cogl_matrix_get_inverse (&matrix, inverse);
+    return cogl_graphene_matrix_get_inverse (&matrix, inverse);
 }
 
 /* In addition to writing the stack matrix into the give @matrix
diff --git a/cogl/cogl/cogl-matrix.c b/cogl/cogl/cogl-matrix.c
index 13e5a46b5a..a4a1e9636d 100644
--- a/cogl/cogl/cogl-matrix.c
+++ b/cogl/cogl/cogl-matrix.c
@@ -67,52 +67,6 @@ cogl_debug_matrix_print (const graphene_matrix_t *matrix)
   _cogl_matrix_prefix_print ("", matrix);
 }
 
-/*
- * Compute inverse of a transformation matrix.
- *
- * @mat pointer to a graphene_matrix_t structure. The matrix inverse will be
- * stored in the graphene_matrix_t::inv attribute.
- *
- * Returns: %TRUE for success, %FALSE for failure (\p singular matrix).
- *
- * Calls the matrix inversion function in inv_mat_tab corresponding to the
- * given matrix type.  In case of failure, updates the MAT_FLAG_SINGULAR flag,
- * and copies the identity matrix into graphene_matrix_t::inv.
- */
-
-#define SCALE 1000.F
-
-static inline gboolean
-calculate_inverse (const graphene_matrix_t *matrix,
-                   graphene_matrix_t       *inverse)
-{
-  graphene_matrix_t scaled;
-  graphene_matrix_t m;
-  gboolean invertible;
-
-  graphene_matrix_init_scale (&scaled, SCALE, SCALE, SCALE);
-
-  // Float precision is a limiting factor
-  graphene_matrix_init_from_matrix (&m, matrix);
-  graphene_matrix_multiply (&m, &scaled, &m);
-
-  invertible = graphene_matrix_inverse (&m, inverse);
-
-  if (invertible)
-    graphene_matrix_multiply (&scaled, inverse, inverse);
-  else
-    graphene_matrix_init_identity (inverse);
-
-  return invertible;
-}
-
-gboolean
-cogl_matrix_get_inverse (const graphene_matrix_t *matrix,
-                         graphene_matrix_t       *inverse)
-{
-  return calculate_inverse (matrix, inverse);
-}
-
 void
 cogl_matrix_rotate (graphene_matrix_t *matrix,
                     float              angle,
diff --git a/cogl/cogl/cogl-matrix.h b/cogl/cogl/cogl-matrix.h
index ce158124e0..c4222a91e2 100644
--- a/cogl/cogl/cogl-matrix.h
+++ b/cogl/cogl/cogl-matrix.h
@@ -485,30 +485,6 @@ cogl_matrix_copy (const graphene_matrix_t *matrix);
 COGL_EXPORT void
 cogl_matrix_free (graphene_matrix_t *matrix);
 
-/**
- * cogl_matrix_get_inverse:
- * @matrix: A 4x4 transformation matrix
- * @inverse: (out): The destination for a 4x4 inverse transformation matrix
- *
- * Gets the inverse transform of a given matrix and uses it to initialize
- * a new #graphene_matrix_t.
- *
- * <note>Although the first parameter is annotated as const to indicate
- * that the transform it represents isn't modified this function may
- * technically save a copy of the inverse transform within the given
- * #graphene_matrix_t so that subsequent requests for the inverse transform may
- * avoid costly inversion calculations.</note>
- *
- * Return value: %TRUE if the inverse was successfully calculated or %FALSE
- *   for degenerate transformations that can't be inverted (in this case the
- *   @inverse matrix will simply be initialized with the identity matrix)
- *
- * Since: 1.2
- */
-COGL_EXPORT gboolean
-cogl_matrix_get_inverse (const graphene_matrix_t *matrix,
-                         graphene_matrix_t       *inverse);
-
 /**
  * cogl_matrix_is_identity:
  * @matrix: A #graphene_matrix_t


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