Skip to content

Commit

Permalink
Added an option to also flip lines to reverse command (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 authored Aug 9, 2023
1 parent bf950de commit 833fcf2
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 194 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Release date: UNRELEASED

### New features and improvements

* ...
* Added an option to the `reverse` command to also flip the line direction (#654)

### Bug fixes

Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ update-deps:
test:
pytest

lint:
mypy
ruff check vpype vpype_cli vpype_viewer tests

# run previously failed tests
test-failed:
pytest --last-failed
Expand Down
386 changes: 201 additions & 185 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ python = ">=3.9, <3.12"
cachetools = ">=4.2.2"
click = ">=8.0.1,<8.2.0"
multiprocess = ">=0.70.11"
numpy = ">=1.20"
numpy = ">=1.25"
pnoise = ">=0.2.0"
Shapely = {extras = ["vectorized"], version = ">=1.8.2"}
Shapely = ">=1.8.2"
scipy = ">=1.6"
svgelements = ">=1.6.10"
svgwrite = "~1.4"
Expand Down
1 change: 1 addition & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Command:
Command("stat"),
Command("snap 1"),
Command("reverse"),
Command("reverse --flip"),
Command("layout a4", keeps_page_size=False),
Command("squiggles"),
Command("text 'hello wold'"),
Expand Down
8 changes: 8 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ def test_line_collection_reverse():
assert np.all(lc[i] == np.array(line))


def test_line_collection_flip_lines():
line_arr = [(0, 100j, 1000, 10), (5j, 3, 25j), (3 + 3j, 100, 10j)]
lc = LineCollection(line_arr)
lc.flip_lines()
for i, line in enumerate(line_arr):
assert np.all(lc[i] == np.array(line[::-1]))


def test_line_collection_clone():
metadata = {"line_width": 0.3}
lc = LineCollection(([0, 1, 10 + 10j], [0, 10]), metadata=metadata)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_float_type():
@click.argument("arg", type=vpype_cli.FloatType())
@vpype_cli.generator
def cmd(arg: float) -> vp.LineCollection:
assert type(arg) is float
assert isinstance(arg, float)
return vp.LineCollection()

vpype_cli.execute("floatcmd 3.5")
Expand All @@ -25,7 +25,7 @@ def test_int_range_type():
@click.argument("arg", type=vpype_cli.IntRangeType(min=10, max=14))
@vpype_cli.generator
def cmd(arg: int) -> vp.LineCollection:
assert type(arg) is int
assert isinstance(arg, int)
assert 10 <= arg <= 14
return vp.LineCollection()

Expand Down
4 changes: 4 additions & 0 deletions vpype/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ def reverse(self) -> None:
"""Reverse order of the lines."""
self._lines = list(reversed(self._lines))

def flip_lines(self) -> None:
"""Flip the direction of all lines."""
self._lines = [np.flip(line) for line in self._lines]

def __iter__(self):
return self._lines.__iter__()

Expand Down
14 changes: 10 additions & 4 deletions vpype_cli/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,22 @@ def snap(line_collection: vp.LineCollection, pitch: float) -> vp.LineCollection:


@cli.command(group="Operations")
@click.option("-f", "--flip", is_flag=True, help="Also flip the path direction.")
@layer_processor
def reverse(line_collection: vp.LineCollection) -> vp.LineCollection:
def reverse(line_collection: vp.LineCollection, flip: bool) -> vp.LineCollection:
"""Reverse order of lines.
Reverse the order of lines within their respective layers. Individual lines are not
modified (in particular, their trajectory is not inverted). Only the order in which they
are drawn is reversed.
Reverse the order of lines within their respective layers. By default, individual lines are
not modified (in particular, their trajectory is not inverted). Only the order in which
they are drawn is reversed.
If -f/--flip is passed, the direction of each line is also reversed.
"""

line_collection.reverse()
if flip:
line_collection.flip_lines()

return line_collection


Expand Down

0 comments on commit 833fcf2

Please sign in to comment.