Source: Core/Spherical.js

  1. /*global define*/
  2. define([
  3. './defaultValue',
  4. './defined',
  5. './DeveloperError'
  6. ], function(
  7. defaultValue,
  8. defined,
  9. DeveloperError) {
  10. 'use strict';
  11. /**
  12. * A set of curvilinear 3-dimensional coordinates.
  13. *
  14. * @alias Spherical
  15. * @constructor
  16. *
  17. * @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.
  18. * @param {Number} [cone=0.0] The angular coordinate measured from the positive z-axis and toward the negative z-axis.
  19. * @param {Number} [magnitude=1.0] The linear coordinate measured from the origin.
  20. */
  21. function Spherical(clock, cone, magnitude) {
  22. this.clock = defaultValue(clock, 0.0);
  23. this.cone = defaultValue(cone, 0.0);
  24. this.magnitude = defaultValue(magnitude, 1.0);
  25. }
  26. /**
  27. * Converts the provided Cartesian3 into Spherical coordinates.
  28. *
  29. * @param {Cartesian3} cartesian3 The Cartesian3 to be converted to Spherical.
  30. * @param {Spherical} [spherical] The object in which the result will be stored, if undefined a new instance will be created.
  31. * @returns {Spherical} The modified result parameter, or a new instance if one was not provided.
  32. */
  33. Spherical.fromCartesian3 = function(cartesian3, result) {
  34. //>>includeStart('debug', pragmas.debug);
  35. if (!defined(cartesian3)) {
  36. throw new DeveloperError('cartesian3 is required');
  37. }
  38. //>>includeEnd('debug');
  39. var x = cartesian3.x;
  40. var y = cartesian3.y;
  41. var z = cartesian3.z;
  42. var radialSquared = x * x + y * y;
  43. if (!defined(result)) {
  44. result = new Spherical();
  45. }
  46. result.clock = Math.atan2(y, x);
  47. result.cone = Math.atan2(Math.sqrt(radialSquared), z);
  48. result.magnitude = Math.sqrt(radialSquared + z * z);
  49. return result;
  50. };
  51. /**
  52. * Creates a duplicate of a Spherical.
  53. *
  54. * @param {Spherical} spherical The spherical to clone.
  55. * @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
  56. * @returns {Spherical} The modified result parameter or a new instance if result was undefined. (Returns undefined if spherical is undefined)
  57. */
  58. Spherical.clone = function(spherical, result) {
  59. if (!defined(spherical)) {
  60. return undefined;
  61. }
  62. if (!defined(result)) {
  63. return new Spherical(spherical.clock, spherical.cone, spherical.magnitude);
  64. }
  65. result.clock = spherical.clock;
  66. result.cone = spherical.cone;
  67. result.magnitude = spherical.magnitude;
  68. return result;
  69. };
  70. /**
  71. * Computes the normalized version of the provided spherical.
  72. *
  73. * @param {Spherical} spherical The spherical to be normalized.
  74. * @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
  75. * @returns {Spherical} The modified result parameter or a new instance if result was undefined.
  76. */
  77. Spherical.normalize = function(spherical, result) {
  78. //>>includeStart('debug', pragmas.debug);
  79. if (!defined(spherical)) {
  80. throw new DeveloperError('spherical is required');
  81. }
  82. //>>includeEnd('debug');
  83. if (!defined(result)) {
  84. return new Spherical(spherical.clock, spherical.cone, 1.0);
  85. }
  86. result.clock = spherical.clock;
  87. result.cone = spherical.cone;
  88. result.magnitude = 1.0;
  89. return result;
  90. };
  91. /**
  92. * Returns true if the first spherical is equal to the second spherical, false otherwise.
  93. *
  94. * @param {Spherical} left The first Spherical to be compared.
  95. * @param {Spherical} right The second Spherical to be compared.
  96. * @returns {Boolean} true if the first spherical is equal to the second spherical, false otherwise.
  97. */
  98. Spherical.equals = function(left, right) {
  99. return (left === right) ||
  100. ((defined(left)) &&
  101. (defined(right)) &&
  102. (left.clock === right.clock) &&
  103. (left.cone === right.cone) &&
  104. (left.magnitude === right.magnitude));
  105. };
  106. /**
  107. * Returns true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
  108. *
  109. * @param {Spherical} left The first Spherical to be compared.
  110. * @param {Spherical} right The second Spherical to be compared.
  111. * @param {Number} [epsilon=0.0] The epsilon to compare against.
  112. * @returns {Boolean} true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
  113. */
  114. Spherical.equalsEpsilon = function(left, right, epsilon) {
  115. epsilon = defaultValue(epsilon, 0.0);
  116. return (left === right) ||
  117. ((defined(left)) &&
  118. (defined(right)) &&
  119. (Math.abs(left.clock - right.clock) <= epsilon) &&
  120. (Math.abs(left.cone - right.cone) <= epsilon) &&
  121. (Math.abs(left.magnitude - right.magnitude) <= epsilon));
  122. };
  123. /**
  124. * Returns true if this spherical is equal to the provided spherical, false otherwise.
  125. *
  126. * @param {Spherical} other The Spherical to be compared.
  127. * @returns {Boolean} true if this spherical is equal to the provided spherical, false otherwise.
  128. */
  129. Spherical.prototype.equals = function(other) {
  130. return Spherical.equals(this, other);
  131. };
  132. /**
  133. * Creates a duplicate of this Spherical.
  134. *
  135. * @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
  136. * @returns {Spherical} The modified result parameter or a new instance if result was undefined.
  137. */
  138. Spherical.prototype.clone = function(result) {
  139. return Spherical.clone(this, result);
  140. };
  141. /**
  142. * Returns true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
  143. *
  144. * @param {Spherical} other The Spherical to be compared.
  145. * @param {Number} epsilon The epsilon to compare against.
  146. * @returns {Boolean} true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
  147. */
  148. Spherical.prototype.equalsEpsilon = function(other, epsilon) {
  149. return Spherical.equalsEpsilon(this, other, epsilon);
  150. };
  151. /**
  152. * Returns a string representing this instance in the format (clock, cone, magnitude).
  153. *
  154. * @returns {String} A string representing this instance.
  155. */
  156. Spherical.prototype.toString = function() {
  157. return '(' + this.clock + ', ' + this.cone + ', ' + this.magnitude + ')';
  158. };
  159. return Spherical;
  160. });