Пример #1
0
    def openFile(self):

        #ここでファイルを開くを実行.開けるファイルは「.foam」形式に限定
        fileName = QFileDialog.getOpenFileName(
            self, 'Open file',
            os.path.expanduser('~') + '/Desktop', "OpenFOAM File (*.foam)")

        print(fileName)
        reader = vtk.vtkOpenFOAMReader()
        reader.SetFileName(
            str(fileName[0])
        )  #パス+ファイル名が格納されるのは[0]番.「1]にはファイルの形式「OpenFOAM File (*.foam)」が格納される.

        reader.CreateCellToPointOn()
        reader.DecomposePolyhedraOn()
        reader.EnableAllCellArrays()
        reader.Update()

        filter2 = vtk.vtkGeometryFilter()
        filter2.SetInputConnection(reader.GetOutputPort())

        mapper = vtk.vtkCompositePolyDataMapper2()
        mapper.SetInputConnection(filter2.GetOutputPort())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        ren = vtk.vtkRenderer()
        ##背景色の設定
        ren.GradientBackgroundOn()  #グラデーション背景を設定
        ren.SetBackground2(0.2, 0.4, 0.6)  #上面の色
        ren.SetBackground(1, 1, 1)  #下面の色
        self.widget.GetRenderWindow().AddRenderer(ren)
        ren.AddActor(actor)
Пример #2
0
def pyvista_render_window():
    """
    Allow to download and create a more complex example easily
    """
    from pyvista import examples
    sphere = pv.Sphere()  #test actor
    globe = examples.load_globe()  #test texture
    head = examples.download_head()  #test volume
    uniform = examples.load_uniform()  #test structured grid

    scalars = sphere.points[:, 2]
    sphere.point_arrays['test'] = scalars  #allow to test scalars
    sphere.set_active_scalars('test')

    uniform.set_active_scalars("Spatial Cell Data")

    #test datasetmapper
    threshed = uniform.threshold_percent([0.15, 0.50], invert=True)
    bodies = threshed.split_bodies()
    mapper = vtk.vtkCompositePolyDataMapper2()
    mapper.SetInputDataObject(0, bodies)
    multiblock = vtk.vtkActor()
    multiblock.SetMapper(mapper)

    pl = pv.Plotter()
    pl.add_mesh(globe)
    pl.add_mesh(sphere)
    pl.add_mesh(uniform)
    pl.add_actor(multiblock)
    pl.add_volume(head)
    return pl.ren_win
Пример #3
0
    def __init__(self, parent=None):
        super(VTKViewer, self).__init__(parent)

        self.slicePosition = 0
        self.tubeBlocks = None
        self.volume = None
        self.sliceActor = None

        self.hbox = QHBoxLayout(self)

        self.sliceView = QVTKRenderWindowInteractor(self)
        self.hbox.addWidget(self.sliceView)

        self.sliceSlider = SliceSlider(self)
        self.hbox.addWidget(self.sliceSlider)

        self.volumeView = QVTKRenderWindowInteractor(self)
        self.hbox.addWidget(self.volumeView)

        self.sliceSlider.slicePosChanged.connect(self.updateSlice)

        # vtk-producers and filters
        self.producer = vtk.vtkTrivialProducer()
        self.reslice = vtk.vtkImageReslice()
        self.image2worldTransform = vtk.vtkTransform()
        self.tubeProducer = vtk.vtkTrivialProducer()
        self.tubeMapper = vtk.vtkCompositePolyDataMapper2()
        self.tubeActor = vtk.vtkActor()
