示例#1
0
def test_figure_io():
    vpl.close()
    assert vpl.gcf(False) is None

    vpl.auto_figure(False)
    assert vpl.gcf() is None

    with pytest.raises(vpl.figures.figure_manager.NoFigureError):
        vpl.screenshot_fig()

    fig = vpl.figure()
    assert vpl.gcf() is None
    del fig

    vpl.auto_figure(True)
    fig = vpl.gcf()
    assert fig is not None

    assert fig is vpl.gcf()
    vpl.close()
    assert vpl.gcf(False) is None
    vpl.scf(fig)
    assert fig is vpl.gcf()

    vpl.close()
    fig = vpl.figure()
    assert fig is vpl.gcf()
    vpl.close()
示例#2
0
    def test_figure_io(self):
        vpl.close()
        self.assertIs(vpl.gcf(False), None)

        vpl.auto_figure(False)
        self.assertIs(vpl.gcf(), None)

        with self.assertRaises(vpl.figures.figure_manager.NoFigureError):
            vpl.screenshot_fig()

        fig = vpl.figure()
        self.assertIs(vpl.gcf(), None)
        del fig

        vpl.auto_figure(True)
        fig = vpl.gcf()
        self.assertTrue(fig is not None)

        self.assertIs(fig, vpl.gcf())
        vpl.close()
        self.assertIs(vpl.gcf(False), None)
        vpl.scf(fig)
        self.assertIs(fig, vpl.gcf())

        vpl.close()
        fig = vpl.figure()
        self.assertIs(fig, vpl.gcf())
        vpl.close()
    def generateImages(self, mesh, path):
        axisName = 'w'
        for axis in self.axes:
            if axisName == 'w':
                axisName = 'x'
            elif axisName == 'x':
                mesh.rotate(np.array([1, 0, 0]), math.radians(45), None)
                axisName = 'y'
            elif axisName == 'y':
                mesh.rotate(np.array([0, 1, 0]), math.radians(45), None)
                axisName = 'z'
            elif axisName == 'z':
                mesh.rotate(np.array([0.5, 0.866, 0]), math.radians(45), None)
                axisName = 'zy'
            for x in range(math.floor(self.fullRotation / self.rotationAngle)):
                if axisName == 'z':
                    mesh.rotate(np.array([0, 1, 0]), math.radians(5),
                                None)  # dual rotation for extra variation
                # rotate(axis, theta=0, point=None)
                mesh.rotate(axis, math.radians(self.rotationAngle), None)

                # Plot the mesh
                vtkplotlib.mesh_plot(mesh, color="blue")
                vtkplotlib.save_fig(
                    os.path.normpath(path + '/_' + axisName + str(x) +
                                     '.png'))  #saves the figure as an image
                vtkplotlib.figure.close(vtkplotlib.gcf())
示例#4
0
    def test_mesh(self):
        import time

        fig = vpl.gcf()

        path = vpl.data.get_rabbit_stl()
        _mesh = Mesh.from_file(path)

        self = vpl.mesh_plot(_mesh.vectors)

        fig.show(False)

        t0 = time.time()
        for i in range(100):
            #        self.color = np.random.random(3)
            #        print(self.color)
            self.set_tri_scalars((_mesh.x[:, 0] + 3 * i) % 20)
            _mesh.rotate(np.ones(3), .1, np.mean(_mesh.vectors, (0, 1)))
            fig.update()
            self.update_points()
            #        time.sleep(.01)
            if (time.time() - t0) > 1:
                break

        fig.show()
示例#5
0
def test_texture():

    phi, theta = np.meshgrid(np.linspace(0, 2 * np.pi, 1024),
                             np.linspace(0, np.pi, 1024))

    x = np.cos(phi) * np.sin(theta)
    y = np.sin(phi) * np.sin(theta)
    z = np.cos(theta)

    self = vpl.surface(x, y, z, fig=None)
    path = vpl.data.ICONS["Right"]
    self.polydata.texture_map = vpl.TextureMap(path, interpolate=True)
    self.colors = (vpl.zip_axes(phi * 3, theta * 5) / np.pi) % 1.

    self.connect()
    vpl.gcf().add_plot(self)
示例#6
0
    def quick_show(self):
        import vtkplotlib as vpl
        old_fig = vpl.gcf(create_new=False)

        fig = vpl.figure(repr(self))
        plot = self.to_plot(fig)
        vpl.show(fig)

        vpl.scf(old_fig)
        return plot
