Sphere.js 3.0 KB

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