Skip to content

Commit

Permalink
Added extensions x-loop-in, x-yarn-in, x-yarn-out, and x-loop-out whi…
Browse files Browse the repository at this point in the history
…ch allow one to materialize and dematerialize loops/yarn carriers in order to make compact visualizations of structures without casting on or dropping stitches.
  • Loading branch information
ixchow committed May 4, 2021
1 parent 3f37d91 commit f14cc7e
Show file tree
Hide file tree
Showing 5 changed files with 423 additions and 9 deletions.
69 changes: 69 additions & 0 deletions CellMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,32 @@ CellMachine.prototype.outhook = function CellMachine_outhook(cs) {
this.out(cs); //hook doesn't matter for this code
};

//yarn appears just before location n in direction d:
CellMachine.prototype.yarnInEdge = function CellMachine_yarnInEdge(d, n, cs) {
this.topRow += 1; //HACK
this.in(cs);

cs.forEach(function(cn) {
let c = this.getCarrier(cn);
c.before = true; //HORRIBLE HACK (makeAfter complains if carrier isn't '.before')
}, this);
const beforeN = (d === '-' ? nextNeedle(n) : previousNeedle(n));
this.makeAfter(d, beforeN, cs, []);
};

//yarn vanishes just after location n in direction d:
CellMachine.prototype.yarnOutEdge = function CellMachine_yarnOutEdge(d, n, cs) {
const afterN = (d === '+' ? nextNeedle(n) : previousNeedle(n));
this.bringCarriers(d, afterN, cs);
//more HACKS to fix up yarn carrier metadata:
cs.forEach(function(cn){
let c = this.getCarrier(cn);
delete c.before;
c.after = {d:(d === '+' ? '-' : '+'), n:afterN};
}, this);
this.out(cs);
}

CellMachine.prototype.stitch = function CellMachine_stitch(l, t) {
//TODO: set leading / stitch values.
};
Expand All @@ -1123,6 +1149,49 @@ CellMachine.prototype.rack = function CellMachine_rack(r) {
this.racking = r;
};

CellMachine.prototype.loopInEdge = function CellMachine_loopInEdge(d, n, cs) {
/*this.yarnInEdge(d,n,cs);
this.tuck(d,n,cs);
this.yarnOutEdge(d,n,cs); //TODO: advance
*/

//build a special 'loop in' face:
let cell = new LoopCell('i');

//add loop inputs from the column:
let c = this.beds[needleBed(n)].getColumn(needleIndex(n));
if (c.length) {
c[c.length-1].ports['^'].forEach(function (cn) {
cell.addOut('v', cn);
});
}

//add knit yarns:
cs.slice().reverse().forEach(function(cn){
cell.addOut((d === '+' ? '-' : '+'), cn);
cell.addOut('^', cn);
cell.addOut(d, cn);
}, this);


this.knitTuck('+', n, [], cell);
};

CellMachine.prototype.loopOutEdge = function CellMachine_loopOutEdge(n) {
//build a special 'loop out' face:
let out = new LoopCell('o');

//add loop inputs from the column:
let c = this.beds[needleBed(n)].getColumn(needleIndex(n));
if (c.length) {
c[c.length-1].ports['^'].forEach(function (cn) {
out.addOut('v', cn);
});
}

this.knitTuck('+', n, [], out);
};

CellMachine.prototype.knitTuck = function CellMachine_knitTuck(d, n, cs, knitTuck) {
//Bring carriers on over:
this.bringCarriers(d, n, cs);
Expand Down
4 changes: 4 additions & 0 deletions VectorTiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ VectorTiles.addLoopTile = function VectorTiles_addLoopTile(drawing, styles, tile
loops = [];
doLib('x-' + bed[0]);
loops = temp;
} else if (type === 'i') { //magic loop in
doLib(type + "-" + bed[0]);
} else if (type === 'o') { //magic loop out
doLib(type + "-" + bed[0]);
} else {
//unknown!
drawing.addLine(layer, styles, '#f0f', [ 0.0, 0.0, 13.0, 9.0 ], ll);
Expand Down
108 changes: 108 additions & 0 deletions VectorTilesLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -1216,5 +1216,113 @@ window.VectorTilesLib = {"k-b-l2-y2":{
},
"m-b":{

},
"i-b-y2":{
"y1":[
[8.5,5.1, 8.5,5],
[8.5,6.5, 8.5,9],
[4.5,9, 4.5,6.5],
[4.5,5.1, 4.5,5]
],
"y2":[
[5.5,4.6, 5.5,4.5],
[7.5,4.6, 7.5,4.5],
[7.5,6, 7.5,9],
[5.5,6, 5.5,9]
]
},
"i-b-y1":{
"y1":[
[8.5,5.1, 8.5,5],
[8.5,6.5, 8.5,9],
[4.5,9, 4.5,6.5],
[4.5,5.1, 4.5,5]
]
},
"i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11.5,4],
[1.5,4, 1.5,1.5, 4.5,1.5, 4.5,0]
]
},
"o-f":{

},
"o-b":{

}
};
30 changes: 30 additions & 0 deletions parseKnitout.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,36 @@ function parseKnitout(codeText, machine, useKnitoutAsSource=false) {
machine.pause();
} else if (op === "x-end") {
end = true;
} else if (op === "x-yarn-in") {
//yarn carrier stack appears just before a given location
if (tokens.length < 3) throw "x-yarn-in requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);

machine.yarnInEdge(d, n, cs);
} else if (op === "x-yarn-out") {
//yarn carrier stack vanishes just after a given location
if (tokens.length < 3) throw "x-yarn-out requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);

machine.yarnOutEdge(d, n, cs);
} else if (op === "x-loop-in") {
//yarn loop appears at a given location, as if made by given carriers (but not connected)
if (tokens.length < 3) throw "x-loop-in requires at least three arguments";
var d = parseDirection(tokens.shift());
var n = parseNeedle(tokens.shift());
var cs = parseCarrierSet(tokens);

machine.loopInEdge(d, n, cs);
} else if (op === "x-loop-out") {
//yarn loop vanishes at a given location
if (tokens.length !== 1) throw "x-loop-out requires one argument";
var n = parseNeedle(tokens.shift());

machine.loopOutEdge(n);
} else if (op.startsWith("x-")) {
if (op in machine) {
machine[op](...tokens);
Expand Down
Loading

0 comments on commit f14cc7e

Please sign in to comment.