Source: math/Sphere.js

  1. import { Vector3 } from './Vector3.js';
  2. import { Box3 } from './Box3.js';
  3. var _box3_1 = new Box3();
  4. var _vec3_1 = new Vector3();
  5. /**
  6. * @constructor
  7. * @memberof zen3d
  8. * @param {zen3d.Vector3} [center=Vector3()]
  9. * @param {number} [radius=0]
  10. */
  11. function Sphere(center, radius) {
  12. this.center = (center !== undefined) ? center : new Vector3();
  13. this.radius = (radius !== undefined) ? radius : 0;
  14. }
  15. Object.assign(Sphere.prototype, /** @lends zen3d.Sphere.prototype */{
  16. /**
  17. *
  18. */
  19. set: function(center, radius) {
  20. this.center.copy(center);
  21. this.radius = radius;
  22. return this;
  23. },
  24. /**
  25. * @method
  26. */
  27. setFromArray: function(array, gap) {
  28. var _gap = (gap !== undefined ? gap : 3);
  29. var center = this.center;
  30. _box3_1.setFromArray(array, _gap).getCenter(center);
  31. var maxRadiusSq = 0;
  32. for (var i = 0, l = array.length; i < l; i += _gap) {
  33. _vec3_1.fromArray(array, i);
  34. maxRadiusSq = Math.max(maxRadiusSq, center.distanceToSquared(_vec3_1));
  35. }
  36. this.radius = Math.sqrt(maxRadiusSq);
  37. return this;
  38. },
  39. /**
  40. *
  41. */
  42. applyMatrix4: function(matrix) {
  43. this.center.applyMatrix4(matrix);
  44. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  45. return this;
  46. },
  47. /**
  48. *
  49. */
  50. getBoundingBox: function(optionalTarget) {
  51. var box = optionalTarget || new Box3();
  52. box.set(this.center, this.center);
  53. box.expandByScalar(this.radius);
  54. return box;
  55. },
  56. /**
  57. *
  58. */
  59. clone: function() {
  60. return new Sphere().copy(this);
  61. },
  62. /**
  63. *
  64. */
  65. copy: function(sphere) {
  66. this.center.copy(sphere.center);
  67. this.radius = sphere.radius;
  68. return this;
  69. }
  70. });
  71. export { Sphere };