예제 #1
0
    def test2(self):
        global nExecutions

        r = self.createSource()

        nExecutions = 0

        def addToCounter(obj, event):
            global nExecutions
            nExecutions += 1

        r.AddObserver('StartEvent', addToCounter)

        shrink = vtk.vtkShrinkPolyData()
        shrink.SetInputConnection(r.GetOutputPort())
        shrink.UpdateInformation()

        self.assertTrue(
            shrink.GetOutputInformation(0).Has(
                vtk.vtkEnsembleSource.META_DATA()))

        metaData = shrink.GetOutputInformation(0).Has(
            vtk.vtkEnsembleSource.META_DATA())

        shrink.Update()

        oInfo = shrink.GetOutputInformation(0)
        oInfo.Set(vtk.vtkEnsembleSource.UPDATE_MEMBER(), 2)
        shrink.Update()

        output = shrink.GetOutputDataObject(0)

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[2])

        shrink.Update()

        oInfo = shrink.GetOutputInformation(0)
        oInfo.Set(vtk.vtkEnsembleSource.UPDATE_MEMBER(), 1)
        shrink.Update()

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[1])

        shrink2 = vtk.vtkShrinkPolyData()
        shrink2.SetInputConnection(r.GetOutputPort())

        shrink2.Update()

        output2 = shrink2.GetOutputDataObject(0)

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[1])

        self.assertEquals(nExecutions, 3)
예제 #2
0
파일: TestEnsemble.py 프로젝트: 0004c/VTK
    def test2(self):
        global nExecutions

        r = self.createSource()

        nExecutions = 0
        def addToCounter(obj, event):
            global nExecutions
            nExecutions += 1
        r.AddObserver('StartEvent', addToCounter)

        shrink = vtk.vtkShrinkPolyData()
        shrink.SetInputConnection(r.GetOutputPort())
        shrink.UpdateInformation()

        self.assertTrue(shrink.GetOutputInformation(0).Has(vtk.vtkEnsembleSource.META_DATA()))

        metaData = shrink.GetOutputInformation(0).Has(vtk.vtkEnsembleSource.META_DATA())

        shrink.Update()

        oInfo = shrink.GetOutputInformation(0)
        oInfo.Set(vtk.vtkEnsembleSource.UPDATE_MEMBER(), 2)
        shrink.Update()

        output = shrink.GetOutputDataObject(0)

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[2])

        shrink.Update()

        oInfo = shrink.GetOutputInformation(0)
        oInfo.Set(vtk.vtkEnsembleSource.UPDATE_MEMBER(), 1)
        shrink.Update()

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[1])

        shrink2 = vtk.vtkShrinkPolyData()
        shrink2.SetInputConnection(r.GetOutputPort())

        shrink2.Update()

        output2 = shrink2.GetOutputDataObject(0)

        self.assertEquals(output.GetNumberOfCells(), TestEnsemble.npolys[1])

        self.assertEquals(nExecutions, 3)
예제 #3
0
def main():
    colors = vtk.vtkNamedColors()

    camera = vtk.vtkCamera()
    camera.SetClippingRange(0.1, 0.4)
    planesArray = [0] * 24

    camera.GetFrustumPlanes(1.0, planesArray)

    planes = vtk.vtkPlanes()
    planes.SetFrustumPlanes(planesArray)

    frustumSource = vtk.vtkFrustumSource()
    frustumSource.ShowLinesOff()
    frustumSource.SetPlanes(planes)

    shrink = vtk.vtkShrinkPolyData()
    shrink.SetInputConnection(frustumSource.GetOutputPort())
    shrink.SetShrinkFactor(.9)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(shrink.GetOutputPort())

    back = vtk.vtkProperty()
    back.SetColor(colors.GetColor3d("Tomato"))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
    actor.SetBackfaceProperty(back)

    # a renderer and render window
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Frustum")
    renderWindow.AddRenderer(renderer)

    # an interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # add the actors to the scene
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d("Silver"))

    # Position the camera so that we can see the frustum
    renderer.GetActiveCamera().SetPosition(1, 0, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 1, 0)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCamera()

    # render an image (lights and cameras are created automatically)
    renderWindow.Render()

    # begin mouse interaction
    renderWindowInteractor.Start()
