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