sinesphere working with notes
[SugarCubes.git] / TestPatterns.pde
1 class BlankPattern extends SCPattern {
2 BlankPattern(GLucose glucose) {
3 super(glucose);
4 }
5
6 public void run(double deltaMs) {
7 setColors(#000000);
8 }
9 }
10
11 abstract class TestPattern extends SCPattern {
12 public TestPattern(GLucose glucose) {
13 super(glucose);
14 setEligible(false);
15 }
16 }
17
18 class TestSpeakerMapping extends TestPattern {
19 TestSpeakerMapping(GLucose glucose) {
20 super(glucose);
21 }
22
23 public void run(double deltaMs) {
24 int h = 0;
25 for (Speaker speaker : model.speakers) {
26 for (Strip strip : speaker.strips) {
27 float b = 100;
28 for (Point p : strip.points) {
29 colors[p.index] = lx.hsb(h % 360, 100, b);
30 b = max(0, b - 10);
31 }
32 h += 70;
33 }
34 }
35 }
36
37 }
38
39 class TestBassMapping extends TestPattern {
40 TestBassMapping(GLucose glucose) {
41 super(glucose);
42 }
43
44 public void run(double deltaMs) {
45 int[] strips = { 2, 1, 0, 3, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6 };
46 int h = 0;
47 for (int si : strips) {
48 float b = 100;
49 for (Point p : model.bassBox.strips.get(si).points) {
50 colors[p.index] = lx.hsb(h % 360, 100, b);
51 b = max(0, b - 10);
52 }
53 h += 70;
54 }
55 }
56 }
57
58 class TestFloorMapping extends TestPattern {
59 TestFloorMapping(GLucose glucose) {
60 super(glucose);
61 }
62
63 public void run(double deltaMs) {
64 int[] strutIndices = {6, 5, 4, 3, 2, 1, 0, 7};
65 int h = 0;
66 for (int si : strutIndices) {
67 float b = 100;
68 for (Point p : model.bassBox.struts.get(si).points) {
69 colors[p.index] = lx.hsb(h % 360, 100, b);
70 b = max(0, b - 10);
71 }
72 h += 50;
73 }
74 int[] floorIndices = {0, 1, 2, 3};
75 h = 0;
76 for (int fi : floorIndices) {
77 float b = 100;
78 for (Point p : model.boothFloor.strips.get(fi).points) {
79 colors[p.index] = lx.hsb(h, 100, b);
80 b = max(0, b - 3);
81 }
82 h += 90;
83 }
84 }
85 }
86
87 class TestPerformancePattern extends TestPattern {
88
89 final BasicParameter ops = new BasicParameter("OPS", 0);
90 final BasicParameter iter = new BasicParameter("ITER", 0);
91
92 TestPerformancePattern(GLucose glucose) {
93 super(glucose);
94 addParameter(ops);
95 addParameter(iter);
96 }
97
98 public void run(double deltaMs) {
99 float x = 1;
100 for (int j = 0; j < ops.getValuef() * 400000; ++j) {
101 x *= random(0, 1);
102 }
103
104 if (iter.getValuef() < 0.25) {
105 for (Point p : model.points) {
106 colors[p.index] = lx.hsb(
107 (p.x*.1 + p.y*.1) % 360,
108 100,
109 100
110 );
111 }
112 } else if (iter.getValuef() < 0.5) {
113 for (int i = 0; i < colors.length; ++i) {
114 colors[i] = lx.hsb(
115 (90 + model.px[i]*.1 + model.py[i]*.1) % 360,
116 100,
117 100
118 );
119 }
120 } else if (iter.getValuef() < 0.75) {
121 for (int i = 0; i < colors.length; ++i) {
122 colors[i] = lx.hsb(
123 (180 + model.p[3*i]*.1 + model.p[3*i+1]*.1) % 360,
124 100,
125 100
126 );
127 }
128 } else {
129 for (int i = 0; i < colors.length; ++i) {
130 colors[i] = lx.hsb(
131 (270 + model.x(i)*.1 + model.y(i)*.1) % 360,
132 100,
133 100
134 );
135 }
136 }
137 }
138 }
139
140 class TestStripPattern extends TestPattern {
141
142 SinLFO d = new SinLFO(4, 40, 4000);
143
144 public TestStripPattern(GLucose glucose) {
145 super(glucose);
146 addModulator(d).trigger();
147 }
148
149 public void run(double deltaMs) {
150 for (Strip s : model.strips) {
151 for (Point p : s.points) {
152 colors[p.index] = lx.hsb(
153 lx.getBaseHuef(),
154 100,
155 max(0, 100 - d.getValuef()*dist(p.x, p.y, s.cx, s.cy))
156 );
157 }
158 }
159 }
160 }
161
162 /**
163 * Simplest demonstration of using the rotating master hue.
164 * All pixels are full-on the same color.
165 */
166 class TestHuePattern extends TestPattern {
167 public TestHuePattern(GLucose glucose) {
168 super(glucose);
169 }
170
171 public void run(double deltaMs) {
172 // Access the core master hue via this method call
173 float hv = lx.getBaseHuef();
174 for (int i = 0; i < colors.length; ++i) {
175 colors[i] = lx.hsb(hv, 100, 100);
176 }
177 }
178 }
179
180 /**
181 * Test of a wave moving across the X axis.
182 */
183 class TestXPattern extends TestPattern {
184 private final SinLFO xPos = new SinLFO(0, model.xMax, 4000);
185 public TestXPattern(GLucose glucose) {
186 super(glucose);
187 addModulator(xPos).trigger();
188 }
189 public void run(double deltaMs) {
190 float hv = lx.getBaseHuef();
191 for (Point p : model.points) {
192 // This is a common technique for modulating brightness.
193 // You can use abs() to determine the distance between two
194 // values. The further away this point is from an exact
195 // point, the more we decrease its brightness
196 float bv = max(0, 100 - abs(p.x - xPos.getValuef()));
197 colors[p.index] = lx.hsb(hv, 100, bv);
198 }
199 }
200 }
201
202 /**
203 * Test of a wave on the Y axis.
204 */
205 class TestYPattern extends TestPattern {
206 private final SinLFO yPos = new SinLFO(0, model.yMax, 4000);
207 public TestYPattern(GLucose glucose) {
208 super(glucose);
209 addModulator(yPos).trigger();
210 }
211 public void run(double deltaMs) {
212 float hv = lx.getBaseHuef();
213 for (Point p : model.points) {
214 float bv = max(0, 100 - abs(p.y - yPos.getValuef()));
215 colors[p.index] = lx.hsb(hv, 100, bv);
216 }
217 }
218 }
219
220 /**
221 * Test of a wave on the Z axis.
222 */
223 class TestZPattern extends TestPattern {
224 private final SinLFO zPos = new SinLFO(0, model.zMax, 4000);
225 public TestZPattern(GLucose glucose) {
226 super(glucose);
227 addModulator(zPos).trigger();
228 }
229 public void run(double deltaMs) {
230 float hv = lx.getBaseHuef();
231 for (Point p : model.points) {
232 float bv = max(0, 100 - abs(p.z - zPos.getValuef()));
233 colors[p.index] = lx.hsb(hv, 100, bv);
234 }
235 }
236 }
237
238 /**
239 * This shows how to iterate over towers, enumerated in the model.
240 */
241 class TestTowerPattern extends TestPattern {
242 private final SawLFO towerIndex = new SawLFO(0, model.towers.size(), 1000*model.towers.size());
243
244 public TestTowerPattern(GLucose glucose) {
245 super(glucose);
246 addModulator(towerIndex).trigger();
247 }
248
249 public void run(double deltaMs) {
250 int ti = 0;
251 for (Tower t : model.towers) {
252 for (Point p : t.points) {
253 colors[p.index] = lx.hsb(
254 lx.getBaseHuef(),
255 100,
256 max(0, 100 - 80*LXUtils.wrapdistf(ti, towerIndex.getValuef(), model.towers.size()))
257 );
258 }
259 ++ti;
260 }
261 }
262
263 }
264
265 /**
266 * This is a demonstration of how to use the projection library. A projection
267 * creates a mutation of the coordinates of all the points in the model, creating
268 * virtual x,y,z coordinates. In effect, this is like virtually rotating the entire
269 * art car. However, since in reality the car does not move, the result is that
270 * it appears that the object we are drawing on the car is actually moving.
271 *
272 * Keep in mind that what we are creating a projection of is the view coordinates.
273 * Depending on your intuition, some operations may feel backwards. For instance,
274 * if you translate the view to the right, it will make it seem that the object
275 * you are drawing has moved to the left. If you scale the view up 2x, objects
276 * drawn with the same absolute values will seem to be half the size.
277 *
278 * If this feels counterintuitive at first, don't worry. Just remember that you
279 * are moving the pixels, not the structure. We're dealing with a finite set
280 * of sparse, non-uniformly spaced pixels. Mutating the structure would move
281 * things to a space where there are no pixels in 99% of the cases.
282 */
283 class TestProjectionPattern extends TestPattern {
284
285 private final Projection projection;
286 private final SawLFO angle = new SawLFO(0, TWO_PI, 9000);
287 private final SinLFO yPos = new SinLFO(-20, 40, 5000);
288
289 public TestProjectionPattern(GLucose glucose) {
290 super(glucose);
291 projection = new Projection(model);
292 addModulator(angle).trigger();
293 addModulator(yPos).trigger();
294 }
295
296 public void run(double deltaMs) {
297 // For the same reasons described above, it may logically feel to you that
298 // some of these operations are in reverse order. Again, just keep in mind that
299 // the car itself is what's moving, not the object
300 projection.reset(model)
301
302 // Translate so the center of the car is the origin, offset by yPos
303 .translateCenter(model, 0, yPos.getValuef(), 0)
304
305 // Rotate around the origin (now the center of the car) about an X-vector
306 .rotate(angle.getValuef(), 1, 0, 0)
307
308 // Scale up the Y axis (objects will look smaller in that access)
309 .scale(1, 1.5, 1);
310
311 float hv = lx.getBaseHuef();
312 for (Coord c : projection) {
313 float d = sqrt(c.x*c.x + c.y*c.y + c.z*c.z); // distance from origin
314 // d = abs(d-60) + max(0, abs(c.z) - 20); // life saver / ring thing
315 d = max(0, abs(c.y) - 10 + .1*abs(c.z) + .02*abs(c.x)); // plane / spear thing
316 colors[c.index] = lx.hsb(
317 (hv + .6*abs(c.x) + abs(c.z)) % 360,
318 100,
319 constrain(140 - 40*d, 0, 100)
320 );
321 }
322 }
323 }
324
325 class TestCubePattern extends TestPattern {
326
327 private SawLFO index = new SawLFO(0, Cube.POINTS_PER_CUBE, Cube.POINTS_PER_CUBE*60);
328
329 TestCubePattern(GLucose glucose) {
330 super(glucose);
331 addModulator(index).start();
332 }
333
334 public void run(double deltaMs) {
335 for (Cube c : model.cubes) {
336 int i = 0;
337 for (Point p : c.points) {
338 colors[p.index] = lx.hsb(
339 lx.getBaseHuef(),
340 100,
341 max(0, 100 - 80.*abs(i - index.getValuef()))
342 );
343 ++i;
344 }
345 }
346 }
347 }
348
349 class MappingTool extends TestPattern {
350
351 private int cubeIndex = 0;
352 private int stripIndex = 0;
353 private int channelIndex = 0;
354
355 public final int MAPPING_MODE_ALL = 0;
356 public final int MAPPING_MODE_CHANNEL = 1;
357 public final int MAPPING_MODE_SINGLE_CUBE = 2;
358 public int mappingMode = MAPPING_MODE_ALL;
359
360 public final int CUBE_MODE_ALL = 0;
361 public final int CUBE_MODE_SINGLE_STRIP = 1;
362 public final int CUBE_MODE_STRIP_PATTERN = 2;
363 public int cubeMode = CUBE_MODE_ALL;
364
365 public boolean channelModeRed = true;
366 public boolean channelModeGreen = false;
367 public boolean channelModeBlue = false;
368
369 private final int numChannels;
370
371 private final PandaMapping[] pandaMappings;
372 private PandaMapping activePanda;
373 private ChannelMapping activeChannel;
374
375 MappingTool(GLucose glucose, PandaMapping[] pandaMappings) {
376 super(glucose);
377 this.pandaMappings = pandaMappings;
378 numChannels = pandaMappings.length * PandaMapping.CHANNELS_PER_BOARD;
379 setChannel();
380 }
381
382 public int numChannels() {
383 return numChannels;
384 }
385
386 private void setChannel() {
387 activePanda = pandaMappings[channelIndex / PandaMapping.CHANNELS_PER_BOARD];
388 activeChannel = activePanda.channelList[channelIndex % PandaMapping.CHANNELS_PER_BOARD];
389 }
390
391 private int indexOfCubeInChannel(Cube c) {
392 if (activeChannel.mode == ChannelMapping.MODE_CUBES) {
393 int i = 1;
394 for (int index : activeChannel.objectIndices) {
395 if ((index >= 0) && (c == model.getCubeByRawIndex(index))) {
396 return i;
397 }
398 ++i;
399 }
400 }
401 return 0;
402 }
403
404 private void printInfo() {
405 println("Cube:" + cubeIndex + " Strip:" + (stripIndex+1));
406 }
407
408 public void cube(int delta) {
409 int len = model.cubes.size();
410 cubeIndex = (len + cubeIndex + delta) % len;
411 printInfo();
412 }
413
414 public void strip(int delta) {
415 int len = Cube.STRIPS_PER_CUBE;
416 stripIndex = (len + stripIndex + delta) % len;
417 printInfo();
418 }
419
420 public void run(double deltaMs) {
421 color off = #000000;
422 color c = off;
423 color r = #FF0000;
424 color g = #00FF00;
425 color b = #0000FF;
426 if (channelModeRed) c |= r;
427 if (channelModeGreen) c |= g;
428 if (channelModeBlue) c |= b;
429
430 int ci = 0;
431 for (Cube cube : model.cubes) {
432 boolean cubeOn = false;
433 int indexOfCubeInChannel = indexOfCubeInChannel(cube);
434 switch (mappingMode) {
435 case MAPPING_MODE_ALL: cubeOn = true; break;
436 case MAPPING_MODE_SINGLE_CUBE: cubeOn = (cubeIndex == ci); break;
437 case MAPPING_MODE_CHANNEL: cubeOn = (indexOfCubeInChannel > 0); break;
438 }
439 if (cubeOn) {
440 if (mappingMode == MAPPING_MODE_CHANNEL) {
441 color cc = off;
442 switch (indexOfCubeInChannel) {
443 case 1: cc = r; break;
444 case 2: cc = r|g; break;
445 case 3: cc = g; break;
446 case 4: cc = b; break;
447 case 5: cc = r|b; break;
448 }
449 setColor(cube, cc);
450 } else if (cubeMode == CUBE_MODE_STRIP_PATTERN) {
451 int si = 0;
452 color sc = off;
453 for (Strip strip : cube.strips) {
454 int faceI = si / Face.STRIPS_PER_FACE;
455 switch (faceI) {
456 case 0: sc = r; break;
457 case 1: sc = g; break;
458 case 2: sc = b; break;
459 case 3: sc = r|g|b; break;
460 }
461 if (si % Face.STRIPS_PER_FACE == 2) {
462 sc = r|g;
463 }
464 setColor(strip, sc);
465 ++si;
466 }
467 } else if (cubeMode == CUBE_MODE_SINGLE_STRIP) {
468 setColor(cube, off);
469 setColor(cube.strips.get(stripIndex), c);
470 } else {
471 setColor(cube, c);
472 }
473 } else {
474 setColor(cube, off);
475 }
476 ++ci;
477 }
478 }
479
480 public void setCube(int index) {
481 cubeIndex = index % model.cubes.size();
482 }
483
484 public void incCube() {
485 cubeIndex = (cubeIndex + 1) % model.cubes.size();
486 }
487
488 public void decCube() {
489 --cubeIndex;
490 if (cubeIndex < 0) {
491 cubeIndex += model.cubes.size();
492 }
493 }
494
495 public void setChannel(int index) {
496 channelIndex = index % numChannels;
497 setChannel();
498 }
499
500 public void incChannel() {
501 channelIndex = (channelIndex + 1) % numChannels;
502 setChannel();
503 }
504
505 public void decChannel() {
506 channelIndex = (channelIndex + numChannels - 1) % numChannels;
507 setChannel();
508 }
509
510 public void setStrip(int index) {
511 stripIndex = index % Cube.STRIPS_PER_CUBE;
512 }
513
514 public void incStrip() {
515 stripIndex = (stripIndex + 1) % Cube.STRIPS_PER_CUBE;
516 }
517
518 public void decStrip() {
519 stripIndex = (stripIndex + Cube.STRIPS_PER_CUBE - 1) % Cube.STRIPS_PER_CUBE;
520 }
521
522 public void keyPressed(UIMapping uiMapping) {
523 switch (keyCode) {
524 case UP: if (mappingMode == MAPPING_MODE_CHANNEL) incChannel(); else incCube(); break;
525 case DOWN: if (mappingMode == MAPPING_MODE_CHANNEL) decChannel(); else decCube(); break;
526 case LEFT: decStrip(); break;
527 case RIGHT: incStrip(); break;
528 }
529 switch (key) {
530 case 'r': channelModeRed = !channelModeRed; break;
531 case 'g': channelModeGreen = !channelModeGreen; break;
532 case 'b': channelModeBlue = !channelModeBlue; break;
533 }
534 uiMapping.setChannelID(channelIndex+1);
535 uiMapping.setCubeID(cubeIndex+1);
536 uiMapping.setStripID(stripIndex+1);
537 uiMapping.redraw();
538 }
539
540 }