3D图像处理时,为了让用户理解某个部件的用法,通常会在部件的旁边标注相应的文本,作为注释来;文本注释对视觉理解方面是不可或缺的, 在 VTK 中提供了两种文本注释方法:
- 1,2D 文本注释,这种方式的特点:文本是贴在图像渲染窗口上,视觉效果文本在3D渲染图象的前面,呈现遮挡状态;
- 2,3D文本注释,文本作为 3D Polydata 数据类型创建, 可作为3D集合对象展示,可旋转、可缩放;
2D 文本注释
对于第一种,主要用到 2D actors(vtkActor2D 和它的子类 例如 vtkScaledTextActor) 、Mappers(vtkMapper 2D 和它的子类 vtkTextMapper),2DTextActor 和 Mapper 与 3D文本注释相似,两者最大区别,一个是文本以2D文本贴到窗口上,位置固定不可动;一种是文本以3D对象创建,可旋转、可缩放(绘制窗口设置了交互效果);
下面这个例子中,创建了两个对象:
- 1,Sphere 3D图像,以
类创建,作为3D对象;vtkLODActor
- 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(90, 50);//Position
textActor->SetInput("VTK Text Diaplayed!");
textActor->GetActualPosition2Coordinate()->SetCoordinateSystemToNormalizedViewport();
textActor->GetPosition2Coordinate()->SetValue(0.6, 0.1);
textActor->GetTextProperty()->SetFontSize(18);
textActor->GetTextProperty()->SetFontFamilyToArial();
textActor->GetTextProperty()->SetJustificationToCentered();
textActor->GetTextProperty()->BoldOn();
textActor->GetTextProperty()->ItalicOn();
textActor->GetTextProperty()->ShadowOn();
textActor->GetTextProperty()->SetColor(0, 0, 1);
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.2, 0.2, 0.2);
textActor->AddPosition(0, 0.1, 0);
textActor->GetProperty()->SetColor(0, 0, 1);
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();
}
最终结果展示如下: