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()
相关推荐
IT北辰6 分钟前
用 Python 自动解析药品规格并计算包装总容量 —— pandas + 正则实战
开发语言·python·pandas
python机器学习ML34 分钟前
论文复现-以动物图像分类为例进行多模型性能对比分析
人工智能·python·神经网络·机器学习·计算机视觉·scikit-learn·sklearn
沃斯堡&蓝鸟38 分钟前
DAY30 函数专题1:函数定义与参数
python
小oo呆44 分钟前
【学习心得】Python的TypedDict(简介)
开发语言·python
文洪涛1 小时前
VS Code Python “第一次运行失败 / 先执行 python 再激活 Conda” 问题定位与解决
开发语言·python·conda
zd2005721 小时前
STREAMS指南:环境及宿主相关微生物组研究中的技术报告标准
人工智能·python·算法
Data_agent1 小时前
京东商品价格历史信息API使用指南
java·大数据·前端·数据库·python
云技纵横1 小时前
Stream API 从入门到实践:常用操作、易错点与性能建议
开发语言·windows·python
雪域迷影1 小时前
macOS系统上或首次使用Python的urllib模块时出现 ssl.SSLCertVerificationError 错误
python·macos·ssl