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

5
THREE.Box3 = function ( min, max ) {
6

7 8
	this.min = min !== undefined ? min.clone() : new THREE.Vector3( Infinity, Infinity, Infinity );
	this.max = max !== undefined ? max.clone() : new THREE.Vector3( -Infinity, -Infinity, -Infinity );
9

10
};
11

12
THREE.Box3.prototype = {
B
Ben Houston 已提交
13

14 15 16
	constructor: THREE.Box3,

	set: function ( min, max ) {
17

18 19
		this.min.copy( min );
		this.max.copy( max );
20 21

		return this;
22

23
	},
24

25 26
	setFromPoints: function ( points ) {

27 28 29
		if ( points.length > 0 ) {

			var p = points[ 0 ];
30 31 32 33

			this.min.copy( p );
			this.max.copy( p );

34
			for ( var i = 1, il = points.length; i < il; i ++ ) {
35

36
				p = points[ i ];
37 38

				if ( p.x < this.min.x ) {
39

40
					this.min.x = p.x;
41 42 43

				} else if ( p.x > this.max.x ) {

44
					this.max.x = p.x;
45

46 47 48
				}

				if ( p.y < this.min.y ) {
49

50
					this.min.y = p.y;
51 52 53

				} else if ( p.y > this.max.y ) {

54
					this.max.y = p.y;
55

56 57 58
				}

				if ( p.z < this.min.z ) {
59

60
					this.min.z = p.z;
61 62 63

				} else if ( p.z > this.max.z ) {

64
					this.max.z = p.z;
65

66
				}
67

68
			}
69 70 71

		} else {

72
			this.makeEmpty();
73

74 75 76
		}

		return this;
A
alteredq 已提交
77

78
	},
79 80 81

	setFromCenterAndSize: function ( center, size ) {

82
		var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
83

84 85 86
		this.min.copy( center ).subSelf( halfSize );
		this.max.copy( center ).addSelf( halfSize );

87 88
		return this;

89 90
	},

91
	copy: function ( box ) {
92

93 94
		this.min.copy( box.min );
		this.max.copy( box.max );
95 96

		return this;
97

98
	},
99

100
	makeEmpty: function () {
101

102 103
		this.min.x = this.min.y = this.min.z = Infinity;
		this.max.x = this.max.y = this.max.z = -Infinity;
104 105

		return this;
106

107
	},
108

109
	empty: function () {
110

111
		// this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
A
alteredq 已提交
112

113 114
		return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );

115
	},
116

117
	center: function ( optionalTarget ) {
118

119 120
		var result = optionalTarget || new THREE.Vector3();
		return result.add( this.min, this.max ).multiplyScalar( 0.5 );
121

122
	},
123

124
	size: function ( optionalTarget ) {
125

126 127
		var result = optionalTarget || new THREE.Vector3();
		return result.sub( this.max, this.min );
128

129
	},
130

131
	expandByPoint: function ( point ) {
132

133
		this.min.minSelf( point );
134
		this.max.maxSelf( point );
135

136
		return this;
137

138
	},
139

140
	expandByVector: function ( vector ) {
141

142 143
		this.min.subSelf( vector );
		this.max.addSelf( vector );
144

145
		return this;
146

147
	},
148

149
	expandByScalar: function ( scalar ) {
150

151 152
		this.min.addScalar( -scalar );
		this.max.addScalar( scalar );
153

154
		return this;
155

156
	},
157

158
	containsPoint: function ( point ) {
159 160

		if ( ( this.min.x <= point.x ) && ( point.x <= this.max.x ) &&
A
alteredq 已提交
161 162
			 ( this.min.y <= point.y ) && ( point.y <= this.max.y ) &&
			 ( this.min.z <= point.z ) && ( point.z <= this.max.z ) ) {
163

164
			return true;
165

166
		}
167

168
		return false;
169

170
	},
171

172
	containsBox: function ( box ) {
173 174

		if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
A
alteredq 已提交
175 176
			 ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
			 ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
177

178
			return true;
179

180
		}
181

182
		return false;
183

184
	},
185

