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