123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /**
- * @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 );
- }
- };
|