-
Notifications
You must be signed in to change notification settings - Fork 53
/
controls-manager.ts
485 lines (455 loc) · 14.2 KB
/
controls-manager.ts
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import { Tween } from '@tweenjs/tween.js';
import {
Camera,
PerspectiveCamera,
OrthographicCamera,
Object3D,
Vector3,
Group,
Scene,
Mesh,
TubeGeometry,
} from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
import { RendererManager } from './renderer-manager.js';
/**
* Manager for managing event display controls.
*/
export class ControlsManager {
/** Currently active orbit controls. */
private activeControls: OrbitControls;
/** The main orbit controls. */
private mainControls: OrbitControls;
/** Orbit controls for overlay view. */
private overlayControls: OrbitControls;
/** All orbit controls. */
private controls: OrbitControls[];
/** Orbit controls for the perspective view. */
private perspectiveControls: OrbitControls;
/** Orbit controls for the orthographic view. */
private orthographicControls: OrbitControls;
/**
* Constructor for setting up all the controls.
* @param rendererManager The renderer manager to get the main renderer.
*/
constructor(
rendererManager: RendererManager,
defaultView: number[] = [0, 0, 200],
) {
this.controls = [];
// this.mainControls = new OrbitControls();
// this.overlayControls = null;
const rendererElement = rendererManager.getMainRenderer()?.domElement;
// Arguments: FOV, aspect ratio, near and far distances
const perspectiveCamera = new PerspectiveCamera(
75,
rendererElement.offsetWidth / rendererElement.offsetHeight,
10,
100000,
);
// Arguments: left, right, top, bottom, near and far distances
const orthographicCamera = new OrthographicCamera(
rendererElement.offsetWidth / -2,
rendererElement.offsetWidth / 2,
rendererElement.offsetHeight / 2,
rendererElement.offsetHeight / -2,
10,
100000,
);
// Orbit controls allow to move around
this.perspectiveControls = this.setOrbitControls(
perspectiveCamera,
rendererElement,
);
this.orthographicControls = this.setOrbitControls(
orthographicCamera,
rendererElement,
);
perspectiveCamera.position.z = orthographicCamera.position.z =
defaultView[2];
perspectiveCamera.position.y = orthographicCamera.position.y =
defaultView[1];
perspectiveCamera.position.x = orthographicCamera.position.x =
defaultView[0];
if (defaultView.length >= 6) {
this.perspectiveControls.target = this.orthographicControls.target =
new Vector3(defaultView[3], defaultView[4], defaultView[5]);
}
// Set active orbit controls
this.addControls(this.perspectiveControls);
this.addControls(this.orthographicControls);
this.setActiveControls(this.perspectiveControls);
this.setMainControls(this.perspectiveControls);
this.setOverlayControls(this.orthographicControls);
// Add listener
this.getActiveControls().addEventListener('change', () => {
this.transformSync();
this.updateSync();
});
// Modify camera(s) on window resize
this.setupResize(rendererElement);
}
/**
* Set orbit controls for the camera.
* @param camera The camera with which to create the orbit controls.
* @param domElement DOM element of the renderer to associate the orbit controls with.
* @returns Configured orbit controls.
*/
private setOrbitControls(
camera: PerspectiveCamera | OrthographicCamera,
domElement: HTMLElement,
): OrbitControls {
const controls: OrbitControls = new OrbitControls(camera, domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.enableZoom = true;
controls.autoRotate = false;
return controls;
}
// SET/GET
/**
* Set the currently active orbit controls.
* @param controls Orbit controls to be set as active.
*/
setActiveControls(controls: OrbitControls) {
this.activeControls = controls;
}
/**
* Set the main orbit controls.
* @param controls Orbit controls to be set as main.
*/
setMainControls(controls: OrbitControls) {
this.mainControls = controls;
}
/**
* Set orbit controls for overlay.
* @param controls Orbit controls to be set for overlay.
*/
setOverlayControls(controls: OrbitControls) {
this.overlayControls = controls;
}
/**
* Get currently active orbit controls.
* @returns Currently active orbit controls.
*/
getActiveControls(): OrbitControls {
return this.activeControls;
}
/**
* Get the main orbit controls.
* @returns Main orbit controls.
*/
getMainControls(): OrbitControls {
return this.mainControls;
}
/**
* Get orbit controls for overlay.
* @returns Orbit controls for overlay.
*/
getOverlayControls(): OrbitControls {
return this.overlayControls;
}
/**
* Get the currently active camera.
* @returns Currently active camera.
*/
getActiveCamera(): Camera {
return this.activeControls.object;
}
/**
* Get the main camera.
* @returns Main camera.
*/
getMainCamera(): Camera {
return this.mainControls.object;
}
/**
* Get the camera for overlay.
* @returns The camera for overlay.
*/
getOverlayCamera(): Camera {
return this.overlayControls.object;
}
/**
* Get the main and overlay cameras.
* @returns An array containing the main and overlay cameras.
*/
getAllCameras(): Camera[] {
return [this.getMainCamera(), this.getOverlayCamera()];
}
// FUNCTIONS
/**
* Add orbit controls to the controls list.
* @param controls Orbit controls to be added.
*/
public addControls(controls: OrbitControls): void {
if (!this.containsObject(controls, this.controls)) {
this.controls.push(controls);
}
}
/**
* Remove orbit controls from the controls list.
* @param controls Orbit controls to be removed.
*/
public removeControls(controls: OrbitControls): void {
const index: number = this.controls.indexOf(controls);
if (index > -1) {
this.controls.splice(index, 1);
}
}
/**
* Swap the main and overlay orbit controls.
*/
public swapControls(): void {
const temp: OrbitControls = this.mainControls;
this.mainControls = this.overlayControls;
// this._mainControls.autoRotate = temp.autoRotate;
this.overlayControls = temp;
}
/**
* Synchronously update all controls.
*/
public updateSync(): void {
for (const control of this.controls) {
if (control === this.activeControls) {
continue;
}
this.update(control);
}
}
/**
* Update orbit controls.
* @param controls Orbit controls to be updated.
*/
public update(controls: OrbitControls): void {
controls.update();
}
/**
* Synchronously transform the controls by updating the position and rotation.
*/
public transformSync(): void {
for (const control of this.controls) {
if (control === this.activeControls) {
continue;
}
this.positionSync(control);
this.rotationSync(control);
}
}
/**
* Zoom all the cameras by a specific zoom factor.
* The factor may either be greater or smaller.
* @param zoomFactor The factor to zoom by.
* @param zoomTime The time it takes for a zoom animation to complete.
*/
public zoomTo(zoomFactor: number, zoomTime: number) {
for (const camera of this.getAllCameras()) {
if (camera instanceof OrthographicCamera) {
const animation = new Tween(camera);
animation
.to(
{
zoom: camera.zoom * (1 / zoomFactor),
},
zoomTime,
)
.start();
camera.updateProjectionMatrix();
} else {
const animation = new Tween(camera.position);
animation
.to(
{
x: camera.position.x * zoomFactor,
y: camera.position.y * zoomFactor,
z: camera.position.z * zoomFactor,
},
zoomTime,
)
.start();
}
}
}
/**
* Move the camera to look at the object with the given uuid.
* @param uuid uuid of the object.
* @param objectsGroup Group of objects to be traversed for finding the object
* with the given uuid.
*/
public lookAtObject(
uuid: string,
objectsGroup: Object3D,
offset: number = 0,
) {
const origin = new Vector3(0, 0, 0);
const objectPosition = this.getObjectPosition(uuid, objectsGroup);
if (objectPosition) {
// Check if the object is away from the origin
for (const camera of this.getAllCameras()) {
// Moving the camera to the object's position and then zooming out
new Tween(camera.position)
.to(
{
x: objectPosition.x * 1.1 + offset,
y: objectPosition.y * 1.1 + offset,
z: objectPosition.z * 1.1 + offset,
},
200,
)
.start();
}
}
}
/**
* Get position of object from UUID.
* @param uuid UUID of the object.
* @param objectsGroup Objects group to look into for the object.
* @returns Position of the 3D object.
*/
public getObjectPosition(uuid: string, objectsGroup: Object3D): Vector3 {
const object = objectsGroup.getObjectByProperty('uuid', uuid) as any;
if (object) {
const origin = new Vector3(0, 0, 0);
let objectPosition = new Vector3();
if (object instanceof Group) {
// If it is a group of other event data we traverse through it
object.traverse((childObject: any) => {
// Make sure the child is not a group (e.g Track is a group)
if (childObject.children.length === 0) {
if (childObject.position.equals(origin)) {
// Get the max vector from the bounding box to accumulate with the clusters
if (childObject.geometry?.boundingSphere) {
try {
objectPosition.add(
childObject.geometry.boundingSphere.getBoundingBox().max,
);
} catch (e) {
objectPosition.add(
childObject.geometry.boundingSphere?.center,
);
}
}
} else {
objectPosition.add(childObject.position);
}
}
});
} else if (object.position.equals(origin)) {
// Get the center of bounding sphere of objects with no position
objectPosition = object.geometry?.boundingSphere?.center;
} else {
// Get the object position for all other elements
objectPosition = object.position;
}
return objectPosition;
} else {
return new Vector3();
}
}
/**
* Hide tube geometry of tracks on zoom if the camera is too close.
* (For visibility of vertices)
* @param scene Scene to look in for tracks.
* @param minRadius Radius after which the tube tracks should be invisible.
*/
public hideTubeTracksOnZoom(scene: Scene, minRadius: number) {
let tracksHidden = false;
const origin = new Vector3();
this.activeControls.addEventListener('change', (event) => {
const isCameraClose =
(event?.target?.object?.position as Vector3).distanceTo(origin) <
minRadius;
if (isCameraClose && !tracksHidden) {
scene.getObjectByName('Tracks')?.traverse((track) => {
if (
track.name === 'Track' &&
(track as Mesh).geometry instanceof TubeGeometry
) {
track.visible = false;
}
});
tracksHidden = true;
} else if (!isCameraClose && tracksHidden) {
scene.getObjectByName('Tracks')?.traverse((track) => {
if (
track.name === 'Track' &&
(track as Mesh).geometry instanceof TubeGeometry
) {
track.visible = true;
}
});
tracksHidden = false;
}
});
}
/**
* Synchronously update position of the orbit controls.
* @param controls Orbit controls whose position is to be updated.
*/
private positionSync(controls: OrbitControls): void {
controls.object.position.set(
this.activeControls.object.position.x,
this.activeControls.object.position.y,
this.activeControls.object.position.z,
);
// controls.update();
}
/**
* Synchronously update rotation of the orbit controls.
* @param controls Controls whose rotation is to be updated.
*/
private rotationSync(controls: OrbitControls): void {
controls.target = this.activeControls.target;
// controls.update();
}
/**
* Check if the list of orbit controls contains a specific orbit controls.
* @param obj Orbit controls to be checked for containment.
* @param list List of orbit controls.
* @returns If the list contains the orbit controls.
*/
private containsObject(obj: OrbitControls, list: OrbitControls[]): boolean {
for (const object of list) {
if (object === obj) {
return true;
}
}
return false;
}
/**
* Set up to make camera(s) adapt to window resize.
* @param rendererElement Canvas element of the main renderer.
*/
private setupResize(rendererElement: HTMLCanvasElement) {
window.addEventListener('resize', () => {
let mainCamera = this.getMainCamera() as any;
if (mainCamera.isOrthographicCamera) {
mainCamera = mainCamera as OrthographicCamera;
mainCamera.left = rendererElement.offsetWidth / -2;
mainCamera.right = rendererElement.offsetWidth / 2;
mainCamera.top = rendererElement.offsetHeight / 2;
mainCamera.bottom = rendererElement.offsetHeight / -2;
mainCamera.updateProjectionMatrix();
} else {
mainCamera = mainCamera as PerspectiveCamera;
mainCamera.aspect =
rendererElement.offsetWidth / rendererElement.offsetHeight;
mainCamera.updateProjectionMatrix();
}
});
}
/**
* Get the index of orbit controls from a list of orbit controls.
* @param obj Orbit controls whose index is to be obtained.
* @param list List of orbit controls.
* @returns Index of the orbit controls in the given list. Returns -1 if not found.
*/
private objectIndex(obj: OrbitControls, list: OrbitControls[]): number {
for (let i = 0; i < list.length; i++) {
if (list[i] === obj) {
return i;
}
}
return -1;
}
}