/
Spherical.js
217 lines (197 loc) · 7.75 KB
/
Spherical.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
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
/*global define*/
define([
'./DeveloperError',
'./defaultValue'
], function(
DeveloperError,
defaultValue) {
"use strict";
/**
* A set of curvilinear 3-dimensional coordinates.
*
* @alias Spherical
* @constructor
*
* @param {Number} [clock=0.0] The angular coordinate lying in the xy-plane measured from the positive x-axis and toward the positive y-axis.
* @param {Number} [cone=0.0] The angular coordinate measured from the positive z-axis and toward the negative z-axis.
* @param {Number} [magnitude=1.0] The linear coordinate measured from the origin.
*/
var Spherical = function(clock, cone, magnitude) {
this.clock = defaultValue(clock, 0.0);
this.cone = defaultValue(cone, 0.0);
this.magnitude = defaultValue(magnitude, 1.0);
};
/**
* Converts the provided Cartesian3 into Spherical coordinates.
* @memberof Spherical
*
* @param {Cartesian3} cartesian3 The Cartesian3 to be converted to Spherical.
* @param {Spherical} [spherical] The object in which the result will be stored, if undefined a new instance will be created.
*
* @returns The modified result parameter, or a new instance if one was not provided.
*
* @exception {DeveloperError} cartesian3 is required.
*/
Spherical.fromCartesian3 = function(cartesian3, result) {
if (typeof cartesian3 === 'undefined') {
throw new DeveloperError('cartesian3 is required');
}
var x = cartesian3.x;
var y = cartesian3.y;
var z = cartesian3.z;
var radialSquared = x * x + y * y;
if (typeof result === 'undefined') {
result = new Spherical();
}
result.clock = Math.atan2(y, x);
result.cone = Math.atan2(Math.sqrt(radialSquared), z);
result.magnitude = Math.sqrt(radialSquared + z * z);
return result;
};
/**
* Creates a duplicate of a Spherical.
* @memberof Spherical
*
* @param {Spherical} spherical The spherical to clone.
* @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
*
* @return The modified result parameter or a new instance if result was undefined. (Returns undefined if spherical is undefined)
*/
Spherical.clone = function(spherical, result) {
if (typeof spherical === 'undefined') {
return undefined;
}
if (typeof result === 'undefined') {
return new Spherical(spherical.clock, spherical.cone, spherical.magnitude);
}
result.clock = spherical.clock;
result.cone = spherical.cone;
result.magnitude = spherical.magnitude;
return result;
};
/**
* Computes the normalized version of the provided spherical.
* @memberof Spherical
*
* @param {Spherical} spherical The spherical to be normalized.
* @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
*
* @return The modified result parameter or a new instance if result was undefined.
*
* @exception {DeveloperError} spherical is required.
*/
Spherical.normalize = function(spherical, result) {
if (typeof spherical === 'undefined') {
throw new DeveloperError('spherical is required');
}
if (typeof result === 'undefined') {
return new Spherical(spherical.clock, spherical.cone, 1.0);
}
result.clock = spherical.clock;
result.cone = spherical.cone;
result.magnitude = 1.0;
return result;
};
/**
* Returns true if the first spherical is equal to the second spherical, false otherwise.
* @memberof Spherical
*
* @param {Spherical} left The first Spherical to be compared.
* @param {Spherical} right The second Spherical to be compared.
*
* @return true if the first spherical is equal to the second spherical, false otherwise.
*/
Spherical.equals = function(left, right) {
return (left === right) ||
((typeof left !== 'undefined') &&
(typeof right !== 'undefined') &&
(left.clock === right.clock) &&
(left.cone === right.cone) &&
(left.magnitude === right.magnitude));
};
/**
* Returns true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
* @memberof Spherical
*
* @param {Spherical} left The first Spherical to be compared.
* @param {Spherical} right The second Spherical to be compared.
* @param {Number} [epsilon=0.0] The epsilon to compare against.
*
* @return true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
*/
Spherical.equalsEpsilon = function(left, right, epsilon) {
epsilon = defaultValue(epsilon, 0.0);
return (left === right) ||
((typeof left !== 'undefined') &&
(typeof right !== 'undefined') &&
(Math.abs(left.clock - right.clock) <= epsilon) &&
(Math.abs(left.cone - right.cone) <= epsilon) &&
(Math.abs(left.magnitude - right.magnitude) <= epsilon));
};
/**
* Returns a string representing the provided instance in the format (clock, cone, magnitude).
* @memberof Spherical
*
* @param {Spherical} spherical The object to be converted.
*
* @return A string representing the provided instance.
*/
Spherical.toString = function(spherical) {
return '(' + spherical.clock + ', ' + spherical.cone + ', ' + spherical.magnitude + ')';
};
/**
* Creates a duplicate of this Spherical.
* @memberof Spherical
*
* @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
*
* @return The modified result parameter or a new instance if result was undefined.
*/
Spherical.prototype.clone = function(result) {
return Spherical.clone(this, result);
};
/**
* Computes the normalized version of this spherical.
* @memberof Spherical
*
* @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
*
* @return The modified result parameter or a new instance if result was undefined.
*/
Spherical.prototype.normalize = function(result) {
return Spherical.normalize(this, result);
};
/**
* Returns true if this spherical is equal to the provided spherical, false otherwise.
* @memberof Spherical
*
* @param {Spherical} other The Spherical to be compared.
*
* @return true if this spherical is equal to the provided spherical, false otherwise.
*/
Spherical.prototype.equals = function(other) {
return Spherical.equals(this, other);
};
/**
* Returns true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
* @memberof Spherical
*
* @param {Spherical} other The Spherical to be compared.
* @param {Number} epsilon The epsilon to compare against.
*
* @return true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
*/
Spherical.prototype.equalsEpsilon = function(other, epsilon) {
return Spherical.equalsEpsilon(this, other, epsilon);
};
/**
* Returns a string representing this instance in the format (clock, cone, magnitude).
* @memberof Spherical
*
* @return A string representing this instance.
*/
Spherical.prototype.toString = function() {
return Spherical.toString(this);
};
return Spherical;
});