Sphere.js 2.5 KB
Newer Older
1
/**
B
Ben Houston 已提交
2
 * @author bhouston / http://exocortex.com
3
 * @author mrdoob / http://mrdoob.com/
4 5
 */

6
THREE.Sphere = function ( center, radius ) {
7

8 9
	this.center = ( center !== undefined ) ? center : new THREE.Vector3();
	this.radius = ( radius !== undefined ) ? radius : 0;
10

11
};
12

13 14 15
THREE.Sphere.prototype = {

	constructor: THREE.Sphere,
16

17
	set: function ( center, radius ) {
18

19
		this.center.copy( center );
20 21 22
		this.radius = radius;

		return this;
G
gero3 已提交
23

24
	},
25

26
	setFromPoints: function () {
27

28
		var box = new THREE.Box3();
29

G
gero3 已提交
30
		return function ( points, optionalCenter ) {
31

32
			var center = this.center;
33

34 35 36 37 38 39 40 41 42
			if ( optionalCenter !== undefined ) {

				center.copy( optionalCenter );

			} else {

				box.setFromPoints( points ).center( center );

			}
43

44
			var maxRadiusSq = 0;
45

46 47
			for ( var i = 0, il = points.length; i < il; i ++ ) {

48
				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
49 50 51 52 53

			}

			this.radius = Math.sqrt( maxRadiusSq );

54 55
			return this;

G
gero3 已提交
56
		};
57 58

	}(),
59

60 61
	clone: function () {

62
		return new this.constructor().copy( this );
63 64 65

	},

66
	copy: function ( sphere ) {
67

68
		this.center.copy( sphere.center );
69
		this.radius = sphere.radius;
70 71

		return this;
72

73
	},
74

75
	empty: function () {
76 77

		return ( this.radius <= 0 );
78

79
	},
80

81
	containsPoint: function ( point ) {
82

83
		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
84

85
	},
86

87
	distanceToPoint: function ( point ) {
88

89
		return ( point.distanceTo( this.center ) - this.radius );
90

91
	},
O
Oliver Sand 已提交
92

93
	intersectsSphere: function ( sphere ) {
O
Oliver Sand 已提交
94

95 96
		var radiusSum = this.radius + sphere.radius;

97 98
		return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );

O
Oliver Sand 已提交
99
	},
100

101
	clampPoint: function ( point, optionalTarget ) {
102

103
		var deltaLengthSq = this.center.distanceToSquared( point );
104

105 106
		var result = optionalTarget || new THREE.Vector3();
		result.copy( point );
107

108
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
109

110 111
			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );
112

113 114
		}

115
		return result;
116

117
	},
118

119
	getBoundingBox: function ( optionalTarget ) {
120

121 122 123
		var box = optionalTarget || new THREE.Box3();

		box.set( this.center, this.center );
124 125 126
		box.expandByScalar( this.radius );

		return box;
127

128
	},
129

130
	applyMatrix4: function ( matrix ) {
131

132
		this.center.applyMatrix4( matrix );
133 134 135 136 137 138
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;

	},

139
	translate: function ( offset ) {
140

141
		this.center.add( offset );
142

B
Ben Houston 已提交
143
		return this;
144

145 146 147 148
	},

	equals: function ( sphere ) {

A
alteredq 已提交
149
		return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
150

151
	}
B
Ben Houston 已提交
152

153
};