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