186
	getParameter: function ( point ) {
187

188 189
		// This can potentially have a divide by zero if the box
		// has a size dimension of 0.
190

191 192 193 194
		return new THREE.Vector3(
			( point.x - this.min.x ) / ( this.max.x - this.min.x ),
			( point.y - this.min.y ) / ( this.max.y - this.min.y ),
			( point.z - this.min.z ) / ( this.max.z - this.min.z )
195 196 197 198
		);

	},

199
	isIntersectionBox: function ( box ) {
200 201 202

		// using 6 splitting planes to rule out intersections.

B
Ben Houston 已提交
203 204 205
		if ( ( box.max.x < this.min.x ) || ( box.min.x > this.max.x ) ||
			 ( box.max.y < this.min.y ) || ( box.min.y > this.max.y ) ||
			 ( box.max.z < this.min.z ) || ( box.min.z > this.max.z ) ) {
206 207 208 209 210 211 212

			return false;

		}

		return true;

213
	},
214

215
	clampPoint: function ( point, optionalTarget ) {
216

217
		var result = optionalTarget || new THREE.Vector3();
218
		return new THREE.Vector3().copy( point ).clampSelf( this.min, this.max );
219

220
	},
221

222
	distanceToPoint: function ( point ) {
223

224 225
		var clampedPoint = THREE.Box3.__v1.copy( point ).clampSelf( this.min, this.max );
		return clampedPoint.subSelf( point ).length();
226

227
	},
228

229
	getBoundingSphere: function ( optionalTarget ) {
230 231 232 233 234 235 236 237 238 239

		var result = optionalTarget || new THREE.Sphere();
		
		result.center = this.center();
		result.radius = this.size( THREE.Box3.__v0 ).length() * 0.5;;

		return result;

	},

240
	intersect: function ( box ) {
241 242 243

		this.min.maxSelf( box.min );
		this.max.minSelf( box.max );
244

245
		return this;
246

247
	},
248

249
	union: function ( box ) {
250 251 252 253

		this.min.minSelf( box.min );
		this.max.maxSelf( box.max );

254
		return this;
255

256
	},
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	transform: function ( matrix ) {
		
		// NOTE: I am using a binary pattern to specify all 2^3 combinations below
		var newPoints = [
			matrix.multiplyVector3( THREE.Box3.__v0.set( this.min.x, this.min.y, this.min.z ) ), // 000
			matrix.multiplyVector3( THREE.Box3.__v1.set( this.min.x, this.min.y, this.max.z ) ), // 001
			matrix.multiplyVector3( THREE.Box3.__v2.set( this.min.x, this.max.y, this.min.z ) ), // 010
			matrix.multiplyVector3( THREE.Box3.__v3.set( this.min.x, this.max.y, this.max.z ) ), // 011
			matrix.multiplyVector3( THREE.Box3.__v4.set( this.max.x, this.min.y, this.min.z ) ), // 100
			matrix.multiplyVector3( THREE.Box3.__v5.set( this.max.x, this.min.y, this.max.z ) ), // 101
			matrix.multiplyVector3( THREE.Box3.__v6.set( this.max.x, this.max.y, this.min.z ) ), // 110
			matrix.multiplyVector3( THREE.Box3.__v7.set( this.max.x, this.max.y, this.max.z ) )  // 111
		];

		this.makeEmpty();
		this.setFromPoints( newPoints );
274

275
		return this;
276

277
	},
278

279
	translate: function ( offset ) {
B
Ben Houston 已提交
280

281 282
		this.min.addSelf( offset );
		this.max.addSelf( offset );
B
Ben Houston 已提交
283 284

		return this;
285

286 287 288 289 290 291 292 293 294 295 296 297
	},

	equals: function ( box ) {

		return box.min.equals( this.min ) && box.max.equals( this.max );

	},

	clone: function () {

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

298
	}
299

300 301
};

302
THREE.Box3.__v0 = new THREE.Vector3();
303
THREE.Box3.__v1 = new THREE.Vector3();
304 305 306 307 308 309
THREE.Box3.__v2 = new THREE.Vector3();
THREE.Box3.__v3 = new THREE.Vector3();
THREE.Box3.__v4 = new THREE.Vector3();
THREE.Box3.__v5 = new THREE.Vector3();
THREE.Box3.__v6 = new THREE.Vector3();
THREE.Box3.__v7 = new THREE.Vector3();