首页 > 其他分享 >VTK_Learning_交互部件_标注类Widget的应用

VTK_Learning_交互部件_标注类Widget的应用

时间:2022-12-30 13:02:23浏览次数:56  
标签:Widget interactor VTK vtkSmartPointer Learning New include 标注


1.标注类Widget

在可视化程序中,经常会对某个对象做一些标注说明,比如,在医学图像诊断中,常常会手动标注出被诊断为肿瘤的区域或者其他病变区域,并用文字进行标注。

VTK中,与标注相关的Widget如下表所示:

VTK_Learning_交互部件_标注类Widget的应用_数据

  •  vtkTextWidget:在渲染场景中生成一串标识文本,可以随意调整该文本在渲染场景中的位置,缩放其大小等。
  • vtkScalarBarWidget:根据输入的数据在渲染场景中生成一个标量条,通过设置颜色查找表,可以用标量条上的颜色来指示输入的数据。渲染场景中的标量条可以随意移动、改变大小、设置不同的方向等。
  • vtkCaptionWidget:用一个带线框及箭头的文本信息来标注某一对象。
  • vtkOrientationMarkerWidget:渲染场景中所渲染数据的方向指示标志。在医学图像领域有广泛的应用,比如,通过CT/MR等扫描的数据,当将其导入可视化应用程序时需要标识其上、下、左、右、前、后等方位。
  • vtkBalloonWidget:当鼠标停留在渲染场景中的某个Actor一段时间后,会弹出提示信息。所提示的信息,除了可以用文本表示,也可以用图像表示。
     

 2.标注类Widget应用程序

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)

#include <vtkSmartPointer.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkLookupTable.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

#include <vtkScalarBarActor.h>
#include <vtkScalarBarWidget.h>
#include <vtkTextActor.h>
#include <vtkTextWidget.h>
#include <vtkTextProperty.h>
#include <vtkTextRepresentation.h>
#include <vtkAxesActor.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkCaptionWidget.h>
#include <vtkCaptionActor2D.h>///
#include <vtkCaptionRepresentation.h>
#include <vtkBalloonWidget.h>
#include <vtkBalloonRepresentation.h>

int main()
{
vtkSmartPointer< vtkUnstructuredGridReader > reader = vtkSmartPointer< vtkUnstructuredGridReader >::New();
reader->SetFileName("data.vtk");
reader->Update();

vtkSmartPointer< vtkLookupTable > lut = vtkSmartPointer< vtkLookupTable >::New();
lut->Build();

vtkSmartPointer< vtkDataSetMapper > mapper = vtkSmartPointer< vtkDataSetMapper >::New();
mapper->SetInputData(reader->GetOutput());
mapper->SetScalarRange(reader->GetOutput()->GetScalarRange());
mapper->SetLookupTable(lut);

vtkSmartPointer< vtkActor > actor = vtkSmartPointer< vtkActor >::New();
actor->SetMapper(mapper);

vtkSmartPointer< vtkRenderer > renderer = vtkSmartPointer< vtkRenderer >::New();
renderer->AddActor(actor);
renderer->SetBackground(1, 1, 1);

vtkSmartPointer< vtkRenderWindow > renderWindow = vtkSmartPointer< vtkRenderWindow >::New();
renderWindow->AddRenderer(renderer);
renderWindow->Render();
renderWindow->SetWindowName("AnnotationWidget");
renderWindow->SetSize(400, 400);

vtkSmartPointer< vtkRenderWindowInteractor > interactor =
vtkSmartPointer< vtkRenderWindowInteractor >::New();
interactor->SetRenderWindow(renderWindow);
/********************************************************************************************/
//标注类测试
vtkScalarBarWidget
vtkSmartPointer< vtkScalarBarActor > scalarBarActor = vtkSmartPointer< vtkScalarBarActor >::New();
scalarBarActor->SetOrientationToHorizontal();
scalarBarActor->SetLookupTable(lut);

vtkSmartPointer< vtkScalarBarWidget > scalarBarWidget = vtkSmartPointer< vtkScalarBarWidget >::New();
scalarBarWidget->SetInteractor(interactor);
scalarBarWidget->SetScalarBarActor(scalarBarActor);
scalarBarWidget->On();

vtkTextWidget
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput("VTK Widgets");
textActor->GetTextProperty()->SetColor(1, 0, 0);

vtkSmartPointer<vtkTextWidget> textWidget = vtkSmartPointer<vtkTextWidget>::New();
textWidget->SetInteractor(interactor);
textWidget->SetTextActor(textActor);

vtkSmartPointer<vtkTextRepresentation> textRepresentation =
vtkSmartPointer<vtkTextRepresentation>::New();
textRepresentation->GetPositionCoordinate()->SetValue(0.15, 0.15);
textRepresentation->GetPosition2Coordinate()->SetValue(0.7, 0.2);

textWidget->SetRepresentation(textRepresentation);
textWidget->SelectableOff();
textWidget->On();

/ vtkOrientationMarkerWidget
vtkSmartPointer<vtkAxesActor> iconActor = vtkSmartPointer<vtkAxesActor>::New();
vtkSmartPointer<vtkOrientationMarkerWidget> orientationWidget =
vtkSmartPointer<vtkOrientationMarkerWidget>::New();
orientationWidget->SetOutlineColor(0.9300, 0.5700, 0.1300);
orientationWidget->SetInteractor(interactor);
orientationWidget->SetOrientationMarker(iconActor);
orientationWidget->SetViewport(0.0, 0.0, 0.2, 0.2);
orientationWidget->SetEnabled(1);
orientationWidget->InteractiveOn();

vtkCaptionWidget
//vtkSmartPointer<vtkCaptionWidget> captionWidget =
// vtkSmartPointer<vtkCaptionWidget>::New();
//captionWidget->SetInteractor(interactor);

//vtkSmartPointer<vtkCaptionRepresentation> captionRepresentation =
// vtkSmartPointer<vtkCaptionRepresentation>::New();
//captionRepresentation->GetCaptionActor2D()->SetCaption("Caption Widget");
//captionRepresentation->GetCaptionActor2D()->GetTextActor()->GetTextProperty()->SetFontSize(20);
//
//double pos[3] = { .5, 0, 0 };
//captionRepresentation->SetAnchorPosition(pos);
//captionWidget->SetRepresentation(captionRepresentation);
//captionWidget->On();

/ vtkBalloonWidget
vtkSmartPointer<vtkBalloonWidget> balloonWidget =
vtkSmartPointer<vtkBalloonWidget>::New();
balloonWidget->SetInteractor(interactor);

vtkSmartPointer<vtkBalloonRepresentation> balloonRep =
vtkSmartPointer<vtkBalloonRepresentation>::New();
balloonRep->SetBalloonLayoutToImageRight();

balloonWidget->SetRepresentation(balloonRep);
balloonWidget->AddBalloon(actor, "This is a widget example", NULL);
balloonWidget->On();

renderWindow->Render();
interactor->Initialize();
interactor->Start();
return 0;
}

输出结果如下:

VTK_Learning_交互部件_标注类Widget的应用_标注类Widget_02

 使用标注类Widget需要注意的是,除了指定的Widget表达实体之外,某些Widget还需要与其他Actor协同使用;如上例中的vtkScalarBarWidget要与vtkScalarBarActor协同工作;vtkTextWidget要与vtkTextActor协同工作。

 

标签:Widget,interactor,VTK,vtkSmartPointer,Learning,New,include,标注
From: https://blog.51cto.com/u_15926338/5980091

相关文章