[gtk/wip/otte/lottie: 325/339] curve: Split eval() into get_point() and get_tangent()




commit f0a511a639076f5fa685bcfd33f99a8313003ccf
Author: Benjamin Otte <otte redhat com>
Date:   Tue Dec 8 20:12:33 2020 +0100

    curve: Split eval() into get_point() and get_tangent()
    
    That's more in line with the get_start/end_point/tangent() functions.
    
    Plus, those calls are independent and we usually want one or the other.

 gsk/gskcontour.c      |   5 +-
 gsk/gskcurve.c        | 167 ++++++++++++++++++++++++++++----------------------
 gsk/gskcurveprivate.h |   6 +-
 3 files changed, 103 insertions(+), 75 deletions(-)
---
diff --git a/gsk/gskcontour.c b/gsk/gskcontour.c
index 528540d2ad..aaef60cdb3 100644
--- a/gsk/gskcontour.c
+++ b/gsk/gskcontour.c
@@ -1135,7 +1135,10 @@ gsk_standard_contour_measure_get_point (GskStandardContour        *self,
 
   gsk_curve_init (&curve, self->ops[op]);
 
-  gsk_curve_eval (&curve, progress, pos, tangent);
+  if (pos)
+    gsk_curve_get_point (&curve, progress, pos);
+  if (tangent)
+    gsk_curve_get_tangent (&curve, progress, tangent);
 }
 
 static void
