VTK学习笔记(四十二)vtk绘制箭头

2023-12-24 22:35:03

VTK学习笔记(四十二)vtk绘制箭头

直接使用vtk自带的箭头源绘制的箭头长度固定为1mm,可以调整的参数都是在长度为1mm下的其他属性的调整,因此绘制任意长度的箭头需要自己代码实现。

# -*- coding : UTF-8 -*-
# @file   : draw_arrow.py
# @Time   : 2023/12/24 0024 20:49
# @Author : Administrator
import vtk


def create_single_vector_arrow_data(origin, target, length):
    vtk.vtkMath.Subtract(target, origin, target)
    points = vtk.vtkPoints()
    points.InsertNextPoint(origin)
    vertex = vtk.vtkVertex()
    vertex.GetPointIds().SetNumberOfIds(points.GetNumberOfPoints())
    for i in range(points.GetNumberOfPoints()):
        vertex.GetPointIds().SetId(i,i)

    normals = vtk.vtkDoubleArray()
    normals.SetNumberOfComponents(3)
    normals.InsertNextTuple(target)
    scalars = vtk.vtkDoubleArray()
    scalars.SetNumberOfComponents(1)
    scalars.SetName("scalars")
    scalars.InsertNextTuple1(length)
    vertices = vtk.vtkCellArray()
    vertices.InsertNextCell(vertex)
    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)  # 赋予起点
    polydata.SetVerts(vertices) # 赋予拓扑
    polydata.GetPointData().SetNormals(normals) # 赋予向量朝向
    polydata.GetPointData().SetScalars(scalars) # 赋予向量长度
    return polydata


def generate_normals_arrow(polydata):
    arrow = vtk.vtkArrowSource()
    arrow.Update()
    glyph = vtk.vtkGlyph3D()
    glyph.SetInputData(polydata)
    glyph.SetSourceData(arrow.GetOutput())
    glyph.SetScaleFactor(0.1)
    glyph.SetVectorModeToUseNormal()
    glyph.Update()
    return glyph.GetOutput()


def write_polydata(polydata, name):
    writer = vtk.vtkPolyDataWriter()
    writer.SetInputData(polydata)
    writer.SetFileName(name)
    writer.Write()


if __name__ == "__main__":
    origin = [0, 0, 0]
    target = [1, 0, 0]
    length = 200
    polydata = create_single_vector_arrow_data(origin, target, length)
    polydata = generate_normals_arrow(polydata)
    write_polydata(polydata, "arrow.vtk")



绘制箭头如下,水平向右,长度为20mm。

参考:【VTK】VTK绘制箭头——支持设置起点、方向和长度

文章来源:https://blog.csdn.net/juluwangriyue/article/details/135187353
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。