Sphere.js 3.0 KB
Newer Older
R
Rich Harris 已提交
1 2 3
import { Box3 } from './Box3';
import { Vector3 } from './Vector3';

4
/**
5
 * @author bhouston / http://clara.io
6
 * @author mrdoob / http://mrdoob.com/
7 8
 */

M
Mr.doob 已提交
9
function Sphere( center, radius ) {
10

R
Rich Harris 已提交
11
	this.center = ( center !== undefined ) ? center : new Vector3();
12
	this.radius = ( radius !== undefined ) ? radius : 0;
13

M
Mr.doob 已提交
14
}
15

R
Rich Harris 已提交
16
Sphere.prototype = {
17

R
Rich Harris 已提交
18
	constructor: Sphere,
19

20
	set: function ( center, radius ) {
21

22
		this.center.copy( center );
23 24 25
		this.radius = radius;

		return this;
G
gero3 已提交
26

27
	},
28

29
	setFromPoints: function () {
30

R
Rich Harris 已提交
31
		var box = new Box3();
32

W
WestLangley 已提交
33
		return function setFromPoints( points, optionalCenter ) {
34

35
			var center = this.center;
36

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

				center.copy( optionalCenter );

			} else {

43
				box.setFromPoints( points ).getCenter( center );
44 45

			}
46

47
			var maxRadiusSq = 0;
48

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

51
				maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
52 53 54 55 56

			}

			this.radius = Math.sqrt( maxRadiusSq );

57 58
			return this;

G
gero3 已提交
59
		};
60 61

	}(),
62

63 64
	clone: function () {

65
		return new this.constructor().copy( this );
66 67 68

	},

69
	copy: function ( sphere ) {
70

71
		this.center.copy( sphere.center );
72
		this.radius = sphere.radius;
73 74

		return this;
75

76
	},
77

78
	empty: function () {
79 80

		return ( this.radius <= 0 );
81

82
	},
83

84
	containsPoint: function ( point ) {
85

86
		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
87

88
	},
89

90
	distanceToPoint: function ( point ) {
91

92
		return ( point.distanceTo( this.center ) - this.radius );
93

94
	},
O
Oliver Sand 已提交
95

96
	intersectsSphere: function ( sphere ) {
O
Oliver Sand 已提交
97

98 99
		var radiusSum = this.radius + sphere.radius;

100 101
		return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );

O
Oliver Sand 已提交
102
	},
103

104
	intersectsBox: function ( box ) {
M
Mugen87 已提交
105

106
		return box.intersectsSphere( this );
M
Mugen87 已提交
107 108 109

	},

110 111 112 113 114 115 116 117 118 119
	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 已提交
120
		return Math.abs( this.center.dot( plane.normal ) - plane.constant ) <= this.radius;
121 122 123

	},

124
	clampPoint: function ( point, optionalTarget ) {
125

126
		var deltaLengthSq = this.center.distanceToSquared( point );
127

R
Rich Harris 已提交
128
		var result = optionalTarget || new Vector3();
M
Mr.doob 已提交
129

130
		result.copy( point );
131

132
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
133

134 135
			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );
136

137 138
		}

139
		return result;
140

141
	},
142

143
	getBoundingBox: function ( optionalTarget ) {
144

R
Rich Harris 已提交
145
		var box = optionalTarget || new Box3();
146 147

		box.set( this.center, this.center );
148 149 150
		box.expandByScalar( this.radius );

		return box;
151

152
	},
153

154
	applyMatrix4: function ( matrix ) {
155

156
		this.center.applyMatrix4( matrix );
157 158 159 160 161 162
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;

	},

163
	translate: function ( offset ) {
164

165
		this.center.add( offset );
166

B
Ben Houston 已提交
167
		return this;
168

169 170 171 172
	},

	equals: function ( sphere ) {

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

175
	}
B
Ben Houston 已提交
176

177
};
R
Rich Harris 已提交
178 179


180
export { Sphere };