interpolatableTestStartingPoint.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from .interpolatableHelpers import *
  2. def test_starting_point(glyph0, glyph1, ix, tolerance, matching):
  3. if matching is None:
  4. matching = list(range(len(glyph0.isomorphisms)))
  5. contour0 = glyph0.isomorphisms[ix]
  6. contour1 = glyph1.isomorphisms[matching[ix]]
  7. m0Vectors = glyph0.greenVectors
  8. m1Vectors = [glyph1.greenVectors[i] for i in matching]
  9. proposed_point = 0
  10. reverse = False
  11. min_cost = first_cost = 1
  12. c0 = contour0[0]
  13. # Next few lines duplicated below.
  14. costs = [vdiff_hypot2_complex(c0[0], c1[0]) for c1 in contour1]
  15. min_cost_idx, min_cost = min(enumerate(costs), key=lambda x: x[1])
  16. first_cost = costs[0]
  17. if min_cost < first_cost * tolerance:
  18. this_tolerance = min_cost / first_cost
  19. # c0 is the first isomorphism of the m0 master
  20. # contour1 is list of all isomorphisms of the m1 master
  21. #
  22. # If the two shapes are both circle-ish and slightly
  23. # rotated, we detect wrong start point. This is for
  24. # example the case hundreds of times in
  25. # RobotoSerif-Italic[GRAD,opsz,wdth,wght].ttf
  26. #
  27. # If the proposed point is only one off from the first
  28. # point (and not reversed), try harder:
  29. #
  30. # Find the major eigenvector of the covariance matrix,
  31. # and rotate the contours by that angle. Then find the
  32. # closest point again. If it matches this time, let it
  33. # pass.
  34. proposed_point = contour1[min_cost_idx][1]
  35. reverse = contour1[min_cost_idx][2]
  36. num_points = len(glyph1.points[ix])
  37. leeway = 3
  38. if not reverse and (
  39. proposed_point <= leeway or proposed_point >= num_points - leeway
  40. ):
  41. # Try harder
  42. # Recover the covariance matrix from the GreenVectors.
  43. # This is a 2x2 matrix.
  44. transforms = []
  45. for vector in (m0Vectors[ix], m1Vectors[ix]):
  46. meanX = vector[1]
  47. meanY = vector[2]
  48. stddevX = vector[3] * 0.5
  49. stddevY = vector[4] * 0.5
  50. correlation = vector[5] / abs(vector[0])
  51. # https://cookierobotics.com/007/
  52. a = stddevX * stddevX # VarianceX
  53. c = stddevY * stddevY # VarianceY
  54. b = correlation * stddevX * stddevY # Covariance
  55. delta = (((a - c) * 0.5) ** 2 + b * b) ** 0.5
  56. lambda1 = (a + c) * 0.5 + delta # Major eigenvalue
  57. lambda2 = (a + c) * 0.5 - delta # Minor eigenvalue
  58. theta = atan2(lambda1 - a, b) if b != 0 else (pi * 0.5 if a < c else 0)
  59. trans = Transform()
  60. # Don't translate here. We are working on the complex-vector
  61. # that includes more than just the points. It's horrible what
  62. # we are doing anyway...
  63. # trans = trans.translate(meanX, meanY)
  64. trans = trans.rotate(theta)
  65. trans = trans.scale(sqrt(lambda1), sqrt(lambda2))
  66. transforms.append(trans)
  67. trans = transforms[0]
  68. new_c0 = (
  69. [complex(*trans.transformPoint((pt.real, pt.imag))) for pt in c0[0]],
  70. ) + c0[1:]
  71. trans = transforms[1]
  72. new_contour1 = []
  73. for c1 in contour1:
  74. new_c1 = (
  75. [
  76. complex(*trans.transformPoint((pt.real, pt.imag)))
  77. for pt in c1[0]
  78. ],
  79. ) + c1[1:]
  80. new_contour1.append(new_c1)
  81. # Next few lines duplicate from above.
  82. costs = [
  83. vdiff_hypot2_complex(new_c0[0], new_c1[0]) for new_c1 in new_contour1
  84. ]
  85. min_cost_idx, min_cost = min(enumerate(costs), key=lambda x: x[1])
  86. first_cost = costs[0]
  87. if min_cost < first_cost * tolerance:
  88. # Don't report this
  89. # min_cost = first_cost
  90. # reverse = False
  91. # proposed_point = 0 # new_contour1[min_cost_idx][1]
  92. pass
  93. return proposed_point, reverse, min_cost, first_cost