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