예제 #4
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkShrinkPolyData(),
                                       'Processing.', ('vtkPolyData', ),
                                       ('vtkPolyData', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
예제 #5
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self,
         module_manager,
         vtk.vtkShrinkPolyData(),
         "Processing.",
         ("vtkPolyData",),
         ("vtkPolyData",),
         replaceDoc=True,
         inputFunctions=None,
         outputFunctions=None,
     )
예제 #6
0
 def RequestData(self, request, inInfo, outInfo):
     print("MyFilter RequestData:")
     inInfo0 = inInfo[0].GetInformationObject(0)
     outInfo0 = outInfo.GetInformationObject(0)
     _input = inInfo0.Get(vtk.vtkDataObject.DATA_OBJECT())
     output = outInfo0.Get(
         vtk.vtkDataObject.DATA_OBJECT())  # EXTRACT OUTPUT
     sh = vtk.vtkShrinkPolyData()
     sh.SetInputData(_input)
     sh.Update()
     output.ShallowCopy(sh.GetOutput())
     return 1
예제 #7
0
def shrink(actor, fraction=0.85):  # N.B. input argument gets modified
    '''Shrink the triangle polydata in the representation of actor'''

    poly = polydata(actor, True)
    shrink = vtk.vtkShrinkPolyData()
    setInput(shrink, poly)
    shrink.SetShrinkFactor(fraction)
    shrink.Update()
    mapper = actor.GetMapper()
    setInput(mapper, shrink.GetOutput())
    mapper.Update()
    actor.Modified()
    return actor  # return same obj for concatenation
예제 #8
0
파일: meshes.py 프로젝트: pattonw/sarbor
def visualize_mesh(contour, cutoff=0.5):
    # visualize a volume
    aTransform = vtk.vtkTransform()
    aTransform.Scale(1, 1, 1)
    transFilter = vtk.vtkTransformFilter()
    transFilter.SetInputConnection(contour.GetOutputPort())
    transFilter.SetTransform(aTransform)
    aShrinker = vtk.vtkShrinkPolyData()
    aShrinker.SetShrinkFactor(1)
    aShrinker.SetInputConnection(transFilter.GetOutputPort())
    aMapper = vtk.vtkPolyDataMapper()
    aMapper.ScalarVisibilityOff()
    aMapper.SetInputConnection(transFilter.GetOutputPort())
    Triangles = vtk.vtkActor()
    Triangles.SetMapper(aMapper)
    Triangles.GetProperty().SetDiffuseColor(lime_green)
    Triangles.GetProperty().SetOpacity(0.2)
    Triangles.SetScale

    # Create the Renderer, RenderWindow, and RenderWindowInteractor
    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    renWin.SetSize(640, 480)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    # Add the actors to the renderer
    ren.AddActor(Triangles)

    # Set the background color.
    ren.SetBackground(slate_grey)

    # Position the camera.
    ren.ResetCamera()
    ren.GetActiveCamera().Dolly(1.2)
    ren.GetActiveCamera().Azimuth(30)
    ren.GetActiveCamera().Elevation(20)
    ren.ResetCameraClippingRange()

    iren.Initialize()
    renWin.Render()
    iren.Start()
예제 #9
0
def main():
    colors = vtk.vtkNamedColors()

    # Create a pentagon
    polygonSource = vtk.vtkRegularPolygonSource()
    polygonSource.SetNumberOfSides(5)
    polygonSource.SetRadius(5)
    polygonSource.SetCenter(0, 0, 0)

    shrink = vtk.vtkShrinkPolyData()
    shrink.SetInputConnection(polygonSource.GetOutputPort())
    shrink.SetShrinkFactor(.9)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(shrink.GetOutputPort())

    back = vtk.vtkProperty()
    back.SetColor(colors.GetColor3d("Tomato"))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetLineWidth(5)
    actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
    actor.SetBackfaceProperty(back)

    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Regular Polygon Source")
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d("Silver"))

    renderWindow.Render()
    renderWindowInteractor.Start()
