Bug fixes and new mapping for sub
[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 != 11) exit();
79 // for (int i=0; i<NumBackTowers; 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 ArrayList<Cube> dcubes = new ArrayList<Cube>();
86 // for (int i=1; i<6; i++) {
87 // if (i>1) dcubes.add(new Cube(-6+CW*4/3*i , 0, 0, 0, 0, 0, WRR));
88 // dcubes.add(new Cube(-6+CW*4/3*i+CW*2/3., CH*.5, 0, 0, 0, 0, WRR));
89 // }
90
91 scubes.add(new StaggeredTower(//tower 1
92 0, // x
93 0 , // y
94 0 , // z
95 0, 3, new Cube.Wiring[]{ WFR, WRL, WFR}));
96 scubes.add(new StaggeredTower(// tower 2
97 22, // x
98 0 , // y
99 26 , // z
100 0, 3, new Cube.Wiring[]{ WRL, WFR, WRL}) );
101 scubes.add(new StaggeredTower(//tower 3
102 27, // x
103 0 , // y
104 64, // z
105 0, 4, new Cube.Wiring[]{ WFR, WRL, WRL, WFR}) );
106 scubes.add(new StaggeredTower(//tower 4
107 54, // x
108 7, // y
109 75.5 , // z
110 0, 4, new Cube.Wiring[]{ WFR, WRL, WFR, WRL}) );
111
112 scubes.add(new StaggeredTower(//tower 5
113 75.5, // x
114 0 , // y
115 100.5 , // z
116 0, 4, new Cube.Wiring[]{ WRR, WFL, WRR, WFL}) );
117
118 scubes.add(new StaggeredTower(//tower 6
119 93.5, // x
120 7 , // y
121 75.5, // z
122 0, 4, new Cube.Wiring[]{ WFR, WRL, WFR, WRL}) );
123 scubes.add(new StaggeredTower(// tower 7
124 119, // x
125 0 , // y
126 56.5, // z
127 0, 4, new Cube.Wiring[]{ WRL, WFR, WFR, WRL}) );
128
129 scubes.add(new StaggeredTower(//tower 8
130 136.5, // x
131 7 , // y
132 31.5 , // z
133 0, 4, new Cube.Wiring[]{ WFR, WRL, WFR, WRL}) );
134
135 scubes.add(new StaggeredTower(//tower 9
136 161.5, // x
137 0 , // y
138 20 , // z
139 0, 4, new Cube.Wiring[]{ WFR, WRL, WFR, WRL}) );
140 scubes.add(new StaggeredTower(//tower 10
141 176, // x
142 7 , // y
143 -6.5 , // z
144 0, 3, new Cube.Wiring[]{ WRL, WFR, WRL}) );
145 scubes.add(new StaggeredTower(// tower 11
146 202.5, // x
147 0 , // y
148 -26.5, // z
149 0, 3, new Cube.Wiring[]{ WRL, WFR, WRL}) );
150 scubes.add(new StaggeredTower(// tower 12 CENTER TOWER AT 45degrees
151 73, // x
152 0 , // y
153 58, // z
154 -45, 4, new Cube.Wiring[]{ WFR, WRL, WFR, WRL}) );
155 scubes.add(new StaggeredTower(// Single cube on top of tower 3
156 22, // x
157 81.5 , // y
158 39, // z
159 -45, 1, new Cube.Wiring[]{ WRL}) );
160 scubes.add(new StaggeredTower(// Single cube on top of tower 4
161 42, // x
162 112 , // y
163 72, // z
164 -10, 1, new Cube.Wiring[]{ WRL}) );
165
166
167
168
169
170
171
172 //////////////////////////////////////////////////////////////////////
173 // BENEATH HERE SHOULD NOT REQUIRE ANY MODIFICATION!!!! //
174 //////////////////////////////////////////////////////////////////////
175
176 // These guts just convert the shorthand mappings into usable objects
177 ArrayList<Tower> towerList = new ArrayList<Tower>();
178 ArrayList<Cube> tower;
179 Cube[] cubes = new Cube[100];
180 int cubeIndex = 1;
181 float px, pz, ny;
182 for (TowerMapping tm : towerCubes) {
183 px = tm.x;
184 ny = tm.y;
185 pz = tm.z;
186 tower = new ArrayList<Cube>();
187 for (CubeMapping cm : tm.cubeMappings) {
188 tower.add(cubes[cubeIndex++] = new Cube(px = px + cm.dx, ny, pz = pz + cm.dz, 0, cm.ry, 0, cm.wiring));
189 ny += Cube.EDGE_HEIGHT;
190 }
191 towerList.add(new Tower(tower));
192 }
193
194
195 for (Cube cube : singleCubes) cubes[cubeIndex++] = cube;
196 for (Cube cube : dcubes) cubes[cubeIndex++] = cube;
197 for (StaggeredTower st : scubes) {
198 tower = new ArrayList<Cube>();
199 for (int i=0; i < st.n; i++) {
200 Cube.Wiring w = (i < st.wiring.length) ? st.wiring[i] : WRR;
201 tower.add(cubes[cubeIndex++] = new Cube(st.x, st.y + CH* 4/3.*i, st.z, 0, st.r, 0, w));
202 }
203 towerList.add(new Tower(tower));
204 }
205
206 return new Model(towerList, cubes, bassBox, speakers);
207 }
208
209 /**
210 * This function maps the panda boards. We have an array of them, each has
211 * an IP address and a list of channels.
212 */
213 public PandaMapping[] buildPandaList() {
214 final int LEFT_SPEAKER = 0;
215 final int RIGHT_SPEAKER = 1;
216
217 // 8 channels map to: 3, 4, 7, 8, 13, 14, 15, 16.
218 return new PandaMapping[] {
219 new PandaMapping(
220 "10.200.1.28", new ChannelMapping[] {
221 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J3 *
222 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J4 //ORIG *
223 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 41, 42}), // 30 J7 *
224 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 43, 44}), // 30 J8 *
225 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 6, 3}), // 30 J13 (not working)
226 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 45, 46}), // 30 J14 (unplugged)
227 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 1, 2}), // 30 J15 (unplugged)
228 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 4, 5}), // 30 J16
229 }),
230 new PandaMapping(
231 "10.200.1.29", new ChannelMapping[] {
232 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J3 *
233 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J4 //ORIG *
234 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 7, 8}), // 30 J7 *
235 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 9, 10}), // 30 J8 *
236 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 11, 12}), // 30 J13 (not working)
237 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 13, 14}), // 30 J14 (unplugged)
238 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 15, 16}), // 30 J15 (unplugged)
239 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 17, 18}), // 30 J16
240 }),
241 new PandaMapping(
242 "10.200.1.30", new ChannelMapping[] {
243 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 38, 39}), // 30 J3 *
244 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J4 //ORIG *
245 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 23, 24}), // 30 J7 *
246 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 25, 26}), // 30 J8 *
247 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 19, 20}), // 30 J13 (not working)
248 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 21, 22}), // 30 J14 (unplugged)
249 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J15 (unplugged)
250 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J16
251
252 }),
253 new PandaMapping(
254 "10.200.1.31", new ChannelMapping[] {
255 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 37, 40}), // 30 J3 *
256 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J4 //ORIG *
257 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 27, 28}), // 30 J7 *
258 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 29, 30}), // 30 J8 *
259 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 31, 32}), // 30 J13 (not working)
260 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 33, 34}), // 30 J14 (unplugged)
261 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { 35, 36}), // 30 J15 (unplugged)
262 new ChannelMapping(ChannelMapping.MODE_CUBES, new int[] { }), // 30 J16 // 31 J16
263 }),
264 };
265 }
266
267 class TowerMapping {
268 public final float x, y, z;
269 public final CubeMapping[] cubeMappings;
270
271 TowerMapping(float x, float y, float z, CubeMapping[] cubeMappings) {
272 this.x = x;
273 this.y = y;
274 this.z = z;
275 this.cubeMappings = cubeMappings;
276 }
277 }
278
279 class CubeMapping {
280 public final float dx, dz, ry;
281 public final Cube.Wiring wiring;
282
283 CubeMapping(float dx, float dz, Cube.Wiring wiring) {
284 this(dx, dz, 0., wiring);
285 }
286 CubeMapping(float dx, float dz, float ry) {
287 this(dz, dz, ry, Cube.Wiring.FRONT_LEFT);
288 }
289
290 CubeMapping(float dx, float dz, float ry, Cube.Wiring wiring) {
291 this.dx = dx;
292 this.dz = dz;
293 this.ry = ry;
294 this.wiring = wiring;
295 }
296 }
297
298 class StaggeredTower {
299 public final float x, y, z, r;
300 public final int n;
301 public final Cube.Wiring[] wiring;
302 StaggeredTower(float _x, float _y, float _z, float _r, int _n) { this(_x, _y, _z, _r, _n, new Cube.Wiring[]{}); }
303 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;}
304 }
305
306 /**
307 * Each panda board has an IP address and a fixed number of channels. The channels
308 * each have a fixed number of pixels on them. Whether or not that many physical
309 * pixels are connected to the channel, we still send it that much data.
310 */
311 class PandaMapping {
312
313 // How many channels are on the panda board
314 public final static int CHANNELS_PER_BOARD = 8;
315
316 // How many total pixels on the whole board
317 public final static int PIXELS_PER_BOARD = ChannelMapping.PIXELS_PER_CHANNEL * CHANNELS_PER_BOARD;
318
319 final String ip;
320 final ChannelMapping[] channelList = new ChannelMapping[CHANNELS_PER_BOARD];
321
322 PandaMapping(String ip, ChannelMapping[] rawChannelList) {
323 this.ip = ip;
324
325 // Ensure our array is the right length and has all valid items in it
326 for (int i = 0; i < channelList.length; ++i) {
327 channelList[i] = (i < rawChannelList.length) ? rawChannelList[i] : new ChannelMapping();
328 if (channelList[i] == null) {
329 channelList[i] = new ChannelMapping();
330 }
331 }
332 }
333 }
334
335 /**
336 * Each channel on a pandaboard can be mapped in a number of modes. The typical is
337 * to a series of connected cubes, but we also have special mappings for the bass box,
338 * the speaker enclosures, and the DJ booth floor.
339 *
340 * This class is just the mapping meta-data. It sanitizes the input to make sure
341 * that the cubes and objects being referenced actually exist in the model.
342 *
343 * The logic for how to encode the pixels is contained in the PandaDriver.
344 */
345 class ChannelMapping {
346
347 // How many cubes per channel xc_PB is configured for
348 public final static int CUBES_PER_CHANNEL = 4;
349
350 // How many total pixels on each channel
351 public final static int PIXELS_PER_CHANNEL = Cube.POINTS_PER_CUBE * CUBES_PER_CHANNEL;
352
353 public static final int MODE_NULL = 0;
354 public static final int MODE_CUBES = 1;
355 public static final int MODE_BASS = 2;
356 public static final int MODE_SPEAKER = 3;
357 public static final int MODE_STRUTS_AND_FLOOR = 4;
358 public static final int MODE_INVALID = 5;
359
360 public static final int NO_OBJECT = -1;
361
362 final int mode;
363 final int[] objectIndices = new int[CUBES_PER_CHANNEL];
364
365 ChannelMapping() {
366 this(MODE_NULL);
367 }
368
369 ChannelMapping(int mode) {
370 this(mode, new int[]{});
371 }
372
373 ChannelMapping(int mode, int rawObjectIndex) {
374 this(mode, new int[]{ rawObjectIndex });
375 }
376
377 ChannelMapping(int mode, int[] rawObjectIndices) {
378 if (mode < 0 || mode >= MODE_INVALID) {
379 throw new RuntimeException("Invalid channel mapping mode: " + mode);
380 }
381 if (mode == MODE_SPEAKER) {
382 if (rawObjectIndices.length != 1) {
383 throw new RuntimeException("Speaker channel mapping mode must specify one speaker index");
384 }
385 int speakerIndex = rawObjectIndices[0];
386 if (speakerIndex < 0 || speakerIndex >= glucose.model.speakers.size()) {
387 throw new RuntimeException("Invalid speaker channel mapping: " + speakerIndex);
388 }
389 } else if ((mode == MODE_STRUTS_AND_FLOOR) || (mode == MODE_BASS) || (mode == MODE_NULL)) {
390 if (rawObjectIndices.length > 0) {
391 throw new RuntimeException("Bass/floor/null mappings cannot specify object indices");
392 }
393 } else if (mode == MODE_CUBES) {
394 for (int rawCubeIndex : rawObjectIndices) {
395 if (glucose.model.getCubeByRawIndex(rawCubeIndex) == null) {
396 throw new RuntimeException("Non-existing cube specified in cube mapping: " + rawCubeIndex);
397 }
398 }
399 }
400
401 this.mode = mode;
402 for (int i = 0; i < objectIndices.length; ++i) {
403 objectIndices[i] = (i < rawObjectIndices.length) ? rawObjectIndices[i] : NO_OBJECT;
404 }
405 }
406 }