|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Licensed under the terms of the BSD 3-Clause |
| 4 | +# (see plotpy/LICENSE for details) |
| 5 | + |
| 6 | +"""Test the fast drawing of thick curves as several one-pixel strokes""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import numpy as np |
| 11 | +import pytest |
| 12 | +from guidata.qthelpers import qt_app_context |
| 13 | +from qtpy import QtCore as QC |
| 14 | +from qtpy import QtGui as QG |
| 15 | +from qwt import QwtScaleMap |
| 16 | + |
| 17 | +from plotpy.builder import make |
| 18 | +from plotpy.config import CONF |
| 19 | +from plotpy.items.curve import base as curve_base |
| 20 | +from plotpy.items.curve.base import ( |
| 21 | + THICK_LINE_MAX_STROKES_WIDTH, |
| 22 | + get_thick_line_strokes, |
| 23 | +) |
| 24 | + |
| 25 | +WIDTH, HEIGHT = 200, 120 |
| 26 | + |
| 27 | + |
| 28 | +def _image(ratio: float = 1.0) -> QG.QImage: |
| 29 | + """Return a white image with the given device pixel ratio""" |
| 30 | + image = QG.QImage(int(WIDTH * ratio), int(HEIGHT * ratio), QG.QImage.Format_ARGB32) |
| 31 | + image.setDevicePixelRatio(ratio) |
| 32 | + image.fill(QC.Qt.white) |
| 33 | + return image |
| 34 | + |
| 35 | + |
| 36 | +def _set_option(monkeypatch: pytest.MonkeyPatch, enabled: bool) -> None: |
| 37 | + """Set the ``curve/fast_thick_lines`` option""" |
| 38 | + original_get = CONF.get |
| 39 | + |
| 40 | + def get(section, option, *args, **kwargs): |
| 41 | + if (section, option) == ("plot", "curve/fast_thick_lines"): |
| 42 | + return enabled |
| 43 | + return original_get(section, option, *args, **kwargs) |
| 44 | + |
| 45 | + monkeypatch.setattr(curve_base.CONF, "get", get) |
| 46 | + |
| 47 | + |
| 48 | +def _strokes(image: QG.QImage, pen: QG.QPen, scale: float = 1.0): |
| 49 | + """Return the strokes of a pen for a painter on an image""" |
| 50 | + painter = QG.QPainter(image) |
| 51 | + if scale != 1.0: |
| 52 | + painter.scale(scale, scale) |
| 53 | + strokes = get_thick_line_strokes(painter, pen) |
| 54 | + painter.end() |
| 55 | + return strokes |
| 56 | + |
| 57 | + |
| 58 | +def _render( |
| 59 | + width: float, color: str = "#000000", style: str = "-", ratio: float = 1.0 |
| 60 | +) -> np.ndarray: |
| 61 | + """Render a noisy sine curve and return the image darkness (0 to 1)""" |
| 62 | + rng = np.random.default_rng(0) |
| 63 | + x = np.linspace(0.0, 1.0, 400) |
| 64 | + y = 0.5 + 0.3 * np.sin(12 * x) + 0.03 * rng.standard_normal(x.size) |
| 65 | + curve = make.curve(x, y, color=color, linewidth=width, linestyle=style) |
| 66 | + xmap, ymap = QwtScaleMap(), QwtScaleMap() |
| 67 | + xmap.setPaintInterval(0, WIDTH) |
| 68 | + xmap.setScaleInterval(0.0, 1.0) |
| 69 | + ymap.setPaintInterval(HEIGHT, 0) |
| 70 | + ymap.setScaleInterval(0.0, 1.0) |
| 71 | + image = _image(ratio) |
| 72 | + painter = QG.QPainter(image) |
| 73 | + painter.setRenderHint(QG.QPainter.Antialiasing, True) |
| 74 | + curve.drawSeries(painter, xmap, ymap, QC.QRectF(0, 0, WIDTH, HEIGHT), 0, -1) |
| 75 | + painter.end() |
| 76 | + gray = image.convertToFormat(QG.QImage.Format_Grayscale8) |
| 77 | + data = np.array( |
| 78 | + [ |
| 79 | + [gray.pixelColor(col, row).value() for col in range(gray.width())] |
| 80 | + for row in range(gray.height()) |
| 81 | + ], |
| 82 | + dtype=float, |
| 83 | + ) |
| 84 | + return 1.0 - data / 255.0 |
| 85 | + |
| 86 | + |
| 87 | +def test_thick_line_strokes_offsets() -> None: |
| 88 | + """Strokes cover the line width, horizontally and vertically""" |
| 89 | + with qt_app_context(exec_loop=False): |
| 90 | + pen = QG.QPen(QG.QColor("#000000"), 2.0) |
| 91 | + stroke_pen, offsets = _strokes(_image(), pen) |
| 92 | + assert stroke_pen.isCosmetic() and stroke_pen.widthF() == 1.0 |
| 93 | + assert sorted(offsets) == [(-0.5, 0.0), (0.0, -0.5), (0.0, 0.5), (0.5, 0.0)] |
| 94 | + # Width of 3 device pixels with a device pixel ratio of 1.5: |
| 95 | + _stroke_pen, offsets = _strokes(_image(1.5), pen) |
| 96 | + assert len(offsets) == 5 |
| 97 | + shifts = sorted(dx * 1.5 for dx, _dy in offsets if _dy == 0.0) |
| 98 | + assert shifts == pytest.approx([-1.0, 0.0, 1.0]) |
| 99 | + assert not pen.isCosmetic() # The original pen is not modified |
| 100 | + |
| 101 | + |
| 102 | +def test_thick_line_strokes_dash_pattern() -> None: |
| 103 | + """Dash patterns of strokes are scaled by the line width""" |
| 104 | + with qt_app_context(exec_loop=False): |
| 105 | + pen = QG.QPen(QG.QColor("#000000"), 2.0, QC.Qt.DashLine) |
| 106 | + stroke_pen, _offsets = _strokes(_image(), pen) |
| 107 | + assert list(stroke_pen.dashPattern()) == [ |
| 108 | + 2.0 * length for length in pen.dashPattern() |
| 109 | + ] |
| 110 | + |
| 111 | + |
| 112 | +@pytest.mark.parametrize( |
| 113 | + "pen_args, scale", |
| 114 | + [ |
| 115 | + (("#000000", 1.0), 1.0), # Thin line |
| 116 | + (("#80000000", 2.0), 1.0), # Translucent color |
| 117 | + (("#000000", THICK_LINE_MAX_STROKES_WIDTH + 1.0), 1.0), # Very wide line |
| 118 | + (("#000000", 2.0), 3.0), # Scaled painter (printing, exporting) |
| 119 | + ], |
| 120 | +) |
| 121 | +def test_thick_line_pen_kept(pen_args: tuple[str, float], scale: float) -> None: |
| 122 | + """Lines are drawn with their pen when strokes do not apply""" |
| 123 | + with qt_app_context(exec_loop=False): |
| 124 | + color, width = pen_args |
| 125 | + pen = QG.QPen(QG.QColor(color), width) |
| 126 | + assert _strokes(_image(), pen, scale) is None |
| 127 | + |
| 128 | + |
| 129 | +def test_thick_line_other_engine() -> None: |
| 130 | + """Lines are drawn with their pen on other paint devices""" |
| 131 | + with qt_app_context(exec_loop=False): |
| 132 | + picture = QG.QPicture() |
| 133 | + painter = QG.QPainter(picture) |
| 134 | + assert get_thick_line_strokes(painter, QG.QPen(QG.QColor("red"), 2.0)) is None |
| 135 | + painter.end() |
| 136 | + |
| 137 | + |
| 138 | +def test_thick_line_option(monkeypatch: pytest.MonkeyPatch) -> None: |
| 139 | + """The ``curve/fast_thick_lines`` option disables strokes""" |
| 140 | + _set_option(monkeypatch, False) |
| 141 | + with qt_app_context(exec_loop=False): |
| 142 | + assert _strokes(_image(), QG.QPen(QG.QColor("red"), 2.0)) is None |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.parametrize("ratio", [1.0, 1.5]) |
| 146 | +@pytest.mark.parametrize("width", [1.5, 2.0, 3.0]) |
| 147 | +def test_thick_line_rendering( |
| 148 | + monkeypatch: pytest.MonkeyPatch, width: float, ratio: float |
| 149 | +) -> None: |
| 150 | + """Strokes look like the line drawn with its pen""" |
| 151 | + with qt_app_context(exec_loop=False): |
| 152 | + _set_option(monkeypatch, True) |
| 153 | + strokes = _render(width, ratio=ratio) |
| 154 | + _set_option(monkeypatch, False) |
| 155 | + reference = _render(width, ratio=ratio) |
| 156 | + # Same amount of ink, at the same place: |
| 157 | + assert strokes.sum() == pytest.approx(reference.sum(), rel=0.1) |
| 158 | + assert np.abs(strokes - reference).sum() < 0.2 * reference.sum() |
| 159 | + assert not np.array_equal(strokes, reference) # Strokes were used |
| 160 | + |
| 161 | + |
| 162 | +def test_translucent_thick_line_rendering(monkeypatch: pytest.MonkeyPatch) -> None: |
| 163 | + """Translucent thick lines are drawn with their pen""" |
| 164 | + with qt_app_context(exec_loop=False): |
| 165 | + _set_option(monkeypatch, True) |
| 166 | + image = _render(2.0, color="#80ff0000") |
| 167 | + _set_option(monkeypatch, False) |
| 168 | + np.testing.assert_array_equal(image, _render(2.0, color="#80ff0000")) |
0 commit comments