modified mappings to render more cubes, modified internals
[SugarCubes.git] / _Mappings.pde
CommitLineData
1ecdb44a
MS
1/**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
186bc4d3 11 * This file implements the mapping functions needed to lay out the physical
1ecdb44a
MS
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 */
45f43cc2 15
f0cc0ba5
MS
16public Model buildModel() {
17
f0cc0ba5
MS
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;
95c50032 23
a3ccf23a 24 // Utility value if you need the height of a cube shorthand
95c50032 25 final float CH = Cube.EDGE_HEIGHT;
73687629 26 final float CW = Cube.EDGE_WIDTH ;
7c7625ec
AG
27
28
ae579223 29
a3ccf23a 30 // Positions for the bass box
ae579223
MS
31 final float BBY = BassBox.EDGE_HEIGHT + BoothFloor.PLEXI_WIDTH;
32 final float BBX = 56;
33 final float BBZ = 2;
34
46fc29d4
MS
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
f0cc0ba5
MS
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.
46fc29d4
MS
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.
e27a8652 48
73687629 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.
e28f168c 53
73687629 54 TowerMapping[] towerCubes = new TowerMapping[] {};
f0cc0ba5
MS
55
56 // Single cubes can be constructed directly here if you need them
57 Cube[] singleCubes = new Cube[] {
bf1fe2d4 58 // new Cube(15, int( Cube.EDGE_HEIGHT), 39, 0, 10, 0, WRL), // Back left channel behind speaker
f1370a0b 59 //new Cube(x, y, z, rx, ry, rz, wiring),
bf1fe2d4 60 //new Cube(0,0,0,0,225,0, WRR),
f0cc0ba5
MS
61 };
62
63 // The bass box!
73687629 64 // BassBox bassBox = BassBox.unlitBassBox(BBX, 0, BBZ); // frame exists, no lights
65 BassBox bassBox = BassBox.noBassBox(); // no bass box at all
e89eda9f 66 // BassBox bassBox = new BassBox(BBX, 0, BBZ); // bass box with lights
e28f168c 67
f0cc0ba5 68 // The speakers!
3b2e0b4a
MS
69 List<Speaker> speakers = Arrays.asList(new Speaker[] {
70 // Each speaker parameter is x, y, z, rotation, the left speaker comes first
73687629 71 // new Speaker(TRAILER_WIDTH - Speaker.EDGE_WIDTH + 8, 6, 3, -15)
3b2e0b4a 72 });
f0cc0ba5 73
bf1fe2d4
MS
74
75 ////////////////////////////////////////////////////////////////////////
76 // dan's proposed lattice
77 ArrayList<StaggeredTower> scubes = new ArrayList<StaggeredTower>();
3fa45e9e
AG
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
bf1fe2d4 84
3fa45e9e
AG
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) );
bf1fe2d4
MS
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
a981ea46 96float current_x_position = 0;
3fa45e9e
AG
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}) );
d93dc84a 173
a981ea46
AK
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}) );
bf1fe2d4
MS
179
180
181
182
183
184
185
a3ccf23a
MS
186 //////////////////////////////////////////////////////////////////////
187 // BENEATH HERE SHOULD NOT REQUIRE ANY MODIFICATION!!!! //
188 //////////////////////////////////////////////////////////////////////
189
f0cc0ba5 190 // These guts just convert the shorthand mappings into usable objects
46fc29d4
MS
191 ArrayList<Tower> towerList = new ArrayList<Tower>();
192 ArrayList<Cube> tower;
3fa45e9e 193 Cube[] cubes = new Cube[200];
46fc29d4 194 int cubeIndex = 1;
f0cc0ba5
MS
195 float px, pz, ny;
196 for (TowerMapping tm : towerCubes) {
197 px = tm.x;
198 ny = tm.y;
199 pz = tm.z;
46fc29d4 200 tower = new ArrayList<Cube>();
f0cc0ba5
MS
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;
46fc29d4
MS
204 }
205 towerList.add(new Tower(tower));
206 }
73687629 207
f1370a0b
AG
208
209 for (Cube cube : singleCubes) cubes[cubeIndex++] = cube;
bf1fe2d4 210 for (Cube cube : dcubes) cubes[cubeIndex++] = cube;
f1370a0b 211for (StaggeredTower st : scubes) {
73687629 212 tower = new ArrayList<Cube>();
bf1fe2d4
MS
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 }
73687629 217 towerList.add(new Tower(tower));
f0cc0ba5 218 }
e76480d4 219
7dceead9 220 return new Model(towerList, cubes, bassBox, speakers);
186bc4d3 221}
e73ef85d 222
a3ccf23a
MS
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 */
186bc4d3 227public PandaMapping[] buildPandaList() {
1d75c8a9
MS
228 final int LEFT_SPEAKER = 0;
229 final int RIGHT_SPEAKER = 1;
230
3b2e0b4a 231 // 8 channels map to: 3, 4, 7, 8, 13, 14, 15, 16.
186bc4d3 232 return new PandaMapping[] {
bf1fe2d4
MS
233 new PandaMapping(
234 "10.200.1.28", new ChannelMapping[] {
a981ea46
AK
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[] { }),
bf1fe2d4
MS
243 }),
244 new PandaMapping(
245 "10.200.1.29", new ChannelMapping[] {
a981ea46
AK
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[] { }),
bf1fe2d4 254 }),
7c7625ec 255 new PandaMapping(
41c436e4 256 "10.200.1.30", new ChannelMapping[] {
a981ea46 257 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J3 *
bf1fe2d4 258 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J4 //ORIG *
a981ea46
AK
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
73687629 265 }),
a981ea46
AK
266 new PandaMapping(
267 "10.200.1.31", new ChannelMapping[] {
11c6a1c8 268 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 65, 66}), // J3
a981ea46
AK
269 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // J4
270 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 55, 56}), // 30 J7
d93dc84a 271 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 57, 58}), // J8
a981ea46
AK
272 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 59, 60}), // J13
273 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 61, 62}), // 30 J14
d93dc84a 274 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 63, 64}), // J15
11c6a1c8 275 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // J16
d93dc84a
AK
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
a981ea46
AK
285 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // J15
286 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // J16
287 }),
3b2e0b4a 288 };
45f43cc2 289}
e73ef85d 290
f0cc0ba5
MS
291class 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
303class CubeMapping {
304 public final float dx, dz, ry;
305 public final Cube.Wiring wiring;
306
307 CubeMapping(float dx, float dz, Cube.Wiring wiring) {
73687629 308 this(dx, dz, 0., wiring);
f0cc0ba5 309 }
f0cc0ba5
MS
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
73687629 322class StaggeredTower {
323 public final float x, y, z, r;
324 public final int n;
bf1fe2d4
MS
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;}
73687629 328}
329
a922e963
MS
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 */
45f43cc2
MS
335class PandaMapping {
336
045b432d 337 // How many channels are on the panda board
e28f168c 338 public final static int CHANNELS_PER_BOARD = 8;
045b432d 339
44b8de9c 340 // How many total pixels on the whole board
84086fa3 341 public final static int PIXELS_PER_BOARD = ChannelMapping.PIXELS_PER_CHANNEL * CHANNELS_PER_BOARD;
44b8de9c 342
45f43cc2 343 final String ip;
84086fa3 344 final ChannelMapping[] channelList = new ChannelMapping[CHANNELS_PER_BOARD];
45f43cc2 345
84086fa3 346 PandaMapping(String ip, ChannelMapping[] rawChannelList) {
45f43cc2 347 this.ip = ip;
a922e963
MS
348
349 // Ensure our array is the right length and has all valid items in it
84086fa3
MS
350 for (int i = 0; i < channelList.length; ++i) {
351 channelList[i] = (i < rawChannelList.length) ? rawChannelList[i] : new ChannelMapping();
a922e963
MS
352 if (channelList[i] == null) {
353 channelList[i] = new ChannelMapping();
354 }
045b432d 355 }
e73ef85d 356 }
1ecdb44a
MS
357}
358
a922e963 359/**
e27a8652 360 * Each channel on a pandaboard can be mapped in a number of modes. The typical is
a922e963
MS
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 */
84086fa3
MS
369class ChannelMapping {
370
371 // How many cubes per channel xc_PB is configured for
372 public final static int CUBES_PER_CHANNEL = 4;
45f43cc2 373
84086fa3
MS
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;
1d75c8a9 381 public static final int MODE_STRUTS_AND_FLOOR = 4;
84086fa3
MS
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()) {
3b2e0b4a 411 throw new RuntimeException("Invalid speaker channel mapping: " + speakerIndex);
84086fa3 412 }
1d75c8a9 413 } else if ((mode == MODE_STRUTS_AND_FLOOR) || (mode == MODE_BASS) || (mode == MODE_NULL)) {
84086fa3 414 if (rawObjectIndices.length > 0) {
3b2e0b4a 415 throw new RuntimeException("Bass/floor/null mappings cannot specify object indices");
84086fa3
MS
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 }
a981ea46 430}