예제 #1
0
def test_use_texture3D():
    """Test using a 3D texture"""
    vals = [0, 200, 100, 0, 255, 0, 100]
    d, h, w = len(vals), 3, 5
    data = np.zeros((d, h, w), np.float32)

    VERT_SHADER = """
    attribute vec2 a_pos;
    varying vec2 v_pos;

    void main (void)
    {
        v_pos = a_pos;
        gl_Position = vec4(a_pos, 0., 1.);
    }
    """

    FRAG_SHADER = """
    uniform sampler3D u_texture;
    varying vec2 v_pos;
    uniform float i;
    void main()
    {
        gl_FragColor = texture3D(u_texture,
                                 vec3((v_pos.y+1.)/2., (v_pos.x+1.)/2., i));
        gl_FragColor.a = 1.;
    }
    """
    # populate the depth "slices" with different gray colors in the bottom left
    for ii, val in enumerate(vals):
        data[ii, :2, :3] = val / 255.
    with Canvas(size=(100, 100)) as c:
        if not has_pyopengl():
            t = Texture3D(data)
            assert_raises(ImportError, t.glir.flush, c.context.shared.parser)
            return
        program = Program(VERT_SHADER, FRAG_SHADER)
        program['a_pos'] = [[-1., -1.], [1., -1.], [-1., 1.], [1., 1.]]
        tex = Texture3D(data, interpolation='nearest')
        assert_equal(tex.width, w)
        assert_equal(tex.height, h)
        assert_equal(tex.depth, d)
        program['u_texture'] = tex
        for ii, val in enumerate(vals):
            set_viewport(0, 0, w, h)
            clear(color='black')
            iii = (ii + 0.5) / float(d)
            print(ii, iii)
            program['i'] = iii
            program.draw('triangle_strip')
            out = _screenshot()[:, :, 0].astype(int)[::-1]
            expected = np.zeros_like(out)
            expected[:2, :3] = val
            assert_allclose(out, expected, atol=1. / 255.)
예제 #2
0
    def test_reset_data_shape(self):
        shape1 = 10, 10, 10
        shape3 = 10, 10, 10, 3

        # Init data (explicit shape)
        data = np.zeros((10, 10, 10, 1), dtype=np.uint8)
        T = Texture3D(data=data)
        assert T.shape == (10, 10, 10, 1)
        assert T._format == gl.GL_LUMINANCE

        # Set data to rgb
        T.set_data(np.zeros(shape3, np.uint8))
        assert T.shape == (10, 10, 10, 3)
        assert T._format == gl.GL_RGB

        # Set data to grayscale
        T.set_data(np.zeros(shape1, np.uint8))
        assert T.shape == (10, 10, 10, 1)
        assert T._format == gl.GL_LUMINANCE

        # Set size to rgb
        T.resize(shape3)
        assert T.shape == (10, 10, 10, 3)
        assert T._format == gl.GL_RGB

        # Set size to grayscale
        T.resize(shape1)
        assert T.shape == (10, 10, 10, 1)
        assert T._format == gl.GL_LUMINANCE
