vtk studying(画线)

想要画线,那首先要先把点画出来。

不记得画点的话,请看第一篇。(温习一下)

points_list=[[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,0,4]]

points=vtk.vtkPoints()
vertices=vtk.vtkCellArray()
for i in points_list:
    point_id=points.InsertNextPoint(i)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(point_id)

然后你就可以画出点了(详细请看画点篇)

之后先说一下画线的逻辑,肯定是你刚才画出的某些点,然后将他们连起来

那么怎么把点连起来呢?

先来看一下下边的这个for循环

for i in points_list:
    point_id=points.InsertNextPoint(i)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(point_id)

 

 这个for循环每次执行一遍就回把某个点(比如(0,0,0))转变成vtk里来记录不同的点的编号

例如:(0,0,0):0     (0,0,1):1    (0,0,2):2

那知道了每个点的编号之后

 line=vtk.vtkLine()
 line.GetPointIds().SetId(0, i)
 line.GetPointIds().SetId(1, i+1)

这样就可以把point_id编号为i,i+1这两个点连成一条线。

so   上代码:(强烈建议,这篇和第一篇对比着看,便于理解)

顺便说一下:这篇的demo里我没有加axes_actor(第一篇里的一个演员  :坐标系)

也可以思考一下,一个ren渲染器里,如果想添加多个actor,应该如何操作

# # -*- coding: utf-8 -*-
# # !/usr/bin/env python
#
# import vtk
#
# points_list=[[1,0,0],[0,0,1],[0,0,0]]
#
# points=vtk.vtkPoints()
# vertices=vtk.vtkCellArray()
# for i in points_list:
#     point_id=points.InsertNextPoint(i)
#     vertices.InsertNextCell(1)
#     vertices.InsertCellPoint(point_id)
#
# # 创建线段类,用 point_id 将线段连起来
# line0=vtk.vtkLine()
# # 将 point_id 号为 0 1 的两个点连起来
# line0.GetPointIds().SetId(0,0)
# line0.GetPointIds().SetId(1,1)
# line1=vtk.vtkLine()
# line1.GetPointIds().SetId(0,1)
# line1.GetPointIds().SetId(1,2)
# line2=vtk.vtkLine()
# line2.GetPointIds().SetId(0,0)
# line2.GetPointIds().SetId(1,2)
#
# # 创建线段拓扑,,,,,可以和插入point点时候的拓扑写法做个对比
# # 创建线段拓扑,将三条线段放在一个cell里
# lines = vtk.vtkCellArray()
# lines.InsertNextCell(line0)
# lines.InsertNextCell(line1)
# lines.InsertNextCell(line2)
#
# # 创建几何数据对象
# polydata=vtk.vtkPolyData()
# polydata.SetPoints(points)
# # 如果去掉 vertices 会不显示顶点,只显示线段
# polydata.SetVerts(vertices)
# polydata.SetLines(lines)
#
# mapper=vtk.vtkPolyDataMapper()
# mapper.SetInputData(polydata)
#
# actor=vtk.vtkActor()
# actor.SetMapper(mapper)
# actor.GetProperty().SetPointSize(10)
# actor.GetProperty().SetLineWidth(2)
# actor.GetProperty().SetColor(0.0,1.0,0.0)
#
# ren=vtk.vtkRenderer()
# ren.SetBackground(0.8,0.3,0.8)
# ren.SetBackground(0.3,0.5,0.9)
# ren.SetGradientBackground(1)
# ren.AddActor(actor)
#
# ren_window=vtk.vtkRenderWindow()
# ren_window.SetWindowName('axiong first cube')
# ren_window.SetSize(300,300)
# ren_window.AddRenderer(ren)
#
# interactor=vtk.vtkRenderWindowInteractor()
# interactor.SetRenderWindow(ren_window)
#
# style=vtk.vtkInteractorStyleMultiTouchCamera()
# interactor.SetInteractorStyle(style)
#
# interactor.Initialize()
# ren_window.Render()
# interactor.Start()



# ----------------------------------------------------------------------------------



# -*- coding: utf-8 -*-
# !/usr/bin/env python
import vtk
points_list=[[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,0,4]]

points=vtk.vtkPoints()
vertices=vtk.vtkCellArray()
lines = vtk.vtkCellArray()
for i in points_list:
    point_id=points.InsertNextPoint(i)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(point_id)

for i in range(len(points_list)):
    line=vtk.vtkLine()
    line.GetPointIds().SetId(0, i)
    line.GetPointIds().SetId(1, i+1)
    lines.InsertNextCell(line)

# 创建几何数据对象
polydata=vtk.vtkPolyData()
polydata.SetPoints(points)
# 如果去掉 vertices 会不显示顶点,只显示线段
polydata.SetVerts(vertices)
polydata.SetLines(lines)

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

actor=vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetPointSize(10)
actor.GetProperty().SetLineWidth(2)
actor.GetProperty().SetColor(0.0,1.0,0.0)

ren=vtk.vtkRenderer()
ren.SetBackground(0.8,0.3,0.8)
ren.SetBackground(0.3,0.5,0.9)
ren.SetGradientBackground(1)
ren.AddActor(actor)

ren_window=vtk.vtkRenderWindow()
ren_window.SetWindowName('axiong first cube')
ren_window.SetSize(300,300)
ren_window.AddRenderer(ren)

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

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

interactor.Initialize()
ren_window.Render()
interactor.Start()

 建议这两篇demo代码彻底看懂,对vtk的流程有基础的了解。

最近在做一部分自动驾驶中高精度地图显示的操作,也用到了vtk画线,所以基础打好很重要。

什么?怎么画不同的线?

实线就是point_id:0-1,1-2,2-3,3-4,,,,,,这样遍历,挨个连起来,

虚线就是point_id:  0-1,2-3,4-5,,,,,这样遍历,挨个连

线的粗细直接用接口:actor.GetProperty().SetLineWidth(line_width) 

线的颜色直接用接口:actor.GetProperty().SetColor(255, 255, 255)

想显示线同时不显示点: polydata.SetVerts(vertices)  这句注释掉就可以

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值