zoukankan      html  css  js  c++  java
  • vtkBoxWidget2Example

    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)

     1 #ifndef INITIAL_OPENGL
     2 #define INITIAL_OPENGL
     3 #include <vtkAutoInit.h>
     4 VTK_MODULE_INIT(vtkRenderingOpenGL)
     5 VTK_MODULE_INIT(vtkInteractionStyle)
     6 #endif
     7 #include <iostream>
     8 using namespace std;
     9 #include <vtkSmartPointer.h>
    10 // For the rendering pipeline setup:
    11 #include <vtkConeSource.h>
    12 #include <vtkPolyDataMapper.h>
    13 #include <vtkActor.h>
    14 #include <vtkRenderer.h>
    15 #include <vtkRenderWindow.h>
    16 #include <vtkRenderWindowInteractor.h>
    17 #include <vtkInteractorStyleTrackballCamera.h>
    18 // For vtkBoxWidget2:
    19 #include <vtkBoxWidget2.h>
    20 #include <vtkBoxRepresentation.h>
    21 #include <vtkCommand.h>
    22 #include <vtkTransform.h>
    23 
    24 class vtkBoxCallback:public vtkCommand
    25 {
    26 public:
    27     static vtkBoxCallback*New(){return new vtkBoxCallback;}
    28     void SetActor(vtkSmartPointer<vtkActor> actor)
    29     {
    30         m_actor=actor;
    31     }
    32     virtual void Execute(vtkObject*caller,unsigned long, void*)
    33     {
    34         //将调用该回调函数的调用者caller指针,转换为vtkBoxWidget2类型对象指针
    35         vtkSmartPointer<vtkBoxWidget2> boxWidget=vtkBoxWidget2::SafeDownCast(caller);
    36 // vtkSmartPointer<vtkBoxWidget2> boxWidget=reinterpret_cast<vtkBoxWidget2>(caller);这样转换不可以,vtkBoxWidget可以
    37         vtkSmartPointer<vtkTransform> t=vtkSmartPointer<vtkTransform>::New();
    38         //将boxWidget中的变换矩阵保存在t中
    39         vtkBoxRepresentation::SafeDownCast(boxWidget->GetRepresentation())->GetTransform(t);
    40         this->m_actor->SetUserTransform(t);
    41     }
    42 protected:
    43     vtkBoxCallback(){} //构造函数
    44 public:
    45     vtkSmartPointer<vtkActor> m_actor;
    46 };
    47 
    48 int main()
    49 {
    50     vtkSmartPointer<vtkConeSource> coneSource=vtkSmartPointer<vtkConeSource>::New();
    51     coneSource->SetHeight(1.5);
    52 
    53     vtkSmartPointer<vtkPolyDataMapper> mapper=vtkSmartPointer<vtkPolyDataMapper>::New();
    54     mapper->SetInputConnection(coneSource->GetOutputPort());
    55 
    56     vtkSmartPointer<vtkActor> actor=vtkSmartPointer<vtkActor>::New();
    57     actor->SetMapper(mapper);
    58     vtkSmartPointer<vtkRenderer> renderer =
    59         vtkSmartPointer<vtkRenderer>::New();
    60       renderer->AddActor(actor);
    61       renderer->ResetCamera(); // Reposition camera so the whole scene is visible
    62 
    63       vtkSmartPointer<vtkRenderWindow> renderWindow =
    64         vtkSmartPointer<vtkRenderWindow>::New();
    65       renderWindow->AddRenderer( renderer );
    66 
    67       vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    68         vtkSmartPointer<vtkRenderWindowInteractor>::New();
    69       renderWindowInteractor->SetRenderWindow( renderWindow );
    70 
    71       // Use the "trackball camera" interactor style, rather than the default "joystick camera"
    72       vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
    73         vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    74       renderWindowInteractor->SetInteractorStyle( style );
    75 /**********************************设置vtkBoxWidget2及回调函数***************************************************************/
    76       vtkSmartPointer<vtkBoxWidget2> boxWidget=vtkSmartPointer<vtkBoxWidget2>::New();
    77       boxWidget->SetInteractor(renderWindowInteractor);
    78       boxWidget->GetRepresentation()->SetPlaceFactor(1.1);//默认为0.5
    79       boxWidget->GetRepresentation()->PlaceWidget(actor->GetBounds());
    80 
    81       //为interactor配置回调函数,这样我们就能控制actor
    82       vtkSmartPointer<vtkBoxCallback> boxCallback=vtkSmartPointer<vtkBoxCallback>::New();
    83       boxCallback->SetActor(actor);
    84       boxWidget->AddObserver(vtkCommand::EndInteractionEvent,boxCallback);
    85       boxWidget->On();
    86       renderWindowInteractor->Start();
    87     return 0;
    88 }
  • 相关阅读:
    shell的正则表达式
    shell语法
    shell通配符
    shell小命令
    DNS
    CCNA参考链接
    Network problem solving flow chart
    我是一个路由器
    我是一个网卡
    Chrome
  • 原文地址:https://www.cnblogs.com/phoenixdsg/p/6241903.html
Copyright © 2011-2022 走看看