-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLXGraphModel.pde
More file actions
424 lines (346 loc) · 12.7 KB
/
LXGraphModel.pde
File metadata and controls
424 lines (346 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import java.io.*;
import java.nio.file.*;
import java.util.*;
//import heronarts.lx.LX;
//import heronarts.lx.LXModel;
/** ******************************************************************** MODEL
* This is the model for the whole brain. It contains four mappings, two of
* which users should use (Bar and Node) and two which are set up to deal
* with the physical reality of the actual brain, double bars and double
* nodes and so on.
*
* SUBCLASSES
* class Node
* class Bar
*
* FIELDS
* Nodes
* Bars
* ChannelMap
* Points
*
*
*
* INITIALIZATION
* setChannelMap()
* initialize()
* import_model()
*
*
*
* DATA ACCESS
* // many variations of these
* getNode()
* getBar()
* getPoint()
*
*
*
*
*
* @author Alex Maki-Jokela
* @author Alexander D. Scouras
************************************************************************* **/
public class LXGraphModel extends LXModel {
//********************************************************************* NODE
/**
* Connection point for bars, usually virtual except to define bars themselves.
* Name, coordinates, and other properties.
* Adjacent bars and nodes.
* Pixel mapping, for effects at nodes?
*********************************************************************** **/
public class Node extends LXModel {
public final String name;
public final float x, y, z;
public final List<String> tags;
public final PVector xyz;
//public final List<String> properties = new ArrayList<String>();
//public final List<Node> adjacent_nodes = new ArrayList<Node>();
//public final List<Bar> adjacent_bars = new ArrayList<Bar>();
/** Constructor
*/
public Node(String name, float x, float y, float z, String[] tags) {
this.name = name;
this.x = x;
this.y = y;
this.z = z;
this.tags = new ArrayList<String>(Arrays.asList(tags));
this.xyz = new PVector(x, y, z);
}
public Node(String name, float x, float y, float z, String tags) {
this(name, x, y, z, tags.trim().split("\\s+"));
}
}
//********************************************************************** BAR
/**
* Edges between nodes with strips of LEDs along their length.
* Name, nodes, direction, and other properties.
* Adjacent bars.
* Pixel map, reversed when traversing opposite direction.
*********************************************************************** **/
public class Bar extends LXModel {
public final String name;
public final Node node1;
public final Node node2;
public final int channel;
public final List<String> tags;
public final boolean reversed;
public final Bar pair;
//private LXPoint[] points;
private float pixel_density = null;
private float pixel_buffer = null;
private String pixel_layout = null;
//public final List<Bar> adjacent_bars = new ArrayList<Bar>();
/**
* Constructor
*/
public Bar(Node node1, Node node2, int channel, String[] tags,
float density, float buffer, String layout) {
this.name = node1.name + "-" + node2.name;
this.node1 = node1;
this.node2 = node2;
this.channel = channel;
this.tags = new ArrayList<String>(Arrays.asList(tags));
this.reversed = false;
this.pixel_buffer = buffer;
this.pixel_density = density;
this.pixel_layout = layout;
}
public Bar(Node node1, Node node2, int channel, String tags,
float density, float buffer, String layout) {
//String[] tag_list = tags.trim().split("\\s+");
//this(node1, node2, tag_list, rev, density, buffer, layout);
this(node1, node2, channel, tags.trim().split("\\s+"),
rev, density, buffer, layout);
}
public Bar(Node node1, Node node2, int channel, String tags) {
this(node1, node2, channel, tags, rev, none, none, none);
}
/**
* Create the same bar in the opposite direction for directed traversals.
*/
private Bar reverse() {
Bar rev = new Bar(this.node2, this.node1, this.channel, this.tags,
this.density, this.buffer, this.layout);
rev.rev = not this.rev;
rev.strip = this.strip;
rev.points = Collections.reverse(this.points);
rev.pair = this;
this.pair = rev;
return rev;
}
}
//***************************************************** FIELDS AND CONSTANTS
//-------------- Constants
String DIR_MAPS = "models";
String FILE_PARAMS = "params.csv";
String FILE_NODES = "nodes.csv";
String FILE_BARS = "bars.csv";
String FILE_PIXELS = "pixels.csv";
//-------------- Fields
//Note that these are stored in maps, not lists.
//Nodes are keyed by their three letter name ("LAB", "YAK", etc)
//Bars are keyed by the two associated nodes in alphabetical order ("LAB-YAK", etc)
public final TreeMap<String, Node> nodes;
public final TreeMap<String, TreeMap<String, Bar>> bars;
/*
public ArrayList<Node> nodes = new ArrayList<Node>();
public ArrayList<Bars> bars = new ArrayList<Bar>();
public ArrayList<Pixels> pixels = new ArrayList<LXPoint>();
*/
/* Automatic pixel layouts are based on model parameters.
* TODO: I suspect using anything besides center is stupid
* TODO: Brainlove has a custom mapping based on binned widths to minimize
* number of unique strip lengths. I don't intend to support that, it
* will require a custom mapping.
* TODO: This does not necessarily give intelligent LED strip layout/maps
* that you would send to a microcontroller.
* Center: Fit as many as possible, given density and buffer, and center
* between the nodes
* Fill: Fit as many as possible, given density and buffer, and start
* filling in from the start node + buffer width
*/
public String pixel_layout = "center";
public float pixel_density = 60.0;
public float pixel_buffer = 0.1;
//============== Physical Hardware Limitations
// TODO: These and other parameters should probably be globals instead
// TODO: This is only here so it compiles temporarily.
public ArrayList<int[]> channelMap;
public final int max_pixels_per_channel = 500;
public int channels = 0;
public int strips = 0;
private int pixels_in_channel = 0;
private int pixels_in_model = 0;
/** ************************************************************ Constructor
*
*********************************************************************** **/
public LXGraphModel (String model_name) {
logTime("-- loading model " + model_name);
this.nodes = new TreeMap<String, Node>();
this.bars = new TreeMap<String, TreeMap<String, Bar>>();
String path_params = DIR_MAPS + "/" + model_name + "/" + FILE_PARAMS;
String path_nodes = DIR_MAPS + "/" + model_name + "/" + FILE_NODES;
String path_bars = DIR_MAPS + "/" + model_name + "/" + FILE_BARS;
String path_pixels = DIR_MAPS + "/" + model_name + "/" + FILE_PIXELS;
this.load_params(path_params);
logTime("---- loaded model parameters");
this.load_nodes(path_nodes);
logTime("---- loaded nodes");
this.load_bars(path_bars);
logTime("---- loaded bars");
this.load_pixels(path_pixels);
logTime("---- loaded pixels");
this.initialize_pixels();
}
// ========================================================= Load Data Files
// ----------- Load Parameters
// Custom layout variables, like physical constraints for pixels per inch,
// borders around nodes, etc.
private void load_params(String path) {
File f = new File(path);
if (!f.exists()) {
return;
}
Table table_params = loadTable(path, "header,tsv");
}
// ----------- Load Nodes
private void load_nodes(String path) {
Table table_nodes = loadTable(path, "header,tsv");
for (processing.data.TableRow row : table_nodes.rows()) {
Node node = new Node (
row.getString("Node"),
row.getFloat("X"),
row.getFloat("Y"),
row.getFloat("Z"),
row.getString("Tags")
);
this.add_node(node);
}
}
// ----------- Load Bars
private void load_bars(String path) {
PVector xyz1, xyz2;
PVector dir;
Table table_bars = loadTable(path, "header,tsv");
for (processing.data.TableRow row : table_bars.rows()) {
Node node1 = this.nodes.get(row.getString("Node1"));
Node node2 = this.nodes.get(row.getString("Node2"));
Bar bar = new Bar(
node1,
node2,
row.getString("Channel"),
row.getString("Tags")
);
this.add_bar(bar);
}
}
// ----------- Load Pixels
// TODO: We'll just generate it based on parameters for now
private void load_pixels(String path) {
File f = new File(path);
if (!f.exists()) {
return;
}
Table table_pixels = loadTable(path, "header,tsv");
}
private void automap_bars() {
}
//******************************************************* GET NODES AND BARS
/** Fetch a node by name */
public Node get_node(String node) {
return this.nodes.get(node);
}
/** Fetch a bar by its nodes - note that ordering is important */
public Bar get_bar(Node node1, Node node2) {
return this.bars.get(node1.name).get(node2.name);
}
/** Fetch a bar by its node names - note that ordering is important*/
public Bar get_bar(String node1, String node2) {
return this.bars.get(node1).get(node2);
}
//******************************************************* ADD NODES AND BARS
/** Add node to model. */
private void add_node(Node node) {
if (this.nodes.get(node.name)) {
throw new RuntimeException("Node " + node.name + " already exists!");
}
this.nodes.put(node.name, node);
this.bars.put(node.name, new TreeMap<String, Bar>());
}
/** Initialize pixels and add bar to the model. */
private void add_bar(Bar bar) {
List<LXPoint> points = new ArrayList<LXPoint>();
float density, rate, buffer;
float len_bar, len_zone, len_true, len_waste;
String layout;
PVector vector = new PVector(); // vector along the bar
PVector norm = new PVector(); // normalized vector
PVector step = new PVector(); // step vector per pixel
PVector start = new PVector(); // starting coordinates
PVector coords = new PVector(); // current coordinates
int count = 0; // number of pixels
//============ Validation
if (!this.get_node(bar.node1.name)) {
throw new RuntimeException("Unknown node " + bar.node1.name);
}
if (!this.get_node(bar.node2.name)) {
throw new RuntimeException("Unknown node " + bar.node2.name);
}
if (this.get_bar(bar.node1, bar.node2)) {
throw new RuntimeException("Bar " + bar.name + " already exists!");
}
//============ Set defaults and initialize
if (bar.pixel_density == null){ bar.pixel_density = model.pixel_density; }
if (bar.pixel_buffer == null){ bar.pixel_buffer = model.pixel_buffer; }
if (bar.pixel_layout == null){ bar.pixel_layout = model.pixel_layout; }
density = bar.pixel_density;
buffer = bar.pixel_buffer;
layout = bar.pixel_layout;
rate = 1.0 / density;
if (buffer == 0.f) { buffer = rate; } // pixels never start right at a node
//============ Vector Magic
PVector.sub(node2.xyz, node1.xyz, vector);
PVector.normalize(vector, norm);
len_bar = vector.mag();
len_zone = len_bar - (2.0 * buffer);
count = (int)Math.floor(len_zone * density);
len_true = (float)count / density;
len_waste = len_zone - len_zone;
// offset coordinates for first pixel
if (layout == "fill" ) {
PVector.mult(norm, buffer, start);
PVector.mult(norm, rate, step);
} else if (layout == "center") {
PVector.mult(norm, buffer + (len_waste / 2.0), start);
PVector.mult(norm, rate, step);
}
// move to true coordinates
PVector.add(start, bar.node1.xyz, coords);
//---------- map channels
// See if we need to add a new channel
// TODO: Actually build the channels
if (pixels_in_channel + count > max_pixels_per_channel) {
channels++;
strips = 0;
pixels_in_channel = 0;
}
pixels_in_channel += count;
pixels_in_model += count;
//------------ Allocate Pixels!
for (int i = 0; i <= count; i++ 1) {
LXPoint point = new LXPoint(coords.x, coords.y, coords.z);
points.add(point);
coords.add(step);
}
bar.channel = channels-1;
bar.strip = strips;
bar.channel_pixel = pixels_in_channel;
bar.model_pixel = pixals_in_model;
bar.points = points;
rev = bar.reverse();
//------------ Add Points and Bars to Model
this.bars.get(node1.name).put(node2.name, bar);
this.bars.get(node2.name).put(node1.name, rev);
}
}