VTK之交互方式(interactor style)

    VTK提供了多种交互方式,最常用的包括joystick和trackball,并且可以分别对actor和camera进行操作。

    在VTK中,可以通过集成已有的交互方式类,然后添加自己的功能,则可以完成在触发响应时,完成响应的操作。

    以下是自定义交互方式类的一个实例。该实例实现的是对鼠标点击的物体进行高亮显示。

   

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
// Handle mouse events
class MouseInteractorHighLightActor : public vtkInteractorStyleTrackballCamera
{
public:
  static MouseInteractorHighLightActor* New();
  vtkTypeMacro(MouseInteractorHighLightActor, vtkInteractorStyleTrackballCamera);
 
  MouseInteractorHighLightActor()
  {
    LastPickedActor = NULL;
    LastPickedProperty = vtkProperty::New();
  }
  virtual ~MouseInteractorHighLightActor()
  {
    LastPickedProperty->Delete();
  }
  virtual void OnLeftButtonDown()
  {
    int* clickPos = this->GetInteractor()->GetEventPosition();
 
    // Pick from this location.
    vtkSmartPointer  picker =
      vtkSmartPointer::New();
    picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());
 
    // If we picked something before, reset its property
    if (this->LastPickedActor)
      {
      this->LastPickedActor->GetProperty()->DeepCopy(this->LastPickedProperty);
      }
    this->LastPickedActor = picker->GetActor();
    if (this->LastPickedActor)
      {
      // Save the property of the picked actor so that we can
      // restore it next time
      this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
      // Highlight the picked actor by changing its properties
      this->LastPickedActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
      this->LastPickedActor->GetProperty()->SetDiffuse(1.0);
      this->LastPickedActor->GetProperty()->SetSpecular(0.0);
      }
 
    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
  }
 
private:
  vtkActor    *LastPickedActor;
  vtkProperty *LastPickedProperty;
};
 
vtkStandardNewMacro(MouseInteractorHighLightActor);
 
// Execute application.
int main(int argc, char *argv[])
{
  int numberOfSpheres = 10;
  if (argc > 1)
    {
    numberOfSpheres = atoi(argv[1]);
    }
  // A renderer and render window
  vtkSmartPointer renderer =
    vtkSmartPointer::New();
  vtkSmartPointer renderWindow =
    vtkSmartPointer::New();
  renderWindow->AddRenderer ( renderer );
 
  // An interactor
  vtkSmartPointer renderWindowInteractor =
    vtkSmartPointer::New();
  renderWindowInteractor->SetRenderWindow ( renderWindow );
 
  // Set the custom type to use for interaction.
  vtkSmartPointer style =
    vtkSmartPointer::New();
  style->SetDefaultRenderer(renderer);
 
  renderWindowInteractor->SetInteractorStyle( style );
 
  for (int i = 0; i < numberOfSpheres; ++i)
    {
    vtkSmartPointer source =
      vtkSmartPointer::New();
    double x, y, z, radius;
    x = vtkMath::Random(-5,5);
    y = vtkMath::Random(-5,5);
    z = vtkMath::Random(-5,5);
    radius = vtkMath::Random(.5, 1.0);
    source->SetRadius(radius);
    source->SetCenter(x, y, z);
    source->SetPhiResolution(11);
    source->SetThetaResolution(21);
    vtkSmartPointer mapper =
      vtkSmartPointer::New();
    mapper->SetInputConnection ( source->GetOutputPort());
    vtkSmartPointer actor =
      vtkSmartPointer::New();
    actor->SetMapper ( mapper );
    double r, g, b;
    r = vtkMath::Random(.4, 1.0);
    g = vtkMath::Random(.4, 1.0);
    b = vtkMath::Random(.4, 1.0);
    actor->GetProperty()->SetDiffuseColor(r, g, b);
    actor->GetProperty()->SetDiffuse(.8);
    actor->GetProperty()->SetSpecular(.5);
    actor->GetProperty()->SetSpecularColor(1.0,1.0,1.0);
    actor->GetProperty()->SetSpecularPower(30.0);
    renderer->AddActor ( actor );
    }
 
  renderer->SetBackground ( .3, .4, .5 );
 
  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

    在VTK的示例中,对常用的交互方式,以及常用的自定义交互方式都进行了实现,各个示例的源码地址如下:

   

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/MoveAGlyph

http://www.itk.org/Wiki/VTK/Examples/Cxx/Interaction/Game

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/TrackballActor

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/SelectAnActor

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Picking/HighlightPickedActor

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/Picking

http://stackoverflow.com/questions/27628493/vtk-interacting-with-multiple-objects

http://www.vtk.org/Wiki/VTK/Examples/Cxx/Interaction/StyleSwitch


你可能感兴趣的:(计算机图形学)