ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
vtkPVInteractorStyle.cxx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ParaView
4  Module: vtkPVInteractorStyle.cxx
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkPVInteractorStyle.h"
16 
17 #include "vtkCamera.h"
18 #include "vtkCameraManipulator.h"
19 #include "vtkCollection.h"
20 #include "vtkCollectionIterator.h"
21 #include "vtkCommand.h"
22 #include "vtkLight.h"
23 #include "vtkLightCollection.h"
24 #include "vtkObjectFactory.h"
25 #include "vtkRenderWindow.h"
26 #include "vtkRenderWindowInteractor.h"
27 #include "vtkRenderer.h"
28 
30 
31 //-------------------------------------------------------------------------
33 {
34  this->UseTimers = 0;
35  this->CameraManipulators = vtkCollection::New();
36  this->CurrentManipulator = NULL;
37  this->CenterOfRotation[0] = this->CenterOfRotation[1] = this->CenterOfRotation[2] = 0;
38  this->RotationFactor = 1.0;
39 }
40 
41 //-------------------------------------------------------------------------
43 {
44  this->CameraManipulators->Delete();
45  this->CameraManipulators = NULL;
46 }
47 
48 //-------------------------------------------------------------------------
50 {
51  this->CameraManipulators->RemoveAllItems();
52 }
53 
54 //-------------------------------------------------------------------------
56 {
57  this->CameraManipulators->AddItem(m);
58 }
59 
60 //-------------------------------------------------------------------------
62 {
63  this->OnButtonDown(1, this->Interactor->GetShiftKey(), this->Interactor->GetControlKey());
64 }
65 
66 //-------------------------------------------------------------------------
68 {
69  this->OnButtonDown(2, this->Interactor->GetShiftKey(), this->Interactor->GetControlKey());
70 }
71 
72 //-------------------------------------------------------------------------
74 {
75  this->OnButtonDown(3, this->Interactor->GetShiftKey(), this->Interactor->GetControlKey());
76 }
77 
78 //-------------------------------------------------------------------------
79 void vtkPVInteractorStyle::OnButtonDown(int button, int shift, int control)
80 {
81  // Must not be processing an interaction to start another.
82  if (this->CurrentManipulator)
83  {
84  return;
85  }
86 
87  // Get the renderer.
88  this->FindPokedRenderer(
89  this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
90  if (this->CurrentRenderer == NULL)
91  {
92  return;
93  }
94 
95  // Look for a matching camera interactor.
96  this->CurrentManipulator = this->FindManipulator(button, shift, control);
97  if (this->CurrentManipulator)
98  {
99  this->CurrentManipulator->Register(this);
100  this->InvokeEvent(vtkCommand::StartInteractionEvent);
101  this->CurrentManipulator->SetCenter(this->CenterOfRotation);
102  this->CurrentManipulator->SetRotationFactor(this->RotationFactor);
104  this->CurrentManipulator->OnButtonDown(this->Interactor->GetEventPosition()[0],
105  this->Interactor->GetEventPosition()[1], this->CurrentRenderer, this->Interactor);
106  }
107 }
108 
109 //-------------------------------------------------------------------------
111 {
112  // Look for a matching camera interactor.
113  this->CameraManipulators->InitTraversal();
114  vtkCameraManipulator* manipulator = NULL;
115  while ((manipulator = (vtkCameraManipulator*)this->CameraManipulators->GetNextItemAsObject()))
116  {
117  if (manipulator->GetButton() == button && manipulator->GetShift() == shift &&
118  manipulator->GetControl() == control)
119  {
120  return manipulator;
121  }
122  }
123  return NULL;
124 }
125 
126 //-------------------------------------------------------------------------
128 {
129  this->OnButtonUp(1);
130 }
131 //-------------------------------------------------------------------------
133 {
134  this->OnButtonUp(2);
135 }
136 //-------------------------------------------------------------------------
138 {
139  this->OnButtonUp(3);
140 }
141 
142 //-------------------------------------------------------------------------
144 {
145  if (this->CurrentManipulator == NULL)
146  {
147  return;
148  }
149  if (this->CurrentManipulator->GetButton() == button)
150  {
151  this->CurrentManipulator->OnButtonUp(this->Interactor->GetEventPosition()[0],
152  this->Interactor->GetEventPosition()[1], this->CurrentRenderer, this->Interactor);
154  this->InvokeEvent(vtkCommand::EndInteractionEvent);
155  this->CurrentManipulator->UnRegister(this);
156  this->CurrentManipulator = NULL;
157  }
158 }
159 
160 //-------------------------------------------------------------------------
162 {
163  if (this->CurrentRenderer && this->CurrentManipulator)
164  {
165  // When an interaction is active, we should not change the renderer being
166  // interacted with.
167  }
168  else
169  {
170  this->FindPokedRenderer(
171  this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
172  }
173 
174  if (this->CurrentManipulator)
175  {
176  this->CurrentManipulator->OnMouseMove(this->Interactor->GetEventPosition()[0],
177  this->Interactor->GetEventPosition()[1], this->CurrentRenderer, this->Interactor);
178  this->InvokeEvent(vtkCommand::InteractionEvent);
179  }
180 }
181 
182 //-------------------------------------------------------------------------
184 {
185  vtkRenderWindowInteractor* rwi = this->Interactor;
186 
187  switch (rwi->GetKeyCode())
188  {
189  case 'Q':
190  case 'q':
191  // It must be noted that this has no effect in QVTKInteractor and hence
192  // we're assured that the Qt application won't exit because the user hit
193  // 'q'.
194  rwi->ExitCallback();
195  break;
196  }
197 }
198 
199 //-------------------------------------------------------------------------
201 {
202  if (!this->CurrentRenderer)
203  {
204  return;
205  }
206 
207  vtkLight* light;
208 
209  vtkLightCollection* lights = this->CurrentRenderer->GetLights();
210  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();
211 
212  lights->InitTraversal();
213  light = lights->GetNextItem();
214  if (!light)
215  {
216  return;
217  }
218  light->SetPosition(camera->GetPosition());
219  light->SetFocalPoint(camera->GetFocalPoint());
220 }
221 
222 //-------------------------------------------------------------------------
224 {
225  // Look for a matching camera interactor.
226  this->CameraManipulators->InitTraversal();
227  vtkCameraManipulator* manipulator = NULL;
228  while ((manipulator = (vtkCameraManipulator*)this->CameraManipulators->GetNextItemAsObject()))
229  {
230  manipulator->OnKeyDown(this->Interactor);
231  }
232 }
233 
234 //-------------------------------------------------------------------------
236 {
237  // Look for a matching camera interactor.
238  this->CameraManipulators->InitTraversal();
239  vtkCameraManipulator* manipulator = NULL;
240  while ((manipulator = (vtkCameraManipulator*)this->CameraManipulators->GetNextItemAsObject()))
241  {
242  manipulator->OnKeyUp(this->Interactor);
243  }
244 }
245 
247 {
248  if (this->Interactor->GetControlKey())
249  {
251  fact, this->Interactor->GetEventPosition(), this->CurrentRenderer);
252  }
253  else
254  {
255  this->Superclass::Dolly(fact);
256  }
257 }
258 
259 //-------------------------------------------------------------------------
260 void vtkPVInteractorStyle::DollyToPosition(double fact, int* position, vtkRenderer* renderer)
261 {
262  vtkCamera* cam = renderer->GetActiveCamera();
263  if (cam->GetParallelProjection())
264  {
265  int x0 = 0, y0 = 0, x1 = 0, y1 = 0;
266  // Zoom relatively to the cursor
267  int* aSize = renderer->GetRenderWindow()->GetSize();
268  int w = aSize[0];
269  int h = aSize[1];
270  x0 = w / 2;
271  y0 = h / 2;
272  x1 = position[0];
273  y1 = position[1];
274  vtkPVInteractorStyle::TranslateCamera(renderer, x0, y0, x1, y1);
275  cam->SetParallelScale(cam->GetParallelScale() / fact);
276  vtkPVInteractorStyle::TranslateCamera(renderer, x1, y1, x0, y0);
277  }
278  else
279  {
280  // Zoom relatively to the cursor position
281  double viewFocus[4], originalViewFocus[3], cameraPos[3], newCameraPos[3];
282  double newFocalPoint[4], norm[3];
283 
284  // Move focal point to cursor position
285  cam->GetPosition(cameraPos);
286  cam->GetFocalPoint(viewFocus);
287  cam->GetFocalPoint(originalViewFocus);
288  cam->GetViewPlaneNormal(norm);
289 
290  vtkPVInteractorStyle::ComputeWorldToDisplay(
291  renderer, viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
292 
293  vtkPVInteractorStyle::ComputeDisplayToWorld(
294  renderer, double(position[0]), double(position[1]), viewFocus[2], newFocalPoint);
295 
296  cam->SetFocalPoint(newFocalPoint);
297 
298  // Move camera in/out along projection direction
299  cam->Dolly(fact);
300 
301  // Find new focal point
302  cam->GetPosition(newCameraPos);
303 
304  double newPoint[3];
305  newPoint[0] = originalViewFocus[0] + newCameraPos[0] - cameraPos[0];
306  newPoint[1] = originalViewFocus[1] + newCameraPos[1] - cameraPos[1];
307  newPoint[2] = originalViewFocus[2] + newCameraPos[2] - cameraPos[2];
308 
309  cam->SetFocalPoint(newPoint);
310  }
311 }
312 
313 //-------------------------------------------------------------------------
315  vtkRenderer* renderer, int toX, int toY, int fromX, int fromY)
316 {
317  vtkCamera* cam = renderer->GetActiveCamera();
318  double viewFocus[4], focalDepth, viewPoint[3];
319  double newPickPoint[4], oldPickPoint[4], motionVector[3];
320  cam->GetFocalPoint(viewFocus);
321 
322  vtkPVInteractorStyle::ComputeWorldToDisplay(
323  renderer, viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
324  focalDepth = viewFocus[2];
325 
326  vtkPVInteractorStyle::ComputeDisplayToWorld(
327  renderer, double(toX), double(toY), focalDepth, newPickPoint);
328  vtkPVInteractorStyle::ComputeDisplayToWorld(
329  renderer, double(fromX), double(fromY), focalDepth, oldPickPoint);
330 
331  // camera motion is reversed
332  motionVector[0] = oldPickPoint[0] - newPickPoint[0];
333  motionVector[1] = oldPickPoint[1] - newPickPoint[1];
334  motionVector[2] = oldPickPoint[2] - newPickPoint[2];
335 
336  cam->GetFocalPoint(viewFocus);
337  cam->GetPosition(viewPoint);
338  cam->SetFocalPoint(
339  motionVector[0] + viewFocus[0], motionVector[1] + viewFocus[1], motionVector[2] + viewFocus[2]);
340 
341  cam->SetPosition(
342  motionVector[0] + viewPoint[0], motionVector[1] + viewPoint[1], motionVector[2] + viewPoint[2]);
343 }
344 
345 //-------------------------------------------------------------------------
346 void vtkPVInteractorStyle::PrintSelf(ostream& os, vtkIndent indent)
347 {
348  this->Superclass::PrintSelf(os, indent);
349  os << indent << "CenterOfRotation: " << this->CenterOfRotation[0] << ", "
350  << this->CenterOfRotation[1] << ", " << this->CenterOfRotation[2] << endl;
351  os << indent << "RotationFactor: " << this->RotationFactor << endl;
352  os << indent << "CameraManipulators: " << this->CameraManipulators << endl;
353 }
math::float3 position
#define NULL
virtual void OnButtonUp(int x, int y, vtkRenderer *ren, vtkRenderWindowInteractor *iren)
virtual void OnMouseMove(int x, int y, vtkRenderer *ren, vtkRenderWindowInteractor *iren)
virtual void OnButtonDown(int x, int y, vtkRenderer *ren, vtkRenderWindowInteractor *iren)
virtual void OnKeyDown(vtkRenderWindowInteractor *iren)
virtual void OnKeyUp(vtkRenderWindowInteractor *iren)
void OnMiddleButtonUp() override
static void TranslateCamera(vtkRenderer *renderer, int toX, int toY, int fromX, int fromY)
void OnButtonDown(int button, int shift, int control)
void Dolly(double factor) override
void PrintSelf(ostream &os, vtkIndent indent) override
static void DollyToPosition(double fact, int *position, vtkRenderer *renderer)
virtual vtkCameraManipulator * FindManipulator(int button, int shift, int control)
void OnMiddleButtonDown() override
vtkCameraManipulator * CurrentManipulator
vtkCollection * CameraManipulators
void AddManipulator(vtkCameraManipulator *m)
void OnLeftButtonDown() override
void OnRightButtonDown() override
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
constexpr Rgbaf light(0.66f, 0.66f, 0.66f, 1.00f)
vtkStandardNewMacro(vtkPVInteractorStyle)