예제 #10
0
파일: visualizer.py 프로젝트: Na-Z/3DMVGOD
def visualize_one_frustum(p_planes):

    colors = vtk.vtkNamedColors()

    planesArray = list(p_planes.flatten())
    planes = vtk.vtkPlanes()
    planes.SetFrustumPlanes(planesArray)

    frustumSource = vtk.vtkFrustumSource()
    frustumSource.ShowLinesOff()
    frustumSource.SetPlanes(planes)

    shrink = vtk.vtkShrinkPolyData()
    shrink.SetInputConnection(frustumSource.GetOutputPort())
    shrink.SetShrinkFactor(1.)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(shrink.GetOutputPort())

    back = vtk.vtkProperty()
    back.SetColor(colors.GetColor3d("Tomato"))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
    actor.SetBackfaceProperty(back)

    # a renderer and render window
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Frustum")
    renderWindow.AddRenderer(renderer)

    # an interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetInteractorStyle(
        vtk.vtkInteractorStyleTrackballCamera())
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # add the actors to the scene
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d("Silver"))

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)
    axes = vtk.vtkAxesActor()
    #  The axes are positioned with a user transform
    axes.SetAxisLabels(1)
    axes.SetUserTransform(transform)
    axes.AxisLabelsOff()
    axes.SetTotalLength(3.0, 3.0, 3.0)
    renderer.AddActor(axes)

    # Position the camera so that we can see the frustum
    renderer.GetActiveCamera().SetPosition(1, 0, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 1, 0)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCamera()

    # render an image (lights and cameras are created automatically)
    renderWindow.Render()

    # begin mouse interaction
    renderWindowInteractor.Start()
예제 #11
0
파일: test.py 프로젝트: sivaroot/ProjectCPE
triangleEdgeTubes.SetRadius(.005)
triangleEdgeTubes.SetNumberOfSides(6)
triangleEdgeTubes.UseDefaultNormalOn()
triangleEdgeTubes.SetDefaultNormal(.577, .577, .577)
triangleEdgeMapper = vtk.vtkPolyDataMapper()
triangleEdgeMapper.SetInputConnection(triangleEdgeTubes.GetOutputPort())
triangleEdgeMapper.ScalarVisibilityOff()
triangleEdgeActor = vtk.vtkActor()
triangleEdgeActor.SetMapper(triangleEdgeMapper)
triangleEdgeActor.GetProperty().SetDiffuseColor(lamp_black)
triangleEdgeActor.GetProperty().SetSpecular(.4)
triangleEdgeActor.GetProperty().SetSpecularPower(10)

# Shrink the triangles we found earlier.  Create the associated mapper
# and actor.  Set the opacity of the shrunken triangles.
aShrinker = vtk.vtkShrinkPolyData()
aShrinker.SetShrinkFactor(1)
aShrinker.SetInputConnection(Marching.GetOutputPort())
aMapper = vtk.vtkPolyDataMapper()
aMapper.ScalarVisibilityOff()
aMapper.SetInputConnection(aShrinker.GetOutputPort())
Triangles = vtk.vtkActor()
Triangles.SetMapper(aMapper)
Triangles.GetProperty().SetDiffuseColor(banana)
Triangles.GetProperty().SetOpacity(.6)

