kopia lustrzana https://github.com/inkstitch/inkstitch
tidy path_to_curves
rodzic
c2c256727b
commit
68848365b7
|
|
@ -183,7 +183,7 @@ def take_stitch(start: Point, points: typing.Sequence[Point], idx: int, stitch_l
|
|||
return points[-1], None
|
||||
|
||||
|
||||
def stitch_curve_even(points: typing.Sequence[Point], stitch_length: float, tolerance: float):
|
||||
def stitch_curve_evenly(points: typing.Sequence[Point], stitch_length: float, tolerance: float):
|
||||
# Will split a straight line into even-length stitches while still handling curves correctly.
|
||||
# Includes end point but not start point.
|
||||
if len(points) < 2:
|
||||
|
|
@ -216,23 +216,29 @@ def path_to_curves(points: typing.List[Point], min_len: float):
|
|||
if len(points) < 3:
|
||||
return [points]
|
||||
curves = []
|
||||
|
||||
last = 0
|
||||
last_seg = points[1] - points[0]
|
||||
seg_len = last_seg.length()
|
||||
for i in range(1, len(points) - 1):
|
||||
# vectors of the last and next segments
|
||||
a = last_seg
|
||||
b = points[i + 1] - points[i]
|
||||
aabb = (a * a) * (b * b)
|
||||
abab = (a * b) * abs(a * b)
|
||||
|
||||
# Test if the turn angle from vectors a to b is more than 45 degrees
|
||||
# Uses the property of inner products that abab = ± aabb * cos(angle(a,b))**2
|
||||
if aabb > 0 and abab < 0.5 * aabb:
|
||||
# inner angle of at most 135 deg
|
||||
if seg_len >= min_len:
|
||||
curves.append(points[last: i + 1])
|
||||
last = i
|
||||
seg_len = 0
|
||||
|
||||
if b * b > 0:
|
||||
last_seg = b
|
||||
seg_len += b.length()
|
||||
|
||||
curves.append(points[last:])
|
||||
return curves
|
||||
|
||||
|
|
@ -242,7 +248,7 @@ def running_stitch(points, stitch_length, tolerance):
|
|||
stitches = [points[0]]
|
||||
for curve in path_to_curves(points, 2 * tolerance):
|
||||
# segments longer than twice the tollerance will usually be forced by it, so set that as the minimum for corner detection
|
||||
stitches.extend(stitch_curve_even(curve, stitch_length, tolerance))
|
||||
stitches.extend(stitch_curve_evenly(curve, stitch_length, tolerance))
|
||||
return stitches
|
||||
|
||||
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue