Exemplo n.º 1
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkRuledSurfaceFilter(), 'Processing.',
         ('vtkPolyData',), ('vtkPolyData',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
Exemplo n.º 2
0
def ribbon(line1, line2, c='m', alpha=1, legend=None, res=(200, 5)):
    '''Connect two lines to generate the surface inbetween.'''
    if isinstance(line1, Actor):
        line1 = line1.coordinates()
    if isinstance(line2, Actor):
        line2 = line2.coordinates()

    ppoints1 = vtk.vtkPoints()  # Generate the polyline1
    ppoints1.SetData(numpy_to_vtk(line1, deep=True))
    lines1 = vtk.vtkCellArray()
    lines1.InsertNextCell(len(line1))
    for i in range(len(line1)):
        lines1.InsertCellPoint(i)
    poly1 = vtk.vtkPolyData()
    poly1.SetPoints(ppoints1)
    poly1.SetLines(lines1)

    ppoints2 = vtk.vtkPoints()  # Generate the polyline2
    ppoints2.SetData(numpy_to_vtk(line2, deep=True))
    lines2 = vtk.vtkCellArray()
    lines2.InsertNextCell(len(line2))
    for i in range(len(line2)):
        lines2.InsertCellPoint(i)
    poly2 = vtk.vtkPolyData()
    poly2.SetPoints(ppoints2)
    poly2.SetLines(lines2)

    # build the lines
    lines1 = vtk.vtkCellArray()
    lines1.InsertNextCell(poly1.GetNumberOfPoints())
    for i in range(poly1.GetNumberOfPoints()):
        lines1.InsertCellPoint(i)

    polygon1 = vtk.vtkPolyData()
    polygon1.SetPoints(ppoints1)
    polygon1.SetLines(lines1)

    lines2 = vtk.vtkCellArray()
    lines2.InsertNextCell(poly2.GetNumberOfPoints())
    for i in range(poly2.GetNumberOfPoints()):
        lines2.InsertCellPoint(i)

    polygon2 = vtk.vtkPolyData()
    polygon2.SetPoints(ppoints2)
    polygon2.SetLines(lines2)

    mergedPolyData = vtk.vtkAppendPolyData()
    mergedPolyData.AddInputData(polygon1)
    mergedPolyData.AddInputData(polygon2)
    mergedPolyData.Update()

    rsf = vtk.vtkRuledSurfaceFilter()
    rsf.CloseSurfaceOff()
    rsf.SetRuledModeToResample()
    rsf.SetResolution(res[0], res[1])
    rsf.SetInputData(mergedPolyData.GetOutput())
    rsf.Update()
    return Actor(rsf.GetOutput(), c=c, alpha=alpha, legend=legend)
    def __init__(self, parent=None):
        super(VTKFrame, self).__init__(parent)

        self.vtkWidget = QVTKRenderWindowInteractor(self)
        vl = QtGui.QVBoxLayout(self)
        vl.addWidget(self.vtkWidget)
        vl.setContentsMargins(0, 0, 0, 0)

        self.ren = vtk.vtkRenderer()
        self.ren.SetBackground(0.3, 0.4, 0.5)
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # Create lines.
        points = vtk.vtkPoints()
        points.InsertPoint(0, 0, 0, 1)
        points.InsertPoint(1, 1, 0, 0)
        points.InsertPoint(2, 0, 1, 0)
        points.InsertPoint(3, 1, 1, 1)

        line1 = vtk.vtkLine()
        line1.GetPointIds().SetId(0, 0)
        line1.GetPointIds().SetId(1, 1)

        line2 = vtk.vtkLine()
        line2.GetPointIds().SetId(0, 2)
        line2.GetPointIds().SetId(1, 3)

        lines = vtk.vtkCellArray()
        lines.InsertNextCell(line1)
        lines.InsertNextCell(line2)

        polyData = vtk.vtkPolyData()
        polyData.SetPoints(points)
        polyData.SetLines(lines)

        ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
        ruledSurfaceFilter.SetInputConnection(polyData.GetProducerPort())

        ruledSurfaceFilter.SetResolution(21, 21)
        ruledSurfaceFilter.SetRuledModeToResample()

        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(ruledSurfaceFilter.GetOutputPort())

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(0.89, 0.81, 0.34)

        self.ren.AddActor(actor)
        self.ren.ResetCamera()

        self._initialized = False
    def __init__(self, parent = None):
        super(VTKFrame, self).__init__(parent)

        self.vtkWidget = QVTKRenderWindowInteractor(self)
        vl = QtGui.QVBoxLayout(self)
        vl.addWidget(self.vtkWidget)
        vl.setContentsMargins(0, 0, 0, 0)
 
        self.ren = vtk.vtkRenderer()
        self.ren.SetBackground(0.3, 0.4, 0.5)
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()
 
        # Create lines.
        points = vtk.vtkPoints()
        points.InsertPoint(0, 0, 0, 1)
        points.InsertPoint(1, 1, 0, 0)
        points.InsertPoint(2, 0, 1, 0)
        points.InsertPoint(3, 1, 1, 1)

        line1 = vtk.vtkLine()
        line1.GetPointIds().SetId(0, 0)
        line1.GetPointIds().SetId(1, 1)

        line2 = vtk.vtkLine()
        line2.GetPointIds().SetId(0, 2)
        line2.GetPointIds().SetId(1, 3)

        lines = vtk.vtkCellArray()
        lines.InsertNextCell(line1)
        lines.InsertNextCell(line2)

        polyData = vtk.vtkPolyData()
        polyData.SetPoints(points)
        polyData.SetLines(lines)

        ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
        ruledSurfaceFilter.SetInputConnection(polyData.GetProducerPort())

        ruledSurfaceFilter.SetResolution(21, 21)
        ruledSurfaceFilter.SetRuledModeToResample()
 
        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(ruledSurfaceFilter.GetOutputPort())
 
        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(0.89, 0.81, 0.34)
 
        self.ren.AddActor(actor)
        self.ren.ResetCamera()

        self._initialized = False
    def create_stream_line(self,x,y,z,opa):

        rake = vtk.vtkLineSource()
        rake.SetPoint1(x, -y, z)
        rake.SetPoint2(x, y, z)
        rake.SetResolution(150)

        """
        rake_mapper = vtk.vtkPolyDataMapper()
        rake_mapper.SetInputConnection(rake.GetOutputPort())
        rake_actor = vtk.vtkActor()
        rake_actor.SetMapper(rake_mapper)
        self.ren.AddActor(rake_actor)
        """

        integ = vtk.vtkRungeKutta4()
        streamline = vtk.vtkStreamLine()
        streamline.SetInputConnection(self.vec_reader.GetOutputPort())
        streamline.SetSourceConnection(rake.GetOutputPort())
        streamline.SetIntegrator(integ)
        streamline.SetMaximumPropagationTime(300)
        streamline.SetIntegrationStepLength(0.01)
        streamline.SetIntegrationDirectionToForward()
        streamline.SpeedScalarsOn()
        streamline.SetStepLength(0.05)

        scalarSurface = vtk.vtkRuledSurfaceFilter()
        scalarSurface.SetInputConnection(streamline.GetOutputPort())
        #scalarSurface.SetOffset(0)
        scalarSurface.SetOnRatio(1)
        scalarSurface.PassLinesOn()
        scalarSurface.SetRuledModeToPointWalk()
        scalarSurface.SetDistanceFactor(30)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(scalarSurface.GetOutputPort())
        #mapper.SetScalarRange(self.vec_reader.GetOutput().GetScalarRange())
        mapper.SetLookupTable(self.arrowColor)

        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetOpacity(opa)

        return actor
Exemplo n.º 6
0
seeds = vtk.vtkLineSource()
seeds.SetPoint1(15, -5, 32)
seeds.SetPoint2(15, 5, 32)
seeds.SetResolution(10)

integ = vtk.vtkRungeKutta4()

sl = vtk.vtkStreamTracer()
sl.SetIntegrator(integ)
sl.SetInputData(pl3d_output)
sl.SetSourceConnection(seeds.GetOutputPort())
sl.SetMaximumPropagation(100)
sl.SetInitialIntegrationStep(0.1)
sl.SetIntegrationDirectionToBackward()

scalarSurface = vtk.vtkRuledSurfaceFilter ()
scalarSurface.SetInputConnection(sl.GetOutputPort())
scalarSurface.SetOffset(0)
scalarSurface.SetOnRatio(2)
scalarSurface.PassLinesOn()
scalarSurface.SetRuledModeToPointWalk()
scalarSurface.SetDistanceFactor(30)

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

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

mmapper = vtk.vtkPolyDataMapper()
mmapper.SetInputConnection(seeds.GetOutputPort())
Exemplo n.º 7
0
 def add_faces(self, faces, color, opacity=0.35):
     for face in faces:
         if len(face) == 3:
             points = vtk.vtkPoints()
             triangle = vtk.vtkTriangle()
             for ii in range(3):
                 points.InsertNextPoint(face[ii][0], face[ii][1],
                                        face[ii][2])
                 triangle.GetPointIds().SetId(ii, ii)
             triangles = vtk.vtkCellArray()
             triangles.InsertNextCell(triangle)
             trianglePolyData = vtk.vtkPolyData()
             trianglePolyData.SetPoints(points)
             trianglePolyData.SetPolys(triangles)
             mapper = vtk.vtkPolyDataMapper()
             mapper.SetInput(trianglePolyData)
             ac = vtk.vtkActor()
             ac.SetMapper(mapper)
             ac.GetProperty().SetOpacity(opacity)
             ac.GetProperty().SetColor(color)
             self.ren.AddActor(ac)
         elif False and len(face) == 4:
             points = vtk.vtkPoints()
             for ii in range(4):
                 points.InsertNextPoint(face[ii][0], face[ii][1],
                                        face[ii][2])
             line1 = vtk.vtkLine()
             line1.GetPointIds().SetId(0, 0)
             line1.GetPointIds().SetId(1, 2)
             line2 = vtk.vtkLine()
             line2.GetPointIds().SetId(0, 3)
             line2.GetPointIds().SetId(1, 1)
             lines = vtk.vtkCellArray()
             lines.InsertNextCell(line1)
             lines.InsertNextCell(line2)
             polydata = vtk.vtkPolyData()
             polydata.SetPoints(points)
             polydata.SetLines(lines)
             ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
             ruledSurfaceFilter.SetInput(polydata)
             ruledSurfaceFilter.SetResolution(15, 15)
             ruledSurfaceFilter.SetRuledModeToResample()
             mapper = vtk.vtkPolyDataMapper()
             mapper.SetInput(ruledSurfaceFilter.GetOutput())
             ac = vtk.vtkActor()
             ac.SetMapper(mapper)
             ac.GetProperty().SetOpacity(opacity)
             ac.GetProperty().SetColor(color)
             self.ren.AddActor(ac)
         elif len(face) > 3:
             center = np.zeros(3, np.float)
             for site in face:
                 center += site
             center /= np.float(len(face))
             for ii in range(len(face)):
                 points = vtk.vtkPoints()
                 triangle = vtk.vtkTriangle()
                 points.InsertNextPoint(face[ii][0], face[ii][1],
                                        face[ii][2])
                 ii2 = np.mod(ii + 1, len(face))
                 points.InsertNextPoint(face[ii2][0], face[ii2][1],
                                        face[ii2][2])
                 points.InsertNextPoint(center[0], center[1], center[2])
                 for ii in range(3):
                     triangle.GetPointIds().SetId(ii, ii)
                 triangles = vtk.vtkCellArray()
                 triangles.InsertNextCell(triangle)
                 trianglePolyData = vtk.vtkPolyData()
                 trianglePolyData.SetPoints(points)
                 trianglePolyData.SetPolys(triangles)
                 mapper = vtk.vtkPolyDataMapper()
                 mapper.SetInput(trianglePolyData)
                 ac = vtk.vtkActor()
                 ac.SetMapper(mapper)
                 ac.GetProperty().SetOpacity(opacity)
                 ac.GetProperty().SetColor(color)
                 self.ren.AddActor(ac)
         else:
             raise ValueError("Number of points for a face should be >= 3")
Exemplo n.º 8
0
 def add_faces(self, faces, color, opacity=0.35):
     for face in faces:
         if len(face) == 3:
             points = vtk.vtkPoints()
             triangle = vtk.vtkTriangle()
             for ii in range(3):
                 points.InsertNextPoint(face[ii][0], face[ii][1], face[ii][2])
                 triangle.GetPointIds().SetId(ii, ii)
             triangles = vtk.vtkCellArray()
             triangles.InsertNextCell(triangle)
             trianglePolyData = vtk.vtkPolyData()
             trianglePolyData.SetPoints(points)
             trianglePolyData.SetPolys(triangles)
             mapper = vtk.vtkPolyDataMapper()
             mapper.SetInput(trianglePolyData)
             ac = vtk.vtkActor()
             ac.SetMapper(mapper)
             ac.GetProperty().SetOpacity(opacity)
             ac.GetProperty().SetColor(color)
             self.ren.AddActor(ac)
         elif False and len(face) == 4:
             points = vtk.vtkPoints()
             for ii in range(4):
                 points.InsertNextPoint(face[ii][0], face[ii][1], face[ii][2])
             line1 = vtk.vtkLine()
             line1.GetPointIds().SetId(0, 0)
             line1.GetPointIds().SetId(1, 2)
             line2 = vtk.vtkLine()
             line2.GetPointIds().SetId(0, 3)
             line2.GetPointIds().SetId(1, 1)
             lines = vtk.vtkCellArray()
             lines.InsertNextCell(line1)
             lines.InsertNextCell(line2)
             polydata = vtk.vtkPolyData()
             polydata.SetPoints(points)
             polydata.SetLines(lines)
             ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
             ruledSurfaceFilter.SetInput(polydata)
             ruledSurfaceFilter.SetResolution(15, 15)
             ruledSurfaceFilter.SetRuledModeToResample()
             mapper = vtk.vtkPolyDataMapper()
             mapper.SetInput(ruledSurfaceFilter.GetOutput())
             ac = vtk.vtkActor()
             ac.SetMapper(mapper)
             ac.GetProperty().SetOpacity(opacity)
             ac.GetProperty().SetColor(color)
             self.ren.AddActor(ac)
         elif len(face) > 3:
             center = np.zeros(3, np.float)
             for site in face:
                 center += site
             center /= np.float(len(face))
             for ii in range(len(face)):
                 points = vtk.vtkPoints()
                 triangle = vtk.vtkTriangle()
                 points.InsertNextPoint(face[ii][0], face[ii][1], face[ii][2])
                 ii2 = np.mod(ii+1, len(face))
                 points.InsertNextPoint(face[ii2][0], face[ii2][1], face[ii2][2])
                 points.InsertNextPoint(center[0], center[1], center[2])
                 for ii in range(3):
                     triangle.GetPointIds().SetId(ii, ii)
                 triangles = vtk.vtkCellArray()
                 triangles.InsertNextCell(triangle)
                 trianglePolyData = vtk.vtkPolyData()
                 trianglePolyData.SetPoints(points)
                 trianglePolyData.SetPolys(triangles)
                 mapper = vtk.vtkPolyDataMapper()
                 mapper.SetInput(trianglePolyData)
                 ac = vtk.vtkActor()
                 ac.SetMapper(mapper)
                 ac.GetProperty().SetOpacity(opacity)
                 ac.GetProperty().SetColor(color)
                 self.ren.AddActor(ac)
         else:
             raise ValueError("Number of points for a face should be >= 3")
Exemplo n.º 9
0
line2 = vtk.vtkLine()
line2.GetPointIds().SetId(0,2)
line2.GetPointIds().SetId(1,3)

#Create a cellArray containing the lines
lines =vtk.vtkCellArray()
lines.InsertNextCell(line1)
lines.InsertNextCell(line2)
 
#Create the vtkPolyData to contain the points and cellArray with the lines
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
polydata.SetLines(lines)
 
#Create the ruledSurfaceFilter from the polydata containing the lines
ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
ruledSurfaceFilter.SetInput(polydata)
ruledSurfaceFilter.SetResolution(21, 21)
ruledSurfaceFilter.SetRuledModeToResample()

#Create the mapper with the ruledSufaceFilter as input
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(ruledSurfaceFilter.GetOutput())

#create the actor with the mapper
actor = vtk.vtkActor()
actor.SetMapper(mapper)

#add the actor to the display
ren.AddActor(actor)
Exemplo n.º 10
0
def main(vector_file, magnitude_file):

    ColorRange = vtk.vtkLookupTable()
    ColorRange.SetTableRange(0, 1)
    ColorRange.SetHueRange(0, 1)
    ColorRange.SetSaturationRange(1, 1)
    ColorRange.SetAlphaRange(0.3, 0.5)
    ColorRange.Build()

    reader = vtk.vtkStructuredPointsReader()
    reader.SetFileName(vector_file)
    reader.Update()

    data = reader.GetOutput()

    range1 = data.GetScalarRange()
    v0 = range1[0]
    v1 = range1[1]

    reader_magnitude = vtk.vtkStructuredPointsReader()
    reader_magnitude.SetFileName(magnitude_file)
    reader_magnitude.Update()

    contour = vtk.vtkContourFilter()
    contour.SetInput(reader_magnitude.GetOutput())
    contour.SetValue(0, 1)

    normals = vtk.vtkPolyDataNormals()
    normals.SetInput(contour.GetOutput())
    normals.SetFeatureAngle(60)
    normals.ConsistencyOff()
    normals.SplittingOff()

    mapper_magnitude = vtk.vtkPolyDataMapper()
    mapper_magnitude.SetInput(normals.GetOutput())
    mapper_magnitude.SetLookupTable(ColorRange)
    mapper_magnitude.SetColorModeToMapScalars()
    mapper_magnitude.SetScalarRange(v0, v1)

    actor_magnitude = vtk.vtkActor()
    actor_magnitude.SetMapper(mapper_magnitude)

    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(2)
    sphere.SetCenter(15, 15, 15)  # Critical point for all 3 test datasets
    sphere.SetThetaResolution(10)

    integrator = vtk.vtkRungeKutta4()

    stream = vtk.vtkStreamLine()
    stream.SetInput(reader.GetOutput())
    stream.SetSource(sphere.GetOutput())
    stream.SetIntegrator(integrator)
    stream.SetMaximumPropagationTime(500)
    stream.SetIntegrationStepLength(0.1)
    stream.SetIntegrationDirectionToIntegrateBothDirections()
    stream.SetStepLength(0.1)

    scalarSurface = vtk.vtkRuledSurfaceFilter()
    scalarSurface.SetInput(stream.GetOutput())
    scalarSurface.SetOffset(0)
    scalarSurface.SetOnRatio(2)
    scalarSurface.PassLinesOn()
    scalarSurface.SetRuledModeToPointWalk()
    scalarSurface.SetDistanceFactor(50)

    tube = vtk.vtkTubeFilter()
    tube.SetInput(scalarSurface.GetOutput())
    tube.SetRadius(0.1)
    tube.SetNumberOfSides(6)

    dataMapper = vtk.vtkPolyDataMapper()
    dataMapper.SetInput(tube.GetOutput())
    dataMapper.SetScalarRange(v0, v1)
    dataMapper.SetLookupTable(ColorRange)

    dataActor = vtk.vtkActor()
    dataActor.SetMapper(dataMapper)

    probe = vtk.vtkProbeFilter()
    probe.SetInput(stream.GetOutput())
    probe.SetSource(reader.GetOutput())

    mask = vtk.vtkMaskPoints()
    mask.SetInput(probe.GetOutput())
    mask.SetOnRatio(60)
    mask.RandomModeOn()

    cone = vtk.vtkConeSource()
    cone.SetResolution(8)
    cone.SetHeight(1.0)
    cone.SetRadius(0.2)

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)

    transformFilter = vtk.vtkTransformPolyDataFilter()
    transformFilter.SetInput(cone.GetOutput())
    transformFilter.SetTransform(transform)

    glyph = vtk.vtkGlyph3D()
    glyph.SetInput(mask.GetOutput())
    glyph.SetSource(transformFilter.GetOutput())
    glyph.SetScaleModeToScaleByVector()
    glyph.SetScaleFactor(1.5)
    glyph.SetVectorModeToUseVector()
    glyph.SetColorModeToColorByVector()

    glyphMapper = vtk.vtkPolyDataMapper()
    glyphMapper.SetInput(glyph.GetOutput())
    glyphMapper.SetLookupTable(ColorRange)

    glyphActor = vtk.vtkActor()
    glyphActor.SetMapper(glyphMapper)

    outline = vtk.vtkOutlineFilter()
    outline.SetInput(reader.GetOutput())

    outlineMapper = vtk.vtkPolyDataMapper()
    outlineMapper.SetInput(outline.GetOutput())

    outlineActor = vtk.vtkActor()
    outlineActor.SetMapper(outlineMapper)
    outlineActor.GetProperty().SetColor(1, 1, 1)

    criticalMarker = vtk.vtkSphereSource()
    criticalMarker.SetRadius(1.0)
    criticalMarker.SetCenter(15, 15, 15)
    criticalMarker.SetThetaResolution(10)

    criticalMapper = vtk.vtkPolyDataMapper()
    criticalMapper.SetInput(criticalMarker.GetOutput())

    criticalActor = vtk.vtkActor()
    criticalActor.SetMapper(criticalMapper)
    criticalActor.GetProperty().SetColor(1, 1, 0)
    criticalActor.GetProperty().SetOpacity(0.5)

    colorActor = vtk.vtkScalarBarActor()
    colorActor.SetLookupTable(ColorRange)

    renderer = vtk.vtkRenderer()

    renderer_window = vtk.vtkRenderWindow()
    renderer_window.AddRenderer(renderer)
    renderer_window.SetSize(512, 512)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderer_window)
    renderer.AddActor(actor_magnitude)
    renderer.AddActor(outlineActor)
    renderer.AddActor(criticalActor)
    renderer.AddActor(colorActor)
    renderer.AddActor(dataActor)
    renderer.AddActor(glyphActor)

    renderer.ResetCamera()

    style = vtk.vtkInteractorStyleTrackballCamera()
    # style = vtk.vtkInteractorStyleRubberBandZoom()
    # style = vtk.vtkInteractorStyleTerrain()
    interactor.SetInteractorStyle(style)

    renderer_window.Render()
    interactor.Start()
