diff --git a/lib/elements/satin_column.py b/lib/elements/satin_column.py index 078e12f56..4dea0534c 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)