Spherical.js 6.8 KB

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