VTK获取鼠标点击的点到actor中最近的点

#include 
#include 
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

// Define interaction style
class MouseInteractorStylePP : public vtkInteractorStyleTrackballCamera
{
public:
  static MouseInteractorStylePP* New();
  vtkTypeMacro(MouseInteractorStylePP, vtkInteractorStyleTrackballCamera);

  virtual void OnLeftButtonDown() override
  {
    std::cout << "Picking pixel: " << this->Interactor->GetEventPosition()[0] << " " << this->Interactor->GetEventPosition()[1] << std::endl;
    this->Interactor->GetPicker()->Pick(this->Interactor->GetEventPosition()[0],
                                        this->Interactor->GetEventPosition()[1],
                                        0,  // always zero.
                                        this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
    double picked[3];
    this->Interactor->GetPicker()->GetPickPosition(picked);
    std::cout << "Picked value: " << picked[0] << " " << picked[1] << " " << picked[2] << std::endl;
    // Forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
  }
};

vtkStandardNewMacro(MouseInteractorStylePP);

int main(int, char *[])
{
  vtkSmartPointer sphereSource =
    vtkSmartPointer::New();
  sphereSource->Update();

  vtkSmartPointer pointPicker =
    vtkSmartPointer::New();

  // Create a mapper and actor
  vtkSmartPointer mapper =
    vtkSmartPointer::New();
  mapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer actor =
    vtkSmartPointer::New();
  actor->SetMapper(mapper);

  // Create a renderer, render window, and interactor
  vtkSmartPointer renderer =
    vtkSmartPointer::New();
  vtkSmartPointer renderWindow =
    vtkSmartPointer::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer renderWindowInteractor =
    vtkSmartPointer::New();
  renderWindowInteractor->SetPicker(pointPicker);
  renderWindowInteractor->SetRenderWindow(renderWindow);

  vtkSmartPointer style =
    vtkSmartPointer::New();
  renderWindowInteractor->SetInteractorStyle( style );

  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(1,1,1); // Background color white

  // Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();
  return EXIT_SUCCESS;
}

 

你可能感兴趣的:(C++,VTK)