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