source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/math/Vector3.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 11.0 KB
Line 
1/**
2 * @author mrdoob / http://mrdoob.com/
3 * @author *kile / http://kile.stravaganza.org/
4 * @author philogb / http://blog.thejit.org/
5 * @author mikael emtinger / http://gomo.se/
6 * @author egraether / http://egraether.com/
7 * @author WestLangley / http://github.com/WestLangley
8 */
9
10THREE.Vector3 = function ( x, y, z ) {
11
12 this.x = x || 0;
13 this.y = y || 0;
14 this.z = z || 0;
15
16};
17
18THREE.Vector3.prototype = {
19
20 constructor: THREE.Vector3,
21
22 set: function ( x, y, z ) {
23
24 this.x = x;
25 this.y = y;
26 this.z = z;
27
28 return this;
29
30 },
31
32 setX: function ( x ) {
33
34 this.x = x;
35
36 return this;
37
38 },
39
40 setY: function ( y ) {
41
42 this.y = y;
43
44 return this;
45
46 },
47
48 setZ: function ( z ) {
49
50 this.z = z;
51
52 return this;
53
54 },
55
56 setComponent: function ( index, value ) {
57
58 switch ( index ) {
59
60 case 0: this.x = value; break;
61 case 1: this.y = value; break;
62 case 2: this.z = value; break;
63 default: throw new Error( "index is out of range: " + index );
64
65 }
66
67 },
68
69 getComponent: function ( index ) {
70
71 switch ( index ) {
72
73 case 0: return this.x;
74 case 1: return this.y;
75 case 2: return this.z;
76 default: throw new Error( "index is out of range: " + index );
77
78 }
79
80 },
81
82 copy: function ( v ) {
83
84 this.x = v.x;
85 this.y = v.y;
86 this.z = v.z;
87
88 return this;
89
90 },
91
92 add: function ( v, w ) {
93
94 if ( w !== undefined ) {
95
96 console.warn( 'DEPRECATED: Vector3\'s .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
97 return this.addVectors( v, w );
98
99 }
100
101 this.x += v.x;
102 this.y += v.y;
103 this.z += v.z;
104
105 return this;
106
107 },
108
109 addScalar: function ( s ) {
110
111 this.x += s;
112 this.y += s;
113 this.z += s;
114
115 return this;
116
117 },
118
119 addVectors: function ( a, b ) {
120
121 this.x = a.x + b.x;
122 this.y = a.y + b.y;
123 this.z = a.z + b.z;
124
125 return this;
126
127 },
128
129 sub: function ( v, w ) {
130
131 if ( w !== undefined ) {
132
133 console.warn( 'DEPRECATED: Vector3\'s .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
134 return this.subVectors( v, w );
135
136 }
137
138 this.x -= v.x;
139 this.y -= v.y;
140 this.z -= v.z;
141
142 return this;
143
144 },
145
146 subVectors: function ( a, b ) {
147
148 this.x = a.x - b.x;
149 this.y = a.y - b.y;
150 this.z = a.z - b.z;
151
152 return this;
153
154 },
155
156 multiply: function ( v, w ) {
157
158 if ( w !== undefined ) {
159
160 console.warn( 'DEPRECATED: Vector3\'s .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
161 return this.multiplyVectors( v, w );
162
163 }
164
165 this.x *= v.x;
166 this.y *= v.y;
167 this.z *= v.z;
168
169 return this;
170
171 },
172
173 multiplyScalar: function ( scalar ) {
174
175 this.x *= scalar;
176 this.y *= scalar;
177 this.z *= scalar;
178
179 return this;
180
181 },
182
183 multiplyVectors: function ( a, b ) {
184
185 this.x = a.x * b.x;
186 this.y = a.y * b.y;
187 this.z = a.z * b.z;
188
189 return this;
190
191 },
192
193 applyMatrix3: function ( m ) {
194
195 var x = this.x;
196 var y = this.y;
197 var z = this.z;
198
199 var e = m.elements;
200
201 this.x = e[0] * x + e[3] * y + e[6] * z;
202 this.y = e[1] * x + e[4] * y + e[7] * z;
203 this.z = e[2] * x + e[5] * y + e[8] * z;
204
205 return this;
206
207 },
208
209 applyMatrix4: function ( m ) {
210
211 // input: THREE.Matrix4 affine matrix
212
213 var x = this.x, y = this.y, z = this.z;
214
215 var e = m.elements;
216
217 this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
218 this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
219 this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
220
221 return this;
222
223 },
224
225 applyProjection: function ( m ) {
226
227 // input: THREE.Matrix4 projection matrix
228
229 var x = this.x, y = this.y, z = this.z;
230
231 var e = m.elements;
232 var d = 1 / ( e[3] * x + e[7] * y + e[11] * z + e[15] ); // perspective divide
233
234 this.x = ( e[0] * x + e[4] * y + e[8] * z + e[12] ) * d;
235 this.y = ( e[1] * x + e[5] * y + e[9] * z + e[13] ) * d;
236 this.z = ( e[2] * x + e[6] * y + e[10] * z + e[14] ) * d;
237
238 return this;
239
240 },
241
242 applyQuaternion: function ( q ) {
243
244 var x = this.x;
245 var y = this.y;
246 var z = this.z;
247
248 var qx = q.x;
249 var qy = q.y;
250 var qz = q.z;
251 var qw = q.w;
252
253 // calculate quat * vector
254
255 var ix = qw * x + qy * z - qz * y;
256 var iy = qw * y + qz * x - qx * z;
257 var iz = qw * z + qx * y - qy * x;
258 var iw = -qx * x - qy * y - qz * z;
259
260 // calculate result * inverse quat
261
262 this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
263 this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
264 this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
265
266 return this;
267
268 },
269
270 transformDirection: function ( m ) {
271
272 // input: THREE.Matrix4 affine matrix
273 // vector interpreted as a direction
274
275 var x = this.x, y = this.y, z = this.z;
276
277 var e = m.elements;
278
279 this.x = e[0] * x + e[4] * y + e[8] * z;
280 this.y = e[1] * x + e[5] * y + e[9] * z;
281 this.z = e[2] * x + e[6] * y + e[10] * z;
282
283 this.normalize();
284
285 return this;
286
287 },
288
289 divide: function ( v ) {
290
291 this.x /= v.x;
292 this.y /= v.y;
293 this.z /= v.z;
294
295 return this;
296
297 },
298
299 divideScalar: function ( scalar ) {
300
301 if ( scalar !== 0 ) {
302
303 var invScalar = 1 / scalar;
304
305 this.x *= invScalar;
306 this.y *= invScalar;
307 this.z *= invScalar;
308
309 } else {
310
311 this.x = 0;
312 this.y = 0;
313 this.z = 0;
314
315 }
316
317 return this;
318
319 },
320
321 min: function ( v ) {
322
323 if ( this.x > v.x ) {
324
325 this.x = v.x;
326
327 }
328
329 if ( this.y > v.y ) {
330
331 this.y = v.y;
332
333 }
334
335 if ( this.z > v.z ) {
336
337 this.z = v.z;
338
339 }
340
341 return this;
342
343 },
344
345 max: function ( v ) {
346
347 if ( this.x < v.x ) {
348
349 this.x = v.x;
350
351 }
352
353 if ( this.y < v.y ) {
354
355 this.y = v.y;
356
357 }
358
359 if ( this.z < v.z ) {
360
361 this.z = v.z;
362
363 }
364
365 return this;
366
367 },
368
369 clamp: function ( min, max ) {
370
371 // This function assumes min < max, if this assumption isn't true it will not operate correctly
372
373 if ( this.x < min.x ) {
374
375 this.x = min.x;
376
377 } else if ( this.x > max.x ) {
378
379 this.x = max.x;
380
381 }
382
383 if ( this.y < min.y ) {
384
385 this.y = min.y;
386
387 } else if ( this.y > max.y ) {
388
389 this.y = max.y;
390
391 }
392
393 if ( this.z < min.z ) {
394
395 this.z = min.z;
396
397 } else if ( this.z > max.z ) {
398
399 this.z = max.z;
400
401 }
402
403 return this;
404
405 },
406
407 negate: function () {
408
409 return this.multiplyScalar( - 1 );
410
411 },
412
413 dot: function ( v ) {
414
415 return this.x * v.x + this.y * v.y + this.z * v.z;
416
417 },
418
419 lengthSq: function () {
420
421 return this.x * this.x + this.y * this.y + this.z * this.z;
422
423 },
424
425 length: function () {
426
427 return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
428
429 },
430
431 lengthManhattan: function () {
432
433 return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
434
435 },
436
437 normalize: function () {
438
439 return this.divideScalar( this.length() );
440
441 },
442
443 setLength: function ( l ) {
444
445 var oldLength = this.length();
446
447 if ( oldLength !== 0 && l !== oldLength ) {
448
449 this.multiplyScalar( l / oldLength );
450 }
451
452 return this;
453
454 },
455
456 lerp: function ( v, alpha ) {
457
458 this.x += ( v.x - this.x ) * alpha;
459 this.y += ( v.y - this.y ) * alpha;
460 this.z += ( v.z - this.z ) * alpha;
461
462 return this;
463
464 },
465
466 cross: function ( v, w ) {
467
468 if ( w !== undefined ) {
469
470 console.warn( 'DEPRECATED: Vector3\'s .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
471 return this.crossVectors( v, w );
472
473 }
474
475 var x = this.x, y = this.y, z = this.z;
476
477 this.x = y * v.z - z * v.y;
478 this.y = z * v.x - x * v.z;
479 this.z = x * v.y - y * v.x;
480
481 return this;
482
483 },
484
485 crossVectors: function ( a, b ) {
486
487 var ax = a.x, ay = a.y, az = a.z;
488 var bx = b.x, by = b.y, bz = b.z;
489
490 this.x = ay * bz - az * by;
491 this.y = az * bx - ax * bz;
492 this.z = ax * by - ay * bx;
493
494 return this;
495
496 },
497
498 angleTo: function ( v ) {
499
500 var theta = this.dot( v ) / ( this.length() * v.length() );
501
502 // clamp, to handle numerical problems
503
504 return Math.acos( THREE.Math.clamp( theta, -1, 1 ) );
505
506 },
507
508 distanceTo: function ( v ) {
509
510 return Math.sqrt( this.distanceToSquared( v ) );
511
512 },
513
514 distanceToSquared: function ( v ) {
515
516 var dx = this.x - v.x;
517 var dy = this.y - v.y;
518 var dz = this.z - v.z;
519
520 return dx * dx + dy * dy + dz * dz;
521
522 },
523
524 setEulerFromRotationMatrix: function ( m, order ) {
525
526 console.error( "REMOVED: Vector3\'s setEulerFromRotationMatrix has been removed in favor of Euler.setFromRotationMatrix(), please update your code.");
527
528 },
529
530 setEulerFromQuaternion: function ( q, order ) {
531
532 console.error( "REMOVED: Vector3\'s setEulerFromQuaternion: has been removed in favor of Euler.setFromQuaternion(), please update your code.");
533
534 },
535
536 getPositionFromMatrix: function ( m ) {
537
538 console.warn( "DEPRECATED: Vector3\'s .getPositionFromMatrix() has been renamed to .setFromMatrixPosition(). Please update your code." );
539
540 return this.setFromMatrixPosition( m );
541
542 },
543
544 getScaleFromMatrix: function ( m ) {
545
546 console.warn( "DEPRECATED: Vector3\'s .getScaleFromMatrix() has been renamed to .setFromMatrixScale(). Please update your code." );
547
548 return this.setFromMatrixScale( m );
549 },
550
551 getColumnFromMatrix: function ( index, matrix ) {
552
553 console.warn( "DEPRECATED: Vector3\'s .getColumnFromMatrix() has been renamed to .setFromMatrixColumn(). Please update your code." );
554
555 return this.setFromMatrixColumn( index, matrix );
556
557 },
558
559 setFromMatrixPosition: function ( m ) {
560
561 this.x = m.elements[ 12 ];
562 this.y = m.elements[ 13 ];
563 this.z = m.elements[ 14 ];
564
565 return this;
566
567 },
568
569 setFromMatrixScale: function ( m ) {
570
571 var sx = this.set( m.elements[ 0 ], m.elements[ 1 ], m.elements[ 2 ] ).length();
572 var sy = this.set( m.elements[ 4 ], m.elements[ 5 ], m.elements[ 6 ] ).length();
573 var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length();
574
575 this.x = sx;
576 this.y = sy;
577 this.z = sz;
578
579 return this;
580 },
581
582 setFromMatrixColumn: function ( index, matrix ) {
583
584 var offset = index * 4;
585
586 var me = matrix.elements;
587
588 this.x = me[ offset ];
589 this.y = me[ offset + 1 ];
590 this.z = me[ offset + 2 ];
591
592 return this;
593
594 },
595
596 equals: function ( v ) {
597
598 return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
599
600 },
601
602 fromArray: function ( array ) {
603
604 this.x = array[ 0 ];
605 this.y = array[ 1 ];
606 this.z = array[ 2 ];
607
608 return this;
609
610 },
611
612 toArray: function () {
613
614 return [ this.x, this.y, this.z ];
615
616 },
617
618 clone: function () {
619
620 return new THREE.Vector3( this.x, this.y, this.z );
621
622 }
623
624};
625
626THREE.extend( THREE.Vector3.prototype, {
627
628 applyEuler: function () {
629
630 var quaternion = new THREE.Quaternion();
631
632 return function ( euler ) {
633
634 if ( euler instanceof THREE.Euler === false ) {
635
636 console.error( 'ERROR: Vector3\'s .applyEuler() now expects a Euler rotation rather than a Vector3 and order. Please update your code.' );
637
638 }
639
640 this.applyQuaternion( quaternion.setFromEuler( euler ) );
641
642 return this;
643
644 };
645
646 }(),
647
648 applyAxisAngle: function () {
649
650 var quaternion = new THREE.Quaternion();
651
652 return function ( axis, angle ) {
653
654 this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
655
656 return this;
657
658 };
659
660 }(),
661
662 projectOnVector: function () {
663
664 var v1 = new THREE.Vector3();
665
666 return function ( vector ) {
667
668 v1.copy( vector ).normalize();
669 var d = this.dot( v1 );
670 return this.copy( v1 ).multiplyScalar( d );
671
672 };
673
674 }(),
675
676 projectOnPlane: function () {
677
678 var v1 = new THREE.Vector3();
679
680 return function ( planeNormal ) {
681
682 v1.copy( this ).projectOnVector( planeNormal );
683
684 return this.sub( v1 );
685
686 }
687
688 }(),
689
690 reflect: function () {
691
692 var v1 = new THREE.Vector3();
693
694 return function ( vector ) {
695
696 v1.copy( this ).projectOnVector( vector ).multiplyScalar( 2 );
697
698 return this.subVectors( v1, this );
699
700 }
701
702 }()
703
704} );
Note: See TracBrowser for help on using the repository browser.