示例#1
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkSplineFilter(), 'Processing.',
         ('vtkPolyData',), ('vtkPolyData',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
    def get_vertices_flow_actor(self, colors="RGB", opacity=1, linewidth=1, spline_subdiv=None):
        # from dipy.fvtk
        lines = np.swapaxes(self.get_vertices_flow(), 0, 1)
        poly_data = vtk_u.lines_to_vtk_polydata(lines, colors)
        next_input = poly_data

        # use spline interpolation
        if (spline_subdiv is not None) and (spline_subdiv > 0):
            spline_filter = vtk_u.set_input(vtk.vtkSplineFilter(), next_input)
            spline_filter.SetSubdivideToSpecified()
            spline_filter.SetNumberOfSubdivisions(spline_subdiv)
            spline_filter.Update()
            next_input = spline_filter.GetOutputPort()

        poly_mapper = vtk_u.set_input(vtk.vtkPolyDataMapper(), next_input)
        poly_mapper.ScalarVisibilityOn()
        poly_mapper.SetScalarModeToUsePointFieldData()
        poly_mapper.SelectColorArray("Colors")
        # poly_mapper.SetColorModeToMapScalars()
        poly_mapper.Update()

        # Set Actor
        actor = vtk.vtkActor()
        actor.SetMapper(poly_mapper)
        actor.GetProperty().SetLineWidth(linewidth)
        actor.GetProperty().SetOpacity(opacity)
        return actor
    def get_vertices_flow_actor(self, colors="RGB", opacity=1,
                                linewidth=1, spline_subdiv=None):
        # from dipy.fvtk
        lines = np.swapaxes(self.get_vertices_flow(), 0, 1)
        poly_data = vtk_u.lines_to_vtk_polydata(lines, colors)
        next_input = poly_data

        # use spline interpolation
        if (spline_subdiv is not None) and (spline_subdiv > 0):
            spline_filter = vtk_u.set_input(vtk.vtkSplineFilter(), next_input)
            spline_filter.SetSubdivideToSpecified()
            spline_filter.SetNumberOfSubdivisions(spline_subdiv)
            spline_filter.Update()
            next_input = spline_filter.GetOutputPort()

        poly_mapper = vtk_u.set_input(vtk.vtkPolyDataMapper(), next_input)
        poly_mapper.ScalarVisibilityOn()
        poly_mapper.SetScalarModeToUsePointFieldData()
        poly_mapper.SelectColorArray("Colors")
        # poly_mapper.SetColorModeToMapScalars()
        poly_mapper.Update()

        # Set Actor
        actor = vtk.vtkActor()
        actor.SetMapper(poly_mapper)
        actor.GetProperty().SetLineWidth(linewidth)
        actor.GetProperty().SetOpacity(opacity)
        return actor
示例#4
0
def EquispacedSpline(line, l):
    splineFilter = vtk.vtkSplineFilter()
    splineFilter.SetInputData(line)
    splineFilter.SetSubdivideToLength()
    splineFilter.SetLength(l)
    splineFilter.Update()

    return splineFilter.GetOutput()
示例#5
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkSplineFilter(),
                                       'Processing.', ('vtkPolyData', ),
                                       ('vtkPolyData', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
示例#6
0
def get_equal_length_pts(pts, sample_spacing, spline_name="cardinal"):
    """
    given a series of points, return equal spacing sampled points
    using vtk spline to approximate the parametric curve
    """
    from vtk.util.numpy_support import vtk_to_numpy
    polyData = np_to_polydata(pts)
    spline = vtk.vtkSplineFilter()
    spline.SetInputDataObject(polyData)
    spline.SetSpline(VTK_SPLINE_DICT[spline_name]())
    spline.SetSubdivideToLength()
    spline.SetLength(sample_spacing)
    spline.Update()
    equal_length_pts = vtk_to_numpy(spline.GetOutput().GetPoints().GetData())
    return equal_length_pts
示例#7
0
def CreateSpline():

    npts = 25
    vtkPoints = vtk.vtkPoints()
    vtkPoints.SetNumberOfPoints(100)
    for i in range(npts):
        x = math.sin(math.pi * i / 5.)
        y = math.cos(math.pi * i / 5.)
        z = 2 * i / float(npts)
        vtkPoints.SetPoint(i, (x, y, z))

    vtkCellArray = vtk.vtkCellArray()
    vtkCellArray.InsertNextCell(npts)
    for i in range(npts):
        vtkCellArray.InsertCellPoint(i)

#    value = lambda i: math.fabs(math.sin(math.pi*i/30.))
#    vtkFloatArray = vtk.vtkFloatArray()
#    vtkFloatArray.SetNumberOfValues(npts)
#    for i in range(npts):
#        vtkFloatArray.SetValue(i, value(i))

    vtkPolyData = vtk.vtkPolyData()
    vtkPolyData.SetPoints(vtkPoints)
    vtkPolyData.SetLines(vtkCellArray)
    #    vtkPolyData.GetPointData().SetScalars(vtkFloatArray)

    vtkSplineFilter = vtk.vtkSplineFilter()

    if vtk.vtkVersion.GetVTKMajorVersion() == 6:
        vtkSplineFilter.SetInputData(vtkPolyData)
    else:
        vtkSplineFilter.SetInput(vtkPolyData)

    vtkSplineFilter.SetNumberOfSubdivisions(5 * npts)
    vtkSplineFilter.Update()

    vtkTubeFilter = vtk.vtkTubeFilter()
    vtkTubeFilter.SetInputConnection(vtkSplineFilter.GetOutputPort())
    vtkTubeFilter.SetRadius(0.15)
    vtkTubeFilter.SetNumberOfSides(10)
    vtkTubeFilter.CappingOn()
    vtkTubeFilter.Update()

    polydata = vtkTubeFilter.GetOutput()
    #polydata.Update()

    return polydata
示例#8
0
def resample_points(poly):

    vs = vtk.vtkStripper()
    vs.SetInputData(poly)
    vs.JoinContiguousSegmentsOn()
    vs.Update()

    cpd2 = vtk.vtkCleanPolyData()
    cpd2.SetInputData(vs.GetOutput())
    cpd2.Update()

    bc = cpd2.GetOutput().GetBounds()
    yl = bc[3] - bc[2]
    zl = bc[5] - bc[4]
    yzl = math.sqrt(yl**2 + zl**2)

    spline = vtk.vtkCardinalSpline()
    spline.SetLeftConstraint(2)
    spline.SetLeftValue(0)
    spline.SetRightConstraint(2)
    spline.SetRightValue(0)

    sp_filter = vtk.vtkSplineFilter()
    sp_filter.SetInputData(cpd2.GetOutput())
    num_points = poly.GetNumberOfPoints()
    sp_filter.SetNumberOfSubdivisions(num_points * 40)
    sp_filter.SetSpline(spline)
    sp_filter.Update()

    def_tol = yzl / 100

    print('bound diag = ', yzl)
    print('max p dist = ', def_tol)

    cpd = vtk.vtkCleanPolyData()
    cpd.SetInputData(sp_filter.GetOutput())
    cpd.ToleranceIsAbsoluteOn()
    cpd.PointMergingOn()
    cpd.ConvertStripsToPolysOn()
    cpd.GetConvertLinesToPoints()
    cpd.ConvertPolysToLinesOn()
    cpd.ToleranceIsAbsoluteOn()
    cpd.SetAbsoluteTolerance(def_tol)
    cpd.Update()

    return cpd.GetOutput()
示例#9
0
    def Execute(self):

        if self.Surface == None:
            self.PrintError('Error: No input surface.')

        cleaner = vtk.vtkCleanPolyData()
        cleaner.SetInputData(self.Surface)
        cleaner.Update()

        if self.Length == 0.0:
            self.Length = cleaner.GetOutput().GetLength() / 100.0

        splineFilter = vtk.vtkSplineFilter()
        splineFilter.SetInputConnection(cleaner.GetOutputPort())
        splineFilter.SetSubdivideToLength()
        splineFilter.SetLength(self.Length)
        splineFilter.Update()

        self.Surface = splineFilter.GetOutput()
示例#10
0
    def Execute(self):

        if self.Surface == None:
            self.PrintError('Error: No input surface.')

        cleaner = vtk.vtkCleanPolyData()
        cleaner.SetInputData(self.Surface)
        cleaner.Update()

        if self.Length == 0.0:
            self.Length = cleaner.GetOutput().GetLength() / 100.0

        splineFilter = vtk.vtkSplineFilter()
        splineFilter.SetInputConnection(cleaner.GetOutputPort())
        splineFilter.SetSubdivideToLength()
        splineFilter.SetLength(self.Length)
        splineFilter.Update()

        self.Surface = splineFilter.GetOutput()
示例#11
0
    def Execute(self):

        if self.Surface == None:
            self.PrintError('Error: No input surface.')

        cleaner = vtk.vtkCleanPolyData()
        cleaner.SetInput(self.Surface)
        cleaner.Update()

        if self.Length == 0.0:
            self.Length = cleaner.GetOutput().GetLength() / 100.0

        splineFilter = vtk.vtkSplineFilter()
        splineFilter.SetInput(cleaner.GetOutput())
        splineFilter.SetSubdivideToLength()
        splineFilter.SetLength(self.Length)
        splineFilter.Update()

        self.Surface = splineFilter.GetOutput()

        if self.Surface.GetSource():
            self.Surface.GetSource().UnRegisterAllOutputs()
示例#12
0
    def Execute(self):

        if self.Surface == None:
            self.PrintError('Error: No input surface.')

        cleaner = vtk.vtkCleanPolyData()
        cleaner.SetInput(self.Surface)
        cleaner.Update()

        if self.Length == 0.0:
            self.Length = cleaner.GetOutput().GetLength() / 100.0

        splineFilter = vtk.vtkSplineFilter()
        splineFilter.SetInput(cleaner.GetOutput())
        splineFilter.SetSubdivideToLength()
        splineFilter.SetLength(self.Length)
        splineFilter.Update()

        self.Surface = splineFilter.GetOutput()

        if self.Surface.GetSource():
            self.Surface.GetSource().UnRegisterAllOutputs()
psMapper = vtk.vtkPolyDataMapper()
psMapper.SetInputConnection(ps.GetOutputPort())
psActor = vtk.vtkActor()
psActor.SetMapper(psMapper)
psActor.GetProperty().SetRepresentationToWireframe()
rk4 = vtk.vtkRungeKutta4()
streamer = vtk.vtkStreamTracer()
streamer.SetInputData(output)
streamer.SetSourceData(ps.GetOutput())
streamer.SetMaximumPropagation(100)
streamer.SetInitialIntegrationStep(.2)
#streamer.SetStepLength(.001)
streamer.SetIntegrationDirectionToForward()
streamer.SetComputeVorticity(1)
streamer.SetIntegrator(rk4)
sf = vtk.vtkSplineFilter()
sf.SetInputConnection(streamer.GetOutputPort())
sf.SetSubdivideToLength()
sf.SetLength(0.15)
rf = vtk.vtkRibbonFilter()
rf.SetInputConnection(sf.GetOutputPort())
rf.SetInputArrayToProcess(1, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS, "Normals")
rf.SetWidth(0.1)
rf.SetWidthFactor(5)
streamMapper = vtk.vtkPolyDataMapper()
streamMapper.SetInputConnection(rf.GetOutputPort())
streamMapper.SetScalarRange(output.GetScalarRange())
streamline = vtk.vtkActor()
streamline.SetMapper(streamMapper)
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(output)
示例#14
0
psMapper = vtk.vtkPolyDataMapper()
psMapper.SetInputConnection(ps.GetOutputPort())
psActor = vtk.vtkActor()
psActor.SetMapper(psMapper)
psActor.GetProperty().SetRepresentationToWireframe()
rk4 = vtk.vtkRungeKutta4()
streamer = vtk.vtkStreamTracer()
streamer.SetInputData(output)
streamer.SetSourceData(ps.GetOutput())
streamer.SetMaximumPropagation(100)
streamer.SetInitialIntegrationStep(.2)
#streamer.SetStepLength(.001)
streamer.SetIntegrationDirectionToForward()
streamer.SetComputeVorticity(1)
streamer.SetIntegrator(rk4)
sf = vtk.vtkSplineFilter()
sf.SetInputConnection(streamer.GetOutputPort())
sf.SetSubdivideToLength()
sf.SetLength(0.15)
rf = vtk.vtkRibbonFilter()
rf.SetInputConnection(sf.GetOutputPort())
rf.SetInputArrayToProcess(1, 0, 0, vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS,
                          "Normals")
rf.SetWidth(0.1)
rf.SetWidthFactor(5)
streamMapper = vtk.vtkPolyDataMapper()
streamMapper.SetInputConnection(rf.GetOutputPort())
streamMapper.SetScalarRange(output.GetScalarRange())
streamline = vtk.vtkActor()
streamline.SetMapper(streamMapper)
outline = vtk.vtkStructuredGridOutlineFilter()
示例#15
0
 def __showBranch(self, branch_arg, mode=3):
     assert self.__ren != None, "vtkRenderer is not Ready!"
     line_data = branch_arg.getLine()
     if mode == 1:
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_sphere = vtk.vtkSphereSource()
             tmp_sphere.SetCenter(tmp_point[2], tmp_point[3], tmp_point[4])
             tmp_sphere.SetRadius(1.0)
             tmp_mapper = vtk.vtkPolyDataMapper()
             tmp_mapper.SetInput(tmp_sphere.GetOutput())
             tmp_actor = vtk.vtkActor()
             tmp_actor.SetMapper(tmp_mapper)
             self.__ren.AddActor(tmp_actor)
     elif mode == 2:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint(
                 (tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper = vtk.vtkPolyDataMapper()
         tmp_mapper.SetInput(tmp_line)
         tmp_actor = vtk.vtkActor()
         tmp_property = vtk.vtkProperty()
         tmp_property.SetLineWidth(2.0)
         tmp_property.SetColor(0.0, 1.0, 0.0)
         tmp_actor.SetMapper(tmp_mapper)
         tmp_actor.SetProperty(tmp_property)
         self.__ren.AddActor(tmp_actor)
     elif mode == 3:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint(
                 (tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper1 = vtk.vtkPolyDataMapper()
         tmp_mapper1.SetInput(tmp_line)
         tmp_actor1 = vtk.vtkActor()
         tmp_property1 = vtk.vtkProperty()
         tmp_property1.SetLineWidth(3.0)
         tmp_property1.SetColor(1.0, 0.0, 0.0)
         tmp_actor1.SetMapper(tmp_mapper1)
         tmp_vertices = vtk.vtkPolyData()
         tmp_vertices.SetPoints(tmp_points)
         tmp_vertices.SetVerts(tmp_line_cell)
         tmp_mapper2 = vtk.vtkPolyDataMapper()
         tmp_mapper2.SetInput(tmp_vertices)
         tmp_actor2 = vtk.vtkActor()
         tmp_property2 = vtk.vtkProperty()
         tmp_property2.SetPointSize(1.0)
         tmp_property2.SetColor(0.0, 1.0, 0.0)
         tmp_actor2.SetMapper(tmp_mapper2)
         tmp_actor1.SetProperty(tmp_property1)
         tmp_actor2.SetProperty(tmp_property2)
         self.__ren.AddActor(tmp_actor1)
         self.__ren.AddActor(tmp_actor2)
     elif mode == 4:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint(
                 (tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_spline_filter = vtk.vtkSplineFilter()
         tmp_spline_filter.SetInput(tmp_line)
         tmp_spline_filter.SetNumberOfSubdivisions(5 * len(line_data))
         tmp_spline_filter.Update()
         tmp_tube_filter = vtk.vtkTubeFilter()
         tmp_tube_filter.SetInputConnection(
             tmp_spline_filter.GetOutputPort())
         tmp_tube_filter.SetRadius(1.0)
         tmp_tube_filter.SetNumberOfSides(20)
         tmp_tube_filter.CappingOn()
         tmp_mapper = vtk.vtkPolyDataMapper()
         tmp_mapper.SetInputConnection(tmp_tube_filter.GetOutputPort())
         tmp_actor = vtk.vtkActor()
         tmp_actor.GetProperty().SetColor(1.0, 0.0, 0.0)
         tmp_actor.SetMapper(tmp_mapper)
         self.__ren.AddActor(tmp_actor)
     elif mode == 5:
         tmp_points1 = vtk.vtkPoints()
         tmp_vertices_cell = vtk.vtkCellArray()
         for i in xrange(2):
             if i == 0:
                 tmp_point = line_data[0]
             else:
                 tmp_point = line_data[len(line_data) - 1]
             tmp_points1.InsertNextPoint(
                 (tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_vertices_cell.InsertNextCell(2)
         for i in xrange(2):
             tmp_vertices_cell.InsertCellPoint(i)
         tmp_points2 = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points2.InsertNextPoint(
                 (tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points2)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper1 = vtk.vtkPolyDataMapper()
         tmp_mapper1.SetInput(tmp_line)
         tmp_actor1 = vtk.vtkActor()
         tmp_property1 = vtk.vtkProperty()
         tmp_property1.SetLineWidth(3.0)
         tmp_property1.SetColor(1.0, 0.0, 0.0)
         tmp_actor1.SetMapper(tmp_mapper1)
         tmp_vertices = vtk.vtkPolyData()
         tmp_vertices.SetPoints(tmp_points1)
         tmp_vertices.SetVerts(tmp_vertices_cell)
         tmp_mapper2 = vtk.vtkPolyDataMapper()
         tmp_mapper2.SetInput(tmp_vertices)
         tmp_actor2 = vtk.vtkActor()
         tmp_property2 = vtk.vtkProperty()
         tmp_property2.SetPointSize(5.0)
         tmp_property2.SetColor(0.0, 1.0, 0.0)
         tmp_actor2.SetMapper(tmp_mapper2)
         tmp_actor1.SetProperty(tmp_property1)
         tmp_actor2.SetProperty(tmp_property2)
         self.__ren.AddActor(tmp_actor1)
         self.__ren.AddActor(tmp_actor2)
示例#16
0
value = lambda i: math.fabs(math.sin(math.pi*i/30.))
vtk_float_array = vtk.vtkFloatArray()
vtk_float_array.SetNumberOfValues(npts)
for i in range(npts):
    vtk_float_array.SetValue(i, value(i))
"""
vtk.vtkPolyData(): The point connectivity and scalar values can be
encapsulated in the vtkPolyData object this is done by using the,
vtk_poly_data.SetPoints(vtk_points), vtk_poly_data.SetLines(vtk_cell_array) and
vtk_poly_data.GetPointData().SetScalars(vtkFLoatArray) methods
"""
vtk_poly_data = vtk.vtkPolyData()
vtk_poly_data.SetPoints(vtk_points)
vtk_poly_data.SetLines(vtk_cell_array)
vtk_poly_data.GetPointData().SetScalars(vtk_float_array)
"""
vtk.vtkSplineFilter(): The data can be smoothly interpolated across a
number number of subdivisions using the vtkSplineFilter object. This
is accomplished by first setting the input of the spline filter to be
the previously constructed vtkPolyData object using the
vtk_spline_filter.SetInput(vtk_poly_data) method, and then setting the
number of desired subdivisions to create a smooth curve with the
vtk_spline_filter.SetNumberOfSubdivisions(int), and finally call the
vtk_spline_filter.Update() method.
"""

vtk_spline_filter = vtk.vtkSplineFilter()
if vtk_major_version == 6:
    vtk_spline_filter.SetInputData(vtk_poly_data)
elif vtk_major_version == 7:
    vtk_spline_filter.SetInputData(vtk_poly_data)
示例#17
0
    def Execute(self):
 
        if not self.Network:
            self.Network = vtk.vtkPolyData()
            networkPoints = vtk.vtkPoints()
            networkLines = vtk.vtkCellArray()
            radiusArray = vtk.vtkDoubleArray()
            radiusArray.SetName(self.RadiusArrayName)
            self.Network.SetPoints(networkPoints)
            self.Network.SetLines(networkLines)
            self.Network.GetPointData().AddArray(radiusArray)
        
        if not self.vmtkRenderer:
            self.vmtkRenderer = vmtkrenderer.vmtkRenderer()
            self.vmtkRenderer.Initialize()
            self.OwnRenderer = 1

        self.vmtkRenderer.ExitAfterTextInputMode = False
        self.vmtkRenderer.RegisterScript(self) 

        if self.Image and (not self.PlaneWidgetX or not self.PlaneWidgetY or not self.PlaneWidgetZ):
            imageViewer = vmtkimageviewer.vmtkImageViewer()
            imageViewer.Image = self.Image
            imageViewer.vmtkRenderer = self.vmtkRenderer
            imageViewer.Display = 0
            imageViewer.Execute()
            self.PlaneWidgetX = imageViewer.PlaneWidgetX
            self.PlaneWidgetY = imageViewer.PlaneWidgetY
            self.PlaneWidgetZ = imageViewer.PlaneWidgetZ

        if self.Image:
            spacing = self.Image.GetSpacing()
            self.CurrentRadius = min(spacing)

        if self.UseActiveTubes and not self.FeatureImage:
            imageFeatures = vmtkimagefeatures.vmtkImageFeatures()
            imageFeatures.Image = self.Image
            imageFeatures.FeatureImageType = 'vtkgradient'
            imageFeatures.Execute()
            self.FeatureImage = imageFeatures.FeatureImage
 
        self.NetworkRadiusArray = self.Network.GetPointData().GetArray(self.RadiusArrayName)

        self.Network.GetPointData().SetActiveScalars(self.RadiusArrayName)

        networkMapper = vtk.vtkPolyDataMapper()
        networkMapper.SetInputData(self.Network)
        networkMapper.SetScalarModeToUseCellData()

        self.NetworkActor = vtk.vtkActor()
        self.NetworkActor.SetMapper(networkMapper)
        self.vmtkRenderer.Renderer.AddActor(self.NetworkActor)

        self.NetworkTube = vtk.vtkTubeFilter()
        self.NetworkTube.SetInputData(self.Network)
        self.NetworkTube.SetVaryRadiusToVaryRadiusByAbsoluteScalar()
        self.NetworkTube.SetNumberOfSides(20)
        networkTubeMapper = vtk.vtkPolyDataMapper()
        networkTubeMapper.SetInputConnection(self.NetworkTube.GetOutputPort())
        networkTubeMapper.ScalarVisibilityOff()
        networkTubeActor = vtk.vtkActor()
        networkTubeActor.SetMapper(networkTubeMapper)
        networkTubeActor.PickableOff()
        networkTubeActor.GetProperty().SetOpacity(0.2)
        self.vmtkRenderer.Renderer.AddActor(networkTubeActor)

        self.Selection = vtk.vtkPolyData()
        self.SelectionPoints = vtk.vtkPoints()
        self.SelectionRadiusArray = vtk.vtkDoubleArray()
        self.SelectionRadiusArray.SetName(self.RadiusArrayName)
        self.Selection.SetPoints(self.SelectionPoints)
        self.Selection.GetPointData().AddArray(self.SelectionRadiusArray)
        self.Selection.GetPointData().SetActiveScalars(self.RadiusArrayName)

        glyphs = vtk.vtkGlyph3D()
        glyphSource = vtk.vtkSphereSource()
        glyphSource.SetRadius(1.0)
        glyphSource.SetThetaResolution(20)
        glyphSource.SetPhiResolution(20)
        glyphs.SetInputData(self.Selection)
        glyphs.SetSourceConnection(glyphSource.GetOutputPort())
        glyphs.SetScaleModeToScaleByScalar()
        glyphs.SetScaleFactor(1.0)

        selectionMapper = vtk.vtkPolyDataMapper()
        selectionMapper.SetInputConnection(glyphs.GetOutputPort())

        self.SelectionActor = vtk.vtkActor()
        self.SelectionActor.SetMapper(selectionMapper)
        self.SelectionActor.GetProperty().SetColor(1.0,0.0,0.0)
        self.SelectionActor.GetProperty().SetOpacity(0.5)
        self.SelectionActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.SelectionActor)

        self.ActiveSegmentSeeds = vtk.vtkPolyData()
        self.ActiveSegmentSeedsPoints = vtk.vtkPoints()
        self.ActiveSegmentSeedsRadiusArray = vtk.vtkDoubleArray()
        self.ActiveSegmentSeedsRadiusArray.SetName(self.RadiusArrayName)
        self.ActiveSegmentSeeds.SetPoints(self.ActiveSegmentSeedsPoints)
        self.ActiveSegmentSeeds.GetPointData().AddArray(self.ActiveSegmentSeedsRadiusArray)
        self.ActiveSegmentSeeds.GetPointData().SetActiveScalars(self.RadiusArrayName)

        activeSegmentSeedsGlyphs = vtk.vtkGlyph3D()
        activeSegmentSeedsGlyphSource = vtk.vtkSphereSource()
        activeSegmentSeedsGlyphSource.SetRadius(1.0)
        activeSegmentSeedsGlyphSource.SetThetaResolution(20)
        activeSegmentSeedsGlyphSource.SetPhiResolution(20)
        activeSegmentSeedsGlyphs.SetInputData(self.ActiveSegmentSeeds)
        activeSegmentSeedsGlyphs.SetSourceConnection(activeSegmentSeedsGlyphSource.GetOutputPort())
        activeSegmentSeedsGlyphs.SetScaleModeToScaleByScalar()
        activeSegmentSeedsGlyphs.SetScaleFactor(1.0)

        activeSegmentSeedsMapper = vtk.vtkPolyDataMapper()
        activeSegmentSeedsMapper.SetInputConnection(activeSegmentSeedsGlyphs.GetOutputPort())
        activeSegmentSeedsMapper.ScalarVisibilityOff()

        self.ActiveSegmentSeedsActor = vtk.vtkActor()
        self.ActiveSegmentSeedsActor.SetMapper(activeSegmentSeedsMapper)
        self.ActiveSegmentSeedsActor.GetProperty().SetColor(1.0,0.0,0.0)
        self.ActiveSegmentSeedsActor.GetProperty().SetOpacity(0.5)
        self.ActiveSegmentSeedsActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.ActiveSegmentSeedsActor)

        self.ActiveSegment = vtk.vtkPolyData()
        self.ActiveSegmentPoints = vtk.vtkPoints()
        self.ActiveSegmentCellArray = vtk.vtkCellArray()
        self.ActiveSegmentRadiusArray = vtk.vtkDoubleArray()
        self.ActiveSegmentRadiusArray.SetName(self.RadiusArrayName)
        self.ActiveSegment.SetPoints(self.ActiveSegmentPoints)
        self.ActiveSegment.SetLines(self.ActiveSegmentCellArray)
        self.ActiveSegment.GetPointData().AddArray(self.ActiveSegmentRadiusArray)
        self.ActiveSegment.GetPointData().SetActiveScalars(self.RadiusArrayName)

        activeSegmentMapper = vtk.vtkPolyDataMapper()
        activeSegmentMapper.ScalarVisibilityOff()
        if self.SplineInterpolation and self.Image != None:
            splineFilter = vtk.vtkSplineFilter()
            splineFilter.SetInputData(self.ActiveSegment)
            splineFilter.SetSubdivideToLength()
            splineFilter.SetLength(2.0*min(self.Image.GetSpacing()))
            activeSegmentMapper.SetInputConnection(splineFilter.GetOutputPort())
        else:
            activeSegmentMapper.SetInputData(self.ActiveSegment)

        self.ActiveSegmentActor = vtk.vtkActor()
        self.ActiveSegmentActor.SetMapper(activeSegmentMapper)
        self.ActiveSegmentActor.GetProperty().SetColor(1.0,1.0,1.0)
        self.ActiveSegmentActor.GetProperty().SetLineWidth(3.0)
        self.ActiveSegmentActor.PickableOff()
        self.vmtkRenderer.Renderer.AddActor(self.ActiveSegmentActor)

        activeTube = vtk.vtkTubeFilter()
        activeTube.SetInputConnection(activeSegmentMapper.GetInputPort())
        activeTube.SetVaryRadiusToVaryRadiusByAbsoluteScalar()
        activeTube.SetNumberOfSides(20)
        activeTubeMapper = vtk.vtkPolyDataMapper()
        activeTubeMapper.SetInputConnection(activeTube.GetOutputPort())
        activeTubeMapper.ScalarVisibilityOff()
        activeTubeActor = vtk.vtkActor()
        activeTubeActor.SetMapper(activeTubeMapper)
        activeTubeActor.PickableOff()
        activeTubeActor.GetProperty().SetOpacity(0.6)
        self.vmtkRenderer.Renderer.AddActor(activeTubeActor)

        self.NetworkLabelsArray = vtk.vtkStringArray.SafeDownCast(self.Network.GetCellData().GetAbstractArray(self.LabelsArrayName))
        if not self.NetworkLabelsArray:
            self.NetworkLabelsArray = vtk.vtkStringArray()
            self.NetworkLabelsArray.SetName(self.LabelsArrayName)
            self.NetworkLabelsArray.SetNumberOfValues(self.Network.GetNumberOfCells())
            for i in range(self.Network.GetNumberOfCells()):
                self.NetworkLabelsArray.SetValue(i,'')
            self.Network.GetCellData().AddArray(self.NetworkLabelsArray)
        self.CellCenters = vtk.vtkCellCenters()
        self.CellCenters.SetInputData(self.Network)
        self.CellCenters.VertexCellsOff()
        self.CellCenters.Update()

        labeledMapper = vtk.vtkLabeledDataMapper()
        labeledMapper.SetInputConnection(self.CellCenters.GetOutputPort())
        labeledMapper.SetLabelModeToLabelFieldData()
        labeledMapper.SetFieldDataName(self.LabelsArrayName)
        labeledMapper.GetLabelTextProperty().SetFontFamilyToArial()
        labeledMapper.GetLabelTextProperty().BoldOff()
        labeledMapper.GetLabelTextProperty().ItalicOff()
        labeledMapper.GetLabelTextProperty().ShadowOff()
        
        self.LabelsActor = vtk.vtkActor2D()
        self.LabelsActor.SetMapper(labeledMapper)
        self.LabelsActor.VisibilityOff()
        self.vmtkRenderer.Renderer.AddActor(self.LabelsActor)

        self.CellPicker = vtk.vtkCellPicker()
        self.CellPicker.SetTolerance(1E-2)
        self.CellPicker.InitializePickList()
        self.CellPicker.AddPickList(self.NetworkActor)
        self.CellPicker.PickFromListOn()

        self.vmtkRenderer.AddKeyBinding('a','Add mode.',self.AddCallback)
        self.vmtkRenderer.AddKeyBinding('d','Delete mode.',self.DeleteCallback)
        self.vmtkRenderer.AddKeyBinding('m','Merge mode.',self.MergeCallback)
        self.vmtkRenderer.AddKeyBinding('s','Split mode.',self.SplitCallback)
        self.vmtkRenderer.AddKeyBinding('l','Label mode.',self.LabelCallback)
        self.vmtkRenderer.AddKeyBinding('Tab','Show labels.',self.ShowLabelCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("KeyReleaseEvent", self.KeyReleaseCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("LeftButtonPressEvent", self.LeftButtonPressCallback)
        self.vmtkRenderer.RenderWindowInteractor.AddObserver("MouseMoveEvent", self.MouseMoveCallback)

        if self.PlaneWidgetX:
            self.PlaneWidgetX.UseContinuousCursorOn()
            self.PlaneWidgetX.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)
        if self.PlaneWidgetY:
            self.PlaneWidgetY.UseContinuousCursorOn()
            self.PlaneWidgetY.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)
        if self.PlaneWidgetZ:
            self.PlaneWidgetZ.UseContinuousCursorOn()
            self.PlaneWidgetZ.AddObserver("StartInteractionEvent", self.PlaneStartInteractionCallback)

        self.FirstRender()

        self.Surface = self.NetworkTube.GetOutput()

        if self.OwnRenderer:
            self.vmtkRenderer.Deallocate()
示例#18
0
vs = vtk.vtkStripper()
vs.SetInputData(con.GetOutput())
vs.JoinContiguousSegmentsOn()
vs.Update()

cpd2 = vtk.vtkCleanPolyData()
cpd2.SetInputData(vs.GetOutput())
cpd2.Update()

spline = vtk.vtkCardinalSpline()
spline.SetLeftConstraint(2)
spline.SetLeftValue(0)
spline.SetRightConstraint(2)
spline.SetRightValue(0)

spFilter = vtk.vtkSplineFilter()
spFilter.SetInputData(cpd2.GetOutput())
conNumPoint = cpd2.GetOutput().GetNumberOfPoints()
spFilter.SetNumberOfSubdivisions(conNumPoint * 10)
spFilter.SetSpline(spline)
spFilter.Update()

bc = cpd2.GetOutput().GetBounds()
yl = bc[3] - bc[2]
zl = bc[5] - bc[4]
yzl = math.sqrt(yl**2 + zl**2)
def_tol = yzl / (100**2)

print 'bound diag = ', yzl
print 'max p dist = ', def_tol
示例#19
0
def main():
    inputFileName, isoValue = get_program_parameters()

    reader = vtk.vtkPNGReader()
    if not reader.CanReadFile(inputFileName):
        print("Error: Could not read %s ." % (inputFileName))
    reader.SetFileName(inputFileName)
    reader.Update()

    iso = vtk.vtkMarchingSquares()
    iso.SetInputConnection(reader.GetOutputPort())
    iso.SetValue(0, isoValue)

    # Test smoothing here
    if 1:
        # Remove duplicate points and join up lines to form polylines
        pCleaner = vtk.vtkCleanPolyData()
        pCleaner.SetInputConnection(iso.GetOutputPort())
        pStripper = vtk.vtkStripper()
        pStripper.SetInputConnection(pCleaner.GetOutputPort())

        # Downsample and smooth the polyline
        pSpline = vtk.vtkSplineFilter()
        mSplineResamplingLength = 50.0
        mReferenceLength = 40.0
        pSpline.SetLength(mSplineResamplingLength / mReferenceLength)
        pSpline.SetSubdivideToLength()
        pSpline.SetInputConnection(pStripper.GetOutputPort())

        pTriangle = vtk.vtkTriangleFilter()
        pTriangle.SetInputConnection(pSpline.GetOutputPort())
        pTriangle.Update()
        pCleaned = pTriangle.GetOutput()
        newMapper = vtk.vtkDataSetMapper()
        newMapper.SetInputConnection(pTriangle.GetOutputPort())
        newActor = vtk.vtkActor()
        newActor.SetMapper(newMapper)

    isoMapper = vtk.vtkDataSetMapper()
    isoMapper.SetInputConnection(iso.GetOutputPort())
    isoMapper.ScalarVisibilityOff()

    isoActor = vtk.vtkActor()
    isoActor.SetMapper(isoMapper)
    isoActor.GetProperty().SetColor(0.8900, 0.8100, 0.3400)

    poly = vtk.vtkContourTriangulator()
    poly.SetInputConnection(iso.GetOutputPort())

    polyMapper = vtk.vtkDataSetMapper()
    polyMapper.SetInputConnection(poly.GetOutputPort())
    polyMapper.ScalarVisibilityOff()

    polyActor = vtk.vtkActor()
    polyActor.SetMapper(polyMapper)
    polyActor.GetProperty().SetColor(1.0000, 0.3882, 0.2784)

    # Standard rendering classes
    renderer = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.SetMultiSamples(0)
    renWin.AddRenderer(renderer)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    renderer.AddActor(polyActor)  # Important
    renderer.AddActor(isoActor)
    renderer.AddActor(newActor)

    # Standard testing code.
    renderer.SetBackground(0.5, 0.5, 0.5)
    renWin.SetSize(800, 800)

    camera = renderer.GetActiveCamera()
    renderer.ResetCamera()
    camera.Azimuth(180)

    renWin.Render()
    iren.Initialize()
    iren.Start()
示例#20
0
 def __showBranch(self, branch_arg, mode=3):
     assert self.__ren != None, "vtkRenderer is not Ready!"
     line_data = branch_arg.getLine()
     if mode == 1:
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_sphere = vtk.vtkSphereSource()
             tmp_sphere.SetCenter(tmp_point[2], tmp_point[3], tmp_point[4])
             tmp_sphere.SetRadius(1.0)
             tmp_mapper = vtk.vtkPolyDataMapper()
             tmp_mapper.SetInput(tmp_sphere.GetOutput())
             tmp_actor = vtk.vtkActor()
             tmp_actor.SetMapper(tmp_mapper)
             self.__ren.AddActor(tmp_actor)
     elif mode == 2:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint((tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper = vtk.vtkPolyDataMapper()
         tmp_mapper.SetInput(tmp_line)
         tmp_actor = vtk.vtkActor()
         tmp_property = vtk.vtkProperty()
         tmp_property.SetLineWidth(2.0)
         tmp_property.SetColor(0.0, 1.0, 0.0)
         tmp_actor.SetMapper(tmp_mapper)
         tmp_actor.SetProperty(tmp_property)
         self.__ren.AddActor(tmp_actor)
     elif mode == 3:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint((tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper1 = vtk.vtkPolyDataMapper()
         tmp_mapper1.SetInput(tmp_line)
         tmp_actor1 = vtk.vtkActor()
         tmp_property1 = vtk.vtkProperty()
         tmp_property1.SetLineWidth(3.0)
         tmp_property1.SetColor(1.0, 0.0, 0.0)
         tmp_actor1.SetMapper(tmp_mapper1)
         tmp_vertices = vtk.vtkPolyData()
         tmp_vertices.SetPoints(tmp_points)
         tmp_vertices.SetVerts(tmp_line_cell)
         tmp_mapper2 = vtk.vtkPolyDataMapper()
         tmp_mapper2.SetInput(tmp_vertices)
         tmp_actor2 = vtk.vtkActor()
         tmp_property2 = vtk.vtkProperty()
         tmp_property2.SetPointSize(1.0)
         tmp_property2.SetColor(0.0, 1.0, 0.0)
         tmp_actor2.SetMapper(tmp_mapper2)
         tmp_actor1.SetProperty(tmp_property1)
         tmp_actor2.SetProperty(tmp_property2)          
         self.__ren.AddActor(tmp_actor1)  
         self.__ren.AddActor(tmp_actor2)
     elif mode == 4:
         tmp_points = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points.InsertNextPoint((tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points)
         tmp_line.SetLines(tmp_line_cell)
         tmp_spline_filter = vtk.vtkSplineFilter()
         tmp_spline_filter.SetInput(tmp_line)
         tmp_spline_filter.SetNumberOfSubdivisions(5*len(line_data))
         tmp_spline_filter.Update()                         
         tmp_tube_filter = vtk.vtkTubeFilter()
         tmp_tube_filter.SetInputConnection(tmp_spline_filter.GetOutputPort())
         tmp_tube_filter.SetRadius(1.0)
         tmp_tube_filter.SetNumberOfSides(20)
         tmp_tube_filter.CappingOn()
         tmp_mapper = vtk.vtkPolyDataMapper()
         tmp_mapper.SetInputConnection(tmp_tube_filter.GetOutputPort())
         tmp_actor = vtk.vtkActor()
         tmp_actor.GetProperty().SetColor(1.0, 0.0, 0.0)
         tmp_actor.SetMapper(tmp_mapper)
         self.__ren.AddActor(tmp_actor)
     elif mode == 5:
         tmp_points1 = vtk.vtkPoints()
         tmp_vertices_cell = vtk.vtkCellArray()
         for i in xrange(2):
             if i == 0:
                 tmp_point = line_data[0]
             else:
                 tmp_point = line_data[len(line_data) - 1]
             tmp_points1.InsertNextPoint((tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_vertices_cell.InsertNextCell(2)
         for i in xrange(2):
             tmp_vertices_cell.InsertCellPoint(i)
         tmp_points2 = vtk.vtkPoints()
         tmp_line_cell = vtk.vtkCellArray()
         for i in xrange(len(line_data)):
             tmp_point = line_data[i]
             tmp_points2.InsertNextPoint((tmp_point[2], tmp_point[3], tmp_point[4]))
         tmp_line_cell.InsertNextCell(len(line_data))
         for i in xrange(len(line_data)):
             tmp_line_cell.InsertCellPoint(i)                 
         tmp_line = vtk.vtkPolyData()
         tmp_line.SetPoints(tmp_points2)
         tmp_line.SetLines(tmp_line_cell)
         tmp_mapper1 = vtk.vtkPolyDataMapper()
         tmp_mapper1.SetInput(tmp_line)
         tmp_actor1 = vtk.vtkActor()
         tmp_property1 = vtk.vtkProperty()
         tmp_property1.SetLineWidth(3.0)
         tmp_property1.SetColor(1.0, 0.0, 0.0)
         tmp_actor1.SetMapper(tmp_mapper1)
         tmp_vertices = vtk.vtkPolyData()
         tmp_vertices.SetPoints(tmp_points1)
         tmp_vertices.SetVerts(tmp_vertices_cell)
         tmp_mapper2 = vtk.vtkPolyDataMapper()
         tmp_mapper2.SetInput(tmp_vertices)
         tmp_actor2 = vtk.vtkActor()
         tmp_property2 = vtk.vtkProperty()
         tmp_property2.SetPointSize(5.0)
         tmp_property2.SetColor(0.0, 1.0, 0.0)
         tmp_actor2.SetMapper(tmp_mapper2)
         tmp_actor1.SetProperty(tmp_property1)
         tmp_actor2.SetProperty(tmp_property2)          
         self.__ren.AddActor(tmp_actor1)  
         self.__ren.AddActor(tmp_actor2)
示例#21
0
文件: actor.py 项目: sitek/fury
def streamtube(lines,
               colors=None,
               opacity=1,
               linewidth=0.1,
               tube_sides=9,
               lod=True,
               lod_points=10**4,
               lod_points_size=3,
               spline_subdiv=None,
               lookup_colormap=None):
    """Use streamtubes to visualize polylines

    Parameters
    ----------
    lines : list
        list of N curves represented as 2D ndarrays

    colors : array (N, 3), list of arrays, tuple (3,), array (K,), None
        If None then a standard orientation colormap is used for every line.
        If one tuple of color is used. Then all streamlines will have the same
        colour.
        If an array (N, 3) is given, where N is equal to the number of lines.
        Then every line is coloured with a different RGB color.
        If a list of RGB arrays is given then every point of every line takes
        a different color.
        If an array (K, ) is given, where K is the number of points of all
        lines then these are considered as the values to be used by the
        colormap.
        If an array (L, ) is given, where L is the number of streamlines then
        these are considered as the values to be used by the colormap per
        streamline.
        If an array (X, Y, Z) or (X, Y, Z, 3) is given then the values for the
        colormap are interpolated automatically using trilinear interpolation.

    opacity : float
        Takes values from 0 (fully transparent) to 1 (opaque). Default is 1.
    linewidth : float
        Default is 0.01.
    tube_sides : int
        Default is 9.
    lod : bool
        Use vtkLODActor(level of detail) rather than vtkActor. Default is True.
        Level of detail actors do not render the full geometry when the
        frame rate is low.
    lod_points : int
        Number of points to be used when LOD is in effect. Default is 10000.
    lod_points_size : int
        Size of points when lod is in effect. Default is 3.
    spline_subdiv : int
        Number of splines subdivision to smooth streamtubes. Default is None.
    lookup_colormap : vtkLookupTable
        Add a default lookup table to the colormap. Default is None which calls
        :func:`fury.actor.colormap_lookup_table`.

    Examples
    --------
    >>> import numpy as np
    >>> from fury import actor, window
    >>> scene = window.Scene()
    >>> lines = [np.random.rand(10, 3), np.random.rand(20, 3)]
    >>> colors = np.random.rand(2, 3)
    >>> c = actor.streamtube(lines, colors)
    >>> scene.add(c)
    >>> #window.show(scene)

    Notes
    -----
    Streamtubes can be heavy on GPU when loading many streamlines and
    therefore, you may experience slow rendering time depending on system GPU.
    A solution to this problem is to reduce the number of points in each
    streamline. In Dipy we provide an algorithm that will reduce the number of
    points on the straighter parts of the streamline but keep more points on
    the curvier parts. This can be used in the following way::

        from dipy.tracking.distances import approx_polygon_track
        lines = [approx_polygon_track(line, 0.2) for line in lines]

    Alternatively we suggest using the ``line`` actor which is much more
    efficient.

    See Also
    --------
    :func:`fury.actor.line`

    """
    # Poly data with lines and colors
    poly_data, is_colormap = lines_to_vtk_polydata(lines, colors)
    next_input = poly_data

    # Set Normals
    poly_normals = set_input(vtk.vtkPolyDataNormals(), next_input)
    poly_normals.ComputeCellNormalsOn()
    poly_normals.ComputePointNormalsOn()
    poly_normals.ConsistencyOn()
    poly_normals.AutoOrientNormalsOn()
    poly_normals.Update()
    next_input = poly_normals.GetOutputPort()

    # Spline interpolation
    if (spline_subdiv is not None) and (spline_subdiv > 0):
        spline_filter = set_input(vtk.vtkSplineFilter(), next_input)
        spline_filter.SetSubdivideToSpecified()
        spline_filter.SetNumberOfSubdivisions(spline_subdiv)
        spline_filter.Update()
        next_input = spline_filter.GetOutputPort()

    # Add thickness to the resulting lines
    tube_filter = set_input(vtk.vtkTubeFilter(), next_input)
    tube_filter.SetNumberOfSides(tube_sides)
    tube_filter.SetRadius(linewidth)
    # TODO using the line above we will be able to visualize
    # streamtubes of varying radius
    # tube_filter.SetVaryRadiusToVaryRadiusByScalar()
    tube_filter.CappingOn()
    tube_filter.Update()
    next_input = tube_filter.GetOutputPort()

    # Poly mapper
    poly_mapper = set_input(vtk.vtkPolyDataMapper(), next_input)
    poly_mapper.ScalarVisibilityOn()
    poly_mapper.SetScalarModeToUsePointFieldData()
    poly_mapper.SelectColorArray("Colors")
    poly_mapper.Update()

    # Color Scale with a lookup table
    if is_colormap:
        if lookup_colormap is None:
            lookup_colormap = colormap_lookup_table()
        poly_mapper.SetLookupTable(lookup_colormap)
        poly_mapper.UseLookupTableScalarRangeOn()
        poly_mapper.Update()

    # Set Actor
    if lod:
        actor = vtk.vtkLODActor()
        actor.SetNumberOfCloudPoints(lod_points)
        actor.GetProperty().SetPointSize(lod_points_size)
    else:
        actor = vtk.vtkActor()

    actor.SetMapper(poly_mapper)

    actor.GetProperty().SetInterpolationToPhong()
    actor.GetProperty().BackfaceCullingOn()
    actor.GetProperty().SetOpacity(opacity)

    return actor
示例#22
0
文件: actor.py 项目: sitek/fury
def line(lines,
         colors=None,
         opacity=1,
         linewidth=1,
         spline_subdiv=None,
         lod=True,
         lod_points=10**4,
         lod_points_size=3,
         lookup_colormap=None):
    """ Create an actor for one or more lines.

    Parameters
    ------------
    lines :  list of arrays

    colors : array (N, 3), list of arrays, tuple (3,), array (K,), None
        If None then a standard orientation colormap is used for every line.
        If one tuple of color is used. Then all streamlines will have the same
        colour.
        If an array (N, 3) is given, where N is equal to the number of lines.
        Then every line is coloured with a different RGB color.
        If a list of RGB arrays is given then every point of every line takes
        a different color.
        If an array (K, ) is given, where K is the number of points of all
        lines then these are considered as the values to be used by the
        colormap.
        If an array (L, ) is given, where L is the number of streamlines then
        these are considered as the values to be used by the colormap per
        streamline.
        If an array (X, Y, Z) or (X, Y, Z, 3) is given then the values for the
        colormap are interpolated automatically using trilinear interpolation.

    opacity : float, optional
        Takes values from 0 (fully transparent) to 1 (opaque). Default is 1.

    linewidth : float, optional
        Line thickness. Default is 1.
    spline_subdiv : int, optional
        Number of splines subdivision to smooth streamtubes. Default is None
        which means no subdivision.
    lod : bool
        Use vtkLODActor(level of detail) rather than vtkActor. Default is True.
        Level of detail actors do not render the full geometry when the
        frame rate is low.
    lod_points : int
        Number of points to be used when LOD is in effect. Default is 10000.
    lod_points_size : int
        Size of points when lod is in effect. Default is 3.
    lookup_colormap : bool, optional
        Add a default lookup table to the colormap. Default is None which calls
        :func:`fury.actor.colormap_lookup_table`.

    Returns
    ----------
    v : vtkActor or vtkLODActor object
        Line.

    Examples
    ----------
    >>> from fury import actor, window
    >>> scene = window.Scene()
    >>> lines = [np.random.rand(10, 3), np.random.rand(20, 3)]
    >>> colors = np.random.rand(2, 3)
    >>> c = actor.line(lines, colors)
    >>> scene.add(c)
    >>> #window.show(scene)
    """
    # Poly data with lines and colors
    poly_data, is_colormap = lines_to_vtk_polydata(lines, colors)
    next_input = poly_data

    # use spline interpolation
    if (spline_subdiv is not None) and (spline_subdiv > 0):
        spline_filter = set_input(vtk.vtkSplineFilter(), next_input)
        spline_filter.SetSubdivideToSpecified()
        spline_filter.SetNumberOfSubdivisions(spline_subdiv)
        spline_filter.Update()
        next_input = spline_filter.GetOutputPort()

    poly_mapper = set_input(vtk.vtkPolyDataMapper(), next_input)
    poly_mapper.ScalarVisibilityOn()
    poly_mapper.SetScalarModeToUsePointFieldData()
    poly_mapper.SelectColorArray("Colors")
    poly_mapper.Update()

    # Color Scale with a lookup table
    if is_colormap:

        if lookup_colormap is None:
            lookup_colormap = colormap_lookup_table()

        poly_mapper.SetLookupTable(lookup_colormap)
        poly_mapper.UseLookupTableScalarRangeOn()
        poly_mapper.Update()

    # Set Actor
    if lod:
        actor = vtk.vtkLODActor()
        actor.SetNumberOfCloudPoints(lod_points)
        actor.GetProperty().SetPointSize(lod_points_size)
    else:
        actor = vtk.vtkActor()

    # actor = vtk.vtkActor()
    actor.SetMapper(poly_mapper)
    actor.GetProperty().SetLineWidth(linewidth)
    actor.GetProperty().SetOpacity(opacity)

    return actor
示例#23
0
value = lambda i: math.fabs(math.sin(math.pi * i / 30.))
vtk_float_array = vtk.vtkFloatArray()
vtk_float_array.SetNumberOfValues(npts)
for i in range(npts):
    vtk_float_array.SetValue(i, value(i))
"""
vtk.vtkPolyData(): The point connectivity and scalar values can be
encapsulated in the vtkPolyData object this is done by using the,
vtk_poly_data.SetPoints(vtk_points), vtk_poly_data.SetLines(vtk_cell_array) and
vtk_poly_data.GetPointData().SetScalars(vtkFLoatArray) methods
"""
vtk_poly_data = vtk.vtkPolyData()
vtk_poly_data.SetPoints(vtk_points)
vtk_poly_data.SetLines(vtk_cell_array)
vtk_poly_data.GetPointData().SetScalars(vtk_float_array)
"""
vtk.vtkSplineFilter(): The data can be smoothly interpolated across a
number number of subdivisions using the vtkSplineFilter object. This
is accomplished by first setting the input of the spline filter to be
the previously constructed vtkPolyData object using the
vtk_spline_filter.SetInput(vtk_poly_data) method, and then setting the
number of desired subdivisions to create a smooth curve with the
vtk_spline_filter.SetNumberOfSubdivisions(int), and finally call the
vtk_spline_filter.Update() method.
"""

vtk_spline_filter = vtk.vtkSplineFilter()
if vtk_major_version == 6:
    vtk_spline_filter.SetInputData(vtk_poly_data)
elif vtk_major_version in [7, 8]:
    vtk_spline_filter.SetInputData(vtk_poly_data)