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

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

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

10
};
11

12
THREE.Sphere.prototype = {
13

14
	constructor: THREE.Sphere,
15

16
	set: function ( center, radius ) {
17

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

		return this;
22
	},
23

24 25 26
	setFromCenterAndPoints: function ( center, points ) {

		var maxRadiusSq = 0;
27 28 29 30

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

			var radiusSq = center.distanceToSquared( points[ i ] );
31
			maxRadiusSq = Math.max( maxRadiusSq, radiusSq );
32

33 34 35 36 37 38
		}

		this.center = center;
		this.radius = Math.sqrt( maxRadiusSq );

		return this;
39

40 41
	},

42
	copy: function ( sphere ) {
43

44
		this.center.copy( sphere.center );
45
		this.radius = sphere.radius;
46 47

		return this;
48

49
	},
50

51
	empty: function () {
52 53

		return ( this.radius <= 0 );
54

55
	},
56

57
	containsPoint: function ( point ) {
58

59
		return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
60

61
	},
62

63
	distanceToPoint: function ( point ) {
64

65
		return ( point.distanceTo( this.center ) - this.radius );
66

67
	},
68

69
	clampPoint: function ( point ) {
70

71
		var deltaLengthSq = this.center.distanceToSquared( point );
72

73
		var result = new THREE.Vector3().copy( point );
74

75
		if ( deltaLengthSq > ( this.radius * this.radius ) ) {
76

77
			result.subSelf( this.center ).normalize();
78
			result.multiplyScalar( this.radius ).addSelf( this.center );
79

80 81
		}

82
		return result;
83

84
	},
85

86
	bounds: function () {
87

88
		var box =  new THREE.Box3( this.center, this.center );
89 90 91
		box.expandByScalar( this.radius );

		return box;
92

93
	},
94

95
	translate: function ( offset ) {
96

97
		this.center.addSelf( this.offset );
98

99
		return this;
100

101
	},
102

103
	scale: function ( factor ) {
B
Ben Houston 已提交
104 105

		this.radius *= factor;
106

B
Ben Houston 已提交
107
		return this;
108

109 110 111 112
	},

	equals: function ( sphere ) {

A
alteredq 已提交
113
		return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
114 115 116 117 118 119 120

	},

	clone: function () {

		return new THREE.Sphere3().copy( this );

121
	}
B
Ben Houston 已提交
122

123
};