Spherical.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { MathUtils } from './MathUtils.js';
  2. /**
  3. * @author bhouston / http://clara.io
  4. * @author WestLangley / http://github.com/WestLangley
  5. *
  6. * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
  7. *
  8. * The polar angle (phi) is measured from the positive y-axis. The positive y-axis is up.
  9. * The azimuthal angle (theta) is measured from the positive z-axis.
  10. */
  11. function Spherical( radius, phi, theta ) {
  12. this.radius = ( radius !== undefined ) ? radius : 1.0;
  13. this.phi = ( phi !== undefined ) ? phi : 0; // polar angle
  14. this.theta = ( theta !== undefined ) ? theta : 0; // azimuthal angle
  15. return this;
  16. }
  17. Object.assign( Spherical.prototype, {
  18. set: function ( radius, phi, theta ) {
  19. this.radius = radius;
  20. this.phi = phi;
  21. this.theta = theta;
  22. return this;
  23. },
  24. clone: function () {
  25. return new this.constructor().copy( this );
  26. },
  27. copy: function ( other ) {
  28. this.radius = other.radius;
  29. this.phi = other.phi;
  30. this.theta = other.theta;
  31. return this;
  32. },
  33. // restrict phi to be betwee EPS and PI-EPS
  34. makeSafe: function () {
  35. const EPS = 0.000001;
  36. this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) );
  37. return this;
  38. },
  39. setFromVector3: function ( v ) {
  40. return this.setFromCartesianCoords( v.x, v.y, v.z );
  41. },
  42. setFromCartesianCoords: function ( x, y, z ) {
  43. this.radius = Math.sqrt( x * x + y * y + z * z );
  44. if ( this.radius === 0 ) {
  45. this.theta = 0;
  46. this.phi = 0;
  47. } else {
  48. this.theta = Math.atan2( x, z );
  49. this.phi = Math.acos( MathUtils.clamp( y / this.radius, - 1, 1 ) );
  50. }
  51. return this;
  52. }
  53. } );
  54. export { Spherical };