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

THREE.Box2 = function ( min, max ) {

A
alteredq 已提交
7
	if ( min === undefined && max === undefined ) {
8

B
Ben Houston 已提交
9 10 11
		this.min = new THREE.Vector2();
		this.max = new THREE.Vector2();
		this.makeEmpty();
12

13
	} else {
14 15 16 17 18 19 20
		this.min = min.clone();
		if( max === undefined ) {
			this.max = new THREE.Vector2().copy( this.min ); // This is done on purpose so you can make a box using a single point and then expand it.
		}
		else {
			this.max = max.clone();
		}
B
Ben Houston 已提交
21
	}
22

B
Ben Houston 已提交
23 24 25 26 27 28 29 30
};

THREE.Box2.prototype = {

	constructor: THREE.Box2,

	set: function ( min, max ) {

31 32
		this.min.copy( min );
		this.max.copy( max );
B
Ben Houston 已提交
33 34 35 36 37 38

		return this;
	},

	setFromPoints: function ( points ) {

39 40 41
		if ( points.length > 0 ) {

			var p = points[ 0 ];
B
Ben Houston 已提交
42 43 44 45

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

46
			for ( var i = 1, il = points.length; i < il; i ++ ) {
B
Ben Houston 已提交
47

48
				p = points[ i ];
B
Ben Houston 已提交
49 50

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

B
Ben Houston 已提交
52
					this.min.x = p.x;
53 54 55

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

B
Ben Houston 已提交
56
					this.max.x = p.x;
57

B
Ben Houston 已提交
58 59 60
				}

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

B
Ben Houston 已提交
62
					this.min.y = p.y;
63 64 65

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

B
Ben Houston 已提交
66
					this.max.y = p.y;
67

B
Ben Houston 已提交
68
				}
69

B
Ben Houston 已提交
70
			}
71 72 73

		} else {

B
Ben Houston 已提交
74
			this.makeEmpty();
75

B
Ben Houston 已提交
76 77 78
		}

		return this;
79

B
Ben Houston 已提交
80 81 82 83
	},

	setFromCenterAndSize: function ( center, size ) {

84
		var halfSize = THREE.Box2.__v1.copy( size ).multiplyScalar( 0.5 );
B
Ben Houston 已提交
85 86 87
		this.min.copy( center ).subSelf( halfSize );
		this.max.copy( center ).addSelf( halfSize );

88 89
		return this;

B
Ben Houston 已提交
90 91 92 93 94 95 96 97
	},

	copy: function ( box ) {

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

		return this;
98

B
Ben Houston 已提交
99 100 101 102
	},

	makeEmpty: function () {

103 104
		this.min.x = this.min.y = Infinity;
		this.max.x = this.max.y = -Infinity;
B
Ben Houston 已提交
105 106

		return this;
107

B
Ben Houston 已提交
108 109 110 111 112
	},

	empty: function () {

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

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

B
Ben Houston 已提交
116 117
	},

118
	volume: function () {
B
Ben Houston 已提交
119

120 121
		return ( this.max.x - this.min.x ) * ( this.max.y - this.min.y );

B
Ben Houston 已提交
122 123 124 125 126
	},

	center: function () {

		return new THREE.Vector2().add( this.min, this.max ).multiplyScalar( 0.5 );
127

B
Ben Houston 已提交
128 129 130 131 132
	},

	size: function () {

		return new THREE.Vector2().sub( this.max, this.min );
133

B
Ben Houston 已提交
134 135 136 137
	},

	expandByPoint: function ( point ) {

138
		this.min.minSelf( point );
B
Ben Houston 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		this.max.maxSelf( point );

		return this;
	},

	expandByVector: function ( vector ) {

		this.min.subSelf( vector );
		this.max.addSelf( vector );

		return this;
	},

	expandByScalar: function ( scalar ) {

		this.min.addScalar( -scalar );
		this.max.addScalar( scalar );
156

B
Ben Houston 已提交
157 158 159 160
		return this;
	},

	containsPoint: function ( point ) {
161 162

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

B
Ben Houston 已提交
165
			return true;
166

B
Ben Houston 已提交
167
		}
168

B
Ben Houston 已提交
169
		return false;
170

B
Ben Houston 已提交
171 172 173
	},

	containsBox: function ( box ) {
174 175

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

B
Ben Houston 已提交
178
			return true;
179

B
Ben Houston 已提交
180
		}
181

B
Ben Houston 已提交
182
		return false;
183

B
Ben Houston 已提交
184 185 186
	},

	getParameter: function ( point ) {
A
alteredq 已提交
187

B
Ben Houston 已提交
188 189
		// This can potentially have a divide by zero if the box
		// has a size dimension of 0.
A
alteredq 已提交
190

B
Ben Houston 已提交
191 192 193
		return new THREE.Vector2(
			( point.x - this.min.x ) / ( this.max.x - this.min.x ),
			( point.y - this.min.y ) / ( this.max.y - this.min.y )
194
		);
A
alteredq 已提交
195

196 197 198
	},

	isIntersection: function ( box ) {
A
alteredq 已提交
199

200
		// using 6 splitting planes to rule out intersections.
A
alteredq 已提交
201

202
		if ( ( this.max.x < box.min.x ) || ( box.min.x > this.max.x ) ||
A
alteredq 已提交
203
			 ( this.max.y < box.min.y ) || ( box.min.y > this.max.y ) ) {
204 205 206 207 208 209

			return false;

		}

		return true;
A
alteredq 已提交
210

B
Ben Houston 已提交
211 212 213 214
	},

	clampPoint: function ( point ) {

215
		return new THREE.Vector2().copy( point ).clampSelf( this.min, this.max );
216

B
Ben Houston 已提交
217 218 219 220
	},

	distanceToPoint: function ( point ) {

221 222
		var clampedPoint = THREE.Box2.__v1.copy( point ).clampSelf( this.min, this.max );
		return clampedPoint.subSelf( point ).length();
223

B
Ben Houston 已提交
224 225 226 227 228 229
	},

	intersect: function ( box ) {

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

B
Ben Houston 已提交
231
		return this;
232

B
Ben Houston 已提交
233 234 235 236 237 238 239 240
	},

	union: function ( box ) {

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

		return this;
241

B
Ben Houston 已提交
242 243 244 245 246 247 248 249
	},

	translate: function ( offset ) {

		this.min.addSelf( offset );
		this.max.addSelf( offset );

		return this;
250

B
Ben Houston 已提交
251 252 253 254
	},

	scale: function ( factor ) {

255
		var sizeDeltaHalf = this.size().multiplyScalar( ( factor - 1 )  * 0.5 );
B
Ben Houston 已提交
256 257 258
		this.expandByVector( sizeDeltaHalf );

		return this;
259

260 261 262 263 264 265 266 267 268 269 270 271
	},

	equals: function ( box ) {

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

	},

	clone: function () {

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

B
Ben Houston 已提交
272
	}
273

274 275
};

276
THREE.Box2.__v1 = new THREE.Vector2();