-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththief.js
6517 lines (5061 loc) · 369 KB
/
thief.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Canvas = {};
Canvas.canvas = null;
Canvas.get = function() {
if(Canvas.canvas === null)
Canvas.canvas = document.getElementById("glcanvas");
return Canvas.canvas;
};
/**
* @class
* @classdesc Represents a 4-dimensional vector.
* @param {Number} x The x component.
* @param {Number} y The y component.
* @param {Number} z The z component.
* @param {Number} w The w component.
*/
var Vector4 = function (x,y,z,w){
this.x = x;
this.y = y;
this.z = z;
this.w = w;
};
//----------------------------------------------------------------------
/**
* Return an array representation of the vector.
* @returns {Array} An array representation of the vector.
*/
Vector4.prototype.toArray = function (){
var array = new Array(4);
array[0] = this.x;
array[1] = this.y;
array[2] = this.z;
array[3] = this.w;
return array;
};
//----------------------------------------------------------------------
/**
* Set the vector from an array.
* @param {Array} array The original array.
* @returns {Vector4} this.
*/
Vector4.prototype.fromArray = function (array){
this.x = array[0];
this.y = array[1];
this.z = array[2];
this.w = array[3];
return this;
};
//----------------------------------------------------------------------
/**
* Set the vector from another vector.
* @param {Vector4} vec The original vector.
* @returns {Vector4} this.
*/
Vector4.prototype.set = function (vec){
this.x = vec.x;
this.y = vec.y;
this.z = vec.z;
this.w = vec.w;
return this;
};
//----------------------------------------------------------------------
/**
* Return a copy of this vector.
* @returns {Vector4} The copy of this vector.
*/
Vector4.prototype.cpy = function (){
return new Vector4(this.x, this.y, this.z, this.w);
};
//----------------------------------------------------------------------
/**
* Return the length of this vector.
* @returns {Number} The length of this vector.
*/
Vector4.prototype.len = function (){
var x = this.x;
var y = this.y;
var z = this.z;
return Math.sqrt((x*x) + (y*y) + (z*z));
};
//----------------------------------------------------------------------
/**
* Return the maximum v in this vector.
* @returns {Number} The maximum v in this vector.
*/
Vector4.prototype.max = function (){
var x = this.x;
var y = this.y;
var z = this.z;
return Math.max(x , Math.max(y , z));
};
//----------------------------------------------------------------------
/**
* Divides this vector by a scalar.
* @returns {Vector4} this.
*/
Vector4.prototype.min = function (){
var x = this.x;
var y = this.y;
var z = this.z;
return Math.min(x , Math.min(y , z));
};
//----------------------------------------------------------------------
/**
* Add a vector to this vector.
* @param {Vector4} vec The other vector.
* @returns {Vector4} this.
*/
Vector4.prototype.add = function (vec){
this.x = this.x + vec.x;
this.y = this.y + vec.y;
this.z = this.z + vec.z;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Subtracts a vector to this vector.
* @param {Vector4} vec The other vector.
* @returns {Vector4} this.
*/
Vector4.prototype.sub = function (vec){
this.x = this.x - vec.x;
this.y = this.y - vec.y;
this.z = this.z - vec.z;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Add a scalar to all the components of this vector.
* @param {Number} value The scalar value.
* @returns {Vector4} this.
*/
Vector4.prototype.addScl = function (value){
this.x = this.x + value;
this.y = this.y + value;
this.z = this.z + value;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Subtracts a scalar to all the components of this vector.
* @param {Number} value The scalar value.
* @returns {Vector4} this.
*/
Vector4.prototype.subScl = function (value){
this.x = this.x - value;
this.y = this.y - value;
this.z = this.z - value;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Multiplies this vector by other vector.
* @param {Vector4} vec The other vector.
* @returns {Vector4} this.
*/
Vector4.prototype.mul = function (vec){
this.x = this.x*vec.x;
this.y = this.y*vec.y;
this.z = this.z*vec.z;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Multiplies this vector by a scalar.
* @param {Number} value The scalar value.
* @returns {Vector4} this.
*/
Vector4.prototype.mulScl = function (value){
this.x = this.x*value;
this.y = this.y*value;
this.z = this.z*value;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Divides this vector by other vector.
* @param {Vector4} vec The other vector.
* @returns {Vector4} this.
*/
Vector4.prototype.div = function (vec){
this.x = this.x/vec.x;
this.y = this.y/vec.y;
this.z = this.z/vec.z;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Divides this vector by a scalar.
* @param {Number} value The scalar value.
* @returns {Vector4} this.
*/
Vector4.prototype.divScl = function (value){
this.x = this.x/value;
this.y = this.y/value;
this.z = this.z/value;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Return the dot product between this vector and other vector.
* @param {Vector4} vec The other vector.
* @returns {Number} The dot product between this vector and other vector..
*/
Vector4.prototype.dot = function (vec){
var x1 = this.x;
var y1 = this.y;
var z1 = this.z;
var x2 = vec.x;
var y2 = vec.y;
var z2 = vec.z;
return x1*x2 + y1*y2 + z1*z2;
};
//----------------------------------------------------------------------
/**
* Calculates the cross product between this vector and other vector.
* @param {Vector4} vec The other vector.
* @returns {Vector4} this.
*/
Vector4.prototype.cross = function (vec){
var x1 = this.x;
var y1 = this.y;
var z1 = this.z;
var x2 = vec.x;
var y2 = vec.y;
var z2 = vec.z;
this.x = y1 * z2 - z1 * y2;
this.y = z1 * x2 - x1 * z2;
this.z = x1 * y2 - y1 * x2;
this.w = 0;
return this;
};
//----------------------------------------------------------------------
/**
* Return the angle in degrees relative to other vector.
* @param {Vector4} vec The other vector.
* @returns {Number} The angle in degrees relative to other vector.
*/
Vector4.prototype.ang = function (vec){
var dot = this.dot(vec);
var len1 = this.len();
var len2 = vec.len();
var cos = (dot/(len1*len2));
if(len1*len2 === 0)
return 0;
return Math.acos(cos);
};
//----------------------------------------------------------------------
/**
* Normalizes this vector.
* @returns {Vector4} this.
*/
Vector4.prototype.nor = function (){
var len = this.len();
if(len > 0){
this.x = this.x/len;
this.y = this.y/len;
this.z = this.z/len;
this.w = this.w/len;
}
return this;
};
//----------------------------------------------------------------------
/**
* Return the distance between this vector and other vector.
* @param {Vector4} vec The other vector.
* @returns {Number} The distance between this vector and other vector.
*/
Vector4.prototype.dst = function (vec){
var deltaX = vec.x-this.x;
var deltaY = vec.y-this.y;
var deltaZ = vec.z-this.z;
return Math.sqrt( (deltaX*deltaX) + (deltaY*deltaY) + (deltaZ*deltaZ) );
};
//----------------------------------------------------------------------
/**
* Tests if this vector is equal to the other vector.
* @param {Vector4} vec The other vector.
* @returns {boolean} this True if this vector is equal to the other vector.
*/
Vector4.prototype.equals = function(vec){
return (this.x === vec.x) &&
(this.y === vec.y) &&
(this.z === vec.z);
};
//----------------------------------------------------------------------
/**
* Tests if this vector is equal to the other vector, whit an epsilon error.
* @param {Vector4} vec The other vector.
* @param {Number} epsilon The error.
* @returns {boolean} this True if this vector is equal to the other vector, whit an epsilon error.
*/
Vector4.prototype.epsilonEquals = function(vec,epsilon){
// Points are equal if each component is within 'epsilon' of each other
if( (Math.abs(this.x - vec.x) <= epsilon) &&
(Math.abs(this.y - vec.y) <= epsilon) &&
(Math.abs(this.z - vec.z) <= epsilon) )
return true;
else
return false;
};
//----------------------------------------------------------------------
/**
* @class
* @extends {Vector4}
* @classdesc Represents a 3-dimensional vector.
* @param {Number} x The x component.
* @param {Number} y The y component.
* @param {Number} z The z component.
*/
var Vector3 = function (x,y,z){
Vector4.call(this,x,y,z,0);
};
Vector3.prototype = new Vector4();
Vector3.prototype.constructor = Vector3;
//----------------------------------------------------------------------
/**
* @class
* @extends {Vector3}
* @classdesc Represents a 2-dimensional vector.
* @param {Number} x The x component.
* @param {Number} y The y component.
*/
var Vector2 = function (x,y){
Vector3.call(this,x,y,0);
};
Vector2.prototype = new Vector3();
Vector2.prototype.constructor = Vector2;
//----------------------------------------------------------------------
/**
* @class
* @classdesc Represents a 4x4 matrix.
* @param {Vector4} row0 The 1st row.
* @param {Vector4} row1 The 2nd row.
* @param {Vector4} row2 The 3rd row.
* @param {Vector4} row3 The 4th row.
*/
var Matrix4 = function (row0,row1,row2,row3){
this.data = new Array(16);
this.transposed = null;
this.setRows(row0, row1, row2, row3);
// REMEMBER
// WebGL uses column-major order (transposed)
// a webGL matrix : [ [column 0] [column 1] [column 2] [column 3] ]
// NOTE:
// You can use the get(row,col) and set(row,col,value) functions
// to access the data as a row-major ordered matrix.
// NOTE:
// Here I use, column-major order.
/*
ROW-MAJOR ORDER:
row 0 | a b c d |
row 1 | e f g h |
row 2 | i j k l |
row 3 | m n o p |
COLUMN-MAJOR ORDER:
column 0 | a e i m |
column 1 | b f j n |
column 2 | c g k o |
column 3 | d h l p |
*/
};
//----------------------------------------------------------------------
/**
* Return a copy of the matrix.
* @returns {Matrix4} The copy.
*/
Matrix4.prototype.cpy = function (){
var copy = Matrix4.zeros();
copy.data = this.data.slice();
return copy;
};
//----------------------------------------------------------------------
/**
* Return an array that contains the matrix data.
* @returns {Array} An array that contains the matrix data.
*/
Matrix4.prototype.getData = function (){
return this.data;
};
//----------------------------------------------------------------------
/**
* Set the internal data array.
* @param {Array} data The data array.
*/
Matrix4.prototype.setData = function (data){
this.data = data;
};
//----------------------------------------------------------------------
/**
* Return an element of the matrix.
* @param {Number} row The row.
* @param {Number} col The col.
* @returns {Number} An element of the matrix.
*/
Matrix4.prototype.get = function (row,col){
return this.data[row+(4*col)];
};
//----------------------------------------------------------------------
/**
* Set an element of the matrix.
* @param {Number} row The row.
* @param {Number} col The col.
* @param {Number} value The value of the element.
*/
Matrix4.prototype.set = function (row,col,value){
this.data[row+(4*col)] = value;
};
//----------------------------------------------------------------------
/**
* Fill the matrix with the given rows.
* @param {Vector4} row0 The 1st row.
* @param {Vector4} row1 The 2nd row.
* @param {Vector4} row2 The 3rd row.
* @param {Vector4} row3 The 4th row.
*/
Matrix4.prototype.setRows = function (row0,row1,row2,row3){
var rows = new Array(4);
rows[0] = row0.toArray();
rows[1] = row1.toArray();
rows[2] = row2.toArray();
rows[3] = row3.toArray();
for (var row = 0; row < 4; row++)
for (var col = 0; col < 4; col++)
this.set(row,col,rows[row][col]);
};
//----------------------------------------------------------------------
/**
* Return the transposed matrix.
* @returns {Matrix4} The transposed matrix.
*/
Matrix4.prototype.transpose = function () {
if(this.transposed === null){
this.transposed = Matrix4.zeros();
for (var row = 0; row < 4; row++)
for (var col = 0; col < 4; col++)
this.transposed.set(col,row,this.get(row,col));
}
return this.transposed;
};
//----------------------------------------------------------------------
/**
* Multiply M1 by M2 and returns the result matrix.
* @param {Matrix4} M1 The 1st matrix.
* @param {Matrix4} M2 The 2nd matrix.
* @returns {Matrix4} The result matrix.
*/
Matrix4.mulMM = function (M1,M2){
// The result matrix
var result = this.zeros();
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
for (var k = 0; k < 4; k++){
result.set(i,j,result.get(i,j) + M1.get(i,k) * M2.get(k,j));
}
}
}
return result;
};
//----------------------------------------------------------------------
/**
* Multiply M by v and returns the result matrix.
* @param {Matrix4} M The matrix.
* @param {Vector4} v The vector.
* @returns {Vector4} The result vector.
*/
Matrix4.mulMV = function (M,v){
var vec = v.toArray();
// The result vector
var result = new Array(4);
for (var row = 0; row < 4; row++){
result[row] = 0;
for (var col = 0; col < 4; col++)
result[row] += M.get(row,col) * vec[col];
}
var vectorResult = new Vector4(0,0,0,0);
vectorResult.fromArray(result);
return vectorResult;
};
//----------------------------------------------------------------------
/**
* Creates a matrix filled with 0s.
* @returns {Matrix4} The result matrix.
*/
Matrix4.zeros = function(){
return new Matrix4(
new Vector4(0,0,0,0),
new Vector4(0,0,0,0),
new Vector4(0,0,0,0),
new Vector4(0,0,0,0));
};
//----------------------------------------------------------------------
/**
* Creates an identity matrix.
* @returns {Matrix4} The result matrix.
*/
Matrix4.identity = function(){
return new Matrix4(
new Vector4(1,0,0,0),
new Vector4(0,1,0,0),
new Vector4(0,0,1,0),
new Vector4(0,0,0,1));
};
//----------------------------------------------------------------------
/**
* Creates a translation matrix.
* @param {Vector3} vec The translation.
* @returns {Matrix4} The result matrix.
*/
Matrix4.translation = function(vec){
return new Matrix4(
new Vector4(1,0,0,vec.x),
new Vector4(0,1,0,vec.y),
new Vector4(0,0,1,vec.z),
new Vector4(0,0,0,1));
};
//----------------------------------------------------------------------
/**
* Creates a rotation matrix. Each component of the vector represents an axis (x,y,z),
* only one component must have a non-zero value. The other two must be zero.
* @param {Vector3} vec The rotation.
* @returns {Matrix4} The result matrix.
*/
Matrix4.rotation = function(vec){
var result = Matrix4.identity();
var rad, cos, sin;
if(vec.x !== 0){
rad = vec.x*(Math.PI/180);
sin = Math.sin(rad);
cos = Math.cos(rad);
result = new Matrix4(
new Vector4(1,0,0,0),
new Vector4(0,cos,-sin,0),
new Vector4(0,sin,cos,0),
new Vector4(0,0,0,1));
}else if(vec.y !== 0){
rad = vec.y*(Math.PI/180);
sin = Math.sin(rad);
cos = Math.cos(rad);
result = new Matrix4(
new Vector4(cos,0,sin,0),
new Vector4(0,1,0,0),
new Vector4(-sin,0,cos,0),
new Vector4(0,0,0,1));
}else if(vec.z !== 0){
rad = vec.z*(Math.PI/180);
sin = Math.sin(rad);
cos = Math.cos(rad);
result = new Matrix4(
new Vector4(cos,-sin,0,0),
new Vector4(sin,cos,0,0),
new Vector4(0,0,1,0),
new Vector4(0,0,0,1));
}
return result;
};
//----------------------------------------------------------------------
/**
* Creates a scale matrix.
* @param {Vector3} vec The scales.
* @returns {Matrix4} The result matrix.
*/
Matrix4.scale = function(vec){
return new Matrix4(
new Vector4(vec.x,0,0,0),
new Vector4(0,vec.y,0,0),
new Vector4(0,0,vec.z,0),
new Vector4(0,0,0,1));
};
//----------------------------------------------------------------------
/**
* Creates an ortographic projection matrix.
* @param {Number} left The left value.
* @param {Number} right The right value.
* @param {Number} bottom The bottom value.
* @param {Number} top The top value.
* @param {Number} near The near value.
* @param {Number} far The far value.
* @returns {Matrix4} The result matrix.
*/
Matrix4.ortho = function(left, right, bottom, top, near, far){
return new Matrix4(
new Vector4(2.0/(right-left),0.0,0.0,-((right+left)/(right-left))),
new Vector4(0.0,2.0/(top-bottom),0.0,-((top+bottom)/(top-bottom))),
new Vector4(0.0,0.0,(-2.0/(far-near)),-((far+near)/(far-near))),
new Vector4(0.0,0.0,0.0,1.0));
};
//----------------------------------------------------------------------
/**
* Creates an perspective projection matrix.
* @param {Number} near The near value.
* @param {Number} far The far value.
* @param {Number} aspect The aspect value.
* @param {Number} fov The fov value.
* @returns {Matrix4} The result matrix.
*/
Matrix4.perspective = function(near, far, aspect, fov){
var top = near * Math.tan((Math.PI/180)*(fov/2));
var bottom = -top;
var right = top*aspect;
var left = -right;
return new Matrix4(
new Vector4(((2*near)/(right-left)),0,((right+left)/(right-left)),0),
new Vector4(0,((2*near)/(top-bottom)),((top+bottom)/(top-bottom)),0),
new Vector4(0,0,((far+near)/(far-near)),0),
new Vector4(0,0,-1,0));
};
//----------------------------------------------------------------------
Matrix4.prototype.print = function () {
string = "";
for (var row = 0; row < 4; row++){
for (var col = 0; col < 4; col++)
string += this.get(row,col) + " ";
string += "\n";
}
console.log(string);
};
/**
* @class
* @classdesc Provides geometry related methods.
*/
var GeometryUtil = function () {
};
//----------------------------------------------------------------------
/**
* Return if the rectangle contains the point.
* @param {Vector4} leftTopVertex The leftTopVertex of the rectangle.
* @param {Number} width The width of the rectangle.
* @param {Number} height The height of the rectangle.
* @param {Number} point The point.
* @returns {Boolean} True if the rectangle contains the point.
*/
GeometryUtil.testRectanglePoint = function(leftTopVertex,width,height,point, eps){
return (leftTopVertex.x-eps < point.x && leftTopVertex.y+eps > point.y &&
leftTopVertex.x + width +eps > point.x && leftTopVertex.y - height -eps < point.y);
};
//----------------------------------------------------------------------
/**
* Return if the spheres are colliding.
* @param {Vector4} centerA The center of the A sphere.
* @param {Vector4} radiusA The radius of the A sphere.
* @param {Number} centerB The center of the B sphere.
* @param {Number} radiusB The radius of the B sphere.
* @returns {Boolean} True if the spheres are colliding.
*/
GeometryUtil.testSphereSphere = function(centerA, centerB, radiusA, radiusB){
var distance = centerA.dst(centerB);
return (distance < radiusA+radiusB);
};
//----------------------------------------------------------------------
/**
* Return the middle point.
* @param {Vector4} a The a point.
* @param {Vector4} b The b point.
* @returns {Vector4} The middle point.
*/
GeometryUtil.midPoint = function(a,b){
return new Vector3((a.x+b.x)/2.0, (a.y+b.y)/2.0, (a.z+b.z)/2.0 );
};
//----------------------------------------------------------------------
/**
* Return the closest point in a segment to the point p.
* @param {Vector4} p The p point.
* @param {Vector4} a The a point of the segment.
* @param {Vector4} b The b point of the segment.
* @returns {Vector4} The closest point.
*/
GeometryUtil.closestPointInSegment = function (p,a,b){
var closest = null;
var subVector = b.cpy().sub(a);
if ((subVector.x === 0) && (subVector.y === 0) && (subVector.z === 0)){
// It's a point not a line segment.
// return P.sub(A).len();
return a;
}
// Calculate the t that minimizes the distance.
// Perpendicular Distance from a Point to a Line.
// var t = ((p.x - a.x) * dx + (p.y - a.y) * dy + (p.z - a.z) * dz) /
// (dx * dx + dy * dy + dz * dz);
var t = p.cpy().sub(a).dot(subVector)/(subVector.dot(subVector));
// See if this represents one of the segment's
// end points or a point in the middle.
if (t < 0){
closest = a;
}else if (t > 1){
closest = b;
}else{
closest = a.cpy().add(subVector.mulScl(t));
}
return closest;
};
//----------------------------------------------------------------------
/**
* Return the squared distance between a point and a segment.
* @param {Vector4} p The p point.
* @param {Vector4} a The a point of the segment.
* @param {Vector4} b The b point of the segment.
* @returns {Number} The squared distance.
*/
GeometryUtil.sqDistanceToSegment = function (p,a,b){
var closest = GeometryUtil.closestPointInSegment(p,a,b);
var subVector = p.cpy().sub(closest);
// return (dx * dx + dy * dy + dz * dz);
return (subVector.dot(subVector));
};
//----------------------------------------------------------------------
/**
* Return the distance between a point and a segment.
* @param {Vector4} p The p point.
* @param {Vector4} a The a point of the segment.
* @param {Vector4} b The b point of the segment.
* @returns {Number} The distance.
*/
GeometryUtil.distanceToSegment = function (p,a,b){
return Math.sqrt(GeometryUtil.sqDistanceToSegment(p,a,b));
};
//----------------------------------------------------------------------
/**
* Return the closest point in a sphere to the point p.
* @param {Vector4} p The p point.
* @param {Vector4} center The center of the sphere.
* @param {Number} b The radius of the sphere.
* @returns {Vector4} The closest point.
*/
GeometryUtil.closestPointInSphere = function (p,center,radius){
var pToCenter = p.cpy().sub(center);
var radiusVector = pToCenter.nor().mulScl(radius); // vector with length = radius
var closestPoint = center.cpy().add(radiusVector);
return closestPoint;
};
/**
* @class
* @classdesc This class represents a color.
* @param {Number} r The red component.
* @param {Number} g The green component.
* @param {Number} b The blue component.
* @param {Number} a The alpha component.
*/
var Color = function (r,g,b,a) {
// this.vec = new Vector4(r,g,b,a);
this.r = r;
this.g = g;
this.b = b;
this.a = a;
};
// PRE-DEFINED COLORS
Color.NONE = new Color(0.0, 0.0, 0.0, 0.0);
Color.RED = new Color(1.0, 0.0, 0.0, 1.0);
Color.GREEN = new Color(0.0, 1.0, 0.0, 1.0);
Color.BLUE = new Color(0.0, 0.0, 1.0, 1.0);
Color.CYAN = new Color(0.0, 1.0, 1.0, 1.0);
Color.YELLOW = new Color(1.0, 1.0, 0.0, 1.0);
//----------------------------------------------------------------------
/**
* Return a random color.
* @returns {Color} The color.
*/
Color.random = function () {
var r = Math.random();
var g = Math.random();
var b = Math.random();
// var a = Math.Random();
return new Color(r,g,b,1.0);
};
//----------------------------------------------------------------------
/**
* Return an array representation of the color.
* @returns {Array} An array representation of the color.
*/
Color.prototype.toArray = function(){
// return this.vec.toArray;
var array = new Array(4);
array[0] = this.r;
array[1] = this.g;
array[2] = this.b;
array[3] = this.a;
return array;
};
//----------------------------------------------------------------------
/**
* Tests if this color is equal to the other color.
* @param {Vector4} vec The other color.
* @returns {boolean} this True if this color is equal to the other color.
*/
Color.prototype.equals = function(otherColor){
// return this.vec.equals(otherColor.vec);
return (this.r === vec.r) &&
(this.g === vec.g) &&
(this.b === vec.b) &&
(this.a === vec.a);
};
//----------------------------------------------------------------------
/**
* @class
* @classdesc This class represents a Texture.
*/
var Texture = function (name,data){
this.name = name;
this.data = data;
};
//----------------------------------------------------------------------
/**
* Return the data of the texture.
* @returns {Image} The data.