ImplicitPolyDataDistance 隐式距离显示

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①vtkImplicitPolyDataDistance计算任意点到几何体表面的符号距离

二:代码及注释

python 复制代码
import numpy as np
import vtkmodules.vtkRenderingOpenGL2
import vtkmodules.vtkInteractionStyle
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkFloatArray, vtkPoints
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkFiltersCore import vtkImplicitPolyDataDistance
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersGeneral import vtkVertexGlyphFilter
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def main():
    colors = vtkNamedColors()

    sphereSource = vtkSphereSource()
    sphereSource.SetCenter(0, 0, 0)
    sphereSource.SetRadius(1.0)
    sphereSource.Update()

    sphereMapper = vtkPolyDataMapper()
    sphereMapper.SetInputConnection(sphereSource.GetOutputPort())
    sphereMapper.ScalarVisibilityOff()

    sphereActor = vtkActor()
    sphereActor.SetMapper(sphereMapper)
    # SetOpacity 设置模型是否透明,1完全不透明,0完全透明
    sphereActor.GetProperty().SetOpacity(0.3)
    sphereActor.GetProperty().SetColor(1, 0, 0)

    """
    vtkImplicitPolyDataDistance 计算空间中任意一点到这个多边形几何体表面的符号距离
    """
    implicitPolyDataDistance = vtkImplicitPolyDataDistance()
    implicitPolyDataDistance.SetInput(sphereSource.GetOutput())  # 设置隐式距离函数

    points = vtkPoints()
    step = 0.1
    for x in np.arange(-2, 2, step):
        for y in np.arange(-2, 2, step):
            for z in np.arange(-2, 2, step):
                points.InsertNextPoint(x, y, z)

    signedDistance = vtkFloatArray()
    signedDistance.SetNumberOfComponents(1)
    signedDistance.SetName("")
    for pointId in range(points.GetNumberOfPoints()):
        p = points.GetPoint(pointId)
        dist_ = implicitPolyDataDistance.EvaluateFunction(p)
        signedDistance.InsertNextValue(dist_)

    polyData = vtkPolyData()
    polyData.SetPoints(points)
    polyData.GetPointData().SetScalars(signedDistance)

    """
    vtkVertexGlyphFilter  把点集转换成可渲染的几何体
    """
    vertexGlyphFilter = vtkVertexGlyphFilter()
    vertexGlyphFilter.SetInputData(polyData)
    vertexGlyphFilter.Update()

    signedDistanceMapper = vtkPolyDataMapper()
    signedDistanceMapper.SetInputConnection(vertexGlyphFilter.GetOutputPort())
    signedDistanceMapper.ScalarVisibilityOn() # 根据标量进行着色

    signedDistanceActor = vtkActor()
    signedDistanceActor.SetMapper(signedDistanceMapper)

    renderer = vtkRenderer()
    renderer.AddViewProp(sphereActor)
    renderer.AddViewProp(signedDistanceActor)
    renderer.SetBackground(colors.GetColor3d('SlateGray'))

    renderWindow = vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindow.SetWindowName('ImplicitPolyDataDistance')

    renWinInteractor = vtkRenderWindowInteractor()
    renWinInteractor.SetRenderWindow(renderWindow)

    renderWindow.Render()
    renWinInteractor.Start()


if __name__ == '__main__':
    main()
相关推荐
m5655bj2 小时前
如何通过 Python 在 Excel 中添加或删除图片
python·excel
qq_356196952 小时前
day42Dataset和Dataloader@浙大疏锦行
python
free-elcmacom2 小时前
机器学习高阶教程<6>推荐系统高阶修炼手册:混排、多任务与在线学习,解锁精准推荐新境界
人工智能·python·学习·算法·机器学习·机器人
西西学代码2 小时前
Flutter---常用打印图标
前端·python·flutter
企微自动化2 小时前
企业微信外部群自动化系统的异常处理机制设计
开发语言·python
山土成旧客2 小时前
【Python学习打卡-Day24】从不可变元组到漫游文件系统:掌握数据结构与OS模块
数据结构·python·学习
技术小甜甜2 小时前
[Python] 使用 Tesseract 实现 OCR 文字识别全流程指南
开发语言·python·ocr·实用工具
idkmn_2 小时前
Daily AI 20251219 (PyTorch基础回顾3)
人工智能·pytorch·python·深度学习·神经网络
Iridescent11212 小时前
Iridescent:Day28
python