-
-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathvtkF3DInteractorStyle.cxx
More file actions
420 lines (356 loc) · 11.7 KB
/
vtkF3DInteractorStyle.cxx
File metadata and controls
420 lines (356 loc) · 11.7 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "vtkF3DInteractorStyle.h"
#include "F3DLog.h"
#include "vtkF3DRenderer.h"
#include <vtkCamera.h>
#include <vtkMath.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRendererCollection.h>
#include <vtkSkybox.h>
#include <vtkStringArray.h>
#include <vtkTransform.h>
vtkStandardNewMacro(vtkF3DInteractorStyle);
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnLeftButtonDown()
{
this->FindPokedRenderer(
this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
assert(this->CurrentRenderer != nullptr);
if (this->Interactor->GetShiftKey())
{
this->StartPan();
}
else
{
if (this->Interactor->GetControlKey())
{
this->StartSpin();
}
else
{
this->StartRotate();
}
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnLeftButtonUp()
{
switch (this->State)
{
case VTKIS_PAN:
this->EndPan();
break;
case VTKIS_SPIN:
this->EndSpin();
break;
case VTKIS_ROTATE:
this->EndRotate();
break;
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnMiddleButtonDown()
{
this->FindPokedRenderer(
this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
assert(this->CurrentRenderer != nullptr);
this->StartPan();
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnMiddleButtonUp()
{
switch (this->State)
{
case VTKIS_PAN:
this->EndPan();
break;
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnRightButtonDown()
{
this->FindPokedRenderer(
this->Interactor->GetEventPosition()[0], this->Interactor->GetEventPosition()[1]);
assert(this->CurrentRenderer != nullptr);
if (this->Interactor->GetShiftKey())
{
this->StartEnvRotate();
}
else
{
this->StartDolly();
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnRightButtonUp()
{
switch (this->State)
{
case VTKIS_ENV_ROTATE:
this->EndEnvRotate();
break;
case VTKIS_DOLLY:
this->EndDolly();
break;
}
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnDropFiles(vtkStringArray* files)
{
if (files == nullptr)
{
F3DLog::Print(F3DLog::Severity::Warning, "Drop event without any provided files.");
return;
}
this->InvokeEvent(vtkF3DInteractorStyle::DropFilesEvent, files);
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::OnKeyPress()
{
this->InvokeEvent(vtkF3DInteractorStyle::KeyPressEvent, nullptr);
std::string interaction = this->Interactor->GetKeySym();
if (!interaction.empty())
{
// Make sure key symbols starts with an upper char (e.g. "space" -> "Space")
interaction[0] = std::toupper(interaction[0]);
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::Rotate()
{
if (this->CameraMovementDisabled)
{
return;
}
vtkF3DRenderer* ren = vtkF3DRenderer::SafeDownCast(this->CurrentRenderer);
vtkRenderWindowInteractor* rwi = this->Interactor;
int dx = rwi->GetEventPosition()[0] - rwi->GetLastEventPosition()[0];
int dy = rwi->GetEventPosition()[1] - rwi->GetLastEventPosition()[1];
const int* size = ren->GetRenderWindow()->GetSize();
double delta_elevation = -20.0 / size[1];
double delta_azimuth = -20.0 / size[0];
double rxf = dx * delta_azimuth * this->MotionFactor;
double ryf = dy * delta_elevation * this->MotionFactor;
vtkCamera* camera = ren->GetActiveCamera();
if (!ren->GetUseTrackball())
{
double up[3];
this->InterpolateTemporaryUp(0.1, ren->GetUpVector(), up);
double envUpCamDirCross[3];
vtkMath::Cross(up, camera->GetDirectionOfProjection(), envUpCamDirCross);
constexpr double EPSILON = 128 * std::numeric_limits<double>::epsilon();
if (vtkMath::Norm(envUpCamDirCross) < EPSILON)
{
// Keep setting the temporary up to the camera's up vector until the interpolated up vector
// and the camera direction vector are not collinear
this->SetTemporaryUp(camera->GetViewUp());
this->InterpolateTemporaryUp(0.1, ren->GetUpVector(), up);
}
vtkNew<vtkTransform> Transform;
const double* fp = camera->GetFocalPoint();
double newPos[3];
if (ren->GetUseRotationAxis())
{
Transform->Identity();
Transform->Translate(+fp[0], +fp[1], +fp[2]);
Transform->RotateWXYZ(ren->GetMovementVector()[0] * rxf + ren->GetMovementVector()[1] * ryf,
ren->GetRotationAxis());
Transform->Translate(-fp[0], -fp[1], -fp[2]);
Transform->TransformPoint(camera->GetPosition(), newPos);
camera->SetPosition(newPos);
double newViewUp[3];
Transform->TransformVector(camera->GetViewUp(), newViewUp);
camera->SetViewUp(newViewUp);
}
else
{
// Rotate camera around the focal point about the environment's up vector
Transform->Identity();
Transform->Translate(+fp[0], +fp[1], +fp[2]);
Transform->RotateWXYZ(rxf, ren->GetUpVector());
Transform->Translate(-fp[0], -fp[1], -fp[2]);
Transform->TransformPoint(camera->GetPosition(), newPos);
camera->SetPosition(newPos);
// Clamp parameter to `camera->Elevation()` to maintain -90 < elevation < +90
constexpr double maxAbsElevation = 90 - 1e-10;
const double elevation = vtkMath::DegreesFromRadians(
vtkMath::AngleBetweenVectors(ren->GetUpVector(), camera->GetDirectionOfProjection()) -
vtkMath::Pi() / 2);
camera->Elevation(
std::clamp(ryf, -maxAbsElevation - elevation, +maxAbsElevation - elevation));
camera->SetViewUp(up);
}
camera->OrthogonalizeViewUp();
}
else
{
camera->Azimuth(rxf);
camera->Elevation(ryf);
camera->OrthogonalizeViewUp();
}
this->UpdateRendererAfterInteraction();
rwi->Render();
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::Spin()
{
if (this->CameraMovementDisabled)
{
return;
}
this->Superclass::Spin();
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::Pan()
{
if (this->CameraMovementDisabled)
{
return;
}
this->Superclass::Pan();
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::Dolly()
{
if (this->CameraMovementDisabled)
{
return;
}
assert(this->CurrentRenderer != nullptr);
vtkRenderWindowInteractor* rwi = this->Interactor;
const double* center = this->CurrentRenderer->GetCenter();
const int* current_position = rwi->GetEventPosition();
const int* last_position = rwi->GetLastEventPosition();
const int dy = current_position[1] - last_position[1];
const int dx = current_position[0] - last_position[0];
const double dxf = this->MotionFactor * dx / center[0];
const double dyf = this->MotionFactor * dy / center[1];
double dtf = std::abs(dyf) > std::abs(dxf) ? dyf : dxf;
vtkF3DRenderer* ren = vtkF3DRenderer::SafeDownCast(this->CurrentRenderer);
if (ren && ren->GetInvertZoom())
{
dtf *= -1.;
}
this->Dolly(pow(1.1, dtf));
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::Dolly(double factor)
{
if (this->CameraMovementDisabled)
{
return;
}
if (this->Interactor->GetControlKey())
{
vtkF3DInteractorStyle::DollyToPosition(
factor, this->Interactor->GetEventPosition(), this->CurrentRenderer);
}
else
{
this->Superclass::Dolly(factor);
}
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::DollyToPosition(double factor, int* position, vtkRenderer* renderer)
{
vtkCamera* cam = renderer->GetActiveCamera();
double viewFocus[4], originalViewFocus[3], cameraPos[3], newCameraPos[3];
double newFocalPoint[4], norm[3];
// Move focal point to cursor position
cam->GetPosition(cameraPos);
cam->GetFocalPoint(viewFocus);
cam->GetFocalPoint(originalViewFocus);
cam->GetViewPlaneNormal(norm);
vtkF3DInteractorStyle::ComputeWorldToDisplay(
renderer, viewFocus[0], viewFocus[1], viewFocus[2], viewFocus);
vtkF3DInteractorStyle::ComputeDisplayToWorld(
renderer, double(position[0]), double(position[1]), viewFocus[2], newFocalPoint);
cam->SetFocalPoint(newFocalPoint);
// Move camera in/out along projection direction
cam->Dolly(factor);
// Find new focal point
cam->GetPosition(newCameraPos);
double newPoint[3];
newPoint[0] = originalViewFocus[0] + newCameraPos[0] - cameraPos[0];
newPoint[1] = originalViewFocus[1] + newCameraPos[1] - cameraPos[1];
newPoint[2] = originalViewFocus[2] + newCameraPos[2] - cameraPos[2];
cam->SetFocalPoint(newPoint);
}
//----------------------------------------------------------------------------
void vtkF3DInteractorStyle::EnvironmentRotate()
{
this->Superclass::EnvironmentRotate();
vtkF3DRenderer* ren = vtkF3DRenderer::SafeDownCast(this->CurrentRenderer);
if (ren)
{
// update skybox orientation
double* up = ren->GetEnvironmentUp();
double* right = ren->GetEnvironmentRight();
double front[3];
vtkMath::Cross(right, up, front);
ren->GetSkyboxActor()->SetFloorPlane(up[0], up[1], up[2], 0.0);
ren->GetSkyboxActor()->SetFloorRight(front[0], front[1], front[2]);
this->Interactor->Render();
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::UpdateRendererAfterInteraction()
{
// Make sure this->CurrentRenderer is set
this->FindPokedRenderer(0, 0);
if (this->CurrentRenderer)
{
if (this->AutoAdjustCameraClippingRange)
{
this->CurrentRenderer->ResetCameraClippingRange();
}
vtkRenderWindowInteractor* rwi = this->Interactor;
if (rwi->GetLightFollowCamera())
{
this->CurrentRenderer->UpdateLightsGeometryToFollowCamera();
}
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::FindPokedRenderer(int vtkNotUsed(x), int vtkNotUsed(y))
{
// No need for picking, F3D interaction are only with the first renderer
this->SetCurrentRenderer(this->Interactor->GetRenderWindow()->GetRenderers()->GetFirstRenderer());
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::ResetTemporaryUp()
{
// Make sure this->CurrentRenderer is set
this->FindPokedRenderer(0, 0);
if (this->CurrentRenderer)
{
vtkF3DRenderer* ren = vtkF3DRenderer::SafeDownCast(this->CurrentRenderer);
SetTemporaryUp(ren->GetUpVector());
}
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::SetTemporaryUp(const double* tempUp)
{
for (int i = 0; i < 3; i++)
{
this->TemporaryUp[i] = tempUp[i];
}
this->TemporaryUpFactor = 1.0;
}
//------------------------------------------------------------------------------
void vtkF3DInteractorStyle::InterpolateTemporaryUp(
const double factorDelta, const double* target, double* output)
{
this->TemporaryUpFactor = std::max(this->TemporaryUpFactor - factorDelta, 0.0);
if (this->TemporaryUpFactor >= 0)
{
const double factor = (1.0 - std::cos(vtkMath::Pi() * this->TemporaryUpFactor)) * 0.5;
for (int i = 0; i < 3; i++)
{
output[i] = factor * this->TemporaryUp[i] + (1.0 - factor) * target[i];
}
vtkMath::Normalize(output);
}
}