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()
相关推荐
拿我格子衫来5 小时前
LaserWeb4:矢量图与位图如何变成 G-code
图像处理·图形渲染
傲笑风5 小时前
【openvino】tinybert基于openvino服务化部署(四)
人工智能·python·自然语言处理·nlp·bert·openvino
维基框架5 小时前
WIKI 知识库 v1.1.1 正式发布
人工智能·python
谢白羽9 小时前
SGLang的AWQ量化笔记
笔记·python·sglang
迷迭香yy10 小时前
基金档案数据工程实战从收入分析到持仓穿透的Python解析 IG50免费开源股票数据API接口
开发语言·python
清水白石00810 小时前
Python 类型设计深度解析:TypedDict 能否替代 dataclass?从 JSON 数据边界到 API 设计的最佳实践
java·python·json
️学习的小王10 小时前
Git项目提交忽略文件怎么做?以Python项目为例,详解.gitignore
git·python·elasticsearch
前端 贾公子11 小时前
第09章:上下文与记忆 (6)
开发语言·前端·python
evans在进步11 小时前
Java 常用设计模式入门:建造者、工厂、单例、外观与代理
java·python·设计模式
jayson.h11 小时前
PDF 合并+添加页码 相关库、类、函数
开发语言·前端·python