This repository has been archived by the owner on Jul 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
knitout-3D-visualizer.js
1055 lines (923 loc) · 36.1 KB
/
knitout-3D-visualizer.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
#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
"use strict";
/*knitout-3D-visualizer.js
* Takes a knitout file and turns it into an .txt file. The result should
* be a series of 3D coordinates with each component delineated by a space and
* each new coordinate separated by a new line.
* Carriers will be marked in the resultant file by a "c" preceding the coordinates
* of that carrier.
*/
//parse command line
if (process.argv.length != 4) {
console.error("Usage:\nknitout-3D-visualizer.js <in.k> <out.txt>");
process.exitCode = 1;
return;
}
//TODO ah currently height checking doesnt check the carriers crossed at carrier
//depth whoops
//globals
//file setup
var knitoutFile = process.argv[2];
var textFile = process.argv[3];
const fs = require('fs');
var stream = fs.createWriteStream(textFile);
//data store stuff
var frontActiveRow = []; //info on the active stitch(es) on each needle on the front bed
var backActiveRow = []; //info on the active stitch(es) on each needle on the back bed
var lastNeedle = []; //array of the last used needle for each carrier
var anyLast; //the last needle used. Can be of any carrier.
var maxHeight = [];
//array of the max "height" for each needle's associated stitch and carrier position
var allCarriers = []; //all carriers declared by the header of the .k file
var maxCarriers = 0; //changed during parsing. equal to the length of allCarriers
//parameters (These can *probably* be changed without wrecking everything)
var boxWidth = 1;
var boxHeight = 1;
var boxDepth = 0.1;
var boxSpacing = boxHeight/2;
var epsilon = 0.1; //amount stitches are moved "up" to avoid intersecting
//layer depths
const FRONT_BED = 1;
const BACK_BED = -1;
const CARRIERS = -0.4;
const FRONT_SLIDERS = 0.5;
const BACK_SLIDERS = -0.5;
const CROSSING = boxWidth/3;
const PADDING = boxWidth/10;
let CARRIER_SPACING = (FRONT_SLIDERS-CARRIERS); //to be dividied by number of total carriers
//different pass types:
const TYPE_KNIT_TUCK = 'knit-tuck';
const TYPE_A_MISS = 'a-miss';
const TYPE_SPLIT = 'split';
const TYPE_SPLIT_VIA_SLIDERS = 'split-via-sliders';
const TYPE_XFER = 'xfer';
const TYPE_XFER_TO_SLIDERS = 'xfer-to-sliders';
const TYPE_XFER_FROM_SLIDERS = 'xfer-from-sliders';
//different pass yarn hook actions:
const HOOK_IN = 'hook-in'; //bring in yarn using hook before pass starts (GRIPPER_IN must also be set)
const HOOK_RELEASE = 'hook-release'; //release yarn from hook after pass ends
const HOOK_OUT = 'hook-out'; //bring yarn out using hook after pass ends (GRIPPER_OUT must also be set)
//different pass yarn gripper actions:
const GRIPPER_IN = 'gripper-in'; //bring yarn in from gripper (inhook will also set HOOK_IN)
const GRIPPER_OUT = 'gripper-out'; //bring yarn out to gripper (outhook will also set HOOK_OUT)
//pass directions:
const DIRECTION_LEFT = '-';
const DIRECTION_RIGHT = '+';
const DIRECTION_NONE = '';
//special op, turns into a MISS if slot is unoccupied, or merges with knit/tuck/etc.
const OP_SOFT_MISS = {color:16};
const OP_MISS_FRONT = {color:216 /*bed:'f'*/}; //116 == front miss (with links process), 216 == front miss (independent carrier movement)
const OP_MISS_BACK = {color:217 /*bed:'b'*/}; //117 == back miss (with links process), 217 == back miss (independent carrier movement)
//NOTE: this code sometimes uses 216/217 without independent carrier movement, at that seems to be okay(?!?)
const OP_TUCK_FRONT = {color:11, isFront:true };
const OP_TUCK_BACK = {color:12, isBack:true };
const OP_KNIT_FRONT = {color:51, isFront:true};
const OP_KNIT_BACK = {color:52, isBack:true};
//combo ops:
const OP_XFER_TO_BACK = {color:20};
const OP_XFER_TO_FRONT = {color:30};
const OP_SPLIT_TO_BACK = {color:101};
const OP_SPLIT_TO_FRONT = {color:102};
//helper functions
//return a combined operation that does 'a' then 'b' (moving right) or null
//if such a thing doesn't exist
function merge_ops(a,b,quarterPitch){
if(a===OP_SOFT_MISS)
return b;
else if(b===OP_SOFT_MISS)
return a;
return null;
}
//BedNeedle helps store needles:
function BedNeedle(bed, needle) {
if (arguments.length == 1 && typeof(arguments[0]) === "string") {
let str = arguments[0];
let m = str.match(/^([fb]s?)(-?\d+)$/);
if (!m) {
throw "ERROR: invalid needle specification '" + str + "'.";
}
this.bed = m[1];
this.needle = parseInt(m[2]);
} else if (arguments.length == 2 && typeof(arguments[0]) === "string"
&& typeof(arguments[1]) === "number") {
this.bed = arguments[0];
this.needle = arguments[1];
} else {
throw "Don't know how to construct a BedNeedle from the given arguments";
}
}
BedNeedle.prototype.toString = function() {
return this.bed + this.needle;
};
BedNeedle.prototype.isFront = function(){
if (this.bed === 'f' || this.bed === 'fs') return true;
else if (this.bed === 'b' || this.bed === 'bs') return false;
else throw "Invalid bed in BedNeedle.";
};
BedNeedle.prototype.isBack = function(){
if (this.bed === 'f' || this.bed === 'fs') return false;
else if (this.bed === 'b' || this.bed === 'bs') return true;
else throw "Invalid bed in BedNeedle.";
};
BedNeedle.prototype.isHook = function(){
if (this.bed === 'f' || this.bed === 'b') return true;
else if (this.bed === 'fs' || this.bed === 'bs') return false;
else throw "Invalid bed in BedNeedle.";
};
BedNeedle.prototype.isSlider = function(){
if (this.bed === 'fs' || this.bed === 'bs') return true;
else if (this.bed === 'f' || this.bed === 'b') return false;
else throw "Invalid bed in BedNeedle.";
};
//Carrier objects store information about each carrier:
function Carrier(name) {
this.name = name;
this.last = null; //last stitch -- {needle:, direction:} -- or null if not yet brought in
this.in = null; //the "in" operation that added this to the active set. (format: {op:"in", cs:["", "", ...]})
}
//yarn is a 2D array that stores points for the entire knitted yarn thing
//yarn is organized by the number value assigned to each carrier.
//withing each yarn[carrier number] is a another array where the indexes represent
//"rows".
//
//the idea of "rows" in this code is primarily for organization purposes. A new
//row is formed when:
// a) the direction of knitting changes
// b) a knit is made somewhere that already has a stitch
let yarn = [];
//each yarn[carrier number][row number] holds a "yarnPass" object.
//the field "bloops" is an array of loop objects on the back bed
//the field "floops" is an array of loop objects on the front bed.
// (each loop is stored in the array index = to its needle number)
//the field direction is the direction that the loops were all knit in ('+' or '-')
function yarnPass(floops, bloops, direction){
this.bloops = bloops;
this.floops = floops;
this.direction = direction;
}
//used in frontActiveRow and backActiveRow.
//Its just an object that stores the "row" and carrier of the currently active
//loop on a certain needle.
function loopSpec(row, carrier){
this.row = row;
this.carrier = carrier;
}
//stored in the array that is accessed by yarn[carrier #][row #].floops
//or yarn[carrier #][row #].bloops
function loop(pts, carrier){
this.ctrlPts = pts; //array of coordinates for the stitch
this.carrier = carrier; //carrier the stitch was knit with
}
//just a helper to put points in the proper format for the output file
function format(x, y, z){
return x+" "+y+" "+z+"\n";
}
//converts a carrier string to a set number
function getCarrierNum(cs){
for(let i = 0; i<maxCarriers;i++){
if(allCarriers[i] == cs)
return i;
}
console.assert(false, "Tried to access a non-active carrier.");
}
//This function is used to obtain a index for acessing "horizons"
//(the max height of part of the yarn)
//horizons shall be stored only for 2 points of each needle now:
//the main stitch area, and the carrier area associated with that needle
//planes should be 'f' or 'b' or 'c'
//needle should be a number
//direction should be '+' or '-' if 'c' for plane
// direction does NOT refer to the direction of knitting, but the side of
// the stitch the desired carrier position is on. '-' means left and '+' right
//carrier should be some string
function pointName(plane, direction, needle, carrier){
console.assert(plane==='f'||plane==='b'||plane==='c',
"'plane' field must be 'f', 'b', or 'c'.");
console.assert(typeof(needle) == 'number', "'needle' must be a number.");
if(plane === 'c')
console.assert(direction==='+'||direction==='-',
"'direction' field can only be '-' or '+'");
console.assert(typeof(carrier) == 'string',
"'carrier' field must be a string.");
let index = 0;
let c = maxCarriers;
//each needle occupies c+2 indices.
//front and back versions of each needle
//and then c indices, one for each carrier at the carrier depth.
let n = needle;
if(plane==='c' && direction==='-')
n -=1 ;
index+=n*(c+2);
if(plane==='b')
index+1;
else if(plane==='c')
index+=+2;
index+=getCarrierNum(carrier);
return index;
}
//gets the necessary height to prevent intersections at the carrier level
function getMaxHeight(minHeight, startNeedle, endNeedle, carrier){
let raised = false;
let max = minHeight;
for(let i = startNeedle; i<endNeedle; i++){
let index = pointName('c', '+', i, carrier);
if(maxHeight[index]>=max){
raised = true;
max = maxHeight[index];
}
}
if(raised) max +=epsilon;
return max;
}
//updates max height for a range
function setMaxHeight(newHeight, startNeedle, endNeedle, carrier){
for(let i = startNeedle; i<endNeedle; i++){
let index = pointName('c', '+', i, carrier);
maxHeight[index] = newHeight;
}
}
//gets yarn "height" of neighboring loops
function neighborHeight(bed, needle, carrier){
let max = 0;
let cNum = getCarrierNum(carrier);
let left = needle-1;
let right = needle+1;
let activeRow = (bed==='f' ? frontActiveRow : backActiveRow);
if(yarn[cNum] === undefined)
return max;
while(left>=0||right<activeRow.length){
if(left>=0){
if(activeRow[left]!==undefined){
let row = activeRow[left].row;
if(yarn[cNum][row]!==undefined){
if((bed==='f' && yarn[cNum][row].floops[left])
||(bed==='b' && yarn[cNum][row].bloops[left])){
max = Math.max(max, minHeight(activeRow[left], bed, left));
left = -1;
}
}
}
left--;
}
if(right<activeRow.length){
if(activeRow[right]!==undefined){
let row = activeRow[right].row;
if(yarn[cNum][row]!==undefined){
if((bed==='f'&& yarn[cNum][row].floops[right])
||(bed==='b'&&yarn[cNum][row].bloops[right])){
max = Math.max(max, minHeight(activeRow[right], bed, right));
right = activeRow.length;
}
}
}
right++;
}
}
return max;
}
//this gets the minimum height needed for starting a stitch at a certain needle
//this is obtained by subtracting from the top of the stitch because in some
//edge cases, the head of the stitch is raised to avoide intersections, and this
//way ensures that obtaining the height for knitting the next stitch is correct
function minHeight(needleSpec, bed, needle){
let max = -Infinity;
let carrier = needleSpec.carrier;
let cNum = getCarrierNum(carrier);
let loops = (bed==='f' ? yarn[cNum][needleSpec.row].floops[needle]
: yarn[cNum][needleSpec.row].bloops[needle]);
for(let i = 0; i<loops.length; i++){
max = Math.max(max, loops[i].ctrlPts[6][1]);
}
return max - (boxHeight/6) - boxSpacing;
}
//Note that when going from back bed to the appropriate carrier depth, the yarn
//passes over any carrier slots < the desired.
//When going from front bed to the appropriate carrier depth, the yarn passes
//carrier slots > the number of the desired carrier.
function carrierCheck(height, bed, needle, cNum){
let raised = false;
let min = height;
if(bed === 'f'){
for(let i = cNum+1; i<maxCarriers; i++){
let index = pointName('c', '+', needle, allCarriers[i]);
if(maxHeight[index]>=min){
raised = true;
min = maxHeight[index];
}
}
}else{
for(let i = 0; i<cNum; i++){
let index = pointName('c', '+', needle, allCarriers[i]);
if(maxHeight[index]>=min){
raised = true;
min = maxHeight[index];
}
}
}
if(raised) min+=epsilon;
return min;
}
//updates last stitch to avoid intersections at the carrier level
function updateLast(lastNeedle, direction, bed, needle, carrier, height){
let cNum = getCarrierNum(carrier);
let padding = (direction==='-' ? -PADDING : PADDING);
//padding is the little space between the start of the "box" for each
//stitch and the position that it goes back to the carrier at. It's different
//depending on what carrier is knitting the stitch
let carrierDepth = CARRIERS+CARRIER_SPACING*cNum;
//end is the starting point of the current stitch, so the last stitch,
//which is being updated in this function should end there
let end = [needle*(boxWidth+boxSpacing), height, carrierDepth];
if(direction === '+') end[0] -= boxWidth;
let carrierHeight = height;
if(lastNeedle[cNum]!==undefined){
//get the stitch that needs to be updated
let toUpdate = (lastNeedle[cNum].bed==='f'?
yarn[cNum][lastNeedle[cNum].row].floops[lastNeedle[cNum].needle]
: yarn[cNum][lastNeedle[cNum].row].bloops[lastNeedle[cNum].needle]);
toUpdate = toUpdate[toUpdate.length-1].ctrlPts;
let lastPt = toUpdate.length-1;
//determine which sides of the current and previous needle are part of the
//region that needs to be checked
let n1 = needle;
let n2 = lastNeedle[cNum].needle;
let LorR1 = (n1>n2 ? '-' : '+');
if(lastNeedle[cNum].direction !== direction)
LorR1 = (LorR1==='-' ? '+' : '-');
let LorR2 = (n2<n1 ? '+' : '-');
let upperBound = Math.max(n1, n2);
let lowerBound = Math.min(n1, n2);
carrierHeight = toUpdate[lastPt][1];
carrierHeight = getMaxHeight(carrierHeight, lowerBound,
upperBound, carrier);
//checking for intersection with yarn from other carriers
carrierHeight = carrierCheck(carrierHeight, bed, n1, cNum);
carrierHeight = carrierCheck(carrierHeight, lastNeedle[cNum].bed, n2, cNum);
//update last stitch
let subPadPrev = padding/maxCarriers * cNum;
toUpdate[lastPt][0] = end[0]+subPadPrev;
toUpdate[lastPt][1] = carrierHeight;
toUpdate[lastPt-1][1] = carrierHeight;
toUpdate[lastPt-2][1] = carrierHeight;
//set maxheight in between needles
setMaxHeight(carrierHeight, lowerBound, upperBound, carrier);
}
return carrierHeight;
}
//makes a list of points for a standard stitch
function makeStitch(direction, bed, needle, carrier, height){
let cNum = getCarrierNum(carrier);
let info = [];
let width = boxWidth-PADDING*2;
let dx = width/5;
let dy = boxHeight/3;
let dz = boxDepth/2;
let padding = (direction==='-' ? -PADDING : PADDING);
let activeRow = (bed==='f' ? frontActiveRow : backActiveRow);
let carrierDepth = CARRIERS+CARRIER_SPACING*cNum;
let start = [needle*(boxWidth+boxSpacing), height, carrierDepth];
if(direction === '-') dx*= -1;
else start[0] -= boxWidth;
if(bed==='b'){
dz*=-1;
}
//updates last stitch to prevent intersections
//carrierHeight is the starting height for this stitch as well.
let carrierHeight = updateLast(lastNeedle, direction, bed, needle, carrier, height);
//find the current needed height for the end of the current stitch
let x = start[0];
let y = start[1];
let z = start[2];
//again, this represents the carrier specific location that a stitch goes
//between the bed to carrier depth
let subPad = padding/maxCarriers * getCarrierNum(carrier);
let bedx1 = x+padding; //this is the start of the stitch after the padding
let bedx2 = x + padding + 5*dx +padding - subPad;
//this is the bed to carrier location at the end of the stitch
let nextStart = x +
(direction==='-' ? (-boxSpacing-boxWidth) : (boxSpacing+boxWidth));
//this is the start of the next stitch aka the end of the current "box"
x += subPad;
z = (bed==='b' ? BACK_BED : FRONT_BED);
info.push([x, carrierHeight, z]);
info.push([x, y, z]);
x = bedx1;
info.push([x, y, z]);
x += 2*dx;
z -= dz;
info.push([x, y, z]);
y += dy;
z += 2*dz;
info.push([x, y, z]);
x -= dx;
info.push([x, y, z]);
y += dy;
info.push([x, y, z]);
x += dx;
z -= 2*dz;
info.push([x, y, z]);
x += dx;
info.push([x, y, z]);
x += dx;
z += 2*dz;
info.push([x, y, z]);
y -= dy;
info.push([x, y, z]);
x -= dx;
info.push([x, y, z]);
y -= dy;
z -= 2*dz;
info.push([x, y, z]);
x += 2*dx;
z += dz;
info.push([x, y, z]);
x = bedx2;
info.push([x, y, z]);
info.push([x, y, z]);
y = height;
z = carrierDepth;
info.push([x, y, z]);
x = nextStart;
info.push([x, y, z]);
return info;
}
//makes a tuck. TODO multiple tucks on same needle
function tuck(direction, bed, needle, carrier){
let cNum = getCarrierNum(carrier);
let activeRow = (bed==='f' ? frontActiveRow : backActiveRow);
let row = (lastNeedle[cNum]===undefined ? 0 : lastNeedle[cNum].row);
if(lastNeedle[cNum]!==undefined && lastNeedle[cNum].direction!==direction)
row++;
let height = (activeRow[needle] !== undefined ?
minHeight(activeRow[needle], bed, needle)
: neighborHeight(bed, needle, carrier));
let info = makeStitch(direction, bed, needle, carrier, height);
let newLoop = new loop(info, carrier);
if(yarn[cNum]!==undefined && yarn[cNum][row]!==undefined){
let yarnLoops = (bed==='f' ?
yarn[cNum][row].floops : yarn[cNum][row].bloops);
if(yarnLoops[needle]){
yarnLoops[needle].push(newLoop);
}else{
yarnLoops[needle] = [newLoop];
}
}else{
let newFloop = [];
let newBloop = [];
if(bed==='f'){
newFloop[needle] = [newLoop];
}else
newBloop[needle] = [newLoop];
if(yarn[cNum]===undefined)
yarn[cNum] = [];
yarn[cNum][row] = new yarnPass(newFloop, newBloop, direction);
}
if(!activeRow[needle]){
activeRow[needle] = new loopSpec(row, carrier);
}else{
activeRow[needle].row = [row];
activeRow[needle].carrier = carrier;
}
lastNeedle[cNum] = {}
lastNeedle[cNum].needle = needle;
lastNeedle[cNum].carrier = carrier;
lastNeedle[cNum].bed = bed;
lastNeedle[cNum].direction = direction;
lastNeedle[cNum].row = row;
anyLast = lastNeedle[cNum];
}
//makes a knit
function knit(direction, bed, needle, carrier){
let cNum = getCarrierNum(carrier);
let activeRow = (bed==='f' ? frontActiveRow : backActiveRow);
let topHeight = neighborHeight(bed, needle, carrier);
let bottomHeight = (activeRow[needle] !== undefined ?
minHeight(activeRow[needle], bed, needle)+boxSpacing
: topHeight);
let info = makeStitch(direction, bed, needle, carrier, bottomHeight);
if(topHeight>bottomHeight){
//if the height of neighbor yarns is taller than the current stitch,
//it causes an intersection at the place where the stitch goes up in order
//to go back to the carrier depth.
//
//this attempts to fix it by raising the stretching the top loop of a
//stitch up. It seems to work for simple cases. TODO Needs more testing.
let topInfo = makeStitch(direction, bed, needle, carrier, topHeight);
for(let i = 4; i<=11; i++){
info[i] = topInfo[i];
}
}
let row = (lastNeedle[cNum]===undefined ? anyLast.row : lastNeedle[cNum].row);
let lastDir = (lastNeedle[cNum]===undefined?
anyLast.direction : lastNeedle[cNum].direction);
if(direction!==lastDir)
row++;
let newLoop = new loop(info, carrier);
if(yarn[cNum]!==undefined && yarn[cNum][row]!==undefined){
let yarnLoops = (bed==='f' ?
yarn[cNum][row].floops : yarn[cNum][row].bloops);
yarnLoops[needle] = [newLoop];
}else{
let newFloop = [];
let newBloop = [];
if(bed==='f'){
newFloop[needle] = [newLoop];
}else
newBloop[needle] = [newLoop];
if(yarn[cNum]===undefined)
yarn[cNum] = [];
yarn[cNum][row] = new yarnPass(newFloop, newBloop, direction);
}
activeRow[needle] = new loopSpec(row, carrier);
lastNeedle[cNum] = {};
lastNeedle[cNum].carrier = carrier;
lastNeedle[cNum].needle = needle;
lastNeedle[cNum].bed = bed;
lastNeedle[cNum].direction = direction;
lastNeedle[cNum].row = row;
anyLast = lastNeedle[cNum];
}
//transfer one loop to a different place
//TODO xfer should keep the places the needle has been to to ensure proper
//representation of yarn crossing and stuff.
//When doing: make sure to check for intersections and also update code that
//assumes each yarn loop has a set number of points (just in knit() and
//makeStitch() I think).
function xfer(fromSide, fromNeedle, toSide, toNeedle){
let fromActiveRow = (fromSide==='f' ? frontActiveRow : backActiveRow);
let toActiveRow = (toSide==='f' ? frontActiveRow : backActiveRow);
if(!fromActiveRow[fromNeedle]){
console.warn("Hmmm why are you trying to transfer from a needle without yarn? Ignored the instruction for now");
return;
}
let specs = fromActiveRow[fromNeedle];
let cNum = getCarrierNum(specs.carrier);
let info = (fromSide==='f' ? yarn[cNum][specs.row].floops[fromNeedle]
: yarn[cNum][specs.row].bloops[fromNeedle]);
let dx = (info[0].ctrlPts[2][0]-info[0].ctrlPts[1][0])/2;
let direction = (dx<0 ? '-' : '+');
let height = (toActiveRow[toNeedle] ?
minHeight(toActiveRow[toNeedle], toSide, toNeedle)
: minHeight(fromActiveRow[fromNeedle], fromSide, fromNeedle));
let updatedInfo = makeStitch(direction, toSide, toNeedle,
info[0].carrier, height);
for(let i = 5; i<=10; i++){
for(let j = 0; j<info.length; j++){
let x = updatedInfo[i][0];
let y = updatedInfo[i][1]-epsilon;
let z = updatedInfo[i][2];
info[j].ctrlPts[i] = [x, y, z];
}
}
let row = specs.row;
if(toActiveRow[toNeedle]===undefined){
toActiveRow[toNeedle] = new loopSpec(row, specs.carrier);
}
let destRow = yarn[cNum][row];
if(yarn[cNum][toRow] === undefined){
let newFloop = [];
let newBloop = [];
yarn[cNum][row] = new yarnPass(newFloop, newBloop,
yarn[cNum][row].direction);
destRow = yarn[cNum][row];
}
let destination = (toSide==='f' ? destRow.floops[toNeedle]
: destRow.bloops[toNeedle]);
for(let i = 0; i<info.length; i++){
let newLoop = new loop(info[i].ctrlPts.slice(), info[i].carrier);
if(destination){
if(toSide==='f')
destRow.floops[toNeedle].push(newLoop);
else
destRow.bloops[toNeedle].push(newLoop);
}else{
if(toSide==='f')
destRow.floops[toNeedle] = [newLoop];
else
destRow.bloops[toNeedle] = [newLoop];
}
}
if(lastNeedle[cNum].needle === fromNeedle
&& lastNeedle[cNum].bed === fromSide){
lastNeedle[cNum].needle = toNeedle;
lastNeedle[cNum].bed = toSide;
}
info = undefined;
fromActiveRow[fromNeedle] = undefined;
if(fromSide==='f') yarn[cNum][fromRow].floops[fromNeedle] = undefined;
else yarn[cNum][fromRow].bloops[fromNeedle] = undefined;
}
//transfer all the points stored in "yarn" into the output file
//ft. a incredibly gross number of nested things and braces
function makeTxt(){
let mostRecentC;
for(let cNum = 0; cNum<maxCarriers;cNum++){
if(yarn[cNum]!==undefined){
stream.write("usemtl mtl"+cNum+"\n");
for(let row = 0; row<yarn[cNum].length; row++){
if(yarn[cNum][row]!==undefined){
let direction = yarn[cNum][row].direction;
let yarnRow = yarn[cNum][row];
let maxNeedle = Math.max(yarnRow.floops.length,
yarnRow.bloops.length);
for(let col = 0; col<maxNeedle; col++){
let needle = col;
if(direction === '-') needle = maxNeedle-col-1;
let loop = yarnRow.floops[needle];
if(loop){
for(let i = 0; i<loop.length; i++){
let pts = loop[i].ctrlPts;
for(let j = 0; j<pts.length; j++){
let pt = pts[j];
stream.write(format(pt[0], pt[1], pt[2]));
}
}
}
loop = yarnRow.bloops[needle];
if(loop){
for(let i = 0; i<loop.length; i++){
let pts = loop[i].ctrlPts;
for(let j = 0; j<pts.length; j++){
let pt = pts[j];
stream.write(format(pt[0], pt[1], pt[2]));
}
}
}
}
}
}
}
}
}
//main parser
//heavily based on the knitout-dat.js parsing code in knitout-backend
function main(){
function slotNumber(bn){
if(bn.isFront())
return bn.needle;
else
return bn.needle+Math.floor(racking);
}
function slotString(bn){
return slotNumber(bn).toString();
}
function handleIn(cs, info){
if(cs.length === 0) return;
let inInfo = null;
cs.forEach(function(c){
if(!(c in carriers))
throw "ERROR: using a carrier ("+c+") that isn't active.";
if(carriers[c].in){
inInfo = carriers[c].in;
carriers[c].in = null;
}
});
if(inInfo){
if(JSON.stringify(inInfo.cs) !== JSON.stringify(cs))
throw "first use of carriers "+JSON.stringify(cs)
+" doesn't match in info " +JSON.stringify(inInfo);
if(inInfo.op === 'in'){
info.gripper = GRIPPER_IN;
}else if(inInfo.op === 'inhook'){
info.gripper = GRIPPER_IN;
info.hook = HOOK_IN;
if(hook !== null)
throw "ERROR: can't bring in "+JSON.stringify(cs)
+" with hook; hook is holding "+JSON.stringify(hook.cs);
hook = {direction: info.direction, cs:cs.slice()};
}else{
console.assert(false, "inInfo.op must be 'in' or 'inhook'.");
}
}
}
//update the .last member of the given carriers
function setLast(cs, d, n){
console.assert(typeof(n)==='object', "setLast needs a needle.");
cs.forEach(function(c){
console.assert(c in carriers, "carrier not in carrier set");
carriers[c].last =
{needle:n, direction:d};
});
}
let lines = fs.readFileSync(knitoutFile, 'utf8').split("\n");
(function checkVersion(){
let m = lines[0].match(/^;!knitout-(\d+)$/);
if(!m)
throw "File does not start with knitout magic string";
if(parseInt(m[1])>2)
console.warn("WARNING: File is version "+m[1]
+", but this code only knows about versions up to 2.");
})();
function getCarriers(line){
let m = line.includes(";;Carriers:");
if(m){
let c = line.substring(12);
allCarriers = c.split(' ');
maxCarriers = allCarriers.length;
CARRIER_SPACING /= maxCarriers;
}
}
let carriers = {}; //each are a name=>object map
let hook = null;
let racking = 0.0; //starts centered
let stitch = 5; //machine-specific stitch number
let row = 0;
lines.forEach(function(line, lineIdx){
row++;
let i = line.indexOf(';');
if(i>=0){
getCarriers(line);
line = line.substr(0,i);
}
let tokens = line.split(/[ ]+/);
if(tokens.length>0 && tokens[0] ==="") tokens.shift();
if(tokens.length>0 && tokens[tokens.length-1] === "") tokens.pop();
if(tokens.length == 0) return;
let op = tokens.shift();
let args = tokens;
//handle synonyms
if(op === 'amiss'){
op = 'tuck';
args.unshift('+');
}else if(op === 'drop'){
op = 'knit';
args.unshift('+');
}else if(op === 'xfer'){
op = 'split';
args.unshift('+');
}
if(op === 'in' || op === 'inhook'){
let cs = args;
if(cs.length === 0)
throw "ERROR: Can't bring in no carriers.";
cs.forEach(function(c){
if (c in carriers)
throw "Can't bring in an already active carrier, "+c;
});
let inInfo = {op:op, cs:cs.slice()};
//mark all carriers as pending
cs.forEach(function(c){
let carrier = new Carrier(c);
carrier.in = inInfo;
carriers[c] = carrier;
});
}else if(op === 'releasehook'){
let cs = args;
if(hook === null){
throw "ERROR: Can't releasehook on "+cs+", it's empty.";
}else if(JSON.stringify(hook.cs) !== JSON.stringify(cs)){
throw "ERROR: Can't releasehook on "+cs+
", hook currently holds "+hook+".";
}
hook = null;
}else if (op === 'out' || op === 'outhook'){
let cs = args;
cs.forEach(function(c){
if(!(c in carriers))
throw "ERROR: Can't bring out inactive carrier '"+c+".";
if(!carriers[c].last){
throw "ERROR: Can't bring out carrier '"+c
+"---it asn't yet stitched.";
}
});
if(op === 'outhook' && hook !==null)
throw "ERROR: Can't outhook carriers "+cs+", hook is holding "
+hook+".";
let s = -Infinity;
let n = null;
cs.forEach(function(c){
let t = slotNumber(carriers[c].last.needle);
if(t>s){
s = t;
n = carriers[c].last.needle;
}
});
//remove carriers from active set:
cs.forEach(function(c){
delete carriers[c];
});
}else if(op === 'tuck'|| op === 'knit'){
let d = args.shift();
let n = new BedNeedle(args.shift());
let cs = args;
if(cs.length === 0){
if(op === 'miss')
throw "ERROR: it makes no sense to miss with no yarns.";
else
d = DIRECTION_NONE; //miss and drop are directionless
}
let type;
if(op === 'miss' && cs.length === 0){
type = TYPE_A_MISS;
}else{
type = TYPE_KNIT_TUCK;
}
let info = {
type:type,
slots:{},
racking:racking,
stitch:stitch,
carriers:cs,
direction:d
}
if(op === 'miss') info.slots[slotString(n)] =
(n.isFront() ? OP_MISS_FRONT : OP_MISS_BACK);
else if(op === 'tuck'){
if(n.isFront()){
tuck(d, 'f', n.needle, cs[0]);
}else{
tuck(d, 'b', n.needle, cs[0]);
}
}else if(op === 'knit'){
if(n.isFront()){
knit(d, 'f', n.needle, cs[0]);
}else{
knit(d, 'b', n.needle, cs[0]);
}
}else console.assert(false, "op was miss, tuck, or knit");
handleIn(cs, info);
setLast(cs, d, n);
} else if(op === 'rack'){
if(args.length !== 1) throw "ERROR: racking takes one argument";
if(!/^[+-]?\d*\.?\d+$/.test(args[0]))
throw "ERROR: racking must be a number";
let newRacking = parseFloat(args.shift());
let frac = newRacking-Math.floor(newRacking);
if(frac != 0.0 && frac != .025)
throw "ERROR: racking must be an integer or an integer+0.25";
racking = newRacking;
}else if(op === 'split'){
let d = args.shift();
let n = new BedNeedle(args.shift()); //from needle
let t = new BedNeedle(args.shift()); //to needle
let cs = args;
//make sure that 't' and 'n' align reasonably:
if(n.isBack() && t.isFront()){
if(n.needle+racking !== t.needle){
throw "ERROR: needles '"+n+"' and '"+t
+"' are not aligned at racking "+racking+".";
}
}else if(n.isFront() && t.isBack()){
if(n.needle !== t.needle+racking){
throw "ERROR: needles '"+n+"' and '"+t
+"' are not aligned at racking "+racking+".";
}
}
let op;
let type;
//make sure this is a valid operation, and fill in proper OP
if(n.isHook && t.isHook()){
if(cs.length === 0){
type= TYPE_XFER;
if(n.isFront()){
xfer('f', n.needle, 'b', t.needle);
}else{