forked from nmccready/cesium-navigation
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.js
101 lines (88 loc) · 2.68 KB
/
Utils.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
import { defined, Ray, Cartesian3, Cartographic, SceneMode } from 'cesium';
import { convertLength } from '@turf/helpers';
const unprojectedScratch = new Cartographic();
const rayScratch = new Ray();
/**
* gets the focus point of the camera
* @param {Viewer|Widget} terria The terria
* @param {boolean} inWorldCoordinates true to get the focus in world coordinates,
* otherwise get it in projection-specific map coordinates, in meters.
* @param {Cartesian3} [result] The object in which the result will be stored.
* @return {Cartesian3} The modified result parameter, a new instance if none
* was provided or undefined if there is no focus point.
*/
export function getCameraFocus(terria, inWorldCoordinates, _result) {
let result = _result;
const { scene } = terria;
const { camera } = scene;
if (scene.mode === SceneMode.MORPHING) {
return undefined;
}
if (!defined(result)) {
result = new Cartesian3();
}
// TODO bug when tracking: if entity moves the current position
// should be used and not only the one when starting orbiting/rotating
// TODO bug when tracking: reset should reset to default view of tracked entity
if (defined(terria.trackedEntity)) {
result = terria.trackedEntity.position.getValue(
terria.clock.currentTime,
result
);
} else {
rayScratch.origin = camera.positionWC;
rayScratch.direction = camera.directionWC;
result = scene.globe.pick(rayScratch, scene, result);
}
if (!defined(result)) {
return undefined;
}
if (
scene.mode === SceneMode.SCENE2D ||
scene.mode === SceneMode.COLUMBUS_VIEW
) {
result = camera.worldToCameraCoordinatesPoint(result, result);
if (inWorldCoordinates) {
result = scene.globe.ellipsoid.cartographicToCartesian(
scene.mapProjection.unproject(result, unprojectedScratch),
result
);
}
} else {
if (!inWorldCoordinates) {
result = camera.worldToCameraCoordinatesPoint(result, result);
}
}
return result;
}
export const UNITS_TO_ABBREVIATION = {
meters: 'm',
millimeters: 'mm',
centimeters: 'cm',
kilometers: 'km',
acres: 'ac',
miles: 'mi',
nauticalmiles: 'nm',
inches: 'inch',
yards: 'yd',
feet: 'ft',
radians: 'rad',
degrees: 'deg'
};
/**
* @param {Number} length
* @param {TufHelper.Units} units
*/
export function distanceLabelFormatter(length, units) {
let fixed = 1;
if (length < 1) {
fixed = 2;
if (units === 'kilometers') {
/* eslint-disable no-param-reassign */
units = 'meters';
length = convertLength(length, 'kilometers', units);
/* eslint-enable no-param-reassign */
}
}
return `${length.toFixed(fixed)} ${UNITS_TO_ABBREVIATION[units]}`;
}