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;
23
	},
24

25
	setFromPoints: function () {
26

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

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

31
			var center = this.center;
32

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

				center.copy( optionalCenter );

			} else {

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

			}
42

43
			var maxRadiusSq = 0;
44

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

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

			}

			this.radius = Math.sqrt( maxRadiusSq );

53 54
			return this;

55 56 57
 		};

	}(),
58

59
	copy: function ( sphere ) {
60

61
		this.center.copy( sphere.center );
62
		this.radius = sphere.radius;
63 64

		return this;
65

66
	},
67

68
	empty: function () {
69 70

		return ( this.radius <= 0 );
71

72
	},
73

74
	containsPoint: function ( point ) {
75

76
		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
77

78
	},
79

80
	distanceToPoint: function ( point ) {
81

82
		return ( point.distanceTo( this.center ) - this.radius );
83

84
	},
O
Oliver Sand 已提交
85

86
	intersectsSphere: function ( sphere ) {
O
Oliver Sand 已提交
87

88 89
		var radiusSum = this.radius + sphere.radius;

90 91
		return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );

O
Oliver Sand 已提交
92
	},
93

94
	clampPoint: function ( point, optionalTarget ) {
95

96
		var deltaLengthSq = this.center.distanceToSquared( point );
97

98 99
		var result = optionalTarget || new THREE.Vector3();
		result.copy( point );
100

101
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
102

103 104
			result.sub( this.center ).normalize();
			result.multiplyScalar( this.radius ).add( this.center );
105

106 107
		}

108
		return result;
109

110
	},
111

112
	getBoundingBox: function ( optionalTarget ) {
113

114 115 116
		var box = optionalTarget || new THREE.Box3();

		box.set( this.center, this.center );
117 118 119
		box.expandByScalar( this.radius );

		return box;
120

121
	},
122

123
	applyMatrix4: function ( matrix ) {
124

125
		this.center.applyMatrix4( matrix );
126 127 128 129 130 131
		this.radius = this.radius * matrix.getMaxScaleOnAxis();

		return this;

	},

132
	translate: function ( offset ) {
133

134
		this.center.add( offset );
135

B
Ben Houston 已提交
136
		return this;
137

138 139 140 141
	},

	equals: function ( sphere ) {

A
alteredq 已提交
142
		return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
143 144 145 146 147

	},

	clone: function () {

148
		return new THREE.Sphere().copy( this );
149

150
	}
B
Ben Houston 已提交
151

152
};