Skip to content

Commit

Permalink
added a new transformation and a sandCastle example
Browse files Browse the repository at this point in the history
  • Loading branch information
kaktus40 committed Jul 9, 2016
1 parent 2c7be6c commit 4aa7eee
Show file tree
Hide file tree
Showing 6 changed files with 662 additions and 198 deletions.
231 changes: 231 additions & 0 deletions Apps/Sandcastle/gallery/development/HeadingPitchRoll.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="description" content="Create 3D models using glTF.">
<meta name="cesium-sandcastle-labels" content="Development">
<title>Cesium Demo</title>
<script type="text/javascript" src="../Sandcastle-header.js"></script>
<script type="text/javascript" src="../../../ThirdParty/requirejs-2.1.20/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl: '../../../Source',
waitSeconds: 60
});
</script>
</head>

<body class="sandcastle-loading" data-sandcastle-bucket="bucket-requirejs.html">
<style>
@import url(../templates/bucket.css);
</style>
<div id="cesiumContainer" class="fullSize"></div>
<div id="loadingOverlay">
<h1>Loading...</h1></div>
<div id="toolbar">
<table>
<tbody>
<tr>
<td>Heading: <span id='heading'></span>°</td>
</tr>
<tr>
<td>&larr; to left/&rarr; to right</td>
</tr>
<tr>
<td>Pitch: <span id='pitch'></span>°</td>
</tr>
<tr>
<td>&uarr; to up/&darr; to down</td>
</tr>
<tr>
<td>roll: <span id='roll'></span>°</td>
</tr>
<tr>
<td>&larr; + &#8679; left/&rarr; + &#8679; right</td>
</tr>
<tr>
<td>Speed: <span id='speed'></span>m/s</td>
</tr>
<tr>
<td>&uarr; + &#8679; to speed up/&darr; + &#8679; to speed down</td>
</tr>
<tr>
<td>following aircraft
<input id='fromBehind' type='checkbox'></input>
</td>
</tr>

</tbody>
</table>
</div>
<script id="cesium_sandcastle_script">
function startup(Cesium) {
'use strict';
//Sandcastle_Begin
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
// viewer.extend(Cesium.viewerCesiumInspectorMixin);

var pathPosition = new Cesium.SampledPositionProperty();
var entityPath = viewer.entities.add({
position: pathPosition,
name: 'path',
path: {
show: true,
leadTime: 0,
trailTime: 60,
width: 10,
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.3,
color: Cesium.Color.PALEGOLDENROD
})
}
});

var camera = viewer.camera;
var controller = scene.screenSpaceCameraController;
var r = 0;
var center = new Cesium.Cartesian3();

var hpRoll = new Cesium.HeadingPitchRoll();
var hpRange = new Cesium.HeadingPitchRange();
var speed = 100.0;

var position = Cesium.Cartesian3.fromDegrees(-123.0744619, 44.0503706, 5000.0);
var speedVector = new Cesium.Cartesian3();

var planePrimitive = scene.primitives.add(Cesium.Model.fromGltf({
url: '../../SampleData/models/CesiumAir/Cesium_Air.glb',
modelMatrix: Cesium.Transforms.aircraftHeadingPitchRollToFixedFrame(position, hpRoll),
minimumPixelSize: 128
}));

planePrimitive.readyPromise.then(function(model) {
// Play and loop all animations at half-speed
model.activeAnimations.addAll({
speedup: 0.5,
loop: Cesium.ModelAnimationLoop.REPEAT
});

// Zoom to model
r = 2.0 * Math.max(model.boundingSphere.radius, camera.frustum.near);
controller.minimumZoomDistance = r * 0.5;
Cesium.Matrix4.multiplyByPoint(model.modelMatrix, model.boundingSphere.center, center);
var heading = Cesium.Math.toRadians(230.0);
var pitch = Cesium.Math.toRadians(-20.0);
hpRange.heading = heading;
hpRange.pitch = pitch;
hpRange.range = r * 50.0;
camera.lookAt(center, hpRange);
update();
}).otherwise(function(error) {
window.alert(error);
});

document.addEventListener("keypress", function(e) {
switch (e.key) {
case 'ArrowDown':
if (e.shiftKey) {
// speed down
speed -= 10;
if (speed < 10) {
speed = 10;
}
} else {
// pitch down until
hpRoll.pitch -= Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.pitch < -Cesium.Math.TWO_PI) {
hpRoll.pitch += Cesium.Math.TWO_PI;
}
}
break;
case 'ArrowUp':
if (e.shiftKey) {
// speed up
speed += 10;
if (speed > 340) {
speed = 340;
}
} else {
// pitch up until Math.PI/2
hpRoll.pitch += Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.pitch > Cesium.Math.TWO_PI) {
hpRoll.pitch -= Cesium.Math.TWO_PI;
}
}
break;
case 'ArrowRight':
if (e.shiftKey) {
// roll right until Math.PI/2
hpRoll.roll += Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.roll > Cesium.Math.TWO_PI) {
hpRoll.roll -= Cesium.Math.TWO_PI;
}
} else {
// turn right
hpRoll.heading += Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.heading > Cesium.Math.TWO_PI) {
hpRoll.heading -= Cesium.Math.TWO_PI;
}
}
break;
case 'ArrowLeft':
if (e.shiftKey) {
// roll left until
hpRoll.roll -= Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.roll < 0.0) {
hpRoll.roll += Cesium.Math.TWO_PI;
}
} else {
// turn left
hpRoll.heading -= Cesium.Math.RADIANS_PER_DEGREE;
if (hpRoll.heading < 0.0) {
hpRoll.heading += Cesium.Math.TWO_PI;
}
}
break;
default:
}
});

var headingSpan = document.getElementById('heading');
var pitchSpan = document.getElementById('pitch');
var rollSpan = document.getElementById('roll');
var speedSpan = document.getElementById('speed');

function update() {
headingSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.heading).toFixed(1);
pitchSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.pitch).toFixed(1);
rollSpan.innerHTML = Cesium.Math.toDegrees(hpRoll.roll).toFixed(1);
speedSpan.innerHTML = speed.toFixed(1);

speedVector = Cesium.Cartesian3.multiplyByScalar(Cesium.Cartesian3.UNIT_X, speed / 10, speedVector);
position = Cesium.Matrix4.multiplyByPoint(planePrimitive.modelMatrix, speedVector, position);
pathPosition.addSample(Cesium.JulianDate.now(), position);
Cesium.Transforms.aircraftHeadingPitchRollToFixedFrame(position, hpRoll,undefined,planePrimitive.modelMatrix);

if (document.getElementById('fromBehind').checked) {
// Zoom to model
Cesium.Matrix4.multiplyByPoint(planePrimitive.modelMatrix, planePrimitive.boundingSphere.center, center);
hpRange.heading = hpRoll.heading;
hpRange.pitch = hpRoll.pitch;
camera.lookAt(center, hpRange);
}
setTimeout(update, 100);
}
//Sandcastle_End
Sandcastle.finishedLoading();
}
if (typeof Cesium !== "undefined") {
startup(Cesium);
} else if (typeof require === "function") {
require(["Cesium"], startup);
}
</script>
</body>

</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 52 additions & 1 deletion Source/Core/Matrix3.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define([
'./defineProperties',
'./DeveloperError',
'./freezeObject',
'./HeadingPitchRoll',
'./Math'
], function(
Cartesian3,
Expand All @@ -14,6 +15,7 @@ define([
defineProperties,
DeveloperError,
freezeObject,
HeadingPitchRoll,
CesiumMath) {
'use strict';

Expand Down Expand Up @@ -302,6 +304,55 @@ define([
return result;
};

/**
* Computes a 3x3 rotation matrix from the provided headingPitchRoll. (see http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles )
*
* @param {HeadingPitchRoll} headingPitchRoll the headingPitchRoll to use.
* @param {Matrix3} [result] The object in which the result will be stored, if undefined a new instance will be created.
* @returns {Matrix3} The 3x3 rotation matrix from this headingPitchRoll.
*/
Matrix3.fromHeadingPitchRoll = function(headingPitchRoll, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(headingPitchRoll)) {
throw new DeveloperError('headingPitchRoll is required');
}
//>>includeEnd('debug');
var cosTheta=Math.cos(-headingPitchRoll.pitch);
var cosPsi=Math.cos(-headingPitchRoll.heading);
var cosPhi=Math.cos(headingPitchRoll.roll);
var sinTheta=Math.sin(-headingPitchRoll.pitch);
var sinPsi=Math.sin(-headingPitchRoll.heading);
var sinPhi=Math.sin(headingPitchRoll.roll);

var m00 = cosTheta*cosPsi;
var m01 = -cosPhi*sinPsi+sinPhi*sinTheta*cosPsi;
var m02 = sinPhi*sinPsi+cosPhi*sinTheta*cosPsi;

var m10 = cosTheta*sinPsi;
var m11 = cosPhi*cosPsi+sinPhi*sinTheta*sinPsi;
var m12 = -sinTheta*cosPhi+cosPhi*sinTheta*sinPsi;

var m20 = -sinTheta;
var m21 = sinPhi*cosTheta;
var m22 = cosPhi*cosTheta;

if (!defined(result)) {
return new Matrix3(m00, m01, m02,
m10, m11, m12,
m20, m21, m22);
}
result[0] = m00;
result[1] = m10;
result[2] = m20;
result[3] = m01;
result[4] = m11;
result[5] = m21;
result[6] = m02;
result[7] = m12;
result[8] = m22;
return result;
};

/**
* Computes a Matrix3 instance representing a non-uniform scale.
*
Expand Down Expand Up @@ -977,7 +1028,7 @@ define([
* @example
* // Instead of Cesium.Matrix3.multiply(m, Cesium.Matrix3.fromScale(scale), m);
* Cesium.Matrix3.multiplyByScale(m, scale, m);
*
*
* @see Matrix3.fromScale
* @see Matrix3.multiplyByUniformScale
*/
Expand Down
Loading

0 comments on commit 4aa7eee

Please sign in to comment.