CapClip 模型的裁剪(平面裁剪与曲线裁剪)

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①vtkClipPolyData对于模型的裁剪

二:代码及注释

python 复制代码
from vtkmodules.vtkIOGeometry import vtkBYUReader
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersCore import vtkClipPolyData, vtkFeatureEdges, vtkStripper,vtkTubeFilter, vtkImplicitPolyDataDistance
from vtkmodules.vtkCommonDataModel import vtkPlane, vtkPolyData
from vtkmodules.vtkRenderingCore import vtkDataSetMapper, vtkActor, vtkRenderer, vtkRenderWindow, \
    vtkRenderWindowInteractor, vtkPolyDataMapper
import math
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonComputationalGeometry import vtkParametricSpline
from vtkmodules.vtkFiltersSources import vtkParametricFunctionSource, vtkSphereSource


def main():
    file_path = "Data/cow.g"

    colors = vtkNamedColors()
    backgroundColor = colors.GetColor3d('steel_blue')
    boundaryColor = colors.GetColor3d('Banana')
    clipColor = colors.GetColor3d('Tomato')

    reader_ = vtkBYUReader()
    reader_.SetFileName(file_path)
    reader_.Update()

    polyData = reader_.GetOutput()

    plane = vtkPlane()
    plane.SetOrigin(polyData.GetCenter())
    plane.SetNormal(1, -1, -1)

    """
    对 vtkPolyData(多边形网格) 进行裁剪(Clip),即根据一个标量场或几何平面,把模型切开,保留某一部分的几何
    标量场的裁剪,比如输入数据中有点属性(如温度、压力、速度大小),根据这些裁剪
    """
    clipper = vtkClipPolyData()
    clipper.SetInputData(polyData)
    clipper.SetClipFunction(plane)
    clipper.SetValue(0)  # 保留平面正侧(法线指向的半空间)
    # clipper.GenerateClippedOutputOn() # 生成被移除的部分
    clipper.Update()

    polyData = clipper.GetOutput()

    clipMapper = vtkDataSetMapper()
    clipMapper.SetInputData(polyData)

    clipActor = vtkActor()
    clipActor.SetMapper(clipMapper)
    clipActor.GetProperty().SetDiffuseColor(clipColor)
    clipActor.GetProperty().SetInterpolationToFlat()
    clipActor.GetProperty().EdgeVisibilityOn()

    boundaryEdges = vtkFeatureEdges()
    boundaryEdges.SetInputData(polyData)
    boundaryEdges.BoundaryEdgesOn()
    boundaryEdges.FeatureEdgesOff()
    boundaryEdges.NonManifoldEdgesOff()
    boundaryEdges.ManifoldEdgesOff()

    boundaryStrips = vtkStripper()
    boundaryStrips.SetInputConnection(boundaryEdges.GetOutputPort())
    boundaryStrips.Update()

    """
    相当于模型的深拷贝
    """
    boundaryPoly = vtkPolyData()
    boundaryPoly.SetPoints(boundaryStrips.GetOutput().GetPoints())
    boundaryPoly.SetPolys(boundaryStrips.GetOutput().GetLines())

    boundaryMapper = vtkPolyDataMapper()
    boundaryMapper.SetInputData(boundaryPoly)

    boundaryActor = vtkActor()
    boundaryActor.SetMapper(boundaryMapper)
    boundaryActor.GetProperty().SetDiffuseColor(boundaryColor)

    renderer = vtkRenderer()
    renderWindow = vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    interactor = vtkRenderWindowInteractor()
    interactor.SetRenderWindow(renderWindow)

    # set background color and size
    renderer.SetBackground(backgroundColor)
    renderWindow.SetSize(640, 480)

    # add our actor to the renderer
    renderer.AddActor(clipActor)
    renderer.AddActor(boundaryActor)

    # Generate an interesting view
    renderer.ResetCamera()
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.GetActiveCamera().Dolly(1.2)
    renderer.ResetCameraClippingRange()

    renderWindow.Render()
    renderWindow.SetWindowName('CapClip')
    renderWindow.Render()

    interactor.Start()

def main1():
    colors = vtkNamedColors()
    # 1. 创建 spline
    cx, cy, cz = 0, 0, 0
    r = 5

    points = vtkPoints()
    for theta in range(0, 390, 30):  # 每隔30度一个点
        rad = math.radians(theta)
        x = cx + r * math.cos(rad)
        y = cy + r * math.sin(rad)
        z = cz
        points.InsertNextPoint(x, y, z)

    """
    vtkParametricSpline 给定一系列离散的 3D 控制点,它能计算出一条平滑、连续的曲线函数(样条)
    """
    spline = vtkParametricSpline()
    spline.SetPoints(points)

    """
    vtkParametricFunctionSource  将一个数学函数转换成一个三维几何模型
    """
    splineSource = vtkParametricFunctionSource()
    splineSource.SetParametricFunction(spline)
    splineSource.Update()

    # 2. 把曲线变成一根"管道"
    tube = vtkTubeFilter()
    tube.SetInputConnection(splineSource.GetOutputPort())
    tube.SetRadius(0.5)
    tube.SetNumberOfSides(200)
    tube.Update()

    # 3. 读取目标模型(例如球)
    sphere = vtkSphereSource()
    sphere.SetThetaResolution(100)
    sphere.SetCenter(0, 0, 0)
    sphere.SetRadius(r)
    sphere.SetPhiResolution(200)
    sphere.SetThetaResolution(200)
    sphere.Update()

    """
    vtkImplicitPolyDataDistance 能把"显式的几何模型(PolyData)"转换成一个可以进行空间距离计算和隐式函数裁剪**的"隐式函数对象"
    """
    implicit = vtkImplicitPolyDataDistance()
    implicit.SetInput(tube.GetOutput())

    # 5. 用隐式函数裁剪模型
    clipper = vtkClipPolyData()
    clipper.SetInputConnection(sphere.GetOutputPort())
    clipper.SetClipFunction(implicit)
    clipper.GenerateClippedOutputOn()

    # 6. 显示结果
    mapper = vtkPolyDataMapper()
    mapper.SetInputConnection(clipper.GetOutputPort())

    actor = vtkActor()
    actor.SetMapper(mapper)

    mapper1 = vtkPolyDataMapper()
    mapper1.SetInputConnection(sphere.GetOutputPort())
    actor1 = vtkActor()
    actor1.SetMapper(mapper1)
    actor1.GetProperty().SetColor(colors.GetColor3d("Yellow"))

    ren = vtkRenderer()
    ren.AddActor(actor)
    # ren.AddActor(actor1)
    ren.SetBackground(0.1, 0.1, 0.1)

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren)
    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.Initialize()
    renWin.Render()
    iren.Start()

if __name__ == '__main__':
    main()

    # 基于模型表面上的点构建曲线进行裁剪
    main1()
相关推荐
金銀銅鐵28 分钟前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li2 小时前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸7 小时前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学8 小时前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田1 天前
Pydantic校验配置文件
python
hboot1 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi2 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽2 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187912 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python