def main():
    colors = vtk.vtkNamedColors()

    # Create Sphere 1.
    sphere1 = vtk.vtkSphereSource()
    sphere1.SetRadius(3)
    sphere1.SetCenter(0, 0, 0)
    sphere1.Update()

    # Create Sphere 2.
    sphere2 = vtk.vtkSphereSource()
    sphere2.SetRadius(2)
    sphere2.SetCenter(2, 0, 0)
    sphere2.Update()

    mbds = vtk.vtkMultiBlockDataSet()
    mbds.SetNumberOfBlocks(3)
    mbds.SetBlock(0, sphere1.GetOutput())
    # Leave block 1 NULL.  NULL blocks are valid and should be handled by
    # algorithms that process multiblock datasets.  Especially when
    # running in parallel where the blocks owned by other processes are
    # NULL in this process.
    mbds.SetBlock(2, sphere2.GetOutput())

    mapper = vtk.vtkCompositePolyDataMapper2()
    mapper.SetInputDataObject(mbds)
    cdsa = vtk.vtkCompositeDataDisplayAttributes()
    mapper.SetCompositeDataDisplayAttributes(cdsa)

    # You can use the vtkCompositeDataDisplayAttributes to set the color,
    # opacity and visibiliy of individual blocks of the multiblock dataset.
    # Attributes are mapped by block pointers (vtkDataObject*), so these can
    # be queried by their flat index through a convenience function in the
    # attribute class (vtkCompositeDataDisplayAttributes::DataObjectFromIndex).
    # Alternatively, one can set attributes directly through the mapper using
    # flat indices.
    #
    # This sets the block at flat index 3 red
    # Note that the index is the flat index in the tree, so the whole multiblock
    # is index 0 and the blocks are flat indexes 1, 2 and 3.  This affects
    # the block returned by mbds.GetBlock(2).
    mapper.SetBlockColor(3, colors.GetColor3d("Red"))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    # Create the Renderer, RenderWindow, and RenderWindowInteractor.
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # Enable user interface interactor.
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d("SteelBlue"))
    renderWindow.Render()
    renderWindowInteractor.Start()
Пример #5
0
def process_tre(in_file, out_dir, phi_vals, theta_vals, width, height):
    import itk, vtk
    from vtk.web.dataset_builder import ImageDataSetBuilder

    reader = itk.SpatialObjectReader[3].New()
    reader.SetFileName(in_file)
    reader.Update()
    blocks = vtk.vtkMultiBlockDataSet()
    tubeGroup = reader.GetGroup()
    for tube in _iter_tubes(tubeGroup):
        polydata = _tube_to_polydata(tube)
        index = blocks.GetNumberOfBlocks()
        blocks.SetBlock(index, polydata)

    prod = vtk.vtkTrivialProducer()
    prod.SetOutput(blocks)

    mapper = vtk.vtkCompositePolyDataMapper2()
    mapper.SetInputConnection(prod.GetOutputPort())

    cdsa = vtk.vtkCompositeDataDisplayAttributes()
    cdsa.SetBlockColor(1, (1, 1, 0))
    cdsa.SetBlockColor(2, (0, 1, 1))

    mapper.SetCompositeDataDisplayAttributes(cdsa)

    window = vtk.vtkRenderWindow()
    window.SetSize(width, height)

    renderer = vtk.vtkRenderer()
    window.AddRenderer(renderer)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    renderer.AddActor(actor)
    renderer.ResetCamera()

    window.Render()

    idb = ImageDataSetBuilder(out_dir, 'image/jpg', {
        'type': 'spherical',
        'phi': phi_vals,
        'theta': theta_vals
    })

    idb.start(window, renderer)

    idb.writeImages()
    idb.stop()
Пример #6
0
def tracts_root(out_list):
    # create tracts only when at least one was computed
    if not out_list.count(None) == len(out_list):
        root = vtk.vtkMultiBlockDataSet()

        for n, tube in enumerate(out_list):
            if tube:
                root.SetBlock(n, tube.GetOutput())

        # https://lorensen.github.io/VTKExamples/site/Python/CompositeData/CompositePolyDataMapper/
        mapper = vtk.vtkCompositePolyDataMapper2()
        mapper.SetInputDataObject(root)

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

    return actor
