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()
相关推荐
七夜zippoe5 分钟前
Java技术未来展望:GraalVM、Quarkus、Helidon等新趋势探讨
java·开发语言·python·quarkus·graaivm·helidon
m0_7381207214 分钟前
网络安全编程——Python编写基于UDP的主机发现工具(解码IP header)
python·网络协议·tcp/ip·安全·web安全·udp
北冥有羽Victoria19 分钟前
OpenCLI 操作网页 从0到1完整实操指南
vscode·爬虫·python·github·api·ai编程·opencli
handsomestWei21 分钟前
scikit-learn数据预处理模块
python·机器学习·scikit-learn
w_t_y_y25 分钟前
机器学习常用的python包(二)工具箱scikit-learn
python·机器学习·scikit-learn
用户83562907805135 分钟前
Python 自动拆分 Word 文档教程:按分节符与分页符处理
后端·python
陈天伟教授38 分钟前
心电心音同步分析-案例:原型设计一
开发语言·人工智能·python·语言模型·架构
我的xiaodoujiao38 分钟前
API 接口自动化测试详细图文教程学习系列9--Requests模块
python·学习·测试工具·pytest
Allen_LVyingbo41 分钟前
量子计算Dirac Notation基本教学—从零基础到读懂量子信息论文(下)
开发语言·人工智能·python·数学建模·量子计算
Dxy12393102161 小时前
Python路径算法简介
开发语言·python·算法