Exemplo n.º 11
0
def Main():
    global ren
    # Create the RenderWindow, Renderer and both Actors
    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.SetMultiSamples(0)
    renWin.AddRenderer(ren)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.RemoveObservers('RightButtonPressEvent')
    iren.AddObserver('RightButtonPressEvent', print_camera_settings, 1.0)
    iren.AddObserver('RightButtonPressEvent', after_print_camera_settings,
                     -1.0)

    print "data: %s %s" % (sys.argv[1], sys.argv[2])

    cfdreader = vtk.vtkStructuredPointsReader()
    cfdreader.SetFileName(sys.argv[1])

    # setup wing data
    wingReader = vtk.vtkUnstructuredGridReader()
    wingReader.SetFileName(sys.argv[2])
    wingReader.Update()
    wingMapper = vtk.vtkDataSetMapper()
    wingMapper.SetInputConnection(wingReader.GetOutputPort())
    wingActor = vtk.vtkActor()
    wingActor.SetMapper(wingMapper)
    wingActor.GetProperty().SetColor(.4, .4, .4)

    bRakesToActor = [True, True, True]
    bWingToActor = True

    datamin = 0
    datamax = 230

    lut = vtk.vtkColorTransferFunction()
    lut.SetColorSpaceToHSV()
    lut.AddHSVPoint(datamin, 0, 1, 1)
    dis = float(datamax - datamin) / 7
    for i in range(0, 8):
        lut.AddHSVPoint(float(datamin + dis * i), 0.1 * i, 1, 1)

    colorBar = vtk.vtkScalarBarActor()
    colorBar.SetLookupTable(lut)
    colorBar.SetTitle("")
    colorBar.SetNumberOfLabels(6)
    colorBar.SetLabelFormat("%4.0f")
    colorBar.SetPosition(0.9, 0.1)
    colorBar.SetWidth(0.05)
    colorBar.SetHeight(0.4)
    ren.AddActor(colorBar)

    rakes = [vtk.vtkLineSource(), vtk.vtkLineSource(), vtk.vtkLineSource()]
    rakes[0].SetPoint1(-230, -230, 0)
    rakes[0].SetPoint2(230, 230, 0)
    rakes[0].SetResolution(50)

    rakes[1].SetPoint1(230, -230, 0)
    rakes[1].SetPoint2(-230, 230, 0)
    rakes[1].SetResolution(50)

    #     rakes[2].SetPoint1(0, -200, 10)
    #     rakes[2].SetPoint2(0, 200, 10)
    #     rakes[2].SetResolution(50)

    for i in range(0, len(rakes)):
        rakeMapper = vtk.vtkPolyDataMapper()
        rakeMapper.SetInputConnection(rakes[i].GetOutputPort())
        rakeActor = vtk.vtkActor()
        rakeActor.SetMapper(rakeMapper)

        integ = vtk.vtkRungeKutta4()
        streamLine = vtk.vtkStreamLine()
        streamLine.SetInputConnection(cfdreader.GetOutputPort())
        streamLine.SetSourceConnection(rakes[i].GetOutputPort())
        streamLine.SetMaximumPropagationTime(50)
        streamLine.SetIntegrationStepLength(.1)
        streamLine.SetStepLength(0.001)
        streamLine.SetIntegrationDirectionToForward()
        streamLine.SetIntegrator(integ)
        streamLine.SpeedScalarsOn()

        scalarSurface = vtk.vtkRuledSurfaceFilter()
        scalarSurface.SetInputConnection(streamLine.GetOutputPort())
        scalarSurface.SetOffset(0)
        scalarSurface.SetOnRatio(0)
        #         scalarSurface.PassLinesOn()
        scalarSurface.SetRuledModeToPointWalk()
        #         scalarSurface.SetDistanceFactor(40)

        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(scalarSurface.GetOutputPort())
        mapper.SetLookupTable(lut)
        mapper.SetScalarRange(cfdreader.GetOutput().GetScalarRange())
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(0, 1, 0)
        actor.GetProperty().SetOpacity(0.5)

        if bRakesToActor[i]:
            ren.AddActor(actor)

    if bWingToActor:
        ren.AddActor(wingActor)

    ren.SetBackground(0, 0, 0)
    renWin.SetSize(1600, 900)

    ren.ResetCamera()
    ren.GetActiveCamera().SetClippingRange(417.55784439078775,
                                           1491.5763714138557)
    ren.GetActiveCamera().SetFocalPoint(118.72183980792761,
                                        0.00012969970703125,
                                        36.469017028808594)
    ren.GetActiveCamera().SetPosition(680.0192576650034, 16.65944318371372,
                                      790.5781258299678)
    ren.GetActiveCamera().SetViewUp(-0.802117714199773, -0.005112780752923929,
                                    0.5971440630533839)

    # Render
    iren.Initialize()
    renWin.Render()
    iren.Start()
