Mapping for Asana hackathon
[SugarCubes.git] / DanUtil.pde
CommitLineData
e28f168c 1//----------------------------------------------------------------------------------------------------------------------------------
596f331f 2int NumApcRows=4, NumApcCols=8;
e28f168c 3
4404e6b8 4boolean btwn (int a,int b,int c) { return a >= b && a <= c; }
5boolean btwn (double a,double b,double c) { return a >= b && a <= c; }
6float interp (float a, float b, float c) { return (1-a)*b + a*c; }
7float randctr (float a) { return random(a) - a*.5; }
abef24f4 8float min (float a, float b, float c, float d) { return min(min(a,b),min(c,d)); }
2bb56822
MS
9float pointDist(LXPoint p1, LXPoint p2) { return dist(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z); }
10float xyDist (LXPoint p1, LXPoint p2) { return dist(p1.x,p1.y,p2.x,p2.y); }
935819e6 11float distToSeg(float x, float y, float x1, float y1, float x2, float y2) {
4404e6b8 12 float A = x - x1, B = y - y1, C = x2 - x1, D = y2 - y1;
13 float dot = A * C + B * D, len_sq = C * C + D * D;
14 float xx, yy,param = dot / len_sq;
15
16 if (param < 0 || (x1 == x2 && y1 == y2)) { xx = x1; yy = y1; }
17 else if (param > 1) { xx = x2; yy = y2; }
18 else { xx = x1 + param * C;
19 yy = y1 + param * D; }
20 float dx = x - xx, dy = y - yy;
21 return sqrt(dx * dx + dy * dy);
22}
23
e28f168c 24public class Pick {
596f331f 25 int NumPicks, Default ,
26 CurRow , CurCol ,
6680a94a 27 StartRow, EndRow ;
28 String tag , Desc[] ;
596f331f 29
32986078 30 Pick (String label, int _Def, int _Num, int nStart, String d[]) {
31 NumPicks = _Num; Default = _Def;
32 StartRow = nStart; EndRow = StartRow + floor((NumPicks-1) / NumApcCols);
6680a94a 33 tag = label; Desc = d;
34 reset();
35 }
3661fcee 36
37 int Cur() { return (CurRow-StartRow)*NumApcCols + CurCol; }
38 String CurDesc() { return Desc[Cur()]; }
39 void reset() { CurCol = Default % NumApcCols; CurRow = StartRow + Default / NumApcCols; }
40
41 boolean set(int r, int c) {
42 if (!btwn(r,StartRow,EndRow) || !btwn(c,0,NumApcCols-1) ||
43 !btwn((r-StartRow)*NumApcCols + c,0,NumPicks-1)) return false;
44 CurRow=r; CurCol=c; return true;
45 }
e28f168c 46}
6680a94a 47
48public class DBool {
49 boolean def, b;
50 String tag;
51 int row, col;
52 void reset() { b = def; }
3661fcee 53 boolean set (int r, int c, boolean val) { if (r != row || c != col) return false; b = val; return true; }
6680a94a 54 DBool(String _tag, boolean _def, int _row, int _col) {
55 def = _def; b = _def; tag = _tag; row = _row; col = _col;
56 }
57}
e28f168c 58//----------------------------------------------------------------------------------------------------------------------------------
935819e6 59public class DPat extends SCPattern
60{
935819e6 61 ArrayList<Pick> picks = new ArrayList<Pick> ();
62 ArrayList<DBool> bools = new ArrayList<DBool> ();
73687629 63
5096de16 64 PVector mMax, mCtr, mHalf;
65
596f331f 66 MidiOutput APCOut;
3661fcee 67 int nMaxRow = 53;
5096de16 68 float LastJog = -1;
32986078 69 float[] xWaveNz, yWaveNz;
dd00b10f 70 int nPoint , nPoints;
5096de16 71 PVector xyzJog = new PVector(), modmin;
af83f61c 72
abef24f4 73 float NoiseMove = random(10000);
74 BasicParameter pSpark, pWave, pRotX, pRotY, pRotZ, pSpin, pTransX, pTransY;
75 DBool pXsym, pYsym, pRsym, pXdup, pXtrip, pJog, pGrey;
596f331f 76
5096de16 77 float lxh () { return lx.getBaseHuef(); }
78 int c1c (float a) { return round(100*constrain(a,0,1)); }
79 float interpWv(float i, float[] vals) { return interp(i-floor(i), vals[floor(i)], vals[ceil(i)]); }
80 void setNorm (PVector vec) { vec.set(vec.x/mMax.x, vec.y/mMax.y, vec.z/mMax.z); }
81 void setRand (PVector vec) { vec.set(random(mMax.x), random(mMax.y), random(mMax.z)); }
2bb56822 82 void setVec (PVector vec, LXPoint p) { vec.set(p.x, p.y, p.z); }
5096de16 83 void interpolate(float i, PVector a, PVector b) { a.set(interp(i,a.x,b.x), interp(i,a.y,b.y), interp(i,a.z,b.z)); }
84 void StartRun(double deltaMs) { }
85 float val (BasicParameter p) { return p.getValuef(); }
86 color CalcPoint(PVector p) { return lx.hsb(0,0,0); }
87 color blend3(color c1, color c2, color c3) { return blendColor(c1,blendColor(c2,c3,ADD),ADD); }
88
89 void rotateZ (PVector p, PVector o, float nSin, float nCos) { p.set( nCos*(p.x-o.x) - nSin*(p.y-o.y) + o.x , nSin*(p.x-o.x) + nCos*(p.y-o.y) + o.y,p.z); }
90 void rotateX (PVector p, PVector o, float nSin, float nCos) { p.set(p.x,nCos*(p.y-o.y) - nSin*(p.z-o.z) + o.y , nSin*(p.y-o.y) + nCos*(p.z-o.z) + o.z ); }
91 void rotateY (PVector p, PVector o, float nSin, float nCos) { p.set( nSin*(p.z-o.z) + nCos*(p.x-o.x) + o.x,p.y, nCos*(p.z-o.z) - nSin*(p.x-o.x) + o.z ); }
92
d6b5635a 93 BasicParameter addParam(String label, double value) { BasicParameter p = new BasicParameter(label, value); addParameter(p); return p; }
5096de16 94
95 PVector vT1 = new PVector(), vT2 = new PVector();
d6b5635a 96 float calcCone (PVector v1, PVector v2, PVector c) { vT1.set(v1); vT2.set(v2); vT1.sub(c); vT2.sub(c);
97 return degrees(PVector.angleBetween(vT1,vT2)); }
af83f61c 98
596f331f 99 Pick addPick(String name, int def, int _max, String[] desc) {
32986078 100 Pick P = new Pick(name, def, _max+1, nMaxRow, desc);
e28f168c 101 nMaxRow = P.EndRow + 1;
dd00b10f 102 picks.add(P);
e28f168c
AG
103 return P;
104 }
3661fcee 105
596f331f 106 boolean noteOff(Note note) {
107 int row = note.getPitch(), col = note.getChannel();
a1396b9e 108 for (int i=0; i<bools.size(); i++) if (bools.get(i).set(row, col, false)) { presetManager.dirty(this); return true; }
596f331f 109 updateLights(); return false;
110 }
111
112 boolean noteOn(Note note) {
113 int row = note.getPitch(), col = note.getChannel();
a1396b9e
MS
114 for (int i=0; i<picks.size(); i++) if (picks.get(i).set(row, col)) { presetManager.dirty(this); return true; }
115 for (int i=0; i<bools.size(); i++) if (bools.get(i).set(row, col, true)) { presetManager.dirty(this); return true; }
596f331f 116 println("row: " + row + " col: " + col); return false;
117 }
118
119 void onInactive() { uiDebugText.setText(""); }
120 void onReset() {
596f331f 121 for (int i=0; i<bools .size(); i++) bools.get(i).reset();
122 for (int i=0; i<picks .size(); i++) picks.get(i).reset();
a1396b9e 123 presetManager.dirty(this);
596f331f 124 updateLights();
125 }
126
dd00b10f 127 DPat(GLucose glucose) {
128 super(glucose);
af83f61c 129
935819e6 130 pSpark = addParam("Sprk", 0);
131 pWave = addParam("Wave", 0);
af83f61c 132 pTransX = addParam("TrnX", .5);
133 pTransY = addParam("TrnY", .5);
134 pRotX = addParam("RotX", .5);
135 pRotY = addParam("RotY", .5);
136 pRotZ = addParam("RotZ", .5);
137 pSpin = addParam("Spin", .5);
138
4404e6b8 139 nPoints = model.points.size();
935819e6 140 pXsym = new DBool("X-SYM", false, 48, 0); bools.add(pXsym );
141 pYsym = new DBool("Y-SYM", false, 48, 1); bools.add(pYsym );
142 pRsym = new DBool("R-SYM", false, 48, 2); bools.add(pRsym );
143 pXdup = new DBool("X-DUP", false, 48, 3); bools.add(pXdup );
144 pJog = new DBool("JOG" , false, 48, 4); bools.add(pJog );
145 pGrey = new DBool("GREY" , false, 48, 5); bools.add(pGrey );
73687629 146
5096de16 147 modmin = new PVector(model.xMin, model.yMin, model.zMin);
148 mMax = new PVector(model.xMax, model.yMax, model.zMax); mMax.sub(modmin);
149 mCtr = new PVector(); mCtr.set(mMax); mCtr.mult(.5);
150 mHalf = new PVector(.5,.5,.5);
32986078 151 xWaveNz = new float[ceil(mMax.y)+1];
152 yWaveNz = new float[ceil(mMax.x)+1];
73687629 153
4404e6b8 154 //println (model.xMin + " " + model.yMin + " " + model.zMin);
155 //println (model.xMax + " " + model.yMax + " " + model.zMax);
1b65b8ce
MS
156 //for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}}
157 }
79499592
MS
158
159 float spin() {
160 float raw = val(pSpin);
161 if (raw <= 0.45) {
162 return raw + 0.05;
163 } else if (raw >= 0.55) {
164 return raw - 0.05;
165 }
166 return 0.5;
167 }
1b65b8ce
MS
168
169 void setAPCOutput(MidiOutput output) {
170 APCOut = output;
dd00b10f 171 }
172
596f331f 173 void updateLights() { if (APCOut == null) return;
174 for (int i = 0; i < NumApcRows; ++i)
1b65b8ce
MS
175 for (int j = 0; j < 8; ++j) APCOut.sendNoteOn(j, 53+i, 0);
176 for (int i=0; i<picks .size(); i++) APCOut.sendNoteOn(picks.get(i).CurCol, picks.get(i).CurRow, 3);
596f331f 177 for (int i=0; i<bools .size(); i++) if (bools.get(i).b) APCOut.sendNoteOn (bools.get(i).col, bools.get(i).row, 1);
178 else APCOut.sendNoteOff (bools.get(i).col, bools.get(i).row, 0);
179 }
180
dd00b10f 181 void run(double deltaMs)
182 {
935819e6 183 if (deltaMs > 100) return;
596f331f 184
185 if (this == midiEngine.getFocusedDeck().getActivePattern()) {
186 String Text1="", Text2="";
187 for (int i=0; i<bools.size(); i++) if (bools.get(i).b) Text1 += " " + bools.get(i).tag + " ";
188 for (int i=0; i<picks.size(); i++) Text1 += picks.get(i).tag + ": " + picks.get(i).CurDesc() + " ";
189 uiDebugText.setText(Text1, Text2);
190 }
191
32986078 192 NoiseMove += deltaMs; NoiseMove = NoiseMove % 1e7;
dd00b10f 193 StartRun (deltaMs);
5096de16 194 PVector P = new PVector(), tP = new PVector(), pSave = new PVector();
195 PVector pTrans = new PVector(val(pTransX)*200-100, val(pTransY)*100-50,0);
dd00b10f 196 nPoint = 0;
af83f61c 197
6680a94a 198 if (pJog.b) {
199 float tRamp = (lx.tempo.rampf() % .25);
4404e6b8 200 if (tRamp < LastJog) xyzJog.set(randctr(mMax.x*.2), randctr(mMax.y*.2), randctr(mMax.z*.2));
6680a94a 201 LastJog = tRamp;
202 }
203
32986078 204 // precalculate this stuff
abef24f4 205 float wvAmp = val(pWave), sprk = val(pSpark);
935819e6 206 if (wvAmp > 0) {
207 for (int i=0; i<ceil(mMax.x)+1; i++)
208 yWaveNz[i] = wvAmp * (noise(i/(mMax.x*.3)-(2e3+NoiseMove)/1500.) - .5) * (mMax.y/2.);
32986078 209
935819e6 210 for (int i=0; i<ceil(mMax.y)+1; i++)
211 xWaveNz[i] = wvAmp * (noise(i/(mMax.y*.3)-(1e3+NoiseMove)/1500.) - .5) * (mMax.x/2.);
212 }
e5308763 213
2bb56822 214 for (LXPoint p : model.points) { nPoint++;
5096de16 215 setVec(P,p);
216 P.sub(modmin);
217 P.sub(pTrans);
935819e6 218 if (sprk > 0) {P.y += sprk*randctr(50); P.x += sprk*randctr(50); P.z += sprk*randctr(50); }
219 if (wvAmp > 0) P.y += interpWv(p.x-modmin.x, yWaveNz);
220 if (wvAmp > 0) P.x += interpWv(p.y-modmin.y, xWaveNz);
32986078 221 if (pJog.b) P.add(xyzJog);
222
dd00b10f 223
4404e6b8 224 color cNew, cOld = colors[p.index];
225 { tP.set(P); cNew = CalcPoint(tP); }
226 if (pXsym.b) { tP.set(mMax.x-P.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
227 if (pYsym.b) { tP.set(P.x,mMax.y-P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
228 if (pRsym.b) { tP.set(mMax.x-P.x,mMax.y-P.y,mMax.z-P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
229 if (pXdup.b) { tP.set((P.x+mMax.x*.5)%mMax.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
935819e6 230 if (pGrey.b) { cNew = lx.hsb(0, 0, lx.b(cNew)); }
231 colors[p.index] = cNew;
dd00b10f 232 }
233 }
e28f168c 234}
190d91c2 235//----------------------------------------------------------------------------------------------------------------------------------
0fbf977b 236class dTurn {
237 dVertex v;
e5308763 238 int pos0, pos1;
0fbf977b 239 dTurn(int _pos0, dVertex _v, int _pos1) { v = _v; pos0 = _pos0; pos1 = _pos1; }
240}
241
242class dVertex {
abef24f4 243 dVertex c0, c1, c2, c3, // connections on the cube
244 opp, same; // opp - same strip, opp direction
245 // same - same strut, diff strip, dir
246 dTurn t0, t1, t2, t3;
0f9ea23f 247 Strip s;
248 int dir, ci; // dir -- 1 or -1.
249 // ci -- color index
0fbf977b 250
2bb56822
MS
251 dVertex(Strip _s, LXPoint _p) { s = _s; ci = _p.index; }
252 LXPoint getPoint(int i) { return s.points.get(dir>0 ? i : 15-i); }
0fbf977b 253 void setOpp(dVertex _opp) { opp = _opp; dir = (ci < opp.ci ? 1 : -1); }
254}
0fbf977b 255//----------------------------------------------------------------------------------------------------------------------------------
e5308763 256class dPixel { dVertex v; int pos; dPixel(dVertex _v, int _pos) { v=_v; pos=_pos; } }
0fbf977b 257class dLattice {
abef24f4 258 void addTurn (dVertex v0, int pos0, dVertex v1, int pos1) { dTurn t = new dTurn(pos0, v1, pos1);
259 if (v0.t0 == null) { v0.t0=t; return; }
260 if (v0.t1 == null) { v0.t1=t; return; }
261 if (v0.t2 == null) { v0.t2=t; return; }
262 if (v0.t3 == null) { v0.t3=t; return; }
263 }
935819e6 264 float dist2 (Strip s1, int pos1, Strip s2, int pos2) { return pointDist(s1.points.get(pos1), s2.points.get(pos2)); }
2bb56822 265 float pd2 (LXPoint p1, float x, float y, float z) { return dist(p1.x,p1.y,p1.z,x,y,z); }
935819e6 266 boolean sameSame (Strip s1, Strip s2) { return max(dist2(s1, 0, s2, 0), dist2(s1,15, s2,15)) < 5 ; } // same strut, same direction
267 boolean sameOpp (Strip s1, Strip s2) { return max(dist2(s1, 0, s2,15), dist2(s1,15, s2,0 )) < 5 ; } // same strut, opp direction
268 boolean sameBar (Strip s1, Strip s2) { return sameSame(s1,s2) || sameOpp(s1,s2); } // 2 strips on same strut
abef24f4 269
270
935819e6 271 void addJoint (dVertex v1, dVertex v2) {
0fbf977b 272 // should probably replace parallel but further with the new one
0f9ea23f 273 if (v1.c0 != null && sameBar(v2.s, v1.c0.s)) return;
274 if (v1.c1 != null && sameBar(v2.s, v1.c1.s)) return;
abef24f4 275 if (v1.c2 != null && sameBar(v2.s, v1.c2.s)) return;
276 if (v1.c3 != null && sameBar(v2.s, v1.c3.s)) return;
277
0fbf977b 278 if (v1.c0 == null) v1.c0 = v2;
279 else if (v1.c1 == null) v1.c1 = v2;
abef24f4 280 else if (v1.c2 == null) v1.c2 = v2;
281 else if (v1.c3 == null) v1.c3 = v2;
0fbf977b 282 }
283
0f9ea23f 284 dVertex v0(Strip s) { return (dVertex)s.obj1; }
285 dVertex v1(Strip s) { return (dVertex)s.obj2; }
286
5096de16 287 dPixel getClosest(PVector p) {
41f26e8f 288 dVertex v = null; int pos=0; float d = 500;
0f9ea23f 289
290 for (Strip s : glucose.model.strips) {
291 float nd = pd2(s.points.get(0),p.x,p.y,p.z); if (nd < d) { v=v0(s); d=nd; pos=0; }
41f26e8f 292 if (nd > 30) continue;
293 for (int k=0; k<=15; k++) {
0f9ea23f 294 nd = pd2(s.points.get(k),p.x,p.y,p.z); if (nd < d) { v =v0(s); d=nd; pos=k; }
41f26e8f 295 }
296 }
297 return random(2) < 1 ? new dPixel(v,pos) : new dPixel(v.opp,15-pos);
298 }
299
0fbf977b 300 dLattice() {
41f26e8f 301 lattice=this;
0f9ea23f 302
303 for (Strip s : glucose.model.strips) {
304 dVertex vrtx0 = new dVertex(s,s.points.get(0 )); s.obj1=vrtx0;
305 dVertex vrtx1 = new dVertex(s,s.points.get(15)); s.obj2=vrtx1;
306 vrtx0.setOpp(vrtx1); vrtx1.setOpp(vrtx0);
0fbf977b 307 }
308
0f9ea23f 309 for (Strip s1 : glucose.model.strips) { for (Strip s2 : glucose.model.strips) {
310 if (s1.points.get(0).index < s2.points.get(0).index) continue;
0fbf977b 311 int c=0;
0f9ea23f 312 if (sameSame(s1,s2)) { v0(s1).same = v0(s2); v1(s1).same = v1(s2);
313 v0(s2).same = v0(s1); v1(s2).same = v1(s1); continue; } // parallel
314 if (sameOpp (s1,s2)) { v0(s1).same = v1(s2); v1(s1).same = v0(s2);
315 v0(s2).same = v1(s1); v1(s2).same = v0(s1); continue; } // parallel
316 if (dist2(s1, 0, s2, 0) < 5) { c++; addJoint(v1(s1), v0(s2)); addJoint(v1(s2), v0(s1)); }
317 if (dist2(s1, 0, s2,15) < 5) { c++; addJoint(v1(s1), v1(s2)); addJoint(v0(s2), v0(s1)); }
318 if (dist2(s1,15, s2, 0) < 5) { c++; addJoint(v0(s1), v0(s2)); addJoint(v1(s2), v1(s1)); }
319 if (dist2(s1,15, s2,15) < 5) { c++; addJoint(v0(s1), v1(s2)); addJoint(v0(s2), v1(s1)); }
0fbf977b 320 if (c>0) continue;
321
322 // Are they touching at all?
323 int pos1=0, pos2=0; float d = 100;
324
325 while (pos1 < 15 || pos2 < 15) {
326 float oldD = d;
0f9ea23f 327 if (pos1<15) { float d2 = dist2(s1, pos1+1, s2, pos2+0); if (d2 < d) { d=d2; pos1++; } }
328 if (pos2<15) { float d2 = dist2(s1, pos1+0, s2, pos2+1); if (d2 < d) { d=d2; pos2++; } }
0fbf977b 329 if (d > 50 || oldD == d) break ;
330 }
331
332 if (d>5) continue;
0f9ea23f 333 addTurn(v0(s1), pos1, v0(s2), pos2); addTurn(v1(s1), 15-pos1, v0(s2), pos2);
334 addTurn(v0(s2), pos2, v0(s1), pos1); addTurn(v1(s2), 15-pos2, v0(s1), pos1);
0fbf977b 335 }}
336 }
337}
338
41f26e8f 339dLattice lattice;
0fbf977b 340//----------------------------------------------------------------------------------------------------------------------------------