From 6a8821d91cdad417c695d9b6d521de2d9f936a07 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Thu, 4 Mar 2021 20:15:24 +0100 Subject: [PATCH] Revert "Use Vector in some places where it improves the clarity of the code" (#2215) * Revert most of "Use Vector in some places where it improves the clarity of the code (#2206)" This reverts most of commit 0d3ce2cafc6f604fc46d4d3bfbab34a5bc59e27b. --- Lib/fontTools/misc/bezierTools.py | 186 +++++++++++++++++++----------- Tests/misc/bezierTools_test.py | 1 - 2 files changed, 118 insertions(+), 69 deletions(-) diff --git a/Lib/fontTools/misc/bezierTools.py b/Lib/fontTools/misc/bezierTools.py index b268f0c55..caa4557d3 100644 --- a/Lib/fontTools/misc/bezierTools.py +++ b/Lib/fontTools/misc/bezierTools.py @@ -4,7 +4,6 @@ from fontTools.misc.arrayTools import calcBounds, sectRect, rectArea from fontTools.misc.transform import Offset, Identity -from fontTools.misc.vector import Vector from fontTools.misc.py23 import * import math from collections import namedtuple @@ -244,15 +243,16 @@ def calcQuadraticBounds(pt1, pt2, pt3): >>> calcQuadraticBounds((0, 0), (100, 0), (100, 100)) (0.0, 0.0, 100, 100) """ - a, b, c = calcQuadraticParameters(pt1, pt2, pt3) - ax2, ay2 = a * 2 + (ax, ay), (bx, by), (cx, cy) = calcQuadraticParameters(pt1, pt2, pt3) + ax2 = ax * 2.0 + ay2 = ay * 2.0 roots = [] if ax2 != 0: - roots.append(-b[0] / ax2) + roots.append(-bx / ax2) if ay2 != 0: - roots.append(-b[1] / ay2) + roots.append(-by / ay2) points = [ - a * (t * t) + b * t + c + (ax * t * t + bx * t + cx, ay * t * t + by * t + cy) for t in roots if 0 <= t < 1 ] + [pt1, pt3] @@ -343,17 +343,21 @@ def calcCubicBounds(pt1, pt2, pt3, pt4): >>> print("%f %f %f %f" % calcCubicBounds((50, 0), (0, 100), (100, 100), (50, 0))) 35.566243 0.000000 64.433757 75.000000 """ - a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4) + (ax, ay), (bx, by), (cx, cy), (dx, dy) = calcCubicParameters(pt1, pt2, pt3, pt4) # calc first derivative - ax3, ay3 = a * 3.0 - bx2, by2 = b * 2.0 - cx, cy = c + ax3 = ax * 3.0 + ay3 = ay * 3.0 + bx2 = bx * 2.0 + by2 = by * 2.0 xRoots = [t for t in solveQuadratic(ax3, bx2, cx) if 0 <= t < 1] yRoots = [t for t in solveQuadratic(ay3, by2, cy) if 0 <= t < 1] roots = xRoots + yRoots points = [ - a * (t * t * t) + b * (t * t) + c * t + d + ( + ax * t * t * t + bx * t * t + cx * t + dx, + ay * t * t * t + by * t * t + cy * t + dy, + ) for t in roots ] + [pt1, pt4] return calcBounds(points) @@ -395,17 +399,22 @@ def splitLine(pt1, pt2, where, isHorizontal): ((0, 100), (0, 50)) ((0, 50), (0, 0)) """ - pt1 = Vector(pt1) - pt2 = Vector(pt2) + pt1x, pt1y = pt1 + pt2x, pt2y = pt2 - a = pt2 - pt1 - b = pt1 + ax = pt2x - pt1x + ay = pt2y - pt1y - if a[isHorizontal] == 0: + bx = pt1x + by = pt1y + + a = (ax, ay)[isHorizontal] + + if a == 0: return [(pt1, pt2)] - t = (where - b[isHorizontal]) / a[isHorizontal] + t = (where - (bx, by)[isHorizontal]) / a if 0 <= t < 1: - midPt = a * t + b + midPt = ax * t + bx, ay * t + by return [(pt1, midPt), (midPt, pt2)] else: return [(pt1, pt2)] @@ -546,18 +555,24 @@ def _splitQuadraticAtT(a, b, c, *ts): segments = [] ts.insert(0, 0.0) ts.append(1.0) + ax, ay = a + bx, by = b + cx, cy = c for i in range(len(ts) - 1): t1 = ts[i] t2 = ts[i + 1] delta = t2 - t1 # calc new a, b and c delta_2 = delta * delta - a1 = a * delta_2 - b1 = (2 * a * t1 + b) * delta + a1x = ax * delta_2 + a1y = ay * delta_2 + b1x = (2 * ax * t1 + bx) * delta + b1y = (2 * ay * t1 + by) * delta t1_2 = t1 * t1 - c1 = a * t1_2 + b * t1 + c + c1x = ax * t1_2 + bx * t1 + cx + c1y = ay * t1_2 + by * t1 + cy - pt1, pt2, pt3 = calcQuadraticPoints(a1, b1, c1) + pt1, pt2, pt3 = calcQuadraticPoints((a1x, a1y), (b1x, b1y), (c1x, c1y)) segments.append((pt1, pt2, pt3)) return segments @@ -567,6 +582,10 @@ def _splitCubicAtT(a, b, c, d, *ts): ts.insert(0, 0.0) ts.append(1.0) segments = [] + ax, ay = a + bx, by = b + cx, cy = c + dx, dy = d for i in range(len(ts) - 1): t1 = ts[i] t2 = ts[i + 1] @@ -578,11 +597,17 @@ def _splitCubicAtT(a, b, c, d, *ts): t1_3 = t1 * t1_2 # calc new a, b, c and d - a1 = a * delta_3 - b1 = (3 * a * t1 + b) * delta_2 - c1 = (2 * b * t1 + c + 3 * a * t1_2) * delta - d1 = a * t1_3 + b * t1_2 + c * t1 + d - pt1, pt2, pt3, pt4 = calcCubicPoints(a1, b1, c1, d1) + a1x = ax * delta_3 + a1y = ay * delta_3 + b1x = (3 * ax * t1 + bx) * delta_2 + b1y = (3 * ay * t1 + by) * delta_2 + c1x = (2 * bx * t1 + cx + 3 * ax * t1_2) * delta + c1y = (2 * by * t1 + cy + 3 * ay * t1_2) * delta + d1x = ax * t1_3 + bx * t1_2 + cx * t1 + dx + d1y = ay * t1_3 + by * t1_2 + cy * t1 + dy + pt1, pt2, pt3, pt4 = calcCubicPoints( + (a1x, a1y), (b1x, b1y), (c1x, c1y), (d1x, d1y) + ) segments.append((pt1, pt2, pt3, pt4)) return segments @@ -725,37 +750,57 @@ def solveCubic(a, b, c, d): def calcQuadraticParameters(pt1, pt2, pt3): - pt1, pt2, pt3 = (Vector(pt) for pt in (pt1, pt2, pt3)) - c = pt1 - b = (pt2 - c) * 2.0 - a = pt3 - c - b - return a, b, c + x2, y2 = pt2 + x3, y3 = pt3 + cx, cy = pt1 + bx = (x2 - cx) * 2.0 + by = (y2 - cy) * 2.0 + ax = x3 - cx - bx + ay = y3 - cy - by + return (ax, ay), (bx, by), (cx, cy) def calcCubicParameters(pt1, pt2, pt3, pt4): - pt1, pt2, pt3, pt4 = (Vector(pt) for pt in (pt1, pt2, pt3, pt4)) - d = pt1 - c = (pt2 - d) * 3.0 - b = (pt3 - pt2) * 3.0 - c - a = pt4 - d - c - b - return a, b, c, d + x2, y2 = pt2 + x3, y3 = pt3 + x4, y4 = pt4 + dx, dy = pt1 + cx = (x2 - dx) * 3.0 + cy = (y2 - dy) * 3.0 + bx = (x3 - x2) * 3.0 - cx + by = (y3 - y2) * 3.0 - cy + ax = x4 - dx - cx - bx + ay = y4 - dy - cy - by + return (ax, ay), (bx, by), (cx, cy), (dx, dy) def calcQuadraticPoints(a, b, c): - a, b, c = (Vector(coeff) for coeff in (a, b, c)) - pt1 = c - pt2 = (b * 0.5) + c - pt3 = a + b + c - return pt1, pt2, pt3 + ax, ay = a + bx, by = b + cx, cy = c + x1 = cx + y1 = cy + x2 = (bx * 0.5) + cx + y2 = (by * 0.5) + cy + x3 = ax + bx + cx + y3 = ay + by + cy + return (x1, y1), (x2, y2), (x3, y3) def calcCubicPoints(a, b, c, d): - a, b, c, d = (Vector(coeff) for coeff in (a, b, c, d)) - pt1 = d - pt2 = (c / 3.0) + d - pt3 = (b + c) / 3.0 + pt2 - pt4 = a + d + c + b - return pt1, pt2, pt3, pt4 + ax, ay = a + bx, by = b + cx, cy = c + dx, dy = d + x1 = dx + y1 = dy + x2 = (cx / 3.0) + dx + y2 = (cy / 3.0) + dy + x3 = (bx + cx) / 3.0 + x2 + y3 = (by + cy) / 3.0 + y2 + x4 = ax + dx + cx + bx + y4 = ay + dy + cy + by + return (x1, y1), (x2, y2), (x3, y3), (x4, y4) # @@ -773,7 +818,7 @@ def linePointAtT(pt1, pt2, t): Returns: A 2D tuple with the coordinates of the point. """ - return (Vector(pt1) * (1 - t) + Vector(pt2) * t) + return ((pt1[0] * (1 - t) + pt2[0] * t), (pt1[1] * (1 - t) + pt2[1] * t)) def quadraticPointAtT(pt1, pt2, pt3, t): @@ -786,9 +831,9 @@ def quadraticPointAtT(pt1, pt2, pt3, t): Returns: A 2D tuple with the coordinates of the point. """ - pt1, pt2, pt3 = (Vector(pt) for pt in (pt1, pt2, pt3)) - t1 = 1 - t - return t1 * t1 * pt1 + 2 * t1 * t * pt2 + t * t * pt3 + x = (1 - t) * (1 - t) * pt1[0] + 2 * (1 - t) * t * pt2[0] + t * t * pt3[0] + y = (1 - t) * (1 - t) * pt1[1] + 2 * (1 - t) * t * pt2[1] + t * t * pt3[1] + return (x, y) def cubicPointAtT(pt1, pt2, pt3, pt4, t): @@ -801,14 +846,19 @@ def cubicPointAtT(pt1, pt2, pt3, pt4, t): Returns: A 2D tuple with the coordinates of the point. """ - pt1, pt2, pt3, pt4 = (Vector(pt) for pt in (pt1, pt2, pt3, pt4)) - t1 = 1 - t - return ( - t1 * t1 * t1 * pt1 - + 3 * t1 * t1 * t * pt2 - + 3 * t1 * t * t * pt3 - + t * t * t * pt4 + x = ( + (1 - t) * (1 - t) * (1 - t) * pt1[0] + + 3 * (1 - t) * (1 - t) * t * pt2[0] + + 3 * (1 - t) * t * t * pt3[0] + + t * t * t * pt4[0] ) + y = ( + (1 - t) * (1 - t) * (1 - t) * pt1[1] + + 3 * (1 - t) * (1 - t) * t * pt2[1] + + 3 * (1 - t) * t * t * pt3[1] + + t * t * t * pt4[1] + ) + return (x, y) def segmentPointAtT(seg, t): @@ -863,7 +913,7 @@ def lineLineIntersections(s1, e1, s2, e2): 1 >>> intersection = a[0] >>> intersection.pt - Vector((374.44882952482897, 313.73458370177315)) + (374.44882952482897, 313.73458370177315) >>> (intersection.t1, intersection.t2) (0.45069111555824454, 0.5408153767394238) """ @@ -887,7 +937,7 @@ def lineLineIntersections(s1, e1, s2, e2): x = s1x slope34 = (e2y - s2y) / (e2x - s2x) y = slope34 * (x - s2x) + s2y - pt = Vector((x, y)) + pt = (x, y) return [ Intersection( pt=pt, t1=_line_t_of_pt(s1, e1, pt), t2=_line_t_of_pt(s2, e2, pt) @@ -897,7 +947,7 @@ def lineLineIntersections(s1, e1, s2, e2): x = s2x slope12 = (e1y - s1y) / (e1x - s1x) y = slope12 * (x - s1x) + s1y - pt = Vector((x, y)) + pt = (x, y) return [ Intersection( pt=pt, t1=_line_t_of_pt(s1, e1, pt), t2=_line_t_of_pt(s2, e2, pt) @@ -910,7 +960,7 @@ def lineLineIntersections(s1, e1, s2, e2): return [] x = (slope12 * s1x - s1y - slope34 * s2x + s2y) / (slope12 - slope34) y = slope12 * (x - s1x) + s1y - pt = Vector((x, y)) + pt = (x, y) if _both_points_are_on_same_side_of_origin( pt, e1, s1 ) and _both_points_are_on_same_side_of_origin(pt, s2, e2): @@ -964,7 +1014,7 @@ def curveLineIntersections(curve, line): >>> len(intersections) 3 >>> intersections[0].pt - Vector((84.90010344084885, 189.87306176459828)) + (84.90010344084885, 189.87306176459828) """ if len(curve) == 3: pointFinder = quadraticPointAtT @@ -1085,7 +1135,7 @@ def curveCurveIntersections(curve1, curve2): >>> len(intersections) 3 >>> intersections[0].pt - Vector((81.7831487395506, 109.88904552375288)) + (81.7831487395506, 109.88904552375288) """ intersection_ts = _curve_curve_intersections_t(curve1, curve2) return [ @@ -1113,14 +1163,14 @@ def segmentSegmentIntersections(seg1, seg2): >>> len(intersections) 3 >>> intersections[0].pt - Vector((81.7831487395506, 109.88904552375288)) + (81.7831487395506, 109.88904552375288) >>> curve3 = [ (100, 240), (30, 60), (210, 230), (160, 30) ] >>> line = [ (25, 260), (230, 20) ] >>> intersections = segmentSegmentIntersections(curve3, line) >>> len(intersections) 3 >>> intersections[0].pt - Vector((84.90010344084885, 189.87306176459828)) + (84.90010344084885, 189.87306176459828) """ # Arrange by degree diff --git a/Tests/misc/bezierTools_test.py b/Tests/misc/bezierTools_test.py index 63b1a15ea..af120e686 100644 --- a/Tests/misc/bezierTools_test.py +++ b/Tests/misc/bezierTools_test.py @@ -132,7 +132,6 @@ def test_solveCubic(): assert solveCubic(9.0, 0.0, 0.0, -7.62939453125e-05) == [-0.0, -0.0, -0.0] - _segmentPointAtT_testData = [ ([(0, 10), (200, 100)], 0.0, (0, 10)), ([(0, 10), (200, 100)], 0.5, (100, 55)),