Пример #7
0
def visualize_tracts(out_list):
    # Create a single block dataset from all the tubes representing the tracts
    # One actor is easier to manipulate
    # Create tracts only when at least one was computed
    if not out_list.count(None) == len(out_list):
        root = vtk.vtkMultiBlockDataSet()

        for n, tube in enumerate(out_list):
            if tube:
                root.SetBlock(n, tube)

        # https://lorensen.github.io/VTKExamples/site/Python/CompositeData/CompositePolyDataMapper/
        mapper = vtk.vtkCompositePolyDataMapper2()
        mapper.SetInputDataObject(root)

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

    return actor
Пример #8
0
def visualize_tracts(out_list, inp, msg):
    renderer, renderWindow = inp
    # create tracts only when at least one was computed
    if not out_list.count(None) == len(out_list):
        root = vtk.vtkMultiBlockDataSet()

        for n, tube in enumerate(out_list):
            if tube:
                root.SetBlock(n, tube.GetOutput())

        # https://lorensen.github.io/VTKExamples/site/Python/CompositeData/CompositePolyDataMapper/
        mapper = vtk.vtkCompositePolyDataMapper2()
        mapper.SetInputDataObject(root)

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

    # render_actors(actor, renderer, interactor)
    renderer.AddActor(actor)
    renderWindow.Render()

    print(f"Visualization sent by: {msg}")
Пример #9
0
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.reader = vtk.vtkOpenFOAMReader()
        self.filter2 = vtk.vtkGeometryFilter()
        self.mapper = vtk.vtkCompositePolyDataMapper2()
        self.actor = vtk.vtkActor()

        self.widget = QVTKRenderWindowInteractor(self)
        self.widget.Initialize()
        ##背景色の設定
        self.ren = vtk.vtkRenderer()
        self.widget.GetRenderWindow().AddRenderer(self.ren)
        self.widget.Start()
        self.widget.show()

        self.setCentralWidget(self.widget)
        #UIのインスタンスを介してアイコンと実体(関数)をつなげる
        self.ui.actionOpenFile.triggered.connect(self.openFile)
        self.ui.actionResetFile.triggered.connect(self.resetFile)
        self.setupInitView()
Пример #10
0
    # Pick in between the stacks
    # note: the ray passes between the data sets but the block's bounding box encompasses both data sets
    ([18., -1.], (True, 2), (False, -1, -1), (False, -1, -1, -1)),
    ([18., -3.], (False, -1), (False, -1, -1), (False, -1, -1, -1)),

    # Pick the second stack
    ([25., 0.], (True, 1), (True, 1, 5), (True, 1, 1, 5)),
    # note: the prop pick hits block #1 because the bounding box of block #1 is picked. The cell picker does not hit a cell of block #1
    ([28., 2.], (True, 1), (True, 2, 6), (True, 2, 2, 6))
]

# Construct the data-set, and set up color mapping of the blocks
mbd = vtk.vtkMultiBlockDataSet()
mbd.SetNumberOfBlocks(3)
cda = vtk.vtkCompositeDataDisplayAttributes()
m = vtk.vtkCompositePolyDataMapper2()
m.SetInputDataObject(mbd)
m.SetCompositeDataDisplayAttributes(cda)

for blk in range(0, 3):
    mbd.SetBlock(blk, vtk.vtkPolyData())
    poly = mbd.GetBlock(blk)
    coords = xyz[blk]
    pts = vtk.vtkPoints()
    for coord in coords:
        pts.InsertNextPoint(coord[0], coord[1], float(blk))

    polys = vtk.vtkCellArray()
    for cell in polyconn[blk]:
        polys.InsertNextCell(len(cell))
        for pid in cell:
Пример #11
0
#!/usr/bin/env python
import vtk
from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot

VTK_DATA_ROOT = vtkGetDataRoot()

# Read some Fluent UCD data in ASCII form
r = vtk.vtkFLUENTReader()
r.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/room.cas")
r.EnableAllCellArrays()

g = vtk.vtkGeometryFilter()
g.SetInputConnection(r.GetOutputPort())

FluentMapper = vtk.vtkCompositePolyDataMapper2()
FluentMapper.SetInputConnection(g.GetOutputPort())
FluentMapper.SetScalarModeToUseCellFieldData()
FluentMapper.SelectColorArray("PRESSURE")
FluentMapper.SetScalarRange(-31, 44)
FluentActor = vtk.vtkActor()
FluentActor.SetMapper(FluentMapper)

ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# Add the actors to the renderer, set the background and size
#
ren1.AddActor(FluentActor)
reader.SetPointResultArrayStatus("CH4", 1)
reader.Update()

tree = vtk.vtkSpanSpace()

#contour = vtk.vtkSMPContourGrid()
contour = vtk.vtkContourGrid()
contour.SetInputConnection(reader.GetOutputPort())
contour.SetInputArrayToProcess(0, 0, 0, vtk.vtkAssignAttribute.POINT_DATA, "CH4")
contour.SetValue(0, 0.000718448)
#contour.MergePiecesOff()
contour.UseScalarTreeOn()
contour.SetScalarTree(tree)
contour.Update()

mapper = vtk.vtkCompositePolyDataMapper2()
mapper.SetInputConnection(contour.GetOutputPort())
mapper.ScalarVisibilityOff()

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create graphics stuff
#
ren1 = vtk.vtkRenderer()
ren1.AddActor(actor)
ren1.ResetCamera()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
renWin.SetSize(sze, sze)
Пример #13
0
inputMBDConverter = vtk.vtkConvertToMultiBlockDataSet()
inputMBDConverter.SetInputConnection(inputPDCReader.GetOutputPort())

# The cut plane
plane = vtk.vtkPlane()
plane.SetOrigin(0, 0, 0)
plane.SetNormal(1, 1, 1)

# Accelerated cutter 0
cut0 = vtk.vtkPlaneCutter()
cut0.SetInputConnection(inputMBDConverter.GetOutputPort())
cut0.SetPlane(plane)
cut0.ComputeNormalsOff()

sCutterMapper0 = vtk.vtkCompositePolyDataMapper2()
sCutterMapper0.SetInputConnection(cut0.GetOutputPort())
sCutterMapper0.ScalarVisibilityOff()

sCutterActor0 = vtk.vtkActor()
sCutterActor0.SetMapper(sCutterMapper0)
sCutterActor0.GetProperty().SetColor(1, 1, 1)

# Accelerated cutter 1
cut1 = vtk.vtkPlaneCutter()
cut1.SetInputConnection(inputPDCReader.GetOutputPort())
cut1.SetPlane(plane)
cut1.ComputeNormalsOff()

sCutterMapper1 = vtk.vtkCompositePolyDataMapper2()
sCutterMapper1.SetInputConnection(cut1.GetOutputPort())
Пример #14
0
        transform.Identity()
        transform.Translate(2, t * 2, 0)

        xform = vtk.vtkTransformPolyDataFilter()
        xform.SetTransform(transform)
        xform.SetInputConnection(source.GetOutputPort())
        xform.Update()

        output.ShallowCopy(xform.GetOutputDataObject(0))
        return 1


source1 = MovingSphereSource()
group1 = vtk.vtkGroupTimeStepsFilter()
group1.SetInputConnection(source1.GetOutputPort())
mapper1 = vtk.vtkCompositePolyDataMapper2()
mapper1.SetInputConnection(group1.GetOutputPort())
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper1)

source2 = MovingPDC()
group2 = vtk.vtkGroupTimeStepsFilter()
group2.SetInputConnection(source2.GetOutputPort())
mapper2 = vtk.vtkCompositePolyDataMapper2()
mapper2.SetInputConnection(group2.GetOutputPort())
actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)

ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)