CutWithScalars根据标量利用vtkContourFilter得到等值线

一:主要的知识点

1、说明

本文只是教程内容的一小段,因博客字数限制,故进行拆分。主教程链接:vtk教程------逐行解析官网所有Python示例-CSDN博客

2、知识点纪要

本段代码主要涉及的有①vtkContour的用法,②点在平面哪一个方向的判定方法

二:代码及注释

python 复制代码
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkDoubleArray
from vtkmodules.vtkCommonDataModel import vtkPlane
from vtkmodules.vtkFiltersCore import vtkContourFilter, vtkMarchingCubes
from vtkmodules.vtkIOXML import vtkXMLPolyDataReader
from vtkmodules.vtkRenderingCore import vtkActor, vtkRenderer, vtkRenderWindow, vtkPolyDataMapper, \
    vtkRenderWindowInteractor
 
 
def main():
    file_name = "Data/Torso.vtp"
    num_of_cuts = 20
 
    colors = vtkNamedColors()
 
    reader = vtkXMLPolyDataReader()
    reader.SetFileName(file_name)
    reader.Update()
 
    bounds = reader.GetOutput().GetBounds()
 
    plane = vtkPlane()
    plane.SetOrigin((bounds[0] + bounds[1]) / 2,
                    (bounds[2] + bounds[3]) / 2,
                    (bounds[4] + bounds[5]) / 2)
    plane.SetNormal(0, 0, 1)
 
    # 计算vtp文件各个点到平面的有向距离
    scalars = vtkDoubleArray()
    numberOfPoints1 = reader.GetOutput().GetNumberOfPoints()
    scalars.SetNumberOfTuples(numberOfPoints1)
    pts = reader.GetOutput().GetPoints()
    for i in range(0, numberOfPoints1):
        point = pts.GetPoint(i)
        """
        EvaluateFunction 接收一个三维点作为输入,并返回一个带符号的标量值,值的大小就是输入点到平面的距离
        如果点在平面的正法线方向一侧,返回值是正数
        如果点在平面的负法线方向一侧,返回值是负数
        """
        scalars.SetTuple1(i, plane.EvaluateFunction(point))
    """
    告诉 VTK ------ 模型的每个点现在有一个标量值,这个标量值就是点到平面的距离。
    后面 vtkContourFilter 就会根据这些标量值来提取等值面
    """
    reader.GetOutput().GetPointData().SetScalars(scalars)
    reader.GetOutput().GetPointData().GetScalars().GetRange()
 
    """
    vtkContourFilter   VTK 中用于从标量场(scalar field)中提取等值面(或等值线)的核心算法
    """
    cutter = vtkContourFilter()
    cutter.SetInputConnection(reader.GetOutputPort())
    cutter.ComputeScalarsOff()
    cutter.ComputeNormalsOff()
    """
    GenerateValues  指定如何生成等值线
    第1个参数,指定要生成的等值线的数量
    第2个参数,指定等值线的最小值
    第3个参数,指定等值线爱你的最大值
    """
    cutter.GenerateValues(num_of_cuts, 0.99 * reader.GetOutput().GetPointData().GetScalars().GetRange()[0],
                          0.99 * reader.GetOutput().GetPointData().GetScalars().GetRange()[1])
 
    cutterMapper = vtkPolyDataMapper()
    cutterMapper.SetInputConnection(cutter.GetOutputPort())
    cutterMapper.ScalarVisibilityOff()
 
    cutterActor = vtkActor()
    cutterActor.SetMapper(cutterMapper)
    cutterActor.GetProperty().SetColor(colors.GetColor3d("Banana"))
    cutterActor.GetProperty().SetLineWidth(2)
 
    modelMapper = vtkPolyDataMapper()
    modelMapper.SetInputConnection(reader.GetOutputPort())
    modelMapper.ScalarVisibilityOff()
 
    modelActor = vtkActor()
    modelActor.GetProperty().SetColor(colors.GetColor3d("Flesh"))
    modelActor.SetMapper(modelMapper)
 
    renderer = vtkRenderer()
    renderer.AddActor(cutterActor)
    # renderer.AddActor(modelActor)
 
    renderWindow = vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(600, 600)
    renderWindow.SetWindowName('CutWithCutScalars')
 
    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)
 
    renderer.SetBackground(colors.GetColor3d('Burlywood'))
    renderer.GetActiveCamera().SetPosition(0, -1, 0)
    renderer.GetActiveCamera().SetFocalPoint(0, 0, 0)
    renderer.GetActiveCamera().SetViewUp(0, 0, 1)
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
 
    renderer.ResetCamera()
    renderWindow.Render()
 
    interactor.Start()
 
 
if __name__ == '__main__':
    main()
相关推荐
失散133 小时前
Python——1 概述
开发语言·python
萧鼎3 小时前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
qq_251533593 小时前
使用 Python 提取 MAC 地址
网络·python·macos
Data_agent5 小时前
学术爬虫实战:构建知网论文关键词共现网络的技术指南
python·算法
_一路向北_7 小时前
爬虫框架:Feapder使用心得
爬虫·python
皇族崛起7 小时前
【3D标注】- Unreal Engine 5.7 与 Python 交互基础
python·3d·ue5
你想知道什么?7 小时前
Python基础篇(上) 学习笔记
笔记·python·学习
Swizard8 小时前
速度与激情:Android Python + CameraX 零拷贝实时推理指南
android·python·ai·移动开发
一直跑8 小时前
Liunx服务器centos7离线升级内核(Liunx服务器centos7.9离线/升级系统内核)
python