summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorLex Neva <github.com@lexneva.name>2019-03-01 21:25:47 -0500
committerLex Neva <github.com@lexneva.name>2019-03-01 21:31:20 -0500
commitb63fe4aa84963ad03d3c2689a6d518d2b10984e4 (patch)
treee78f185cb7211be98199895288d9c97d1e1257f1 /lib
parentbf40f01b5d636a5c918fd1cc9d13c86a0e92626f (diff)
avoid gaps at the end
Diffstat (limited to 'lib')
-rw-r--r--lib/elements/satin_column.py73
1 files changed, 38 insertions, 35 deletions
diff --git a/lib/elements/satin_column.py b/lib/elements/satin_column.py
index 078e12f5..4dea0534 100644
--- a/lib/elements/satin_column.py
+++ b/lib/elements/satin_column.py
@@ -613,7 +613,7 @@ class SatinColumn(EmbroideryElement):
index1 = 0
last_index1 = len(paths[1]) - 1
- while index0 < last_index0 and index1 < last_index1:
+ while index0 < last_index0 or index1 < last_index1:
add_pair(pos0, pos1)
old_pos0 = pos0
@@ -625,47 +625,50 @@ class SatinColumn(EmbroideryElement):
pos0, index0 = self.walk(paths[0], pos0, index0, spacing0)
pos1, index1 = self.walk(paths[1], pos1, index1, spacing1)
- if pos0 == old_pos0 or pos1 == old_pos1:
+ try:
+ # Adjust for rails that contract or expand from each other.
+ # Without any compensation, rail sections that spread out or come
+ # together are longer than parallel rails, and we'll plot stitches
+ # too densely as a result. We can compensate by using some trig,
+ # as described here:
+ #
+ # https://github.com/inkstitch/inkstitch/issues/379#issuecomment-467262685
+ stitch_direction = (pos1 - pos0).unit()
+ peak_to_peak0 = pos0 - old_pos0
+ peak_to_peak1 = pos1 - old_pos1
+
+ # The dot product of two unit vectors is the cosine of the angle
+ # between them. We want the cosine of the angle minus 90 degrees,
+ # so we rotate left by 90 degrees first.
+ #
+ # We take the absolute value to correct for the different direction
+ # of the angles on the opposing rails.
+ cos1 = abs(peak_to_peak0.unit() * stitch_direction.rotate_left())
+ cos2 = abs(peak_to_peak1.unit() * stitch_direction.rotate_left())
+
+ # Use the smaller of the two angles to avoid spacing out
+ # too far on the other rail. Note that the cosine of 0
+ # is 1, so we use min here to mean a bigger angle.
+ cos = min(cos1, cos2)
+
+ # Beyond 0.55 (about 56 degrees), we end up distorting the
+ # stitching and it looks bad.
+ cos = max(cos, 0.55)
+
+ pos0, index0 = self.walk(paths[0], pos0, index0, spacing0 / cos - spacing0)
+ pos1, index1 = self.walk(paths[1], pos1, index1, spacing1 / cos - spacing1)
+ except ZeroDivisionError:
+ # These can occur in unit() if the vector has a length of zero,
+ # which can happen in certain cases. We'll just skip the
+ # compensation.
continue
- # Adjust for rails that contract or expand from each other.
- # Without any compensation, rail sections that spread out or come
- # together are longer than parallel rails, and we'll plot stitches
- # too densely as a result. We can compensate by using some trig,
- # as described here:
- #
- # https://github.com/inkstitch/inkstitch/issues/379#issuecomment-467262685
- stitch_direction = (pos1 - pos0).unit()
- peak_to_peak0 = pos0 - old_pos0
- peak_to_peak1 = pos1 - old_pos1
-
- # The dot product of two unit vectors is the cosine of the angle
- # between them. We want the cosine of the angle minus 90 degrees,
- # so we rotate left by 90 degrees first.
- #
- # We take the absolute value to correct for the different direction
- # of the angles on the opposing rails.
- cos1 = abs(peak_to_peak0.unit() * stitch_direction.rotate_left())
- cos2 = abs(peak_to_peak1.unit() * stitch_direction.rotate_left())
-
- # Use the smaller of the two angles to avoid spacing out
- # too far on the other rail. Note that the cosine of 0
- # is 1, so we use min here to mean a bigger angle.
- cos = min(cos1, cos2)
-
- # Beyond 0.55 (about 56 degrees), we end up distorting the
- # stitching and it looks bad.
- cos = max(cos, 0.55)
-
- pos0, index0 = self.walk(paths[0], pos0, index0, spacing0 / cos - spacing0)
- pos1, index1 = self.walk(paths[1], pos1, index1, spacing1 / cos - spacing1)
-
# We're off by one in the algorithm above, so we need one more
# pair of points. We'd like to add points at the very end to
# make sure we match the vectors on screen as best as possible,
# but we avoid doing so if the stitches will stack up too closely.
- if (pos0 - old_pos0).length() > 0.1 * spacing or \
+ if (pos0 - old_pos0).length() > 0.1 * spacing and \
(pos1 - old_pos1).length() > 0.1 * spacing:
add_pair(pos0, pos1)