This example uses a vtkBoxWidget2 to manipulate an actor. The widget only contains the interaction logic; the actual box is drawn by the accompanying vtkBoxRepresentation. Contrary to the older vtkBoxWidget, this widget doesn't provide functionality to assign it to one or more actors, so that has to be implemented manually. The box is dimensioned and positioned by passing a bounding box to PlaceWidget method, with the SetPlaceFactormethod providing a scaling factor in relation to that bounding box. The transformations applied to the box can be used to manipulate any number of object(s), via a custom callback class, which is passed to the box widget through the AddObserver method.

The older implementation vtkBoxWidget provides functionality to receive a vtkProp3D for the initial positioning and sizing, but the transformation synchronization still needs to be done manually. See BoxWidget for a simple example of how to use it.

vtkBoxWidget2

3D widget 用于操控 a box

This 3D widget interacts with a vtkBoxRepresentation class (i.e., it handles the events that drive its corresponding representation). The representation is assumed to represent a region of interest that is represented by an arbitrarily oriented hexahedron (or box) with interior face angles of 90 degrees (i.e., orthogonal faces). The representation manifests seven handles that can be moused on and manipulated, plus the six faces can also be interacted with. The first six handles are placed on the six faces, the seventh is in the center of the box. In addition, a bounding box outline is shown, the "faces" of which can be selected for object rotation or scaling. A nice feature of vtkBoxWidget2, like any 3D widget, will work with the current interactor style. That is, if vtkBoxWidget2 does not handle an event, then all other registered observers (including the interactor style) have an opportunity to process the event. Otherwise, the vtkBoxWidget will terminate the processing of the event that it handles.

To use this widget, you generally pair it with a vtkBoxRepresentation (or a subclass). Variuos options are available in the representation for controlling how the widget appears, and how the widget functions.

Event Bindings:
By default, the widget responds to the following VTK events (i.e., it watches the vtkRenderWindowInteractor for these events):

If one of the seven handles are selected:
LeftButtonPressEvent - select the appropriate handle
LeftButtonReleaseEvent - release the currently selected handle
MouseMoveEvent - move the handle
If one of the faces is selected:
LeftButtonPressEvent - select a box face
LeftButtonReleaseEvent - release the box face
MouseMoveEvent - rotate the box
In all the cases, independent of what is picked, the widget responds to the
following VTK events:
MiddleButtonPressEvent - translate the widget
MiddleButtonReleaseEvent - release the widget
RightButtonPressEvent - scale the widget's representation
RightButtonReleaseEvent - stop scaling the widget
MouseMoveEvent - scale (if right button) or move (if middle button) the widget
Event Bindings:
Note that the event bindings described above can be changed using this class's vtkWidgetEventTranslator. This class translates VTK events into the vtkBoxWidget2's widget events:

  vtkWidgetEvent::Select -- some part of the widget has been selected
vtkWidgetEvent::EndSelect -- the selection process has completed
vtkWidgetEvent::Scale -- some part of the widget has been selected
vtkWidgetEvent::EndScale -- the selection process has completed
vtkWidgetEvent::Translate -- some part of the widget has been selected
vtkWidgetEvent::EndTranslate -- the selection process has completed
vtkWidgetEvent::Move -- a request for motion has been invoked
Event Bindings:
In turn, when these widget events are processed, the vtkBoxWidget2 invokes the following VTK events on itself (which observers can listen for):

  vtkCommand::StartInteractionEvent (on vtkWidgetEvent::Select)
vtkCommand::EndInteractionEvent (on vtkWidgetEvent::EndSelect)
vtkCommand::InteractionEvent (on vtkWidgetEvent::Move)
Event Bindings:
This class, and the affiliated vtkBoxRepresentation, are second generation VTK widgets. An earlier version of this functionality was defined in the class vtkBoxWidget.
See also
vtkBoxRepresentation vtkBoxWidget
Tests:
vtkBoxWidget2 (Tests)

 #ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)
