-
Notifications
You must be signed in to change notification settings - Fork 24
/
camera_controller.js
364 lines (313 loc) · 11.7 KB
/
camera_controller.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
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
/**
* camera_controller.js
* @flow
*/
import * as React from "react";
import * as THREE from "three";
import TWEEN from "tween.js";
import _ from "lodash";
import {
type OrthoView,
type OrthoViewMap,
type OrthoViewRects,
OrthoViewValuesWithoutTDView,
OrthoViews,
type Vector3,
} from "oxalis/constants";
import { V3 } from "libs/mjs";
import {
getDatasetExtentInLength,
getDatasetCenter,
} from "oxalis/model/accessors/dataset_accessor";
import { getInputCatcherAspectRatio } from "oxalis/model/accessors/view_mode_accessor";
import {
getPlaneExtentInVoxelFromStore,
getPosition,
} from "oxalis/model/accessors/flycam_accessor";
import { listenToStoreProperty } from "oxalis/model/helpers/listener_helpers";
import { setTDCameraWithoutTimeTrackingAction } from "oxalis/model/actions/view_mode_actions";
import { voxelToNm, getBaseVoxel } from "oxalis/model/scaleinfo";
import Store, { type CameraData } from "oxalis/store";
import api from "oxalis/api/internal_api";
type Props = {
cameras: OrthoViewMap<typeof THREE.OrthographicCamera>,
onCameraPositionChanged: () => void,
};
function getQuaternionFromCamera(_up, position, center) {
const up = V3.normalize(_up);
const forward = V3.normalize(V3.sub(center, position));
const right = V3.normalize(V3.cross(up, forward));
const rotationMatrix = new THREE.Matrix4();
// prettier-ignore
rotationMatrix.set(
right[0], up[0], forward[0], 0,
right[1], up[1], forward[1], 0,
right[2], up[2], forward[2], 0,
0, 0, 0, 1,
);
const quat = new THREE.Quaternion();
quat.setFromRotationMatrix(rotationMatrix);
return quat;
}
function getCameraFromQuaternion(quat) {
// Derived from: https://stackoverflow.com/questions/1556260/convert-quaternion-rotation-to-rotation-matrix
const { x, y, z, w } = quat;
const right = [
1.0 - 2.0 * y * y - 2.0 * z * z,
2.0 * x * y + 2.0 * z * w,
2.0 * x * z - 2.0 * y * w,
];
const up = [
2.0 * x * y - 2.0 * z * w,
1.0 - 2.0 * x * x - 2.0 * z * z,
2.0 * y * z + 2.0 * x * w,
];
const forward = [
2.0 * x * z + 2.0 * y * w,
2.0 * y * z - 2.0 * x * w,
1.0 - 2.0 * x * x - 2.0 * y * y,
];
return { right, up, forward };
}
class CameraController extends React.PureComponent<Props> {
storePropertyUnsubscribers: Array<Function>;
componentDidMount() {
const far = 8000000;
for (const cam of _.values(this.props.cameras)) {
cam.near = 0;
cam.far = far;
}
Store.dispatch(
setTDCameraWithoutTimeTrackingAction({
near: 0,
far,
}),
);
this.bindToEvents();
api.tracing.rotate3DViewToDiagonal(false);
}
componentWillUnmount() {
this.storePropertyUnsubscribers.forEach(fn => fn());
}
// Non-TD-View methods
updateCamViewport(inputCatcherRects?: OrthoViewRects): void {
const state = Store.getState();
const { clippingDistance } = state.userConfiguration;
const scaleFactor = getBaseVoxel(state.dataset.dataSource.scale);
for (const planeId of OrthoViewValuesWithoutTDView) {
const [width, height] = getPlaneExtentInVoxelFromStore(
state,
state.flycam.zoomStep,
planeId,
).map(x => x * scaleFactor);
this.props.cameras[planeId].left = -width / 2;
this.props.cameras[planeId].right = width / 2;
this.props.cameras[planeId].bottom = -height / 2;
this.props.cameras[planeId].top = height / 2;
this.props.cameras[planeId].near = -clippingDistance;
this.props.cameras[planeId].updateProjectionMatrix();
}
if (inputCatcherRects != null) {
// Update td camera's aspect ratio
const tdCamera = this.props.cameras[OrthoViews.TDView];
const oldMid = (tdCamera.right + tdCamera.left) / 2;
const oldWidth = tdCamera.right - tdCamera.left;
const oldHeight = tdCamera.top - tdCamera.bottom;
const oldAspectRatio = oldWidth / oldHeight;
const tdRect = inputCatcherRects[OrthoViews.TDView];
const newAspectRatio = tdRect.width / tdRect.height;
// Do not update the tdCamera if the tdView is not visible (height === 0)
if (Number.isNaN(newAspectRatio)) return;
const newWidth = (oldWidth * newAspectRatio) / oldAspectRatio;
tdCamera.left = oldMid - newWidth / 2;
tdCamera.right = oldMid + newWidth / 2;
tdCamera.updateProjectionMatrix();
}
}
update(): void {
const state = Store.getState();
const gPos = getPosition(state.flycam);
// camera position's unit is nm, so convert it.
const cPos = voxelToNm(state.dataset.dataSource.scale, gPos);
this.props.cameras[OrthoViews.PLANE_XY].position.set(cPos[0], cPos[1], cPos[2]);
this.props.cameras[OrthoViews.PLANE_YZ].position.set(cPos[0], cPos[1], cPos[2]);
this.props.cameras[OrthoViews.PLANE_XZ].position.set(cPos[0], cPos[1], cPos[2]);
}
bindToEvents() {
this.storePropertyUnsubscribers = [
listenToStoreProperty(
storeState => storeState.userConfiguration.clippingDistance,
() => this.updateCamViewport(),
true,
),
listenToStoreProperty(
storeState => storeState.flycam.zoomStep,
() => this.updateCamViewport(),
),
listenToStoreProperty(
storeState => storeState.viewModeData.plane.inputCatcherRects,
inputCatcherRects => this.updateCamViewport(inputCatcherRects),
),
listenToStoreProperty(
storeState => storeState.flycam.currentMatrix,
() => this.update(),
true,
),
listenToStoreProperty(
storeState => storeState.viewModeData.plane.tdCamera,
cameraData => this.updateTDCamera(cameraData),
true,
),
];
}
// TD-View methods
updateTDCamera(cameraData: CameraData): void {
const tdCamera = this.props.cameras[OrthoViews.TDView];
tdCamera.position.set(...cameraData.position);
tdCamera.left = cameraData.left;
tdCamera.right = cameraData.right;
tdCamera.top = cameraData.top;
tdCamera.bottom = cameraData.bottom;
tdCamera.up = new THREE.Vector3(...cameraData.up);
tdCamera.lookAt(new THREE.Vector3(...cameraData.lookAt));
tdCamera.updateProjectionMatrix();
this.props.onCameraPositionChanged();
}
render() {
return null;
}
}
type TweenState = {
left: number,
right: number,
top: number,
bottom: number,
};
export function rotate3DViewTo(id: OrthoView, animate: boolean = true): void {
const state = Store.getState();
const { dataset } = state;
const { tdCamera } = state.viewModeData.plane;
const flycamPos = voxelToNm(dataset.dataSource.scale, getPosition(state.flycam));
const datasetExtent = getDatasetExtentInLength(dataset);
// This distance ensures that the 3D camera is so far "in the back" that all elements in the scene
// are in front of it and thus visible.
const clippingOffsetFactor = Math.max(
datasetExtent.width,
datasetExtent.height,
datasetExtent.depth,
);
// Use width and height to keep the same zoom.
let width = tdCamera.right - tdCamera.left;
let height = tdCamera.top - tdCamera.bottom;
let position: Vector3;
let up: Vector3;
// Way to calculate the position and rotation of the camera:
// First, the camera is either positioned at the current center of the flycam or in the dataset center.
// Second, the camera is moved backwards by a clipping offset into the wanted direction.
// Together with matching lookUp (up) vectors and keeping the width and height, the position and rotation updates correctly.
if (id === OrthoViews.TDView && (height <= 0 || width <= 0)) {
// This should only be the case when initializing the 3D-viewport.
const aspectRatio = getInputCatcherAspectRatio(state, OrthoViews.TDView);
const datasetCenter = voxelToNm(dataset.dataSource.scale, getDatasetCenter(dataset));
// The camera has no width and height which might be due to a bug or the camera has not been initialized.
// Thus we zoom out to show the whole dataset.
const paddingFactor = 1.1;
width = Math.sqrt(datasetExtent.width ** 2 + datasetExtent.height ** 2) * paddingFactor;
height = width / aspectRatio;
up = [0, 0, -1];
// For very tall datasets that have a very low or high z starting coordinate, the planes might not be visible.
// Thus take the z coordinate of the flycam instead of the z coordinate of the center.
// The clippingOffsetFactor is added in x and y direction to get a view on the dataset the 3D view that is close to the plane views.
// Thus the rotation between the 3D view to the eg. XY plane views is much shorter and the interpolated rotation does not look weird.
position = [
datasetCenter[0] + clippingOffsetFactor,
datasetCenter[1] + clippingOffsetFactor,
flycamPos[2] - clippingOffsetFactor,
];
} else if (id === OrthoViews.TDView) {
position = [
flycamPos[0] + clippingOffsetFactor,
flycamPos[1] + clippingOffsetFactor,
flycamPos[2] - clippingOffsetFactor,
];
up = [0, 0, -1];
} else {
const positionOffset: OrthoViewMap<Vector3> = {
[OrthoViews.PLANE_XY]: [0, 0, -clippingOffsetFactor],
[OrthoViews.PLANE_YZ]: [clippingOffsetFactor, 0, 0],
[OrthoViews.PLANE_XZ]: [0, clippingOffsetFactor, 0],
};
const upVector: OrthoViewMap<Vector3> = {
[OrthoViews.PLANE_XY]: [0, -1, 0],
[OrthoViews.PLANE_YZ]: [0, -1, 0],
[OrthoViews.PLANE_XZ]: [0, 0, -1],
};
up = upVector[id];
position = [
positionOffset[id][0] + flycamPos[0],
positionOffset[id][1] + flycamPos[1],
positionOffset[id][2] + flycamPos[2],
];
}
const currentFlycamPos = voxelToNm(
Store.getState().dataset.dataSource.scale,
getPosition(Store.getState().flycam),
) || [0, 0, 0];
// Compute current and target orientation as quaternion. When tweening between
// these orientations, we compute the new camera position by keeping the distance
// (radius) to currentFlycamPos constant. Consequently, the camera moves on the
// surfaces of a sphere with the center at currentFlycamPos.
const startQuaternion = getQuaternionFromCamera(tdCamera.up, tdCamera.position, currentFlycamPos);
const targetQuaternion = getQuaternionFromCamera(up, position, currentFlycamPos);
const centerDistance = V3.length(V3.sub(currentFlycamPos, position));
const to: TweenState = {
left: -width / 2,
right: width / 2,
top: height / 2,
bottom: -height / 2,
};
const updateCameraTDView = (tweenState: TweenState, t: number) => {
const { left, right, top, bottom } = tweenState;
const tweenedQuat = new THREE.Quaternion();
THREE.Quaternion.slerp(startQuaternion, targetQuaternion, tweenedQuat, t);
const tweened = getCameraFromQuaternion(tweenedQuat);
// Use forward vector and currentFlycamPos (lookAt target) to calculate the current
// camera's position which should be on a sphere (center=currentFlycamPos, radius=centerDistance).
const newPosition = V3.toArray(
V3.sub(currentFlycamPos, V3.scale(tweened.forward, centerDistance)),
);
Store.dispatch(
setTDCameraWithoutTimeTrackingAction({
position: newPosition,
up: tweened.up,
left,
right,
top,
bottom,
lookAt: currentFlycamPos,
}),
);
};
if (animate) {
const from: TweenState = {
left: tdCamera.left,
right: tdCamera.right,
top: tdCamera.top,
bottom: tdCamera.bottom,
};
const tween = new TWEEN.Tween(from);
const time = 800;
tween
.to(to, time)
.onUpdate(function updater(t) {
// TweenJS passes the current state via the `this` object.
// However, for better type checking, we pass it as an explicit
// parameter.
updateCameraTDView(this, t);
})
.start();
} else {
updateCameraTDView(to, 1);
}
}
export default CameraController;