[gegl-gtk] GeglGtkView: Add accessor to transformation matrix



commit 8e6512fbd9ad5196693184cffc1dc6c7b8da6fcb
Author: Jon Nordby <jononor gmail com>
Date:   Mon Oct 10 01:14:44 2011 +0200

    GeglGtkView: Add accessor to transformation matrix

 examples/c/gegl-gtk-paint.c     |   33 +++++++++++++++++++++++++++++++--
 gegl-gtk/gegl-gtk-view.c        |   15 +++++++++++++++
 gegl-gtk/gegl-gtk-view.h        |    2 ++
 gegl-gtk/internal/view-helper.c |   37 +++++++++++++++++++++++++++++++++++++
 gegl-gtk/internal/view-helper.h |    2 ++
 5 files changed, 87 insertions(+), 2 deletions(-)
---
diff --git a/examples/c/gegl-gtk-paint.c b/examples/c/gegl-gtk-paint.c
index 7e50848..9dcde2a 100644
--- a/examples/c/gegl-gtk-paint.c
+++ b/examples/c/gegl-gtk-paint.c
@@ -39,10 +39,31 @@ static GeglPath   *vector   = NULL;
 static GeglNode   *over     = NULL;
 static GeglNode   *stroke   = NULL;
 
+/* Transform the input coordinate from view coordinates to model coordinates
++ * Returns TRUE if the transformation was successfull, else FALSE */
+gboolean
+transform_view_to_model_coordinate (gdouble *x, gdouble *y)
+{
+  GeglMatrix3 matrix;
+  gegl_gtk_view_get_transformation (GEGL_GTK_VIEW (view), &matrix);
+
+  if (gegl_matrix3_determinant (&matrix) == 0.0) {
+    return FALSE;
+  }
+
+  gegl_matrix3_invert (&matrix);
+  gegl_matrix3_transform_point (&matrix, x, y);
+
+  return TRUE;
+}
 
 static gboolean paint_press (GtkWidget      *widget,
                              GdkEventButton *event)
 {
+  gdouble x = event->x;
+  gdouble y = event->y;
+  transform_view_to_model_coordinate (&x, &y);
+
   if (event->button == 1)
     {
       vector     = gegl_path_new ();
@@ -57,7 +78,7 @@ static gboolean paint_press (GtkWidget      *widget,
                                         NULL);
       gegl_node_link_many (top, over, out, NULL);
       gegl_node_connect_to (stroke, "output", over, "aux");
-      gegl_path_append (vector, 'M', event->x, event->y);
+      gegl_path_append (vector, 'M', x, y);
 
       pen_down = TRUE;
 
@@ -70,6 +91,11 @@ static gboolean paint_press (GtkWidget      *widget,
 static gboolean paint_motion (GtkWidget      *widget,
                               GdkEventMotion *event)
 {
+  gdouble x = event->x;
+  gdouble y = event->y;
+
+  transform_view_to_model_coordinate (&x, &y);
+
   if (event->state & GDK_BUTTON1_MASK)
     {
       if (!pen_down)
@@ -77,7 +103,7 @@ static gboolean paint_motion (GtkWidget      *widget,
           return TRUE;
         }
 
-      gegl_path_append (vector, 'L', event->x, event->y);
+      gegl_path_append (vector, 'L', x, y);
       return TRUE;
     }
   return FALSE;
@@ -166,6 +192,9 @@ main (gint    argc,
     gegl_node_link_many (loadbuf, out, NULL);
 
     view = GTK_WIDGET(gegl_gtk_view_new_for_node(out));
+    gegl_gtk_view_set_x(GEGL_GTK_VIEW(view), -50.0);
+    gegl_gtk_view_set_y(GEGL_GTK_VIEW(view), -50.0);
+    gegl_gtk_view_set_autoscale_policy(GEGL_GTK_VIEW(view), GEGL_GTK_VIEW_AUTOSCALE_DISABLED);
     top  = loadbuf;
   }
 
diff --git a/gegl-gtk/gegl-gtk-view.c b/gegl-gtk/gegl-gtk-view.c
index 392802f..cda52de 100644
--- a/gegl-gtk/gegl-gtk-view.c
+++ b/gegl-gtk/gegl-gtk-view.c
@@ -388,6 +388,21 @@ gegl_gtk_view_get_y(GeglGtkView *self)
     return view_helper_get_y(GET_PRIVATE(self));
 }
 
+/**
+ * gegl_gtk_view_get_transformation: Get the model->view transformation
+ * @self: A #GeglGtkView
+ * @matrix: (out caller-allocates): Pointer to location for transformation matrix
+ *
+ * The transformation matrix describes the transformation between the
+ * model (the output of the GeglNode) and the view (the display in the widget).
+ * To transform coordinates use gegl_matrix3_transform_point().
+ * To get a matrix representing the view->model space transformation, use gegl_matrix3_invert()
+ */
+void gegl_gtk_view_get_transformation(GeglGtkView *self, GeglMatrix3 *matrix)
+{
+    view_helper_get_transformation(GET_PRIVATE(self), matrix);
+}
+
 void
 gegl_gtk_view_set_autoscale_policy(GeglGtkView *self, GeglGtkViewAutoscale autoscale)
 {
diff --git a/gegl-gtk/gegl-gtk-view.h b/gegl-gtk/gegl-gtk-view.h
index 6cf36e4..1d21cc1 100644
--- a/gegl-gtk/gegl-gtk-view.h
+++ b/gegl-gtk/gegl-gtk-view.h
@@ -69,6 +69,8 @@ float gegl_gtk_view_get_x(GeglGtkView *self);
 void gegl_gtk_view_set_y(GeglGtkView *self, float y);
 float gegl_gtk_view_get_y(GeglGtkView *self);
 
+void gegl_gtk_view_get_transformation(GeglGtkView *self, GeglMatrix3 *matrix);
+
 void gegl_gtk_view_set_autoscale_policy(GeglGtkView *self, GeglGtkViewAutoscale autoscale);
 GeglGtkViewAutoscale gegl_gtk_view_get_autoscale_policy(GeglGtkView *self);
 
diff --git a/gegl-gtk/internal/view-helper.c b/gegl-gtk/internal/view-helper.c
index 62e5ee7..ddbedc7 100644
--- a/gegl-gtk/internal/view-helper.c
+++ b/gegl-gtk/internal/view-helper.c
@@ -354,6 +354,43 @@ view_helper_get_y(ViewHelper *self)
     return self->y;
 }
 
+void view_helper_get_transformation(ViewHelper *self, GeglMatrix3 *matrix)
+{
+    /* XXX: Below gives the right result, but is it really the
+     * way we want transformations to work? */
+
+    /* API change in GEGL 1.7 (1.6+git):
+     * GeglMatrix3 changed from float[3][3] to
+     * struct with a float[3][3] coeff member */
+
+#if GEGL_MINOR_VERSION == 1 && GEGL_MICRO_VERSION >= 7
+    matrix->coeff [0][0] = self->scale; /* xx */
+    matrix->coeff [0][1] = 0.0; /* xy */
+    matrix->coeff [0][2] = -self->x; /* x0 */
+
+    matrix->coeff [1][0] = 0.0; /* yx */
+    matrix->coeff [1][1] = self->scale; /* yy */
+    matrix->coeff [1][2] = -self->y; /* y0 */
+
+    matrix->coeff [2][0] = 0.0;
+    matrix->coeff [2][1] = 0.0;
+    matrix->coeff [2][2] = 1.0;
+#else
+    matrix [0][0] = self->scale;
+    matrix [0][1] = 0.0;
+    matrix [0][2] = -self->x;
+
+    matrix [1][0] = 0.0;
+    matrix [1][1] = self->scale;
+    matrix [1][2] = -self->y;
+
+    matrix [2][0] = 0.0;
+    matrix [2][1] = 0.0;
+    matrix [2][2] = 1.0;
+#endif
+
+}
+
 void
 view_helper_set_autoscale_policy(ViewHelper *self, GeglGtkViewAutoscale autoscale)
 {
diff --git a/gegl-gtk/internal/view-helper.h b/gegl-gtk/internal/view-helper.h
index a9e32ed..0c3f95e 100644
--- a/gegl-gtk/internal/view-helper.h
+++ b/gegl-gtk/internal/view-helper.h
@@ -79,6 +79,8 @@ float view_helper_get_x(ViewHelper *self);
 void view_helper_set_y(ViewHelper *self, float y);
 float view_helper_get_y(ViewHelper *self);
 
+void view_helper_get_transformation(ViewHelper *self, GeglMatrix3 *matrix);
+
 void view_helper_set_autoscale_policy(ViewHelper *self, GeglGtkViewAutoscale autoscale);
 GeglGtkViewAutoscale view_helper_get_autoscale_policy(ViewHelper *self);
 



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