Exemplo n.º 12
0
    def add_faces(self, faces, color, opacity=0.35):
        """
        Adding face of polygon.

        Args:
            faces (): Coordinates of the faces.
            color (): Color.
            opacity (float): Opacity
        """
        for face in faces:
            if len(face) == 3:
                points = vtk.vtkPoints()
                triangle = vtk.vtkTriangle()
                for ii in range(3):
                    points.InsertNextPoint(face[ii][0], face[ii][1], face[ii][2])
                    triangle.GetPointIds().SetId(ii, ii)
                triangles = vtk.vtkCellArray()
                triangles.InsertNextCell(triangle)
                trianglePolyData = vtk.vtkPolyData()
                trianglePolyData.SetPoints(points)
                trianglePolyData.SetPolys(triangles)
                mapper = vtk.vtkPolyDataMapper()
                if vtk.VTK_MAJOR_VERSION <= 5:
                    mapper.SetInputConnection(trianglePolyData.GetProducerPort())
                else:
                    mapper.SetInputData(trianglePolyData)
                # mapper.SetInput(trianglePolyData)
                ac = vtk.vtkActor()
                ac.SetMapper(mapper)
                ac.GetProperty().SetOpacity(opacity)
                ac.GetProperty().SetColor(color)
                self.ren.AddActor(ac)
            elif False and len(face) == 4:
                points = vtk.vtkPoints()
                for ii in range(4):
                    points.InsertNextPoint(face[ii][0], face[ii][1], face[ii][2])
                line1 = vtk.vtkLine()
                line1.GetPointIds().SetId(0, 0)
                line1.GetPointIds().SetId(1, 2)
                line2 = vtk.vtkLine()
                line2.GetPointIds().SetId(0, 3)
                line2.GetPointIds().SetId(1, 1)
                lines = vtk.vtkCellArray()
                lines.InsertNextCell(line1)
                lines.InsertNextCell(line2)
                polydata = vtk.vtkPolyData()
                polydata.SetPoints(points)
                polydata.SetLines(lines)
                ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
                ruledSurfaceFilter.SetInput(polydata)
                ruledSurfaceFilter.SetResolution(15, 15)
                ruledSurfaceFilter.SetRuledModeToResample()
                mapper = vtk.vtkPolyDataMapper()
                mapper.SetInput(ruledSurfaceFilter.GetOutput())
                ac = vtk.vtkActor()
                ac.SetMapper(mapper)
                ac.GetProperty().SetOpacity(opacity)
                ac.GetProperty().SetColor(color)
                self.ren.AddActor(ac)
            elif len(face) > 3:
                center = np.zeros(3, np.float_)
                for site in face:
                    center += site
                center /= np.float_(len(face))
                for ii, f in enumerate(face):
                    points = vtk.vtkPoints()
                    triangle = vtk.vtkTriangle()
                    points.InsertNextPoint(f[0], f[1], f[2])
                    ii2 = np.mod(ii + 1, len(face))
                    points.InsertNextPoint(face[ii2][0], face[ii2][1], face[ii2][2])
                    points.InsertNextPoint(center[0], center[1], center[2])
                    for ii in range(3):
                        triangle.GetPointIds().SetId(ii, ii)
                    triangles = vtk.vtkCellArray()
                    triangles.InsertNextCell(triangle)
                    trianglePolyData = vtk.vtkPolyData()
                    trianglePolyData.SetPoints(points)
                    trianglePolyData.SetPolys(triangles)
                    mapper = vtk.vtkPolyDataMapper()
                    if vtk.VTK_MAJOR_VERSION <= 5:
                        mapper.SetInputConnection(trianglePolyData.GetProducerPort())
                    else:
                        mapper.SetInputData(trianglePolyData)
                    # mapper.SetInput(trianglePolyData)
                    ac = vtk.vtkActor()
                    ac.SetMapper(mapper)
                    ac.GetProperty().SetOpacity(opacity)
                    ac.GetProperty().SetColor(color)
                    self.ren.AddActor(ac)
            else:
                raise ValueError("Number of points for a face should be >= 3")
