Sphere.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import { Box3 } from './Box3.js';
  2. import { Vector3 } from './Vector3.js';
  3. const _box = /*@__PURE__*/ new Box3();
  4. const _v1 = /*@__PURE__*/ new Vector3();
  5. const _v2 = /*@__PURE__*/ new Vector3();
  6. /**
  7. * An analytical 3D sphere defined by a center and radius. This class is mainly
  8. * used as a Bounding Sphere for 3D objects.
  9. */
  10. class Sphere {
  11. /**
  12. * Constructs a new sphere.
  13. *
  14. * @param {Vector3} [center=(0,0,0)] - The center of the sphere
  15. * @param {number} [radius=-1] - The radius of the sphere.
  16. */
  17. constructor( center = new Vector3(), radius = - 1 ) {
  18. /**
  19. * This flag can be used for type testing.
  20. *
  21. * @type {boolean}
  22. * @readonly
  23. * @default true
  24. */
  25. this.isSphere = true;
  26. /**
  27. * The center of the sphere
  28. *
  29. * @type {Vector3}
  30. */
  31. this.center = center;
  32. /**
  33. * The radius of the sphere.
  34. *
  35. * @type {number}
  36. */
  37. this.radius = radius;
  38. }
  39. /**
  40. * Sets the sphere's components by copying the given values.
  41. *
  42. * @param {Vector3} center - The center.
  43. * @param {number} radius - The radius.
  44. * @return {Sphere} A reference to this sphere.
  45. */
  46. set( center, radius ) {
  47. this.center.copy( center );
  48. this.radius = radius;
  49. return this;
  50. }
  51. /**
  52. * Computes the minimum bounding sphere for list of points.
  53. * If the optional center point is given, it is used as the sphere's
  54. * center. Otherwise, the center of the axis-aligned bounding box
  55. * encompassing the points is calculated.
  56. *
  57. * @param {Array<Vector3>} points - A list of points in 3D space.
  58. * @param {Vector3} [optionalCenter] - The center of the sphere.
  59. * @return {Sphere} A reference to this sphere.
  60. */
  61. setFromPoints( points, optionalCenter ) {
  62. const center = this.center;
  63. if ( optionalCenter !== undefined ) {
  64. center.copy( optionalCenter );
  65. } else {
  66. _box.setFromPoints( points ).getCenter( center );
  67. }
  68. let maxRadiusSq = 0;
  69. for ( let i = 0, il = points.length; i < il; i ++ ) {
  70. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  71. }
  72. this.radius = Math.sqrt( maxRadiusSq );
  73. return this;
  74. }
  75. /**
  76. * Copies the values of the given sphere to this instance.
  77. *
  78. * @param {Sphere} sphere - The sphere to copy.
  79. * @return {Sphere} A reference to this sphere.
  80. */
  81. copy( sphere ) {
  82. this.center.copy( sphere.center );
  83. this.radius = sphere.radius;
  84. return this;
  85. }
  86. /**
  87. * Returns `true` if the sphere is empty (the radius set to a negative number).
  88. *
  89. * Spheres with a radius of `0` contain only their center point and are not
  90. * considered to be empty.
  91. *
  92. * @return {boolean} Whether this sphere is empty or not.
  93. */
  94. isEmpty() {
  95. return ( this.radius < 0 );
  96. }
  97. /**
  98. * Makes this sphere empty which means in encloses a zero space in 3D.
  99. *
  100. * @return {Sphere} A reference to this sphere.
  101. */
  102. makeEmpty() {
  103. this.center.set( 0, 0, 0 );
  104. this.radius = - 1;
  105. return this;
  106. }
  107. /**
  108. * Returns `true` if this sphere contains the given point inclusive of
  109. * the surface of the sphere.
  110. *
  111. * @param {Vector3} point - The point to check.
  112. * @return {boolean} Whether this sphere contains the given point or not.
  113. */
  114. containsPoint( point ) {
  115. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  116. }
  117. /**
  118. * Returns the closest distance from the boundary of the sphere to the
  119. * given point. If the sphere contains the point, the distance will
  120. * be negative.
  121. *
  122. * @param {Vector3} point - The point to compute the distance to.
  123. * @return {number} The distance to the point.
  124. */
  125. distanceToPoint( point ) {
  126. return ( point.distanceTo( this.center ) - this.radius );
  127. }
  128. /**
  129. * Returns `true` if this sphere intersects with the given one.
  130. *
  131. * @param {Sphere} sphere - The sphere to test.
  132. * @return {boolean} Whether this sphere intersects with the given one or not.
  133. */
  134. intersectsSphere( sphere ) {
  135. const radiusSum = this.radius + sphere.radius;
  136. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  137. }
  138. /**
  139. * Returns `true` if this sphere intersects with the given box.
  140. *
  141. * @param {Box3} box - The box to test.
  142. * @return {boolean} Whether this sphere intersects with the given box or not.
  143. */
  144. intersectsBox( box ) {
  145. return box.intersectsSphere( this );
  146. }
  147. /**
  148. * Returns `true` if this sphere intersects with the given plane.
  149. *
  150. * @param {Plane} plane - The plane to test.
  151. * @return {boolean} Whether this sphere intersects with the given plane or not.
  152. */
  153. intersectsPlane( plane ) {
  154. return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
  155. }
  156. /**
  157. * Clamps a point within the sphere. If the point is outside the sphere, it
  158. * will clamp it to the closest point on the edge of the sphere. Points
  159. * already inside the sphere will not be affected.
  160. *
  161. * @param {Vector3} point - The plane to clamp.
  162. * @param {Vector3} target - The target vector that is used to store the method's result.
  163. * @return {Vector3} The clamped point.
  164. */
  165. clampPoint( point, target ) {
  166. const deltaLengthSq = this.center.distanceToSquared( point );
  167. target.copy( point );
  168. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  169. target.sub( this.center ).normalize();
  170. target.multiplyScalar( this.radius ).add( this.center );
  171. }
  172. return target;
  173. }
  174. /**
  175. * Returns a bounding box that encloses this sphere.
  176. *
  177. * @param {Box3} target - The target box that is used to store the method's result.
  178. * @return {Box3} The bounding box that encloses this sphere.
  179. */
  180. getBoundingBox( target ) {
  181. if ( this.isEmpty() ) {
  182. // Empty sphere produces empty bounding box
  183. target.makeEmpty();
  184. return target;
  185. }
  186. target.set( this.center, this.center );
  187. target.expandByScalar( this.radius );
  188. return target;
  189. }
  190. /**
  191. * Transforms this sphere with the given 4x4 transformation matrix.
  192. *
  193. * @param {Matrix4} matrix - The transformation matrix.
  194. * @return {Sphere} A reference to this sphere.
  195. */
  196. applyMatrix4( matrix ) {
  197. this.center.applyMatrix4( matrix );
  198. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  199. return this;
  200. }
  201. /**
  202. * Translates the sphere's center by the given offset.
  203. *
  204. * @param {Vector3} offset - The offset.
  205. * @return {Sphere} A reference to this sphere.
  206. */
  207. translate( offset ) {
  208. this.center.add( offset );
  209. return this;
  210. }
  211. /**
  212. * Expands the boundaries of this sphere to include the given point.
  213. *
  214. * @param {Vector3} point - The point to include.
  215. * @return {Sphere} A reference to this sphere.
  216. */
  217. expandByPoint( point ) {
  218. if ( this.isEmpty() ) {
  219. this.center.copy( point );
  220. this.radius = 0;
  221. return this;
  222. }
  223. _v1.subVectors( point, this.center );
  224. const lengthSq = _v1.lengthSq();
  225. if ( lengthSq > ( this.radius * this.radius ) ) {
  226. // calculate the minimal sphere
  227. const length = Math.sqrt( lengthSq );
  228. const delta = ( length - this.radius ) * 0.5;
  229. this.center.addScaledVector( _v1, delta / length );
  230. this.radius += delta;
  231. }
  232. return this;
  233. }
  234. /**
  235. * Expands this sphere to enclose both the original sphere and the given sphere.
  236. *
  237. * @param {Sphere} sphere - The sphere to include.
  238. * @return {Sphere} A reference to this sphere.
  239. */
  240. union( sphere ) {
  241. if ( sphere.isEmpty() ) {
  242. return this;
  243. }
  244. if ( this.isEmpty() ) {
  245. this.copy( sphere );
  246. return this;
  247. }
  248. if ( this.center.equals( sphere.center ) === true ) {
  249. this.radius = Math.max( this.radius, sphere.radius );
  250. } else {
  251. _v2.subVectors( sphere.center, this.center ).setLength( sphere.radius );
  252. this.expandByPoint( _v1.copy( sphere.center ).add( _v2 ) );
  253. this.expandByPoint( _v1.copy( sphere.center ).sub( _v2 ) );
  254. }
  255. return this;
  256. }
  257. /**
  258. * Returns `true` if this sphere is equal with the given one.
  259. *
  260. * @param {Sphere} sphere - The sphere to test for equality.
  261. * @return {boolean} Whether this bounding sphere is equal with the given one.
  262. */
  263. equals( sphere ) {
  264. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  265. }
  266. /**
  267. * Returns a new sphere with copied values from this instance.
  268. *
  269. * @return {Sphere} A clone of this instance.
  270. */
  271. clone() {
  272. return new this.constructor().copy( this );
  273. }
  274. /**
  275. * Returns a serialized structure of the bounding sphere.
  276. *
  277. * @return {Object} Serialized structure with fields representing the object state.
  278. */
  279. toJSON() {
  280. return {
  281. radius: this.radius,
  282. center: this.center.toArray()
  283. };
  284. }
  285. /**
  286. * Returns a serialized structure of the bounding sphere.
  287. *
  288. * @param {Object} json - The serialized json to set the sphere from.
  289. * @return {Box3} A reference to this bounding sphere.
  290. */
  291. fromJSON( json ) {
  292. this.radius = json.radius;
  293. this.center.fromArray( json.center );
  294. return this;
  295. }
  296. }
  297. export { Sphere };
粤ICP备19079148号