File: test_lights.py

package info (click to toggle)
python-pyvista 0.46.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176,968 kB
  • sloc: python: 94,346; sh: 216; makefile: 70
file content (352 lines) | stat: -rw-r--r-- 10,311 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from __future__ import annotations

import numpy as np
import pytest
import vtk

import pyvista as pv

# pyvista attr -- value -- vtk name triples:
configuration = [
    ('light_type', pv.Light.CAMERA_LIGHT, 'SetLightType'),  # resets transformation!
    ('position', (1, 1, 1), 'SetPosition'),
    ('focal_point', (2, 2, 2), 'SetFocalPoint'),
    ('ambient_color', (1.0, 0.0, 0.0), 'SetAmbientColor'),
    ('diffuse_color', (0.0, 1.0, 0.0), 'SetDiffuseColor'),
    ('specular_color', (0.0, 0.0, 1.0), 'SetSpecularColor'),
    ('intensity', 0.5, 'SetIntensity'),
    ('on', False, 'SetSwitch'),
    ('positional', True, 'SetPositional'),
    ('exponent', 1.5, 'SetExponent'),
    ('cone_angle', 45, 'SetConeAngle'),
    ('attenuation_values', (3, 2, 1), 'SetAttenuationValues'),
    ('transform_matrix', np.arange(4 * 4).reshape(4, 4), 'SetTransformMatrix'),
    ('shadow_attenuation', 0.5, 'SetShadowAttenuation'),
]


def test_init():
    position = (1, 1, 1)
    focal_point = (2, 2, 2)
    color = (0.5, 0.5, 0.5)
    light_type = 'headlight'
    cone_angle = 15
    intensity = 2
    exponent = 1.5
    positional = True
    show_actor = False
    shadow_attenuation = 0.5
    light = pv.Light(
        position=position,
        focal_point=focal_point,
        color=color,
        light_type=light_type,
        cone_angle=cone_angle,
        intensity=intensity,
        exponent=exponent,
        show_actor=show_actor,
        positional=positional,
        shadow_attenuation=shadow_attenuation,
    )
    assert isinstance(light, pv.Light)
    assert light.position == position
    assert light.focal_point == focal_point
    assert light.ambient_color == color
    assert light.diffuse_color == color
    assert light.specular_color == color
    assert light.light_type == light.HEADLIGHT
    assert light.cone_angle == cone_angle
    assert light.intensity == intensity
    assert light.exponent == exponent
    assert light.positional == positional
    assert light.actor.GetVisibility() == show_actor
    assert light.shadow_attenuation == shadow_attenuation

    # check repr too
    assert repr(light) is not None


def test_eq():
    light = pv.Light()
    other = pv.Light()
    for light_now in light, other:
        for name, value, _ in configuration:
            setattr(light_now, name, value)

    assert light == other

    # check that changing anything will break equality
    for name, value, _ in configuration:
        original_value = getattr(other, name)
        restore_transform = False
        if value is pv.Light.CAMERA_LIGHT:
            changed_value = pv.Light.HEADLIGHT
            restore_transform = True
        elif isinstance(value, bool):
            changed_value = not value
        elif isinstance(value, (int, float)):
            changed_value = 0
        elif isinstance(value, tuple):
            changed_value = (0.5, 0.5, 0.5)
        else:
            # transform_matrix; value is an ndarray
            changed_value = -value
        setattr(other, name, changed_value)
        assert light != other
        setattr(other, name, original_value)
        if restore_transform:
            # setting the light type cleared the transform
            other.transform_matrix = light.transform_matrix

    # sanity check that we managed to restore the original state
    assert light == other

    # check None vs transform_matrix case
    other.transform_matrix = None
    assert light != other


def test_copy():
    light = pv.Light()
    for name, value, _ in configuration:
        setattr(light, name, value)

    deep = light.copy()
    assert deep == light
    assert deep.transform_matrix is not light.transform_matrix
    shallow = light.copy(deep=False)
    assert shallow == light
    assert shallow.transform_matrix is light.transform_matrix


def test_colors():
    light = pv.Light()

    color = (0.0, 1.0, 0.0)
    light.diffuse_color = color
    assert light.diffuse_color == color
    color = (0.0, 0.0, 1.0)
    light.specular_color = color
    assert light.specular_color == color
    color = (1.0, 0.0, 0.0)
    light.ambient_color = color
    assert light.ambient_color == color

    # test whether strings raise but don't test the result
    for valid in 'white', 'r', '#c0ffee':
        light.diffuse_color = valid
        light.specular_color = valid
        light.ambient_color = valid
    with pytest.raises(ValueError):  # noqa: PT011
        light.diffuse_color = 'invalid'
    with pytest.raises(ValueError):  # noqa: PT011
        light.specular_color = 'invalid'
    with pytest.raises(ValueError):  # noqa: PT011
        light.ambient_color = 'invalid'


