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()
相关推荐
方安乐1 天前
python之向量、向量和、向量点积
开发语言·python·numpy
zh1570231 天前
JavaScript中WorkerThreads解决服务端计算瓶颈
jvm·数据库·python
蜡台1 天前
Python包管理工具pip完全指南-----2
linux·windows·python
Mr.朱鹏1 天前
【Python 进阶 | 第四篇】Psycopg3 + Flask 实现 PostgreSQL CRUD 全流程:从连接池到RESTful接口
python·postgresql·flask·virtualenv·fastapi·pip·tornado
2401_871492851 天前
Vue.js监听器watch利用回调函数处理级联下拉框数据联动
jvm·数据库·python
FreakStudio1 天前
亲测可用!可本地部署的 MicroPython 开源仿真器
python·单片机·嵌入式·面向对象·并行计算·电子diy·电子计算机
SilentSamsara1 天前
Python 环境搭建完整指南:从下载安装到运行第一个程序
开发语言·python
zhoutongsheng1 天前
C#怎么实现Swagger文档 C#如何在ASP.NET Core中集成Swagger自动生成API文档【框架】
jvm·数据库·python
.5481 天前
## Sorting(排序算法)
python·算法·排序算法
ydmy1 天前
注意力机制(个人理解)
pytorch·python·深度学习