-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorientation_demo.html
executable file
·365 lines (293 loc) · 12.3 KB
/
orientation_demo.html
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
<!DOCTYPE html>
<html><head>
<meta charset=UTF-8>
<title>Mobile Orientation Demo</title>
<script src="https://cesiumjs.org/Cesium/Build/Cesium/Cesium.js"></script>
<style>
@import url(https://cesiumjs.org/Cesium/Build/Cesium/Widgets/widgets.css);
html, body, #cesiumContainer {
width: 90%; height: 90%; margin: 25px; padding: 0;
}
</style>
</head>
<body>
<!--
// Ref:
// https://www.w3.org/TR/orientation-event/
// https://cesiumjs.org/Cesium/Build/Documentation/Camera.html
// https://w3c.github.io/deviceorientation/spec-source-orientation.html
// http://nghiaho.com/?page_id=846
// http://planning.cs.uiuc.edu/node101.html#fig:yawpitchroll
// https://cesiumjs.org/Cesium/Build/Documentation/Matrix3.html#.fromQuaternion
// https://www.reddit.com/r/GoogleCardboard/comments/2l54q6/what_percent_of_cardboard_users_experience_drift/
// http://electronics.stackexchange.com/questions/110506/dead-reckoning-with-accelerometer-gyro-possible
-->
<!--
Issues:
- Zooming as the first action if you are pointing down causes ceasium to crash...not sure if this is something that I am doing wrong or a bug in ceasium. Workaround either pan first before zooming or angle the camera up closer to the horizon before panning (seems to be less then about 40 deg that the issue presents).
- Not sure that running the rotation update in the tick is the most efficient mechanisim for updating the veiwport - its costing cycles even when it chooses not to update. But this is for proof-of-concept not final implementation.
- This method probably only works in Chrome is this a problem?
- How to implement features/options in a useable UI.
Further Ideas:
- Put a shadow (or dot or person) on the map at the ground height of where you are currently hovering so that you can see where you are hovering above.
-->
<!-- Debug: <span id="debug"> </span> <p/> -->
OPS: <span id="ops"> </span> (Orientations Per Second - Long Term Average) <p/>
Method: <span id="method"> </span> <p/>
Alpha: <span id="alpha"> </span> <p/>
Beta: <span id="beta"> </span> <p/>
Gamma: <span id="gamma"> </span> <p/>
<button id="locate" onclick="locate()" >Locate Me</button>
<button id="toggleHover" onclick="toggleHover()" value="false">Hover Toggle</button>
<button id="toggleOrientation" onclick="toggleOrientation()" value="true">Orientation Toggle</button>
<button id="reset" onclick="reset()" >Reset Orientation</button>
<div id="cesiumContainer"></div>
<script>
"use strict";
var viewer = new Cesium.Viewer('cesiumContainer');
// Set a suitable layer for debugging.
var layer_picker = viewer.baseLayerPicker;
var layers = layer_picker.viewModel.imageryProviderViewModels;
var selected_layer = layers[5];
layer_picker.viewModel.selectedImagery = selected_layer;
// Device Orientation
var alpha = 0;
var beta = 0;
var gamma = 0;
// Origin Offset
var last_start_heading = 0;
var last_start_alpha = 0;
// Flag indicating whether any of the orientation values have been updated.
var orientation_updated = true;
// Constants for the ground and hover height.
var ground_height = 20;
var hover_height = 250;
// "Framerate" Limiting (Maximum number of orientations per second).
var last_update = new Date();
var max_update_rate = 10;
// Operation Counter
var reorientations = 0;
var first_orientation = new Date();
// A few debug locations
//viewer.camera.position = Cesium.Cartesian3.fromDegrees(144.35690, -38.14688, 20);
viewer.camera.position = Cesium.Cartesian3.fromDegrees(149.123, -35.282, 20);
//
hoverToggled();
orientationToggled();
reset();
viewer.clock.onTick.addEventListener(tick);
if ('ondeviceorientationabsolute' in window)
{
window.addEventListener('deviceorientationabsolute', orientationUpdate);
document.getElementById("method").innerHTML = "Device Orientation Absolute";
}
else if ('ondeviceorientation' in window)
{
window.addEventListener('deviceorientation', orientationUpdate);
document.getElementById("method").innerHTML = "Device Orientation";
}
else
{
document.getElementById("method").innerHTML = "Device Orientation Not Supported";
}
if ('oncompassneedscalibration' in window)
{
document.getElementById("method").innerHTML += " (Compass Calibration Alert Supported.)";
window.addEventListener("compassneedscalibration", function(event)
{
alert("Device Recalibration Required!");
event.preventDefault();
}, true);
}
else
{
document.getElementById("method").innerHTML += " (Compass Calibration Alert Not Supported.)";
}
// TODO Fix when the device changes the rotation orientation automatically.
function locate()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(setPosition);
}
}
function toggleHover()
{
// Toggle State
if (document.getElementById("toggleHover").value == "true")
{
document.getElementById("toggleHover").value = "false";
}
else
{
document.getElementById("toggleHover").value = "true";
}
hoverToggled();
}
function toggleOrientation()
{
// Toggle State
if (document.getElementById("toggleOrientation").value == "true")
{
document.getElementById("toggleOrientation").value = "false";
}
else
{
document.getElementById("toggleOrientation").value = "true";
}
orientationToggled();
}
function reset()
{
last_start_alpha = 0;
last_start_heading = 0;
orientation_updated = true;
}
function hoverToggled()
{
// Update UI
if (document.getElementById("toggleHover").value == "false")
{
document.getElementById("toggleHover").innerHTML = "Hover";
}
else
{
document.getElementById("toggleHover").innerHTML = "Ground";
}
var height = ground_height;
if (document.getElementById("toggleHover").value == "true")
{
height = hover_height;
}
// Reset the viewer height.
var original_position = viewer.camera.positionCartographic;
viewer.camera.position = Cesium.Cartesian3.fromRadians(original_position.longitude, original_position.latitude, height);
}
function orientationToggled()
{
// Update State
if (document.getElementById("toggleOrientation").value == "true")
{
last_start_alpha = alpha;
last_start_heading = Cesium.Math.toDegrees(viewer.camera.heading);
orientation_updated = true;
}
// Update UI
if (document.getElementById("toggleOrientation").value == "false")
{
document.getElementById("toggleOrientation").innerHTML = "Start Orientation";
}
else
{
document.getElementById("toggleOrientation").innerHTML = "Stop Orientation";
}
}
function orientationUpdate(event)
{
alpha = event.alpha;
beta = event.beta;
gamma = event.gamma;
orientation_updated = true;
document.getElementById("alpha").innerHTML = alpha;
document.getElementById("beta").innerHTML = beta;
document.getElementById("gamma").innerHTML = gamma;
}
function setPosition(position)
{
var height = ground_height;
if (document.getElementById("toggleHover").value == "true")
{
height = hover_height;
}
// TODO check that the position is not zero zero.
viewer.camera.position = Cesium.Cartesian3.fromDegrees(position.coords.longitude, position.coords.latitude, height);
}
function screenOrientation()
{
if (('orientation' in screen) && ('angle' in screen.orientation))
{
return screen.orientation.angle;
}
if ('orientation' in window)
{
return window.orientation;
}
return 0;
}
function tick(clock)
{
if (document.getElementById("toggleOrientation").value != "true")
{
// We are not updating the camera orientation / Reinitalise.
reorientations = 0;
document.getElementById("ops").innerHTML = "Stopped";
}
else // If we are updating the camera orientation
{
// If a sufficent amount of time has elapsed such that we are below the update rate cap. Also check that we are only updating if the values have changed.
if (((Date.now() - last_update) >= 1000/max_update_rate) && orientation_updated)
{
// Mark that we are updating now.
last_update = Date.now();
orientation_updated = false;
// Collect performance metrics
if (reorientations == 0)
{
first_orientation = new Date();
}
reorientations++;
// Display performace metrics
document.getElementById("ops").innerHTML = reorientations/((Date.now()-first_orientation)/1000);
// TODO replace this with a nicer initalisation...this works but is not particularly clean.
var rotation_vector = [1, 0, 0, 0, 1, 0, 0, 0, 1];
var r = Cesium.Matrix3.fromArray(rotation_vector);
var roll_increment;
// Roll - Counteract the screen rotation when the screen is rotated and the rotation lock is not on so the browser reorients the screen.
roll_increment = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(screenOrientation()));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Pitch - Align the device orientation frame with the ceasium orientation frame.
roll_increment = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(90));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Roll - Apply the deivce roll.
roll_increment = Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(gamma));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Pitch - Apply the deivce pitch.
roll_increment = Cesium.Matrix3.fromRotationX(Cesium.Math.toRadians(-beta));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Heading - Apply the incremental deivce heading (from when start was last triggered).
roll_increment = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(-(alpha-last_start_alpha)));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Heading - Use the offset when the orientation was last started.
roll_increment = Cesium.Matrix3.fromRotationY(Cesium.Math.toRadians(last_start_heading));
Cesium.Matrix3.multiply(r, roll_increment, r);
// Relable the matrix elements to match with the math formulation.
// TODO Insert documentation here on the math.
rotation_vector = Cesium.Matrix3.toArray(r);
var r11 = rotation_vector[0];
var r12 = rotation_vector[3];
var r13 = rotation_vector[6];
var r21 = rotation_vector[1];
var r22 = rotation_vector[4];
var r23 = rotation_vector[7];
var r31 = rotation_vector[2];
var r32 = rotation_vector[5];
var r33 = rotation_vector[8];
var heading = Cesium.Math.toDegrees(Math.atan2(-r31, r33));
var roll = Cesium.Math.toDegrees(Math.atan2(-r12, r22));
var pitch = Cesium.Math.toDegrees(Math.atan2(-r32, Math.sqrt(r31*r31 + r33*r33)));
// Set the orientation of the camera.
viewer.camera.setView(
{
orientation:
{
roll : Cesium.Math.toRadians(roll),
pitch : Cesium.Math.toRadians(pitch),
heading : Cesium.Math.toRadians(heading)
}
});
}
}
}
</script>
</body>
</html>