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