def main():
    global app
    app = qt.QApplication([])

    # Create a SceneWindow
    window = SceneWindow()
    window.show()

    sceneWidget = window.getSceneWidget()
    scatter = items.Scatter3D()
    scatter.setSymbol(',')
    scatter.getColormap().setName('magma')
    sceneWidget.addItem(scatter)

    # Create the thread that calls submitToQtMainThread
    updateThread = UpdateScatterThread(scatter)
    updateThread.start()  # Start updating the plot

    app.exec_()

    updateThread.stop()  # Stop updating the plot
Example #2
0
def main():
    global app
    app = qt.QApplication([])

    # Create a SceneWindow
    window = SceneWindow()
    window.show()

    sceneWidget = window.getSceneWidget()
    scatter = items.Scatter3D()
    scatter.setSymbol(',')
    scatter.getColormap().setName('magma')
    sceneWidget.addItem(scatter)

    # Create the thread that calls submitToQtMainThread
    updateThread = UpdateScatterThread(scatter)
    updateThread.start()  # Start updating the plot

    app.exec_()

    updateThread.stop()  # Stop updating the plot
Example #3
0
# Create a 3D scalar field item and set its data
volume = items.ScalarField3D()  # Create a new 3D volume item
volume.setData(data)  # Set its data
group.addItem(volume)  # Add it to the group (and thus to the scene)

# Set volume tranform
volume.setTranslation(0., SIZE, 0.)
volume.setScale(SIZE/data.shape[2], SIZE/data.shape[1], SIZE/data.shape[0])

# Add isosurfaces to the volume item given isolevel and color
volume.addIsosurface(0.2, '#FF000080')
volume.addIsosurface(0.5, '#0000FFFF')

# Set the volume cut plane
cutPlane = volume.getCutPlanes()[0]  # Get the volume's cut plane
cutPlane.setVisible(True)  # Set it to be visible
cutPlane.getColormap().setName('jet')  # Set cut plane's colormap
cutPlane.setNormal((0., 0., 1.))  # Set cut plane's normal
cutPlane.moveToCenter()  # Place the cut plane at the center of the volume

sceneWidget.addItem(group)  # Add the group as an item of the scene

# Show the SceneWidget widget
window.show()

# Display exception in a pop-up message box
sys.excepthook = qt.exceptionHandler

