Sphere.js 3.0 KB
Newer Older
1
/**
2
 * @author bhouston / http://clara.io
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
	intersectsBox: function ( box ) {
M
Mugen87 已提交
102

103
		return box.intersectsSphere( this );
M
Mugen87 已提交
104 105 106

	},

107 108 109 110 111 112 113 114 115 116
	intersectsPlane: function ( plane ) {

		// We use the following equation to compute the signed distance from
		// the center of the sphere to the plane.
		//
		// distance = q * n - d
		//
		// If this distance is greater than the radius of the sphere,
		// then there is no intersection.

M
Mr.doob 已提交
117
		return Math.abs( this.center.dot( plane.normal ) - plane.constant ) <= this.radius;
118 119 120

	},

121
	clampPoint: function ( point, optionalTarget ) {
122

123
		var deltaLengthSq = this.center.distanceToSquared( point );
124

125
		var result = optionalTarget || new THREE.Vector3();
M
Mr.doob 已提交
126

127
		result.copy( point );
128

129
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
130

131 132
			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );
133

134 135
		}

136
		return result;
137

138
	},
139

140
	getBoundingBox: function ( optionalTarget ) {
141

142 143 144
		var box = optionalTarget || new THREE.Box3();

		box.set( this.center, this.center );
145 146 147
		box.expandByScalar( this.radius );

		return box;
148

149
	},
150

151
	applyMatrix4: function ( matrix ) {
152

153
		this.center.applyMatrix4( matrix );
154 155 156 157 158 159
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;

	},

160
	translate: function ( offset ) {
161

162
		this.center.add( offset );
163

B
Ben Houston 已提交
164
		return this;
165

166 167 168 169
	},

	equals: function ( sphere ) {

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

172
	}
B
Ben Houston 已提交
173

174
};