Skip to content

Commit 03bc20f

Browse files
authored
Merge pull request #4896 from kaktus40/fixedFrameConverter
Fixed frame converter
2 parents 4bcc68f + 1ca538a commit 03bc20f

13 files changed

+742
-415
lines changed

Apps/Sandcastle/gallery/development/HeadingPitchRoll.html Apps/Sandcastle/gallery/HeadingPitchRoll.html

+17-16
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="utf-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
7-
<meta name="description" content="Create 3D models using glTF.">
8-
<meta name="cesium-sandcastle-labels" content="Development">
7+
<meta name="description" content="Use HeadingPitchRoll">
8+
<meta name="cesium-sandcastle-labels" content="Tutorials">
99
<title>Cesium Demo</title>
1010
<script type="text/javascript" src="../Sandcastle-header.js"></script>
1111
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
@@ -70,9 +70,10 @@ <h1>Loading...</h1>
7070
var viewer = new Cesium.Viewer('cesiumContainer');
7171
var canvas = viewer.canvas;
7272
canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
73-
canvas.onclick = function() {
74-
canvas.focus();
75-
};
73+
canvas.addEventListener('click', function() {
74+
canvas.focus();
75+
});
76+
canvas.focus();
7677

7778
var scene = viewer.scene;
7879

@@ -101,13 +102,15 @@ <h1>Loading...</h1>
101102
var hpRoll = new Cesium.HeadingPitchRoll();
102103
var hpRange = new Cesium.HeadingPitchRange();
103104
var speed = 10;
105+
var deltaRadians = Cesium.Math.toRadians(3.0);
104106

105107
var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 5000.0);
106108
var speedVector = new Cesium.Cartesian3();
109+
var fixedFrameTransform = Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west');
107110

108111
var planePrimitive = scene.primitives.add(Cesium.Model.fromGltf({
109112
url : '../../SampleData/models/CesiumAir/Cesium_Air.glb',
110-
modelMatrix : Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll),
113+
modelMatrix : Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform),
111114
minimumPixelSize : 128
112115
}));
113116

@@ -128,19 +131,16 @@ <h1>Loading...</h1>
128131
hpRange.pitch = pitch;
129132
hpRange.range = r * 50.0;
130133
camera.lookAt(center, hpRange);
131-
update();
132134
});
133135