# Run Qt event loop
qapp.exec_()
Example #4
0
class TestSceneWindow(TestCaseQt, ParametricTestCase):
    """Tests SceneWidget picking feature"""

    def setUp(self):
        super(TestSceneWindow, self).setUp()
        self.window = SceneWindow()
        self.window.show()
        self.qWaitForWindowExposed(self.window)

    def tearDown(self):
        self.qapp.processEvents()
        self.window.setAttribute(qt.Qt.WA_DeleteOnClose)
        self.window.close()
        del self.window
        super(TestSceneWindow, self).tearDown()

    def testAdd(self):
        """Test add basic scene primitive"""
        sceneWidget = self.window.getSceneWidget()
        items = []

        # RGB image
        image = sceneWidget.addImage(numpy.random.random(
            10*10*3).astype(numpy.float32).reshape(10, 10, 3))
        image.setLabel('RGB image')
        items.append(image)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # Data image
        image = sceneWidget.addImage(
            numpy.arange(100, dtype=numpy.float32).reshape(10, 10))
        image.setTranslation(10.)
        items.append(image)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # 2D scatter
        scatter = sceneWidget.add2DScatter(
            *numpy.random.random(3000).astype(numpy.float32).reshape(3, -1),
            index=0)
        scatter.setTranslation(0, 10)
        scatter.setScale(10, 10, 10)
        items.insert(0, scatter)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # 3D scatter
        scatter = sceneWidget.add3DScatter(
            *numpy.random.random(4000).astype(numpy.float32).reshape(4, -1))
        scatter.setTranslation(10, 10)
        scatter.setScale(10, 10, 10)
        items.append(scatter)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # 3D array of float
        volume = sceneWidget.addVolume(
            numpy.arange(10**3, dtype=numpy.float32).reshape(10, 10, 10))
        volume.setTranslation(0, 0, 10)
        volume.setRotation(45, (0, 0, 1))
        volume.addIsosurface(500, 'red')
        volume.getCutPlanes()[0].getColormap().setName('viridis')
        items.append(volume)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # 3D array of complex
        volume = sceneWidget.addVolume(
            numpy.arange(10**3).reshape(10, 10, 10).astype(numpy.complex64))
        volume.setTranslation(10, 0, 10)
        volume.setRotation(45, (0, 0, 1))
        volume.setComplexMode(volume.ComplexMode.REAL)
        volume.addIsosurface(500, (1., 0., 0., .5))
        items.append(volume)
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        sceneWidget.resetZoom('front')
        self.qapp.processEvents()

    def testHeightMap(self):
        """Test height map items"""
        sceneWidget = self.window.getSceneWidget()

        height = numpy.arange(10000).reshape(100, 100) /100.

        for shape in ((100, 100), (4, 5), (150, 20), (110, 110)):
            with self.subTest(shape=shape):
                items = []

                # Colormapped data height map
                data = numpy.arange(numpy.prod(shape)).astype(numpy.float32).reshape(shape)

                heightmap = HeightMapData()
                heightmap.setData(height)
                heightmap.setColormappedData(data)
                heightmap.getColormap().setName('viridis')
                items.append(heightmap)
                sceneWidget.addItem(heightmap)

                # RGBA height map
                colors = numpy.zeros(shape + (3,), dtype=numpy.float32)
                colors[:, :, 1] = numpy.random.random(shape)

                heightmap = HeightMapRGBA()
                heightmap.setData(height)
                heightmap.setColorData(colors)
                heightmap.setTranslation(100., 0., 0.)
                items.append(heightmap)
                sceneWidget.addItem(heightmap)

                self.assertEqual(sceneWidget.getItems(), tuple(items))
                sceneWidget.resetZoom('front')
                self.qapp.processEvents()
                sceneWidget.clearItems()

    def testChangeContent(self):
        """Test add/remove/clear items"""
        sceneWidget = self.window.getSceneWidget()
        items = []

        # Add 2 images
        image = numpy.arange(100, dtype=numpy.float32).reshape(10, 10)
        items.append(sceneWidget.addImage(image))
        items.append(sceneWidget.addImage(image))
        self.qapp.processEvents()
        self.assertEqual(sceneWidget.getItems(), tuple(items))

        # Clear
        sceneWidget.clearItems()
        self.qapp.processEvents()
        self.assertEqual(sceneWidget.getItems(), ())

        # Add 2 images and remove first one
        image = numpy.arange(100, dtype=numpy.float32).reshape(10, 10)
        sceneWidget.addImage(image)
        items = (sceneWidget.addImage(image),)
        self.qapp.processEvents()

        sceneWidget.removeItem(sceneWidget.getItems()[0])
        self.qapp.processEvents()
        self.assertEqual(sceneWidget.getItems(), items)

    def testColors(self):
        """Test setting scene colors"""
        sceneWidget = self.window.getSceneWidget()

        color = qt.QColor(128, 128, 128)
        sceneWidget.setBackgroundColor(color)
        self.assertEqual(sceneWidget.getBackgroundColor(), color)

        color = qt.QColor(0, 0, 0)
        sceneWidget.setForegroundColor(color)
        self.assertEqual(sceneWidget.getForegroundColor(), color)

        color = qt.QColor(255, 0, 0)
        sceneWidget.setTextColor(color)
        self.assertEqual(sceneWidget.getTextColor(), color)

        color = qt.QColor(0, 255, 0)
        sceneWidget.setHighlightColor(color)
        self.assertEqual(sceneWidget.getHighlightColor(), color)

        self.qapp.processEvents()

    def testInteractiveMode(self):
        """Test changing interactive mode"""
        sceneWidget = self.window.getSceneWidget()
        center = numpy.array((sceneWidget.width() //2, sceneWidget.height() // 2))

        self.mouseMove(sceneWidget, pos=center)
        self.mouseClick(sceneWidget, qt.Qt.LeftButton, pos=center)

        volume = sceneWidget.addVolume(
            numpy.arange(10**3).astype(numpy.float32).reshape(10, 10, 10))
        sceneWidget.selection().setCurrentItem( volume.getCutPlanes()[0])
        sceneWidget.resetZoom('side')

        for mode in (None, 'rotate', 'pan', 'panSelectedPlane'):
            with self.subTest(mode=mode):
                sceneWidget.setInteractiveMode(mode)
                self.qapp.processEvents()
                self.assertEqual(sceneWidget.getInteractiveMode(), mode)

                self.mouseMove(sceneWidget, pos=center)
                self.mousePress(sceneWidget, qt.Qt.LeftButton, pos=center)
                self.mouseMove(sceneWidget, pos=center-10)
                self.mouseMove(sceneWidget, pos=center-20)
                self.mouseRelease(sceneWidget, qt.Qt.LeftButton, pos=center-20)

                self.keyPress(sceneWidget, qt.Qt.Key_Control)
                self.mouseMove(sceneWidget, pos=center)
                self.mousePress(sceneWidget, qt.Qt.LeftButton, pos=center)
                self.mouseMove(sceneWidget, pos=center-10)
                self.mouseMove(sceneWidget, pos=center-20)
                self.mouseRelease(sceneWidget, qt.Qt.LeftButton, pos=center-20)
                self.keyRelease(sceneWidget, qt.Qt.Key_Control)