# -*- coding: utf-8 -*-

import numpy as np
from vispy import scene

from vispy.testing import (TestingCanvas, requires_application,
                           run_tests_if_main, requires_pyopengl,
                           raises)
from vispy.testing.image_tester import assert_image_approved


@requires_pyopengl()
def test_volume():
    vol = np.zeros((20, 20, 20), 'float32')
    vol[8:16, 8:16, :] = 1.0
    
    # Create
    V = scene.visuals.Volume(vol)
    assert V.clim == (0, 1)
    assert V.method == 'mip'
    
    # Set wrong data
    with raises(ValueError):
        V.set_data(np.zeros((20, 20), 'float32'))
    
    # Clim
    V.set_data(vol, (0.5, 0.8))
    assert V.clim == (0.5, 0.8)
    with raises(ValueError):
        V.set_data((0.5, 0.8, 1.0))
    
    # Method
    V.method = 'iso'
    assert V.method == 'iso'
    
    # Step size
    V.relative_step_size = 1.1
    assert V.relative_step_size == 1.1
    # Disallow 0 step size to avoid GPU stalling
    with raises(ValueError):
        V.relative_step_size = 0


@requires_pyopengl()
@requires_application()
def test_volume_draw():
    with TestingCanvas(bgcolor='k', size=(100, 100)) as c:
        v = c.central_widget.add_view()
        v.camera = 'turntable'
        v.camera.fov = 70
        # Create
        np.random.seed(2376)
        vol = np.random.normal(size=(20, 20, 20), loc=0.5, scale=0.2)
        vol[8:16, 8:16, :] += 1.0
        scene.visuals.Volume(vol, parent=v.scene)  # noqa
        v.camera.set_range()
        assert_image_approved(c.render(), 'visuals/volume.png')


run_tests_if_main()

@@ -1561,16 +1561,924 @@
 png')%0A%0A%0A
+@requires_pyopengl()%0Adef test_set_data_does_not_change_input():%0A    # Create volume%0A    V = scene.visuals.Volume(np.zeros((20, 20, 20)))%0A%0A    # calling Volume.set_data() should NOT alter the values of the input array%0A    # regardless of data type%0A    vol = np.random.randint(0, 200, (20, 20, 20))%0A    for dtype in %5B'uint8', 'int16', 'uint16', 'float32', 'float64'%5D:%0A        vol_copy = np.array(vol, dtype=dtype, copy=True)%0A        # setting clim so that normalization would otherwise change the data%0A        V.set_data(vol_copy, clim=(0, 200))%0A        assert np.allclose(vol, vol_copy)%0A%0A    # for those using float32 who want to avoid the copy operation,%0A    # using set_data() with %60copy=False%60 should be expected to alter the data.%0A    vol2 = np.array(vol, dtype='float32', copy=True)%0A    assert np.allclose(vol, vol2)%0A    V.set_data(vol2, clim=(0, 200), copy=False)%0A    assert not np.allclose(vol, vol2)%0A%0A
 run_test

add test to verify data integrity