134-
document.addEventListener('keyup', function(e) {
135-
var deltaRadians = Cesium.Math.toRadians(3.0);
136-
136+
document.addEventListener('keydown', function(e) {
137137
switch (e.keyCode) {
138138
case 40:
139139
if (e.shiftKey) {
140140
// speed down
141141
speed = Math.max(--speed, 1);
142142
} else {
143-
// pitch down until
143+
// pitch down
144144
hpRoll.pitch -= deltaRadians;
145145
if (hpRoll.pitch < -Cesium.Math.TWO_PI) {
146146
hpRoll.pitch += Cesium.Math.TWO_PI;
@@ -150,9 +150,9 @@ <h1>Loading...</h1>
150150
case 38:
151151
if (e.shiftKey) {
152152
// speed up
153-
speed = Math.max(speed++, 100);
153+
speed = Math.min(++speed, 100);
154154
} else {
155-
// pitch up until Math.PI/2
155+
// pitch up
156156
hpRoll.pitch += deltaRadians;
157157
if (hpRoll.pitch > Cesium.Math.TWO_PI) {
158158
hpRoll.pitch -= Cesium.Math.TWO_PI;
@@ -161,7 +161,7 @@ <h1>Loading...</h1>
161161
break;
162162
case 39:
163163
if (e.shiftKey) {
164-
// roll right until Math.PI/2
164+
// roll right
165165
hpRoll.roll += deltaRadians;
166166
if (hpRoll.roll > Cesium.Math.TWO_PI) {
167167
hpRoll.roll -= Cesium.Math.TWO_PI;
@@ -208,7 +208,7 @@ <h1>Loading...</h1>
208208
speedVector = Cesium.Cartesian3.multiplyByScalar(Cesium.Cartesian3.UNIT_X, speed / 10, speedVector);
209209
position = Cesium.Matrix4.multiplyByPoint(planePrimitive.modelMatrix, speedVector, position);
210210
pathPosition.addSample(Cesium.JulianDate.now(), position);
211-
Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, undefined, planePrimitive.modelMatrix);
211+
Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, fixedFrameTransform, planePrimitive.modelMatrix);
212212

213213
if (fromBehind.checked) {
214214
// Zoom to model
@@ -217,7 +217,8 @@ <h1>Loading...</h1>
217217
hpRange.pitch = hpRoll.pitch;
218218
camera.lookAt(center, hpRange);
219219
}
220-
});//Sandcastle_End
220+
});
221+
//Sandcastle_End
221222
Sandcastle.finishedLoading();
222223
}
223224
if (typeof Cesium !== "undefined") {
13.3 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
7+
<meta name="description" content="Use localFrameToFixedFrameGenerator for adequate rotation.">
8+
<meta name="cesium-sandcastle-labels" content="Tutorials">
9+
<title>Cesium Demo</title>
10+
<script type="text/javascript" src="../Sandcastle-header.js"></script>
11+
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
12+
<script type="text/javascript">
13+
require.config({
14+
baseUrl : '../../../Source',
15+
waitSeconds : 60
16+
});
17+
</script>
18+
</head>
19+
<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
20+
<style>
21+
@import url(../templates/bucket.css);
22+
</style>
23+
<div id="cesiumContainer" class="fullSize"></div>
24+
<div id="loadingOverlay">
25+
<h1>Loading...</h1>
26+
</div>
27+
<div id="toolbar">
28+
<table>
29+
<tbody>
30+
<tr>
31+
<td>Click on the 3D window then use the keyboard to change settings.</td>
32+
</tr>
33+
<tr>
34+
<td>Heading: <span id="heading"></span>°</td>
35+
</tr>
36+
<tr>
37+
<td>← to left/→ to right</td>
38+
</tr>
39+
<tr>
40+
<td>Pitch: <span id="pitch"></span>°</td>
41+
</tr>
42+
<tr>
43+
<td>↑ to up/↓ to down</td>
44+
</tr>
45+
<tr>
46+
<td>roll: <span id="roll"></span>°</td>
47+
</tr>
48+
<tr>
49+
<td>← + ⇧ left/→ + ⇧ right</td>
50+
</tr>
51+
</tbody>
52+
</table>
53+
</div>
54+
<script id="cesium_sandcastle_script">
55+
function startup(Cesium) {
56+
'use strict';
57+
//Sandcastle_Begin
58+
var viewer = new Cesium.Viewer('cesiumContainer');
59+
var canvas = viewer.canvas;
60+
canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
61+
canvas.addEventListener('click', function() {
62+
canvas.focus();
63+
});
64+
canvas.focus();
65+
66+
var scene = viewer.scene;
67+
var camera = viewer.camera;
68+
var controller = scene.screenSpaceCameraController;
69+
var r = 0;
70+
var center = new Cesium.Cartesian3();
71+
72+
var hpRoll = new Cesium.HeadingPitchRoll();
73+
var hpRange = new Cesium.HeadingPitchRange();
74+
var deltaRadians = Cesium.Math.toRadians(1.0);
75+
76+
var localFrames = [
77+
{
78+
pos : Cesium.Cartesian3.fromDegrees(-123.075, 44.045000, 5000.0),
79+
converter : Cesium.Transforms.eastNorthUpToFixedFrame,
80+
comments : 'Classical East North Up\nlocal Frame'
81+
},
82+
{
83+
pos : Cesium.Cartesian3.fromDegrees(-123.075, 44.050000, 5500.0),
84+
converter : Cesium.Transforms.localFrameToFixedFrameGenerator('north', 'west'),
85+
comments : 'North West Up\nlocal Frame'
86+
},
87+
{
88+
pos : Cesium.Cartesian3.fromDegrees(-123.075, 44.040000, 4500.0),
89+
converter : Cesium.Transforms.localFrameToFixedFrameGenerator('south', 'up'),
90+
comments : 'South Up West\nlocal Frame'
91+
},
92+
{
93+
pos : Cesium.Cartesian3.fromDegrees(-123.075, 44.050000, 4500.0),
94+
converter : Cesium.Transforms.localFrameToFixedFrameGenerator('up', 'east'),
95+
comments : 'Up East North\nlocal Frame'
96+
},
97+
{
98+
pos : Cesium.Cartesian3.fromDegrees(-123.075, 44.040000, 5500.0),
99+
converter : Cesium.Transforms.localFrameToFixedFrameGenerator('down', 'east'),
100+
comments : 'Down East South\nlocal Frame'
101+
},
102+
];
103+
104+
var primitives = [];
105+
var hprRollZero = new Cesium.HeadingPitchRoll();
106+
107+
for (var i = 0; i < localFrames.length; i++) {
108+
var position = localFrames[i].pos;
109+
var converter = localFrames[i].converter;
110+
var comments = localFrames[i].comments;
111+
var planePrimitive = scene.primitives.add(Cesium.Model.fromGltf({
112+
url : '../../SampleData/models/CesiumAir/Cesium_Air.glb',
113+
modelMatrix : Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, converter),
114+
minimumPixelSize : 128
115+
}));
116+
117+
primitives.push({primitive : planePrimitive, converter : converter, position : position});
118+
var modelMatrix = Cesium.Transforms.headingPitchRollToFixedFrame(position, hprRollZero, Cesium.Ellipsoid.WGS84, converter);
119+
scene.primitives.add(new Cesium.DebugModelMatrixPrimitive({
120+
modelMatrix : modelMatrix,
121+
length : 300.0,
122+
width : 10.0
123+
}));
124+
125+
var positionLabel = position.clone();
126+
positionLabel.z = position.z + 300.0;
127+
viewer.entities.add({
128+
position : positionLabel,
129+
label : {
130+
text : comments,
131+
font : '24px Helvetica',
132+
fillColor : Cesium.Color.SKYBLUE,
133+
outlineColor : Cesium.Color.BLACK,
134+
outlineWidth : 2,
135+
style : Cesium.LabelStyle.FILL_AND_OUTLINE,
136+
verticalOrigin : Cesium.VerticalOrigin.CENTER,
137+
HorizontalOrigin : Cesium.HorizontalOrigin.RIGHT
138+
}
139+
});
140+
}
141+
142+
primitives[0].primitive.readyPromise.then(function(model) {
143+
// Play and loop all animations at half-speed
144+
model.activeAnimations.addAll({
145+
speedup : 0.5,
146+
loop : Cesium.ModelAnimationLoop.REPEAT
147+
});
148+
149+
// Zoom to model
150+
r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
151+
controller.minimumZoomDistance = r * 0.5;
152+
Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, center);
153+
var heading = Cesium.Math.toRadians(90.0);
154+
var pitch = Cesium.Math.toRadians(0.0);
155+
hpRange.heading = heading;
156+
hpRange.pitch = pitch;
157+
hpRange.range = r * 100.0;
158+
camera.lookAt(center, hpRange);
159+
});
160+
161+
document.addEventListener('keydown', function(e) {
162+
switch (e.keyCode) {
163+
case 40:
164+
// pitch down
165+
hpRoll.pitch -= deltaRadians;
166+
if (hpRoll.pitch < -Cesium.Math.TWO_PI) {
167+
hpRoll.pitch += Cesium.Math.TWO_PI;
168+
}
169+
break;
170+
case 38:
171+
// pitch up
172+
hpRoll.pitch += deltaRadians;
173+
if (hpRoll.pitch > Cesium.Math.TWO_PI) {
174+
hpRoll.pitch -= Cesium.Math.TWO_PI;
175+
}
176+
break;
177+
case 39:
178+
if (e.shiftKey) {
179+
// roll right
180+
hpRoll.roll += deltaRadians;
181+
if (hpRoll.roll > Cesium.Math.TWO_PI) {
182+
hpRoll.roll -= Cesium.Math.TWO_PI;
183+
}
184+
} else {
185+
// turn right
186+
hpRoll.heading += deltaRadians;
187+
if (hpRoll.heading > Cesium.Math.TWO_PI) {
188+
hpRoll.heading -= Cesium.Math.TWO_PI;
189+
}
190+
}
191+
break;
192+
case 37:
193+
if (e.shiftKey) {
194+
// roll left until
195+
hpRoll.roll -= deltaRadians;
196+
if (hpRoll.roll < 0.0) {
197+
hpRoll.roll += Cesium.Math.TWO_PI;
198+
}
199+
} else {
200+
// turn left
201+
hpRoll.heading -= deltaRadians;
202+
if (hpRoll.heading < 0.0) {
203+
hpRoll.heading += Cesium.Math.TWO_PI;
204+
}
205+
}
206+
break;
207+
default:
208+
}
209+
});
210+
211+
var headingSpan = document.getElementById('heading');
212+
var pitchSpan = document.getElementById('pitch');
213+
var rollSpan = document.getElementById('roll');
214+
215+
viewer.scene.preRender.addEventListener(function(scene, time) {
216+
headingSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.heading).toFixed(1);
217+
pitchSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.pitch).toFixed(1);
218+
rollSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.roll).toFixed(1);
219+
220+
for (var i = 0; i < primitives.length; i++) {
221+
var primitive = primitives[i].primitive;
222+
var converter = primitives[i].converter;
223+
var position = primitives[i].position;
224+
Cesium.Transforms.headingPitchRollToFixedFrame(position, hpRoll, Cesium.Ellipsoid.WGS84, converter, primitive.modelMatrix);
225+
}
226+
});
227+
//Sandcastle_End
228+
Sandcastle.finishedLoading();
229+
}
230+
if (typeof Cesium !== "undefined") {
231+
startup(Cesium);
232+
} else if (typeof require === "function") {
233+
require(["Cesium"], startup);
234+
}
235+
</script>
236+
</body>
237+
</html>
27.7 KB
Loading

Apps/Sandcastle/gallery/development/3D Models Node Explorer.html

+8-7
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
return transformations[nodeName];
147147
});
148148

149-
// these writable computed properties produce individual values for use in the UI
149+
// these writable computed properties produce individual values for use in the UI
150150
['translationX', 'translationY', 'translationZ', 'rotationHeading', 'rotationPitch', 'rotationRoll', 'scaleX', 'scaleY', 'scaleZ'].forEach(function(p) {
151151
Cesium.knockout.defineProperty(viewModel, p, {
152152
get: function() {
@@ -155,7 +155,7 @@
155155
set: function(value) {
156156
// coerce values to number
157157
viewModel.transformation[p] = +value;
158-
}
158+
}
159159
});
160160
});
161161

@@ -164,7 +164,8 @@
164164
return new Cesium.Cartesian3(viewModel.translationX, viewModel.translationY, viewModel.translationZ);
165165
});
166166
Cesium.knockout.defineProperty(viewModel, 'rotation', function() {
167-
return Cesium.Quaternion.fromHeadingPitchRoll(viewModel.rotationHeading, viewModel.rotationPitch, viewModel.rotationRoll);
167+
var hpr = new Cesium.HeadingPitchRoll(viewModel.rotationHeading, viewModel.rotationPitch, viewModel.rotationRoll);
168+
return Cesium.Quaternion.fromHeadingPitchRoll(hpr);
168169
});
169170
Cesium.knockout.defineProperty(viewModel, 'scale', function() {
170171
return new Cesium.Cartesian3(viewModel.scaleX, viewModel.scaleY, viewModel.scaleZ);
@@ -192,17 +193,17 @@
192193

193194
model.readyPromise.then(function(model) {
194195
var camera = viewer.camera;
195-
196+
196197
// Zoom to model
197198
var controller = scene.screenSpaceCameraController;
198199
var r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
199200
controller.minimumZoomDistance = r * 0.5;
200-
201+
201202
var center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, new Cesium.Cartesian3());
202203
var heading = Cesium.Math.toRadians(230.0);
203204
var pitch = Cesium.Math.toRadians(-20.0);
204205
camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 2.0));
205-
206+
206207
// enumerate nodes and add options
207208
var options = Object.keys(model._runtime.nodesByName).map(function(nodeName) {
208209
return {
@@ -214,7 +215,7 @@
214215
});
215216
options[0].onselect();
216217
Sandcastle.addToolbarMenu(options);
217-
218+
218219
// respond to viewmodel changes by applying the computed matrix
219220
Cesium.knockout.getObservable(viewModel, 'matrix').subscribe(function(newValue) {
220221
var node = model.getNode(viewModel.nodeName);
Binary file not shown.

0 commit comments

Comments
 (0)