about summary refs log tree commit diff
path: root/converter/other/pamtosvg
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-09-06 23:17:18 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2023-09-06 23:17:18 +0000
commit24ef77c17672db9412323197f0f4f6f632534cfb (patch)
treea5b1913cd8093a651280f0d5e0587316bd941fcf /converter/other/pamtosvg
parentde1d329358a406932f09f383c42b320d33b94ed8 (diff)
downloadnetpbm-mirror-24ef77c17672db9412323197f0f4f6f632534cfb.tar.gz
netpbm-mirror-24ef77c17672db9412323197f0f4f6f632534cfb.tar.xz
netpbm-mirror-24ef77c17672db9412323197f0f4f6f632534cfb.zip
Fix to previous commit
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@4635 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/other/pamtosvg')
-rw-r--r--converter/other/pamtosvg/fit.c64
-rw-r--r--converter/other/pamtosvg/vector.c25
-rw-r--r--converter/other/pamtosvg/vector.h7
3 files changed, 76 insertions, 20 deletions
diff --git a/converter/other/pamtosvg/fit.c b/converter/other/pamtosvg/fit.c
index 548d23da..c5a46310 100644
--- a/converter/other/pamtosvg/fit.c
+++ b/converter/other/pamtosvg/fit.c
@@ -1411,34 +1411,58 @@ findHalfTangent(LineEnd      const toWhichEnd,
   be no slope between those points) -- they're part of the count, but don't
   contribute to the slope.  If _all_ of the points to be considered are
   identical to the end point, arbitrarily return a horizontal slope.
+
+  Return the slope as an unnormalized vector.  (I don't know if that was
+  intended by the designer, since it isn't sensible unless it's a
+  computational efficiency thing; it's just how I found the code).
+
+  It is possible for the mean described above to be the zero vector, because
+  the mean of a vector pointing left and one pointing right is the zero
+  vector.  In that case, we use fewer "tangentSurround" points.
 -----------------------------------------------------------------------------*/
     float_coord  const tangentPoint =
         CURVE_POINT(curveP,
                     toWhichEnd == LINEEND_INIT ? 0 : CURVE_LENGTH(curveP) - 1);
     vector_type  const zeroZero = { 0.0, 0.0 };
-    unsigned int const surroundCt =
-        MIN(CURVE_LENGTH(curveP) / 2, tangentSurround);
 
-    unsigned int i;
-    vector_type sum;
-    unsigned int n;
-    vector_type mean;
-
-    for (i = 0, n = 0, sum = zeroZero; i < surroundCt; ++i) {
-        unsigned int const thisIndex =
-            toWhichEnd == LINEEND_INIT ? i + 1 :  CURVE_LENGTH(curveP) - 1 - i;
-        float_coord  const thisPoint = CURVE_POINT(curveP, thisIndex);
-
-        if (!pointsEqual(thisPoint, tangentPoint)) {
-            /* Perhaps we should weight the tangent from `thisPoint' by some
-               factor dependent on the distance from the tangent point.
-            */
-            sum = Vadd(sum, Pdirection(thisPoint, tangentPoint));
-            ++n;
+    unsigned int surroundCt;
+    bool         gotNonzero;
+    vector_type  mean;
+
+    for (surroundCt = MIN(CURVE_LENGTH(curveP) / 2, tangentSurround),
+             gotNonzero = false;
+         !gotNonzero;
+         --surroundCt) {
+
+        unsigned int i;
+        vector_type sum;
+        unsigned int n;
+
+        for (i = 0, n = 0, sum = zeroZero; i < surroundCt; ++i) {
+            unsigned int const thisIndex =
+                toWhichEnd == LINEEND_INIT ?
+                    i + 1 :  CURVE_LENGTH(curveP) - 1 - i;
+            float_coord  const thisPoint = CURVE_POINT(curveP, thisIndex);
+
+            if (!pointsEqual(thisPoint, tangentPoint)) {
+                /* Perhaps we should weight the tangent from `thisPoint' by
+                   some factor dependent on the distance from the tangent
+                   point.
+                */
+                sum = Vadd(sum, Pdirection(thisPoint, tangentPoint));
+                ++n;
+            }
         }
-    }
+        mean = n > 0 ? Vmult_scalar(sum, 1.0 / n) : Vhorizontal();
 
-    mean = n > 0 ? Vmult_scalar(sum, 1.0 / n) : Vhorizontal();
+        if (Vequal(mean, Vzero())) {
+            /* We have points on multiple sides of the endpoint whose vectors
+               happen to add up to zero, which is not usable.
+            */
+            assert(surroundCt > 0);
+        } else
+            gotNonzero = true;
+    }
 
     return mean;
 }
diff --git a/converter/other/pamtosvg/vector.c b/converter/other/pamtosvg/vector.c
index 17c4ec77..b1cffb4a 100644
--- a/converter/other/pamtosvg/vector.c
+++ b/converter/other/pamtosvg/vector.c
@@ -304,3 +304,28 @@ Vhorizontal(void) {
 }
 
 
+vector_type
+Vzero(void) {
+
+    vector_type retval;
+
+    retval.dx = 0.0;
+    retval.dy = 0.0;
+    retval.dz = 0.0;
+
+    return retval;
+}
+
+
+bool
+Vequal(vector_type const comparand,
+       vector_type const comparator) {
+
+    return
+        epsilon_equal(comparand.dx, comparator.dx)
+        &&
+        epsilon_equal(comparand.dy, comparator.dy)
+        &&
+        epsilon_equal(comparand.dz, comparator.dz)
+        ;
+}
diff --git a/converter/other/pamtosvg/vector.h b/converter/other/pamtosvg/vector.h
index 8c172f4f..7dbf4dcb 100644
--- a/converter/other/pamtosvg/vector.h
+++ b/converter/other/pamtosvg/vector.h
@@ -103,4 +103,11 @@ Pmult_scalar(float_coord const,
 vector_type
 Vhorizontal(void);
 
+vector_type
+Vzero(void);
+
+bool
+Vequal(vector_type const,
+       vector_type const);
+
 #endif