Exemplo n.º 13
0
def main():
    colors = vtk.vtkNamedColors()

    # create a rendering window and renderer
    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)

    # create a renderwindowinteractor
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    # Create the points fot the lines.
    points = vtk.vtkPoints()
    points.InsertPoint(0, 0, 0, 1)
    points.InsertPoint(1, 1, 0, 0)
    points.InsertPoint(2, 0, 1, 0)
    points.InsertPoint(3, 1, 1, 1)

    # Create line1
    line1 = vtk.vtkLine()
    line1.GetPointIds().SetId(0, 0)
    line1.GetPointIds().SetId(1, 1)

    # Create line2
    line2 = vtk.vtkLine()
    line2.GetPointIds().SetId(0, 2)
    line2.GetPointIds().SetId(1, 3)

    # Create a cellArray containing the lines
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(line1)
    lines.InsertNextCell(line2)

    # Create the vtkPolyData to contain the points and cellArray with the lines
    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.SetLines(lines)

    # Create the ruledSurfaceFilter from the polydata containing the lines
    ruledSurfaceFilter = vtk.vtkRuledSurfaceFilter()
    ruledSurfaceFilter.SetInputData(polydata)
    ruledSurfaceFilter.SetResolution(21, 21)
    ruledSurfaceFilter.SetRuledModeToResample()

    # Create the mapper with the ruledSufaceFilter as input
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(ruledSurfaceFilter.GetOutputPort())

    # create the actor with the mapper
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(colors.GetColor3d("Gold"))
    actor.GetProperty().SetSpecular(0.6)
    actor.GetProperty().SetSpecularPower(30)

    # add the actor to the display
    ren.AddActor(actor)
    ren.SetBackground(colors.GetColor3d("MidnightBlue"))

    # enable user interface interactor
    iren.Initialize()
    renWin.Render()
    ren.GetActiveCamera().SetPosition(3.7, -0.5, -0.5)
    ren.GetActiveCamera().SetFocalPoint(0.5, 0.5, 0.5)
    ren.GetActiveCamera().SetViewUp(-0.3, 0.1, -1.0)
    renWin.Render()
    iren.Start()
Exemplo n.º 14
0
    def execute_module(self):
        if self._giaHumerus and self._giaGlenoid and \
           len(self._glenoidEdge) >= 6 and self._inputPolyData:

            # _glenoidEdgeImplicitFunction
            
            # construct eight planes with the insertion axis as mid-line
            # the planes should go somewhat further proximally than the
            # proximal insertion axis point

            # first calculate the distal-proximal glenoid insertion axis
            gia = tuple(map(operator.sub, self._giaGlenoid, self._giaHumerus))
            # and in one swift move, we normalize it and get the magnitude
            giaN = list(gia)
            giaM = vtk.vtkMath.Normalize(giaN)

            # extend gia with a few millimetres
            giaM += 5
            gia = tuple([giaM * i  for i in giaN])

            stuff = []
            yN = [0,0,0]
            zN = [0,0,0]
            angleIncr = 2.0 * vtk.vtkMath.Pi() / 8.0
            for i in range(4):
                angle = float(i) * angleIncr
                vtk.vtkMath.Perpendiculars(gia, yN, zN, angle)
                # each ridge is 1 cm (10 mm) - we'll change this later
                y = [10.0 * j for j in yN]
                
                origin = map(operator.add, self._giaHumerus, y)
                point1 = map(operator.add, origin, [-2.0 * k for k in y])
                point2 = map(operator.add, origin, gia)

                # now create the plane source
                ps = vtk.vtkPlaneSource()
                ps.SetOrigin(origin)
                ps.SetPoint1(point1)
                ps.SetPoint2(point2)
                ps.Update()

                plane = vtk.vtkPlane()
                plane.SetOrigin(ps.GetOrigin())
                plane.SetNormal(ps.GetNormal())

                pdn = vtk.vtkPolyDataNormals()
                pdn.SetInput(self._inputPolyData)

                cut = vtk.vtkCutter()
                cut.SetInput(pdn.GetOutput())
                cut.SetCutFunction(plane)
                cut.GenerateCutScalarsOn()
                cut.SetValue(0,0)
                cut.Update()

                contour = cut.GetOutput()

                # now find line segment closest to self._giaGlenoid
                pl = vtk.vtkPointLocator()
                pl.SetDataSet(contour)
                pl.BuildLocator()
                startPtId = pl.FindClosestPoint(self._giaGlenoid)

                cellIds = vtk.vtkIdList()
                contour.GetPointCells(startPtId, cellIds)

                twoLineIds = cellIds.GetId(0), cellIds.GetId(1)

                ptIds = vtk.vtkIdList()
                cellIds = vtk.vtkIdList()

                # we'll use these to store tuples:
                # (ptId, (pt0, pt1, pt2), (n0, n1, n2))
                lines = [[],[]]
                lineIdx = 0
                for startLineId in twoLineIds:

                    # we have a startLineId, a startPtId and polyData
                    curStartPtId = startPtId
                    curLineId = startLineId
                    
                    onGlenoid = True
                    offCount = 0
                    while onGlenoid:
                        contour.GetCellPoints(curLineId, ptIds)
                        if ptIds.GetNumberOfIds() != 2:
                            print 'aaaaaaaaaaaaack!'
                            
                        ptId0 = ptIds.GetId(0)
                        ptId1 = ptIds.GetId(1)
                        nextPointId = [ptId0, ptId1]\
                                      [bool(ptId0 == curStartPtId)]

                        contour.GetPointCells(nextPointId, cellIds)
                        if cellIds.GetNumberOfIds() != 2:
                            print 'aaaaaaaaaaaaaaaack2!'
                        cId0 = cellIds.GetId(0)
                        cId1 = cellIds.GetId(1)
                        nextLineId = [cId0, cId1]\
                                     [bool(cId0 == curLineId)]


                        # get the normal for the current point
                        n = contour.GetPointData().GetNormals().GetTuple3(
                            curStartPtId)

                        # get the current point
                        pt0 = contour.GetPoints().GetPoint(curStartPtId)

                        # store the real ptid, point coords and normal
                        lines[lineIdx].append((curStartPtId,
                                               tuple(pt0), tuple(n)))

                        
                        if vtk.vtkMath.Dot(giaN, n) > -0.9:
                            # this means that this point could be falling off
                            # the glenoid, let's make a note of the incident
                            offCount += 1
                            # if the last N points have been "off" the glenoid,
                            # it could mean we've really fallen off!
                            if offCount >= 40:
                                del lines[lineIdx][-40:]
                                onGlenoid = False

                        # get ready for next iteration
                        curStartPtId = nextPointId
                        curLineId = nextLineId

                
                    # closes: while onGlenoid
                    lineIdx += 1

                # closes: for startLineId in twoLineIds
                # we now have two line lists... we have to combine them and
                # make sure it still constitutes one long line
                lines[0].reverse()
                edgeLine = lines[0] + lines[1]

                # do line extrusion resulting in a list of 5-element tuples,
                # each tuple representing the 5 3-d vertices of a "house"
                houses = self._lineExtrudeHouse(edgeLine, plane)
                
                # we will dump ALL the new points in here
                newPoints = vtk.vtkPoints()
                newPoints.SetDataType(contour.GetPoints().GetDataType())
                # but we're going to create 5 lines
                idLists = [vtk.vtkIdList() for i in range(5)]

                for house in houses:
                    for vertexIdx in range(5):
                        ptId = newPoints.InsertNextPoint(house[vertexIdx])
                        idLists[vertexIdx].InsertNextId(ptId)
                    
                # create a cell with the 5 lines
                newCellArray = vtk.vtkCellArray()
                for idList in idLists:
                    newCellArray.InsertNextCell(idList)

                newPolyData = vtk.vtkPolyData()
                newPolyData.SetLines(newCellArray)
                newPolyData.SetPoints(newPoints)


                rsf = vtk.vtkRuledSurfaceFilter()
                rsf.CloseSurfaceOn()
                #rsf.SetRuledModeToPointWalk()
                rsf.SetRuledModeToResample()
                rsf.SetResolution(128, 4)
                rsf.SetInput(newPolyData)
                rsf.Update()

                stuff.append(rsf.GetOutput())

                # also add two housies to cap all the ends
                capHousePoints = vtk.vtkPoints()
                capHouses = []
                if len(houses) > 1:
                    # we only cap if there are at least two houses
                    capHouses.append(houses[0])
                    capHouses.append(houses[-1])
                    
                capHouseIdLists = [vtk.vtkIdList() for dummy in capHouses]
                for capHouseIdx in range(len(capHouseIdLists)):
                    house = capHouses[capHouseIdx]
                    for vertexIdx in range(5):
                        ptId = capHousePoints.InsertNextPoint(house[vertexIdx])
                        capHouseIdLists[capHouseIdx].InsertNextId(ptId)

                if capHouseIdLists:
                    newPolyArray = vtk.vtkCellArray()
                    for capHouseIdList in capHouseIdLists:
                        newPolyArray.InsertNextCell(capHouseIdList)

                    capPolyData = vtk.vtkPolyData()
                    capPolyData.SetPoints(capHousePoints)
                    capPolyData.SetPolys(newPolyArray)
                        
                    # FIXME: put back
                    stuff.append(capPolyData)
            
            # closes: for i in range(4)
            ap = vtk.vtkAppendPolyData()
            # copy everything to output (for testing)
            for thing in stuff:
                ap.AddInput(thing)
            #ap.AddInput(stuff[0])
 
            # seems to be important for vtkAppendPolyData
            ap.Update()

            # now cut it with the FBZ planes
            fbzSupPlane = self._fbzCutPlane(self._fbzSup, giaN,
                                            self._giaGlenoid)
            fbzSupClip = vtk.vtkClipPolyData()
            fbzSupClip.SetClipFunction(fbzSupPlane)
            fbzSupClip.SetValue(0)
            fbzSupClip.SetInput(ap.GetOutput())

            fbzInfPlane = self._fbzCutPlane(self._fbzInf, giaN,
                                            self._giaGlenoid)
            fbzInfClip = vtk.vtkClipPolyData()
            fbzInfClip.SetClipFunction(fbzInfPlane)
            fbzInfClip.SetValue(0)
            fbzInfClip.SetInput(fbzSupClip.GetOutput())

            cylinder = vtk.vtkCylinder()
            cylinder.SetCenter([0,0,0])
            # we make the cut-cylinder slightly larger... it's only there
            # to cut away the surface edges, so precision is not relevant
            cylinder.SetRadius(self.drillGuideInnerDiameter / 2.0)

            # cylinder is oriented along y-axis (0,1,0) -
            # we need to calculate the angle between the y-axis and the gia
            # 1. calc dot product (|a||b|cos(\phi))
            cylDotGia = - giaN[1]
            # 2. because both factors are normals, angle == acos
            phiRads = math.acos(cylDotGia)
            # 3. cp is the vector around which gia can be turned to
            #    coincide with the y-axis
            cp = [0,0,0]
            vtk.vtkMath.Cross((-giaN[0], -giaN[1], -giaN[2]),
                              (0.0, 1.0, 0.0), cp)

            # this transform will be applied to all points BEFORE they are
            # tested on the cylinder implicit function
            trfm = vtk.vtkTransform()
            # it's premultiply by default, so the last operation will get
            # applied FIRST:
            # THEN rotate it around the cp axis so it's relative to the
            # y-axis instead of the gia-axis
            trfm.RotateWXYZ(phiRads * vtk.vtkMath.RadiansToDegrees(),
                            cp[0], cp[1], cp[2])
            # first translate the point back to the origin
            trfm.Translate(-self._giaGlenoid[0], -self._giaGlenoid[1],
                           -self._giaGlenoid[2])

            cylinder.SetTransform(trfm)

            cylinderClip = vtk.vtkClipPolyData()
            cylinderClip.SetClipFunction(cylinder)
            cylinderClip.SetValue(0)
            cylinderClip.SetInput(fbzInfClip.GetOutput())
            cylinderClip.GenerateClipScalarsOn()

            ap2 = vtk.vtkAppendPolyData()
            ap2.AddInput(cylinderClip.GetOutput())
            # this will cap the just cut polydata
            ap2.AddInput(self._capCutPolyData(fbzSupClip))
            ap2.AddInput(self._capCutPolyData(fbzInfClip))
            # thees one she dosint werk so gooood
            #ap2.AddInput(self._capCutPolyData(cylinderClip))

            # now add outer guide cylinder, NOT capped
            cs1 = vtk.vtkCylinderSource()
            cs1.SetResolution(32)
            cs1.SetRadius(self.drillGuideOuterDiameter / 2.0)
            cs1.CappingOff()
            cs1.SetHeight(self.drillGuideHeight) # 15 mm height
            cs1.SetCenter(0,0,0)
            cs1.Update()

            # inner cylinder
            cs2 = vtk.vtkCylinderSource()
            cs2.SetResolution(32)
            cs2.SetRadius(self.drillGuideInnerDiameter / 2.0)
            cs2.CappingOff()
            cs2.SetHeight(self.drillGuideHeight) # 15 mm height
            cs2.SetCenter(0,0,0)
            cs2.Update()

            # top cap
            tc = vtk.vtkDiskSource()
            tc.SetInnerRadius(self.drillGuideInnerDiameter / 2.0)
            tc.SetOuterRadius(self.drillGuideOuterDiameter / 2.0)
            tc.SetCircumferentialResolution(64)

            tcTrfm = vtk.vtkTransform()

            # THEN flip it so that its centre-line is the y-axis
            tcTrfm.RotateX(90)
            # FIRST translate the disc
            tcTrfm.Translate(0,0,- self.drillGuideHeight / 2.0)            
            tcTPDF = vtk.vtkTransformPolyDataFilter()
            tcTPDF.SetTransform(tcTrfm)
            tcTPDF.SetInput(tc.GetOutput())

            # bottom cap
            bc = vtk.vtkDiskSource()
            bc.SetInnerRadius(self.drillGuideInnerDiameter / 2.0)
            bc.SetOuterRadius(self.drillGuideOuterDiameter / 2.0)
            bc.SetCircumferentialResolution(64)

            bcTrfm = vtk.vtkTransform()

            # THEN flip it so that its centre-line is the y-axis
            bcTrfm.RotateX(90)
            # FIRST translate the disc
            bcTrfm.Translate(0,0, self.drillGuideHeight / 2.0)            
            bcTPDF = vtk.vtkTransformPolyDataFilter()
            bcTPDF.SetTransform(bcTrfm)
            bcTPDF.SetInput(bc.GetOutput())

            tubeAP = vtk.vtkAppendPolyData()
            tubeAP.AddInput(cs1.GetOutput())
            tubeAP.AddInput(cs2.GetOutput())
            tubeAP.AddInput(tcTPDF.GetOutput())
            tubeAP.AddInput(bcTPDF.GetOutput())            

            # we have to transform this f****r as well
            csTrfm = vtk.vtkTransform()
            # go half the height + 2mm upwards from surface
            drillGuideCentre = - 1.0 * self.drillGuideHeight / 2.0 - 2
            cs1Centre = map(operator.add,
                            self._giaGlenoid,
                            [drillGuideCentre * i for i in giaN])
            # once again, this is performed LAST
            csTrfm.Translate(cs1Centre)
            # and this FIRST (we have to rotate the OTHER way than for
            # the implicit cylinder cutting, because the cylinder is
            # transformed from y-axis to gia, not the other way round)
            csTrfm.RotateWXYZ(-phiRads * vtk.vtkMath.RadiansToDegrees(),
                              cp[0], cp[1], cp[2])
            # actually perform the transform
            csTPDF = vtk.vtkTransformPolyDataFilter()
            csTPDF.SetTransform(csTrfm)
            csTPDF.SetInput(tubeAP.GetOutput())
            csTPDF.Update()

            ap2.AddInput(csTPDF.GetOutput())

            ap2.Update()
            
            self._outputPolyData.DeepCopy(ap2.GetOutput())
