Sphere.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { Box3 } from './Box3.js';
  2. import { Vector3 } from './Vector3.js';
  3. /**
  4. * @author bhouston / http://clara.io
  5. * @author mrdoob / http://mrdoob.com/
  6. */
  7. function Sphere( center, radius ) {
  8. this.center = ( center !== undefined ) ? center : new Vector3();
  9. this.radius = ( radius !== undefined ) ? radius : 0;
  10. }
  11. Object.assign( Sphere.prototype, {
  12. set: function ( center, radius ) {
  13. this.center.copy( center );
  14. this.radius = radius;
  15. return this;
  16. },
  17. setFromPoints: function () {
  18. var box = new Box3();
  19. return function setFromPoints( points, optionalCenter ) {
  20. var center = this.center;
  21. if ( optionalCenter !== undefined ) {
  22. center.copy( optionalCenter );
  23. } else {
  24. box.setFromPoints( points ).getCenter( center );
  25. }
  26. var maxRadiusSq = 0;
  27. for ( var i = 0, il = points.length; i < il; i ++ ) {
  28. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  29. }
  30. this.radius = Math.sqrt( maxRadiusSq );
  31. return this;
  32. };
  33. }(),
  34. clone: function () {
  35. return new this.constructor().copy( this );
  36. },
  37. copy: function ( sphere ) {
  38. this.center.copy( sphere.center );
  39. this.radius = sphere.radius;
  40. return this;
  41. },
  42. empty: function () {
  43. return ( this.radius <= 0 );
  44. },
  45. containsPoint: function ( point ) {
  46. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  47. },
  48. distanceToPoint: function ( point ) {
  49. return ( point.distanceTo( this.center ) - this.radius );
  50. },
  51. intersectsSphere: function ( sphere ) {
  52. var radiusSum = this.radius + sphere.radius;
  53. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  54. },
  55. intersectsBox: function ( box ) {
  56. return box.intersectsSphere( this );
  57. },
  58. intersectsPlane: function ( plane ) {
  59. return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
  60. },
  61. clampPoint: function ( point, optionalTarget ) {
  62. var deltaLengthSq = this.center.distanceToSquared( point );
  63. var result = optionalTarget || new Vector3();
  64. result.copy( point );
  65. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  66. result.sub( this.center ).normalize();
  67. result.multiplyScalar( this.radius ).add( this.center );
  68. }
  69. return result;
  70. },
  71. getBoundingBox: function ( optionalTarget ) {
  72. var box = optionalTarget || new Box3();
  73. box.set( this.center, this.center );
  74. box.expandByScalar( this.radius );
  75. return box;
  76. },
  77. applyMatrix4: function ( matrix ) {
  78. this.center.applyMatrix4( matrix );
  79. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  80. return this;
  81. },
  82. translate: function ( offset ) {
  83. this.center.add( offset );
  84. return this;
  85. },
  86. equals: function ( sphere ) {
  87. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  88. }
  89. } );
  90. export { Sphere };