forked from textiles-lab/autoknit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoknit.js
360 lines (314 loc) · 10.1 KB
/
autoknit.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
"use strict";
const fs = require('fs');
let Carrier = "5";
let StartRows = 10;
let EndRows = 10;
let YarnInStitchNumber = 67;
let CastOnStitchNumber = 67;
let PlainStitchNumber = 68;
let OutFile = "";
//figure out output filename based on input js name / other arguments:
if (process.argv.length >= 2) {
if (process.argv[1].endsWith(".js")) {
OutFile = process.argv[1].substr(0, process.argv[1].length - 3) + ".k";
}
for (let i = 2; i < process.argv.length; ++i) {
if (process.argv[i].startsWith("out:")) {
OutFile = process.argv[i].substr(4);
}
}
}
if (OutFile === "") {
console.log("NOTE: will not write output file.");
} else {
console.log("Will write output to '" + OutFile + "'");
}
function parseBedNeedle(bn) {
if (typeof(bn) !== "string") throw new Error("parseBedNeedle must be called with a string");
let m = bn.match(/^([fb]s?)([-+]?\d+)$/);
if (m === null) throw new Error("string '" + bn + "' does not look like a needle");
return {
bed:m[1],
needle:parseInt(m[2])
};
}
function bnToHalf(bn_str) {
let bn = parseBedNeedle(bn_str);
if (bn.bed === 'f') return 'f' + (2*bn.needle);
else if (bn.bed === 'fs') return 'f' + (2*bn.needle+1);
else if (bn.bed === 'b') return 'b' + (2*bn.needle+1);
else if (bn.bed === 'bs') return 'b' + (2*bn.needle);
else throw "don't know how to half-guage the needle '" + bn_str + "'";
}
function Helpers() {
this.knitout = [];
this.inYarns = {};
this.yarnUsedCount = {};
this.needsReleaseHook = {};
this.racking = 0;
this.needIn = true; //TODO: bring in carrier in a nice way when resuming another tube
this.out(";!knitout-2");
this.out(";;Carriers: 1 2 3 4 5 6 7 8 9 10");
}
Helpers.prototype.out = function out(str) {
//console.log("OUT: " + str);
this.knitout.push(str);
};
Helpers.prototype.write = function write() {
if (OutFile !== "") {
fs.writeFileSync(OutFile, this.knitout.join("\n") + "\n");
}
};
Helpers.prototype.start_tube = function start_tube(dir, bns) {
let front = [];
let back = [];
bns.forEach(function(bn_str){
let bn = parseBedNeedle(bn_str)
if (bn.bed === 'f') {
front.push(bn.needle);
} else if (bn.bed === 'b') {
back.push(bn.needle);
} else {
console.assert("start_tube should only be called with 'f' or 'b' needles.");
}
});
// not lexicographic, numeric sort please!
front.sort(function(a,b){return a-b});
back.sort(function(a,b){return a-b});
console.assert(front.length !== 0 && back.length !== 0, "should start a tube with at least a stitch on each bed.");
//do a tuck pattern to anchor yarn:
// v v v <--
// v v -->
// ^------ first needle to be knit is here
let n = Math.max(front[front.length-1], back[back.length-1]);
let toDrop = [];
let me = this;
function initTuck(d, bn) {
me.tuck(d, bn);
toDrop.push(bn);
}
this.out("x-stitch-number " + YarnInStitchNumber);
if(!this.inYarns[Carrier]){
this.out("inhook " + Carrier);
this.inYarns[Carrier] = true;
this.yarnUsedCount[Carrier] = 0;
console.log("Bringing Carrier " + Carrier.toString() + " in.");
} else {
console.warn("Bringing in carrier that was already in " + Carrier.toString());
}
initTuck('-', 'f' + n);
initTuck('-', 'f' + (n-2));
initTuck('-', 'f' + (n-4));
initTuck('+', 'f' + (n-3));
initTuck('+', 'f' + (n-1));
if(this.inYarns[Carrier])
this.out("releasehook " + Carrier);
//make list of needles and directions in tube order:
let sts = [];
if (dir === 'clockwise') {
for (let i = front.length-1; i >= 0; --i) {
sts.push(['-', 'f' + front[i]]);
}
for (let i = 0; i < back.length; ++i) {
sts.push(['+', 'b' + back[i]]);
}
} else { console.assert(dir === 'anticlockwise');
for (let i = back.length-1; i >= 0; --i) {
sts.push(['-', 'b' + back[i]]);
}
for (let i = 0; i < front.length; ++i) {
sts.push(['+', 'f' + front[i]]);
}
}
//alternating tuck cast on:
this.out("x-stitch-number " + CastOnStitchNumber);
sts.forEach(function(dbn, i) {
if (i%2 == 0) this.knit(dbn[0], dbn[1]);
}, this);
sts.forEach(function(dbn, i) {
if (i%2 == 1) this.knit(dbn[0], dbn[1]);
}, this);
//drop everything in 'toDrop' that wasn't part of alternating tucks:
toDrop.forEach(function(bn){
//WARNING: this might actually drop **TOO MUCH** if the tucked needles overlap other existing stitches
let idx = bns.indexOf(bn);
if (idx === -1) {
this.drop(bn);
}
}, this);
//knit some plain rows:
this.out("x-stitch-number " + PlainStitchNumber);
for (let row = 0; row < StartRows; ++row) {
sts.forEach(function(dbn, i) {
this.knit(dbn[0], dbn[1]);
}, this);
}
let first = 0;
while (first < sts.length && sts[first][1] !== bns[0]) ++first;
console.assert(first < sts.length, "First stitch from 'bns' should exist in 'sts'.");
//knit a bit extra to get aligned to the input bns:
for (let i = 0; i < first; ++i) {
let st = sts.shift();
this.knit(st[0], st[1]);
sts.push(st);
}
//alternating stitches to separate starting tube from knitting:
this.out("x-stitch-number " + CastOnStitchNumber);
sts.forEach(function(dbn, i) {
if (i%2 == 0) this.knit(dbn[0], dbn[1]);
}, this);
sts.forEach(function(dbn, i) {
if (i%2 == 1) this.knit(dbn[0], dbn[1]);
}, this);
this.out("x-stitch-number " + PlainStitchNumber);
};
Helpers.prototype.bringIn = function bringIn(Carrier) {
console.warn("Bringing in Carrier " + Carrier.toString() + " before use.");
this.out("inhook " + Carrier);
this.inYarns[Carrier] = true;
this.needsReleaseHook[Carrier] = true;
this.yarnUsedCount[Carrier] = 0;
}
Helpers.prototype.knit = function knit(d, bn) {
if(!this.inYarns[Carrier]){
console.warn("Using carrier before inhook : " + Carrier.toString());
this.bringIn(Carrier);
}
if(this.needsReleaseHook[Carrier] && this.yarnUsedCount[Carrier] > 5){
this.out("releasehook " + Carrier);
this.needsReleaseHook[Carrier] = false;
}
this.yarnUsedCount[Carrier] += 1;
this.out("knit " + d + " " + bnToHalf(bn) + " " + Carrier);
};
Helpers.prototype.drop = function drop(bn) {
this.out("drop " + bnToHalf(bn));
};
Helpers.prototype.tuck = function tuck(d, bn) {
if(!this.inYarns[Carrier]){
console.warn("Using carrier before inhook : " + Carrier.toString());
this.bringIn(Carrier);
}
if(this.needsReleaseHook[Carrier] && this.yarnUsedCount[Carrier] > 5){
this.out("releasehook " + Carrier);
this.needsReleaseHook[Carrier] = false;
}
this.yarnUsedCount[Carrier] += 1;
this.out("tuck " + d + " " + bnToHalf(bn) + " " + Carrier);
};
Helpers.prototype.miss = function miss(d, bn) {
if(!this.inYarns[Carrier]){
console.warn("Using carrier before inhook : " + Carrier.toString());
this.bringIn(Carrier);
}
if(this.needsReleaseHook[Carrier] && this.yarnUsedCount[Carrier] > 5){
this.out("releasehook " + Carrier);
this.needsReleaseHook[Carrier] = false;
}
this.out("miss " + d + " " + bnToHalf(bn) + " " + Carrier);
};
Helpers.prototype.decrease = function decrease(d, bn) {
this.knit(d, bn);
};
Helpers.prototype.increase = function increase(d0, bn0, d1, bn1) {
this.knit(d0, bn0);
this.tuck(d1 == '+' ? '-' : '+', bn1);
};
Helpers.prototype.end_tube = function end_tube(dir, bns) {
//d(bn) returns direction to knit on 'bn' given overall tube direction
function d(bn_str) {
let bn = parseBedNeedle(bn_str);
if (bn.bed[0] === 'f') {
if (dir === 'clockwise') {
return '-';
} else { console.assert(dir === 'anticlockwise', "dir is always clockwise or anticlockwise");
return '+';
}
} else { console.assert(bn.bed[0] === 'b', "bed is always f* or b*");
if (dir === 'clockwise') {
return '+';
} else { console.assert(dir === 'anticlockwise', "dir is always clockwise or anticlockwise");
return '-';
}
}
}
//alternating stitches to separate ending tube from knitting:
this.out("x-stitch-number " + CastOnStitchNumber);
bns.forEach(function(bn, i) {
if (i%2 == 0) this.knit(d(bn), bn);
}, this);
bns.forEach(function(bn, i) {
if (i%2 == 1) this.knit(d(bn), bn);
}, this);
this.out("x-stitch-number " + PlainStitchNumber);
for (let row = 0; row < EndRows; ++row) {
bns.forEach(function(bn, i) {
this.knit(d(bn), bn);
}, this);
}
if(this.inYarns[Carrier]){
this.out("outhook " + Carrier);
this.inYarns[Carrier] = false;
this.yarnUsedCount[Carrier] = 0;
console.log("Taking Carrier " + Carrier.toString() + " out.");
} else {
console.warn("Taking out carrier that was never in : " + Carrier.toString());
}
bns.forEach(function(bn, i) {
this.drop(bn);
}, this);
};
Helpers.prototype.xfer = function xfer(from, to) {
// if any releasehooks are pending, do them before xfers
for (let c in this.needsReleaseHook) {
if (this.needsReleaseHook[c]) {
this.out("releasehook " + c);
this.needsReleaseHook[c] = false;
}
}
this.out("xfer " + bnToHalf(from) + " " + bnToHalf(to));
};
Helpers.prototype.setRacking = function setRacking(from_str, to_str) {
let target;
if (arguments.length === 0) {
target = 0;
} else {
let from = parseBedNeedle(bnToHalf(from_str));
let to = parseBedNeedle(bnToHalf(to_str));
if (from.bed === 'f' && to.bed === 'b') {
target = from.needle - to.needle;
} else { console.assert(from.bed === 'b' && to.bed === 'f');
target = to.needle - from.needle;
}
console.assert(Math.abs(target) <= 8, "Racking out of limits?"+from_str+" "+to_str);
}
if (this.racking !== target) {
this.racking = target;
this.out("rack " + this.racking);
}
};
Helpers.prototype.xfer_cycle = function xfer_cycle(opts, from, to, xfers) {
xfers.forEach(function(xf){
this.setRacking(xf[0], xf[1]);
this.xfer(xf[0], xf[1]);
}, this);
};
Helpers.prototype.stash = function stash(from, to) {
if (from.length !== to.length) throw new Error("from and to arrays should be the same length");
if (from.length === 0) return;
this.setRacking(from[0], to[0]);
for (let i = 0; i < from.length; ++i) {
this.xfer(from[i], to[i]);
}
this.setRacking();
};
Helpers.prototype.unstash = function unstash(from, to) {
if (from.length !== to.length) throw new Error("from and to arrays should be the same length");
if (from.length === 0) return;
this.setRacking(from[0], to[0]);
for (let i = 0; i < from.length; ++i) {
this.xfer(from[i], to[i]);
}
this.setRacking();
};
module.exports = {Helpers:Helpers};