Sphere.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Box3 } from './Box3.js';
  2. import { Vector3 } from './Vector3.js';
  3. const _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 : - 1;
  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. const center = this.center;
  20. if ( optionalCenter !== undefined ) {
  21. center.copy( optionalCenter );
  22. } else {
  23. _box.setFromPoints( points ).getCenter( center );
  24. }
  25. let maxRadiusSq = 0;
  26. for ( let 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. isEmpty: function () {
  41. return ( this.radius < 0 );
  42. },
  43. makeEmpty: function () {
  44. this.center.set( 0, 0, 0 );
  45. this.radius = - 1;
  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. const 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. const 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. if ( this.isEmpty() ) {
  83. // Empty sphere produces empty bounding box
  84. target.makeEmpty();
  85. return target;
  86. }
  87. target.set( this.center, this.center );
  88. target.expandByScalar( this.radius );
  89. return target;
  90. },
  91. applyMatrix4: function ( matrix ) {
  92. this.center.applyMatrix4( matrix );
  93. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  94. return this;
  95. },
  96. translate: function ( offset ) {
  97. this.center.add( offset );
  98. return this;
  99. },
  100. equals: function ( sphere ) {
  101. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  102. }
  103. } );
  104. export { Sphere };
粤ICP备19079148号