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()
相关推荐
CHANG_THE_WORLD15 分钟前
python 批量终止进程exe
开发语言·python
liann11921 分钟前
3.2_红队攻击框架--MITRE ATT&CK‌
python·网络协议·安全·网络安全·系统安全·信息与通信
云天AI实战派31 分钟前
AI 智能体问题排查指南:ChatGPT、API 调用到 Agent 上线失灵的全流程修复手册
大数据·人工智能·python·chatgpt·aigc
我的xiaodoujiao1 小时前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
多思考少编码2 小时前
PAT甲级真题1001 - 1005题详细题解(C++)(个人题解)
c++·python·最短路·pat·算法竞赛
ZhengEnCi2 小时前
M5-markconv自定义CSS样式指南 📝
前端·css·python
ZhengEnCi3 小时前
M4-更新日志v0.1.3-Mermaid图表支持 📝
python
hsjcjh3 小时前
多模态长文本协同:用Gemini 3.1 Pro镜像官网破解复杂办公场景的效率困局(国内实测方案)
python
凯瑟琳.奥古斯特3 小时前
SQLAlchemy核心功能解析
开发语言·python·flask
卷Java3 小时前
GPTQ vs AWQ vs GGUF:模型量化工具横向测评
开发语言·windows·python