LoginSignup
3
5

More than 5 years have passed since last update.

VTKによるマウスイベント処理(オブジェクト上の点選択)

Posted at

VTKによるマウスイベント処理(オブジェクト上の点選択)

VTKではオブジェクト描画ウィンドウ上のマウスイベント時の処理を簡単に行う仕組みが用意されている.ここでは,描画ウィンドウ上に表示したオブジェクト上の点座標をクリックで取得する方法を公式サンプルに基づいて説明する.

手順は以下の通り.
1. スタイルクラスを作成する.
2. スタイルをセットしてオブジェクトを描画する.

1. スタイルクラスを作成する.

スタイルクラスを作成して,このクラスでイベント時の処理を記述する.直接オブジェクト上の点を取得するのは,vtkPointPickerを使う.
以下に右クリックを押したときのオブジェクト上の点の座標を取得するスタイルクラスのサンプルを示す.メンバ変数のvtkPointsに取得した点を格納する.(公式サンプルとほぼ同じです...)

class MouseInteractorStylePP : public vtkInteractorStyleTrackballCamera
{
  protected:
    vtkSmartPointer<vtkPoints> PickPoints;//取得した点を格納.
  public:
    static MouseInteractorStylePP* New();
    vtkTypeMacro(MouseInteractorStylePP, vtkInteractorStyleTrackballCamera);
    vtkSmartPointer<vtkPoints> GetPickPoints(){return this->PickPoints;}

    //各関数をオーバーロードする.
    virtual void OnRightButtonDown()//右クリックした時の処理
    {
      this->Interactor->GetPicker()->Pick(
       this->Interactor->GetEventPosition()[0], 
       this->Interactor->GetEventPosition()[1], 
       0,
       this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer()
       );
      //オブジェクト上の点を取得.
      double picked[3];
      this->Interactor->GetPicker()->GetPickPosition(picked);
      this->PickPoints->InsertNextPoint(picked);//点をメンバ変数に格納.
     //コンソールに表示.
     std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl;

      vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
    }
};

2. スタイルをセットしてオブジェクトを描画する.

通常の描画処理と異なるのは2つ.
* vtkPointPickerを作成して,vtkRenderWindowInteractorにセットする.
* スタイルを作成して,vtkRenderWindowInteractorにセットする.


auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
sphereSource->Update();

auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

auto renderer = vtkSmartPointer<vtkRenderer>::New();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);

auto renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();

//pointpickerを作成して,セット.
auto pointPicker = vtkSmartPointer<vtkPointPicker>::New();
renderWindowInteractor->SetPicker(pointPicker);
renderWindowInteractor->SetRenderWindow(renderWindow);

//作成したスタイルをInteractorにセットする.
auto style = vtkSmartPointer<MouseInteractorStylePP>::New();
renderWindowInteractor->SetInteractorStyle( style );

renderer->AddActor(actor);
renderer->SetBackground(1,1,1);
renderWindow->Render();
renderWindowInteractor->Start();

//取得した点
vtkSmartPointer<vtkPoints> pickPoints = style->GetPickPoints();
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5