#endif
#include <iostream>
using namespace std;
#include <vtkSmartPointer.h>
// For the rendering pipeline setup:
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
// For vtkBoxWidget2:
#include <vtkBoxWidget2.h>
#include <vtkBoxRepresentation.h>
#include <vtkCommand.h>
#include <vtkTransform.h> class vtkBoxCallback:public vtkCommand
{
public:
static vtkBoxCallback*New(){return new vtkBoxCallback;}
void SetActor(vtkSmartPointer<vtkActor> actor)
{
m_actor=actor;
}
virtual void Execute(vtkObject*caller,unsigned long, void*)
{
//将调用该回调函数的调用者caller指针,转换为vtkBoxWidget2类型对象指针
vtkSmartPointer<vtkBoxWidget2> boxWidget=vtkBoxWidget2::SafeDownCast(caller);
// vtkSmartPointer<vtkBoxWidget2> boxWidget=reinterpret_cast<vtkBoxWidget2>(caller);这样转换不可以,vtkBoxWidget可以
vtkSmartPointer<vtkTransform> t=vtkSmartPointer<vtkTransform>::New();
//将boxWidget中的变换矩阵保存在t中
vtkBoxRepresentation::SafeDownCast(boxWidget->GetRepresentation())->GetTransform(t);
this->m_actor->SetUserTransform(t);
}
protected:
vtkBoxCallback(){} //构造函数
public:
vtkSmartPointer<vtkActor> m_actor;
}; int main()
{
vtkSmartPointer<vtkConeSource> coneSource=vtkSmartPointer<vtkConeSource>::New();
coneSource->SetHeight(1.5); vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(coneSource->GetOutputPort()); vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->ResetCamera(); // Reposition camera so the whole scene is visible vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer( renderer ); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow( renderWindow ); // Use the "trackball camera" interactor style, rather than the default "joystick camera"
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle( style );
/**********************************设置vtkBoxWidget2及回调函数***************************************************************/
vtkSmartPointer<vtkBoxWidget2> boxWidget=vtkSmartPointer<vtkBoxWidget2>::New();
boxWidget->SetInteractor(renderWindowInteractor);
boxWidget->GetRepresentation()->SetPlaceFactor(1.1);//默认为0.5
boxWidget->GetRepresentation()->PlaceWidget(actor->GetBounds()); //为interactor配置回调函数,这样我们就能控制actor
vtkSmartPointer<vtkBoxCallback> boxCallback=vtkSmartPointer<vtkBoxCallback>::New();
boxCallback->SetActor(actor);
boxWidget->AddObserver(vtkCommand::EndInteractionEvent,boxCallback);
boxWidget->On();
renderWindowInteractor->Start();
return ;
}

vtkBoxWidget2Example的更多相关文章

随机推荐

  1. iOS实现渐变色背景(两种方式实现)

    之前做过类似的功能,现在记录一下,来来来... 效果图: 说明=========================== 方法1: 说明:无返回值 用法:直接调用方法.原理是在view的layer层添加. ...

  2. 原生Ajax

    使用原生Ajax 验证用户名是否被注册 创建出注册信息: <h1>注册信息</h1><input type="text" name="txt ...

  3. Compiler Error Message: CS0016: Could not write to output file 回绝访问

    Compiler Error Message: CS0016: Could not write to output file 'c:\Windows...dll' 拒绝访问 C:\Windows\Te ...

  4. [LeetCode] Number of 1 Bits 位1的个数

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  5. 谈一下如何设计Oracle 分区表

    在谈设计Oracle分区表之间先区分一下分区表和表空间的个概念: 表空间:表空间是一个或多个数据文件的集合,所有数据对象都存放在指定的表空间中,但主要存放表,故称表空间. 分区表:分区致力于解决支持极 ...

  6. 基于C/S架构的3D对战网络游戏C++框架 _【不定期更新通知】

    由于笔者最近有比赛项目要赶,这个基于C/S架构的3D对战网络游戏C++框架也遇到了一点瓶颈需要点时间沉淀,所以近一段时间不能保证每天更新了,会保持不定期更新.同时近期笔者也会多分享一些已经做过学过的C ...

  7. JS组件系列——又一款MVVM组件:Vue(一:30分钟搞定前端增删改查)

    前言:关于Vue框架,好几个月之前就听说过,了解一项新技术之后,总是处于观望状态,一直在犹豫要不要系统学习下.正好最近有点空,就去官网了解了下,看上去还不错的一个组件,就抽空研究了下.最近园子里vue ...

  8. 委托,匿名函数和lambda表达式

    很早之前就接触到了委托,但是一直对他用的不是太多,主要是本人是菜鸟,能写的比较高级的代码确实不多,但是最近在看MSDN微软的类库的时候,发现了微软的类库好多都用到了委托,于是决定好好的研究研究,加深一 ...

  9. 并发包的线程池第一篇--ThreadPoolExecutor执行逻辑

    学习这个很长时间了一直没有去做个总结,现在大致总结一下并发包的线程池. 首先,任何代码都是解决问题的,线程池解决什么问题? 如果我们不用线程池,每次需要跑一个线程的时候自己new一个,会导致几个问题: ...

  10. 【poj2409】 Let it Bead

    http://poj.org/problem?id=2409 (题目链接) 题意 一个n个珠子的项链,每个珠子可以被染成t种颜色.项链可以翻转和旋转,问不同的染色方案数. Solution Pólya ...