diff --git a/gsk/gskcurve.c b/gsk/gskcurve.c
index c5ec9d4ded..ed28700b3f 100644
--- a/gsk/gskcurve.c
+++ b/gsk/gskcurve.c
@@ -29,9 +29,11 @@ struct _GskCurveClass
 {
   void                          (* init)                (GskCurve               *curve,
                                                          gskpathop               op);
-  void                          (* eval)                (const GskCurve         *curve,
-                                                         float                   progress,
-                                                         graphene_point_t       *pos,
+  void                          (* get_point)           (const GskCurve         *curve,
+                                                         float                   t,
+                                                         graphene_point_t       *pos);
+  void                          (* get_tangent)         (const GskCurve         *curve,
+                                                         float                   t,
                                                          graphene_vec2_t        *tangent);
   void                          (* split)               (const GskCurve         *curve,
                                                          float                   progress,
@@ -83,21 +85,23 @@ gsk_line_curve_init (GskCurve  *curve,
 }
 
 static void
-gsk_line_curve_eval (const GskCurve   *curve,
-                     float             progress,
-                     graphene_point_t *pos,
-                     graphene_vec2_t  *tangent)
+gsk_line_curve_get_point (const GskCurve   *curve,
+                          float             t,
+                          graphene_point_t *pos)
 {
   const GskLineCurve *self = &curve->line;
 
-  if (pos)
-    graphene_point_interpolate (&self->points[0], &self->points[1], progress, pos);
+  graphene_point_interpolate (&self->points[0], &self->points[1], t, pos);
+}
 
-  if (tangent)
-    {
-      graphene_vec2_init (tangent, self->points[1].x - self->points[0].x, self->points[1].y - 
self->points[0].y);
-      graphene_vec2_normalize (tangent, tangent);
-    }
+static void
+gsk_line_curve_get_tangent (const GskCurve  *curve,
+                            float            t,
+                            graphene_vec2_t *tangent)
+{
+  const GskLineCurve *self = &curve->line;
+
+  get_tangent (&self->points[0], &self->points[1], tangent);
 }
 
 static void
@@ -153,8 +157,8 @@ gsk_line_curve_get_end_point (const GskCurve *curve)
 }
 
 static void
-gsk_line_curve_get_tangent (const GskCurve  *curve,
-                            graphene_vec2_t *tangent)
+gsk_line_curve_get_start_end_tangent (const GskCurve  *curve,
+                                      graphene_vec2_t *tangent)
 {
   const GskLineCurve *self = &curve->line;
 
@@ -163,14 +167,15 @@ gsk_line_curve_get_tangent (const GskCurve  *curve,
 
 static const GskCurveClass GSK_LINE_CURVE_CLASS = {
   gsk_line_curve_init,
-  gsk_line_curve_eval,
+  gsk_line_curve_get_point,
+  gsk_line_curve_get_tangent,
   gsk_line_curve_split,
   gsk_line_curve_decompose,
   gsk_line_curve_pathop,
   gsk_line_curve_get_start_point,
   gsk_line_curve_get_end_point,
-  gsk_line_curve_get_tangent,
-  gsk_line_curve_get_tangent
+  gsk_line_curve_get_start_end_tangent,
+  gsk_line_curve_get_start_end_tangent
 };
 
 /** CURVE **/
@@ -214,26 +219,33 @@ gsk_curve_curve_ensure_coefficients (const GskCurveCurve *curve)
 }
 
 static void
-gsk_curve_curve_eval (const GskCurve   *curve,
-                      float             progress,
-                      graphene_point_t *pos,
-                      graphene_vec2_t  *tangent)
+gsk_curve_curve_get_point (const GskCurve   *curve,
+                           float             t,
+                           graphene_point_t *pos)
 {
   const GskCurveCurve *self = &curve->curve;
   const graphene_point_t *c = self->coeffs;
 
   gsk_curve_curve_ensure_coefficients (self);
 
-  if (pos)
-    *pos = GRAPHENE_POINT_INIT (((c[0].x * progress + c[1].x) * progress +c[2].x) * progress + c[3].x,
-                                ((c[0].y * progress + c[1].y) * progress +c[2].y) * progress + c[3].y);
-  if (tangent)
-    {
-      graphene_vec2_init (tangent,
-                          (3.0f * c[0].x * progress + 2.0f * c[1].x) * progress + c[2].x,
-                          (3.0f * c[0].y * progress + 2.0f * c[1].y) * progress + c[2].y);
-      graphene_vec2_normalize (tangent, tangent);
-    }
+  *pos = GRAPHENE_POINT_INIT (((c[0].x * t + c[1].x) * t +c[2].x) * t + c[3].x,
+                              ((c[0].y * t + c[1].y) * t +c[2].y) * t + c[3].y);
+}
+
+static void
+gsk_curve_curve_get_tangent (const GskCurve   *curve,
+                             float             t,
+                             graphene_vec2_t  *tangent)
+{
+  const GskCurveCurve *self = &curve->curve;
+  const graphene_point_t *c = self->coeffs;
+
+  gsk_curve_curve_ensure_coefficients (self);
+
+  graphene_vec2_init (tangent,
+                      (3.0f * c[0].x * t + 2.0f * c[1].x) * t + c[2].x,
+                      (3.0f * c[0].y * t + 2.0f * c[1].y) * t + c[2].y);
+  graphene_vec2_normalize (tangent, tangent);
 }
 
 static void
@@ -355,7 +367,8 @@ gsk_curve_curve_get_end_tangent (const GskCurve  *curve,
 
 static const GskCurveClass GSK_CURVE_CURVE_CLASS = {
   gsk_curve_curve_init,
-  gsk_curve_curve_eval,
+  gsk_curve_curve_get_point,
+  gsk_curve_curve_get_tangent,
   gsk_curve_curve_split,
   gsk_curve_curve_decompose,
   gsk_curve_curve_pathop,
@@ -439,46 +452,48 @@ gsk_conic_curve_eval_point (const GskConicCurve *self,
 }
                        
 static void
-gsk_conic_curve_eval (const GskCurve   *curve,
-                      float             progress,
-                      graphene_point_t *pos,
-                      graphene_vec2_t  *tangent)
+gsk_conic_curve_get_point (const GskCurve   *curve,
+                           float             t,
+                           graphene_point_t *pos)
 {
   const GskConicCurve *self = &curve->conic;
 
   gsk_conic_curve_ensure_coefficents (self);
 
-  if (pos)
-    gsk_conic_curve_eval_point (self, progress, pos);
+  gsk_conic_curve_eval_point (self, t, pos);
+}
 
-  if (tangent)
-    {
-      graphene_point_t tmp;
-      float w = gsk_conic_curve_get_weight (self);
-      const graphene_point_t *pts = self->points;
-
-      /* The tangent will be 0 in these corner cases, just
-       * treat it like a line here. */
-      if ((progress <= 0.f && graphene_point_equal (&pts[0], &pts[1])) ||
-          (progress >= 1.f && graphene_point_equal (&pts[1], &pts[3])))
-        {
-          graphene_vec2_init (tangent, pts[3].x - pts[0].x, pts[3].y - pts[0].y);
-          return;
-        }
+static void
+gsk_conic_curve_get_tangent (const GskCurve   *curve,
+                             float             t,
+                             graphene_vec2_t  *tangent)
+{
+  const GskConicCurve *self = &curve->conic;
+  graphene_point_t tmp;
+  float w = gsk_conic_curve_get_weight (self);
+  const graphene_point_t *pts = self->points;
 
-      gsk_curve_eval_quad ((graphene_point_t[3]) {
-                             GRAPHENE_POINT_INIT ((w - 1) * (pts[3].x - pts[0].x),
-                                                  (w - 1) * (pts[3].y - pts[0].y)),
-                             GRAPHENE_POINT_INIT (pts[3].x - pts[0].x - 2 * w * (pts[1].x - pts[0].x),
-                                                  pts[3].y - pts[0].y - 2 * w * (pts[1].y - pts[0].y)),
-                             GRAPHENE_POINT_INIT (w * (pts[1].x - pts[0].x),
-                                                  w * (pts[1].y - pts[0].y))
-                           },
-                           progress,
-                           &tmp);
-      graphene_vec2_init (tangent, tmp.x, tmp.y);
-      graphene_vec2_normalize (tangent, tangent);
+  /* The tangent will be 0 in these corner cases, just
+   * treat it like a line here. */
+  if ((t <= 0.f && graphene_point_equal (&pts[0], &pts[1])) ||
+      (t >= 1.f && graphene_point_equal (&pts[1], &pts[3])))
+    {
+      graphene_vec2_init (tangent, pts[3].x - pts[0].x, pts[3].y - pts[0].y);
+      return;
     }
+
+  gsk_curve_eval_quad ((graphene_point_t[3]) {
+                         GRAPHENE_POINT_INIT ((w - 1) * (pts[3].x - pts[0].x),
+                                              (w - 1) * (pts[3].y - pts[0].y)),
+                         GRAPHENE_POINT_INIT (pts[3].x - pts[0].x - 2 * w * (pts[1].x - pts[0].x),
+                                              pts[3].y - pts[0].y - 2 * w * (pts[1].y - pts[0].y)),
+                         GRAPHENE_POINT_INIT (w * (pts[1].x - pts[0].x),
+                                              w * (pts[1].y - pts[0].y))
+                       },
+                       t,
+                       &tmp);
+  graphene_vec2_init (tangent, tmp.x, tmp.y);
+  graphene_vec2_normalize (tangent, tangent);
 }
 
 static void
@@ -688,7 +703,8 @@ gsk_conic_curve_get_end_tangent (const GskCurve  *curve,
 
 static const GskCurveClass GSK_CONIC_CURVE_CLASS = {
   gsk_conic_curve_init,
-  gsk_conic_curve_eval,
+  gsk_conic_curve_get_point,
+  gsk_conic_curve_get_tangent,
   gsk_conic_curve_split,
   gsk_conic_curve_decompose,
   gsk_conic_curve_pathop,
@@ -723,12 +739,19 @@ gsk_curve_init (GskCurve  *curve,
 }
 
 void
-gsk_curve_eval (const GskCurve   *curve,
-                float             progress,
-                graphene_point_t *pos,
-                graphene_vec2_t  *tangent)
+gsk_curve_get_point (const GskCurve   *curve,
+                     float             progress,
+                     graphene_point_t *pos)
+{
+  get_class (curve->op)->get_point (curve, progress, pos);
+}
+
+void
+gsk_curve_get_tangent (const GskCurve   *curve,
+                       float             progress,
+                       graphene_vec2_t  *tangent)
 {
-  get_class (curve->op)->eval (curve, progress, pos, tangent);
+  get_class (curve->op)->get_tangent (curve, progress, tangent);
 }
 
 void
diff --git a/gsk/gskcurveprivate.h b/gsk/gskcurveprivate.h
index 0f4e89d2c5..1fd7794836 100644
--- a/gsk/gskcurveprivate.h
+++ b/gsk/gskcurveprivate.h
@@ -82,9 +82,11 @@ typedef gboolean (* GskCurveAddLineFunc) (const graphene_point_t *from,
 void                    gsk_curve_init                          (GskCurve               *curve,
                                                                  gskpathop               op);
 
-void                    gsk_curve_eval                          (const GskCurve         *curve,
+void                    gsk_curve_get_point                     (const GskCurve         *curve,
+                                                                 float                   progress,
+                                                                 graphene_point_t       *pos);
+void                    gsk_curve_get_tangent                   (const GskCurve         *curve,
                                                                  float                   progress,
-                                                                 graphene_point_t       *pos,
                                                                  graphene_vec2_t        *tangent);
 void                    gsk_curve_split                         (const GskCurve         *curve,
                                                                  float                   progress,


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