Skip to content

Commit

Permalink
Fixed aliasing issue with relative HPGL export. (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 authored Nov 12, 2021
1 parent ce8026b commit 2cebb44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
New features and improvements:
* Added `lswap` command to swap the content of two layers (#300)
* Added `lreverse` command to reverse the order of paths within a layer (#300)
* Improved HPGL export (#253, #310, #316)
* Improved HPGL export (#253, #310, #316, #335)

* Relative coordinates are now used by default to reduce file size. If absolute coordinates are needed, they a new `--absolute` option for the `write` command.
* A homing command (as defined by the `final_pu_params` configuration parameter) is no longer emitted between layers.
Expand Down
25 changes: 25 additions & 0 deletions tests/test_hpgl.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,31 @@ def test_hpgl_paper_size_inference_fail(runner):
assert res.stdout.strip() == ""


def test_hpgl_relative_no_aliasing(runner):
# This generates two lines of the same length, one split in small bits. Relative export
# is expected to avoid aliasing when differentiating subsequent point position.
res = runner.invoke(
cli,
"begin grid -o 0 0.11mm 1 1200 line 0 0 0 0.11mm end linemerge translate 0.1cm 0 "
"line 0 0 0 13.2cm linemerge layout a4 write -f hpgl -d hp7475a -",
)

cmds = res.stdout.strip().split(";")

# Note: the following indices depends on the weird bug that a `line` command after a
# `begin`/`end` block is using a new layer (i.e. layer 2) instead of the same layer as
# before
line1_pd = cmds[6]
assert line1_pd.startswith("PD")
line2_pd = cmds[10]
assert line2_pd.startswith("PD")

line1_length = sum(map(int, line1_pd.lstrip("PD").split(",")[0::2]))
line2_length = int(line2_pd.lstrip("PD").split(",")[0])

assert abs(line1_length) == abs(line2_length)


def test_hpgl_flex_no_pagesize(simple_printer_config):
doc = vp.Document()
doc.add(vp.LineCollection([(1 + 1j, 2 + 4j)]))
Expand Down
4 changes: 4 additions & 0 deletions vpype/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,10 @@ def complex_to_str(p: complex) -> str:
+ ";"
)
else:
# snap all points of the line to the HPGL grid, such as to avoid any aliasing
# when computing the diff between points
line = np.round(line)

if last_point is None:
output.write(f"PU{complex_to_str(line[0])};PR;")
else:
Expand Down

0 comments on commit 2cebb44

Please sign in to comment.