Sphere.js 3.0 KB

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