Vector4.js 11.1 KB
Newer Older
1 2
/**
 * @author supereggbert / http://www.paulbrunt.co.uk/
M
Mr.doob 已提交
3
 * @author philogb / http://blog.thejit.org/
4
 * @author mikael emtinger / http://gomo.se/
M
Mr.doob 已提交
5
 * @author egraether / http://egraether.com/
W
WestLangley 已提交
6
 * @author WestLangley / http://github.com/WestLangley
7 8 9 10
 */

THREE.Vector4 = function ( x, y, z, w ) {

11 12 13
	this.x = x || 0;
	this.y = y || 0;
	this.z = z || 0;
14
	this.w = ( w !== undefined ) ? w : 1;
15 16 17

};

18 19 20
THREE.Vector4.prototype = {

	constructor: THREE.Vector4,
21

22
	set: function ( x, y, z, w ) {
23

24 25 26 27
		this.x = x;
		this.y = y;
		this.z = z;
		this.w = w;
28

29
		return this;
30

31
	},
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	setX: function ( x ) {

		this.x = x;

		return this;

	},

	setY: function ( y ) {

		this.y = y;

		return this;

	},

	setZ: function ( z ) {

		this.z = z;

		return this;

	},

	setW: function ( w ) {

		this.w = w;

		return this;

	},

M
Mr.doob 已提交
65
	setComponent: function ( index, value ) {
B
Ben Houston 已提交
66

M
Mr.doob 已提交
67
		switch ( index ) {
B
Ben Houston 已提交
68

M
Mr.doob 已提交
69 70 71 72
			case 0: this.x = value; break;
			case 1: this.y = value; break;
			case 2: this.z = value; break;
			case 3: this.w = value; break;
73
			default: throw new Error( 'index is out of range: ' + index );
74

M
Mr.doob 已提交
75
		}
B
Ben Houston 已提交
76

M
Mr.doob 已提交
77
	},
78

M
Mr.doob 已提交
79
	getComponent: function ( index ) {
80

M
Mr.doob 已提交
81
		switch ( index ) {
82

M
Mr.doob 已提交
83 84 85 86
			case 0: return this.x;
			case 1: return this.y;
			case 2: return this.z;
			case 3: return this.w;
87
			default: throw new Error( 'index is out of range: ' + index );
88

M
Mr.doob 已提交
89
		}
90

M
Mr.doob 已提交
91
	},
92

93
	copy: function ( v ) {
M
Mr.doob 已提交
94

95 96 97
		this.x = v.x;
		this.y = v.y;
		this.z = v.z;
98
		this.w = ( v.w !== undefined ) ? v.w : 1;
99

100 101
		return this;

102
	},
103

104 105 106 107
	add: function ( v, w ) {

		if ( w !== undefined ) {

108
			console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
109 110 111
			return this.addVectors( v, w );

		}
112 113 114 115 116 117 118 119 120 121

		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
		this.w += v.w;

		return this;

	},

122 123 124 125 126 127 128 129 130 131 132
	addScalar: function ( s ) {

		this.x += s;
		this.y += s;
		this.z += s;
		this.w += s;

		return this;

	},

133
	addVectors: function ( a, b ) {
M
Mr.doob 已提交
134

M
Mr.doob 已提交
135 136 137 138
		this.x = a.x + b.x;
		this.y = a.y + b.y;
		this.z = a.z + b.z;
		this.w = a.w + b.w;
139

140 141
		return this;

142
	},
143

G
gero3 已提交
144 145
	addScaledVector: function ( v, s ) {

G
gero3 已提交
146 147 148 149
		this.x += v.x * s;
		this.y += v.y * s;
		this.z += v.z * s;
		this.w += v.w * s;
G
gero3 已提交
150

G
gero3 已提交
151
		return this;
G
gero3 已提交
152

G
gero3 已提交
153
	},
154

155 156 157 158
	sub: function ( v, w ) {

		if ( w !== undefined ) {

159
			console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
160 161 162
			return this.subVectors( v, w );

		}
M
Mr.doob 已提交
163

164 165 166 167
		this.x -= v.x;
		this.y -= v.y;
		this.z -= v.z;
		this.w -= v.w;
168

M
Mr.doob 已提交
169
		return this;
M
Mr.doob 已提交
170

M
Mr.doob 已提交
171 172
	},

173 174 175 176 177 178 179 180 181 182 183
	subScalar: function ( s ) {

		this.x -= s;
		this.y -= s;
		this.z -= s;
		this.w -= s;

		return this;

	},

184
	subVectors: function ( a, b ) {
M
Mr.doob 已提交
185

M
Mr.doob 已提交
186 187 188 189
		this.x = a.x - b.x;
		this.y = a.y - b.y;
		this.z = a.z - b.z;
		this.w = a.w - b.w;
190

B
Ben Nolan 已提交
191 192
		return this;

193
	},
194

195
	multiplyScalar: function ( scalar ) {
M
Mr.doob 已提交
196

197 198 199 200
		this.x *= scalar;
		this.y *= scalar;
		this.z *= scalar;
		this.w *= scalar;
201

202 203
		return this;

204
	},
205

206
	applyMatrix4: function ( m ) {
207 208 209 210 211 212 213 214

		var x = this.x;
		var y = this.y;
		var z = this.z;
		var w = this.w;

		var e = m.elements;

215 216 217 218
		this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
		this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
		this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
		this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
219 220 221 222 223

		return this;

	},

224
	divideScalar: function ( scalar ) {
M
Mr.doob 已提交
225

226
		if ( scalar !== 0 ) {
M
Mr.doob 已提交
227

228 229 230 231 232 233
			var invScalar = 1 / scalar;

			this.x *= invScalar;
			this.y *= invScalar;
			this.z *= invScalar;
			this.w *= invScalar;
234

M
Mr.doob 已提交
235 236
		} else {

237 238 239 240
			this.x = 0;
			this.y = 0;
			this.z = 0;
			this.w = 1;
M
Mr.doob 已提交
241 242

		}
243

B
Ben Nolan 已提交
244 245
		return this;

246
	},
M
Mr.doob 已提交
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	setAxisAngleFromQuaternion: function ( q ) {

		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm

		// q is assumed to be normalized

		this.w = 2 * Math.acos( q.w );

		var s = Math.sqrt( 1 - q.w * q.w );

		if ( s < 0.0001 ) {

			 this.x = 1;
			 this.y = 0;
			 this.z = 0;

		} else {

			 this.x = q.x / s;
			 this.y = q.y / s;
			 this.z = q.z / s;

		}

		return this;

	},

	setAxisAngleFromRotationMatrix: function ( m ) {

		// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm

		// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)

		var angle, x, y, z,		// variables for result
			epsilon = 0.01,		// margin to allow for rounding errors
			epsilon2 = 0.1,		// margin to distinguish between 0 and 180 degrees

			te = m.elements,

288 289 290
			m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
			m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
			m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
291 292

		if ( ( Math.abs( m12 - m21 ) < epsilon )
293 294
		   && ( Math.abs( m13 - m31 ) < epsilon )
		   && ( Math.abs( m23 - m32 ) < epsilon ) ) {
295 296 297 298 299 300

			// singularity found
			// first check for identity matrix which must have +1 for all terms
			// in leading diagonal and zero in other terms

			if ( ( Math.abs( m12 + m21 ) < epsilon2 )
301 302 303
			   && ( Math.abs( m13 + m31 ) < epsilon2 )
			   && ( Math.abs( m23 + m32 ) < epsilon2 )
			   && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

				// this singularity is identity matrix so angle = 0

				this.set( 1, 0, 0, 0 );

				return this; // zero angle, arbitrary axis

			}

			// otherwise this singularity is angle = 180

			angle = Math.PI;

			var xx = ( m11 + 1 ) / 2;
			var yy = ( m22 + 1 ) / 2;
			var zz = ( m33 + 1 ) / 2;
			var xy = ( m12 + m21 ) / 4;
			var xz = ( m13 + m31 ) / 4;
			var yz = ( m23 + m32 ) / 4;

G
gero3 已提交
324 325 326
			if ( ( xx > yy ) && ( xx > zz ) ) {

				// m11 is the largest diagonal term
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

				if ( xx < epsilon ) {

					x = 0;
					y = 0.707106781;
					z = 0.707106781;

				} else {

					x = Math.sqrt( xx );
					y = xy / x;
					z = xz / x;

				}

G
gero3 已提交
342 343 344
			} else if ( yy > zz ) {

				// m22 is the largest diagonal term
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

				if ( yy < epsilon ) {

					x = 0.707106781;
					y = 0;
					z = 0.707106781;

				} else {

					y = Math.sqrt( yy );
					x = xy / y;
					z = yz / y;

				}

G
gero3 已提交
360 361 362
			} else {

				// m33 is the largest diagonal term so base result on this
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

				if ( zz < epsilon ) {

					x = 0.707106781;
					y = 0.707106781;
					z = 0;

				} else {

					z = Math.sqrt( zz );
					x = xz / z;
					y = yz / z;

				}

			}

			this.set( x, y, z, angle );

			return this; // return 180 deg rotation

		}

		// as we have reached here there are no singularities so we can handle normally

		var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
389 390
						  + ( m13 - m31 ) * ( m13 - m31 )
						  + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

		if ( Math.abs( s ) < 0.001 ) s = 1;

		// prevent divide by zero, should not happen if matrix is orthogonal and should be
		// caught by singularity test above, but I've left it in just in case

		this.x = ( m32 - m23 ) / s;
		this.y = ( m13 - m31 ) / s;
		this.z = ( m21 - m12 ) / s;
		this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );

		return this;

	},

