More mapping cleanup, got rid of tons of mess code, clean definitions now
[SugarCubes.git] / DanUtil.pde
1 //----------------------------------------------------------------------------------------------------------------------------------
2 int NumApcRows=4, NumApcCols=8;
3
4 boolean btwn (int a,int b,int c) { return a >= b && a <= c; }
5 boolean btwn (double a,double b,double c) { return a >= b && a <= c; }
6 float interp (float a, float b, float c) { return (1-a)*b + a*c; }
7 float randctr (float a) { return random(a) - a*.5; }
8 float min (float a, float b, float c, float d) { return min(min(a,b),min(c,d)); }
9 float pointDist(LXPoint p1, LXPoint p2) { return dist(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z); }
10 float xyDist (LXPoint p1, LXPoint p2) { return dist(p1.x,p1.y,p2.x,p2.y); }
11 float distToSeg(float x, float y, float x1, float y1, float x2, float y2) {
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
24 public class Pick {
25 int NumPicks, Default ,
26 CurRow , CurCol ,
27 StartRow, EndRow ;
28 String tag , Desc[] ;
29
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);
33 tag = label; Desc = d;
34 reset();
35 }
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 }
46 }
47
48 public class DBool {
49 boolean def, b;
50 String tag;
51 int row, col;
52 void reset() { b = def; }
53 boolean set (int r, int c, boolean val) { if (r != row || c != col) return false; b = val; return true; }
54 DBool(String _tag, boolean _def, int _row, int _col) {
55 def = _def; b = _def; tag = _tag; row = _row; col = _col;
56 }
57 }
58 //----------------------------------------------------------------------------------------------------------------------------------
59 public class DPat extends SCPattern
60 {
61 ArrayList<Pick> picks = new ArrayList<Pick> ();
62 ArrayList<DBool> bools = new ArrayList<DBool> ();
63
64 PVector mMax, mCtr, mHalf;
65
66 MidiOutput APCOut;
67 int nMaxRow = 53;
68 float LastJog = -1;
69 float[] xWaveNz, yWaveNz;
70 int nPoint , nPoints;
71 PVector xyzJog = new PVector(), modmin;
72
73 float NoiseMove = random(10000);
74 BasicParameter pSpark, pWave, pRotX, pRotY, pRotZ, pSpin, pTransX, pTransY;
75 DBool pXsym, pYsym, pRsym, pXdup, pXtrip, pJog, pGrey;
76
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)); }
82 void setVec (PVector vec, LXPoint p) { vec.set(p.x, p.y, p.z); }
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
93 BasicParameter addParam(String label, double value) { BasicParameter p = new BasicParameter(label, value); addParameter(p); return p; }
94
95 PVector vT1 = new PVector(), vT2 = new PVector();
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)); }
98
99 Pick addPick(String name, int def, int _max, String[] desc) {
100 Pick P = new Pick(name, def, _max+1, nMaxRow, desc);
101 nMaxRow = P.EndRow + 1;
102 picks.add(P);
103 return P;
104 }
105
106 boolean noteOff(Note note) {
107 int row = note.getPitch(), col = note.getChannel();
108 for (int i=0; i<bools.size(); i++) if (bools.get(i).set(row, col, false)) { presetManager.dirty(this); return true; }
109 updateLights(); return false;
110 }
111
112 boolean noteOn(Note note) {
113 int row = note.getPitch(), col = note.getChannel();
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; }
116 println("row: " + row + " col: " + col); return false;
117 }
118
119 void onInactive() { uiDebugText.setText(""); }
120 void onReset() {
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();
123 presetManager.dirty(this);
124 updateLights();
125 }
126
127 DPat(GLucose glucose) {
128 super(glucose);
129
130 pSpark = addParam("Sprk", 0);
131 pWave = addParam("Wave", 0);
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
139 nPoints = model.points.size();
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 );
146
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);
151 xWaveNz = new float[ceil(mMax.y)+1];
152 yWaveNz = new float[ceil(mMax.x)+1];
153
154 //println (model.xMin + " " + model.yMin + " " + model.zMin);
155 //println (model.xMax + " " + model.yMax + " " + model.zMax);
156 //for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}}
157 }
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 }
168
169 void setAPCOutput(MidiOutput output) {
170 APCOut = output;
171 }
172
173 void updateLights() { if (APCOut == null) return;
174 for (int i = 0; i < NumApcRows; ++i)
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);
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
181 void run(double deltaMs)
182 {
183 if (deltaMs > 100) return;
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
192 NoiseMove += deltaMs; NoiseMove = NoiseMove % 1e7;
193 StartRun (deltaMs);
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);
196 nPoint = 0;
197
198 if (pJog.b) {
199 float tRamp = (lx.tempo.rampf() % .25);
200 if (tRamp < LastJog) xyzJog.set(randctr(mMax.x*.2), randctr(mMax.y*.2), randctr(mMax.z*.2));
201 LastJog = tRamp;
202 }
203
204 // precalculate this stuff
205 float wvAmp = val(pWave), sprk = val(pSpark);
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.);
209
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 }
213
214 for (LXPoint p : model.points) { nPoint++;
215 setVec(P,p);
216 P.sub(modmin);
217 P.sub(pTrans);
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);
221 if (pJog.b) P.add(xyzJog);
222
223
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); }
230 if (pGrey.b) { cNew = lx.hsb(0, 0, lx.b(cNew)); }
231 colors[p.index] = cNew;
232 }
233 }
234 }
235 //----------------------------------------------------------------------------------------------------------------------------------
236 class dTurn {
237 dVertex v;
238 int pos0, pos1;
239 dTurn(int _pos0, dVertex _v, int _pos1) { v = _v; pos0 = _pos0; pos1 = _pos1; }
240 }
241
242 class dVertex {
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;
247 Strip s;
248 int dir, ci; // dir -- 1 or -1.
249 // ci -- color index
250
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); }
253 void setOpp(dVertex _opp) { opp = _opp; dir = (ci < opp.ci ? 1 : -1); }
254 }
255 //----------------------------------------------------------------------------------------------------------------------------------
256 class dPixel { dVertex v; int pos; dPixel(dVertex _v, int _pos) { v=_v; pos=_pos; } }
257 class dLattice {
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 }
264 float dist2 (Strip s1, int pos1, Strip s2, int pos2) { return pointDist(s1.points.get(pos1), s2.points.get(pos2)); }
265 float pd2 (LXPoint p1, float x, float y, float z) { return dist(p1.x,p1.y,p1.z,x,y,z); }
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
269
270
271 void addJoint (dVertex v1, dVertex v2) {
272 // should probably replace parallel but further with the new one
273 if (v1.c0 != null && sameBar(v2.s, v1.c0.s)) return;
274 if (v1.c1 != null && sameBar(v2.s, v1.c1.s)) return;
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
278 if (v1.c0 == null) v1.c0 = v2;
279 else if (v1.c1 == null) v1.c1 = v2;
280 else if (v1.c2 == null) v1.c2 = v2;
281 else if (v1.c3 == null) v1.c3 = v2;
282 }
283
284 dVertex v0(Strip s) { return (dVertex)s.obj1; }
285 dVertex v1(Strip s) { return (dVertex)s.obj2; }
286
287 dPixel getClosest(PVector p) {
288 dVertex v = null; int pos=0; float d = 500;
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; }
292 if (nd > 30) continue;
293 for (int k=0; k<=15; k++) {
294 nd = pd2(s.points.get(k),p.x,p.y,p.z); if (nd < d) { v =v0(s); d=nd; pos=k; }
295 }
296 }
297 return random(2) < 1 ? new dPixel(v,pos) : new dPixel(v.opp,15-pos);
298 }
299
300 dLattice() {
301 lattice=this;
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);
307 }
308
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;
311 int c=0;
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)); }
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;
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++; } }
329 if (d > 50 || oldD == d) break ;
330 }
331
332 if (d>5) continue;
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);
335 }}
336 }
337 }
338
339 dLattice lattice;
340 //----------------------------------------------------------------------------------------------------------------------------------