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