New Grizzly code using LXOutput framework
[SugarCubes.git] / _Mappings.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * This file implements the mapping functions needed to lay out the physical
12 * cubes and the output ports on the panda board. It should only be modified
13 * when physical changes or tuning is being done to the structure.
14 */
15
16 final int MaxCubeHeight = 6;
17 final int NumBackTowers = 16;
18
19 public Model buildModel() {
20
21 // Shorthand helpers for specifying wiring more quickly
22 final Cube.Wiring WFL = Cube.Wiring.FRONT_LEFT;
23 final Cube.Wiring WFR = Cube.Wiring.FRONT_RIGHT;
24 final Cube.Wiring WRL = Cube.Wiring.REAR_LEFT;
25 final Cube.Wiring WRR = Cube.Wiring.REAR_RIGHT;
26
27 // Utility value if you need the height of a cube shorthand
28 final float CH = Cube.EDGE_HEIGHT;
29 final float CW = Cube.EDGE_WIDTH ;
30
31 // Positions for the bass box
32 final float BBY = BassBox.EDGE_HEIGHT + BoothFloor.PLEXI_WIDTH;
33 final float BBX = 56;
34 final float BBZ = 2;
35
36 // The model is represented as an array of towers. The cubes in the tower
37 // are represenented relatively. Each tower has an x, y, z reference position,
38 // which is typically the base cube's bottom left corner.
39 //
40 // Following that is an array of floats. A 2-d array contains an x-offset
41 // and a z-offset from the previous reference position. Typically the first cube
42 // will just be {0, 0}. Each successive cube uses the position of the previous
43 // cube as its reference.
44 //
45 // A 3-d array contains an x-offset, a z-offset, and a rotation about the
46 // y-axis.
47 //
48 // The cubes automatically increment their y-position by Cube.EDGE_HEIGHT.
49
50 // To-Do: (Mark Slee, Alex Green, or Ben Morrow): The Cube # is determined by the order in this list.
51 // "raw object index" is serialized by running through towermapping and then individual cube mapping below.
52 // We can do better than this. The raw object index should be obvious from the code-- looking through the
53 // rendered simulation and counting through cubes in mapping mode is grossly inefficient.
54
55 TowerMapping[] towerCubes = new TowerMapping[] {};
56
57 // Single cubes can be constructed directly here if you need them
58 Cube[] singleCubes = new Cube[] {
59 // new Cube(15, int( Cube.EDGE_HEIGHT), 39, 0, 10, 0, WRL), // Back left channel behind speaker
60 //new Cube(x, y, z, rx, ry, rz, wiring),
61 //new Cube(0,0,0,0,225,0, WRR),
62 };
63
64 // The bass box!
65 // BassBox bassBox = BassBox.unlitBassBox(BBX, 0, BBZ); // frame exists, no lights
66 BassBox bassBox = BassBox.noBassBox(); // no bass box at all
67 // BassBox bassBox = new BassBox(BBX, 0, BBZ); // bass box with lights
68
69 // The speakers!
70 List<Speaker> speakers = Arrays.asList(new Speaker[] {
71 // Each speaker parameter is x, y, z, rotation, the left speaker comes first
72 // new Speaker(TRAILER_WIDTH - Speaker.EDGE_WIDTH + 8, 6, 3, -15)
73 });
74
75
76 ////////////////////////////////////////////////////////////////////////
77 // dan's proposed lattice
78 ArrayList<StaggeredTower> scubes = new ArrayList<StaggeredTower>();
79 //if (NumBackTowers != 25) exit();
80 // for (int i=0; i<NumBackTowers/2; i++) scubes.add(new StaggeredTower(
81 // (i+1)*CW, // x
82 // (i % 2 == 0) ? 0 : CH * 2./3. , // y
83 // - ((i % 2 == 0) ? 11 : 0) + 80 , // z
84 // -45, (i % 2 == 0) ? MaxCubeHeight : MaxCubeHeight) ); // num cubes
85
86 // for (int i=0; i<NumBackTowers/2; i++) scubes.add(new StaggeredTower(
87 // (i+1)*CW, // x
88 // (i % 2 == 0) ? 0 : CH * 2./3. , // y
89 // - ((i % 2 == 0) ? 0 : 11) + 80 - pow(CH*CH + CW*CW, .5), // z
90 // 225, (i % 2 == 0) ? MaxCubeHeight : MaxCubeHeight-1) );
91
92 // for (int i=0; i<2 ; i++) scubes.add(new StaggeredTower(
93 // (i+1)*CW, // x
94 // 0 , // y
95 // - 0 + 97 - 2*pow(CH*CH + CW*CW, .5), // z
96 // 225, MaxCubeHeight ) );
97
98 ArrayList<Cube> dcubes = new ArrayList<Cube>();
99 // for (int i=1; i<6; i++) {
100 // if (i>1) dcubes.add(new Cube(-6+CW*4/3*i , 0, 0, 0, 0, 0, WRR));
101 // dcubes.add(new Cube(-6+CW*4/3*i+CW*2/3., CH*.5, 0, 0, 0, 0, WRR));
102 // }
103
104 float[] pos = new float[3];
105 pos[0] = 50;
106 pos[2] = 100;
107 scubes.add(new StaggeredTower(//tower 1
108 pos[0], // x
109 0 , // y
110 pos[2], // z
111 0, 4, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
112
113
114 pos[0] += 25;
115 pos[2] -= 10;
116 scubes.add(new StaggeredTower(// tower 2
117 pos[0], // x
118 15 , // y
119 pos[2], // z
120 0, 4, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
121
122 pos[0] += 25;
123 pos[2] += -12.5;
124 scubes.add(new StaggeredTower(//tower 3
125 pos[0], // x
126 0 , // y
127 pos[2], // z
128 0, 5, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
129
130 pos[0] += -32.75;
131 pos[2] += -13;
132 scubes.add(new StaggeredTower(//tower 4
133 pos[0], // x
134 0, // y
135 pos[2], // z
136 0, 6, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
137
138 pos[0] += 26;
139 pos[2] += -16;
140 scubes.add(new StaggeredTower(//tower 5
141 pos[0], // x
142 15 , // y
143 pos[2], // z
144 0, 6, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
145
146 pos[0] += 26;
147 pos[2] += -4.5;
148 scubes.add(new StaggeredTower(//tower 6
149 pos[0], // x
150 0 , // y
151 pos[2], // z
152 0, 6, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
153
154 pos[0] += -56.5;
155 pos[2] += -6.5;
156 scubes.add(new StaggeredTower(// tower 7
157 pos[0], // x
158 15 , // y
159 pos[2], // z
160 0, 4, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
161
162 pos[0] += 26;
163 pos[2] += -16.5;
164 scubes.add(new StaggeredTower(//tower 8
165 pos[0], // x
166 0 , // y
167 pos[2], // z
168 0, 5, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
169
170 pos[0] += 27;
171 pos[2] += -4.5;
172 scubes.add(new StaggeredTower(//tower 9
173 pos[0], // x
174 15 , // y
175 pos[2], // z
176 0, 5, new Cube.Wiring[] { WRR, WRR, WRR, WRR, WRR, WRR}) );
177
178 // //TOWERS ON DANCE FLOOR
179 // scubes.add(new StaggeredTower(//tower 10
180 // 83.75+39+43-124.5, // x
181 // 0, // y
182 // -47.5-43, // z
183 // 45, 4, new Cube.Wiring[]{ WRR, WRR, WRR, WRR}) );
184 // scubes.add(new StaggeredTower(//tower 11
185 // 83.75, // x
186 // 0, // y
187 // -47.5, // z
188 // 45, 4, new Cube.Wiring[]{ WRR, WRR, WRR, WRR}) );
189 // scubes.add(new StaggeredTower(//tower 12
190 // 83.75+39, // x
191 // 0, // y
192 // -47.5, // z
193 // 45, 4, new Cube.Wiring[]{ WRR, WRR, WRR, WRR}) );
194 // scubes.add(new StaggeredTower(//tower 13
195 // 83.75+39+43, // x
196 // 0, // y
197 // -47.5-43, // z
198 // 45, 4, new Cube.Wiring[]{ WRR, WRR, WRR, WRR}) );
199
200 // scubes.add(new StaggeredTower(// Single cube on top of tower 4
201 // 42, // x
202 // 112 , // y
203 // 72, // z
204 // -10, 1, new Cube.Wiring[]{ WRL}) );
205
206
207
208
209
210
211
212 //////////////////////////////////////////////////////////////////////
213 // BENEATH HERE SHOULD NOT REQUIRE ANY MODIFICATION!!!! //
214 //////////////////////////////////////////////////////////////////////
215
216 // These guts just convert the shorthand mappings into usable objects
217 ArrayList<Tower> towerList = new ArrayList<Tower>();
218 ArrayList<Cube> tower;
219 Cube[] cubes = new Cube[200];
220 int cubeIndex = 1;
221 float px, pz, ny;
222 for (TowerMapping tm : towerCubes) {
223 px = tm.x;
224 ny = tm.y;
225 pz = tm.z;
226 tower = new ArrayList<Cube>();
227 for (CubeMapping cm : tm.cubeMappings) {
228 tower.add(cubes[cubeIndex++] = new Cube(px = px + cm.dx, ny, pz = pz + cm.dz, 0, cm.ry, 0, cm.wiring));
229 ny += Cube.EDGE_HEIGHT;
230 }
231 towerList.add(new Tower(tower));
232 }
233
234
235 for (Cube cube : singleCubes) {
236 cubes[cubeIndex++] = cube;
237 }
238 for (Cube cube : dcubes) {
239 cubes[cubeIndex++] = cube;
240 }
241 for (StaggeredTower st : scubes) {
242 tower = new ArrayList<Cube>();
243 for (int i=0; i < st.n; i++) {
244 Cube.Wiring w = (i < st.wiring.length) ? st.wiring[i] : WRR;
245 tower.add(cubes[cubeIndex++] = new Cube(st.x, st.y + CH* 4/3.*i, st.z, 0, st.r, 0, w));
246 }
247 towerList.add(new Tower(tower));
248 }
249
250 return new Model(towerList, cubes, bassBox, speakers);
251 }
252
253 /**
254 * This function maps the panda boards. We have an array of them, each has
255 * an IP address and a list of channels.
256 */
257 public PandaMapping[] buildPandaList() {
258 final int LEFT_SPEAKER = 0;
259 final int RIGHT_SPEAKER = 1;
260
261 // 8 channels map to: 3, 4, 7, 8, 13, 14, 15, 16.
262 return new PandaMapping[] {
263 new PandaMapping(
264 "192.168.88.100", new ChannelMapping[] { // G1
265 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 6}),
266 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 5}),
267 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 6}),
268 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 7}),
269 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 7}),
270 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 8}),
271 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
272 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 2}),
273
274 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 4}),
275 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 3}),
276 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 11}),
277 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 10}),
278 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 9}),
279 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 9}),
280 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 12}),
281 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 13}),
282 }),
283
284 new PandaMapping(
285 "192.168.88.101", new ChannelMapping[] { //G4
286 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 25}),
287 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 23}),
288 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 24}),
289 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 43}),
290 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 45}),
291 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 44}),
292 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
293 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
294
295 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
296 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
297 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
298 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 41}),
299 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 42}),
300 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 21}),
301 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 20}),
302 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 22}),
303 }),
304
305 new PandaMapping(
306 "192.168.88.104", new ChannelMapping[] { // G3
307 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 26}),
308 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 28}),
309 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 27}),
310 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 19}),
311 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 18}),
312 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 17}),
313 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
314 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
315
316 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 18}),
317 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 19}),
318 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 15}),
319 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 16}),
320 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 14}),
321 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 29}),
322 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 30}),
323 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 31}),
324 }),
325
326 new PandaMapping(
327 "192.168.88.105", new ChannelMapping[] { // G2
328 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
329 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
330 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
331 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 39}),
332 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 38}),
333 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 40}),
334 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 34}),
335 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 35}),
336
337 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 33}),
338 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 32}),
339 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 37}),
340 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 37}),
341 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
342 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
343 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
344 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1}),
345 }),
346 };
347 }
348
349 class TowerMapping {
350 public final float x, y, z;
351 public final CubeMapping[] cubeMappings;
352
353 TowerMapping(float x, float y, float z, CubeMapping[] cubeMappings) {
354 this.x = x;
355 this.y = y;
356 this.z = z;
357 this.cubeMappings = cubeMappings;
358 }
359 }
360
361 class CubeMapping {
362 public final float dx, dz, ry;
363 public final Cube.Wiring wiring;
364
365 CubeMapping(float dx, float dz, Cube.Wiring wiring) {
366 this(dx, dz, 0., wiring);
367 }
368 CubeMapping(float dx, float dz, float ry) {
369 this(dz, dz, ry, Cube.Wiring.FRONT_LEFT);
370 }
371
372 CubeMapping(float dx, float dz, float ry, Cube.Wiring wiring) {
373 this.dx = dx;
374 this.dz = dz;
375 this.ry = ry;
376 this.wiring = wiring;
377 }
378 }
379
380 class StaggeredTower {
381 public final float x, y, z, r;
382 public final int n;
383 public final Cube.Wiring[] wiring;
384 StaggeredTower(float _x, float _y, float _z, float _r, int _n) { this(_x, _y, _z, _r, _n, new Cube.Wiring[]{}); }
385 StaggeredTower(float _x, float _y, float _z, float _r, int _n, Cube.Wiring[] _wiring) { x=_x; y=_y; z=_z; r=_r; n=_n; wiring=_wiring;}
386 }
387
388 /**
389 * Each panda board has an IP address and a fixed number of channels. The channels
390 * each have a fixed number of pixels on them. Whether or not that many physical
391 * pixels are connected to the channel, we still send it that much data.
392 */
393 class PandaMapping {
394
395 // How many channels are on the panda board
396 public final static int CHANNELS_PER_BOARD = 16;
397
398 // How many total pixels on the whole board
399 public final static int PIXELS_PER_BOARD = ChannelMapping.PIXELS_PER_CHANNEL * CHANNELS_PER_BOARD;
400
401 final String ip;
402 final ChannelMapping[] channelList = new ChannelMapping[CHANNELS_PER_BOARD];
403
404 PandaMapping(String ip, ChannelMapping[] rawChannelList) {
405 this.ip = ip;
406
407 // Ensure our array is the right length and has all valid items in it
408 for (int i = 0; i < channelList.length; ++i) {
409 channelList[i] = (i < rawChannelList.length) ? rawChannelList[i] : new ChannelMapping();
410 if (channelList[i] == null) {
411 channelList[i] = new ChannelMapping();
412 }
413 }
414 }
415 }
416
417 /**
418 * Each channel on a pandaboard can be mapped in a number of modes. The typical is
419 * to a series of connected cubes, but we also have special mappings for the bass box,
420 * the speaker enclosures, and the DJ booth floor.
421 *
422 * This class is just the mapping meta-data. It sanitizes the input to make sure
423 * that the cubes and objects being referenced actually exist in the model.
424 *
425 * The logic for how to encode the pixels is contained in the PandaDriver.
426 */
427 class ChannelMapping {
428
429 // How many cubes per channel xc_PB is configured for
430 public final static int CUBES_PER_CHANNEL = 1;
431
432 // How many total pixels on each channel
433 public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL;
434
435 public static final int MODE_NULL = 0;
436 public static final int MODE_CUBES = 1;
437 public static final int MODE_BASS = 2;
438 public static final int MODE_SPEAKER = 3;
439 public static final int MODE_STRUTS_AND_FLOOR = 4;
440 public static final int MODE_INVALID = 5;
441
442 public static final int NO_OBJECT = -1;
443
444 final int mode;
445 final int[] objectIndices = new int[CUBES_PER_CHANNEL];
446
447 ChannelMapping() {
448 this(MODE_NULL);
449 }
450
451 ChannelMapping(int mode) {
452 this(mode, new int[]{});
453 }
454
455 ChannelMapping(int mode, int rawObjectIndex) {
456 this(mode, new int[]{ rawObjectIndex });
457 }
458
459 ChannelMapping(int mode, int[] rawObjectIndices) {
460 if (mode < 0 || mode >= MODE_INVALID) {
461 throw new RuntimeException("Invalid channel mapping mode: " + mode);
462 }
463 if (mode == MODE_SPEAKER) {
464 if (rawObjectIndices.length != 1) {
465 throw new RuntimeException("Speaker channel mapping mode must specify one speaker index");
466 }
467 int speakerIndex = rawObjectIndices[0];
468 if (speakerIndex < 0 || speakerIndex >= glucose.model.speakers.size()) {
469 throw new RuntimeException("Invalid speaker channel mapping: " + speakerIndex);
470 }
471 } else if ((mode == MODE_STRUTS_AND_FLOOR) || (mode == MODE_BASS) || (mode == MODE_NULL)) {
472 if (rawObjectIndices.length > 0) {
473 throw new RuntimeException("Bass/floor/null mappings cannot specify object indices");
474 }
475 } else if (mode == MODE_CUBES) {
476 for (int rawCubeIndex : rawObjectIndices) {
477 if (glucose.model.getCubeByRawIndex(rawCubeIndex) == null) {
478 throw new RuntimeException("Non-existing cube specified in cube mapping: " + rawCubeIndex);
479 }
480 }
481 }
482
483 this.mode = mode;
484 for (int i = 0; i < objectIndices.length; ++i) {
485 objectIndices[i] = (i < rawObjectIndices.length) ? rawObjectIndices[i] : NO_OBJECT;
486 }
487 }
488 }