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()
相关推荐
SuperEugene2 分钟前
Python 异步 async/await:为什么 AI 框架大量使用?| 基础篇
开发语言·人工智能·python
SMF19198 分钟前
【uv】Python包管理器uv安装和应用
开发语言·python·uv
gergul8 分钟前
在llama-cpp-python中使用自己编译的llama.cpp,解决pip install llama-cpp-python报错
python·llama·llama.cpp·llamacpppython
深蓝轨迹8 分钟前
#Python零基础机器学习入门教程
人工智能·python·机器学习
蓝色的杯子10 分钟前
Python面试30分钟突击掌握-LeetCode1-Array
开发语言·python·面试
怪祝浙12 分钟前
超简洁YOLO8n快速上手人员检测
python
财经资讯数据_灵砚智能18 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月10日
人工智能·python·信息可视化·自然语言处理·ai编程
啦啦啦_999924 分钟前
0. Python进阶概要
python
weixin_4080996730 分钟前
OCR 识别率提升实战:模糊 / 倾斜 / 反光图片全套优化方案(附 Python / Java / PHP 代码)
图像处理·人工智能·后端·python·ocr·api·抠图
翻斗包菜32 分钟前
Python 网络编程从入门到精通:TCP/UDP/Socket 实战详解
网络·python·tcp/ip