Sphere.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { Box3 } from './Box3';
  2. import { Vector3 } from './Vector3';
  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. constructor: Sphere,
  13. set: function ( center, radius ) {
  14. this.center.copy( center );
  15. this.radius = radius;
  16. return this;
  17. },
  18. setFromPoints: function () {
  19. var box;
  20. return function setFromPoints( points, optionalCenter ) {
  21. if ( box === undefined ) box = new Box3(); // see #10547
  22. var center = this.center;
  23. if ( optionalCenter !== undefined ) {
  24. center.copy( optionalCenter );
  25. } else {
  26. box.setFromPoints( points ).getCenter( center );
  27. }
  28. var maxRadiusSq = 0;
  29. for ( var i = 0, il = points.length; i < il; i ++ ) {
  30. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  31. }
  32. this.radius = Math.sqrt( maxRadiusSq );
  33. return this;
  34. };
  35. }(),
  36. clone: function () {
  37. return new this.constructor().copy( this );
  38. },
  39. copy: function ( sphere ) {
  40. this.center.copy( sphere.center );
  41. this.radius = sphere.radius;
  42. return this;
  43. },
  44. empty: function () {
  45. return ( this.radius <= 0 );
  46. },
  47. containsPoint: function ( point ) {
  48. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  49. },
  50. distanceToPoint: function ( point ) {
  51. return ( point.distanceTo( this.center ) - this.radius );
  52. },
  53. intersectsSphere: function ( sphere ) {
  54. var radiusSum = this.radius + sphere.radius;
  55. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  56. },
  57. intersectsBox: function ( box ) {
  58. return box.intersectsSphere( this );
  59. },
  60. intersectsPlane: function ( plane ) {
  61. // We use the following equation to compute the signed distance from
  62. // the center of the sphere to the plane.
  63. //
  64. // distance = q * n - d
  65. //
  66. // If this distance is greater than the radius of the sphere,
  67. // then there is no intersection.
  68. return Math.abs( this.center.dot( plane.normal ) - plane.constant ) <= this.radius;
  69. },
  70. clampPoint: function ( point, optionalTarget ) {
  71. var deltaLengthSq = this.center.distanceToSquared( point );
  72. var result = optionalTarget || new Vector3();
  73. result.copy( point );
  74. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  75. result.sub( this.center ).normalize();
  76. result.multiplyScalar( this.radius ).add( this.center );
  77. }
  78. return result;
  79. },
  80. getBoundingBox: function ( optionalTarget ) {
  81. var box = optionalTarget || new Box3();
  82. box.set( this.center, this.center );
  83. box.expandByScalar( this.radius );
  84. return box;
  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 };
粤ICP备19079148号