Spherical.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. const x = cartesian3.x;
  46. const y = cartesian3.y;
  47. const z = cartesian3.z;
  48. const 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 (
  104. left === right ||
  105. (defined(left) &&
  106. defined(right) &&
  107. left.clock === right.clock &&
  108. left.cone === right.cone &&
  109. left.magnitude === right.magnitude)
  110. );
  111. };
  112. /**
  113. * Returns true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
  114. *
  115. * @param {Spherical} left The first Spherical to be compared.
  116. * @param {Spherical} right The second Spherical to be compared.
  117. * @param {Number} [epsilon=0.0] The epsilon to compare against.
  118. * @returns {Boolean} true if the first spherical is within the provided epsilon of the second spherical, false otherwise.
  119. */
  120. Spherical.equalsEpsilon = function (left, right, epsilon) {
  121. epsilon = defaultValue(epsilon, 0.0);
  122. return (
  123. left === right ||
  124. (defined(left) &&
  125. defined(right) &&
  126. Math.abs(left.clock - right.clock) <= epsilon &&
  127. Math.abs(left.cone - right.cone) <= epsilon &&
  128. Math.abs(left.magnitude - right.magnitude) <= epsilon)
  129. );
  130. };
  131. /**
  132. * Returns true if this spherical is equal to the provided spherical, false otherwise.
  133. *
  134. * @param {Spherical} other The Spherical to be compared.
  135. * @returns {Boolean} true if this spherical is equal to the provided spherical, false otherwise.
  136. */
  137. Spherical.prototype.equals = function (other) {
  138. return Spherical.equals(this, other);
  139. };
  140. /**
  141. * Creates a duplicate of this Spherical.
  142. *
  143. * @param {Spherical} [result] The object to store the result into, if undefined a new instance will be created.
  144. * @returns {Spherical} The modified result parameter or a new instance if result was undefined.
  145. */
  146. Spherical.prototype.clone = function (result) {
  147. return Spherical.clone(this, result);
  148. };
  149. /**
  150. * Returns true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
  151. *
  152. * @param {Spherical} other The Spherical to be compared.
  153. * @param {Number} epsilon The epsilon to compare against.
  154. * @returns {Boolean} true if this spherical is within the provided epsilon of the provided spherical, false otherwise.
  155. */
  156. Spherical.prototype.equalsEpsilon = function (other, epsilon) {
  157. return Spherical.equalsEpsilon(this, other, epsilon);
  158. };
  159. /**
  160. * Returns a string representing this instance in the format (clock, cone, magnitude).
  161. *
  162. * @returns {String} A string representing this instance.
  163. */
  164. Spherical.prototype.toString = function () {
  165. return `(${this.clock}, ${this.cone}, ${this.magnitude})`;
  166. };
  167. export default Spherical;