406
	min: function ( v ) {
407

A
alteredq 已提交
408 409
		if ( this.x > v.x ) {

410
			this.x = v.x;
A
alteredq 已提交
411

412
		}
A
alteredq 已提交
413 414 415

		if ( this.y > v.y ) {

416
			this.y = v.y;
A
alteredq 已提交
417

418
		}
A
alteredq 已提交
419 420 421

		if ( this.z > v.z ) {

422
			this.z = v.z;
A
alteredq 已提交
423

424
		}
A
alteredq 已提交
425 426 427

		if ( this.w > v.w ) {

428
			this.w = v.w;
A
alteredq 已提交
429

430 431 432 433 434 435
		}

		return this;

	},

436
	max: function ( v ) {
437

A
alteredq 已提交
438 439
		if ( this.x < v.x ) {

440
			this.x = v.x;
A
alteredq 已提交
441

442
		}
A
alteredq 已提交
443 444 445

		if ( this.y < v.y ) {

446
			this.y = v.y;
A
alteredq 已提交
447

448
		}
A
alteredq 已提交
449 450 451

		if ( this.z < v.z ) {

452
			this.z = v.z;
A
alteredq 已提交
453

454
		}
A
alteredq 已提交
455 456 457

		if ( this.w < v.w ) {

458
			this.w = v.w;
A
alteredq 已提交
459

460 461 462 463 464 465
		}

		return this;

	},

