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(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();
}
最終結果展現以下:
本文分享自微信公衆號 - Z先生點記(gh_683d048a482a)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。