vtkOrientationMarkerWidget
vtkOrientationMarkerWidget用于操纵标记道具的2D小部件;
此类支持以交互方式操纵表示方向标记的道具的位置、大小和外观方向。此类的工作方式是将其内部渲染器添加到不同层上的外部“父”渲染器。输入方向标记在父渲染器上渲染为覆盖,因此显示为覆盖父场景中的所有道具。通过观察者机制使标记方向的摄影机视图与父对象的方向相匹配,从而产生一种错觉,即标记方向反映父对象场景中道具的方向。
小部件监听鼠标左键和鼠标移动事件。它将根据光标的位置更改光标形状。如果光标位于覆盖渲染器上,它会将光标形状更改为SIZEALL形状,如果光标靠近某个角点,则会将光标形状更改为resize角点形状(例如SIZENW)。如果在移动时按住鼠标左键,则会调整或移动覆盖渲染器的大小,从而移动方向标记。在调整大小操作的情况下,释放鼠标左键会使小部件强制其呈现为正方形。将重新定位与所移动边对角的角,以便渲染器的所有边具有相同的长度:最小值。
要使用此对象,有两个关键步骤:1)使用vtkRenderWindowInteractior方法的参数调用SetInteractior(),2)使用vtkProp实例调用SetOrientationMarker(请参见下面的注意事项)。具体来说,vtkAxesActor和vtkAnnotatedCubeActor是两个被设计用于和vtkOrientationMarkerWidget 类一同工作的类。通过将vtkAxesActor和vtkAnnotatedCubeActor的实例添加到vtkPropAssembly中,可以生成复合方向标记,然后将其设置为输入方向标记。也可以通过非交互方式设置小部件,方法是将Ineractive设置为Off,并通过调用小部件的SetViewport方法来调整覆盖渲染器的大小/将其放置在其父渲染器中。
注意:输入方向标记属性应该计算其边界,就好像它们是关于其原点对称的一样。当前必须这样做才能正确实现ivar渲染器和与SetInteractior关联的渲染器之间的相机同步。重要的是,与interactor关联的InteractorStyle必须是vtkInteractorStyle*Camera类型。如果需要,父渲染器应该由SetDefaultRenderer方法设置。如果需要,父渲染器的层数将修改为2。
接口
设置要在此小部件中显示的方向标记。
virtual void SetOrientationMarker(vtkProp* prop);
vtkGetObjectMacro(OrientationMarker, vtkProp);
设置启用
void SetEnabled(int) override;
代码1
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSuperquadricSource.h>
#include <vtkXMLPolyDataReader.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
using namespace std;
int main(){
vtkNew<vtkNamedColors> colors;
// Read the polydata for the icon
vtkNew<vtkXMLPolyDataReader> reader;
reader->SetFileName("E:\\Bunny.vtp");
vtkNew<vtkDataSetMapper> iconMapper;
iconMapper->SetInputConnection(reader->GetOutputPort());
vtkNew<vtkActor> iconActor;
iconActor->SetMapper(iconMapper);
iconActor->GetProperty()->SetColor(colors->GetColor3d("Silver").GetData());
// Set up the renderer, window, and interactor
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer);
renWin->SetSize(400, 400);
renWin->SetWindowName("OrientationMarkerWidget1");
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
double r;
double g;
double b;
colors->GetColor("Wheat", r, g, b);
// Set up the widget
vtkNew<vtkOrientationMarkerWidget> widget;
widget->SetOrientationMarker(iconActor);
widget->SetInteractor(iren);
widget->SetViewport(0.0, 0.0, 0.2, 0.2);
widget->SetOutlineColor(r, g, b);
widget->SetEnabled(1);
widget->InteractiveOn();
// Create a superquadric
vtkNew<vtkSuperquadricSource> superquadricSource;
superquadricSource->SetPhiRoundness(0.2);
superquadricSource->SetThetaRoundness(0.8);
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> superquadricMapper;
superquadricMapper->SetInputConnection(superquadricSource->GetOutputPort());
vtkNew<vtkActor> superquadricActor;
superquadricActor->SetMapper(superquadricMapper);
superquadricActor->GetProperty()->SetInterpolationToFlat();
superquadricActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Carrot").GetData());
superquadricActor->GetProperty()->SetSpecularColor(colors->GetColor3d("White").GetData());
superquadricActor->GetProperty()->SetDiffuse(.6);
superquadricActor->GetProperty()->SetSpecular(.5);
superquadricActor->GetProperty()->SetSpecularPower(5.0);
renderer->AddActor(superquadricActor);
renderer->ResetCamera();
renWin->Render();
iren->Initialize();
iren->Start();
system("pause");
return 0;
}
vtkAnnotatedCubeActor
vtkAnnotatedCubeActor是一种混合3D Actor,用于表示场景中的解剖方向标记。该类由一个以原点为中心的三维单位立方体组成,每个面的标签对应于特定的坐标方向。例如,对于笛卡尔方向,用户定义的文本标签可以是:+X、-X、+Y、-Y、+Z、-Z,而对于解剖方向:A、P、L、R、S、I。文本自动居中于每个立方体面上,并且不限制为单个字符。除纯文本标签表示法外,还可以显示标签的轮廓边缘,或者替换纯文本标签表示法。立方体、面标签和文本轮廓的各个特性以及它们的可见性都可以进行操作。
代码2
#include <vtkActor.h>
#include <vtkAnnotatedCubeActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
using namespace std;
int main(){
vtkNew<vtkNamedColors> colors;
// create a rendering window and renderer;
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren);
renWin->SetWindowName("OrientationMarkerWidget");
// create a renderwindowinteractor;
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
vtkNew<vtkCubeSource> cube;
cube->SetXLength(200);
cube->SetYLength(200);
cube->SetZLength(200);
cube->Update();
vtkNew<vtkPolyDataMapper> cm;
cm->SetInputConnection(cube->GetOutputPort());
vtkNew<vtkActor> ca;
ca->SetMapper(cm);
ca->GetProperty()->SetColor(colors->GetColor3d("BurlyWood").GetData());
ca->GetProperty()->EdgeVisibilityOn();
ca->GetProperty()->SetEdgeColor(colors->GetColor3d("Red").GetData());
// assign actor to the renderer;
ren->AddActor(ca);
ren->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());
vtkNew<vtkAnnotatedCubeActor> axesActor;
axesActor->SetXPlusFaceText("L");
axesActor->SetXMinusFaceText("R");
axesActor->SetYMinusFaceText("I");
axesActor->SetYPlusFaceText("S");
axesActor->SetZMinusFaceText("P");
axesActor->SetZPlusFaceText("A");
axesActor->GetTextEdgesProperty()->SetColor(colors->GetColor3d("Yellow").GetData());
axesActor->GetTextEdgesProperty()->SetLineWidth(2);
axesActor->GetCubeProperty()->SetColor(colors->GetColor3d("Blue").GetData());
vtkNew<vtkOrientationMarkerWidget> axes;
axes->SetOrientationMarker(axesActor);
axes->SetInteractor(iren);
axes->EnabledOn();
axes->InteractiveOn();
ren->ResetCamera();
iren->Initialize();
renWin->Render();
ren->GetActiveCamera()->Azimuth(45);
ren->GetActiveCamera()->Elevation(30);
renWin->Render();
iren->Start();
system("pause");
return 0;
}