forked from Kitware/vtk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (93 loc) · 3.71 KB
/
index.js
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
import { vec3, mat4 } from 'gl-matrix';
import macro from 'vtk.js/Sources/macro';
import vtkCameraManipulator from 'vtk.js/Sources/Interaction/Manipulators/CameraManipulator';
import vtkMath from 'vtk.js/Sources/Common/Core/Math';
// glMatrix.setMatrixArrayType(Float64Array);
// ----------------------------------------------------------------------------
// vtkTrackballRotate methods
// ----------------------------------------------------------------------------
function vtkTrackballRotate(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkTrackballRotate');
const newCamPos = new Float64Array(3);
const newFp = new Float64Array(3);
const newViewUp = new Float64Array(3);
const trans = new Float64Array(16);
const v2 = new Float64Array(3);
const centerNeg = new Float64Array(3);
const direction = new Float64Array(3);
publicAPI.onAnimation = (interactor, renderer) => {
const lastPtr = interactor.getPointerIndex();
const pos = interactor.getAnimationEventPosition(lastPtr);
const lastPos = interactor.getLastAnimationEventPosition(lastPtr);
if (!pos || !lastPos || !renderer) {
return;
}
const camera = renderer.getActiveCamera();
const cameraPos = camera.getPosition();
const cameraFp = camera.getFocalPoint();
mat4.identity(trans);
const { center, rotationFactor } = model;
// Translate to center
mat4.translate(trans, trans, center);
const dx = lastPos.x - pos.x;
const dy = lastPos.y - pos.y;
const size = interactor.getView().getSize();
// Azimuth
const viewUp = camera.getViewUp();
mat4.rotate(
trans,
trans,
vtkMath.radiansFromDegrees(360.0 * dx / size[0] * rotationFactor),
viewUp
);
// Elevation
vtkMath.cross(camera.getDirectionOfProjection(), viewUp, v2);
mat4.rotate(
trans,
trans,
vtkMath.radiansFromDegrees(-360.0 * dy / size[1] * rotationFactor),
v2
);
// Translate back
centerNeg[0] = -center[0];
centerNeg[1] = -center[1];
centerNeg[2] = -center[2];
mat4.translate(trans, trans, centerNeg);
// Apply transformation to camera position, focal point, and view up
vec3.transformMat4(newCamPos, cameraPos, trans);
vec3.transformMat4(newFp, cameraFp, trans);
direction[0] = viewUp[0] + cameraPos[0];
direction[1] = viewUp[1] + cameraPos[1];
direction[2] = viewUp[2] + cameraPos[2];
vec3.transformMat4(newViewUp, direction, trans);
camera.setPosition(newCamPos[0], newCamPos[1], newCamPos[2]);
camera.setFocalPoint(newFp[0], newFp[1], newFp[2]);
camera.setViewUp(
newViewUp[0] - newCamPos[0],
newViewUp[1] - newCamPos[1],
newViewUp[2] - newCamPos[2]
);
camera.orthogonalizeViewUp();
renderer.resetCameraClippingRange();
if (interactor.getLightFollowCamera()) {
renderer.updateLightsGeometryToFollowCamera();
}
};
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
// Inheritance
vtkCameraManipulator.extend(publicAPI, model, initialValues);
// Object specific methods
vtkTrackballRotate(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(extend, 'vtkTrackballRotate');
// ----------------------------------------------------------------------------
export default Object.assign({ newInstance, extend });