示例#7
0
    def test_qfigure(self):
        fig = vpl.QtFigure("a qt widget figure")

        self.assertIs(fig, vpl.gcf())

        direction = np.array([1, 0, 0])
        vpl.quiver(np.array([0, 0, 0]), direction)
        vpl.view(camera_direction=direction)
        vpl.reset_camera()

        vpl.show()
示例#8
0
    def test_save(self):
        plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3)))

        # I can't get python2 to cooperate with unicode here.
        # The os functions just don't like them.
        if sys.version[0] == "3":

            path = Path.cwd() / u"ҢघԝઌƔࢳܢˀા\\Հએࡓ\u061cཪЈतயଯ\u0886.png"
            try:
                os.mkdir(str(path.parent))
                vpl.save_fig(path)
                self.assertTrue(path.exists())
                os.remove(str(path))
            finally:
                if path.parent.exists():
                    os.rmdir(str(path.parent))

        else:
            path = Path.cwd() / "image.png"
            vpl.save_fig(path)
            os.remove(str(path))

        array = vpl.screenshot_fig(2)
        self.assertEqual(array.shape,
                         tuple(i * 2 for i in vpl.gcf().render_size) + (3, ))
        plt.imshow(array)
        plt.show()

        shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3)))
        vpl.screenshot_fig(pixels=shape).shape
        # The following will fail depending on VTK version
        #        self.assertEqual(vpl.screenshot_fig(pixels=shape).shape,
        #                         shape[::-1] + (3,))

        vpl.close()
        fig = vpl.figure()
        for plot in plots:
            fig += plot
        vpl.show()
示例#9
0
def test_save():
    plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3)))

    path = TEST_DIR / "name.png"

    if path.exists():
        os.remove(str(path))

    vpl.save_fig(path)
    assert path.exists()

    array = vpl.screenshot_fig(magnification=2)
    assert array.shape == tuple(i * 2 for i in vpl.gcf().render_size) + (3, )

    shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3)))
    vpl.screenshot_fig(pixels=shape).shape
    # The following will fail depending on VTK version
    # .assertEqual(vpl.screenshot_fig(pixels=shape).shape,
    #                  shape[::-1] + (3,))

    vpl.close()
    return array
示例#10
0
def test_edge_scalars():

    fig = vpl.gcf()

    _mesh = numpy_stl().Mesh.from_file(path)

    mesh_data = _mesh.vectors
    mesh_data = path

    edge_scalars = vpl.geometry.distance(
        _mesh.vectors[:,np.arange(1, 4) % 3] - _mesh.vectors) # yapf: disable

    self = vpl.mesh_plot_with_edge_scalars(_mesh,
                                           edge_scalars,
                                           centre_scalar=0)
    self.cmap = "Reds"
示例#11
0
def test_mesh():
    import time

    fig = vpl.gcf()

    path = vpl.data.get_rabbit_stl()
    _mesh = numpy_stl().Mesh.from_file(path)

    mp = vpl.mesh_plot(_mesh.vectors)

    fig.show(False)

    for i in range(10):
        mp.tri_scalars = (_mesh.x[:, 0] + 3 * i) % 20
        _mesh.rotate(np.ones(3), .1, np.mean(_mesh.vectors, (0, 1)))
        mp.vectors = _mesh.vectors
        fig.update()

        time.sleep(.01)
示例#12
0
    def test_doc_04(self):
        import vtkplotlib as vpl
        import numpy as np

        # Create an octagon, using `t` as scalar values.
        t = np.arange(0, 1, .125) * 2 * np.pi
        vertices = vpl.zip_axes(np.cos(t), np.sin(t), 0)

        # Plot the octagon.
        vpl.plot(
            vertices,
            line_width=6,  # use a chunky (6pt) line
            join_ends=True,  # join the first and last points
            color=t,  # use `t` as scalar values to color it
        )

        # use a dark background for contrast
        fig = vpl.gcf()
        fig.background_color = "grey"