# Draw a cube the same size and at the same position as the one
# created previously.  Extract the edges because we only want to see
# the outline of the cube.  Pass the edges through a vtkTubeFilter so
# they are displayed as tubes rather than lines.
CubeModel = vtk.vtkCubeSource()
예제 #12
0
파일: visualizer.py 프로젝트: Na-Z/3DMVGOD
def visualize_n_frustums_plus_ptclouds(p_planes_list, frustum_ptcloud):
    colors = vtk.vtkNamedColors()

    # set a renderer and a render window
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d("Silver"))
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetSize(600, 600)
    renderWindow.SetWindowName("Frustum Intersection")
    renderWindow.AddRenderer(renderer)

    # an interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetInteractorStyle(
        vtk.vtkInteractorStyleTrackballCamera())
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # add n frustums
    for i, p_planes in enumerate(p_planes_list):

        planesArray = list(p_planes.flatten())
        planes = vtk.vtkPlanes()
        planes.SetFrustumPlanes(planesArray)

        frustum = vtk.vtkFrustumSource()
        frustum.ShowLinesOff()
        frustum.SetPlanes(planes)

        shrink = vtk.vtkShrinkPolyData()
        shrink.SetInputConnection(frustum.GetOutputPort())
        shrink.SetShrinkFactor(1.)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(shrink.GetOutputPort())

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().EdgeVisibilityOn()
        actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
        actor.GetProperty().SetOpacity(.2)

        renderer.AddActor(actor)

    # add ptclouds
    # assin color to ptcloud (n,4)
    cm = colormap.get_cmap('RdBu')
    color_mapper = colormap.ScalarMappable(norm=matplotlib.colors.Normalize(
        vmin=0, vmax=1, clip=True),
                                           cmap=cm)
    color = color_mapper.to_rgba(frustum_ptcloud[:, 3])[:, :3] * 255
    ptcloud = np.hstack((frustum_ptcloud[:, 0:3], color))

    # Create the geometry of a point (the coordinate)
    points = vtk.vtkPoints()
    # Create the topology of the point (a vertex)
    vertices = vtk.vtkCellArray()
    # Setup colors
    Colors = vtk.vtkUnsignedCharArray()
    Colors.SetNumberOfComponents(3)
    Colors.SetName("Colors")
    # Add points
    for i in range(0, len(ptcloud)):
        p = ptcloud[i, :3]
        id = points.InsertNextPoint(p)
        vertices.InsertNextCell(1)
        vertices.InsertCellPoint(id)
        Colors.InsertNextTuple3(ptcloud[i, 3], ptcloud[i, 4], ptcloud[i, 5])
    point = vtk.vtkPolyData()
    # Set the points and vertices we created as the geometry and topology of the polydata
    point.SetPoints(points)
    point.SetVerts(vertices)
    point.GetPointData().SetScalars(Colors)
    point.Modified()

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(point)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
    actor.GetProperty().SetOpacity(.3)

    renderer.AddActor(actor)

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)
    axes = vtk.vtkAxesActor()
    #  The axes are positioned with a user transform
    axes.SetAxisLabels(1)
    axes.SetUserTransform(transform)
    axes.AxisLabelsOff()
    axes.SetTotalLength(3.0, 3.0, 3.0)
    renderer.AddActor(axes)

    renderer.SetUseDepthPeeling(1)
    renderer.SetOcclusionRatio(0.1)
    renderer.SetMaximumNumberOfPeels(100)
    renderWindow.SetMultiSamples(0)
    renderWindow.SetAlphaBitPlanes(1)

    # Position the camera so that we can see the frustum
    renderer.GetActiveCamera().SetPosition(1, 0, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 1, 0)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCamera()

    # render an image (lights and cameras are created automatically)
    renderWindow.Render()

    # begin mouse interaction
    renderWindowInteractor.Start()
예제 #13
0
appendF.AddInputData(QLWedgeGrid)
appendF.AddInputData(aTriGrid)
appendF.AddInputData(aQuadGrid)
appendF.AddInputData(aTetGrid)
appendF.AddInputData(aHexGrid)
appendF.AddInputData(TQHexGrid)
appendF.AddInputData(BQHexGrid)
appendF.AddInputData(aWedgeGrid)
appendF.AddInputData(BQWedgeGrid)
appendF.AddInputData(aPyramidGrid)
appendF.AddInputData(aTQPyramidGrid)

# Extract the edges
extract = vtk.vtkExtractEdges()
extract.SetInputConnection(appendF.GetOutputPort())
shrink = vtk.vtkShrinkPolyData()
shrink.SetInputConnection(extract.GetOutputPort())
shrink.SetShrinkFactor(0.90)
aMapper = vtk.vtkDataSetMapper()
aMapper.SetInputConnection(shrink.GetOutputPort())
aActor = vtk.vtkActor()
aActor.SetMapper(aMapper)
aActor.GetProperty().SetRepresentationToWireframe()

