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()
相关推荐
码云骑士16 分钟前
09-Python模块导入机制-sys.path与循环导入的死锁式排查
开发语言·python
天佑木枫32 分钟前
第5天:循环 —— 让程序重复执行
python
聆风吟º33 分钟前
【Python编程日志】Python基础数据类型完整梳理
开发语言·python·数据类型
盼小辉丶35 分钟前
OpenCV-Python实战(28)——OpenCV计算摄影从HDR图像融合到全景拼接
python·opencv·计算机视觉
shchojj1 小时前
ChatGPT Prompt Engineering for Developers - Expanding
开发语言·python·prompt
俊俊谢1 小时前
【python】FastAPI 实时推送:从 SSE 到 WebSocket
python·websocket·fastapi
stephon_1002 小时前
Agent 接入 MCP 后上下文爆炸、工具选串?一种“按需激活“的工具加载方案(含实现)
人工智能·python·ai
TickDB2 小时前
统一行情 API 查 A 股、港股、美股和数字货币:code=0 不代表 symbol 一个没少
人工智能·python·websocket·mcp·行情数据 api
大貔貅喝啤酒9 小时前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf10 小时前
LangChain 调用 LLM
后端·python·agent