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()
相关推荐
weixin_457340211 小时前
旋转OBB数据集标注查看器
图像处理·人工智能·python·yolo·目标检测·数据集·旋转
nvd111 小时前
LLM 对话记忆功能实现深度解析
python
电饭叔1 小时前
Luhn算法初介绍
python
badmonster01 小时前
实时代码库索引:用 CocoIndex 构建智能代码搜索的终极方案
python·rust
晓山清2 小时前
Meeting Summarizer Using Natural Language Processing论文理解
人工智能·python·nlp·摘要生成
zqy02272 小时前
python安装与环境配置
开发语言·python
Wise玩转AI2 小时前
从LLM到Agent:技术迁移的必然趋势
人工智能·python·语言模型·ai智能体
ada7_2 小时前
LeetCode(python)——94.二叉
python·算法·leetcode·链表·职场和发展