示例#13
0
def test_plot():
    t = np.arange(0, 1, .001) * 2 * np.pi
    vertices = np.array(
        [np.cos(2 * t),
         np.sin(3 * t),
         np.cos(5 * t) * np.sin(7 * t)]).T
    vertices = np.array([vertices, vertices + 2])

    t = np.arange(0, 1, .125) * 2 * np.pi
    vertices = vpl.zip_axes(np.cos(t), np.sin(t), 0)

    # vertices = np.random.uniform(-30, 30, (3, 3))
    # color = np.broadcast_to(t, vertices.shape[:-1])

    self = vpl.plot(vertices, line_width=6, join_ends=True, color=t)
    # self.polydata.point_scalars = vpl.geometry.distance(vertices)
    # self.polydata.point_colors = t
    fig = vpl.gcf()
    fig.background_color = "grey"
示例#14
0
def test():
    import vtkplotlib as vpl

    t = np.arange(0, 1, .001) * 2 * np.pi
    vertices = np.array([np.cos(2 * t),
                         np.sin(3 * t),
                         np.cos(5 * t) * np.sin(7 *t)]).T
    vertices = np.array([vertices, vertices + 2])

    t = np.arange(0, 1, .125) * 2 * np.pi
    vertices = np.array([np.cos(t), np.sin(t), np.zeros_like(t)]).T

#    vertices = np.random.uniform(-30, 30, (3, 3))
    self = vpl.plot(vertices, color="green", line_width=6, join_ends=True)
#    self.polydata.point_scalars = vpl.geometry.distance(vertices)
    self.polydata.point_scalars = t
    fig = vpl.gcf()
    fig.background_color = "grey"
    self.add_to_plot()
    vpl.show()
示例#15
0
def test_qfigure():
    vpl.QtFigure._abc_assert_no_abstract_methods()

    self = vpl.QtFigure("a Qt widget figure")

    assert self is vpl.gcf()

    direction = np.array([1, 0, 0])
    vpl.quiver(np.array([0, 0, 0]), direction)
    vpl.view(camera_direction=direction)
    vpl.reset_camera()

    self.show(block=False)
    self.close()

    self.showMaximized(block=not VTKPLOTLIB_WINDOWLESS_TEST)
    out = vpl.screenshot_fig(fig=self)
    vpl.close(fig=self)

    globals().update(locals())
    return out
示例#16
0
    def test_qfigure2(self):
        fig = vpl.QtFigure2("a qt widget figure")
        self.assertIs(fig, vpl.gcf())

        vpl.scatter(np.arange(9).reshape((3, 3)).T)
        vpl.quick_test_plot()

        fig.add_all()

        fig.show(block=False)
        fig.qapp.processEvents()

        for i in fig.view_buttons.buttons:
            i.released.emit()
            fig.qapp.processEvents()
            time.sleep(.1)

        fig.screenshot_button.released.emit()
        fig.show_plot_table_button.released.emit()

        fig.show()
示例#17
0
def test_qfigure2():
    fig = vpl.QtFigure2("a QWidget figure")
    fig.setWindowTitle(fig.window_name)
    assert fig is vpl.gcf()

    plot = vpl.scatter(np.arange(9).reshape((3, 3)).T)[0]
    vpl.quick_test_plot()

    fig.add_all()

    fig.show(block=False)
    fig.qapp.processEvents()

    for i in fig.view_buttons.buttons:
        i.released.emit()
        fig.qapp.processEvents()
        time.sleep(.1)

    if not VTKPLOTLIB_WINDOWLESS_TEST:
        fig.screenshot_button.released.emit()
    fig.show_plot_table_button.released.emit()

    fig.show(block=False)

    for plot in fig.plot_table.rows:
        fig.plot_table.rows[plot].text.released.emit()
        fig.qapp.processEvents()
        assert not plot.visible

    assert np.allclose(vpl.screenshot_fig(fig=fig),
                       np.array(255) * fig.background_color,
                       atol=1.)

    for plot in fig.plot_table.rows:
        fig.plot_table.rows[plot].text.released.emit()
        fig.qapp.processEvents()
        assert plot.visible

    fig.plot_table.close()
示例#18
0
import vtkplotlib as vpl
import numpy as np

# You can create a figure explicitly using figure()
fig = vpl.figure("Your Figure Title Here")

# Creating a figure automatically sets it as the current working figure
# You can get the current figure using gcf()
vpl.gcf() is fig  # Should be True

# If a figure hadn't been explicitly created using figure() then gcf()
# would have created one. If gcf() had also not been called here then
# the plotting further down will have internally called gcf().

# A figure's properties can be edited directly
fig.background_color = "dark green"
fig.window_name = "A New Window Title"

points = np.random.uniform(-10, 10, (2, 3))

