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()
相关推荐
AI探索者7 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者7 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh9 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅9 小时前
Python函数入门详解(定义+调用+参数)
python
曲幽10 小时前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama
两万五千个小时14 小时前
落地实现 Anthropic Multi-Agent Research System
人工智能·python·架构
哈里谢顿16 小时前
Python 高并发服务限流终极方案:从原理到生产落地(2026 实战指南)
python
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python