/** * @author bhouston / http://exocortex.com */ THREE.Sphere = function ( center, radius ) { if ( center === undefined && radius === undefined ) { this.center = new THREE.Vector3(); this.radius = 0; } else { this.center = center.clone(); this.radius = radius || 0; } }; THREE.Sphere.prototype = { constructor: THREE.Sphere, set: function ( center, radius ) { this.center.copy( center ); this.radius = radius; return this; }, setFromCenterAndPoints: function ( center, points ) { var maxRadiusSq = 0; for ( var i = 0, il = points.length; i < il; i ++ ) { var radiusSq = center.distanceToSquared( points[ i ] ); maxRadiusSq = Math.max( maxRadiusSq, radiusSq ); } this.center = center; this.radius = Math.sqrt( maxRadiusSq ); return this; }, copy: function ( sphere ) { this.center.copy( sphere.center ); this.radius = sphere.radius; return this; }, empty: function () { return ( this.radius <= 0 ); }, containsPoint: function ( point ) { return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) ); }, distanceToPoint: function ( point ) { return ( point.distanceTo( this.center ) - this.radius ); }, clampPoint: function ( point ) { var deltaLengthSq = this.center.distanceToSquared( point ); var result = new THREE.Vector3().copy( point ); if ( deltaLengthSq > ( this.radius * this.radius ) ) { result.subSelf( this.center ).normalize(); result.multiplyScalar( this.radius ).addSelf( this.center ); } return result; }, bounds: function () { var box = new THREE.Box3( this.center, this.center ); box.expandByScalar( this.radius ); return box; }, translate: function ( offset ) { this.center.addSelf( this.offset ); return this; }, scale: function ( factor ) { this.radius *= factor; return this; }, equals: function ( sphere ) { return sphere.center.equals( this.center ) && ( sphere.radius === this.radius ); }, clone: function () { return new THREE.Sphere3().copy( this ); } };