# Create the rendering related stuff.
ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren1.SetBackground(.1, .2, .3)
예제 #14
0
triangleEdgeTubes.SetRadius(.005)
triangleEdgeTubes.SetNumberOfSides(6)
triangleEdgeTubes.UseDefaultNormalOn()
triangleEdgeTubes.SetDefaultNormal(.577, .577, .577)
triangleEdgeMapper = vtk.vtkPolyDataMapper()
triangleEdgeMapper.SetInputConnection(triangleEdgeTubes.GetOutputPort())
triangleEdgeMapper.ScalarVisibilityOff()
triangleEdgeActor = vtk.vtkActor()
triangleEdgeActor.SetMapper(triangleEdgeMapper)
triangleEdgeActor.GetProperty().SetDiffuseColor(lamp_black)
triangleEdgeActor.GetProperty().SetSpecular(.4)
triangleEdgeActor.GetProperty().SetSpecularPower(10)

# Shrink the triangles we found earlier.  Create the associated mapper
# and actor.  Set the opacity of the shrunken triangles.
aShrinker = vtk.vtkShrinkPolyData()
aShrinker.SetShrinkFactor(1)
aShrinker.SetInputConnection(Marching.GetOutputPort())
aMapper = vtk.vtkPolyDataMapper()
aMapper.ScalarVisibilityOff()
aMapper.SetInputConnection(aShrinker.GetOutputPort())
Triangles = vtk.vtkActor()
Triangles.SetMapper(aMapper)
Triangles.GetProperty().SetDiffuseColor(banana)
Triangles.GetProperty().SetOpacity(.6)

# Draw a cube the same size and at the same position as the one
# created previously.  Extract the edges because we only want to see
# the outline of the cube.  Pass the edges through a vtkTubeFilter so
# they are displayed as tubes rather than lines.
CubeModel = vtk.vtkCubeSource()
VTK_DATA_ROOT = vtkGetDataRoot()

# This creates a polygonal cylinder model with eight circumferential
# facets.
part = vtk.vtkSTLReader()
part.SetFileName(VTK_DATA_ROOT + "/Data/42400-IDGH.stl")

# A filter is a module that takes at least one input and produces at
# least one output. The SetInput and GetOutput methods are used to do
# the connection. What is returned by GetOutput is a particulat
# dataset type. If the type is compatible with the SetInput method,
# then the filters can be connected together.
#
# Here we add a filter that computes surface normals from the geometry.
shrink = vtk.vtkShrinkPolyData()
shrink.SetInputConnection(part.GetOutputPort())
shrink.SetShrinkFactor(0.85)

# The mapper is responsible for pushing the geometry into the graphics
# library. It may also do color mapping, if scalars or other
# attributes are defined.
partMapper = vtk.vtkPolyDataMapper()
partMapper.SetInputConnection(shrink.GetOutputPort())

