vtk 点信息标注 控件 BillboardTextActor3D BalloonWidget 悬浮显示

 悬浮显示 控件

BillboardTextActor3D,可以用于3D信息标注,简单实用,效果与官方demo(vtkBillboardTextActor3D)基本一致;

1. 减少了遮盖;

2.增加 悬浮显示 控件

源码如下:

#!/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.vtkInteractionWidgets import vtkBalloonRepresentation, vtkBalloonWidget
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkBillboardTextActor3D,
    vtkPolyDataMapper,
    vtkRenderWindow,
    vtkRenderWindowInteractor,
    vtkRenderer
)


def main():
    colors = vtkNamedColors()

    # For testing
    rng = vtkMinimalStandardRandomSequence()
    # rng.SetSeed(8775070)
    rng.SetSeed(5127)

    # Create a renderer
    renderer = vtkRenderer()
    renderer.SetBackground(colors.GetColor3d('DarkSlateGray'))

    # Create a render window
    render_window = vtkRenderWindow()
    render_window.AddRenderer(renderer)
    render_window.SetWindowName('BillboardTextActor3D')

    # Create an interactor
    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(render_window)

    # Create a sphere
    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
    balloonWidgetArry = []
    for i in range(0, 10):
        if i == 0:
            # Create an actor representing the origin
            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)

        # Create a mapper
        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(sphere_source.GetOutputPort())

        # Create an actor
        actor = vtkActor()
        actor.SetMapper(mapper)
        actor.SetPosition(0, 0, 0)
        actor.GetProperty().SetColor(colors.GetColor3d('MistyRose'))
        actor.GetProperty().SetOpacity(0.5)

        # Set up the text and add it to the renderer
        text_actor = vtkBillboardTextActor3D()
        text_actor.SetInput('')
        text_actor.SetPosition(actor.GetPosition())
        text_actor.GetTextProperty().SetFontSize(22)
        text_actor.GetTextProperty().SetColor(colors.GetColor3d('Gold'))
        text_actor.GetTextProperty().SetJustificationToCentered()

        position = random_position(min_r, max_r, rng)
        actor.SetPosition(position)
        # If you want to use a callback, do this:
        #observer = ActorCallback(text_actor)
        #actor.AddObserver(vtkCommand.Pick3DEvent, observer(actor))
        # Otherwise do this:
        #label = f'{position[0]:0.3g}, {position[1]:0.3g}, {position[2]:0.3g}'
        label = ' Store :'+str(i)
        text_actor.SetPosition(position)
        text_actor.SetInput(label)

        ##########
        balloonRep = vtkBalloonRepresentation()
        balloonRep.SetBalloonLayoutToImageRight()
        balloonWidget = vtkBalloonWidget()
        balloonWidget.SetInteractor(iren);
        balloonWidget.SetRepresentation(balloonRep);
        balloonWidget.AddBalloon(actor, "This is a sphere");
        balloonWidget.On()
        balloonWidgetArry.append(balloonWidget)
        #balloonWidget.AddBalloon(regularPolygonActor, "This is a regular polygon",


        renderer.AddActor(actor)
        renderer.AddActor(text_actor)
        #renderer.AddActor(balloonWidget)

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


class ActorCallback:
    def __init__(self, text_actor):
        self.text_actor = text_actor

    def __call__(self, caller):
        position = caller.GetPosition()
        label = f'{position[0]:0.3g}, {position[1]:0.3g}, {position[2]:0.3g}'
        self.text_actor.SetPosition(position)
        self.text_actor.SetInput(label)


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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值