File: test_polygon.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (112 lines) | stat: -rw-r--r-- 4,157 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-

"""
Tests for PolygonVisual
All images are of size (100,100) to keep a small file size
"""

import numpy as np

from vispy.scene import visuals, transforms
from vispy.testing import (requires_application, requires_scipy, TestingCanvas,
                           run_tests_if_main)
from vispy.testing.image_tester import assert_image_approved


@requires_application()
@requires_scipy()
def test_square_draw():
    """Test drawing squares without transforms using PolygonVisual"""
    pos = np.array([[-0.5, 0.5, 0],
                    [0.5, 0.5, 0],
                    [0.5, -0.5, 0],
                    [-0.5, -0.5, 0]])
    with TestingCanvas() as c:
        polygon = visuals.Polygon(pos=pos, color=(1, 0, 0, 1),
                                  parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(50, 50),
                                                   translate=(50, 50))

        assert_image_approved(c.render(), 'visuals/square1.png')

        polygon.parent = None
        polygon = visuals.Polygon(pos=pos, color=(1, 0, 0, 1),
                                  border_color=(1, 1, 1, 1),
                                  parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(50, 50),
                                                   translate=(50, 50))

        assert_image_approved(c.render(), 'visuals/square2.png')

        polygon.parent = None
        polygon = visuals.Polygon(pos=pos, border_color=(1, 1, 1, 1),
                                  parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(50, 50),
                                                   translate=(50, 50))

        assert_image_approved(c.render(), 'visuals/square3.png',
                              min_corr=0.45)


@requires_application()
@requires_scipy()
def test_rectangle_draw():
    """Test drawing rectangles with transforms using PolygonVisual"""
    pos = np.array([[-0.1, 0.5, 0],
                    [0.1, 0.5, 0],
                    [0.1, -0.5, 0],
                    [-0.1, -0.5, 0]])
    with TestingCanvas() as c:
        polygon = visuals.Polygon(pos=pos, color=(1, 1, 0, 1), parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(200.0, 25),
                                                   translate=(50, 50))

        assert_image_approved(c.render(), 'visuals/rectangle1.png')

        polygon.parent = None
        polygon = visuals.Polygon(pos=pos, color=(1, 1, 0, 1),
                                  border_color=(1, 0, 0, 1),
                                  parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(200.0, 25),
                                                   translate=(50, 50))

        assert_image_approved(c.render(), 'visuals/rectangle2.png')

        polygon.parent = None
        polygon = visuals.Polygon(pos=pos, border_color=(1, 0, 0, 1),
                                  border_width=1, parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(200.0, 25),
                                                   translate=(50, 49))

        assert_image_approved(c.render(), 'visuals/rectangle3.png',
                              min_corr=0.7)


@requires_application()
@requires_scipy()
def test_reactive_draw():
    """Test reactive polygon attributes"""
    pos = np.array([[-0.1, 0.5, 0],
                    [0.1, 0.5, 0],
                    [0.1, -0.5, 0],
                    [-0.1, -0.5, 0]])
    with TestingCanvas() as c:
        polygon = visuals.Polygon(pos=pos, color='yellow', parent=c.scene)
        polygon.transform = transforms.STTransform(scale=(50, 50),
                                                   translate=(50, 50))

        polygon.pos += [0.1, -0.1, 0]

        assert_image_approved(c.render(), 'visuals/reactive_polygon1.png')

        polygon.color = 'red'

        assert_image_approved(c.render(), 'visuals/reactive_polygon2.png')

        polygon.border_color = 'yellow'

        assert_image_approved(c.render(), 'visuals/reactive_polygon3.png',
                              min_corr=0.8)


run_tests_if_main()