466
	clamp: function ( min, max ) {
467 468 469

		// This function assumes min < max, if this assumption isn't true it will not operate correctly

A
alteredq 已提交
470 471
		if ( this.x < min.x ) {

472
			this.x = min.x;
A
alteredq 已提交
473 474 475

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

476
			this.x = max.x;
A
alteredq 已提交
477

478 479
		}

A
alteredq 已提交
480 481
		if ( this.y < min.y ) {

482
			this.y = min.y;
A
alteredq 已提交
483 484 485

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

486
			this.y = max.y;
A
alteredq 已提交
487

488 489
		}

A
alteredq 已提交
490 491
		if ( this.z < min.z ) {

492
			this.z = min.z;
A
alteredq 已提交
493 494 495

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

496
			this.z = max.z;
A
alteredq 已提交
497

498 499
		}

A
alteredq 已提交
500 501
		if ( this.w < min.w ) {

502
			this.w = min.w;
A
alteredq 已提交
503 504 505

		} else if ( this.w > max.w ) {

506
			this.w = max.w;
A
alteredq 已提交
507

508 509 510
		}

		return this;
A
alteredq 已提交
511

512
	},
M
Mr.doob 已提交
513

514
	clampScalar: function () {
M
Mr.doob 已提交
515

516
		var min, max;
517

518
		return function clampScalar( minVal, maxVal ) {
M
Mr.doob 已提交
519 520 521

			if ( min === undefined ) {

522 523
				min = new THREE.Vector4();
				max = new THREE.Vector4();
M
Mr.doob 已提交
524

525 526
			}

M
Mr.doob 已提交
527 528 529 530
			min.set( minVal, minVal, minVal, minVal );
			max.set( maxVal, maxVal, maxVal, maxVal );

			return this.clamp( min, max );
531 532

		};
M
Mr.doob 已提交
533

534
	}(),
535

G
gero3 已提交
536
	floor: function () {
M
Mr.doob 已提交
537

M
Mr.doob 已提交
538 539 540 541
		this.x = Math.floor( this.x );
		this.y = Math.floor( this.y );
		this.z = Math.floor( this.z );
		this.w = Math.floor( this.w );
M
Mr.doob 已提交
542

M
Mr.doob 已提交
543
		return this;
M
Mr.doob 已提交
544

G
gero3 已提交
545
	},
546

G
gero3 已提交
547
	ceil: function () {
M
Mr.doob 已提交
548

M
Mr.doob 已提交
549 550 551 552
		this.x = Math.ceil( this.x );
		this.y = Math.ceil( this.y );
		this.z = Math.ceil( this.z );
		this.w = Math.ceil( this.w );
M
Mr.doob 已提交
553

M
Mr.doob 已提交
554
		return this;
M
Mr.doob 已提交
555

G
gero3 已提交
556
	},
557

G
gero3 已提交
558
	round: function () {
M
Mr.doob 已提交
559

M
Mr.doob 已提交
560 561 562 563
		this.x = Math.round( this.x );
		this.y = Math.round( this.y );
		this.z = Math.round( this.z );
		this.w = Math.round( this.w );
M
Mr.doob 已提交
564

M
Mr.doob 已提交
565
		return this;
M
Mr.doob 已提交
566

G
gero3 已提交
567
	},
568

G
gero3 已提交
569
	roundToZero: function () {
M
Mr.doob 已提交
570

M
Mr.doob 已提交
571 572 573 574
		this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
		this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
		this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
		this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
M
Mr.doob 已提交
575

M
Mr.doob 已提交
576
		return this;
M
Mr.doob 已提交
577

G
gero3 已提交
578
	},
579

M
Mr.doob 已提交
580
	negate: function () {
M
Mr.doob 已提交
581

M
Mr.doob 已提交
582 583 584 585 586 587
		this.x = - this.x;
		this.y = - this.y;
		this.z = - this.z;
		this.w = - this.w;

		return this;
M
Mr.doob 已提交
588

M
Mr.doob 已提交
589
	},
M
Mr.doob 已提交
590

591
	dot: function ( v ) {
M
Mr.doob 已提交
592 593

		return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
M
Mr.doob 已提交
594 595 596

	},

597
	lengthSq: function () {
M
Mr.doob 已提交
598

599
		return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
M
Mr.doob 已提交
600

M
Mr.doob 已提交
601
	},
M
Mr.doob 已提交
602

603
	length: function () {
M
Mr.doob 已提交
604

605
		return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
M
Mr.doob 已提交
606 607

	},
M
Mr.doob 已提交
608

609 610 611 612 613
	lengthManhattan: function () {

		return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );

	},
M
Mr.doob 已提交
614

615
	normalize: function () {
M
Mr.doob 已提交
616 617

		return this.divideScalar( this.length() );
M
Mr.doob 已提交
618

M
Mr.doob 已提交
619
	},
M
Mr.doob 已提交
620

621
	setLength: function ( l ) {
M
Mr.doob 已提交
622

623
		var oldLength = this.length();
M
Mr.doob 已提交
624 625

		if ( oldLength !== 0 && l !== oldLength ) {
626

627
			this.multiplyScalar( l / oldLength );
628

629 630 631
		}

		return this;
M
Mr.doob 已提交
632

M
Mr.doob 已提交
633
	},
634

635
	lerp: function ( v, alpha ) {
M
Mr.doob 已提交
636

637 638 639 640
		this.x += ( v.x - this.x ) * alpha;
		this.y += ( v.y - this.y ) * alpha;
		this.z += ( v.z - this.z ) * alpha;
		this.w += ( v.w - this.w ) * alpha;
641

M
Mr.doob 已提交
642 643
		return this;

M
Mr.doob 已提交
644 645
	},

W
WestLangley 已提交
646 647
	lerpVectors: function ( v1, v2, alpha ) {

G
gero3 已提交
648
		this.subVectors( v2, v1 ).multiplyScalar( alpha ).add( v1 );
W
WestLangley 已提交
649

G
gero3 已提交
650
		return this;
W
WestLangley 已提交
651 652 653

	},

654 655 656 657 658 659
	equals: function ( v ) {

		return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );

	},

660
	fromArray: function ( array, offset ) {
661

662 663 664 665 666 667
		if ( offset === undefined ) offset = 0;

		this.x = array[ offset ];
		this.y = array[ offset + 1 ];
		this.z = array[ offset + 2 ];
		this.w = array[ offset + 3 ];
668

M
Mr.doob 已提交
669 670
		return this;

671
	},
672

A
arose 已提交
673
	toArray: function ( array, offset ) {
674

A
arose 已提交
675 676
		if ( array === undefined ) array = [];
		if ( offset === undefined ) offset = 0;
677

A
arose 已提交
678 679 680 681
		array[ offset ] = this.x;
		array[ offset + 1 ] = this.y;
		array[ offset + 2 ] = this.z;
		array[ offset + 3 ] = this.w;
682

A
arose 已提交
683
		return array;
684

685 686
	},

W
WestLangley 已提交
687 688
	fromAttribute: function ( attribute, index, offset ) {

G
gero3 已提交
689
		if ( offset === undefined ) offset = 0;
W
WestLangley 已提交
690

G
gero3 已提交
691
		index = index * attribute.itemSize + offset;
W
WestLangley 已提交
692

G
gero3 已提交
693 694 695 696
		this.x = attribute.array[ index ];
		this.y = attribute.array[ index + 1 ];
		this.z = attribute.array[ index + 2 ];
		this.w = attribute.array[ index + 3 ];
W
WestLangley 已提交
697

G
gero3 已提交
698
		return this;
W
WestLangley 已提交
699 700 701

	},

M
Mr.doob 已提交
702 703 704 705
	clone: function () {

		return new THREE.Vector4( this.x, this.y, this.z, this.w );

706
	}
707

708
};