-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathVector4.js
880 lines (624 loc) · 16.3 KB
/
Vector4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/**
* @author supereggbert / http://www.paulbrunt.co.uk/
* @author philogb / http://blog.thejit.org/
* @author mikael emtinger / http://gomo.se/
* @author egraether / http://egraether.com/
* @author WestLangley / http://github.com/WestLangley
*/
/**
* @classdesc 4维向量<br />
* 注释内容部分参照 http://blog.csdn.net/omni360
* @desc w称为比例因子,当w不为0时(一般设1),表示一个坐标<br />,
* 一个三维坐标的三个分量x,y,z用齐次坐标表示为变为x,y,z,w的4维空间,变换成三维坐标是方式是x/w,y/w,z/w,<br />
* 当w为0时,在数学上代表无穷远点,即并非一个具体的坐标位置,而是一个具有大小和方向的向量<br />
* 从而,通过w我们就可以用同一系统表示两种不同的量
* @param {float} x
* @param {float} y
* @param {float} z
* @param {float} w 齐次坐标
* @class
* @example var p4d = new Vector4(5,3,2,1)
*/
THREE.Vector4 = function ( x, y, z, w ) {
/**
* @default 0
* @type {float}
*/
this.x = x || 0;
/**
* @default 0
* @type {float}
*/
this.y = y || 0;
/**
* @default 0
* @type {float}
*/
this.z = z || 0;
/**
* @default 1
* @type {float}
*/
this.w = ( w !== undefined ) ? w : 1;
};
THREE.Vector4.prototype = {
constructor: THREE.Vector4,
/**
* @desc 设置4维向量
* @param {float} x
* @param {float} y
* @param {float} z
* @param {float} w
* @returns {THREE.Vector4}
*/
set: function ( x, y, z, w ) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
return this;
},
/**
* @desc 设置X坐标
* @param {float} x
* @returns {THREE.Vector4}
*/
setX: function ( x ) {
this.x = x;
return this;
},
/**
* @desc 设置Y坐标
* @param {float} y
* @returns {THREE.Vector4}
*/
setY: function ( y ) {
this.y = y;
return this;
},
/**
* @desc 设置Z坐标
* @param {float} z
* @returns {THREE.Vector4}
*/
setZ: function ( z ) {
this.z = z;
return this;
},
/**
* @desc 设置W坐标
* @param {float} w
* @returns {THREE.Vector4}
*/
setW: function ( w ) {
this.w = w;
return this;
},
/**
* @desc 根据索引设置坐标
* @param {number} index (0,3)
* @param {float} value
*/
setComponent: function ( index, value ) {
switch ( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
case 3: this.w = value; break;
default: throw new Error( 'index is out of range: ' + index );
}
},
/**
* @desc 根据索引获取坐标
* @param {number} index (0,3)
* @returns {float}
*/
getComponent: function ( index ) {
switch ( index ) {
case 0: return this.x;
case 1: return this.y;
case 2: return this.z;
case 3: return this.w;
default: throw new Error( 'index is out of range: ' + index );
}
},
/**
* @desc 拷贝4维向量
* @param {THREE.Vector4} v
* @returns {THREE.Vector4}
*/
copy: function ( v ) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
this.w = ( v.w !== undefined ) ? v.w : 1;
return this;
},
/**
* @desc 4维向量 v , w 相加<br />
* 几何意义: 合并 v , w 分量 , v的尾到w的头
* @param {THREE.Vector4} v
* @param {THREE.Vector4} w 当w未定义,则为当前向量加v
* @returns {THREE.Vector4}
*/
add: function ( v, w ) {
if ( w !== undefined ) {
console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
return this.addVectors( v, w );
}
this.x += v.x;
this.y += v.y;
this.z += v.z;
this.w += v.w;
return this;
},
/**
* @desc @desc 4维向量x,y,z,w分量与s标量相加<br />
* 几何意义: 向量分别向 x , y ,z , w轴平移s
* @param {float} s 四个轴的偏移量
* @returns {THREE.Vector4}
*/
addScalar: function ( s ) {
this.x += s;
this.y += s;
this.z += s;
this.w += s;
return this;
},
/**
* @desc 4维向量 a + b 相加 <br />
* 几何意义: 合并a + b 分量 a的尾到b的头
* @param {THREE.Vector4} a
* @param {THREE.Vector4} b
* @returns {THREE.Vector4}
*/
addVectors: function ( a, b ) {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
this.w = a.w + b.w;
return this;
},
/**
* @desc 4维向量v ,w相减
* 几何意义: v -w 分量 ; v的尾到w的尾
* @param {THREE.Vector4} v
* @param {THREE.Vector4} w 未定义则为当前向量减v
* @returns {THREE.Vector4}
*/
sub: function ( v, w ) {
if ( w !== undefined ) {
console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
return this.subVectors( v, w );
}
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
this.w -= v.w;
return this;
},
/**
* @desc 4维向量减法
* @param {THREE.Vector4} a
* @param {THREE.Vector4} b
* @returns {THREE.Vector4}
*/
subVectors: function ( a, b ) {
this.x = a.x - b.x;
this.y = a.y - b.y;
this.z = a.z - b.z;
this.w = a.w - b.w;
return this;
},
/**
* @desc 4维向量与标量scalar的乘法<br />
* 几何意义:向量的缩放
* @param {float} scalar
* @returns {THREE.Vector4}
*/
multiplyScalar: function ( scalar ) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
this.w *= scalar;
return this;
},
/**
* @desc 4维向量与矩阵乘法
* @param {THREE.Matrix4} m 4维矩阵
* @returns {THREE.Vector4}
*/
applyMatrix4: function ( m ) {
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
var e = m.elements;
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;
return this;
},
/**
* @desc 4维向量与标量scalar的除法<br />
* 几何意义:向量的缩放<br />
* 当scalar = 0 结果为4维单位向量
* @param {float} scalar
* @returns {THREE.Vector4}
*/
divideScalar: function ( scalar ) {
if ( scalar !== 0 ) {
var invScalar = 1 / scalar;
this.x *= invScalar;
this.y *= invScalar;
this.z *= invScalar;
this.w *= invScalar;
} else {
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 1;
}
return this;
},
/**
* @desc 利用四元数设置轴角,达到坐标旋转变换的目的
* @param {THREE.Quaternion} q 四元数,必须是单位向量
* @returns {THREE.Vector4}
*/
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;
},
/**
* @desc 一个参数m(旋转矩阵),达到坐标旋转变换的目的
* @param {THREE.Matrix4} m 4*4矩阵,其3*3的区域必须是纯旋转矩阵
* @returns {THREE.Vector4}
*/
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,
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 ];
if ( ( Math.abs( m12 - m21 ) < epsilon )
&& ( Math.abs( m13 - m31 ) < epsilon )
&& ( Math.abs( m23 - m32 ) < epsilon ) ) {
// 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 )
&& ( Math.abs( m13 + m31 ) < epsilon2 )
&& ( Math.abs( m23 + m32 ) < epsilon2 )
&& ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
// 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;
if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
if ( xx < epsilon ) {
x = 0;
y = 0.707106781;
z = 0.707106781;
} else {
x = Math.sqrt( xx );
y = xy / x;
z = xz / x;
}
} else if ( yy > zz ) { // m22 is the largest diagonal term
if ( yy < epsilon ) {
x = 0.707106781;
y = 0;
z = 0.707106781;
} else {
y = Math.sqrt( yy );
x = xy / y;
z = yz / y;
}
} else { // m33 is the largest diagonal term so base result on this
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 )
+ ( m13 - m31 ) * ( m13 - m31 )
+ ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
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;
},
/**
* @desc 4维向量的(x,y,z,w)坐标值直接与参数v的(x,y,z,w)比较,返回最小值
* @param {THREE.Vector4} v
* @returns {THREE.Vector4}
*/
min: function ( v ) {
if ( this.x > v.x ) {
this.x = v.x;
}
if ( this.y > v.y ) {
this.y = v.y;
}
if ( this.z > v.z ) {
this.z = v.z;
}
if ( this.w > v.w ) {
this.w = v.w;
}
return this;
},
/**
* @desc 4维向量的(x,y,z,w)坐标值直接与参数v的(x,y,z,w)比较,返回最大值
* @param {THREE.Vector4} v
* @returns {THREE.Vector4}
*/
max: function ( v ) {
if ( this.x < v.x ) {
this.x = v.x;
}
if ( this.y < v.y ) {
this.y = v.y;
}
if ( this.z < v.z ) {
this.z = v.z;
}
if ( this.w < v.w ) {
this.w = v.w;
}
return this;
},
/**
* @desc 4维向量的(x,y,z,w)坐标值直接与参数min,max的(x,y,z,w)比较,返回返回内的值
* @param {THREE.Vector4} min
* @param {THREE.Vector4} max
* @returns {THREE.Vector4}
*/
clamp: function ( min, max ) {
// This function assumes min < max, if this assumption isn't true it will not operate correctly
if ( this.x < min.x ) {
this.x = min.x;
} else if ( this.x > max.x ) {
this.x = max.x;
}
if ( this.y < min.y ) {
this.y = min.y;
} else if ( this.y > max.y ) {
this.y = max.y;
}
if ( this.z < min.z ) {
this.z = min.z;
} else if ( this.z > max.z ) {
this.z = max.z;
}
if ( this.w < min.w ) {
this.w = min.w;
} else if ( this.w > max.w ) {
this.w = max.w;
}
return this;
},
/**
* @function
* @desc 4维向量的(x,y,z,w)坐标值直接与参数minVal,maxVal比较,返回返回内的值
* @param {float} minVal
* @param {float} maxVal
* @return {THREE.Vector4}
*/
clampScalar: ( function () {
var min, max;
return function ( minVal, maxVal ) {
if ( min === undefined ) {
min = new THREE.Vector4();
max = new THREE.Vector4();
}
min.set( minVal, minVal, minVal, minVal );
max.set( maxVal, maxVal, maxVal, maxVal );
return this.clamp( min, max );
};
} )(),
/**
* @returns {THREE.Vector4}
*/
floor: function () {
this.x = Math.floor( this.x );
this.y = Math.floor( this.y );
this.z = Math.floor( this.z );
this.w = Math.floor( this.w );
return this;
},
/**
*
* @returns {THREE.Vector4}
*/
ceil: function () {
this.x = Math.ceil( this.x );
this.y = Math.ceil( this.y );
this.z = Math.ceil( this.z );
this.w = Math.ceil( this.w );
return this;
},
/**
*
* @returns {THREE.Vector4}
*/
round: function () {
this.x = Math.round( this.x );
this.y = Math.round( this.y );
this.z = Math.round( this.z );
this.w = Math.round( this.w );
return this;
},
/**
*
* @returns {THREE.Vector4}
*/
roundToZero: function () {
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 );
return this;
},
/**
* @desc 求负向量<br />
* 几何意义:和原向量大小相等,方向相反的向量
* @returns {THREE.Vector4}
*/
negate: function () {
this.x = - this.x;
this.y = - this.y;
this.z = - this.z;
this.w = - this.w;
return this;
},
/**
* @desc 4维向量的点积<br />
* 几何意义:向量大小与向量夹角cos的积 a.b =||a|| ||b|| cos0
* @param {THREE.Vector4} v
* @returns {float}
*/
dot: function ( v ) {
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
},
/**
* @desc 求向量长度(模)的平方<br />
* 几何意义:向量两分量构成的直角三角形斜边长的平方
* @returns {float}
*/
lengthSq: function () {
return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
},
/**
* @desc 求向量长度(模)<br />
* 几何意义:向量两分量构成的直角三角形斜边长
* @returns {float}
*/
length: function () {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
},
/**
* @desc 4维向量的曼哈顿长度<br />
* 几何意义: 4维向量在各坐标轴长度和
* @returns {float}
*/
lengthManhattan: function () {
return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
},
/**
* @desc 4维向量的单位化<br />
* 几何意义:转换为长度为1,方向相同的向量
* @returns {THREE.Vector4}
*/
normalize: function () {
return this.divideScalar( this.length() );
},
/**
* @desc 按照参数l(长度)设置新的4维向量(x,y,z,w)值
* @param {float} l
* @returns {THREE.Vector4}
*/
setLength: function ( l ) {
var oldLength = this.length();
if ( oldLength !== 0 && l !== oldLength ) {
this.multiplyScalar( l / oldLength );
}
return this;
},
/**
* @desc 当前4维向量(x,y,z,w)设置为下限和参数v(x,y,z,w)设为上限 之间进行线性插值
* @param {THREE.Vector4} v
* @param {float} alpha
* @returns {THREE.Vector4}
*/
lerp: function ( v, alpha ) {
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;
return this;
},
/**
* @desc 4维向量的等号
* @param {THREE.Vector4} v
* @returns {boolean}
*/
equals: function ( v ) {
return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
},
/**
* @desc 数组转换4维向量
* @param {float[]} array 坐标数组
* @param {number} offset 偏移量
* @returns {THREE.Vector4}
*/
fromArray: function ( array, offset ) {
if ( offset === undefined ) offset = 0;
this.x = array[ offset ];
this.y = array[ offset + 1 ];
this.z = array[ offset + 2 ];
this.w = array[ offset + 3 ];
return this;
},
/**
* @desc 4维向量转换数组
* @param {float[]} array 坐标数组
* @param {number} offset 偏移量
* @returns {float[]}
*/
toArray: function ( array, offset ) {
if ( array === undefined ) array = [];
if ( offset === undefined ) offset = 0;
array[ offset ] = this.x;
array[ offset + 1 ] = this.y;
array[ offset + 2 ] = this.z;
array[ offset + 3 ] = this.w;
return array;
},
/**
* @克隆4维向量
* @returns {THREE.Vector4}
*/
clone: function () {
return new THREE.Vector4( this.x, this.y, this.z, this.w );
}
};