BillboardTextActor3D 3D字体随镜头旋转

一:主要的知识点

1、说明

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

2、知识点纪要

本段代码主要涉及的有①vtk中几种txt的对比,②vtkBillboardTextActor3D的简介

二:代码及注释

python 复制代码
#!/usr/bin/env python3

# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingFreeType
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
# noinspection PyUnresolvedReferences
from vtkmodules.vtkCommonCore import vtkCommand
from vtkmodules.vtkCommonCore import (
    vtkMinimalStandardRandomSequence
)
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkBillboardTextActor3D,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def main():
    colors = vtkNamedColors()

    rng = vtkMinimalStandardRandomSequence()
    rng.SetSeed(5127)

    renderer = vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))

    render_window = vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetWindowName('BillboardTextActor3D')

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(render_window)

    sphere_source = vtkSphereSource()
    sphere_source.SetCenter(0.0, 0.0, 0.0)
    sphere_source.SetRadius(1.0)

    min_r = -10.0
    max_r = 10.0

    for i in range(0, 10):
        if i == 0:
            mapper = vtkPolyDataMapper()
            mapper.SetInputConnection(sphere_source.GetOutputPort())

            actor = vtkActor()
            actor.SetMapper(mapper)
            actor.SetPosition(0, 0, 0)
            actor.GetProperty().SetColor(colors.GetColor3d('Peacock'))

            renderer.AddActor(actor)

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(sphere_source.GetOutputPort())


        actor = vtkActor()
        actor.SetMapper(mapper)
        actor.SetPosition(0, 0, 0)
        actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))

        """
        vtkBillboardTextActor3D  是一种 3D 文字显示类,能够在三维空间中的任意坐标位置显示文字,并自动面向相机
        """
        text_actor = vtkBillboardTextActor3D()
        text_actor.SetPosition(actor.GetPosition())
        text_actor.GetTextProperty().SetFontSize(12)
        text_actor.GetTextProperty().SetColor(colors.GetColor3d('Gold'))
        text_actor.GetTextProperty().SetJustificationToCentered()

        """
        几种text的比对
         类名                               | 类型       | 位置坐标系          | 是否随相机旋转   | 是否受光照影响        | 常见用途 
         vtkTextActor            | 2D 屏幕文字  | 屏幕坐标(Viewport) |  固定在屏幕上  |  否            | UI文字、标题、图例    
         vtkVectorText           | 3D 几何体文字 | 世界坐标(World)    | 会随相机旋转  | 是(真正的 3D 模型) | 立体文字、雕刻文字     
         vtkBillboardTextActor3D | 3D 文字贴图  | 世界坐标(World)    | 自动面向相机 | 否            | 3D 场景标注、标签、说明 

        """

        position = random_position(min_r, max_r, rng)
        actor.SetPosition(position)
        label = f'{position[0]:0.3g}, {position[1]:0.3g}, {position[2]:0.3g}'
        text_actor.SetPosition(position)
        text_actor.SetInput(label)

        renderer.AddActor(actor)
        renderer.AddActor(text_actor)

    render_window.Render()
    render_window.SetWindowName('BillboardTextActor3D')
    iren.Start()





def random_position(min_r, max_r, rng):
    p = list()
    for i in range(0, 3):
        p.append(rng.GetRangeValue(min_r, max_r))
        rng.Next()
    return p


if __name__ == '__main__':
    main()
相关推荐
tkevinjd19 小时前
MiniCode 项目详解6:原项目控制系统的10个缺陷(已修复)
python·llm·agent
dNGUZ7UGj20 小时前
10分钟完成第一个Python小游戏
python·django
没有梦想的咸鱼185-1037-166320 小时前
AI-Python机器学习与深度学习技术:CNN/Transformer/扩散模型、SHAP可解释及Hermes智能体自动化
人工智能·python·深度学习·机器学习·chatgpt·cnn·transformer
霸道流氓气质21 小时前
SpringBoot中通用工具类库(Utils)封装与使用实践
spring boot·后端·python
Draina1 天前
CBC填充预言攻击-CBC Padding Oracle Crypto Attack
python·安全·web安全·网络安全·密码学·安全性测试
Angel Q.1 天前
特征提取 | DINO 到 DINOv3
图像处理·python
不如语冰1 天前
AI大模型入门-pytorch-张量2 tensor创建
python
NPE~1 天前
[AI]Agent开发——ADK框架使用
人工智能·python·ai·教程·adk·agent开发
矮个史蒂芬1 天前
统计verilog .v文件中output 的bit数
python
gwf2161 天前
磨损均衡算法(Wear Leveling)——SSD如何让每块闪存“公平退休“?
运维·数据库·人工智能·python·嵌入式硬件·算法·智能硬件