-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverb.js
6323 lines (6278 loc) · 229 KB
/
verb.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
// browser context
if ( typeof exports != 'object' || exports === undefined )
{
// todo fix this
var verb = exports = {};
} else {
var Worker = require('webworker-threads').Worker;
}
// web worker context
if ( typeof window != 'object'){
var global = this;
var window = global; // required for promhx
var lookup = function(className, methodName){
var obj = global;
className.split(".").forEach(function(x){
if (obj) obj = obj[ x ];
});
if (!obj) return null;
return obj[ methodName ];
}
onmessage = function( e ){
var method = lookup( e.data.className, e.data.methodName );
if (!method){
return console.error("could not find " + e.data.className + "." + e.data.methodName)
}
postMessage( { result: method.apply( null, e.data.args ), id: e.data.id } );
};
}
(function ($hx_exports) { "use strict";
$hx_exports.geom = $hx_exports.geom || {};
$hx_exports.exe = $hx_exports.exe || {};
$hx_exports.core = $hx_exports.core || {};
$hx_exports.promhx = $hx_exports.promhx || {};
var $estr = function() { return js.Boot.__string_rec(this,''); };
function $extend(from, fields) {
function Inherit() {} Inherit.prototype = from; var proto = new Inherit();
for (var name in fields) proto[name] = fields[name];
if( fields.toString !== Object.prototype.toString ) proto.toString = fields.toString;
return proto;
}
var HxOverrides = function() { };
HxOverrides.__name__ = ["HxOverrides"];
HxOverrides.iter = function(a) {
return { cur : 0, arr : a, hasNext : function() {
return this.cur < this.arr.length;
}, next : function() {
return this.arr[this.cur++];
}};
};
var Lambda = function() { };
Lambda.__name__ = ["Lambda"];
Lambda.fold = function(it,f,first) {
var $it0 = $iterator(it)();
while( $it0.hasNext() ) {
var x = $it0.next();
first = f(x,first);
}
return first;
};
var List = function() {
this.length = 0;
};
List.__name__ = ["List"];
List.prototype = {
add: function(item) {
var x = [item];
if(this.h == null) this.h = x; else this.q[1] = x;
this.q = x;
this.length++;
}
,pop: function() {
if(this.h == null) return null;
var x = this.h[0];
this.h = this.h[1];
if(this.h == null) this.q = null;
this.length--;
return x;
}
,isEmpty: function() {
return this.h == null;
}
};
var IMap = function() { };
IMap.__name__ = ["IMap"];
Math.__name__ = ["Math"];
var Type = function() { };
Type.__name__ = ["Type"];
Type.getClassName = function(c) {
var a = c.__name__;
return a.join(".");
};
var haxe = {};
haxe.ds = {};
haxe.ds.IntMap = function() {
this.h = { };
};
haxe.ds.IntMap.__name__ = ["haxe","ds","IntMap"];
haxe.ds.IntMap.__interfaces__ = [IMap];
haxe.ds.IntMap.prototype = {
set: function(key,value) {
this.h[key] = value;
}
,get: function(key) {
return this.h[key];
}
,exists: function(key) {
return this.h.hasOwnProperty(key);
}
,remove: function(key) {
if(!this.h.hasOwnProperty(key)) return false;
delete(this.h[key]);
return true;
}
};
haxe.ds.Option = { __constructs__ : ["Some","None"] };
haxe.ds.Option.Some = function(v) { var $x = ["Some",0,v]; $x.__enum__ = haxe.ds.Option; $x.toString = $estr; return $x; };
haxe.ds.Option.None = ["None",1];
haxe.ds.Option.None.toString = $estr;
haxe.ds.Option.None.__enum__ = haxe.ds.Option;
var promhx = {};
promhx.base = {};
promhx.base.AsyncBase = function(d) {
this._resolved = false;
this._pending = false;
this._errorPending = false;
this._fulfilled = false;
this._update = [];
this._error = [];
this._errored = false;
if(d != null) promhx.base.AsyncBase.link(d,this,function(x) {
return x;
});
};
promhx.base.AsyncBase.__name__ = ["promhx","base","AsyncBase"];
promhx.base.AsyncBase.link = function(current,next,f) {
current._update.push({ async : next, linkf : function(x) {
next.handleResolve(f(x));
}});
promhx.base.AsyncBase.immediateLinkUpdate(current,next,f);
};
promhx.base.AsyncBase.immediateLinkUpdate = function(current,next,f) {
if(current._errored) next.handleError(current._errorVal);
if(current._resolved && !current._pending) try {
next.handleResolve(f(current._val));
} catch( e ) {
next.handleError(e);
}
};
promhx.base.AsyncBase.linkAll = function(all,next) {
var cthen = function(arr,current,v) {
if(arr.length == 0 || promhx.base.AsyncBase.allFulfilled(arr)) {
var vals;
var _g = [];
var $it0 = $iterator(all)();
while( $it0.hasNext() ) {
var a = $it0.next();
_g.push(a == current?v:a._val);
}
vals = _g;
next.handleResolve(vals);
}
return null;
};
var $it1 = $iterator(all)();
while( $it1.hasNext() ) {
var a1 = $it1.next();
a1._update.push({ async : next, linkf : (function(f,a11,a2) {
return function(v1) {
return f(a11,a2,v1);
};
})(cthen,(function($this) {
var $r;
var _g1 = [];
var $it2 = $iterator(all)();
while( $it2.hasNext() ) {
var a21 = $it2.next();
if(a21 != a1) _g1.push(a21);
}
$r = _g1;
return $r;
}(this)),a1)});
}
if(promhx.base.AsyncBase.allFulfilled(all)) next.handleResolve((function($this) {
var $r;
var _g2 = [];
var $it3 = $iterator(all)();
while( $it3.hasNext() ) {
var a3 = $it3.next();
_g2.push(a3._val);
}
$r = _g2;
return $r;
}(this)));
};
promhx.base.AsyncBase.pipeLink = function(current,ret,f) {
var linked = false;
var linkf = function(x) {
if(!linked) {
linked = true;
var pipe_ret = f(x);
pipe_ret._update.push({ async : ret, linkf : $bind(ret,ret.handleResolve)});
promhx.base.AsyncBase.immediateLinkUpdate(pipe_ret,ret,function(x1) {
return x1;
});
}
};
current._update.push({ async : ret, linkf : linkf});
if(current._resolved && !current._pending) try {
linkf(current._val);
} catch( e ) {
ret.handleError(e);
}
};
promhx.base.AsyncBase.allResolved = function($as) {
var $it0 = $iterator($as)();
while( $it0.hasNext() ) {
var a = $it0.next();
if(!a._resolved) return false;
}
return true;
};
promhx.base.AsyncBase.allFulfilled = function($as) {
var $it0 = $iterator($as)();
while( $it0.hasNext() ) {
var a = $it0.next();
if(!a._fulfilled) return false;
}
return true;
};
promhx.base.AsyncBase.prototype = {
catchError: function(f) {
this._error.push(f);
return this;
}
,errorThen: function(f) {
this._errorMap = f;
return this;
}
,isResolved: function() {
return this._resolved;
}
,isErrored: function() {
return this._errored;
}
,isFulfilled: function() {
return this._fulfilled;
}
,isPending: function() {
return this._pending;
}
,handleResolve: function(val) {
this._resolve(val);
}
,_resolve: function(val) {
var _g = this;
if(this._pending) promhx.base.EventLoop.enqueue((function(f,a1) {
return function() {
return f(a1);
};
})($bind(this,this._resolve),val)); else {
this._resolved = true;
this._pending = true;
promhx.base.EventLoop.queue.add(function() {
_g._val = val;
var _g1 = 0;
var _g2 = _g._update;
while(_g1 < _g2.length) {
var up = _g2[_g1];
++_g1;
try {
up.linkf(val);
} catch( e ) {
up.async.handleError(e);
}
}
_g._fulfilled = true;
_g._pending = false;
});
promhx.base.EventLoop.continueOnNextLoop();
}
}
,handleError: function(error) {
this._handleError(error);
}
,_handleError: function(error) {
var _g = this;
var update_errors = function(e) {
if(_g._error.length > 0) {
var _g1 = 0;
var _g2 = _g._error;
while(_g1 < _g2.length) {
var ef = _g2[_g1];
++_g1;
ef(e);
}
} else if(_g._update.length > 0) {
var _g11 = 0;
var _g21 = _g._update;
while(_g11 < _g21.length) {
var up = _g21[_g11];
++_g11;
up.async.handleError(e);
}
} else throw e;
_g._errorPending = false;
};
if(!this._errorPending) {
this._errorPending = true;
this._errored = true;
this._errorVal = error;
promhx.base.EventLoop.queue.add(function() {
if(_g._errorMap != null) try {
_g._resolve(_g._errorMap(error));
} catch( e1 ) {
update_errors(e1);
} else update_errors(error);
});
promhx.base.EventLoop.continueOnNextLoop();
}
}
,then: function(f) {
var ret = new promhx.base.AsyncBase();
promhx.base.AsyncBase.link(this,ret,f);
return ret;
}
,unlink: function(to) {
var _g = this;
promhx.base.EventLoop.queue.add(function() {
_g._update = _g._update.filter(function(x) {
return x.async != to;
});
});
promhx.base.EventLoop.continueOnNextLoop();
}
,isLinked: function(to) {
var updated = false;
var _g = 0;
var _g1 = this._update;
while(_g < _g1.length) {
var u = _g1[_g];
++_g;
if(u.async == to) return true;
}
return updated;
}
};
promhx.Deferred = $hx_exports.promhx.Deferred = function() {
promhx.base.AsyncBase.call(this);
};
promhx.Deferred.__name__ = ["promhx","Deferred"];
promhx.Deferred.__super__ = promhx.base.AsyncBase;
promhx.Deferred.prototype = $extend(promhx.base.AsyncBase.prototype,{
resolve: function(val) {
this.handleResolve(val);
}
,throwError: function(e) {
this.handleError(e);
}
,promise: function() {
return new promhx.Promise(this);
}
,stream: function() {
return new promhx.Stream(this);
}
,publicStream: function() {
return new promhx.PublicStream(this);
}
});
promhx.Promise = $hx_exports.promhx.Promise = function(d) {
promhx.base.AsyncBase.call(this,d);
this._rejected = false;
};
promhx.Promise.__name__ = ["promhx","Promise"];
promhx.Promise.whenAll = function(itb) {
var ret = new promhx.Promise();
promhx.base.AsyncBase.linkAll(itb,ret);
return ret;
};
promhx.Promise.promise = function(_val) {
var ret = new promhx.Promise();
ret.handleResolve(_val);
return ret;
};
promhx.Promise.__super__ = promhx.base.AsyncBase;
promhx.Promise.prototype = $extend(promhx.base.AsyncBase.prototype,{
isRejected: function() {
return this._rejected;
}
,reject: function(e) {
this._rejected = true;
this.handleError(e);
}
,handleResolve: function(val) {
if(this._resolved) {
var msg = "Promise has already been resolved";
throw promhx.error.PromiseError.AlreadyResolved(msg);
}
this._resolve(val);
}
,then: function(f) {
var ret = new promhx.Promise();
promhx.base.AsyncBase.link(this,ret,f);
return ret;
}
,unlink: function(to) {
var _g = this;
promhx.base.EventLoop.queue.add(function() {
if(!_g._fulfilled) {
var msg = "Downstream Promise is not fullfilled";
_g.handleError(promhx.error.PromiseError.DownstreamNotFullfilled(msg));
} else _g._update = _g._update.filter(function(x) {
return x.async != to;
});
});
promhx.base.EventLoop.continueOnNextLoop();
}
,handleError: function(error) {
this._rejected = true;
this._handleError(error);
}
,pipe: function(f) {
var ret = new promhx.Promise();
promhx.base.AsyncBase.pipeLink(this,ret,f);
return ret;
}
,errorPipe: function(f) {
var ret = new promhx.Promise();
this.catchError(function(e) {
var piped = f(e);
piped.then($bind(ret,ret._resolve));
});
this.then($bind(ret,ret._resolve));
return ret;
}
});
promhx.Stream = $hx_exports.promhx.Stream = function(d) {
promhx.base.AsyncBase.call(this,d);
this._end_deferred = new promhx.Deferred();
this._end_promise = this._end_deferred.promise();
};
promhx.Stream.__name__ = ["promhx","Stream"];
promhx.Stream.foreach = function(itb) {
var s = new promhx.Stream();
var $it0 = $iterator(itb)();
while( $it0.hasNext() ) {
var i = $it0.next();
s.handleResolve(i);
}
s.end();
return s;
};
promhx.Stream.wheneverAll = function(itb) {
var ret = new promhx.Stream();
promhx.base.AsyncBase.linkAll(itb,ret);
return ret;
};
promhx.Stream.concatAll = function(itb) {
var ret = new promhx.Stream();
var $it0 = $iterator(itb)();
while( $it0.hasNext() ) {
var i = $it0.next();
ret.concat(i);
}
return ret;
};
promhx.Stream.mergeAll = function(itb) {
var ret = new promhx.Stream();
var $it0 = $iterator(itb)();
while( $it0.hasNext() ) {
var i = $it0.next();
ret.merge(i);
}
return ret;
};
promhx.Stream.stream = function(_val) {
var ret = new promhx.Stream();
ret.handleResolve(_val);
return ret;
};
promhx.Stream.__super__ = promhx.base.AsyncBase;
promhx.Stream.prototype = $extend(promhx.base.AsyncBase.prototype,{
then: function(f) {
var ret = new promhx.Stream();
promhx.base.AsyncBase.link(this,ret,f);
this._end_promise.then(function(x) {
ret.end();
});
return ret;
}
,detachStream: function(str) {
var filtered = [];
var removed = false;
var _g = 0;
var _g1 = this._update;
while(_g < _g1.length) {
var u = _g1[_g];
++_g;
if(u.async == str) removed = true; else filtered.push(u);
}
this._update = filtered;
return removed;
}
,first: function() {
var s = new promhx.Promise();
this.then(function(x) {
if(!s._resolved) s.handleResolve(x);
});
return s;
}
,handleResolve: function(val) {
if(!this._end && !this._pause) this._resolve(val);
}
,pause: function(set) {
if(set == null) set = !this._pause;
this._pause = set;
}
,pipe: function(f) {
var ret = new promhx.Stream();
promhx.base.AsyncBase.pipeLink(this,ret,f);
this._end_promise.then(function(x) {
ret.end();
});
return ret;
}
,errorPipe: function(f) {
var ret = new promhx.Stream();
this.catchError(function(e) {
var piped = f(e);
piped.then($bind(ret,ret._resolve));
piped._end_promise.then(($_=ret._end_promise,$bind($_,$_._resolve)));
});
this.then($bind(ret,ret._resolve));
this._end_promise.then(function(x) {
ret.end();
});
return ret;
}
,handleEnd: function() {
if(this._pending) {
promhx.base.EventLoop.queue.add($bind(this,this.handleEnd));
promhx.base.EventLoop.continueOnNextLoop();
} else if(this._end_promise._resolved) return; else {
this._end = true;
var o;
if(this._resolved) o = haxe.ds.Option.Some(this._val); else o = haxe.ds.Option.None;
this._end_promise.handleResolve(o);
this._update = [];
this._error = [];
}
}
,end: function() {
promhx.base.EventLoop.queue.add($bind(this,this.handleEnd));
promhx.base.EventLoop.continueOnNextLoop();
return this;
}
,endThen: function(f) {
return this._end_promise.then(f);
}
,filter: function(f) {
var ret = new promhx.Stream();
this._update.push({ async : ret, linkf : function(x) {
if(f(x)) ret.handleResolve(x);
}});
promhx.base.AsyncBase.immediateLinkUpdate(this,ret,function(x1) {
return x1;
});
return ret;
}
,concat: function(s) {
var ret = new promhx.Stream();
this._update.push({ async : ret, linkf : $bind(ret,ret.handleResolve)});
promhx.base.AsyncBase.immediateLinkUpdate(this,ret,function(x) {
return x;
});
this._end_promise.then(function(_) {
s.pipe(function(x1) {
ret.handleResolve(x1);
return ret;
});
s._end_promise.then(function(_1) {
ret.end();
});
});
return ret;
}
,merge: function(s) {
var ret = new promhx.Stream();
this._update.push({ async : ret, linkf : $bind(ret,ret.handleResolve)});
s._update.push({ async : ret, linkf : $bind(ret,ret.handleResolve)});
promhx.base.AsyncBase.immediateLinkUpdate(this,ret,function(x) {
return x;
});
promhx.base.AsyncBase.immediateLinkUpdate(s,ret,function(x1) {
return x1;
});
return ret;
}
});
promhx.PublicStream = $hx_exports.promhx.PublicStream = function(def) {
promhx.Stream.call(this,def);
};
promhx.PublicStream.__name__ = ["promhx","PublicStream"];
promhx.PublicStream.publicstream = function(val) {
var ps = new promhx.PublicStream();
ps.handleResolve(val);
return ps;
};
promhx.PublicStream.__super__ = promhx.Stream;
promhx.PublicStream.prototype = $extend(promhx.Stream.prototype,{
resolve: function(val) {
this.handleResolve(val);
}
,throwError: function(e) {
this.handleError(e);
}
,update: function(val) {
this.handleResolve(val);
}
});
promhx.base.EventLoop = function() { };
promhx.base.EventLoop.__name__ = ["promhx","base","EventLoop"];
promhx.base.EventLoop.enqueue = function(eqf) {
promhx.base.EventLoop.queue.add(eqf);
promhx.base.EventLoop.continueOnNextLoop();
};
promhx.base.EventLoop.set_nextLoop = function(f) {
if(promhx.base.EventLoop.nextLoop != null) throw "nextLoop has already been set"; else promhx.base.EventLoop.nextLoop = f;
return promhx.base.EventLoop.nextLoop;
};
promhx.base.EventLoop.queueEmpty = function() {
return promhx.base.EventLoop.queue.isEmpty();
};
promhx.base.EventLoop.finish = function(max_iterations) {
if(max_iterations == null) max_iterations = 1000;
var fn = null;
while(max_iterations-- > 0 && (fn = promhx.base.EventLoop.queue.pop()) != null) fn();
return promhx.base.EventLoop.queue.isEmpty();
};
promhx.base.EventLoop.clear = function() {
promhx.base.EventLoop.queue = new List();
};
promhx.base.EventLoop.f = function() {
var fn = promhx.base.EventLoop.queue.pop();
if(fn != null) fn();
if(!promhx.base.EventLoop.queue.isEmpty()) promhx.base.EventLoop.continueOnNextLoop();
};
promhx.base.EventLoop.continueOnNextLoop = function() {
if(promhx.base.EventLoop.nextLoop != null) promhx.base.EventLoop.nextLoop(promhx.base.EventLoop.f); else setImmediate(promhx.base.EventLoop.f);
};
promhx.error = {};
promhx.error.PromiseError = { __constructs__ : ["AlreadyResolved","DownstreamNotFullfilled"] };
promhx.error.PromiseError.AlreadyResolved = function(message) { var $x = ["AlreadyResolved",0,message]; $x.__enum__ = promhx.error.PromiseError; $x.toString = $estr; return $x; };
promhx.error.PromiseError.DownstreamNotFullfilled = function(message) { var $x = ["DownstreamNotFullfilled",1,message]; $x.__enum__ = promhx.error.PromiseError; $x.toString = $estr; return $x; };
var verb = {};
verb.Verb = function() { };
verb.Verb.__name__ = ["verb","Verb"];
verb.Verb.main = function() {
console.log("verb 0.2.0");
};
verb.core = {};
verb.core.KnotMultiplicity = $hx_exports.core.KnotMultiplicity = function(knot,mult) {
this.knot = knot;
this.mult = mult;
};
verb.core.KnotMultiplicity.__name__ = ["verb","core","KnotMultiplicity"];
verb.core.KnotMultiplicity.prototype = {
inc: function() {
this.mult++;
}
};
verb.core.Analyze = $hx_exports.core.Analyze = function() { };
verb.core.Analyze.__name__ = ["verb","core","Analyze"];
verb.core.Analyze.knotMultiplicities = function(knots) {
var mults = [new verb.core.KnotMultiplicity(knots[0],0)];
var curr = mults[0];
var _g = 0;
while(_g < knots.length) {
var knot = knots[_g];
++_g;
if(Math.abs(knot - curr.knot) > 1e-10) {
curr = new verb.core.KnotMultiplicity(knot,0);
mults.push(curr);
}
curr.inc();
}
return mults;
};
verb.core.Analyze.isRationalSurfaceClosed = function(surface,uDir) {
if(uDir == null) uDir = true;
var cpts;
if(uDir) cpts = surface.controlPoints; else cpts = verb.core.Mat.transpose(surface.controlPoints);
var _g1 = 0;
var _g = cpts[0].length;
while(_g1 < _g) {
var i = _g1++;
var test = verb.core.Vec.dist(cpts[0][i],cpts[cpts.length - 1][i]) < 1e-10;
if(!test) return false;
}
return true;
};
verb.core.Analyze.rationalSurfaceClosestPoint = function(surface,p) {
var uv = verb.core.Analyze.rationalSurfaceClosestParam(surface,p);
return verb.core.Eval.rationalSurfacePoint(surface,uv[0],uv[1]);
};
verb.core.Analyze.rationalSurfaceClosestParam = function(surface,p) {
var maxits = 5;
var i = 0;
var e;
var eps1 = 0.0001;
var eps2 = 0.0005;
var dif;
var minu = surface.knotsU[0];
var maxu = verb.core.ArrayExtensions.last(surface.knotsU);
var minv = surface.knotsV[0];
var maxv = verb.core.ArrayExtensions.last(surface.knotsV);
var closedu = verb.core.Analyze.isRationalSurfaceClosed(surface);
var closedv = verb.core.Analyze.isRationalSurfaceClosed(surface,false);
var cuv;
var tess = verb.core.Tess.rationalSurfaceAdaptive(surface,new verb.core.types.AdaptiveRefinementOptions());
var dmin = Math.POSITIVE_INFINITY;
var _g1 = 0;
var _g = tess.points.length;
while(_g1 < _g) {
var i1 = _g1++;
var x = tess.points[i1];
var d = verb.core.Vec.normSquared(verb.core.Vec.sub(p,x));
if(d < dmin) {
dmin = d;
cuv = tess.uvs[i1];
}
}
var f = function(uv) {
return verb.core.Eval.rationalSurfaceDerivatives(surface,uv[0],uv[1],2);
};
var n = function(uv1,e1,r) {
var Su = e1[1][0];
var Sv = e1[0][1];
var Suu = e1[2][0];
var Svv = e1[0][2];
var Suv = e1[1][1];
var Svu = e1[1][1];
var f1 = verb.core.Vec.dot(Su,r);
var g = verb.core.Vec.dot(Sv,r);
var k = [-f1,-g];
var J00 = verb.core.Vec.dot(Su,Su) + verb.core.Vec.dot(Suu,r);
var J01 = verb.core.Vec.dot(Su,Sv) + verb.core.Vec.dot(Suv,r);
var J10 = verb.core.Vec.dot(Su,Sv) + verb.core.Vec.dot(Svu,r);
var J11 = verb.core.Vec.dot(Sv,Sv) + verb.core.Vec.dot(Svv,r);
var J = [[J00,J01],[J10,J11]];
var d1 = verb.core.Mat.solve(J,k);
return verb.core.Vec.add(d1,uv1);
};
while(i < maxits) {
e = f(cuv);
dif = verb.core.Vec.sub(e[0][0],p);
var c1v = verb.core.Vec.norm(dif);
var c2an = verb.core.Vec.dot(e[1][0],dif);
var c2ad = verb.core.Vec.norm(e[1][0]) * c1v;
var c2bn = verb.core.Vec.dot(e[0][1],dif);
var c2bd = verb.core.Vec.norm(e[0][1]) * c1v;
var c2av = c2an / c2ad;
var c2bv = c2bn / c2bd;
var c1 = c1v < eps1;
var c2a = c2av < eps2;
var c2b = c2bv < eps2;
if(c1 && c2a && c2b) return cuv;
var ct = n(cuv,e,dif);
if(ct[0] < minu) if(closedu) ct = [maxu - (ct[0] - minu),ct[1]]; else ct = [minu + 1e-10,ct[1]]; else if(ct[0] > maxu) if(closedu) ct = [minu + (ct[0] - maxu),ct[1]]; else ct = [maxu - 1e-10,ct[1]];
if(ct[1] < minv) if(closedv) ct = [ct[0],maxv - (ct[1] - minv)]; else ct = [ct[0],minv + 1e-10]; else if(ct[1] > maxv) if(closedv) ct = [ct[0],minv + (ct[0] - maxv)]; else ct = [ct[0],maxv - 1e-10];
var c3v0 = verb.core.Vec.norm(verb.core.Vec.mul(ct[0] - cuv[0],e[1][0]));
var c3v1 = verb.core.Vec.norm(verb.core.Vec.mul(ct[1] - cuv[1],e[0][1]));
if(c3v0 + c3v1 < eps1) return cuv;
cuv = ct;
i++;
}
return cuv;
};
verb.core.Analyze.rationalCurveClosestPoint = function(curve,p) {
return verb.core.Eval.rationalCurvePoint(curve,verb.core.Analyze.rationalCurveClosestParam(curve,p));
};
verb.core.Analyze.rationalCurveClosestParam = function(curve,p) {
var min = Math.POSITIVE_INFINITY;
var u = 0.0;
var pts = verb.core.Tess.rationalCurveRegularSample(curve,curve.controlPoints.length * curve.degree,true);
var _g1 = 0;
var _g = pts.length - 1;
while(_g1 < _g) {
var i = _g1++;
var u0 = pts[i][0];
var u1 = pts[i + 1][0];
var p0 = pts[i].slice(1);
var p1 = pts[i + 1].slice(1);
var proj = verb.core.Trig.segmentClosestPoint(p,p0,p1,u0,u1);
var d = verb.core.Vec.norm(verb.core.Vec.sub(p,proj.pt));
if(d < min) {
min = d;
u = proj.u;
}
}
var maxits = 5;
var i1 = 0;
var e;
var eps1 = 0.0001;
var eps2 = 0.0005;
var dif;
var minu = curve.knots[0];
var maxu = verb.core.ArrayExtensions.last(curve.knots);
var closed = verb.core.Vec.normSquared(verb.core.Vec.sub(curve.controlPoints[0],verb.core.ArrayExtensions.last(curve.controlPoints))) < 1e-10;
var cu = u;
var f = function(u2) {
return verb.core.Eval.rationalCurveDerivatives(curve,u2,2);
};
var n = function(u3,e1,d1) {
var f1 = verb.core.Vec.dot(e1[1],d1);
var s0 = verb.core.Vec.dot(e1[2],d1);
var s1 = verb.core.Vec.dot(e1[1],e1[1]);
var df = s0 + s1;
return u3 - f1 / df;
};
while(i1 < maxits) {
e = f(cu);
dif = verb.core.Vec.sub(e[0],p);
var c1v = verb.core.Vec.norm(dif);
var c2n = verb.core.Vec.dot(e[1],dif);
var c2d = verb.core.Vec.norm(e[1]) * c1v;
var c2v = c2n / c2d;
var c1 = c1v < eps1;
var c2 = Math.abs(c2v) < eps2;
if(c1 && c2) return cu;
var ct = n(cu,e,dif);
if(ct < minu) if(closed) ct = maxu - (ct - minu); else ct = minu; else if(ct > maxu) if(closed) ct = minu + (ct - maxu); else ct = maxu;
var c3v = verb.core.Vec.norm(verb.core.Vec.mul(ct - cu,e[1]));
if(c3v < eps1) return cu;
cu = ct;
i1++;
}
return cu;
};
verb.core.Analyze.rationalCurveParamAtArcLength = function(curve,len,tol,beziers,bezierLengths) {
if(tol == null) tol = 1e-3;
if(len < 1e-10) return curve.knots[0];
var crvs;
if(beziers != null) crvs = beziers; else crvs = verb.core.Modify.decomposeCurveIntoBeziers(curve);
var i = 0;
var cc = crvs[i];
var cl = -1e-10;
var bezier_lengths;
if(bezierLengths != null) bezier_lengths = bezierLengths; else bezier_lengths = [];
while(cl < len && i < crvs.length) {
if(i < bezier_lengths.length) bezier_lengths[i] = bezier_lengths[i]; else bezier_lengths[i] = verb.core.Analyze.rationalBezierCurveArcLength(curve);
cl += bezier_lengths[i];
if(len < cl + 1e-10) return verb.core.Analyze.rationalBezierCurveParamAtArcLength(curve,len,tol,bezier_lengths[i]);
i++;
}
return -1;
};
verb.core.Analyze.rationalBezierCurveParamAtArcLength = function(curve,len,tol,totalLength) {
if(len < 0) return curve.knots[0];
var totalLen;
if(totalLength != null) totalLen = totalLength; else totalLen = verb.core.Analyze.rationalBezierCurveArcLength(curve);
if(len > totalLen) return verb.core.ArrayExtensions.last(curve.knots);
var start_p = curve.knots[0];
var start_l = 0.0;
var end_p = verb.core.ArrayExtensions.last(curve.knots);
var end_l = totalLen;
var mid_p = 0.0;
var mid_l = 0.0;
var tol1;
if(tol != null) tol1 = tol; else tol1 = 2e-06;
while(end_l - start_l > tol1) {
mid_p = (start_p + end_p) / 2;
mid_l = verb.core.Analyze.rationalBezierCurveArcLength(curve,mid_p);
if(mid_l > len) {
end_p = mid_p;
end_l = mid_l;verb.geom
} else {
start_p = mid_p;
start_l = mid_l;
}
}
return (start_p + end_p) / 2;
};
verb.core.Analyze.rationalCurveArcLength = function(curve,u,gaussDegIncrease) {
if(gaussDegIncrease == null) gaussDegIncrease = 16;
if(u == null) u = verb.core.ArrayExtensions.last(curve.knots); else u = u;
var crvs = verb.core.Modify.decomposeCurveIntoBeziers(curve);
var i = 0;
var cc = crvs[0];
var sum = 0.0;
while(i < crvs.length && cc.knots[0] + 1e-10 < u) {
var param = Math.min(verb.core.ArrayExtensions.last(cc.knots),u);
sum += verb.core.Analyze.rationalBezierCurveArcLength(cc,param,gaussDegIncrease);
cc = crvs[++i];
}
return sum;
};
verb.core.Analyze.rationalBezierCurveArcLength = function(curve,u,gaussDegIncrease) {
if(gaussDegIncrease == null) gaussDegIncrease = 16;
var u1;
if(u == null) u1 = verb.core.ArrayExtensions.last(curve.knots); else u1 = u;
var z = (u1 - curve.knots[0]) / 2;
var sum = 0.0;
var gaussDeg = curve.degree + gaussDegIncrease;
var cu;
var tan;
var _g = 0;
while(_g < gaussDeg) {
var i = _g++;
cu = z * verb.core.Analyze.Tvalues[gaussDeg][i] + z + curve.knots[0];
tan = verb.core.Eval.rationalCurveDerivatives(curve,cu,1);
sum += verb.core.Analyze.Cvalues[gaussDeg][i] * verb.core.Vec.norm(tan[1]);
}
return z * sum;
};
verb.core.ArrayExtensions = function() { };
verb.core.ArrayExtensions.__name__ = ["verb","core","ArrayExtensions"];
verb.core.ArrayExtensions.reversed = function(a) {
var ac = a.slice();
ac.reverse();
return ac;
};
verb.core.ArrayExtensions.last = function(a) {
return a[a.length - 1];
};
verb.core.ArrayExtensions.first = function(a) {
return a[0];
};
verb.core.ArrayExtensions.spliceAndInsert = function(a,start,end,ele) {
a.splice(start,end);
a.splice(start,0,ele);
};
verb.core.ArrayExtensions.left = function(arr) {
if(arr.length == 0) return [];
var len = Math.ceil(arr.length / 2);
return arr.slice(0,len);
};
verb.core.ArrayExtensions.right = function(arr) {
if(arr.length == 0) return [];
var len = Math.ceil(arr.length / 2);
return arr.slice(len);
};
verb.core.ArrayExtensions.rightWithPivot = function(arr) {
if(arr.length == 0) return [];
var len = Math.ceil(arr.length / 2);
return arr.slice(len - 1);
};
verb.core.ArrayExtensions.unique = function(arr,comp) {
if(arr.length == 0) return [];
var uniques = [arr.pop()];
while(arr.length > 0) {
var ele = arr.pop();
var isUnique = true;
var _g = 0;
while(_g < uniques.length) {
var unique = uniques[_g];
++_g;
if(comp(ele,unique)) {
isUnique = false;
break;
}
}
if(isUnique) uniques.push(ele);
}
return uniques;
};
verb.core.Binomial = function() { };
verb.core.Binomial.__name__ = ["verb","core","Binomial"];
verb.core.Binomial.get = function(n,k) {
if(k == 0.0) return 1.0;
if(n == 0 || k > n) return 0.0;
if(k > n - k) k = n - k;
if(verb.core.Binomial.memo_exists(n,k)) return verb.core.Binomial.get_memo(n,k);
var r = 1;
var n_o = n;
var _g1 = 1;
var _g = k + 1;
while(_g1 < _g) {
var d = _g1++;
if(verb.core.Binomial.memo_exists(n_o,d)) {
n--;
r = verb.core.Binomial.get_memo(n_o,d);
continue;
}
r *= n--;
r /= d;
verb.core.Binomial.memoize(n_o,d,r);
}
return r;
};