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圖像,以
vtkLODActor
類創建,作爲3D對象; - 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();
}
最終結果展示如下: