Math/SphericalCoords.js

  1. /** A class representing spherical coordinates. */
  2. Lore.SphericalCoords = class SphericalCoords {
  3. /**
  4. * Creates an instance of SphericalCoords.
  5. * @param {Number} radius The radius.
  6. * @param {Number} phi Phi in radians.
  7. * @param {Number} theta Theta in radians.
  8. */
  9. constructor(radius, phi, theta) {
  10. this.components = new Float32Array(3);
  11. this.radius = (radius !== undefined) ? radius : 1.0;
  12. this.phi = phi ? phi : 0.0;
  13. this.theta = theta ? theta : 0.0;
  14. }
  15. /**
  16. * Set the spherical coordinates from the radius, the phi angle and the theta angle.
  17. *
  18. * @param {Number} radius
  19. * @param {Number} phi
  20. * @param {Number} theta
  21. * @returns {SphericalCoords} Returns itself.
  22. */
  23. set(radius, phi, theta) {
  24. this.components[0] = radius;
  25. this.components[1] = phi;
  26. this.components[2] = theta;
  27. return this;
  28. }
  29. /**
  30. * Avoid overflows.
  31. *
  32. * @returns {SphericalCoords} Returns itself.
  33. */
  34. secure() {
  35. this.components[1] = Math.max(0.000001, Math.min(Math.PI - 0.000001, this.components[1]));
  36. return this;
  37. }
  38. /**
  39. * Set the spherical coordaintes from a vector.
  40. *
  41. * @param {Vector3f} v A vector.
  42. * @returns {SphericalCoords} Returns itself.
  43. */
  44. setFromVector(v) {
  45. this.components[0] = v.length();
  46. if (this.components[0] === 0.0) {
  47. this.components[1] = 0.0;
  48. this.components[2] = 0.0;
  49. } else {
  50. this.components[1] = Math.acos(Math.max(-1.0, Math.min(1.0, v.components[1] /
  51. this.components[0])));
  52. this.components[2] = Math.atan2(v.components[0], v.components[2]);
  53. }
  54. return this;
  55. }
  56. /**
  57. * Limit the rotation by setting maxima and minima for phi and theta.
  58. *
  59. * @param {Number} phiMin The minimum for phi.
  60. * @param {Number} phiMax The maximum for phi.
  61. * @param {Number} thetaMin The minimum for theta.
  62. * @param {Number} thetaMax The maximum for theta.
  63. * @returns {SphericalCoords} Returns itself.
  64. */
  65. limit(phiMin, phiMax, thetaMin, thetaMax) {
  66. // Limits for orbital controls
  67. this.components[1] = Math.max(phiMin, Math.min(phiMax, this.components[1]));
  68. this.components[2] = Math.max(thetaMin, Math.min(thetaMax, this.components[2]));
  69. return this;
  70. }
  71. /**
  72. * Clone this spherical coordinates object.
  73. *
  74. * @returns {SphericalCoords} A clone of the spherical coordinates object.
  75. */
  76. clone() {
  77. return new Lore.SphericalCoords(this.radius, this.phi, this.theta);
  78. }
  79. /**
  80. * Returns a string representation of these spherical coordinates.
  81. *
  82. * @returns {String} A string representing spherical coordinates.
  83. */
  84. toString() {
  85. return '(' + this.components[0] + ', ' +
  86. this.components[1] + ', ' + this.components[2] + ')';
  87. }
  88. }