forked from antimatter15/evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsfeat.js
5601 lines (4895 loc) · 219 KB
/
jsfeat.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
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*/
// namespace ?
var jsfeat = jsfeat || { REVISION: 'ALPHA' };
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*/
(function(global) {
"use strict";
//
// CONSTANTS
var EPSILON = 0.0000001192092896;
var FLT_MIN = 1E-37;
// implementation from CCV project
// currently working only with u8,s32,f32
var U8_t = 0x0100,
S32_t = 0x0200,
F32_t = 0x0400,
S64_t = 0x0800,
F64_t = 0x1000;
var C1_t = 0x01,
C2_t = 0x02,
C3_t = 0x03,
C4_t = 0x04;
var _data_type_size = new Int32Array([ -1, 1, 4, -1, 4, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1, -1, 8 ]);
var get_data_type = (function () {
return function(type) {
return (type & 0xFF00);
}
})();
var get_channel = (function () {
return function(type) {
return (type & 0xFF);
}
})();
var get_data_type_size = (function () {
return function(type) {
return _data_type_size[(type & 0xFF00) >> 8];
}
})();
// color conversion
var COLOR_RGBA2GRAY = 0;
var COLOR_RGB2GRAY = 1;
var COLOR_BGRA2GRAY = 2;
var COLOR_BGR2GRAY = 3;
// box blur option
var BOX_BLUR_NOSCALE = 0x01;
// svd options
var SVD_U_T = 0x01;
var SVD_V_T = 0x02;
var data_t = (function () {
function data_t(size_in_bytes, buffer) {
// we need align size to multiple of 8
this.size = ((size_in_bytes + 7) | 0) & -8;
if (typeof buffer === "undefined") {
this.buffer = new ArrayBuffer(this.size);
} else {
this.buffer = buffer;
this.size = buffer.length;
}
this.u8 = new Uint8Array(this.buffer);
this.i32 = new Int32Array(this.buffer);
this.f32 = new Float32Array(this.buffer);
this.f64 = new Float64Array(this.buffer);
}
return data_t;
})();
var matrix_t = (function () {
// columns, rows, data_type
function matrix_t(c, r, data_type, data_buffer) {
this.type = get_data_type(data_type)|0;
this.channel = get_channel(data_type)|0;
this.cols = c|0;
this.rows = r|0;
if (typeof data_buffer === "undefined") {
this.allocate();
} else {
this.buffer = data_buffer;
// data user asked for
this.data = this.type&U8_t ? this.buffer.u8 : (this.type&S32_t ? this.buffer.i32 : (this.type&F32_t ? this.buffer.f32 : this.buffer.f64));
}
}
matrix_t.prototype.allocate = function() {
// clear references
delete this.data;
delete this.buffer;
//
this.buffer = new data_t((this.cols * get_data_type_size(this.type) * this.channel) * this.rows);
this.data = this.type&U8_t ? this.buffer.u8 : (this.type&S32_t ? this.buffer.i32 : (this.type&F32_t ? this.buffer.f32 : this.buffer.f64));
}
matrix_t.prototype.copy_to = function(other) {
var od = other.data, td = this.data;
var i = 0, n = (this.cols*this.rows*this.channel)|0;
for(; i < n-4; i+=4) {
od[i] = td[i];
od[i+1] = td[i+1];
od[i+2] = td[i+2];
od[i+3] = td[i+3];
}
for(; i < n; ++i) {
od[i] = td[i];
}
}
matrix_t.prototype.resize = function(c, r, ch) {
if (typeof ch === "undefined") { ch = this.channel; }
// relocate buffer only if new size doesnt fit
var new_size = (c * get_data_type_size(this.type) * ch) * r;
if(new_size > this.buffer.size) {
this.cols = c;
this.rows = r;
this.channel = ch;
this.allocate();
} else {
this.cols = c;
this.rows = r;
this.channel = ch;
}
}
return matrix_t;
})();
var pyramid_t = (function () {
function pyramid_t(levels) {
this.levels = levels|0;
this.data = new Array(levels);
this.pyrdown = jsfeat.imgproc.pyrdown;
}
pyramid_t.prototype.allocate = function(start_w, start_h, data_type) {
var i = this.levels;
while(--i >= 0) {
this.data[i] = new matrix_t(start_w >> i, start_h >> i, data_type);
}
}
pyramid_t.prototype.build = function(input, skip_first_level) {
if (typeof skip_first_level === "undefined") { skip_first_level = true; }
// just copy data to first level
var i = 2, a = input, b = this.data[0];
if(!skip_first_level) {
var j=input.cols*input.rows;
while(--j >= 0) {
b.data[j] = input.data[j];
}
}
b = this.data[1];
this.pyrdown(a, b);
for(; i < this.levels; ++i) {
a = b;
b = this.data[i];
this.pyrdown(a, b);
}
}
return pyramid_t;
})();
var keypoint_t = (function () {
function keypoint_t(x,y,score,level,angle) {
if (typeof x === "undefined") { x=0; }
if (typeof y === "undefined") { y=0; }
if (typeof score === "undefined") { score=0; }
if (typeof level === "undefined") { level=0; }
if (typeof angle === "undefined") { angle=-1.0; }
this.x = x;
this.y = y;
this.score = score;
this.level = level;
this.angle = angle;
}
return keypoint_t;
})();
// data types
global.U8_t = U8_t;
global.S32_t = S32_t;
global.F32_t = F32_t;
global.S64_t = S64_t;
global.F64_t = F64_t;
// data channels
global.C1_t = C1_t;
global.C2_t = C2_t;
global.C3_t = C3_t;
global.C4_t = C4_t;
// popular formats
global.U8C1_t = U8_t | C1_t;
global.U8C3_t = U8_t | C3_t;
global.U8C4_t = U8_t | C4_t;
global.F32C1_t = F32_t | C1_t;
global.F32C2_t = F32_t | C2_t;
global.S32C1_t = S32_t | C1_t;
global.S32C2_t = S32_t | C2_t;
// constants
global.EPSILON = EPSILON;
global.FLT_MIN = FLT_MIN;
// color convert
global.COLOR_RGBA2GRAY = COLOR_RGBA2GRAY;
global.COLOR_RGB2GRAY = COLOR_RGB2GRAY;
global.COLOR_BGRA2GRAY = COLOR_BGRA2GRAY;
global.COLOR_BGR2GRAY = COLOR_BGR2GRAY;
// options
global.BOX_BLUR_NOSCALE = BOX_BLUR_NOSCALE;
global.SVD_U_T = SVD_U_T;
global.SVD_V_T = SVD_V_T;
global.get_data_type = get_data_type;
global.get_channel = get_channel;
global.get_data_type_size = get_data_type_size;
global.data_t = data_t;
global.matrix_t = matrix_t;
global.pyramid_t = pyramid_t;
global.keypoint_t = keypoint_t;
})(jsfeat);
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*/
(function(global) {
"use strict";
//
var cache = (function() {
// very primitive array cache, still need testing if it helps
// of course V8 has its own powerful cache sys but i'm not sure
// it caches several multichannel 640x480 buffer creations each frame
var _pool_node_t = (function () {
function _pool_node_t(size_in_bytes) {
this.next = null;
this.data = new jsfeat.data_t(size_in_bytes);
this.size = this.data.size;
this.buffer = this.data.buffer;
this.u8 = this.data.u8;
this.i32 = this.data.i32;
this.f32 = this.data.f32;
this.f64 = this.data.f64;
}
_pool_node_t.prototype.resize = function(size_in_bytes) {
delete this.data;
this.data = new jsfeat.data_t(size_in_bytes);
this.size = this.data.size;
this.buffer = this.data.buffer;
this.u8 = this.data.u8;
this.i32 = this.data.i32;
this.f32 = this.data.f32;
this.f64 = this.data.f64;
}
return _pool_node_t;
})();
var _pool_head, _pool_tail;
var _pool_size = 0;
return {
allocate: function(capacity, data_size) {
_pool_head = _pool_tail = new _pool_node_t(data_size);
for (var i = 0; i < capacity; ++i) {
var node = new _pool_node_t(data_size);
_pool_tail = _pool_tail.next = node;
_pool_size++;
}
},
get_buffer: function(size_in_bytes) {
// assume we have enough free nodes
var node = _pool_head;
_pool_head = _pool_head.next;
_pool_size--;
if(size_in_bytes > node.size) {
node.resize(size_in_bytes);
}
return node;
},
put_buffer: function(node) {
_pool_tail = _pool_tail.next = node;
_pool_size++;
}
};
})();
global.cache = cache;
// for now we dont need more than 30 buffers
// if having cache sys really helps we can add auto extending sys
cache.allocate(30, 640*4);
})(jsfeat);
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*/
(function(global) {
"use strict";
//
var math = (function() {
var qsort_stack = new Int32Array(48*2);
return {
get_gaussian_kernel: function(size, sigma, kernel, data_type) {
var i=0,x=0.0,t=0.0,sigma_x=0.0,scale_2x=0.0;
var sum = 0.0;
var kern_node = jsfeat.cache.get_buffer(size<<2);
var _kernel = kern_node.f32;//new Float32Array(size);
if((size&1) == 1 && size <= 7 && sigma <= 0) {
switch(size>>1) {
case 0:
_kernel[0] = 1.0;
sum = 1.0;
break;
case 1:
_kernel[0] = 0.25, _kernel[1] = 0.5, _kernel[2] = 0.25;
sum = 0.25+0.5+0.25;
break;
case 2:
_kernel[0] = 0.0625, _kernel[1] = 0.25, _kernel[2] = 0.375,
_kernel[3] = 0.25, _kernel[4] = 0.0625;
sum = 0.0625+0.25+0.375+0.25+0.0625;
break;
case 3:
_kernel[0] = 0.03125, _kernel[1] = 0.109375, _kernel[2] = 0.21875,
_kernel[3] = 0.28125, _kernel[4] = 0.21875, _kernel[5] = 0.109375, _kernel[6] = 0.03125;
sum = 0.03125+0.109375+0.21875+0.28125+0.21875+0.109375+0.03125;
break;
}
} else {
sigma_x = sigma > 0 ? sigma : ((size-1)*0.5 - 1.0)*0.3 + 0.8;
scale_2x = -0.5/(sigma_x*sigma_x);
for( ; i < size; ++i )
{
x = i - (size-1)*0.5;
t = Math.exp(scale_2x*x*x);
_kernel[i] = t;
sum += t;
}
}
if(data_type & jsfeat.U8_t) {
// int based kernel
sum = 256.0/sum;
for (i = 0; i < size; ++i) {
kernel[i] = (_kernel[i] * sum + 0.5)|0;
}
} else {
// classic kernel
sum = 1.0/sum;
for (i = 0; i < size; ++i) {
kernel[i] = _kernel[i] * sum;
}
}
jsfeat.cache.put_buffer(kern_node);
},
// model is 3x3 matrix_t
perspective_4point_transform: function(model, src_x0, src_y0, dst_x0, dst_y0,
src_x1, src_y1, dst_x1, dst_y1,
src_x2, src_y2, dst_x2, dst_y2,
src_x3, src_y3, dst_x3, dst_y3) {
var t1 = src_x0;
var t2 = src_x2;
var t4 = src_y1;
var t5 = t1 * t2 * t4;
var t6 = src_y3;
var t7 = t1 * t6;
var t8 = t2 * t7;
var t9 = src_y2;
var t10 = t1 * t9;
var t11 = src_x1;
var t14 = src_y0;
var t15 = src_x3;
var t16 = t14 * t15;
var t18 = t16 * t11;
var t20 = t15 * t11 * t9;
var t21 = t15 * t4;
var t24 = t15 * t9;
var t25 = t2 * t4;
var t26 = t6 * t2;
var t27 = t6 * t11;
var t28 = t9 * t11;
var t30 = 1.0 / (t21-t24 - t25 + t26 - t27 + t28);
var t32 = t1 * t15;
var t35 = t14 * t11;
var t41 = t4 * t1;
var t42 = t6 * t41;
var t43 = t14 * t2;
var t46 = t16 * t9;
var t48 = t14 * t9 * t11;
var t51 = t4 * t6 * t2;
var t55 = t6 * t14;
var Hr0 = -(t8-t5 + t10 * t11 - t11 * t7 - t16 * t2 + t18 - t20 + t21 * t2) * t30;
var Hr1 = (t5 - t8 - t32 * t4 + t32 * t9 + t18 - t2 * t35 + t27 * t2 - t20) * t30;
var Hr2 = t1;
var Hr3 = (-t9 * t7 + t42 + t43 * t4 - t16 * t4 + t46 - t48 + t27 * t9 - t51) * t30;
var Hr4 = (-t42 + t41 * t9 - t55 * t2 + t46 - t48 + t55 * t11 + t51 - t21 * t9) * t30;
var Hr5 = t14;
var Hr6 = (-t10 + t41 + t43 - t35 + t24 - t21 - t26 + t27) * t30;
var Hr7 = (-t7 + t10 + t16 - t43 + t27 - t28 - t21 + t25) * t30;
t1 = dst_x0;
t2 = dst_x2;
t4 = dst_y1;
t5 = t1 * t2 * t4;
t6 = dst_y3;
t7 = t1 * t6;
t8 = t2 * t7;
t9 = dst_y2;
t10 = t1 * t9;
t11 = dst_x1;
t14 = dst_y0;
t15 = dst_x3;
t16 = t14 * t15;
t18 = t16 * t11;
t20 = t15 * t11 * t9;
t21 = t15 * t4;
t24 = t15 * t9;
t25 = t2 * t4;
t26 = t6 * t2;
t27 = t6 * t11;
t28 = t9 * t11;
t30 = 1.0 / (t21-t24 - t25 + t26 - t27 + t28);
t32 = t1 * t15;
t35 = t14 * t11;
t41 = t4 * t1;
t42 = t6 * t41;
t43 = t14 * t2;
t46 = t16 * t9;
t48 = t14 * t9 * t11;
t51 = t4 * t6 * t2;
t55 = t6 * t14;
var Hl0 = -(t8-t5 + t10 * t11 - t11 * t7 - t16 * t2 + t18 - t20 + t21 * t2) * t30;
var Hl1 = (t5 - t8 - t32 * t4 + t32 * t9 + t18 - t2 * t35 + t27 * t2 - t20) * t30;
var Hl2 = t1;
var Hl3 = (-t9 * t7 + t42 + t43 * t4 - t16 * t4 + t46 - t48 + t27 * t9 - t51) * t30;
var Hl4 = (-t42 + t41 * t9 - t55 * t2 + t46 - t48 + t55 * t11 + t51 - t21 * t9) * t30;
var Hl5 = t14;
var Hl6 = (-t10 + t41 + t43 - t35 + t24 - t21 - t26 + t27) * t30;
var Hl7 = (-t7 + t10 + t16 - t43 + t27 - t28 - t21 + t25) * t30;
// the following code computes R = Hl * inverse Hr
t2 = Hr4-Hr7*Hr5;
t4 = Hr0*Hr4;
t5 = Hr0*Hr5;
t7 = Hr3*Hr1;
t8 = Hr2*Hr3;
t10 = Hr1*Hr6;
var t12 = Hr2*Hr6;
t15 = 1.0 / (t4-t5*Hr7-t7+t8*Hr7+t10*Hr5-t12*Hr4);
t18 = -Hr3+Hr5*Hr6;
var t23 = -Hr3*Hr7+Hr4*Hr6;
t28 = -Hr1+Hr2*Hr7;
var t31 = Hr0-t12;
t35 = Hr0*Hr7-t10;
t41 = -Hr1*Hr5+Hr2*Hr4;
var t44 = t5-t8;
var t47 = t4-t7;
t48 = t2*t15;
var t49 = t28*t15;
var t50 = t41*t15;
var mat = model.data;
mat[0] = Hl0*t48+Hl1*(t18*t15)-Hl2*(t23*t15);
mat[1] = Hl0*t49+Hl1*(t31*t15)-Hl2*(t35*t15);
mat[2] = -Hl0*t50-Hl1*(t44*t15)+Hl2*(t47*t15);
mat[3] = Hl3*t48+Hl4*(t18*t15)-Hl5*(t23*t15);
mat[4] = Hl3*t49+Hl4*(t31*t15)-Hl5*(t35*t15);
mat[5] = -Hl3*t50-Hl4*(t44*t15)+Hl5*(t47*t15);
mat[6] = Hl6*t48+Hl7*(t18*t15)-t23*t15;
mat[7] = Hl6*t49+Hl7*(t31*t15)-t35*t15;
mat[8] = -Hl6*t50-Hl7*(t44*t15)+t47*t15;
},
// The current implementation was derived from *BSD system qsort():
// Copyright (c) 1992, 1993
// The Regents of the University of California. All rights reserved.
qsort: function(array, low, high, cmp) {
var isort_thresh = 7;
var t,ta,tb,tc;
var sp = 0,left=0,right=0,i=0,n=0,m=0,ptr=0,ptr2=0,d=0;
var left0=0,left1=0,right0=0,right1=0,pivot=0,a=0,b=0,c=0,swap_cnt=0;
var stack = qsort_stack;
if( (high-low+1) <= 1 ) return;
stack[0] = low;
stack[1] = high;
while( sp >= 0 ) {
left = stack[sp<<1];
right = stack[(sp<<1)+1];
sp--;
for(;;) {
n = (right - left) + 1;
if( n <= isort_thresh ) {
//insert_sort:
for( ptr = left + 1; ptr <= right; ptr++ ) {
for( ptr2 = ptr; ptr2 > left && cmp(array[ptr2],array[ptr2-1]); ptr2--) {
t = array[ptr2];
array[ptr2] = array[ptr2-1];
array[ptr2-1] = t;
}
}
break;
} else {
swap_cnt = 0;
left0 = left;
right0 = right;
pivot = left + (n>>1);
if( n > 40 ) {
d = n >> 3;
a = left, b = left + d, c = left + (d<<1);
ta = array[a],tb = array[b],tc = array[c];
left = cmp(ta, tb) ? (cmp(tb, tc) ? b : (cmp(ta, tc) ? c : a))
: (cmp(tc, tb) ? b : (cmp(ta, tc) ? a : c));
a = pivot - d, b = pivot, c = pivot + d;
ta = array[a],tb = array[b],tc = array[c];
pivot = cmp(ta, tb) ? (cmp(tb, tc) ? b : (cmp(ta, tc) ? c : a))
: (cmp(tc, tb) ? b : (cmp(ta, tc) ? a : c));
a = right - (d<<1), b = right - d, c = right;
ta = array[a],tb = array[b],tc = array[c];
right = cmp(ta, tb) ? (cmp(tb, tc) ? b : (cmp(ta, tc) ? c : a))
: (cmp(tc, tb) ? b : (cmp(ta, tc) ? a : c));
}
a = left, b = pivot, c = right;
ta = array[a],tb = array[b],tc = array[c];
pivot = cmp(ta, tb) ? (cmp(tb, tc) ? b : (cmp(ta, tc) ? c : a))
: (cmp(tc, tb) ? b : (cmp(ta, tc) ? a : c));
if( pivot != left0 ) {
t = array[pivot];
array[pivot] = array[left0];
array[left0] = t;
pivot = left0;
}
left = left1 = left0 + 1;
right = right1 = right0;
ta = array[pivot];
for(;;) {
while( left <= right && !cmp(ta, array[left]) ) {
if( !cmp(array[left], ta) ) {
if( left > left1 ) {
t = array[left1];
array[left1] = array[left];
array[left] = t;
}
swap_cnt = 1;
left1++;
}
left++;
}
while( left <= right && !cmp(array[right], ta) ) {
if( !cmp(ta, array[right]) ) {
if( right < right1 ) {
t = array[right1];
array[right1] = array[right];
array[right] = t;
}
swap_cnt = 1;
right1--;
}
right--;
}
if( left > right ) break;
t = array[left];
array[left] = array[right];
array[right] = t;
swap_cnt = 1;
left++;
right--;
}
if( swap_cnt == 0 ) {
left = left0, right = right0;
//goto insert_sort;
for( ptr = left + 1; ptr <= right; ptr++ ) {
for( ptr2 = ptr; ptr2 > left && cmp(array[ptr2],array[ptr2-1]); ptr2--) {
t = array[ptr2];
array[ptr2] = array[ptr2-1];
array[ptr2-1] = t;
}
}
break;
}
n = Math.min( (left1 - left0), (left - left1) );
m = (left-n)|0;
for( i = 0; i < n; ++i,++m ) {
t = array[left0+i];
array[left0+i] = array[m];
array[m] = t;
}
n = Math.min( (right0 - right1), (right1 - right) );
m = (right0-n+1)|0;
for( i = 0; i < n; ++i,++m ) {
t = array[left+i];
array[left+i] = array[m];
array[m] = t;
}
n = (left - left1);
m = (right1 - right);
if( n > 1 ) {
if( m > 1 ) {
if( n > m ) {
++sp;
stack[sp<<1] = left0;
stack[(sp<<1)+1] = left0 + n - 1;
left = right0 - m + 1, right = right0;
} else {
++sp;
stack[sp<<1] = right0 - m + 1;
stack[(sp<<1)+1] = right0;
left = left0, right = left0 + n - 1;
}
} else {
left = left0, right = left0 + n - 1;
}
}
else if( m > 1 )
left = right0 - m + 1, right = right0;
else
break;
}
}
}
},
median: function(array, low, high) {
var w;
var middle=0,ll=0,hh=0,median=(low+high)>>1;
for (;;) {
if (high <= low) return array[median];
if (high == (low + 1)) {
if (array[low] > array[high]) {
w = array[low];
array[low] = array[high];
array[high] = w;
}
return array[median];
}
middle = ((low + high) >> 1);
if (array[middle] > array[high]) {
w = array[middle];
array[middle] = array[high];
array[high] = w;
}
if (array[low] > array[high]) {
w = array[low];
array[low] = array[high];
array[high] = w;
}
if (array[middle] > array[low]) {
w = array[middle];
array[middle] = array[low];
array[low] = w;
}
ll = (low + 1);
w = array[middle];
array[middle] = array[ll];
array[ll] = w;
hh = high;
for (;;) {
do ++ll; while (array[low] > array[ll]);
do --hh; while (array[hh] > array[low]);
if (hh < ll) break;
w = array[ll];
array[ll] = array[hh];
array[hh] = w;
}
w = array[low];
array[low] = array[hh];
array[hh] = w;
if (hh <= median)
low = ll;
else if (hh >= median)
high = (hh - 1);
}
return 0;
}
};
})();
global.math = math;
})(jsfeat);
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*
*/
(function(global) {
"use strict";
//
var matmath = (function() {
return {
identity: function(M, value) {
if (typeof value === "undefined") { value=1; }
var src=M.data;
var rows=M.rows, cols=M.cols, cols_1=(cols+1)|0;
var len = rows * cols;
var k = len;
while(--len >= 0) src[len] = 0.0;
len = k;
k = 0;
while(k < len) {
src[k] = value;
k = k + cols_1;
}
},
transpose: function(At, A) {
var i=0,j=0,nrows=A.rows,ncols=A.cols;
var Ai=0,Ati=0,pAt=0;
var ad=A.data,atd=At.data;
for (; i < nrows; Ati += 1, Ai += ncols, i++) {
pAt = Ati;
for (j = 0; j < ncols; pAt += nrows, j++) atd[pAt] = ad[Ai+j];
}
},
// C = A * B
multiply: function(C, A, B) {
var i=0,j=0,k=0;
var Ap=0,pA=0,pB=0,p_B=0,Cp=0;
var ncols=A.cols,nrows=A.rows,mcols=B.cols;
var ad=A.data,bd=B.data,cd=C.data;
var sum=0.0;
for (; i < nrows; Ap += ncols, i++) {
for (p_B = 0, j = 0; j < mcols; Cp++, p_B++, j++) {
pB = p_B;
pA = Ap;
sum = 0.0;
for (k = 0; k < ncols; pA++, pB += mcols, k++) {
sum += ad[pA] * bd[pB];
}
cd[Cp] = sum;
}
}
},
// C = A * B'
multiply_ABt: function(C, A, B) {
var i=0,j=0,k=0;
var Ap=0,pA=0,pB=0,Cp=0;
var ncols=A.cols,nrows=A.rows,mrows=B.rows;
var ad=A.data,bd=B.data,cd=C.data;
var sum=0.0;
for (; i < nrows; Ap += ncols, i++) {
for (pB = 0, j = 0; j < mrows; Cp++, j++) {
pA = Ap;
sum = 0.0;
for (k = 0; k < ncols; pA++, pB++, k++) {
sum += ad[pA] * bd[pB];
}
cd[Cp] = sum;
}
}
},
// C = A' * B
multiply_AtB: function(C, A, B) {
var i=0,j=0,k=0;
var Ap=0,pA=0,pB=0,p_B=0,Cp=0;
var ncols=A.cols,nrows=A.rows,mcols=B.cols;
var ad=A.data,bd=B.data,cd=C.data;
var sum=0.0;
for (; i < ncols; Ap++, i++) {
for (p_B = 0, j = 0; j < mcols; Cp++, p_B++, j++) {
pB = p_B;
pA = Ap;
sum = 0.0;
for (k = 0; k < nrows; pA += ncols, pB += mcols, k++) {
sum += ad[pA] * bd[pB];
}
cd[Cp] = sum;
}
}
},
// C = A * A'
multiply_AAt: function(C, A) {
var i=0,j=0,k=0;
var pCdiag=0,p_A=0,pA=0,pB=0,pC=0,pCt=0;
var ncols=A.cols,nrows=A.rows;
var ad=A.data,cd=C.data;
var sum=0.0;
for (; i < nrows; pCdiag += nrows + 1, p_A = pA, i++) {
pC = pCdiag;
pCt = pCdiag;
pB = p_A;
for (j = i; j < nrows; pC++, pCt += nrows, j++) {
pA = p_A;
sum = 0.0;
for (k = 0; k < ncols; k++) {
sum += ad[pA++] * ad[pB++];
}
cd[pC] = sum
cd[pCt] = sum;
}
}
},
// C = A' * A
multiply_AtA: function(C, A) {
var i=0,j=0,k=0;
var p_A=0,pA=0,pB=0,p_C=0,pC=0,p_CC=0;
var ncols=A.cols,nrows=A.rows;
var ad=A.data,cd=C.data;
var sum=0.0;
for (; i < ncols; p_C += ncols, i++) {
p_A = i;
p_CC = p_C + i;
pC = p_CC;
for (j = i; j < ncols; pC++, p_CC += ncols, j++) {
pA = p_A;
pB = j;
sum = 0.0;
for (k = 0; k < nrows; pA += ncols, pB += ncols, k++) {
sum += ad[pA] * ad[pB];
}
cd[pC] = sum
cd[p_CC] = sum;
}
}
},
// various small matrix operations
identity_3x3: function(M, value) {
if (typeof value === "undefined") { value=1; }
var dt=M.data;
dt[0] = dt[4] = dt[8] = value;
dt[1] = dt[2] = dt[3] = 0;
dt[5] = dt[6] = dt[7] = 0;
},
invert_3x3: function(from, to) {
var A = from.data, invA = to.data;
var t1 = A[4];
var t2 = A[8];
var t4 = A[5];
var t5 = A[7];
var t8 = A[0];
var t9 = t8*t1;
var t11 = t8*t4;
var t13 = A[3];
var t14 = A[1];
var t15 = t13*t14;
var t17 = A[2];
var t18 = t13*t17;
var t20 = A[6];
var t21 = t20*t14;
var t23 = t20*t17;
var t26 = 1.0/(t9*t2-t11*t5-t15*t2+t18*t5+t21*t4-t23*t1);
invA[0] = (t1*t2-t4*t5)*t26;
invA[1] = -(t14*t2-t17*t5)*t26;
invA[2] = -(-t14*t4+t17*t1)*t26;
invA[3] = -(t13*t2-t4*t20)*t26;
invA[4] = (t8*t2-t23)*t26;
invA[5] = -(t11-t18)*t26;
invA[6] = -(-t13*t5+t1*t20)*t26;
invA[7] = -(t8*t5-t21)*t26;
invA[8] = (t9-t15)*t26;
},
// C = A * B
multiply_3x3: function(C, A, B) {
var Cd=C.data, Ad=A.data, Bd=B.data;
var m1_0 = Ad[0], m1_1 = Ad[1], m1_2 = Ad[2];
var m1_3 = Ad[3], m1_4 = Ad[4], m1_5 = Ad[5];
var m1_6 = Ad[6], m1_7 = Ad[7], m1_8 = Ad[8];
var m2_0 = Bd[0], m2_1 = Bd[1], m2_2 = Bd[2];
var m2_3 = Bd[3], m2_4 = Bd[4], m2_5 = Bd[5];
var m2_6 = Bd[6], m2_7 = Bd[7], m2_8 = Bd[8];
Cd[0] = m1_0 * m2_0 + m1_1 * m2_3 + m1_2 * m2_6;
Cd[1] = m1_0 * m2_1 + m1_1 * m2_4 + m1_2 * m2_7;
Cd[2] = m1_0 * m2_2 + m1_1 * m2_5 + m1_2 * m2_8;
Cd[3] = m1_3 * m2_0 + m1_4 * m2_3 + m1_5 * m2_6;
Cd[4] = m1_3 * m2_1 + m1_4 * m2_4 + m1_5 * m2_7;
Cd[5] = m1_3 * m2_2 + m1_4 * m2_5 + m1_5 * m2_8;
Cd[6] = m1_6 * m2_0 + m1_7 * m2_3 + m1_8 * m2_6;
Cd[7] = m1_6 * m2_1 + m1_7 * m2_4 + m1_8 * m2_7;
Cd[8] = m1_6 * m2_2 + m1_7 * m2_5 + m1_8 * m2_8;
},
mat3x3_determinant: function(M) {
var md=M.data;
return md[0] * md[4] * md[8] -
md[0] * md[5] * md[7] -
md[3] * md[1] * md[8] +
md[3] * md[2] * md[7] +
md[6] * md[1] * md[5] -
md[6] * md[2] * md[4];
},
determinant_3x3: function(M11, M12, M13,
M21, M22, M23,
M31, M32, M33) {
return M11 * M22 * M33 - M11 * M23 * M32 -
M21 * M12 * M33 + M21 * M13 * M32 +
M31 * M12 * M23 - M31 * M13 * M22;
}
};
})();
global.matmath = matmath;
})(jsfeat);
/**
* @author Eugene Zatepyakin / http://inspirit.ru/
*
*/
(function(global) {
"use strict";
//
var linalg = (function() {
var swap = function(A, i0, i1, t) {
t = A[i0];
A[i0] = A[i1];
A[i1] = t;
}
var hypot = function(a, b) {
a = Math.abs(a);
b = Math.abs(b);
if( a > b ) {
b /= a;
return a*Math.sqrt(1.0 + b*b);
}
if( b > 0 ) {
a /= b;
return b*Math.sqrt(1.0 + a*a);
}
return 0.0;
}
var JacobiImpl = function(A, astep, W, V, vstep, n) {
var eps = jsfeat.EPSILON;
var i=0,j=0,k=0,m=0,l=0,idx=0,_in=0,_in2=0;
var iters=0,max_iter=n*n*30;
var mv=0.0,val=0.0,p=0.0,y=0.0,t=0.0,s=0.0,c=0.0,a0=0.0,b0=0.0;