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()
相关推荐
二十雨辰1 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码1 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
前端摸鱼匠2 小时前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生
人工智能·pytorch·python·yolo·目标检测
WangYaolove13142 小时前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
AALoveTouch2 小时前
大麦网协议分析
javascript·python
ZH15455891313 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh3 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh3 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
YongCheng_Liang3 小时前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕3 小时前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann