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 #if __cplusplus > 199711L
22 // Define interaction style
23 class MouseInteractorStyleDoubleClick : public vtkInteractorStyleTrackballCamera
27 static MouseInteractorStyleDoubleClick* New();
28 vtkTypeMacro(MouseInteractorStyleDoubleClick, vtkInteractorStyleTrackballCamera);
30 MouseInteractorStyleDoubleClick() : NumberOfClicks(0), ResetPixelDistance(5), DoubleClickTolerance(1000)
32 this->PreviousPosition[0] = 0;
33 this->PreviousPosition[1] = 0;
35 this->DoubleClickTolerance = GetDoubleClickTime();
37 // I dont have any idea if this has an equivalent for linux
38 //this->DoubleClickTolerance = GetDoubleClickTime();
42 virtual void OnLeftButtonDown()
44 //std::cout << "Pressed left mouse button." << std::endl;
45 this->NumberOfClicks++;
47 if (this->NumberOfClicks > 1)
49 this->LastClickTime = this->CurrentClickTime;
50 #if __cplusplus <= 199711L
51 this->CurrentClickTime = time(0) * 1000;
53 this->CurrentClickTime = std::chrono::duration_cast<std::chrono::milliseconds>(
54 std::chrono::system_clock::now().time_since_epoch()
61 #if __cplusplus <= 199711L
62 this->CurrentClickTime = time(0) * 1000;
64 this->CurrentClickTime = std::chrono::duration_cast<std::chrono::milliseconds>(
65 std::chrono::system_clock::now().time_since_epoch()
71 //std::cout << "NumberOfClicks = " << this->NumberOfClicks << std::endl;
73 this->GetInteractor()->GetEventPosition(pickPosition);
75 int xdist = pickPosition[0] - this->PreviousPosition[0];
76 int ydist = pickPosition[1] - this->PreviousPosition[1];
78 this->PreviousPosition[0] = pickPosition[0];
79 this->PreviousPosition[1] = pickPosition[1];
81 int moveDistance = (int)sqrt((double)(xdist*xdist + ydist*ydist));
83 // Reset numClicks - If mouse moved further than resetPixelDistance
84 if (moveDistance > this->ResetPixelDistance)
86 this->NumberOfClicks = 1;
90 if (this->NumberOfClicks == 2)
93 #if __cplusplus <= 199711L
94 seconds = difftime(this->CurrentClickTime, this->LastClickTime);
96 seconds = this->CurrentClickTime - this->LastClickTime;
99 if (seconds <= this->DoubleClickTolerance)
101 std::cout << "Double clicked. with " << seconds << " of diference" << std::endl;
102 this->NumberOfClicks = 0;
106 std::cout << "Too slow to be considered as a double click" << std::endl;
107 this->NumberOfClicks = 1;
111 vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
117 unsigned int NumberOfClicks;
118 int PreviousPosition[2];
119 int ResetPixelDistance;
120 double DoubleClickTolerance; // time in miliseconds
122 #if __cplusplus <= 199711L
123 time_t LastClickTime;
124 time_t CurrentClickTime;
126 std::chrono::milliseconds LastClickTime;
127 std::chrono::milliseconds CurrentClickTime;
130 vtkStandardNewMacro(MouseInteractorStyleDoubleClick);
132 int main(int, char *[])
134 vtkSmartPointer<vtkSphereSource> sphereSource =
135 vtkSmartPointer<vtkSphereSource>::New();
136 sphereSource->SetCenter(0.0, 0.0, 0.0);
137 sphereSource->SetRadius(5.0);
138 sphereSource->Update();
140 vtkSmartPointer<vtkPolyDataMapper> mapper =
141 vtkSmartPointer<vtkPolyDataMapper>::New();
142 mapper->SetInputConnection(sphereSource->GetOutputPort());
144 vtkSmartPointer<vtkActor> actor =
145 vtkSmartPointer<vtkActor>::New();
146 actor->SetMapper(mapper);
148 vtkSmartPointer<vtkRenderer> renderer =
149 vtkSmartPointer<vtkRenderer>::New();
150 renderer->SetBackground(1, 1, 1); // Background color white
151 renderer->AddActor(actor);
153 vtkSmartPointer<vtkRenderWindow> renderWindow =
154 vtkSmartPointer<vtkRenderWindow>::New();
155 renderWindow->AddRenderer(renderer);
157 vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
158 vtkSmartPointer<vtkRenderWindowInteractor>::New();
159 renderWindowInteractor->SetRenderWindow(renderWindow);
161 vtkSmartPointer<MouseInteractorStyleDoubleClick> style =
162 vtkSmartPointer<MouseInteractorStyleDoubleClick>::New();
163 renderWindowInteractor->SetInteractorStyle(style);
165 renderWindowInteractor->Initialize();
166 renderWindowInteractor->Start();