Sphere.js 2.6 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
	clampPoint: function ( point, optionalTarget ) {
108

109
		var deltaLengthSq = this.center.distanceToSquared( point );
110

111 112
		var result = optionalTarget || new THREE.Vector3();
		result.copy( point );
113

114
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
115

116 117
			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );
118

119 120
		}

121
		return result;
122

123
	},
124

125
	getBoundingBox: function ( optionalTarget ) {
126

127 128 129
		var box = optionalTarget || new THREE.Box3();

		box.set( this.center, this.center );
130 131 132
		box.expandByScalar( this.radius );

		return box;
133

134
	},
135

136
	applyMatrix4: function ( matrix ) {
137

138
		this.center.applyMatrix4( matrix );
139 140 141 142 143 144
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;

	},

145
	translate: function ( offset ) {
146

147
		this.center.add( offset );
148

B
Ben Houston 已提交
149
		return this;
150

151 152 153 154
	},

	equals: function ( sphere ) {

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

157
	}
B
Ben Houston 已提交
158

159
};