Skip to content

Commit a38b967

Browse files
committed
add fast_visualise function and FastDirectStitch
also fixed directions, and set default running stitch length
1 parent 88b0c1c commit a38b967

File tree

5 files changed

+306
-7
lines changed

5 files changed

+306
-7
lines changed

src/turtlethread/base_turtle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def right(self, angle):
250250
>>> turtle.heading()
251251
337.0
252252
"""
253-
self._rotate(-angle)
253+
self._rotate(angle)
254254

255255
def left(self, angle):
256256
"""Turn turtle left by angle units.
@@ -271,7 +271,7 @@ def left(self, angle):
271271
>>> turtle.heading()
272272
67.0
273273
"""
274-
self._rotate(angle)
274+
self._rotate(-angle)
275275

276276
def pos(self):
277277
"""Return the turtle's current location (x,y), as a Vec2D-vector.

src/turtlethread/fills.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _fill_at_angle(self, turtle, points, angle, simulate=False):
132132
prev_line = None
133133
jump = False
134134
for i in range(start_idx, len(scanned_lines) - 1): # For each scanned line
135-
with turtle.direct_stitch():
135+
with turtle.fast_direct_stitch():
136136
if len(scanned_lines[i]) >= 2: # If there are at least 2 coordinates, there needs to be a stitch between them!
137137
no_fill_in_current_iteration_flag = False # Something was filled this iteration! For while loop to continue
138138
stitch_rot = (

src/turtlethread/stitches.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def to_pyembroidery(self) -> pyembroidery.EmbPattern:
4949

5050
return pattern
5151

52+
def get_pyembroidery_of(self, stitch_group_idx):
53+
"""Get the PyEmbroidery pattern with the stitch commands of the i-th stitch group"""
54+
pattern = pyembroidery.EmbPattern()
55+
stitch_group = self.stitch_groups[stitch_group_idx]
56+
if (not isinstance(stitch_group, JumpStitch)) and stitch_group.color is not None:
57+
pattern += stitch_group.color
58+
59+
scaled_stitch_commands = (
60+
(x * self.scale, y * self.scale, cmd) for x, y, cmd in stitch_group.get_stitch_commands()
61+
)
62+
pattern.stitches.extend(scaled_stitch_commands)
63+
64+
return pattern
65+
5266
def get_stitch_command(self) -> list[tuple[float, float, StitchCommand]]:
5367
"""Get stitch commands for PyEmbroidery.
5468
@@ -60,6 +74,7 @@ def get_stitch_command(self) -> list[tuple[float, float, StitchCommand]]:
6074

6175

6276
class StitchGroup(ABC):
77+
speedup=0
6378
"""Object representing one contiguous set of commands for the embroidery machine.
6479
6580
Stitch groups are used to convert the Turtle commands into embroidery machine commands. For example, if you want to
@@ -579,6 +594,7 @@ class SatinStitch(ZigzagStitch):
579594
"""Stitch group for satin stitches.
580595
A satin stitch is simply a zigzag stitch with a tight density. This creates a solid fill.
581596
We use 0.3mm for the density."""
597+
speedup=1
582598

583599
def __init__(self, start_pos: Vec2D, color: str, stitch_width: int | float, center: bool = True) -> None:
584600
super().__init__(start_pos=start_pos, color=color, stitch_width=stitch_width, stitch_length=3, center=center)
@@ -754,4 +770,9 @@ def _get_stitch_commands(self) -> list[tuple[float, float, StitchCommand]]:
754770
for pos1, pos2 in itertools.pairwise(self._positions):
755771
stitch_commands.extend(self._iter_stitches_between_positions(pos1, pos2))
756772

757-
return stitch_commands
773+
return stitch_commands
774+
775+
776+
class FastDirectStitch(DirectStitch):
777+
"""A variation of direct stitch that will be fast when using fast_visualise """
778+
speedup = 2

src/turtlethread/turtle.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import fills
1010
from .base_turtle import TNavigator, Vec2D
1111
from .pattern_info import show_info
12-
from .visualise import visualise_pattern
12+
from .visualise import visualise_pattern, fast_visualise
1313

1414
USE_SPHINX_GALLERY = False
1515

@@ -182,7 +182,7 @@ def circle(self, radius, extent=None, steps=None):
182182

183183
super().circle(radius=radius, extent=extent, steps=steps)
184184

185-
def start_running_stitch(self, stitch_length):
185+
def start_running_stitch(self, stitch_length=30):
186186
"""Set the stitch mode to running stitch (not recommended, use ``running_stitch``-context instead).
187187
188188
With a running stitch, we get stitches with a constant distance between each stitch.
@@ -290,6 +290,9 @@ def start_z_stitch(
290290

291291
def start_direct_stitch(self):
292292
self.set_stitch_type(stitches.DirectStitch(self.pos()))
293+
294+
def start_fast_direct_stitch(self):
295+
self.set_stitch_type(stitches.FastDirectStitch(self.pos()))
293296

294297
def cleanup_stitch_type(self):
295298
"""Cleanup after switching stitch type."""
@@ -331,7 +334,7 @@ def use_stitch_group(self, stitch_group):
331334

332335
self.cleanup_stitch_type()
333336

334-
def running_stitch(self, stitch_length):
337+
def running_stitch(self, stitch_length=30):
335338
"""Set the stitch mode to running stitch and cleanup afterwards.
336339
337340
With a running stitch, we get stitches with a constant distance between each stitch.
@@ -444,6 +447,9 @@ def z_stitch(
444447

445448
def direct_stitch(self):
446449
return self.use_stitch_group(stitches.DirectStitch(self.pos(), self.curr_color))
450+
451+
def fast_direct_stitch(self):
452+
return self.use_stitch_group(stitches.FastDirectStitch(self.pos(), self.curr_color))
447453

448454
@property
449455
def _position(self):
@@ -523,6 +529,46 @@ def visualise(self, turtle=None, width=800, height=800, scale=1, speed=6, trace_
523529
print(e)
524530
# Errors when you close the window! Yikes
525531
pass
532+
533+
def fast_visualise(self, turtle=None, width=800, height=800, scale=1, speed=6, trace_jump=False, skip=False, check_density=True, done=True, bye=True):
534+
"""A fast version of the visualise() function, though it has undergone less testing.
535+
536+
Parameters
537+
----------
538+
pattern : pyembroidery.EmbPattern
539+
Embroidery pattern to visualise
540+
turtle : turtle.Turtle (optional)
541+
Python turtle object to use for drawing. If not specified, then the default turtle
542+
is used.
543+
width : int
544+
Canvas width
545+
height : int
546+
Canvas height
547+
scale : int
548+
Factor the embroidery length's are scaled by.
549+
speed : int
550+
Speed that the turtle object moves at.
551+
trace_jump : bool
552+
If True, then draw a grey line connecting the origin and destination of jumps.
553+
skip : bool
554+
If True, then skip the drawing animation and jump to the completed visualisation.
555+
check_density : bool
556+
If True, then check the density of the embroidery pattern. Recommended but slow.
557+
done : bool
558+
If True, then ``turtle.done()`` will be called after drawing.
559+
bye : bool
560+
If True, then ``turtle.bye()`` will be called after drawing.
561+
"""
562+
try:
563+
fast_visualise(
564+
self,
565+
turtle=turtle, width=width, height=height, scale=scale, speed=speed, trace_jump=trace_jump, skip=skip,
566+
check_density=check_density, done=done, bye=bye
567+
)
568+
except Exception as e:
569+
print(e)
570+
# Errors when you close the window! Yikes
571+
pass
526572

527573
def show_info(self):
528574
"""Display information about this turtle's embroidery pattern."""

0 commit comments

Comments
 (0)