Exemplo n.º 15
0
def ribbon(line1, line2, c='m', alpha=1, legend=None, res=(200, 5)):
    '''Connect two lines to generate the surface inbetween.

    .. hint:: Example: `ribbon.py <https://github.com/marcomusy/vtkplotter/blob/master/examples/basic/ribbon.py>`_

        .. image:: https://user-images.githubusercontent.com/32848391/50738851-be9bcb00-11d8-11e9-80ee-bd73c1c29c06.jpg
    '''
    if isinstance(line1, Actor):
        line1 = line1.coordinates()
    if isinstance(line2, Actor):
        line2 = line2.coordinates()

    ppoints1 = vtk.vtkPoints()  # Generate the polyline1
    ppoints1.SetData(numpy_to_vtk(line1, deep=True))
    lines1 = vtk.vtkCellArray()
    lines1.InsertNextCell(len(line1))
    for i in range(len(line1)):
        lines1.InsertCellPoint(i)
    poly1 = vtk.vtkPolyData()
    poly1.SetPoints(ppoints1)
    poly1.SetLines(lines1)

    ppoints2 = vtk.vtkPoints()  # Generate the polyline2
    ppoints2.SetData(numpy_to_vtk(line2, deep=True))
    lines2 = vtk.vtkCellArray()
    lines2.InsertNextCell(len(line2))
    for i in range(len(line2)):
        lines2.InsertCellPoint(i)
    poly2 = vtk.vtkPolyData()
    poly2.SetPoints(ppoints2)
    poly2.SetLines(lines2)

    # build the lines
    lines1 = vtk.vtkCellArray()
    lines1.InsertNextCell(poly1.GetNumberOfPoints())
    for i in range(poly1.GetNumberOfPoints()):
        lines1.InsertCellPoint(i)

    polygon1 = vtk.vtkPolyData()
    polygon1.SetPoints(ppoints1)
    polygon1.SetLines(lines1)

    lines2 = vtk.vtkCellArray()
    lines2.InsertNextCell(poly2.GetNumberOfPoints())
    for i in range(poly2.GetNumberOfPoints()):
        lines2.InsertCellPoint(i)

    polygon2 = vtk.vtkPolyData()
    polygon2.SetPoints(ppoints2)
    polygon2.SetLines(lines2)

    mergedPolyData = vtk.vtkAppendPolyData()
    mergedPolyData.AddInputData(polygon1)
    mergedPolyData.AddInputData(polygon2)
    mergedPolyData.Update()

    rsf = vtk.vtkRuledSurfaceFilter()
    rsf.CloseSurfaceOff()
    rsf.SetRuledModeToResample()
    rsf.SetResolution(res[0], res[1])
    rsf.SetInputData(mergedPolyData.GetOutput())
    rsf.Update()
    return Actor(rsf.GetOutput(), c=c, alpha=alpha, legend=legend)
