-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern.js
1272 lines (1208 loc) · 34.7 KB
/
pattern.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
// Alexandre Kaspar <akaspar@mit.edu>
"use strict";
const assert = require('./assert.js');
const DSL = require('./dsl.js');
const env = require('./env.js');
const Timer = require('./timer.js');
const noise = require('simplenoise');
const P = {
// pattern instructions
STITCH: 1,
PURL: 2,
TUCK: 3,
MISS: 4,
FRONT_RIGHT1: 5,
FRONT_RIGHT2: 6,
FRONT_LEFT1: 7,
FRONT_LEFT2: 8,
BACK_RIGHT1: 9,
BACK_RIGHT2: 10,
BACK_LEFT1: 11,
BACK_LEFT2: 12,
CROSS_RIGHT_UPPER: 13,
CROSS_RIGHT_LOWER: 14,
CROSS_LEFT_UPPER: 15,
CROSS_LEFT_LOWER: 16,
STACK: 17,
// pattern groups
REVERSE_PATTERNS: {
2: true, 9: true, 10: true, 11: true, 12: true
},
MOVE_PATTERNS: {
5: true, 6: true, 7: true, 8: true, 9: true, 10: true, 11: true, 12: true
},
CROSS_PATTERNS: {
13: true, 14: true, 15: true, 16: true
},
CROSS_ABOVE: {
13: true, 15: true
},
CROSS_BELOW: {
14: true, 16: true
},
// pattern directions
DIRECTION_OF: {
// move directions
5: 1, 6: 1, 7: -1, 8: -1, 9: 1, 10: 1, 11: -1, 12: -1,
// cross directions
13: 1, 14: 1, 15: -1, 16: -1
},
// pattern steps
STEPS_OF: {
// move steps
5: 1, 6: 2, 7: 1, 8: 2, 9: 1, 10: 2, 11: 1, 12: 2,
// cable steps (assumed 1)
13: 1, 14: 1, 15: 1, 16: 1
},
// cross complements
CROSS_COMPLEMENT_OF: {
13: 16, 14: 15, 15: 14, 16: 13
},
// move map
MOVE: {
'front': {
'left': [ 7, 8 ],
'right': [ 5, 6 ]
},
'back': {
'left': [ 11, 12 ],
'right': [ 9, 10 ]
},
},
moveFor(...args){
let side = null;
let dir = null;
let steps = 0;
for(let i = 0; i < args.length; ++i){
if(typeof args[i] == 'number'){
assert(args[i] && Math.abs(args[i]) <= 2, 'Move can only use steps 1 or 2');
if(args[i] < 0){
assert(!dir, 'Step sign conflicts with direction argument');
dir = 'left';
steps = -args[i];
} else {
steps = args[i];
}
} else {
let arg = args[i].toLowerCase();
switch(arg){
case 'front':
case 'back':
assert(!side, 'Side argument conflict');
side = arg;
break;
case 'left':
case 'right':
assert(!dir, 'Direction argument conflict');
dir = arg;
break;
default:
throw "Invalid argument " + arg;
}
}
}
// default arguments
if(!side)
side = 'front';
if(!dir)
dir = 'right';
if(!steps)
steps = 1;
assert(side in P.MOVE, 'Invalid side');
assert(dir in P.MOVE[side], 'Invalid direction');
assert(steps == 1 || steps == 2, 'Invalid steps');
return P.MOVE[side][dir][steps - 1];
},
// cross map
CROSS: {
'right': {
// upper
1: 13,
'+': 13,
'upper': 13,
// lower
0: 14,
'-': 14,
'lower': 14
},
'left': {
// upper
1: 15,
'+': 15,
'upper': 15,
// lower
0: 16,
'-': 16,
'lower': 16
}
},
crossFor: function(...args){
let dir = null;
let order = null;
for(let i = 0; i < args.length; ++i){
let arg = args[i].toString().toLowerCase();
if(arg in P.CROSS){
assert(!dir, 'Direction argument conflict');
dir = arg;
} else if(arg in P.CROSS.right){
assert(!order, 'Order argument conflict');
order = arg;
} else {
throw 'Invalid argument ' + arg;
}
}
if(!dir)
dir = 'right';
if(!order)
order = 'upper';
assert(dir in P.CROSS, 'Invalid direction');
assert(order in P.CROSS[dir], 'Invalid order');
return P.CROSS[dir][order];
},
// pattern modifiers
SINGULAR: 0,
SCALABLE: 1,
TILEABLE: 2,
// pass storage
PASS_STORAGE: 'pattern'
};
/**
* Pattern constructor
*/
function Pattern(context, stitches, indices, nodes, id2idx, passID){
this.context = context;
this.stitches = stitches; // reference
this.indices = indices || stitches.map((s, i) => i);
this.nodes = nodes || stitches.map((s, i) => wrap(s, context ? context.node : null, i));
if(id2idx)
this.id2idx = id2idx;
else {
// build mapping
this.id2idx = {};
for(let i = 0; i < stitches.length; ++i)
this.id2idx[stitches[i].id] = i;
}
this.passID = passID !== undefined ? passID : -1;
}
/**
* Run a function with this pattern as input, normalized to all stitches
* with a specific pass id.
*
* @param passID the pass identifier to use for that function's pattern
* @param func a function (pat) => {}
*/
Pattern.prototype.forPass = function(passID, func){
const pat = this.all();
pat.passID = passID;
func(pat);
};
/**
* Return the pattern node (wrapper over stitch) at a given index
*
* @param idx the index to query
* @return the pattern wrapped stitch
*/
Pattern.prototype.node = function(idx){
return this.nodes[idx || 0];
};
/**
* Return a new pattern wrapping a single stitch
*
* @param sid the stitch id
* @return the singleton pattern
*/
Pattern.prototype.stitchPattern = function(sid){
const idx = this.id2idx[sid];
return new Pattern(this.context, [this.stitches[idx]], [0], [this.nodes[idx]], null, this.passID);
};
/**
* Create a sub-pattern selection using given indices
*
* @param indices the new indices
*/
Pattern.prototype.withIndices = function(indices){
return new Pattern(this.context, this.stitches, indices, this.nodes, this.id2idx, this.passID);
};
/**
* Converts a stitch primitive into a node
* that can safely be handled in the pattern code.
* This adds easy access to some properties.
*
* General properties include
*
* - id
* - shapes
* - anchors
* - flat
* - siblings (# of course connections)
* - links (# of wale connections)
*
* whereas context-dependent ones include
*
* - names
* - courseId
* - courseEnd
* - waleId
* - waleEnd
* - gauge (1 for full, 2 for half gauge)
*
* @param stitch the new stitch
* @param node the node context (or null if global)
*/
function wrap(stitch, node, index){
let ctxData = {
names: stitch.meta(node, 'names')
};
for(let what of ['courseId', 'courseEnd', 'waleId', 'waleEnd', 'gauge'])
ctxData[what] = stitch.meta(node, what)[0] || 0;
return Object.assign(ctxData, {
// general
id: stitch.id,
index,
shapes: stitch.meta('shape'),
anchors: [],
flat: stitch.meta('flat').length > 0,
siblings: stitch.courses.size,
links: stitch.wales.size
});
}
/**
* Select all stitches but modifies the associated nodes
*
* @param mapFunc the mapping
* @return the new pattern selection with remapped nodes
*/
Pattern.prototype.map = function(mapFunc){
return new Pattern(this.context, this.stitches, this.indices, this.nodes.map(mapFunc), this.id2idx, this.passID);
};
Pattern.prototype.swapXY = function(revX, revY){
if(revX){
if(revY){
return this.map(n => Object.assign({}, n, {
courseId: n.waleEnd - n.waleId,
courseEnd: n.waleEnd,
waleId: n.courseEnd - n.courseId,
waleEnd: n.courseEnd
}));
} else {
return this.map(n => Object.assign({}, n, {
courseId: n.waleId,
courseEnd: n.waleEnd,
waleId: n.courseEnd - n.courseId,
waleEnd: n.courseEnd
}));
}
} else {
if(revY){
return this.map(n => Object.assign({}, n, {
courseId: n.waleEnd - n.waleId,
courseEnd: n.waleEnd,
waleId: n.courseId,
waleEnd: n.courseEnd
}));
} else {
return this.map(n => Object.assign({}, n, {
courseId: n.waleId,
courseEnd: n.waleEnd,
waleId: n.courseId,
waleEnd: n.courseEnd
}));
}
}
};
Pattern.prototype.rot90 = function(num){
switch(num){
case undefined:
case 1:
return this.swapXY(false, true);
case 2:
return this.map(n => Object.assign({}, n, {
courseId: n.courseEnd - n.courseId,
waleId: n.waleEnd - n.waleId
}));
case 3:
return this.swapXY(true, false);
default:
return this;
}
};
/**
* Get a list of reachable stitches from a list of shapes.
* The list must include a regular shape with sequential courses.
*
* @param shapes the list of shapes
* @return a list of stitches reachable from one of the regular shapes
*/
function stitchesFromShapes(shapes){
let shape0 = Array.isArray(shapes) ? shapes[0] : shapes;
assert(shape0, 'No shape found for patterning');
let crs0 = shape0.getCourse(0) || shape0.getCourse('base');
assert(crs0, 'No first course for patterning');
let stitch0 = crs0.first();
assert(stitch0, 'No first stitch for patterning');
return stitch0.all();
}
/**
* Applies patterning on top of a set of shapes
* This applies a sequence of patterns programs:
* - an initial global pattern
* - individual per-shape patterns
* - a final global pattern
*
* @param shapes the list of shapes
*/
Pattern.transform = function(shapes){
if(shapes.length == 0)
return;
let t = Timer.create();
let stitches = stitchesFromShapes(shapes);
// 1. Apply global before
if(env.global.beforePattern)
Pattern.eval(env.global.beforePattern, new Pattern(null, stitches));
// 2. Apply per shape
for(let shape of shapes){
if(shape.node.pattern){
const shapeStitches = stitches.filter(s => s.meta('shape').includes(shape.node.id));
Pattern.eval(shape.node.pattern.toString(), new Pattern(shape, shapeStitches));
}
}
// 3. Apply global after
if(env.global.afterPattern)
Pattern.eval(env.global.afterPattern, new Pattern(null, stitches));
t.measure('tran');
console.log('Pattern', t.toString());
};
/**
* Clears the pattern information of a set of shapes
*
* @param shapes the list of shapes
*/
Pattern.clearShapes = function(shapes){
let stitches = stitchesFromShapes(shapes);
Pattern.clearStitches(stitches);
};
/**
* Clears the pattern information of a set of stitches
*
* @param stitches the stitches to clear
*/
Pattern.clearStitches = function(stitches){
for(let s of stitches)
s.pattern = P.STITCH; // reset all to default
};
/**
* Evaluate a pattern program on top of a pattern selection
*
* @param code the code to evaluate
* @param pat the pattern selection
* @param args an optional object containing pattern code arguments to be passed
* @see DSL.eval
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
* @return the output of the pattern program if any (usually none)
*/
Pattern.eval = function(code, pat, args){
if(args){
console.assert(typeof args == 'object', 'args must be an object whose keys are argument names');
return DSL.eval(code, Object.assign({ pat, math: Math, noise }, args), DSL.range, env.verbose);
} else
return DSL.eval(code, { pat, math: Math, noise }, DSL.range, env.verbose);
};
/**
* Attempt to run the the given pattern code on some arbitrary input
*
* @param code the patterner code
*/
Pattern.compile = function(code){
Pattern.eval(code, new Pattern(null, []));
};
// ----------------------------------------------------------------------------
// --- Stitch operations ------------------------------------------------------
// ----------------------------------------------------------------------------
Pattern.prototype.proc = function(proc){
for(let idx of this.indices)
proc(this.stitches[idx]);
return this;
};
Pattern.prototype.set = function(type) {
if(this.passID != -1){
for(let idx of this.indices){
const stitch = this.stitches[idx];
stitch.pattern = type;
stitch.meta(this.passID, P.PASS_STORAGE, type);
}
} else {
for(let idx of this.indices)
this.stitches[idx].pattern = type;
}
return this;
};
Pattern.prototype.mod = function(type) {
for(let idx of this.indices)
this.stitches[idx].modifier = type;
return this;
};
Pattern.prototype.stitch =
Pattern.prototype.knit = function(reversed){
return this.set(reversed ? P.PURL : P.STITCH);
};
Pattern.prototype.purl = function(){
return this.stitch(true);
};
Pattern.prototype.miss = function(){
return this.set(P.MISS);
};
Pattern.prototype.tuck = function(){
return this.set(P.TUCK);
};
Pattern.prototype.move = function(...args){
return this.set(P.moveFor(...args));
};
Pattern.prototype.left = function(...args){
return this.move('left', ...args);
};
Pattern.prototype.right = function(...args){
return this.move('right', ...args);
};
Pattern.prototype.cable =
Pattern.prototype.cross = function(...args){
return this.set(P.crossFor(...args));
};
Pattern.prototype.scalable = function(){
return this.mod(P.SCALABLE);
};
Pattern.prototype.tileable = function(){
return this.mod(P.TILEABLE);
};
Pattern.prototype.singular = function(){
return this.mod(P.SINGULAR);
};
Pattern.prototype.color =
Pattern.prototype.mark = function(mark, mode){
if(!mode)
mode = 'set';
switch(mode){
case 'add':
this.proc(function(n){
n.mark += mark;
});
break;
case 'remove':
this.proc(function(n){
n.mark -= mark;
});
break;
case 'multiply':
this.proc(function(n){
n.mark *= mark;
});
break;
case 'or':
this.proc(function(n){
n.mark |= mark;
});
break;
case 'and':
this.proc(function(n){
n.mark &= mark;
});
break;
case 'xor':
this.proc(function(n){
n.mark ^= mark;
});
break;
case 'set':
/* falls through */
default:
this.proc(function(n){
n.mark = mark;
});
break;
}
return this;
};
// ----------------------------------------------------------------------------
// --- Stitch queries ---------------------------------------------------------
// ----------------------------------------------------------------------------
/**
* Select all stitches of a pattern source
*
* @return the new stitch selection
*/
Pattern.prototype.all = function(){
return this.withIndices(this.stitches.map((s, i) => i));
};
/**
* Filter the current pattern selection
*
* @param predicate the node predicate
* @return the filtered stitch selection
*/
Pattern.prototype.filter = function(predicate){
return this.withIndices(this.indices.filter(i => predicate(this.nodes[i], this.stitches[i].type)));
};
/**
* Filter the all selection directly.
* Slightly faster than applying consecutively this.all().filter(predicate)
*
* @param predicate
* @return the filtered stitch selection
*/
Pattern.prototype.filterAll = function(predicate){
let indices = [];
for(let i = 0; i < this.stitches.length; ++i){
if(predicate(this.nodes[i], this.stitches[i].type))
indices.push(i);
}
return this.withIndices(indices);
};
//
// --- Indexed queries --------------------------------------------------------
//
Pattern.prototype.side = function(side){
return this.select([0, -1], [0, -1], side);
};
/**
* Interpret range value
*
* @param value the range component (either start or end)
* @param valueEnd the local context's end value
* @return the effective location number
*/
function interpretRange(value, valueOffset, valueEnd){
if(value < 0)
return value + valueEnd + 1;
if(value > 0 && value < 1)
return valueOffset + value * valueEnd;
return valueOffset + value;
}
function sidedRange(value, valueEnd, side){
switch(side){
case 'front':
case 0:
return interpretRange(value, 0, Math.floor(valueEnd / 2));
case 'back':
case 1:
return interpretRange(value, Math.ceil(valueEnd / 2), valueEnd);
default:
return interpretRange(value, 0, valueEnd);
}
}
/**
* Select a subset of stitches based on indices
*
* @param rows the course range
* @param cols the wale range
* @return the new stitch selection
*/
Pattern.prototype.select = function(rows, cols, side){
// normalize $rows range
if(!Array.isArray(rows))
rows = [rows, rows, 1];
// normalize $cols range
if(!Array.isArray(cols))
cols = [cols, cols, 1];
// interval step
const rowStep = rows[2] || 1;
const colStep = cols[2] || 1;
// side information
if(!this.context || !this.context.sided)
side = 'both';
// generic filter
return this.filter(n => {
// local normalization of ends that are fractional
let fromRow = interpretRange(rows[0], 0, n.courseEnd);
let toRow = interpretRange(rows[1], 0, n.courseEnd);
let fromCol = sidedRange(cols[0], n.waleEnd, side);
let toCol = sidedRange(cols[1], n.waleEnd, side);
return n.courseId >= fromRow && n.courseId <= toRow
&& n.waleId >= fromCol && n.waleId <= toCol
&& (rowStep == 1 || ((n.courseId - fromRow) % rowStep == 0))
&& (colStep == 1 || ((n.waleId - fromCol) % colStep == 0));
});
};
Pattern.prototype.courses = function(range){
// normalize range
if(!Array.isArray(range))
range = [range, range, 1];
// interval step
const step = range[2] || 1;
// generic filter
return this.filter(n => {
// local normalization of ends that are fractional
let from = interpretRange(range[0], 0, n.courseEnd);
let to = interpretRange(range[1], 0, n.courseEnd);
return n.courseId >= from && n.courseId <= to
&& (step == 1 || ((n.courseId - from) % step == 0));
});
};
Pattern.prototype.wales = function(range, side){
// normalize range
if(!Array.isArray(range))
range = [range, range, 1];
// interval step
const step = range[2] || 1;
// side information
if(!this.context || !this.context.sided)
side = 'both';
if(side == 'both' || side === undefined){
// generic filter
return this.filter(n => {
// local normalization of ends that are fractional
let from = interpretRange(range[0], 0, n.waleEnd);
let to = interpretRange(range[1], 0, n.waleEnd);
return n.waleId >= from && n.waleId <= to
&& (step == 1 || ((n.waleId - from) % step == 0));
});
} else {
// use extra wale filtering with full select query
return this.select([0, -1], range, side);
}
};
//
// --- Neighborhood queries -------------------------------------------------------
//
/**
* Select the neighbors of the current selection within some range
*
* @param range the range of rings to select
* @return the new neighborhood
*/
Pattern.prototype.neighbors = function(range /*, diags */){
if(!range)
range = [1, 1, 1]; // 1-ring neighborhood
else if(!Array.isArray(range))
range = [range, range, 1]; // N-ring neighborhood
assert(range[2] == 1, 'Neighbors can only use step=1');
let N = this.stitches.length; // workspace size
let order = new Uint8Array(N); // initialized to 0
let current = this.indices;
// first pass: mark current selection to 1
for(let idx of current){
order[idx] = 1;
}
// next passes: mark neighbors iteratively
for(let r = 1; r <= range[1] && current.length; ++r){
current = current.reduce((accu, idx) => {
let s = this.stitches[idx];
for(let n of s.neighbors()){
let nidx = this.id2idx[n.id]; // get neighbor's index
if(order[nidx] == 0){
order[nidx] = r + 1;
accu.push(nidx); // no duplicates because of order information
}
}
return accu;
}, []);
}
// create new selection that matches the range information
current = [];
for(let i = 0; i < N; ++i){
let r = order[i] - 1;
if(r >= range[0] && r <= range[1])
current.push(i);
}
return this.withIndices(current);
};
/**
* Filter current stitches to those that
* have neighbors outside the selection.
*
* When used on a shape, this also includes
* nodes that have no neighbors outside the selection
* but which have minimal or extremal courseId.
*/
Pattern.prototype.boundaries = function(){
// XXX select linear array vs hashmap depending on size of current selection
let nodeMap = {};
for(let idx of this.indices){
nodeMap[idx] = true;
}
if(this.context){
return this.filter((node, stitch) => {
if(node.names.length)
return true;
return stitch.neighbors().every(s => this.id2idx[s.id] in nodeMap);
});
} else {
return this.filter((node, stitch) => {
return stitch.neighbors().every(s => this.id2idx[s.id] in nodeMap);
});
}
};
//
// --- Set operations ---------------------------------------------------------
//
/**
* Select the intersection with another selection
*
* @param sel the other selection
* @return their intersection
*/
Pattern.prototype.and =
Pattern.prototype.inter = function(sel){
let flags = new Uint8Array(this.stitches.length);
for(let idx of this.indices)
flags[idx] = 1;
return this.withIndices(sel.indices.reduce((accu, idx) => {
if(flags[idx])
accu.push(idx);
return accu;
}, []));
};
/**
* Select the union with another selection
*
* @param sel the other selection
* @return their union
*/
Pattern.prototype.or =
Pattern.prototype.union = function(sel){
let flags = new Uint8Array(this.stitches.length);
for(let idx of this.indices)
flags[idx] = 1;
return this.withIndices( sel.indices.reduce((accu, idx) => {
if(!flags[idx])
accu.push(idx);
return accu;
}, this.indices.slice()));
};
/**
* Subtract a selection from this one
*
* @param sel the selection to subtract
* @return the difference selection
*/
Pattern.prototype.minus = function(sel){
let flags = new Uint8Array(this.stitches.length);
for(let idx of sel.indices)
flags[idx] = 1;
return this.withIndices(this.indices.filter(idx => {
return !flags[idx];
}));
};
/**
* Select the inverse of the current selection
*/
Pattern.prototype.inverse = function(){
let flags = new Uint8Array(this.stitches.length);
for(let idx of this.indices)
flags[idx] = 1;
let newIndices = [];
for(let i = 0, n = this.stitches.length; i < n; ++i){
if(!flags[i])
newIndices.push(i);
}
return this.withIndices(newIndices);
};
//
// --- Named queries ----------------------------------------------------------
//
/**
* Return the node identifier for a given name
*
* @param name either a string or a numeric identifier
* @return the node id or -1
*/
function getNodeID(name){
// find the matching node
let node = null;
if(typeof name == 'string')
node = env.nodes.find(n => n.name == name);
else {
assert(typeof name == 'number', 'Invalid shape argument: ' + name);
node = env.node.find(n => n.id == name);
}
return node ? node.id : -1;
}
/**
* Select all stitches that come from a shape
*
* @param name the name of the shape node (or its id)
* @return its set of stitches
*/
Pattern.prototype.shape = function(name){
let nodeID = getNodeID(name);
if(nodeID == -1)
return this.withIndices([]);
else
return this.filter(n => n.shapes.includes(nodeID));
};
/**
* Select all stitches that have some name annotation
*/
Pattern.prototype.named = function(){
return this.filter(n => n.names.length);
};
/**
* Select all stitches that match an interface name.
*
* @param name the interface name
*/
Pattern.prototype.itf = function(name){
return this.filter(n => n.names.includes(name));
};
/**
* Select all stitches associated with a named anchor
*
* @param name the anchor name
*/
Pattern.prototype.anchor = function(name){
return this.filter(n => n.anchors.includes(name));
};
//
// --- Masking queries --------------------------------------------------------
//
/**
* Filter stitches using a mask predicate on the grid domain spanned
* by the current selection.
* The mask predicate receives normalized coordinates in [0;1]^2,
* together with the real region's height and width, and the target node, i.e.
* the parameters (y, x, h, w, s)
* and should return whether to keep or not the stitch.
*
* The adapting version uses a local width w, varying with y,
* whereas the non-adapting one uses a global w value.
*
* @param predicate (y,x,h,w,s)=>(true|false)
* @param adapt whether to make the grid stretch locally (i.e. w varies with y)
* @return the masked selection
*/
Pattern.prototype.mask = function(predicate, adapt){
if(adapt){
// compute extents
let minCrs = Infinity;
let maxCrs = -Infinity;
let minWal = {}; // adapt
let maxWal = {}; // adapt
for(let idx of this.indices){
let node = this.nodes[idx];
minCrs = Math.min(minCrs, node.courseId);
maxCrs = Math.max(maxCrs, node.courseId);
if(node.courseId in minWal){
minWal[node.courseId] = Math.min(minWal[node.courseId], node.waleId);
maxWal[node.courseId] = Math.max(maxWal[node.courseId], node.waleId);
} else {
minWal[node.courseId] = node.waleId;
maxWal[node.courseId] = node.waleId;
}
}
// create masked pattern using predicate on location
// within extents in normalized coordinates (y,x) in [0;1]^2
return this.withIndices(this.indices.filter(idx => {
let node = this.nodes[idx];
let w = minWal[node.courseId];
let W = maxWal[node.courseId];
return predicate(
(node.courseId - minCrs) / (maxCrs - minCrs + 1),
(node.waleId - w) / (W - w + 1),
maxCrs - minCrs + 1,
W - w + 1,
node
);
}));
} else {
// compute extents
let minCrs = Infinity;
let maxCrs = -Infinity;
let minWal = Infinity;
let maxWal = -Infinity;
for(let idx of this.indices){
let node = this.nodes[idx];
minCrs = Math.min(minCrs, node.courseId);
maxCrs = Math.max(maxCrs, node.courseId);
minWal = Math.min(minWal, node.waleId);
maxWal = Math.max(maxWal, node.waleId);
}
// create masked pattern using predicate on location
// within extents in normalized coordinates (y,x) in [0;1]^2
return this.withIndices(this.indices.filter(idx => {
let node = this.nodes[idx];
return predicate(
(node.courseId - minCrs) / (maxCrs - minCrs + 1),
(node.waleId - minWal) / (maxWal - minWal + 1),
maxCrs - minCrs + 1,
maxWal - minWal + 1,
node
);
}));
}
};
Pattern.prototype.maskdo = function(map, adapt){
return this.mask((y, x, h, w, s) => {
return map(y, x, h, w, this.stitchPattern(s.id));
}, adapt);
};
/**
* Creates a grid from the bits of a number
*
* @param num the number, whose bits are used from most significant to least significant
* @param width the expect grid width (for padding purposes on the left)
* @return the grid
*/
function bitGrid(num, width){
let grid = num.toString(2).split('').map(c => parseInt(c));
if(!width)
return grid;
// pad on the left with 0 until width matches
while(grid.length % width)
grid.unshift(0);
return grid;
}