TessellatedBoxSource 创建并渲染一个细分的、可移动的箱体模型

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①箱体模型的细分级别

二:代码及注释

python 复制代码
#!/usr/bin/env python
 
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonExecutionModel import vtkAlgorithm
from vtkmodules.vtkFiltersGeneral import vtkShrinkFilter
from vtkmodules.vtkFiltersSources import vtkTessellatedBoxSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkDataSetMapper,
    vtkProperty,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)
 
 
def main():
    colors = vtkNamedColors()
 
    bounds = [-10.0, 10.0, 10.0, 20.0, -5.0, 5.0]
 
    boxSource = vtkTessellatedBoxSource()
    """
    设置箱体的细分级别。Level=3意味着箱体的每个维度(x, y, z)将被划分为 2的3次方=8,
    总共创建 8×8×8=512 个小立方体
    """
    boxSource.SetLevel(3)
    boxSource.QuadsOn()  # 使用四边形(而不是三角形)来构建箱体的面
    boxSource.SetBounds(bounds)
    """
    SetOutputPointsPrecision
    设置输出点坐标的精度
    """
    boxSource.SetOutputPointsPrecision(vtkAlgorithm.SINGLE_PRECISION)
 
 
    shrink = vtkShrinkFilter()
    shrink.SetInputConnection(boxSource.GetOutputPort())
    shrink.SetShrinkFactor(.8)
 
    # Create a mapper and actor.
    mapper = vtkDataSetMapper()
    mapper.SetInputConnection(shrink.GetOutputPort())
 
    back = vtkProperty()
    back.SetColor(colors.GetColor3d('Tomato'))
 
    actor = vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().EdgeVisibilityOn()
    actor.GetProperty().SetColor(colors.GetColor3d('Banana'))
    actor.SetBackfaceProperty(back)
 
    # Create a renderer, render window, and interactor.
    renderer = vtkRenderer()
    renderWindow = vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindowInteractor = vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)
 
    # Add the actors to the scene.
    renderer.AddActor(actor)
    renderer.SetBackground(colors.GetColor3d('Silver'))
 
    renderer.ResetCamera()
    renderer.GetActiveCamera().Azimuth(30)
    renderer.GetActiveCamera().Elevation(30)
    renderer.ResetCameraClippingRange()
 
    # Render and interact.
    renderWindow.SetSize(640, 480)
    renderWindow.SetWindowName('TessellatedBoxSource')
    renderWindow.Render()
    renderWindowInteractor.Start()
 
 
if __name__ == '__main__':
    main()
相关推荐
qq_417695054 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水5 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
yy我不解释5 小时前
关于comfyui的mmaudio音频生成插件时时间不一致问题(一)
python·ai作画·音视频·comfyui
紫丁香6 小时前
AutoGen详解一
后端·python·flask
FreakStudio7 小时前
不用费劲编译ulab了!纯Mpy矩阵micronumpy库,单片机直接跑
python·嵌入式·边缘计算·电子diy
清水白石0089 小时前
Free-Threaded Python 实战指南:机遇、风险与 PoC 验证方案
java·python·算法
飞Link9 小时前
具身智能核心架构之 Python 行为树 (py_trees) 深度剖析与实战
开发语言·人工智能·python·架构
桃气媛媛9 小时前
Pycharm常用快捷键
python·pycharm
Looooking10 小时前
Python 之获取安装包所占用磁盘空间大小
python
WenGyyyL10 小时前
ColBERT论文研读——NLP(IR)里程碑之作
人工智能·python·语言模型·自然语言处理