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()
相关推荐
热心不起来的市民小周1 分钟前
测测你的牌:基于 MobileNetV2 的车牌内容检测
python·深度学习·计算机视觉
BinaryBoss4 分钟前
Python 从Maxcompute导出海量数据到文本文件(txt)或Excel
chrome·python·odps
落羽凉笙5 分钟前
Python基础(4)| 详解程序选择结构:单分支、双分支与多分支逻辑(附代码)
android·服务器·python
数据光子10 分钟前
【YOLO数据集】国内交通信号检测
人工智能·python·安全·yolo·目标检测·目标跟踪
百***787516 分钟前
2026 优化版 GPT-5.2 国内稳定调用指南:API 中转实操与成本优化
开发语言·人工智能·python
Amelia11111130 分钟前
day48
python
小北方城市网32 分钟前
第 6 课:云原生架构终极落地|K8s 全栈编排与高可用架构设计实战
大数据·人工智能·python·云原生·架构·kubernetes·geo
智航GIS36 分钟前
10.1 网站防爬与伪装策略
python
belldeep43 分钟前
python:pyTorch 入门教程
pytorch·python·ai·torch
YJlio1 小时前
Registry Usage (RU) 学习笔记(15.5):注册表内存占用体检与 Hive 体量分析
服务器·windows·笔记·python·学习·tcp/ip·django