def test_positioning():
    light = pv.Light()

    position = (1, 1, 1)
    light.position = position
    assert light.position == position
    # with no transformation matrix this is also the world position
    assert light.world_position == position

    focal_point = (2, 2, 2)
    light.focal_point = focal_point
    assert light.focal_point == focal_point
    # with no transformation matrix this is also the world focal point
    assert light.world_focal_point == focal_point

    elev, azim = (30, 60)
    expected_position = (np.sqrt(3) / 2 * 1 / 2, np.sqrt(3) / 2 * np.sqrt(3) / 2, 1 / 2)
    light.positional = True
    light.set_direction_angle(elev, azim)
    assert not light.positional
    assert light.focal_point == (0, 0, 0)
    assert np.allclose(light.position, expected_position)

    with pytest.raises(AttributeError):
        light.world_position = position
    with pytest.raises(AttributeError):
        light.world_focal_point = focal_point


def test_transforms():
    position = (1, 2, 3)
    focal_point = (4, 5, 6)
    light = pv.Light(position=position)
    light.focal_point = focal_point

    trans_array = np.arange(4 * 4).reshape(4, 4)
    trans_matrix = pv.vtkmatrix_from_array(trans_array)

    assert light.transform_matrix is None
    light.transform_matrix = trans_array
    assert isinstance(light.transform_matrix, vtk.vtkMatrix4x4)
    array = pv.array_from_vtkmatrix(light.transform_matrix)
    assert np.array_equal(array, trans_array)
    light.transform_matrix = trans_matrix
    matrix = light.transform_matrix
    assert all(
        matrix.GetElement(i, j) == trans_matrix.GetElement(i, j)
        for i in range(4)
        for j in range(4)
    )

    linear_trans = trans_array[:-1, :-1]
    shift = trans_array[:-1, -1]
    assert light.position == position
    assert np.allclose(light.world_position, linear_trans @ position + shift)
    assert light.focal_point == focal_point
    assert np.allclose(light.world_focal_point, linear_trans @ focal_point + shift)

    with pytest.raises(TypeError, match='Input transform must be one of'):
        light.transform_matrix = 'invalid'


def test_intensity():
    light = pv.Light()

    intensity = 0.5
    light.intensity = intensity
    assert light.intensity == intensity


def test_switch_state():
    light = pv.Light()

    light.switch_on()
    assert light.on
    light.switch_off()
    assert not light.on
    light.on = False
    assert not light.on


def test_positional():
    light = pv.Light()

    # default is directional light
    assert not light.positional
    light.positional = True
    assert light.positional
    light.positional = False
    assert not light.positional


def test_shape():
    light = pv.Light()

    exponent = 1.5
    light.exponent = exponent
    assert light.exponent == exponent

    cone_angle = 45
    light.cone_angle = cone_angle
    assert light.cone_angle == cone_angle

    attenuation_values = (3, 2, 1)
    light.attenuation_values = attenuation_values
    assert light.attenuation_values == attenuation_values


@pytest.mark.parametrize(
    ('int_code', 'enum_code'),
    [
        (1, pv.Light.HEADLIGHT),
        (2, pv.Light.CAMERA_LIGHT),
        (3, pv.Light.SCENE_LIGHT),
    ],
)
def test_type_properties(int_code, enum_code):
    light = pv.Light()

    # test that the int and enum codes match up
    assert int_code == enum_code

    # test that both codes work
    light.light_type = int_code
    assert light.light_type == int_code
    light.light_type = enum_code
    assert light.light_type == enum_code


def test_type_setters():
    light = pv.Light()

    light.set_headlight()
    assert light.is_headlight
    light.set_camera_light()
    assert light.is_camera_light
    light.set_scene_light()
    assert light.is_scene_light


def test_type_invalid():
    with pytest.raises(TypeError):
        light = pv.Light(light_type=['invalid'])
    with pytest.raises(ValueError):  # noqa: PT011
        light = pv.Light(light_type='invalid')

    light = pv.Light()

    with pytest.raises(TypeError):
        light.light_type = ['invalid']


def test_from_vtk():
    vtk_light = vtk.vtkLight()

    # set the vtk light
    for _, value, vtkname in configuration:
        vtk_setter = getattr(vtk_light, vtkname)
        # we can't pass the array to vtkLight directly
        input_value = pv.vtkmatrix_from_array(value) if isinstance(value, np.ndarray) else value
        vtk_setter(input_value)
    light = pv.Light.from_vtk(vtk_light)
    for pvname, value, _ in configuration:
        if isinstance(value, np.ndarray):
            trans_arr = pv.array_from_vtkmatrix(getattr(light, pvname))
            assert np.array_equal(trans_arr, value)
        else:
            assert getattr(light, pvname) == value

    # invalid case
    with pytest.raises(TypeError):
        pv.Light.from_vtk('invalid')
    with pytest.raises(TypeError):
        pv.Light(position='invalid')


def test_add_vtk_light():
    pl = pv.Plotter(lighting=None)
    pl.add_light(vtk.vtkLight())
    assert len(pl.renderer.lights) == 1


def test_actors():
    light = pv.Light()
    actor = light.actor

    # test showing
    assert not actor.GetVisibility()
    light.show_actor()
    assert not actor.GetVisibility()
    light.positional = True
    light.show_actor()
    assert actor.GetVisibility()

    # test hiding
    light.positional = False
    assert not actor.GetVisibility()
    light.positional = True
    light.show_actor()
    assert actor.GetVisibility()
    light.hide_actor()
    assert not actor.GetVisibility()
    light.show_actor()
    light.cone_angle = 90
    assert not actor.GetVisibility()