File: vtkInteractorStyleMultiTouchCamera.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (253 lines) | stat: -rw-r--r-- 8,141 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkInteractorStyleMultiTouchCamera.h"

#include "vtkCallbackCommand.h"
#include "vtkCamera.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPlane.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkTransform.h"

VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkInteractorStyleMultiTouchCamera);

//------------------------------------------------------------------------------
vtkInteractorStyleMultiTouchCamera::vtkInteractorStyleMultiTouchCamera() = default;

//------------------------------------------------------------------------------
vtkInteractorStyleMultiTouchCamera::~vtkInteractorStyleMultiTouchCamera() = default;

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnStartRotate()
{
  this->StartGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnRotate()
{
  if (this->State != VTKIS_GESTURE)
  {
    return;
  }

  int pointer = this->Interactor->GetPointerIndex();

  this->FindPokedRenderer(this->Interactor->GetEventPositions(pointer)[0],
    this->Interactor->GetEventPositions(pointer)[1]);

  if (this->CurrentRenderer == nullptr)
  {
    return;
  }

  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();

  int* pinchPositionDisplay = this->Interactor->GetEventPositions(pointer);

  // Calculate the focal depth since we'll be using it a lot
  double viewFocus[4];
  camera->GetFocalPoint(viewFocus);
  this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
  double focalDepth = viewFocus[2];

  double oldPickPoint[4] = { 0, 0, 0, 0 };
  vtkInteractorObserver::ComputeDisplayToWorld(this->CurrentRenderer, pinchPositionDisplay[0],
    pinchPositionDisplay[1], focalDepth, oldPickPoint);

  camera->Roll(this->Interactor->GetRotation() - this->Interactor->GetLastRotation());

  camera->GetFocalPoint(viewFocus);
  this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
  focalDepth = viewFocus[2];

  double newPickPoint[4] = { 0, 0, 0, 0 };
  vtkInteractorObserver::ComputeDisplayToWorld(this->CurrentRenderer, pinchPositionDisplay[0],
    pinchPositionDisplay[1], focalDepth, newPickPoint);

  double motionVector[3] = { 0, 0, 0 };
  vtkMath::Subtract(oldPickPoint, newPickPoint, motionVector);

  vtkNew<vtkTransform> cameraTransform;
  cameraTransform->Identity();
  cameraTransform->Translate(motionVector);
  camera->ApplyTransform(cameraTransform);

  camera->OrthogonalizeViewUp();

  this->Interactor->Render();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnEndRotate()
{
  this->EndGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnStartPinch()
{
  this->StartGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnPinch()
{
  if (this->State != VTKIS_GESTURE)
  {
    return;
  }

  int pointer = this->Interactor->GetPointerIndex();

  this->FindPokedRenderer(this->Interactor->GetEventPositions(pointer)[0],
    this->Interactor->GetEventPositions(pointer)[1]);

  if (this->CurrentRenderer == nullptr)
  {
    return;
  }

  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();

  int* pinchPositionDisplay = this->Interactor->GetEventPositions(pointer);

  // Calculate the focal depth since we'll be using it a lot
  double viewFocus[4], focalDepth;
  camera->GetFocalPoint(viewFocus);
  this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
  focalDepth = viewFocus[2];

  // Remember the position of the center of the pinch in world coordinates
  // This position should stay in the same location on the screen after the dolly has been performed
  double oldPickPoint[4] = { 0, 0, 0, 0 };
  this->ComputeDisplayToWorld(
    pinchPositionDisplay[0], pinchPositionDisplay[1], focalDepth, oldPickPoint);

  double dyf = this->Interactor->GetScale() / this->Interactor->GetLastScale();
  if (camera->GetParallelProjection())
  {
    camera->SetParallelScale(camera->GetParallelScale() / dyf);
  }
  else
  {
    camera->Dolly(dyf);
    if (this->AutoAdjustCameraClippingRange)
    {
      this->CurrentRenderer->ResetCameraClippingRange();
    }
  }

  camera->GetFocalPoint(viewFocus);
  this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
  focalDepth = viewFocus[2];

  // New position at the center of the pinch gesture
  double newPickPoint[4] = { 0, 0, 0, 0 };
  this->ComputeDisplayToWorld(
    pinchPositionDisplay[0], pinchPositionDisplay[1], focalDepth, newPickPoint);

  // Determine how far the pinch center has been shifted from it's original location
  double motionVector[4] = { 0, 0, 0 };
  vtkMath::Subtract(oldPickPoint, newPickPoint, motionVector);

  // Translate the camera to compensate for the shift of the pinch center
  vtkNew<vtkTransform> cameraTransform;
  cameraTransform->Identity();
  cameraTransform->Translate(motionVector);
  camera->ApplyTransform(cameraTransform);

  if (this->Interactor->GetLightFollowCamera())
  {
    this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
  }
  this->Interactor->Render();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnEndPinch()
{
  this->EndGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnStartPan()
{
  this->StartGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnPan()
{
  if (this->State != VTKIS_GESTURE)
  {
    return;
  }

  vtkRenderWindowInteractor* rwi = this->Interactor;
  int pointer = rwi->GetPointerIndex();

  int* eventPos = rwi->GetEventPositions(pointer);
  this->FindPokedRenderer(eventPos[0], eventPos[1]);

  if (this->CurrentRenderer == nullptr)
  {
    return;
  }

  vtkCamera* camera = this->CurrentRenderer->GetActiveCamera();

  // handle panning - 2 DOF
  double viewFocus[4], focalDepth, viewPoint[3];
  double newPickPoint[4], oldPickPoint[4], motionVector[3];

  // Calculate the focal depth since we'll be using it a lot
  camera->GetFocalPoint(viewFocus);
  this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
  focalDepth = viewFocus[2];

  this->ComputeDisplayToWorld(eventPos[0], eventPos[1], focalDepth, newPickPoint);

  // Has to recalc old mouse point since the viewport has moved,
  // so can't move it outside the loop
  int* lastEventPos = rwi->GetLastEventPositions(pointer);
  this->ComputeDisplayToWorld(lastEventPos[0], lastEventPos[1], focalDepth, oldPickPoint);

  // Camera motion is reversed
  motionVector[0] = oldPickPoint[0] - newPickPoint[0];
  motionVector[1] = oldPickPoint[1] - newPickPoint[1];
  motionVector[2] = oldPickPoint[2] - newPickPoint[2];

  camera->GetFocalPoint(viewFocus);
  camera->GetPosition(viewPoint);
  camera->SetFocalPoint(
    motionVector[0] + viewFocus[0], motionVector[1] + viewFocus[1], motionVector[2] + viewFocus[2]);

  camera->SetPosition(
    motionVector[0] + viewPoint[0], motionVector[1] + viewPoint[1], motionVector[2] + viewPoint[2]);

  // clean up
  if (this->Interactor->GetLightFollowCamera())
  {
    this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
  }

  rwi->Render();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::OnEndPan()
{
  this->EndGesture();
}

//------------------------------------------------------------------------------
void vtkInteractorStyleMultiTouchCamera::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}
VTK_ABI_NAMESPACE_END