-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawdown.js
380 lines (335 loc) · 13.7 KB
/
Drawdown.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
class Drawdown {
constructor(_wefts, _warps) {
this.wefts = _wefts;
this.warps = _warps;
this.patternName;
this.numShafts;
this.shafts = [];
this.threading = [];
this.glitchSectionSize;
this.rowDataTemplate = {
positions: null,
originalPositions: null,
glitched: null // true or false
};
this.drawdownData = []; // array of rowDataTemplate objects
this.threadingData = []; // array of rowDataTemplate objects
this.tieupData = []; // array of rowDataTemplate objects
this.liftPlanData = []; // array of rowDataTemplate objects
}
loadPattern(_patternName) {
if (jsonData) {
let structure = this.findInObject(jsonData, struct => struct.name === _patternName);
if (structure) {
this.patternName = structure.name;
this.numShafts = structure.numShafts;
this.shafts = structure.shafts;
this.threading = this.fillArray(structure.threadingBase); // expands the threading base across the warps;
this.glitchSectionSize = structure.glitchSectionSize;
}
}
}
createThreading() {
// populate positions based on number of shafts
let positions = [];
for (let i = 0; i < this.numShafts; i++) {
positions[i] = [];
}
// populate positions with threading data
for (let i = 0; i < this.warps; i++) {
let currKey = this.threading[i];
if (positions[currKey - 1].length == 0) {
// not seen this key yet, add it
positions[currKey - 1] = [i + 1];
} else {
// update existing key
positions[currKey - 1].push(i + 1);
}
}
// position data saved into threadingData array
this.threadingData = [];
for (let i = 0; i < this.numShafts; i++) {
this.threadingData[i] = { ...this.rowDataTemplate };
this.threadingData[i].positions = [...positions[i]];
this.threadingData[i].originalPositions = [...positions[i]];
this.threadingData[i].glitched = false;
}
}
createTieup() {
for (let i = 0; i < this.numShafts; i++) {
let shaft = [i + 1];
this.tieupData[i] = { ...this.rowDataTemplate };
this.tieupData[i].positions = [...shaft];
this.tieupData[i].originalPositions = [...shaft];
this.tieupData[i].glitched = false;
}
}
createLiftPlan() {
this.liftPlanData = []
for (let i = 0; i < this.wefts; i = i + this.shafts.length) {
for (let j = 0; j < this.shafts.length; j++) {
if (this.liftPlanData.length < this.wefts) {
// append the next row
let importedRow = this.shafts[j];
let newRow = { ...this.rowDataTemplate };
newRow.positions = [...importedRow];
newRow.originalPositions = [...importedRow];
newRow.glitched = false;
this.liftPlanData.push(newRow);
} else {
break;
}
}
}
}
glitchLiftPlan() {
let currRow = 0;
let currSection = 0;
for (let i = 0; i < this.liftPlanData.length; i += this.glitchSectionSize) {
let sliceSize;
if (this.liftPlanData.length - currRow > this.glitchSectionSize) {
sliceSize = this.glitchSectionSize;
} else {
sliceSize = this.liftPlanData.length - currRow;
}
// Take a slice of the lift plan
let slice = this.liftPlanData.slice(i, i + sliceSize);
// Perform the glitch operation
let glitchSection = this.gradientGlitch(slice, currSection, currRow);
for (let j = 0; j < glitchSection.length; j++) {
if (currRow < this.liftPlanData.length) {
this.liftPlanData[currRow] = glitchSection[j];
currRow++;
}
}
currSection++;
}
}
gradientGlitch(liftPlanSegment, currSection, currRow) {
// set number of changes based on current section and glitchMod
let numChanges = currSection === 0 ? 0 : currSection + glitchMod;
// make a copy of the lift plan slice to modify without affecting the original
let modLiftPlan = this.copyRowDataArray(liftPlanSegment);
// loop the number of changes
let px = 0; // left-most point on the rectangle
for (let i = 0; i < numChanges; i++) {
if (modLiftPlan.length > 1) {
// select row and shaft
let py = currRow * cellSize;
let selectedRow = this.perlinChoose(modLiftPlan.length, px, py);
py = (currRow + selectedRow) * cellSize;
let selectedShaft = this.perlinChoose(modLiftPlan[selectedRow].positions.length, px, py);
// apply glitch
let whichGlitch = this.perlinChoose(3, px, py);
// Glitch 0: Add a shaft, Glitch 1: Remove a shaft, Glitch 2: Swap a shaft
if (whichGlitch === 0) {
// add a shaft
if (modLiftPlan[selectedRow].positions.length < this.numShafts - 1) {
let newShaft = this.perlinChoose(this.numShafts, px, py) + 1;
if (!modLiftPlan[selectedRow].positions.includes(newShaft)) {
modLiftPlan[selectedRow].positions.push(newShaft);
} else {
whichGlitch = this.perlinChoose(2, px, py) + 1;
}
} else {
whichGlitch = this.perlinChoose(2, px, py) + 1;
}
}
if (whichGlitch === 1) {
// remove a shaft
if (modLiftPlan[selectedRow].positions.length > 1) {
modLiftPlan[selectedRow].positions.splice(selectedShaft, 1);
} else {
// deleting the last shaft will leave none, choose a new shaft instead
whichGlitch = 2;
}
}
if (whichGlitch === 2) {
// switch a shaft
modLiftPlan[selectedRow].positions = this.switchShaft(modLiftPlan[selectedRow].positions, px, py);
}
// mark if the row has been glitched
modLiftPlan[selectedRow].glitched = !this.arraysEqual(modLiftPlan[selectedRow].positions, modLiftPlan[selectedRow].originalPositions);
// change sampling position in Perlin noise field
px = px + cellSize;
}
}
return modLiftPlan;
}
switchShaft(currentRow, px, py) {
// Choose a shaft to delete
let selectedShaftIndex = this.perlinChoose(currentRow.length, px, py);
// Delete selected shaft from current row
currentRow.splice(selectedShaftIndex, 1); // Removes the item at the selected index
// Choose a new shaft
let newShaft = this.perlinChoose(this.numShafts, px, py) + 1;
// Add new shaft to current row
currentRow.push(newShaft);
return currentRow;
}
perlinChoose(numItems, px, py) {
px = px + pan; // Add offset when panning
// noise() returns a value between 0 and 1
let trim = 0.3;
let pNoise = noise(px / pZoom, py / pZoom); // 0..1
// Adjust noise value within the trimmed range
if (pNoise < trim) {
pNoise = trim + 0.01;
} else if (pNoise > (1 - trim)) {
pNoise = 1 - trim - 0.01;
}
// Map the noise value to a number between 0 and numItems
let selected = Math.floor(map(pNoise, trim, 1 - trim, 0, numItems));
return selected;
}
createDrawdown() {
// Create drawdown from liftPlanData and threadingData
for (let i = 0; i < this.liftPlanData.length; i++) {
let rowLiftedWarps = [];
for (let j = 0; j < this.liftPlanData[i].positions.length; j++) {
let shaft = this.liftPlanData[i].positions[j];
rowLiftedWarps = rowLiftedWarps.concat(this.threadingData[shaft - 1].positions);
}
// Create a new rowData object for drawdown
this.drawdownData[i] = { ...this.rowDataTemplate };
this.drawdownData[i].positions = rowLiftedWarps;
if (this.liftPlanData[i].glitched) {
this.drawdownData[i].glitched = true;
}
}
}
// Display methods
printDraft() {
background(100); // Dark grey
// calc padding and dimensions
let padding = cellSize;
let liftPlanWidth = this.numShafts * cellSize;
let threadingHeight = this.numShafts * cellSize;
// Print sections
this.printSection("tieup", liftPlanWidth + padding, threadingHeight);
this.printSection("threading", 3 * padding + liftPlanWidth, threadingHeight);
this.printSection("liftplan", liftPlanWidth + padding, 2 * padding + threadingHeight);
this.printSection("drawdown", 3 * padding + liftPlanWidth, 2 * padding + threadingHeight);
if (showLines) {
let numX = cellSize + 7;
let numY = 7 * cellSize - 2;
for (let i = this.wefts; i > 0; i--) {
fill(i % 5 === 0 ? [255, 204, 255] : 255);
text(i, numX, numY);
numY += cellSize;
}
}
}
printSection(section, leftBuffer, topBuffer) {
let sectionData;
let numCols;
switch (section) {
case "tieup":
sectionData = this.tieupData;
numCols = this.numShafts;
break;
case "threading":
sectionData = this.threadingData;
numCols = this.warps;
break;
case "liftplan":
sectionData = this.liftPlanData;
numCols = this.numShafts;
break;
case "drawdown":
sectionData = this.drawdownData;
numCols = this.warps;
break;
default:
console.log("Error finding section data");
}
// Display section grids
for (let row = 0; row < sectionData.length; row++) {
for (let col = 0; col < numCols; col++) {
let warpLifted = sectionData[row].positions.includes(col + 1);
let weftGlitched = sectionData[row].glitched;
if (warpLifted && weftGlitched) {
fill(255, 51, 153); // Pink cell
} else if (!warpLifted && weftGlitched) {
fill(255, 204, 255); // Light pink cell
} else if (warpLifted) {
fill(0); // Black cell
} else {
fill(255); // White cell
}
// Determine the pixel position
let pixelX = 0
let pixelY = 0;
switch (section) {
case "tieup":
// bottom-right
pixelX = leftBuffer - (col * cellSize);
pixelY = topBuffer - (row * cellSize);
break;
case "threading":
// bottom-left
pixelX = leftBuffer + (col * cellSize);
pixelY = topBuffer - (row * cellSize);
break;
case "liftplan":
// top-right
pixelX = leftBuffer - (col * cellSize);
pixelY = topBuffer + (row * cellSize);
break;
case "drawdown":
// top-left
pixelX = leftBuffer + (col * cellSize);
pixelY = topBuffer + (row * cellSize);
break;
}
rect(pixelX, pixelY, cellSize, cellSize);
}
}
}
// helpers
findInObject(obj, searchFn) {
for (let key in obj) {
if (obj.hasOwnProperty(key) && searchFn(obj[key])) {
return obj[key];
}
}
return null;
}
fillArray(array) {
let filledArray = [];
let loopQuant = Math.ceil(this.warps / array.length);
for (let i = 0; i < loopQuant; i++) {
for (let j = 0; j < array.length; j++) {
if (filledArray.length === this.warps) {
break;
} else {
filledArray.push(array[j]);
}
}
}
return filledArray;
}
copyRowDataArray(rowDataArray) {
return rowDataArray.map(rowData => {
return {
positions: [...rowData.positions], // Copy of positions array
originalPositions: [...rowData.originalPositions], // Copy of originalPositions array
glitched: rowData.glitched
};
});
}
arraysEqual(arr1, arr2) {
// Check if the arrays are the same length
if (arr1.length !== arr2.length) {
return false;
}
// Check each element in the arrays
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
// If no elements were different, the arrays are equal
return true;
}
}