-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerative_weaving.pde
539 lines (444 loc) · 14.9 KB
/
generative_weaving.pde
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
import processing.svg.*;
import java.util.Arrays;
//==== global variables ====//
int seed;
int pZoom;
int pan;
int glitchMod;
int glitchSectionSize;
int cellSize;
int weftQuant;
int warpQuant;
int numShafts;
PFont rowNum;
boolean showLines;
// weave structures
int selectedStructure;
int[] threadingBase;
int[][] structureShafts;
RowData[] liftPlan;
RowData[] drawdown;
RowData[] threading;
RowData[] tieUps;
// glitch metrics to check error distribution
int[] rowFrequency;
void setup() {
// size(720, 705);
size(720, 1400);
seed = int(random(1, 100));
noiseSeed(seed);
pan = 0;
pZoom = 10; // Perlin noise zoom level
glitchMod = 0;
// cellSize = 15; // size of each cell in the output
cellSize = 8;
rowNum = createFont("Arial",10,true);
showLines = false;
// loom variables
warpQuant = 45;
weftQuant = 55;
// weave structure variables
Structure[] structures = importJSONStructures("structures.json");
selectedStructure = 14;
structureShafts = structures[selectedStructure].shafts;
numShafts = structures[selectedStructure].numShafts;
threadingBase = structures[selectedStructure].threadingBase;
glitchSectionSize = structures[selectedStructure].glitchSectionSize;
threading = createThreading(threadingBase, warpQuant, numShafts);
tieUps = createTieUps(numShafts);
println("Selected Structure: ", structures[selectedStructure].name);
}
void draw() {
rowFrequency = new int[structureShafts.length];
// create unglitched lift plan by tiling the structure
int structureLength = structureShafts.length;
liftPlan = new RowData[0];
for (int i = 0; i < weftQuant; i = i + structureLength) {
for (int j = 0; j < structureLength; j++) {
if (liftPlan.length <= weftQuant) {
// append the next row
int[] importedRow = structureShafts[j];
RowData newRow = new RowData(importedRow);
liftPlan = addRow(liftPlan, newRow);
} else {
break;
}
}
}
int currRow = 0;
int currSection = 0;
// glitch sections of lift plan, increasing in frequency, deterministally
for (int i = 0; i < liftPlan.length; i = i + glitchSectionSize) {
int sliceSize;
if (liftPlan.length - currRow > glitchSectionSize) {
sliceSize = glitchSectionSize;
} else {
sliceSize = liftPlan.length - currRow;
}
// glitch section
RowData[] slice = (RowData[]) subset(liftPlan, i, sliceSize);
RowData[] glitchSection = gradientGlitch(slice, currSection, currRow);
for (int j = 0; j < glitchSection.length; j++) {
liftPlan[currRow] = glitchSection[j];
currRow++;
}
currSection++;
}
drawdown = createDrawdown(liftPlan, threading);
printDraft(liftPlan, drawdown, threading, tieUps);
// println(rowFrequency);
noLoop();
}
RowData[] gradientGlitch(RowData[] liftPlanSegment, int currSection, int currRow) {
int numChanges;
if (currSection == 0) {
numChanges = 0;
} else {
numChanges = currSection + glitchMod;
}
// copy liftPlanSegment into modLiftPlan
RowData[] modLiftPlan = new RowData[liftPlanSegment.length];
arrayCopy(liftPlanSegment, modLiftPlan);
int px = 0; // left-most point on the rectangle
for (int i = 0; i < numChanges; i++) {
if (modLiftPlan.length > 1) {
// choose row
int py = currRow * cellSize;
int selectedRow = perlinChoose(modLiftPlan.length, px, py);
rowFrequency[selectedRow]++;
// select shaft
py = (currRow + selectedRow) * cellSize;
int selectedShaft = perlinChoose(modLiftPlan[selectedRow].positions.length, px, py);
// three possible modifications: 1) add a shaft, 2) remove a shaft, and 3) switch a shaft
int whichGlitch = perlinChoose(3, px, py);
if (whichGlitch == 0) {
// add a shaft
if (modLiftPlan[selectedRow].positions.length < numShafts - 1) {
int newShaft = perlinChoose(numShafts, px, py) + 1;
if (arrayContains(modLiftPlan[selectedRow].positions, newShaft) == false) {
int[] newRow = append(modLiftPlan[selectedRow].positions, newShaft);
modLiftPlan[selectedRow].positions = newRow;
} else {
// cannot add any more shafts
whichGlitch = perlinChoose(2, px, py) + 1;
}
} else {
// cannot add any more shafts
whichGlitch = perlinChoose(2, px, py) + 1;
}
}
if (whichGlitch == 1) {
// remove a shaft
if (modLiftPlan[selectedRow].positions.length > 1) {
modLiftPlan[selectedRow].positions = deleteElement(modLiftPlan[selectedRow].positions, selectedShaft);
} else {
// deleting the last shaft will leave none, choose a new shaft instead
whichGlitch = 2;
}
}
if (whichGlitch == 2) {
// switch a shaft
modLiftPlan[selectedRow].positions = switchShaft(modLiftPlan[selectedRow].positions, px, py);
}
// check if glitch was successful
if (Arrays.equals(modLiftPlan[selectedRow].positions, modLiftPlan[selectedRow].backupPositions) == false) {
modLiftPlan[selectedRow].glitched = true;
} else {
modLiftPlan[selectedRow].glitched = false;
}
// change sampling position in Perlin noise field
px = px + cellSize;
}
}
return modLiftPlan;
}
int perlinChoose(int numItems, int px, int py) {
// use xy coords to select an item deterministically
// if numItems = 4, will return a num between 0 and 3
px = px + pan; // add offest when panning
// noise() never returns 0 or 1, but some value in between and more likely a number in the middle
// use trim to adjust row distribution more equally across all shafts when mapping noise to shaft selection
float trim = 0.3;
float pNoise = noise(px/pZoom, py/pZoom); //0..1
if (pNoise < trim) {
pNoise = trim + 0.01;
} else if (pNoise > (1 - trim)) {
pNoise = 1 - trim - 0.01;
}
int selected = floor(map(pNoise, trim, (1-trim), 0, numItems));
return selected;
}
int[] switchShaft(int[] currentRow, int px, int py) {
int selectedShaft = perlinChoose(currentRow.length, px, py);
// delete selected shaft from current row
int[] newRow = deleteElement(currentRow, selectedShaft);
// choose new shaft
int newShaft = perlinChoose(numShafts, px, py) + 1;
// add new shaft to current row
newRow = append(newRow, newShaft);
return newRow;
}
RowData[] createDrawdown(RowData[] liftPlan, RowData[] threading) {
// uses threading and liftplan to make drawdown
RowData[] drawdown = new RowData[liftPlan.length];
for (int i = 0; i < liftPlan.length; i++) {
int[] rowLiftedWarps = new int[0];
for (int j = 0; j < liftPlan[i].positions.length; j++) {
// building drawdown by accessing warps lifted
int shaft = liftPlan[i].positions[j];
rowLiftedWarps = concat(rowLiftedWarps, threading[shaft - 1].positions);
}
// println("lifted warps on a row:");
// println(rowLiftedWarps);
drawdown[i] = new RowData(rowLiftedWarps);
if (liftPlan[i].glitched == true) {
drawdown[i].glitched = true;
}
}
return drawdown;
}
RowData[] createThreading(int[] threadingBase, int targetSize, int numShafts) {
int[] fullThread = fillArray(threadingBase, targetSize); // [1,2,3,4,1,2,3,4, etc...]
// index the position data for all the warp shaft connections
int[][] positions = new int[numShafts][0];
for (int i = 0; i < fullThread.length; i++) {
int currKey = fullThread[i];
if (positions[currKey - 1].length == 0) {
// not seen this key yet, add it
positions[currKey - 1] = new int[]{i + 1};
} else {
// update existing key
positions[currKey-1] = append(positions[currKey-1], i + 1);
}
}
RowData[] threading = new RowData[numShafts];
for (int i = 0; i < threading.length; i++) {
RowData threadRow = new RowData(positions[i]);
threading[i] = threadRow;
}
return threading;
}
RowData[] createTieUps(int numShafts) {
RowData[] tieUps = new RowData[numShafts];
// tieUps = {{1}, {2}, {3}, ...}
for (int i = 0; i < numShafts; i++) {
int[] shaft = {i + 1};
tieUps[i] = new RowData(shaft);
}
return tieUps;
}
void printDraft(RowData[] liftPlan, RowData[] drawdown, RowData[] threading, RowData[] tieUps) {
// visual output for draft
background(100); // dark grey
int padding = cellSize;
int liftPlanWidth = numShafts * cellSize;
int threadingHeight = numShafts * cellSize;
// print tie-ups
printSection(tieUps, "bottom-right", numShafts, liftPlanWidth+padding, threadingHeight);
// print threading
printSection(threading, "bottom-left", warpQuant, 3*padding+liftPlanWidth, threadingHeight);
// print lift plan
printSection(liftPlan, "top-right", numShafts, liftPlanWidth+padding, 2*padding+threadingHeight);
// print drawdown
printSection(drawdown, "top-left", warpQuant, 3*padding+liftPlanWidth, 2*padding+threadingHeight);
if (showLines == true) {
textFont(rowNum, 10);
textAlign(RIGHT);
int numX = cellSize + 7;
int numY = 7*cellSize - 2;
for (int i = weftQuant; i > 0; i--) {
if (i % 5 == 0) {
fill(255, 204, 255);
} else {
fill(255);
}
text(i, numX, numY);
numY = numY + cellSize;
}
}
}
void printSection(RowData[] sectionData, String mode, int numCols, int leftBuffer, int topBuffer) {
for (int row = 0; row < sectionData.length; row++) {
for (int col = 0; col < numCols; col++) {
boolean warpLifted = arrayContains(sectionData[row].positions, col + 1);
boolean weftGlitched = sectionData[row].glitched;
if (warpLifted == true) {
fill(0); // black cell
} else {
fill(255); // white cell
}
if (warpLifted == true && weftGlitched == true) {
fill(255, 51, 153); // pink cell
} else if (warpLifted == false && weftGlitched == true){
fill(255, 204, 255); // light pink cell
}
// draw rectangle
int pixelX = 0;
int pixelY = 0;
// mode defines which corner of the grid section to start printing from
switch(mode) {
case "bottom-right":
pixelX = leftBuffer - (col * cellSize);
pixelY = topBuffer - (row * cellSize);
break;
case "bottom-left":
pixelX = leftBuffer + (col * cellSize);
pixelY = topBuffer - (row * cellSize);
break;
case "top-right":
pixelX = leftBuffer - (col * cellSize);
pixelY = topBuffer + (row * cellSize);
break;
case "top-left":
pixelX = leftBuffer + (col * cellSize);
pixelY = topBuffer + (row * cellSize);
break;
}
rect(pixelX, pixelY, cellSize, cellSize);
}
}
}
//==== controls ====//
void keyPressed() {
if (key == 's') {
String filename = "drawdowns/drawdown-s" + seed + "-p" + pan + "-z" + pZoom + "-g" + glitchMod + ".svg";
beginRecord(SVG, filename);
printDraft(liftPlan, drawdown, threading, tieUps);
endRecord();
} else if (key == 'g') {
glitchMod++;
println("glitchMod: ", glitchMod);
loop();
} else if (key == 'd') {
glitchMod--;
println("glitchMod: ", glitchMod);
loop();
} else if (key == 'r') {
showLines = !showLines;
loop();
} else if (key == CODED) {
// Zoom and Pan the Perlin Field
if (keyCode == UP) {
pZoom = pZoom + cellSize ;
println("pZoom: ", pZoom);
loop();
} else if (keyCode == DOWN) {
pZoom = pZoom - cellSize ;
println("pZoom: ", pZoom);
loop();
} else if (keyCode == LEFT) {
pan = pan - cellSize ;
println("pan: ", pan);
loop();
}else if (keyCode == RIGHT) {
pan = pan + cellSize ;
println("pan: ", pan);
loop();
}
}
}
//==== helper functions ====//
Structure[] importJSONStructures(String filename) {
JSONArray structuresJSON = loadJSONArray(filename);
Structure[] structures = new Structure[structuresJSON.size()];
for (int i = 0; i < structuresJSON.size(); i++) {
// load structure
JSONObject structureJSON = structuresJSON.getJSONObject(i);
String name = structureJSON.getString("name");
int numShafts = structureJSON.getInt("numShafts");
int glitchSectionSize = structureJSON.getInt("glitchSectionSize");
// parse the shafts 2d int array
JSONArray shaftsJSON = structureJSON.getJSONArray("shafts");
int[][] shafts = new int[shaftsJSON.size()][0];
for (int j = 0; j < shaftsJSON.size(); j++) {
JSONArray shaftJSON = shaftsJSON.getJSONArray(j);
int[] shaft = new int[shaftJSON.size()];
for (int k = 0; k < shaftJSON.size(); k++) {
shaft[k] = shaftJSON.getInt(k);
}
shafts[j] = shaft;
}
JSONArray threadingJSON = structureJSON.getJSONArray("threading");
int[] threadingBase = new int[threadingJSON.size()];
for (int j = 0; j < threadingJSON.size(); j++) {
threadingBase[j] = threadingJSON.getInt(j);
}
Structure structure = new Structure(name, numShafts, shafts, threadingBase, glitchSectionSize);
structures[i] = structure;
}
return structures;
}
boolean arrayContains(int[] array, int check) {
// checks if the array contains an integer
for (int item : array) {
if (item == check) {
return true;
}
}
return false;
}
int[] deleteElement(int[] array, int skipIndex) {
// remove item at index position
int[] modifiedArray = new int[array.length - 1];
int j = 0;
for (int i = 0; i < array.length; i++) {
if (i == skipIndex) {
// skip
} else {
modifiedArray[j] = array[i];
j++;
}
}
return modifiedArray;
}
RowData[] addRow(RowData[] rows, RowData newRow) {
RowData[] appendedRows = new RowData[rows.length + 1];
for (int i = 0; i < rows.length; i++) {
appendedRows[i] = rows[i];
}
appendedRows[rows.length] = newRow;
return appendedRows;
}
int[] fillArray(int[] array, int targetSize) {
// duplicating array until it hits the target length
int loopQuant = ceil(float(targetSize) / float(array.length));
int[] filledArray = new int[0];
for (int i = 0; i < loopQuant; i++) {
for (int j = 0; j < array.length; j++) {
if (filledArray.length == targetSize) {
break;
} else {
filledArray = append(filledArray, array[j]);
}
}
}
return filledArray;
}
//==== custom classes ====//
class RowData {
int[] positions; // selected shafts or threads
int[] backupPositions;
boolean glitched;
// constructor
RowData(int[] positions_) {
positions = positions_;
backupPositions = new int[positions.length];
arrayCopy(positions, backupPositions);
glitched = false;
}
}
class Structure {
String name;
int numShafts;
int[][] shafts;
int[] threadingBase;
int glitchSectionSize;
// constructor
Structure(String name_, int numShafts_, int[][] shafts_, int[] threading_, int glitchSectionSize_) {
name = name_;
numShafts = numShafts_;
shafts = shafts_;
threadingBase = threading_;
glitchSectionSize = glitchSectionSize_;
}
}