VTK交互之拾取

选择拾取

  选择拾取是人机交互过程的一个重要功能。VTK中定义了多个拾取功能的类,所有拾取类都继承自vtkAbstractPicker类,继承关系如下:

VTK交互之拾取_第1张图片

1. vtkAbstractPicker

  该类是一个抽象基类,为它的具体的子类定义了最基本的API,最基本的功能是返回拾取位置的x-y-z 世界坐标系统的坐标值(pick自身是在屏幕坐标中进行定义的) 。
  类定义了一个纯虚函数Pick(),它的作用是给定一个选择点(屏幕坐标系统里的,以像素为单位)以及一个Renderer,然后通过调用GetPickPosition()方法,生成世界坐标系统的坐标值。
Pick()函数的原型如下:

virtual int Pick (double selectionX, double selectionY, double selectionZ, vtkRenderer *renderer)

   通常都把selectionZ设为0,与Renderer相关联的actor就是拾取的对象范围。在拾取过程中vtkPicker会触发一些事件:1、StartPickEvent, 2、PickEvent, 3、EndPickEvent。当something被拾取的时候,并且在所有的picking申请者已经通过测试,1/2/3事件就会在picking之前调用。注意:在pick的过程中,vtkProp的PickEvent事件会在vtkPicker的PickEvent之前被调用。 
   vtkAbstractPicker以及它的子类都不能拾取”拒绝拾取”的Prop(也就是该Prop通过调用自身的PickableOff()方法)和完全透明的Prop(即prop->GetProperty()->SetOpacity(0))。
  Picker可以分为两类,分别是用几何方法拾取(典型的例子是光线投射)和基于硬件拾取。几何方法拾取能获取的信息较多同时也较慢,而基于硬件的拾取拾取速度快,但返回的信息最少。几何方法拾取主要的类有:vtkPicker, vtkCellPicker和vtkPointPicker。基于硬件拾取的类有:vtkWorldPointPicker和vtkPropPicker。vtkAbstractPicker有两个直接子类:vtkWorldPointPicker和vtkAbstractPropPicker。

2. vtkWorldPointPicker

  vtkWorldPointPicker是基于硬件的拾取,它返回与屏幕坐标(x, y, z)相对应的世界坐标点。这个类不能拾取Actor和/或Mapper,它只是简单地确定世界坐标上的坐标值。这个类不调用PickMethod()事件,只能调用StartPickEvent()和EndPickMethod()两个事件。

3. vtkPropPicker

   vtkPropPicker是基于图形硬件拾取一个Actor/Prop实例,速度比vtkCellPicker/vtkPointPicker快。这个类在世界坐标系统中判定Actor/Prop和拾取位置,对于点和单元的ID则不作判定。

4. vtkPicker

  vtkPicker用于拾取vtkProp3D实例,它通过发射一条光线到图形窗口中,并与Actor的线框(Bounding box)相交,光线是从窗口坐标系统中拾取的一点到相机所在的位置的连线。因为光线可能会与多个Actor的线框相交,所以可能会返回多个vtkProp3D的实例。vtkPicker返回的是与光线相交的Prop实例的一个列表、世界坐标系统中的拾取坐标以及最靠近相机的Prop和Mapper实例。所谓的最靠近相机,是指该Prop线框中心点在光线上的投影最靠近相机的那个Prop实例。vtkPicker速度相对较快,可用于快速拾取几何对象。但如果想返回拾取的点或者单元的话,得用它的子类vtkCellPicker和vtkPointPicker vtkPicker里其中有一个方法:virtual void SetTolerance(double)—-是设置容差。

5. vtkPointPicker

  vtkPointPicker是vtkPicker的子类,它返回选择点的ID以及坐标。它也是通过发射光线与Actor相交而拾取对象的,除了返回坐标值,Actor和Mapper,它也返回在指定容差内沿着光线,最靠近光线的那个点的ID。vtkPointPicker::GetPointId()可以获取点的ID号。
  vtkPointPicker不能单一拾取,其速度比vtkPicker慢,但比vtkCellPicker要快。

6. vtkCellPicker

  vtkCellPicker是vtkPicker的子类,它返回选择单元的ID以及坐标。它也是通过发射光线与Actor相交而拾取对象的,除了返回坐标值,Actor和Mapper,它也返回在指定容差内沿着光线,最靠近光线的那个单元的ID。vtkPointPicker::GetCellId()可以获取单元的ID号。vtkCellPicker可以单一拾取,其速度是所有Picker中最慢的,当然,提供的信息也是最丰富的。

示例演示

CMakeLists.txt文件代码如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(PropPickerExample)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(PropPickerExample    PropPickerExample.cpp)
TARGET_LINK_LIBRARIES(PropPickerExample  ${VTK_LIBRARIES})

PropPickerExample.cpp文件代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

// Handle mouse events
class PropPickerInteractorStyle : public vtkInteractorStyleTrackballCamera
{
public:
    static PropPickerInteractorStyle* New();
    vtkTypeMacro(PropPickerInteractorStyle, vtkInteractorStyleTrackballCamera);

    PropPickerInteractorStyle()
    {
        LastPickedActor = NULL;
        LastPickedProperty = vtkProperty::New();
    }
    virtual ~PropPickerInteractorStyle()
    {
        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());

        double* pos = picker->GetPickPosition();
        // 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(PropPickerInteractorStyle);

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->Render();
    renderWindow->SetWindowName("PropPicker");
    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 ( 1.0, 1.0, 1.0 );

    // Render and interact
    renderWindow->Render();
    renderWindowInteractor->Initialize();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

运行结果:

VTK交互之拾取_第2张图片

你可能感兴趣的:(Study-VTK,vtk,拾取,交互)