예제 #3
0
 def test_set_oversized_data(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     T.set_data(np.ones((20, 20, 20), np.uint8))
     assert T.shape == (20, 20, 20, 1)
     assert T._data.shape == (20, 20, 20, 1)
     assert len(T._pending_data) == 1
예제 #4
0
 def test_set_whole_data(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     T.set_data(np.ones((10, 10, 10), np.uint8))
     assert T.shape == (10, 10, 10, 1)
     glir_cmd = T._glir.clear()[-1]
     assert glir_cmd[0] == 'DATA'
예제 #5
0
 def test_set_misplaced_data(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     # with self.assertRaises(ValueError):
     #    T.set_data(np.ones((5, 5, 5)), offset=(8, 8, 8))
     self.assertRaises(ValueError, T.set_data,
                       np.ones((5, 5, 5)), offset=(8, 8, 8))
예제 #6
0
 def test_resize(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     T.resize((5, 5, 5))
     assert T.shape == (5, 5, 5, 1)
     glir_cmd = T._glir.clear()[-1]
     assert glir_cmd[0] == 'SIZE'
     assert glir_cmd[2] == (5, 5, 5, 1)
예제 #7
0
 def test_resize(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     T.resize((5, 5, 5))
     assert T.shape == (5, 5, 5, 1)
     assert T._data.shape == (5, 5, 5, 1)
     assert T._need_resize is True
     assert not T._pending_data
     assert len(T._pending_data) == 0
예제 #8
0
 def test_reset_data_type(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     
     data = np.zeros((10, 11, 11), dtype=np.float32)
     T.set_data(data)
     
     data = np.zeros((12, 12, 10), dtype=np.int32)
     T.set_data(data)
예제 #9
0
    def test_reset_data_type(self):
        shape = 10, 10, 10
        T = Texture3D(data=np.zeros(shape, dtype=np.uint8))
        assert T.dtype == np.uint8
        assert T._gtype == gl.GL_UNSIGNED_BYTE

        newdata = np.zeros(shape, dtype=np.float32)
        self.assertRaises(ValueError, T.set_data, newdata)

        newdata = np.zeros(shape, dtype=np.int32)
        self.assertRaises(ValueError, T.set_data, newdata)
예제 #10
0
 def test_invalid_views(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     Z = T[5:, 5:, 5:]
     T.resize((5, 5, 5))
     assert Z._valid is False
예제 #11
0
 def test_set_misshaped_data_3D(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     # with self.assertRaises(ValueError):
     #    T.set_data(np.ones((10, 10, 10)))
     self.assertRaises(ValueError, T.set_data, np.ones((10, )))
예제 #12
0
 def test_resize_unresizeable(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data, resizeable=False)
     # with self.assertRaises(RuntimeError):
     #    T.resize((5, 5, 5))
     self.assertRaises(RuntimeError, T.resize, (5, 5, 5))
예제 #13
0
 def test_width_height_depth(self):
     data = np.zeros((10, 20, 30), dtype=np.uint8)
     T = Texture3D(data=data)
     assert T.width == 30
     assert T.height == 20
     assert T.depth == 10
예제 #14
0
 def test_init(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     assert T._shape == (10, 10, 10, 1)
예제 #15
0
 def test_set_undersized_data(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     T.set_data(np.ones((5, 5, 5), np.uint8))
     assert T.shape == (5, 5, 5, 1)
예제 #16
0
def test_texture_3D():
    # Note: put many tests related to (re)sizing here, because Texture
    # is not really aware of shape.

    # Shape extension
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    assert T._shape == (10, 10, 10, 1)
    assert 'Texture3D' in repr(T)
    assert T.glsl_type == ('uniform', 'sampler3D')

    # Width & height
    # ---------------------------------
    data = np.zeros((10, 20, 30), dtype=np.uint8)
    T = Texture3D(data=data)
    assert T.width == 30
    assert T.height == 20
    assert T.depth == 10

    # Resize
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    T.resize((5, 5, 5))
    assert T.shape == (5, 5, 5, 1)
    glir_cmd = T._glir.clear()[-1]
    assert glir_cmd[0] == 'SIZE'
    assert glir_cmd[2] == (5, 5, 5, 1)

    # Resize with bad shape
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    # with self.assertRaises(ValueError):
    #    T.resize((5, 5, 5, 5))
    assert_raises(ValueError, T.resize, (5, 5, 5, 5))

    # Resize not resizable
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data, resizable=False)
    # with self.assertRaises(RuntimeError):
    #    T.resize((5, 5, 5))
    assert_raises(RuntimeError, T.resize, (5, 5, 5))

    # Set oversized data (-> resize)
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    T.set_data(np.ones((20, 20, 20), np.uint8))
    assert T.shape == (20, 20, 20, 1)

    # Set undersized data
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    T.set_data(np.ones((5, 5, 5), np.uint8))
    assert T.shape == (5, 5, 5, 1)

    # Set misplaced data
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    # with self.assertRaises(ValueError):
    #    T.set_data(np.ones((5, 5, 5)), offset=(8, 8, 8))
    assert_raises(ValueError, T.set_data, np.ones((5, 5, 5)), offset=(8, 8, 8))

    # Set misshaped data
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    # with self.assertRaises(ValueError):
    #    T.set_data(np.ones((10, 10, 10)))
    assert_raises(ValueError, T.set_data, np.ones((10, )))

    # Set whole data (clear pending data)
    # ---------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)
    T.set_data(np.ones((10, 10, 10), np.uint8))
    assert T.shape == (10, 10, 10, 1)
    glir_cmd = T._glir.clear()[-1]
    assert glir_cmd[0] == 'DATA'

    # Test set data with different shape
    # ---------------------------------
    shape1 = 10, 10, 10
    shape3 = 10, 10, 10, 3

    # Init data (explicit shape)
    data = np.zeros((10, 10, 10, 1), dtype=np.uint8)
    T = Texture3D(data=data)
    assert T.shape == (10, 10, 10, 1)
    assert T._format == 'luminance'

    # Set data to rgb
    T.set_data(np.zeros(shape3, np.uint8))
    assert T.shape == (10, 10, 10, 3)
    assert T._format == 'rgb'

    # Set data to grayscale
    T.set_data(np.zeros(shape1, np.uint8))
    assert T.shape == (10, 10, 10, 1)
    assert T._format == 'luminance'

    # Set size to rgb
    T.resize(shape3)
    assert T.shape == (10, 10, 10, 3)
    assert T._format == 'rgb'

    # Set size to grayscale
    T.resize(shape1)
    assert T.shape == (10, 10, 10, 1)
    assert T._format == 'luminance'

    # Test set data with different shape and type
    # -------------------------------------------
    data = np.zeros((10, 10, 10), dtype=np.uint8)
    T = Texture3D(data=data)

    data = np.zeros((10, 11, 11), dtype=np.float32)
    T.set_data(data)

    data = np.zeros((12, 12, 10), dtype=np.int32)
    T.set_data(data)
예제 #17
0
 def test_resize_bad_shape(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     # with self.assertRaises(ValueError):
     #    T.resize((5, 5, 5, 5))
     self.assertRaises(ValueError, T.resize, (5, 5, 5, 5))
예제 #18
0
 def test_init(self):
     data = np.zeros((10, 10, 10), dtype=np.uint8)
     T = Texture3D(data=data)
     assert T._shape == (10, 10, 10, 1)
     assert 'Texture3D' in repr(T)
     assert T.glsl_type == ('uniform', 'sampler3D')