Sphere.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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. // We use the following equation to compute the signed distance from
  60. // the center of the sphere to the plane.
  61. //
  62. // distance = q * n - d
  63. //
  64. // If this distance is greater than the radius of the sphere,
  65. // then there is no intersection.
  66. return Math.abs( this.center.dot( plane.normal ) - plane.constant ) <= this.radius;
  67. },
  68. clampPoint: function ( point, optionalTarget ) {
  69. var deltaLengthSq = this.center.distanceToSquared( point );
  70. var result = optionalTarget || new Vector3();
  71. result.copy( point );
  72. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  73. result.sub( this.center ).normalize();
  74. result.multiplyScalar( this.radius ).add( this.center );
  75. }
  76. return result;
  77. },
  78. getBoundingBox: function ( optionalTarget ) {
  79. var box = optionalTarget || new Box3();
  80. box.set( this.center, this.center );
  81. box.expandByScalar( this.radius );
  82. return box;
  83. },
  84. applyMatrix4: function ( matrix ) {
  85. this.center.applyMatrix4( matrix );
  86. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  87. return this;
  88. },
  89. translate: function ( offset ) {
  90. this.center.add( offset );
  91. return this;
  92. },
  93. equals: function ( sphere ) {
  94. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  95. }
  96. } );
  97. export { Sphere };
粤ICP备19079148号