vtk 监听鼠标双击功能

C++

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
 
#include 
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2)
 
 
namespace {
// Define interaction style
class MouseInteractorStyleDoubleClick
    : public vtkInteractorStyleTrackballCamera {
 public:
  static MouseInteractorStyleDoubleClick* New();
  vtkTypeMacro(MouseInteractorStyleDoubleClick,
               vtkInteractorStyleTrackballCamera);
 
  MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5) {
    this->PreviousPosition[0] = 0;
    this->PreviousPosition[1] = 0;
  }
 
  virtual void OnLeftButtonDown() override {
    this->NumberOfClicks++;
    int pickPosition[2];
    this->GetInteractor()->GetEventPosition(pickPosition);
 
    int xdist = pickPosition[0] - this->PreviousPosition[0];
    int ydist = pickPosition[1] - this->PreviousPosition[1];
 
    this->PreviousPosition[0] = pickPosition[0];
    this->PreviousPosition[1] = pickPosition[1];
 
    int moveDistance = (int)sqrt((double)(xdist * xdist + ydist * ydist));
 
    // Reset numClicks - If mouse moved further than resetPixelDistance
    if (moveDistance > this->ResetPixelDistance) {
      this->NumberOfClicks = 1;
    }
 
    if (this->NumberOfClicks == 2) {
      std::cout << "Double clicked." << std::endl;
      this->NumberOfClicks = 0;
    }
 
    // forward events
    vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
  }
 
 private:
  unsigned int NumberOfClicks;
  int PreviousPosition[2];
  int ResetPixelDistance;
};
vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
}  // namespace
 
int main(int, char* []) {
  vtkNew colors;
 
  vtkNew sphereSource;
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(5.0);
  sphereSource->Update();
 
  vtkNew mapper;
  mapper->SetInputConnection(sphereSource->GetOutputPort());
 
  vtkNew actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("Silver").GetData());
 
  vtkNew renderer;
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  renderer->AddActor(actor);
 
  vtkNew renderWindow;
  renderWindow->AddRenderer(renderer);
 
  vtkNew renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);
 
  vtkNew style;
  renderWindowInteractor->SetInteractorStyle(style);
 
  renderWindow->SetWindowName("DoubleClick");
  renderWindow->Render();
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
 
  return EXIT_SUCCESS;
}

Python

# 监听事件
class MyEvent(vtk.vtkInteractorStyleTrackballCamera):

    def __init__(self, parent=None):
        self.AddObserver("MiddleButtonPressEvent", self.middle_button_press_event)
        self.AddObserver("MiddleButtonReleaseEvent", self.middle_button_release_event)
        self.AddObserver("LeftButtonPressEvent", self.left_button_press_event)
        self.AddObserver("LeftButtonReleaseEvent", self.left_button_release_event)
        self.AddObserver("RightButtonPressEvent", self.right_button_press_event)
        self.AddObserver("RightButtonReleaseEvent", self.right_button_release_event)

    def middle_button_press_event(self, obj, event):
        print("Middle Button pressed")
        self.OnMiddleButtonDown()
        return

    def middle_button_release_event(self, obj, event):
        print("Middle Button released")
        self.OnMiddleButtonUp()
        return

    def left_button_press_event(self, obj, event):
        print("Left Button pressed")
        self.OnLeftButtonDown()
        return

   def left_button_release_event(self, obj, event):
        print("Left Button released")
        self.NumberOfClicks+=1
        pickPosition=[0.0,0.0]
        pickPosition = self.GetInteractor().GetEventPosition()
        xdist = pickPosition[0] - self.PreviousPosition[0]
        ydist = pickPosition[1] - self.PreviousPosition[1]

        self.PreviousPosition[0] = pickPosition[0]
        self.PreviousPosition[1] = pickPosition[1]

        moveDistance = math.sqrt((xdist * xdist + ydist * ydist))
        ## Reset numClicks - If mouse moved further than resetPixelDistance
        if (moveDistance > self.ResetPixelDistance):
            self.NumberOfClicks = 1

        if (self.NumberOfClicks == 2) :
            print("Double clicked.")
            self.NumberOfClicks = 0

        self.OnLeftButtonUp()
        return

    def right_button_press_event(self, obj, event):
        print("right Button pressed")
        self.OnRightButtonDown()
        return

    def right_button_release_event(self, obj, event):
        print("right Button released")
        self.OnLeftButtonUp()
        return

# 引入上一段代码调用
#iren.SetInteractorStyle(MyEvent())

你可能感兴趣的:(Python,c++,Python)