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()
相关推荐
woxihuan1234561 分钟前
golang如何读写YAML配置文件_golang YAML配置文件读写解析
jvm·数据库·python
彳亍1011 分钟前
mysql如何实现数据库按月分表_利用分区表优化查询性能
jvm·数据库·python
Captain_Data1 分钟前
Python机器学习实战:用Scikit-learn从0构建信用风险评分模型(含WOE编码+AUC/KS/PSI评估+评分卡转换)
python·机器学习·数据分析·scikit-learn·风控建模
m0_463672202 分钟前
Golang怎么获取当前工作目录_Golang如何用os.Getwd获取程序运行路径【基础】
jvm·数据库·python
号码认证服务3 分钟前
企业固话号码认证能覆盖哪些手机品牌?支持华为、小米、OPPO、vivo等机型
服务器·网络·经验分享·python·华为·智能手机·云计算
2401_884454154 分钟前
mysql如何处理大量重复值索引_mysql索引存储特征分析
jvm·数据库·python
kexnjdcncnxjs14 分钟前
SQL批量删除不同条件的记录_使用IN子句简化删除逻辑
jvm·数据库·python
2303_8212873816 分钟前
如何安装Oracle 12c Cloud Control_OMS服务端组件与Agent部署
jvm·数据库·python
Be reborn16 分钟前
用例不是孤立执行的:依赖、变量池与 storage_state 设计
python·自动化·pytest
m0_6091604918 分钟前
React Flow 边缘错位与消失问题的根源分析与 Hooks 重构方案
jvm·数据库·python