Exemplo n.º 16
0
lines.InsertCellPoint(3)
profile = vtk.vtkPolyData()
profile.SetPoints(points)
profile.SetLines(lines)
xfm = vtk.vtkTransform()
xfm.Translate(0,0,8)
xfm.RotateZ(90)
xfmPd = vtk.vtkTransformPolyDataFilter()
xfmPd.SetInputData(profile)
xfmPd.SetTransform(xfm)
appendPD = vtk.vtkAppendPolyData()
appendPD.AddInputData(profile)
appendPD.AddInputConnection(xfmPd.GetOutputPort())
# extrude profile to make wall
#
extrude = vtk.vtkRuledSurfaceFilter()
extrude.SetInputConnection(appendPD.GetOutputPort())
extrude.SetResolution(51,51)
extrude.SetRuledModeToResample()
map = vtk.vtkPolyDataMapper()
map.SetInputConnection(extrude.GetOutputPort())
wall = vtk.vtkActor()
wall.SetMapper(map)
wall.GetProperty().SetColor(0.3800,0.7000,0.1600)
# Add the actors to the renderer, set the background and size
#
ren1.AddActor(wall)
ren1.SetBackground(1,1,1)
renWin.SetSize(200,200)
ren1.GetActiveCamera().SetPosition(12.9841,-1.81551,8.82706)
ren1.GetActiveCamera().SetFocalPoint(0.5,1,4)
Exemplo n.º 17
0
def main(vector_file, magnitude_file):

    num_critical_points = 6
    CriticalPoints = vtk.vtkPoints()

    CriticalPoints.InsertNextPoint(35, 14, 20)
    CriticalPoints.InsertNextPoint(55, 15, 20)
    CriticalPoints.InsertNextPoint(65, 45, 19)
    CriticalPoints.InsertNextPoint(45, 44.8, 20)
    CriticalPoints.InsertNextPoint(20, 29.7, 19.8)
    CriticalPoints.InsertNextPoint(10, 32.2, 16.1)

    ColorRange = vtk.vtkLookupTable()
    ColorRange.SetTableRange(0, 1)
    ColorRange.SetHueRange(0, 1)
    ColorRange.SetSaturationRange(1, 1)
    ColorRange.SetAlphaRange(0.3, 0.5)
    ColorRange.Build()

    reader = vtk.vtkStructuredPointsReader()
    reader.SetFileName(vector_file)
    reader.Update()

    mags = reader.GetOutput()

    range1 = mags.GetScalarRange()
    v0 = range1[0]
    v1 = range1[1]

    reader_magnitude = vtk.vtkStructuredPointsReader()
    reader_magnitude.SetFileName(magnitude_file)
    reader_magnitude.Update()

    # All entities initialized equal to number of critical points
    sphere1, stream1, scalarSurface1, tube1, dataMapper1, dataActor1, criticalMarker1, criticalMapper1, criticalActor1, probe1, mask1, glyph1, glyphMapper1, glyphActor1, plane1 = (
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
        [],
    )
    for i in range(0, num_critical_points):
        sphere1.append(vtk.vtkSphereSource())
        stream1.append(vtk.vtkStreamLine())
        scalarSurface1.append(vtk.vtkRuledSurfaceFilter())
        tube1.append(vtk.vtkTubeFilter())
        dataMapper1.append(vtk.vtkPolyDataMapper())
        dataActor1.append(vtk.vtkActor())

        criticalMarker1.append(vtk.vtkSphereSource())
        criticalMapper1.append(vtk.vtkPolyDataMapper())
        criticalActor1.append(vtk.vtkActor())

        probe1.append(vtk.vtkProbeFilter())
        mask1.append(vtk.vtkMaskPoints())
        glyph1.append(vtk.vtkGlyph3D())
        glyphMapper1.append(vtk.vtkPolyDataMapper())
        glyphActor1.append(vtk.vtkActor())

        plane1.append(vtk.vtkPlaneSource())

    integ = vtk.vtkRungeKutta4()

    cone = vtk.vtkConeSource()
    cone.SetResolution(8)
    cone.SetHeight(1.0)
    cone.SetRadius(0.2)

    transform = vtk.vtkTransform()
    transform.Translate(0, 0, 0)

    transformFilter = vtk.vtkTransformPolyDataFilter()
    transformFilter.SetInput(cone.GetOutput())
    transformFilter.SetTransform(transform)

    outline = vtk.vtkOutlineFilter()
    outline.SetInput(reader.GetOutput())

    outlineMapper = vtk.vtkPolyDataMapper()
    outlineMapper.SetInput(outline.GetOutput())

    outlineActor = vtk.vtkActor()
    outlineActor.SetMapper(outlineMapper)
    outlineActor.GetProperty().SetColor(1, 1, 1)

    bar = vtk.vtkScalarBarActor()
    bar.SetLookupTable(ColorRange)

    renderer = vtk.vtkRenderer()

    for i in range(0, num_critical_points):
        sphere1[i].SetRadius(2)
        sphere1[i].SetCenter(
            CriticalPoints.GetPoint(i)[0], CriticalPoints.GetPoint(i)[1], CriticalPoints.GetPoint(i)[2]
        )
        sphere1[i].SetThetaResolution(1)
        stream1[i].SetInput(reader.GetOutput())
        stream1[i].SetSource(sphere1[i].GetOutput())
        stream1[i].SetIntegrator(integ)
        stream1[i].SetMaximumPropagationTime(500)
        stream1[i].SetIntegrationStepLength(0.1)
        stream1[i].SetIntegrationDirectionToIntegrateBothDirections()
        stream1[i].SetStepLength(0.1)

        scalarSurface1[i].SetInput(stream1[i].GetOutput())
        scalarSurface1[i].SetOffset(0)
        scalarSurface1[i].SetOnRatio(2)
        scalarSurface1[i].PassLinesOn()
        scalarSurface1[i].SetRuledModeToPointWalk()
        scalarSurface1[i].SetDistanceFactor(50)

        tube1[i].SetInput(scalarSurface1[i].GetOutput())
        tube1[i].SetRadius(0.1)
        tube1[i].SetNumberOfSides(6)

        dataMapper1[i].SetInput(tube1[i].GetOutput())
        dataMapper1[i].SetScalarRange(v0, v1)
        dataMapper1[i].SetLookupTable(ColorRange)

        dataActor1[i].SetMapper(dataMapper1[i])
        # renderer.AddActor(dataActor1[i])

        criticalMarker1[i].SetRadius(1.0)
        criticalMarker1[i].SetCenter(
            CriticalPoints.GetPoint(i)[0], CriticalPoints.GetPoint(i)[1], CriticalPoints.GetPoint(i)[2]
        )
        criticalMarker1[i].SetThetaResolution(10)
        criticalMapper1[i].SetInput(criticalMarker1[i].GetOutput())

        criticalActor1[i].SetMapper(criticalMapper1[i])
        criticalActor1[i].GetProperty().SetColor(1, 1, 0)
        criticalActor1[i].GetProperty().SetOpacity(0.5)
        # renderer.AddActor(criticalActor1[i])

        probe1[i].SetInput(stream1[i].GetOutput())
        probe1[i].SetSource(reader.GetOutput())

        mask1[i].SetInput(probe1[i].GetOutput())
        mask1[i].SetOnRatio(60)
        mask1[i].RandomModeOn()

        glyph1[i].SetInput(mask1[i].GetOutput())
        glyph1[i].SetSource(transformFilter.GetOutput())
        glyph1[i].SetScaleModeToScaleByVector()
        glyph1[i].SetScaleFactor(2)
        glyph1[i].SetVectorModeToUseVector()
        glyph1[i].SetColorModeToColorByVector()

        glyphMapper1[i].SetInput(glyph1[i].GetOutput())
        glyphMapper1[i].SetLookupTable(ColorRange)

        glyphActor1[i].SetMapper(glyphMapper1[i])
        # renderer.AddActor(glyphActor1[i])

    # removeActors1(renderer, dataActor, criticalActor, glyphActor, dataActor1, criticalActor1, glyphActor1)

    mags = reader.GetOutput()
    bounds = mags.GetBounds()
    x0 = bounds[0]
    x1 = bounds[1]
    y0 = bounds[2]
    y1 = bounds[3]
    z0 = bounds[4]
    z1 = bounds[5]

    range1 = mags.GetScalarRange()
    v0 = range1[0]
    v1 = range1[1]

    plane1[0].SetOrigin(x0, y0, z0)
    plane1[0].SetPoint1(x0, y1, z0)
    plane1[0].SetPoint2(x0, y0, z1)

    plane1[1].SetOrigin(x0, y0, z0)
    plane1[1].SetPoint1(x0, y1, z0)
    plane1[1].SetPoint2(x1, y0, z0)

    plane1[2].SetOrigin(x0, y0, z0)
    plane1[2].SetPoint1(x0, y0, z1)
    plane1[2].SetPoint2(x1, y0, z0)

    plane1[3].SetOrigin(x1, y1, z1)
    plane1[3].SetPoint1(x1, y1, z0)
    plane1[3].SetPoint2(x1, y0, z1)

    plane1[4].SetOrigin(x1, y1, z1)
    plane1[4].SetPoint1(x0, y1, z1)
    plane1[4].SetPoint2(x1, y1, z0)

    plane1[5].SetOrigin(x1, y1, z1)
    plane1[5].SetPoint1(x0, y1, z1)
    plane1[5].SetPoint2(x1, y1, z0)

    for i in range(0, num_critical_points):
        plane1[i].SetResolution(5, 5)
        stream1[i].SetSource(plane1[i].GetOutput())
        renderer.AddActor(dataActor1[i])
        renderer.AddActor(glyphActor1[i])
        glyph1[i].SetScaleFactor(4)

    renderer.AddActor(bar)
    renderer.AddActor(outlineActor)

    for i in range(0, num_critical_points):
        renderer.AddActor(criticalActor1[i])

    renderer_window = vtk.vtkRenderWindow()
    renderer_window.AddRenderer(renderer)
    renderer_window.SetSize(512, 512)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderer_window)

    style = vtk.vtkInteractorStyleTrackballCamera()
    interactor.SetInteractorStyle(style)

    renderer.AddActor(bar)

    renderer.AddActor(outlineActor)

    renderer_window.Render()
    interactor.Start()
Exemplo n.º 18
0
rakeActor = vtk.vtkActor()
rakeActor.SetMapper(rakeMapper)

integ = vtk.vtkRungeKutta4()
sl = vtk.vtkStreamTracer()
sl.SetInputData(pl3d_output)
sl.SetSourceConnection(rake.GetOutputPort())
sl.SetIntegrator(integ)
sl.SetMaximumPropagation(100)
sl.SetInitialIntegrationStep(0.1)
sl.SetIntegrationDirectionToBackward()

# The ruled surface stiches together lines with triangle strips.
# Note the SetOnRatio method. It turns on every other strip that
# the filter generates (only when multiple lines are input).
scalarSurface = vtk.vtkRuledSurfaceFilter()
scalarSurface.SetInputConnection(sl.GetOutputPort())
scalarSurface.SetOffset(0)
scalarSurface.SetOnRatio(2)
scalarSurface.PassLinesOn()
scalarSurface.SetRuledModeToPointWalk()
scalarSurface.SetDistanceFactor(30)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(scalarSurface.GetOutputPort())
mapper.SetScalarRange(pl3d_output.GetScalarRange())
actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Put an outline around for context.
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(pl3d_output)