> 文章列表 > Python VTK 绘制线条

Python VTK 绘制线条

Python VTK 绘制线条

 

前言:

   Python-VTK绘制线条,主要绘制直线和曲线

主要函数介绍:

vtk.vtkPoints() 在VTK中用于定义点的类,使用points.InsertPoint(index, x, y, z) 即可插入点集。函数中,第一个参数是点的序号,后面是三个参数是点的坐标。

vtk.vtkLineSource() 在VTK中定义直线的类,通过SetPoints(points),输入直线经过的点。

vtk.vtkParametricSpline() 在VTK中定义曲线的类,通过SetPoints(points),输入曲线经过的点。

vtk.vtkParametricFunctionSource() 曲线插值拟合函数,可以将输入的点集拟合成一条曲线。有很多生成方法。

actor.GetProperty().SetColor() 线条颜色配置

actor.GetProperty().SetLineWidth() 线条宽度配置

曲线 :

Code:

import vtkpoints = vtk.vtkPoints()  # 定义一个点工具
points.InsertPoint(0, 329, 338, 45)  # 使用InsertPoint可以插入点
# 注意:points.InsertPoint(a, b, c, d)
# 其中a表示点的序号,(b,c,d)表示点的三维坐标
points.InsertPoint(1, 328, 319, 46)
points.InsertPoint(2, 300, 329, 96)
# 定义曲线工具
# 将前面的几个点插值拟合成一条曲线
spline = vtk.vtkParametricSpline()
spline.SetPoints(points)splineSource = vtk.vtkParametricFunctionSource()
splineSource.SetParametricFunction(spline)
splineSource.Update()splineMapper = vtk.vtkPolyDataMapper()
splineMapper.SetInputConnection(splineSource.GetOutputPort())splineActor = vtk.vtkActor()
splineActor.SetMapper(splineMapper)
# 设置线条颜色
splineActor.GetProperty().SetColor(0.3800, 0.7000, 0.1600)
# 设置线条宽度
splineActor.GetProperty().SetLineWidth(5)ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren1.AddActor(splineActor)ren1.SetBackground(1, 1, 1)
renWin.SetSize(600, 600)
renWin.Render()
iren.Start()

直线:

 

Code:

import vtkpoints = vtk.vtkPoints()  # 定义一个点工具
points.InsertPoint(0, 329, 338, 45)  # 使用InsertPoint可以插入点
# 注意:points.InsertPoint(a, b, c, d)
# 其中a表示点的序号,(b,c,d)表示点的三维坐标
points.InsertPoint(1, 328, 319, 46)
points.InsertPoint(2, 300, 329, 96)
# 定义直线工具lineSource = vtk.vtkLineSource()
lineSource.SetPoints(points)lineSource.Update()lineMapper = vtk.vtkPolyDataMapper()
lineMapper.SetInputConnection(lineSource.GetOutputPort())splineActor = vtk.vtkActor()
splineActor.SetMapper(lineMapper)
# 设置线条颜色
splineActor.GetProperty().SetColor(0.3800, 0.7000, 0.1600)
# 设置线条宽度
splineActor.GetProperty().SetLineWidth(5)ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren1.AddActor(splineActor)ren1.SetBackground(1, 1, 1)
renWin.SetSize(600, 600)
renWin.Render()
iren.Start()