vtk基础(三) — 文本注释 vtkTextActor 和 vtkFollower 的使用

3D图像处理时,为了让用户理解某个部件的用法,一般会在部件的旁边标注相应的文本,做为注释来;文本注释对视觉理解方面是不可或缺的。
web

在 VTK 中提供了两种文本注释方法:微信

  • 2D 文本注释,这种方式的特色:文本是贴在图像渲染窗口上,视觉效果文本在3D渲染图像的前面,呈现遮挡状态;app

  • 3D文本注释,文本做为 3D Polydata 数据类型建立, 可做为3D集合对象展现,可旋转、可缩放;字体

2D 文本注释

对于第一种,主要用到 2D Actors(vtkActor2D 和它的子类 例如 vtkScaledTextActor) 、Mappers(vtkMapper 2D 和它的子类 vtkTextMapper )spa

2DTextActor 和 Mapper 与 3D文本注释类似,二者最大区别:.net

一种是文本以2D文本贴到窗口上,位置固定不可动;一种是文本以3D对象建立,可旋转、可缩放(绘制窗口设置了交互效果);3d

下面这个例子中展现的就是 2D Text 文本注释,例子中建立了两个对象:code

  • 1,Sphere 3D 图像,以 vtkLODActor 类建立,做为 3D 对象;orm

  • 2,Text 文本,以 vtkTextActor 类建立,用于做为注释文本;对象

绘制时,把两个 Actor  所有加入 Renderer (绘制器)中;这里文本属性用到 vtkTextProperty 类,用于设置文本走向(垂直、水平)、颜色、坐标及文字类型(大小、字体)等;

#include<vtkSphereSource.h>
#include<vtkLODActor.h>
#include<vtkTextActor.h>
#include<vtkPolyDataMapper.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkNamedColors.h>


#include<vtkAutoInit.h>
#include<vtkTextProperty.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);


int main()
{
    vtkSmartPointer<vtkSphereSource> sphere =
        vtkSmartPointer<vtkSphereSource>::New();

    vtkSmartPointer<vtkPolyDataMapper> polymapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();

    vtkSmartPointer<vtkLODActor> sphereActor =
        vtkSmartPointer<vtkLODActor>::New();
    vtkSmartPointer<vtkTextActor> textActor =
        vtkSmartPointer<vtkTextActor>::New();

    vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

    polymapper->SetInputConnection(sphere->GetOutputPort());
    polymapper->GlobalImmediateModeRenderingOn();//ImmadianteMode on;
    sphereActor->SetMapper(polymapper);

    textActor->SetTextScaleModeToProp();
    textActor->SetDisplayPosition(9050);//Position
    textActor->SetInput("VTK Text Diaplayed!");
    textActor->GetActualPosition2Coordinate()->SetCoordinateSystemToNormalizedViewport();
    textActor->GetPosition2Coordinate()->SetValue(0.60.1);


    textActor->GetTextProperty()->SetFontSize(18);
    textActor->GetTextProperty()->SetFontFamilyToArial();
    textActor->GetTextProperty()->SetJustificationToCentered();
    textActor->GetTextProperty()->BoldOn();
    textActor->GetTextProperty()->ItalicOn();
    textActor->GetTextProperty()->ShadowOn();
    textActor->GetTextProperty()->SetColor(001);



    vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();

    ren->SetBackground(colors->GetColor3d("SlateGray").GetData());

    ren->AddViewProp(textActor);
    ren->AddActor(sphereActor);

    renWin->AddRenderer(ren);
    iren->SetRenderWindow(renWin);

    try
    {
        renWin->Render();
        iren->Start();

    }
    catch (std::exception & e)
    {
        std::cout << "Exceptation caught!" << std::endl;
        std::cout << e.what() << std::endl;
        return EXIT_FAILURE;
    }


    return EXIT_SUCCESS;


}

展现效果以下:


3D 文本注释 vtkFollower

3D文本注释利用 vtkVectorText 来建立一个 文本字符串的 polygonal 表示方式,而后将其放置在场景的合适位置;

定位 3D Text有用的类为  vtkFollower  ,此类是常常面向 Renderer 的 active camera  表示的是 Actor  的类,借此确保文本可读性;

下面的代码部分就是关于 3D文本注释的效果实现,建立 一个数轴,结合 vtkVectorText 与   vtkFollower 对数轴原点进行标注;

#include<vtkRenderer.h>
#include<vtkPolyDataMapper.h>
#include<vtkActor.h>
#include<vtkRenderWindow.h>
#include<vtkPolyDataMapper.h>
#include<vtkFollower.h>
#include<vtkVectorText.h>
#include<vtkAxes.h>
#include<vtkProperty.h>
#include<vtkNamedColors.h>

#include<vtkRenderWindowInteractor.h>
#include<vtkAutoInit.h>

int main()
{
    vtkSmartPointer<vtkAxes> axes = vtkSmartPointer<vtkAxes>::New();

    vtkSmartPointer<vtkPolyDataMapper> polymapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();



    polymapper->SetInputConnection(axes->GetOutputPort());// axes object;

    vtkSmartPointer<vtkActor> axesActor = vtkSmartPointer<vtkActor>::New();
    axesActor->SetMapper(polymapper);

    vtkSmartPointer<vtkVectorText> atext = vtkSmartPointer<vtkVectorText>::New();
    atext->SetText("Origin");


    vtkSmartPointer<vtkPolyDataMapper> textMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    textMapper->SetInputConnection(atext->GetOutputPort());

    vtkSmartPointer<vtkFollower> textActor =// vtkVectorActor vtkFollwer;
        vtkSmartPointer<vtkFollower>::New();
    textActor->SetMapper(textMapper);


    textActor->SetScale(0.20.20.2);
    textActor->AddPosition(00.10);
    textActor->GetProperty()->SetColor(001);


    vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
    vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();


    ren->AddViewProp(textActor);//Add Actor;
    ren->AddActor(axesActor);

    ren->SetBackground(colors->GetColor3d("StateGray").GetData());//back ground color;

    textActor->SetCamera(ren->GetActiveCamera());

    renWin->AddRenderer(ren);
    iren->SetRenderWindow(renWin);

    renWin->Render();
    iren->Start();

}

最终结果展现以下:



推荐阅读

VTK 基础(一)  —  经常使用控件介绍及实现圆锥体绘制

VTK 基础(二)  —  单窗口实现多对象绘制!

分别用 VTK 体绘制和面绘制来实现医学图像三维重建;

本文分享自微信公众号 - Z先生点记(gh_683d048a482a)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索