freetype-commit
[Top][All Lists]
Advanced

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

[freetype2] anuj-distance-field 02cbe8a 2/2: [sdf -> bsdf] Optimize the


From: Anuj Verma
Subject: [freetype2] anuj-distance-field 02cbe8a 2/2: [sdf -> bsdf] Optimize the first pass of the 8SED.
Date: Sun, 26 Jul 2020 07:35:59 -0400 (EDT)

branch: anuj-distance-field
commit 02cbe8a12c62df85fae98160a72d108c1242894a
Author: Anuj Verma <anujv@iitbhilai.ac.in>
Commit: Anuj Verma <anujv@iitbhilai.ac.in>

    [sdf -> bsdf] Optimize the first pass of the 8SED.
    
    * src/sdf/ftbsdf.c (first_pass): Optimize the first
      pass by first approximating the neighbor's distance
      by adding an offset. The offset will be max root(2)
      because the maximum be add to a vector is (1, 1).
      By approximating we can reduce the number of
      `FT_Vector_Length' calls and thus make the process
      faster.
---
 [GSoC]ChangeLog  | 12 +++++++
 src/sdf/ftbsdf.c | 98 +++++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 77 insertions(+), 33 deletions(-)

diff --git a/[GSoC]ChangeLog b/[GSoC]ChangeLog
index d53dfd4..eecc8a4 100644
--- a/[GSoC]ChangeLog
+++ b/[GSoC]ChangeLog
@@ -1,5 +1,17 @@
 2020-07-26  Anuj Verma  <anujv@iitbhilai.ac.in>
 
+       [sdf -> bsdf] Optimize the first pass of the 8SED.
+
+       * src/sdf/ftbsdf.c (first_pass): Optimize the first
+         pass by first approximating the neighbor's distance
+         by adding an offset. The offset will be max root(2)
+         because the maximum be add to a vector is (1, 1).
+         By approximating we can reduce the number of
+         `FT_Vector_Length' calls and thus make the process
+         faster.
+
+2020-07-26  Anuj Verma  <anujv@iitbhilai.ac.in>
+
        [sdf] Fix GNU Make build.
 
        * src/sdf/rules.mk (SDF_DRV_H): Manually add source files
diff --git a/src/sdf/ftbsdf.c b/src/sdf/ftbsdf.c
index c48ca98..322773e 100644
--- a/src/sdf/ftbsdf.c
+++ b/src/sdf/ftbsdf.c
@@ -206,9 +206,9 @@
           if ( s_i < 0 || s_i >= s_width ||
                s_j < 0 || s_j >= s_rows )
           {
-            t[t_index].near.x = 100000;
-            t[t_index].near.y = 100000;
-            t[t_index].dist   = 100000;
+            t[t_index].near.x = FT_INT_MAX;
+            t[t_index].near.y = FT_INT_MAX;
+            t[t_index].dist   = 128 * ONE;
             continue;
           }
 
@@ -236,9 +236,9 @@
             t[t_index].dist = 0;
           else
           {
-            t[t_index].near.x = 100000;
-            t[t_index].near.y = 100000;
-            t[t_index].dist   = 100000;
+            t[t_index].near.x = FT_INT_MAX;
+            t[t_index].near.y = FT_INT_MAX;
+            t[t_index].dist   = 128 * ONE;
           }
 
   #endif
@@ -305,50 +305,75 @@
         index = j * w + i;
         current = dm + index;
 
+        /* While checking for nearest point we first     */
+        /* approximate the dist of the `current' point   */
+        /* by adding the deviation ( which will be root  */
+        /* 2 max ). And if the new value is lesser than  */
+        /* the current value then only we calculate the  */
+        /* actual distances using `FT_Vector_Length'.    */
+        /* Of course this will be eliminated while using */
+        /* squared distances.                            */
+
         /* left-up */
         to_check = current - w - 1;
-        dist_vec = to_check->near;
-        dist_vec.x -= ONE;
-        dist_vec.y -= ONE;
-        dist = FT_Vector_Length( &dist_vec );
+        dist = to_check->dist + 2 * ONE; /* will be root(2) but 2 is fine */
         if ( dist < current->dist )
         {
-          current->dist = dist;
-          current->near = dist_vec;
+          dist_vec = to_check->near;
+          dist_vec.x -= ONE;
+          dist_vec.y -= ONE;
+          dist = FT_Vector_Length( &dist_vec );
+          if ( dist < current->dist )
+          {
+            current->dist = dist;
+            current->near = dist_vec;
+          }
         }
 
         /* up */
         to_check = current - w;
-        dist_vec = to_check->near;
-        dist_vec.y -= ONE;
-        dist = FT_Vector_Length( &dist_vec );
+        dist = to_check->dist + 1 * ONE; /* can only be 1 */
         if ( dist < current->dist )
         {
-          current->dist = dist;
-          current->near = dist_vec;
+          dist_vec = to_check->near;
+          dist_vec.y -= ONE;
+          dist = FT_Vector_Length( &dist_vec );
+          if ( dist < current->dist )
+          {
+            current->dist = dist;
+            current->near = dist_vec;
+          }
         }
 
         /* up-right */
         to_check = current - w + 1;
-        dist_vec = to_check->near;
-        dist_vec.x += ONE;
-        dist_vec.y -= ONE;
-        dist = FT_Vector_Length( &dist_vec );
+        dist = to_check->dist + 2 * ONE;
         if ( dist < current->dist )
         {
-          current->dist = dist;
-          current->near = dist_vec;
+          dist_vec = to_check->near;
+          dist_vec.x += ONE;
+          dist_vec.y -= ONE;
+          dist = FT_Vector_Length( &dist_vec );
+          if ( dist < current->dist )
+          {
+            current->dist = dist;
+            current->near = dist_vec;
+          }
         }
 
         /* left */
         to_check = current - 1;
-        dist_vec = to_check->near;
-        dist_vec.x -= ONE;
-        dist = FT_Vector_Length( &dist_vec );
+        dist = to_check->dist + 1 * ONE;
         if ( dist < current->dist )
         {
-          current->dist = dist;
-          current->near = dist_vec;
+          dist_vec = to_check->near;
+          dist_vec.x -= ONE;
+          dist = FT_Vector_Length( &dist_vec );
+          if ( dist < current->dist )
+          {
+            current->dist = dist;
+            current->near = dist_vec;
+          }
         }
       }
 
@@ -361,13 +386,17 @@
 
         /* right */
         to_check = current + 1;
-        dist_vec = to_check->near;
-        dist_vec.x += ONE;
-        dist = FT_Vector_Length( &dist_vec );
+        dist = to_check->dist + 1 * ONE;
         if ( dist < current->dist )
         {
-          current->dist = dist;
-          current->near = dist_vec;
+          dist_vec = to_check->near;
+          dist_vec.x += ONE;
+          dist = FT_Vector_Length( &dist_vec );
+          if ( dist < current->dist )
+          {
+            current->dist = dist;
+            current->near = dist_vec;
+          }
         }
       }
     }
@@ -466,6 +495,9 @@
 
         /* [TODO]: Assing the sign properly. */
 
+        if ( final_dist > worker->params.spread * 1024 )
+          final_dist = worker->params.spread * 1024;
+
         t_buffer[index] = final_dist;
       }
     }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]