# To add to a figure you can either:

# 1) Let it automatically add to the whichever figure gcf() returns
vpl.scatter(points[0], color="r")

# 2) Explicitly give it a figure to add to
vpl.scatter(points[1], radius=2, fig=fig)

# 3) Or pass fig=None to prevent it being added then add it later
arrow = vpl.arrow(points[0], points[1], color="g", fig=None)
fig += arrow
示例#19
0

#
    new_scalars = np.empty((len(tri_scalars), 3), dtype=tri_scalars.dtype)
    new_scalars[:, 0] = new_scalars[:, 1] = tri_scalars
    for i in range(3):
        new_scalars[i::3, 2] = centre_scalars

    return MeshPlot(new_vectors, scalars=new_scalars, opacity=opacity, fig=fig)

if __name__ == "__main__":
    import time
    import vtkplotlib as vpl
    from stl.mesh import Mesh

    fig = vpl.gcf()

    path = vpl.data.get_rabbit_stl()
    #    path = "rabbit2.stl"
    _mesh = Mesh.from_file(path)

    mesh_data = _mesh.vectors
    mesh_data = path

    #    vpl.mesh_plot(mesh_data)

    edge_scalars = vpl.geometry.distance(_mesh.vectors[:,
                                                       np.arange(1, 4) % 3] -
                                         _mesh.vectors)

    self = vpl.mesh_plot_with_edge_scalars(_mesh,
示例#20
0
def test_legend():
    import vtkplotlib as vpl

    self = vpl.legend(None, fig=None)
    assert self.fig is None
    assert self.length == 0
    vpl.gcf().add_plot(self)

    self.set_entry(label="Blue Square", color="blue")

    sphere = vpl.scatter([0, 5, 10], color="g", fig=None, label="Ball")
    self.set_entry(sphere, color="b")
    self.set_entry(
        sphere,
        "Green ball",
    )

    rabbit = vpl.mesh_plot(vpl.data.get_rabbit_stl())
    self.set_entry(rabbit, "rabbit")

    rabbit_wire = vpl.plot(rabbit.vectors,
                           color=rabbit.vectors[:, :, 0],
                           label="octopus")
    self.set_entry(rabbit_wire)
    assert self.legend.GetEntryString(self.length - 1) == "octopus"

    # self.set_entry(vpl.quiver(np.zeros(3), np.array([-1, 0, 1])), "right")
    self.set_entry(None, label="shark", icon=vpl.data.ICONS["Right"])

    for size in ((.3, .4), (.3, .4, 0)):
        self.size = size
        assert np.array_equal(self.size, [.3, .4, 0])

    position = np.array(1) - self.size
    self.position = position
    assert np.array_equal(self.position, position)

    with pytest.raises(TypeError):
        self.set_entry(object())

    length = self.length
    for i in range(2):
        eggs = vpl.text3d("eggs", label="eggs")
        self.add_plots([eggs])
        # Re-adding labels shouldn't cause the legend to grow
        assert self.length == length + 1

    vpl.text("text")

    auto_legend = vpl.legend(position=(0, 0))
    auto_legend_no_label = vpl.legend(position=(0, .7),
                                      allow_no_label=True,
                                      allow_non_polydata_plots=True,
                                      color=(.2, .3, .4, .5))
    assert auto_legend_no_label.color == (.2, .3, .4)
    assert auto_legend_no_label.opacity == .5

    self.set_entry(vpl.scatter(np.arange(12).reshape((-1, 3)),
                               label="scatter"),
                   label="fish",
                   index=self.length + 3)

    self.add_plots(vpl.gcf().plots)
示例#21
0
"""

from __future__ import print_function, unicode_literals, with_statement
from builtins import super

import numpy as np
import os, sys
from pathlib2 import Path

import pytest
import vtkplotlib as vpl

from tests._common import checker


@pytest.mark.parametrize("invoker", [vpl.gcf().style, vpl.gcf().iren])
@pytest.mark.parametrize("command", vpl.i.vtkCommands)
def test_super_command(invoker, command):
    cb = vpl.i.get_super_callback(invoker, command)
    assert cb is vpl.i.null_super_callback or cb.__self__ is invoker


def test_raise():
    with pytest.raises(RuntimeError):
        vpl.i.get_super_callback()

    def _test(x, y, z):
        vpl.i.get_super_callback()

    with pytest.raises(RuntimeError):
        _test(1, 2, 3)