如何使用Python VTK绘制线条

 更新时间:2022年04月18日 13:14:04   作者:派大大大星   
这篇文章主要介绍了如何使用Python-VTK绘制线条,主要绘制直线和曲线,下面文章详细实现过程需要的小伙伴可以参考一下

主要函数介绍:

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

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

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

vtk.vtkParametricFunctionSource() 曲线插值拟合函数,可以将输入的点集拟合成一条曲线。有很多生成方法。我们可以简单的看一下VTK官方文档介绍

S\CALAR_NONE - Scalars are not generated (default).
SCALAR_U - The scalar is set to the u-value.
SCALAR_V - The scalar is set to the v-value.
SCALAR_U0 - The scalar is set to 1 if u = (u_max - u_min)/2 = u_avg, 0 otherwise.
SCALAR_V0 - The scalar is set to 1 if v = (v_max - v_min)/2 = v_avg, 0 otherwise.
SCALAR_U0V0 - The scalar is set to 1 if u == u_avg, 2 if v == v_avg, 3 if u = u_avg && v = v_avg, 0 otherwise.
SCALAR_MODULUS - The scalar is set to (sqrt(uu+vv)), this is measured relative to (u_avg,v_avg).
SCALAR_PHASE - The scalar is set to (atan2(v,u)) (in degrees, 0 to 360), this is measured relative to (u_avg,v_avg).
SCALAR_QUADRANT - The scalar is set to 1, 2, 3 or 4. depending upon the quadrant of the point (u,v).
SCALAR_X - The scalar is set to the x-value.
SCALAR_Y - The scalar is set to the y-value.
SCALAR_Z - The scalar is set to the z-value.
SCALAR_DISTANCE - The scalar is set to (sqrt(xx+yy+z*z)). I.e. distance from the origin.
SCALAR_USER_DEFINED - The scalar is set to the value returned from EvaluateScalar().

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

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

拟合曲线代码:

import vtk

points = 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(250, 250)
renWin.Render()
iren.Start()

image.png

绘制直线代码

import vtk

points = 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(250, 250)
renWin.Render()
iren.Start()

1636445384(1).jpg

到此这篇关于如何使用Python-VTK绘制线条的文章就介绍到这了,更多相关Python-VTK绘制线条内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python3.6笔记之将程序运行结果输出到文件的方法

    Python3.6笔记之将程序运行结果输出到文件的方法

    下面小编就为大家分享一篇Python3.6笔记之将程序运行结果输出到文件的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • python3用PIL把图片转换为RGB图片的实例

    python3用PIL把图片转换为RGB图片的实例

    今天小编就为大家分享一篇python3用PIL把图片转换为RGB图片的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • Django上使用数据可视化利器Bokeh解析

    Django上使用数据可视化利器Bokeh解析

    这篇文章主要介绍了Django上使用数据可视化利器Bokeh解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python获取当前git的repo地址的示例代码

    python获取当前git的repo地址的示例代码

    大家好,当谈及版本控制系统时,Git是最为广泛使用的一种,而Python作为一门多用途的编程语言,在处理Git仓库时也展现了其强大的能力,本文给大家介绍了python获取当前git的repo地址的方法,需要的朋友可以参考下
    2024-09-09
  • Python一行代码实现快速排序的方法

    Python一行代码实现快速排序的方法

    排序算法是在高考或中考中出现频率最多的点,所以大家要掌握,今天小编给大家带来了通过Python一行代码实现快速排序的方法,感兴趣的朋友跟随小编一起看看吧
    2019-04-04
  • 解决django xadmin主题不显示和只显示bootstrap2的问题

    解决django xadmin主题不显示和只显示bootstrap2的问题

    这篇文章主要介绍了解决django xadmin主题不显示和只显示bootstrap2的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取

    Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取

    这篇文章主要介绍了Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Python3与fastdfs分布式文件系统如何实现交互

    Python3与fastdfs分布式文件系统如何实现交互

    这篇文章主要介绍了Python3与fastdfs分布式文件系统如何实现交互,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python中通过预先编译正则表达式提高效率

    python中通过预先编译正则表达式提高效率

    Python是一个很酷的语言,因为你可以在很短的时间内利用很少的代码做很多事情,再加上正则表达式就更能体现其效果,下面这篇文章主要给大家介绍了关于python中通过预先编译正则表达式提高效率的相关资料,需要的朋友可以参考下。
    2017-09-09
  • python requests指定出口ip的例子

    python requests指定出口ip的例子

    今天小编就为大家分享一篇python requests指定出口ip的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07

最新评论