1 #include <vtkRenderWindow.h>
2 #include <vtkRenderWindowInteractor.h>
3 #include <vtkRenderer.h>
4 #include <vtkSphereSource.h>
5 #include <vtkPolyDataMapper.h>
7 #include <vtkSmartPointer.h>
8 #include <vtkPointPicker.h>
10 #include <vtkInteractorStyleTrackballCamera.h>
11 #include <vtkObjectFactory.h>
18 // Define interaction style
19 class MouseInteractorStyleDoubleClick : public vtkInteractorStyleTrackballCamera
23 static MouseInteractorStyleDoubleClick* New();
24 vtkTypeMacro(MouseInteractorStyleDoubleClick, vtkInteractorStyleTrackballCamera);
26 MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5), DoubleClickTolerance(1000)
28 this->PreviousPosition[0] = 0;
29 this->PreviousPosition[1] = 0;
31 this->DoubleClickTolerance = GetDoubleClickTime();
33 // I dont have any idea if this has an equivalent for linux
34 //this->DoubleClickTolerance = GetDoubleClickTime();
38 virtual void OnLeftButtonDown()
40 //std::cout << "Pressed left mouse button." << std::endl;
41 this->NumberOfClicks++;
43 if (this->NumberOfClicks > 1)
45 this->LastClickTime = this->CurrentClickTime;
46 this->CurrentClickTime = time(0);
49 this->CurrentClickTime = time(0);
52 //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
54 this->GetInteractor()->GetEventPosition(pickPosition);
56 int xdist = pickPosition[0] - this->PreviousPosition[0];
57 int ydist = pickPosition[1] - this->PreviousPosition[1];
59 this->PreviousPosition[0] = pickPosition[0];
60 this->PreviousPosition[1] = pickPosition[1];
62 int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));
64 // Reset numClicks - If mouse moved further than resetPixelDistance
65 if (moveDistance > this->ResetPixelDistance)
67 this->NumberOfClicks = 1;
71 if (this->NumberOfClicks == 2)
73 double seconds = difftime(this->CurrentClickTime, this->LastClickTime);
74 if (seconds * 1000 <= this->DoubleClickTolerance)
76 std::cout << "Double clicked. with " << seconds << " of diference" << std::endl;
77 this->NumberOfClicks = 0;
80 std::cout << "Too slow to be considered as a double click" << std::endl;
81 this->NumberOfClicks = 1;
86 vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
90 unsigned int NumberOfClicks;
91 int PreviousPosition[2];
92 int ResetPixelDistance;
93 double DoubleClickTolerance; // time in miliseconds
95 time_t CurrentClickTime;
97 vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
99 int main(int, char *[])
101 vtkSmartPointer<vtkSphereSource> sphereSource =
102 vtkSmartPointer<vtkSphereSource>::New();
103 sphereSource->SetCenter(0.0, 0.0, 0.0);
104 sphereSource->SetRadius(5.0);
105 sphereSource->Update();
107 vtkSmartPointer<vtkPolyDataMapper> mapper =
108 vtkSmartPointer<vtkPolyDataMapper>::New();
109 mapper->SetInputConnection(sphereSource->GetOutputPort());
111 vtkSmartPointer<vtkActor> actor =
112 vtkSmartPointer<vtkActor>::New();
113 actor->SetMapper(mapper);
115 vtkSmartPointer<vtkRenderer> renderer =
116 vtkSmartPointer<vtkRenderer>::New();
117 renderer->SetBackground(1, 1, 1); // Background color white
118 renderer->AddActor(actor);
120 vtkSmartPointer<vtkRenderWindow> renderWindow =
121 vtkSmartPointer<vtkRenderWindow>::New();
122 renderWindow->AddRenderer(renderer);
124 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
125 vtkSmartPointer<vtkRenderWindowInteractor>::New();
126 renderWindowInteractor->SetRenderWindow(renderWindow);
128 vtkSmartPointer<MouseInteractorStyleDoubleClick> style =
129 vtkSmartPointer<MouseInteractorStyleDoubleClick>::New();
130 renderWindowInteractor->SetInteractorStyle(style);
132 renderWindowInteractor->Initialize();
133 renderWindowInteractor->Start();