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();
}
最終結果展示如下: