/
Rotation.js
executable file
·150 lines (137 loc) · 5.74 KB
/
Rotation.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
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
define([
'../Core/defaultValue',
'../Core/defined',
'../Core/DeveloperError',
'../Core/Math'
], function(
defaultValue,
defined,
DeveloperError,
CesiumMath) {
'use strict';
/**
* Represents a {@link Packable} number that always interpolates values
* towards the shortest angle of rotation. This object is never used directly
* but is instead passed to the constructor of {@link SampledProperty}
* in order to represent a two-dimensional angle of rotation.
*
* @exports Rotation
*
*
* @example
* var time1 = Cesium.JulianDate.fromIso8601('2010-05-07T00:00:00');
* var time2 = Cesium.JulianDate.fromIso8601('2010-05-07T00:01:00');
* var time3 = Cesium.JulianDate.fromIso8601('2010-05-07T00:02:00');
*
* var property = new Cesium.SampledProperty(Cesium.Rotation);
* property.addSample(time1, 0);
* property.addSample(time3, Cesium.Math.toRadians(350));
*
* //Getting the value at time2 will equal 355 degrees instead
* //of 175 degrees (which is what you get if you construct
* //a SampledProperty(Number) instead. Note, the actual
* //return value is in radians, not degrees.
* property.getValue(time2);
*
* @see PackableForInterpolation
*/
var Rotation = {
/**
* The number of elements used to pack the object into an array.
* @type {Number}
*/
packedLength : 1,
/**
* Stores the provided instance into the provided array.
*
* @param {Rotation} value The value to pack.
* @param {Number[]} array The array to pack into.
* @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
*
* @returns {Number[]} The array that was packed into
*/
pack : function(value, array, startingIndex) {
//>>includeStart('debug', pragmas.debug);
if (!defined(value)) {
throw new DeveloperError('value is required');
}
if (!defined(array)) {
throw new DeveloperError('array is required');
}
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
array[startingIndex] = value;
return array;
},
/**
* Retrieves an instance from a packed array.
*
* @param {Number[]} array The packed array.
* @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
* @param {Rotation} [result] The object into which to store the result.
* @returns {Rotation} The modified result parameter or a new Rotation instance if one was not provided.
*/
unpack : function(array, startingIndex, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(array)) {
throw new DeveloperError('array is required');
}
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
return array[startingIndex];
},
/**
* Converts a packed array into a form suitable for interpolation.
*
* @param {Number[]} packedArray The packed array.
* @param {Number} [startingIndex=0] The index of the first element to be converted.
* @param {Number} [lastIndex=packedArray.length] The index of the last element to be converted.
* @param {Number[]} result The object into which to store the result.
*/
convertPackedArrayForInterpolation : function(packedArray, startingIndex, lastIndex, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(packedArray)) {
throw new DeveloperError('packedArray is required');
}
//>>includeEnd('debug');
startingIndex = defaultValue(startingIndex, 0);
lastIndex = defaultValue(lastIndex, packedArray.length);
var previousValue;
for (var i = 0, len = lastIndex - startingIndex + 1; i < len; i++) {
var value = packedArray[startingIndex + i];
if (i === 0 || Math.abs(previousValue - value) < Math.PI) {
result[i] = value;
} else {
result[i] = value - CesiumMath.TWO_PI;
}
previousValue = value;
}
},
/**
* Retrieves an instance from a packed array converted with {@link Rotation.convertPackedArrayForInterpolation}.
*
* @param {Number[]} array The array previously packed for interpolation.
* @param {Number[]} sourceArray The original packed array.
* @param {Number} [firstIndex=0] The firstIndex used to convert the array.
* @param {Number} [lastIndex=packedArray.length] The lastIndex used to convert the array.
* @param {Rotation} [result] The object into which to store the result.
* @returns {Rotation} The modified result parameter or a new Rotation instance if one was not provided.
*/
unpackInterpolationResult : function(array, sourceArray, firstIndex, lastIndex, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(array)) {
throw new DeveloperError('array is required');
}
if (!defined(sourceArray)) {
throw new DeveloperError('sourceArray is required');
}
//>>includeEnd('debug');
result = array[0];
if (result < 0) {
return result + CesiumMath.TWO_PI;
}
return result;
}
};
return Rotation;
});