# The LOD actor is a special type of actor. It will change appearance
# in order to render faster. At the highest resolution, it renders
# ewverything just like an actor. The middle level is a point cloud,
# and the lowest level is a simple bounding box.
partActor = vtk.vtkLODActor()
partActor.SetMapper(partMapper)
예제 #16
0
def marching_cubes(mcCases):
    color = vtk.vtkNamedColors()

    renWin = vtk.vtkRenderWindow()
    renWin.SetSize(640, 480)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    renderers = list()
    gridSize = ((len(mcCases) + 3) // 4) * 4
    if len(mcCases) < 4:
        gridSize = len(mcCases)
    print("gridSize:", gridSize)
    for i in range(0, gridSize):
        # Create the Renderer
        renderer = vtk.vtkRenderer()
        renderers.append(renderer)
        renWin.AddRenderer(renderer)

    for i in range(0, len(mcCases)):
        # Define a Single Cube
        Scalars = vtk.vtkFloatArray()
        Scalars.InsertNextValue(1.0)
        Scalars.InsertNextValue(0.0)
        Scalars.InsertNextValue(0.0)
        Scalars.InsertNextValue(1.0)
        Scalars.InsertNextValue(0.0)
        Scalars.InsertNextValue(0.0)
        Scalars.InsertNextValue(0.0)
        Scalars.InsertNextValue(0.0)

        Points = vtk.vtkPoints()
        Points.InsertNextPoint(0, 0, 0)
        Points.InsertNextPoint(1, 0, 0)
        Points.InsertNextPoint(1, 1, 0)
        Points.InsertNextPoint(0, 1, 0)
        Points.InsertNextPoint(0, 0, 1)
        Points.InsertNextPoint(1, 0, 1)
        Points.InsertNextPoint(1, 1, 1)
        Points.InsertNextPoint(0, 1, 1)

        Ids = vtk.vtkIdList()
        Ids.InsertNextId(0)
        Ids.InsertNextId(1)
        Ids.InsertNextId(2)
        Ids.InsertNextId(3)
        Ids.InsertNextId(4)
        Ids.InsertNextId(5)
        Ids.InsertNextId(6)
        Ids.InsertNextId(7)

        Grid = vtk.vtkUnstructuredGrid()
        Grid.Allocate(10, 10)
        Grid.InsertNextCell(12, Ids)
        Grid.SetPoints(Points)
        Grid.GetPointData().SetScalars(Scalars)

        # Find the triangles that lie along the 0.5 contour in this cube.
        Marching = vtk.vtkContourFilter()
        Marching.SetInputData(Grid)
        Marching.SetValue(0, 0.5)
        Marching.Update()

        # Extract the edges of the triangles just found.
        triangleEdges = vtk.vtkExtractEdges()
        triangleEdges.SetInputConnection(Marching.GetOutputPort())

        # Draw the edges as tubes instead of lines.  Also create the associated
        # mapper and actor to display the tubes.
        triangleEdgeTubes = vtk.vtkTubeFilter()
        triangleEdgeTubes.SetInputConnection(triangleEdges.GetOutputPort())
        triangleEdgeTubes.SetRadius(.005)
        triangleEdgeTubes.SetNumberOfSides(6)
        triangleEdgeTubes.UseDefaultNormalOn()
        triangleEdgeTubes.SetDefaultNormal(.577, .577, .577)

        triangleEdgeMapper = vtk.vtkPolyDataMapper()
        triangleEdgeMapper.SetInputConnection(
            triangleEdgeTubes.GetOutputPort())
        triangleEdgeMapper.ScalarVisibilityOff()

        triangleEdgeActor = vtk.vtkActor()
        triangleEdgeActor.SetMapper(triangleEdgeMapper)
        triangleEdgeActor.GetProperty().SetDiffuseColor(
            color.GetColor3d("lamp_black"))
        triangleEdgeActor.GetProperty().SetSpecular(.4)
        triangleEdgeActor.GetProperty().SetSpecularPower(10)

        # Shrink the triangles we found earlier.  Create the associated mapper
        # and actor.  Set the opacity of the shrunken triangles.
        aShrinker = vtk.vtkShrinkPolyData()
        aShrinker.SetShrinkFactor(1)
        aShrinker.SetInputConnection(Marching.GetOutputPort())

        aMapper = vtk.vtkPolyDataMapper()
        aMapper.ScalarVisibilityOff()
        aMapper.SetInputConnection(aShrinker.GetOutputPort())

        Triangles = vtk.vtkActor()
        Triangles.SetMapper(aMapper)
        Triangles.GetProperty().SetDiffuseColor(color.GetColor3d("banana"))
        Triangles.GetProperty().SetOpacity(.6)

        # Draw a cube the same size and at the same position as the one
        # created previously.  Extract the edges because we only want to see
        # the outline of the cube.  Pass the edges through a vtkTubeFilter so
        # they are displayed as tubes rather than lines.
        CubeModel = vtk.vtkCubeSource()
        CubeModel.SetCenter(.5, .5, .5)

        Edges = vtk.vtkExtractEdges()
        Edges.SetInputConnection(CubeModel.GetOutputPort())

        Tubes = vtk.vtkTubeFilter()
        Tubes.SetInputConnection(Edges.GetOutputPort())
        Tubes.SetRadius(.01)
        Tubes.SetNumberOfSides(6)
        Tubes.UseDefaultNormalOn()
        Tubes.SetDefaultNormal(.577, .577, .577)
        # Create the mapper and actor to display the cube edges.
        TubeMapper = vtk.vtkPolyDataMapper()
        TubeMapper.SetInputConnection(Tubes.GetOutputPort())
        CubeEdges = vtk.vtkActor()
        CubeEdges.SetMapper(TubeMapper)
        CubeEdges.GetProperty().SetDiffuseColor(color.GetColor3d("khaki"))
        CubeEdges.GetProperty().SetSpecular(.4)
        CubeEdges.GetProperty().SetSpecularPower(10)

        # Create a sphere to use as a glyph source for vtkGlyph3D.
        Sphere = vtk.vtkSphereSource()
        Sphere.SetRadius(0.04)
        Sphere.SetPhiResolution(20)
        Sphere.SetThetaResolution(20)
        # Remove the part of the cube with data values below 0.5.
        ThresholdIn = vtk.vtkThresholdPoints()
        ThresholdIn.SetInputData(Grid)
        ThresholdIn.ThresholdByUpper(.5)
        # Display spheres at the vertices remaining in the cube data set after
        # it was passed through vtkThresholdPoints.
        Vertices = vtk.vtkGlyph3D()
        Vertices.SetInputConnection(ThresholdIn.GetOutputPort())
        Vertices.SetSourceConnection(Sphere.GetOutputPort())
        # Create a mapper and actor to display the glyphs.
        SphereMapper = vtk.vtkPolyDataMapper()
        SphereMapper.SetInputConnection(Vertices.GetOutputPort())
        SphereMapper.ScalarVisibilityOff()

        CubeVertices = vtk.vtkActor()
        CubeVertices.SetMapper(SphereMapper)
        CubeVertices.GetProperty().SetDiffuseColor(color.GetColor3d("tomato"))

        # Define the text for the label
        caseLabel = vtk.vtkVectorText()
        caseLabel.SetText("Case 1")

        # Set up a transform to move the label to a new position.
        aLabelTransform = vtk.vtkTransform()
        aLabelTransform.Identity()
        aLabelTransform.Translate(-0.2, 0, 1.25)
        aLabelTransform.Scale(.05, .05, .05)

        # Move the label to a new position.
        labelTransform = vtk.vtkTransformPolyDataFilter()
        labelTransform.SetTransform(aLabelTransform)
        labelTransform.SetInputConnection(caseLabel.GetOutputPort())

        # Create a mapper and actor to display the text.
        labelMapper = vtk.vtkPolyDataMapper()
        labelMapper.SetInputConnection(labelTransform.GetOutputPort())

        labelActor = vtk.vtkActor()
        labelActor.SetMapper(labelMapper)

        # Define the base that the cube sits on.  Create its associated mapper
        # and actor.  Set the position of the actor.
        baseModel = vtk.vtkCubeSource()
        baseModel.SetXLength(1.5)
        baseModel.SetYLength(.01)
        baseModel.SetZLength(1.5)

        baseMapper = vtk.vtkPolyDataMapper()
        baseMapper.SetInputConnection(baseModel.GetOutputPort())

        base = vtk.vtkActor()
        base.SetMapper(baseMapper)
        base.SetPosition(.5, -0.09, .5)

        # Set the scalar values for this case of marching cubes.
        # A negative case number will generate a complementary case
        mcCase = mcCases[i]
        if mcCase < 0:
            cases[-mcCase](Scalars, caseLabel, 0, 1)
        else:
            cases[mcCase](Scalars, caseLabel, 1, 0)
        # Force the grid to update.
        Grid.Modified()

        # Add the actors to the renderer
        renderers[i].AddActor(triangleEdgeActor)
        renderers[i].AddActor(base)
        renderers[i].AddActor(labelActor)
        renderers[i].AddActor(CubeEdges)
        renderers[i].AddActor(CubeVertices)
        renderers[i].AddActor(Triangles)
        # Set the background color.
        renderers[i].SetBackground(color.GetColor3d("slate_grey"))

        # Position the camera.
        renderers[i].GetActiveCamera().Dolly(1.2)
        renderers[i].GetActiveCamera().Azimuth(30)
        renderers[i].GetActiveCamera().Elevation(20)
        renderers[i].ResetCamera()
        renderers[i].ResetCameraClippingRange()
        if i > 0:
            renderers[i].SetActiveCamera(renderers[0].GetActiveCamera())

    # Setup viewports for the renderers
    rendererSize = 300
    xGridDimensions = 4
    if len(mcCases) < 4:
        xGridDimensions = len(mcCases)
    yGridDimensions = (len(mcCases) - 1) // 4 + 1
    print("x, y:", xGridDimensions, ",", yGridDimensions)
    renWin.SetSize(rendererSize * xGridDimensions,
                   rendererSize * yGridDimensions)
    for row in range(0, yGridDimensions):
        for col in range(0, xGridDimensions):
            index = row * xGridDimensions + col

            # (xmin, ymin, xmax, ymax)
            viewport = [
                float(col) / xGridDimensions,
                float(yGridDimensions - (row + 1)) / yGridDimensions,
                float(col + 1) / xGridDimensions,
                float(yGridDimensions - row) / yGridDimensions
            ]

            renderers[index].SetViewport(viewport)

    iren.Initialize()
    renWin.Render()
    iren.Start()
예제 #17
0
파일: visualizer.py 프로젝트: Na-Z/3DMVGOD
def visualize_frustums_intersection(p_planes_list, intersections):

    colors = vtk.vtkNamedColors()

    # set a renderer and a render window
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d("Silver"))
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetSize(600, 600)
    renderWindow.SetWindowName("Frustum Intersection plus intersection points")
    renderWindow.AddRenderer(renderer)

    # an interactor
    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetInteractorStyle(
        vtk.vtkInteractorStyleTrackballCamera())
    renderWindowInteractor.SetRenderWindow(renderWindow)

    for i, p_planes in enumerate(p_planes_list):

        planesArray = list(p_planes.flatten())
        planes = vtk.vtkPlanes()
        planes.SetFrustumPlanes(planesArray)

        frustum = vtk.vtkFrustumSource()
        frustum.ShowLinesOff()
        frustum.SetPlanes(planes)

        shrink = vtk.vtkShrinkPolyData()
        shrink.SetInputConnection(frustum.GetOutputPort())
        shrink.SetShrinkFactor(1.)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(shrink.GetOutputPort())

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().EdgeVisibilityOn()
        if i % 2 == 0:
            actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
            actor.GetProperty().SetOpacity(.2)
        else:
            actor.GetProperty().SetColor(colors.GetColor3d("Banana"))
            actor.GetProperty().SetOpacity(.4)

        renderer.AddActor(actor)

    # create sphere for all the intersection point
    for i in range(intersections.shape[0]):
        point = intersections[i]
        sphereSource = vtk.vtkSphereSource()
        sphereSource.SetCenter(point[0], point[1], point[2])
        sphereSource.SetRadius(.05)
        # Make the surface smooth.
        sphereSource.SetPhiResolution(100)
        sphereSource.SetThetaResolution(100)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(sphereSource.GetOutputPort())

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(colors.GetColor3d("DarkGreen"))

        renderer.AddActor(actor)

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)
    axes = vtk.vtkAxesActor()
    #  The axes are positioned with a user transform
    axes.SetAxisLabels(1)
    axes.SetUserTransform(transform)
    axes.AxisLabelsOff()
    axes.SetTotalLength(3.0, 3.0, 3.0)
    renderer.AddActor(axes)

    renderer.SetUseDepthPeeling(1)
    renderer.SetOcclusionRatio(0.1)
    renderer.SetMaximumNumberOfPeels(100)
    renderWindow.SetMultiSamples(0)
    renderWindow.SetAlphaBitPlanes(1)

    # Position the camera so that we can see the frustum
    renderer.GetActiveCamera().SetPosition(1, 0, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 1, 0)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCamera()

    # render an image (lights and cameras are created automatically)
    renderWindow.Render()

    # begin mouse interaction
    renderWindowInteractor.Start()