eliminated xyz class in favor of PVector
[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(Point p1, Point p2) { return dist(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z); }
10 float xyDist (Point p1, Point 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, Point 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) { // use vec.angleBetween() for this
97 vT1.set(v1); vT2.set(v2); vT1.sub(c); vT2.sub(c);
98 return degrees(PVector.angleBetween(vT1,vT2)); }
99
100 Pick addPick(String name, int def, int _max, String[] desc) {
101 Pick P = new Pick(name, def, _max+1, nMaxRow, desc);
102 nMaxRow = P.EndRow + 1;
103 picks.add(P);
104 return P;
105 }
106
107 boolean noteOff(Note note) {
108 int row = note.getPitch(), col = note.getChannel();
109 for (int i=0; i<bools.size(); i++) if (bools.get(i).set(row, col, false)) { presetManager.dirty(this); return true; }
110 updateLights(); return false;
111 }
112
113 boolean noteOn(Note note) {
114 int row = note.getPitch(), col = note.getChannel();
115 for (int i=0; i<picks.size(); i++) if (picks.get(i).set(row, col)) { presetManager.dirty(this); return true; }
116 for (int i=0; i<bools.size(); i++) if (bools.get(i).set(row, col, true)) { presetManager.dirty(this); return true; }
117 println("row: " + row + " col: " + col); return false;
118 }
119
120 void onInactive() { uiDebugText.setText(""); }
121 void onReset() {
122 for (int i=0; i<bools .size(); i++) bools.get(i).reset();
123 for (int i=0; i<picks .size(); i++) picks.get(i).reset();
124 presetManager.dirty(this);
125 updateLights();
126 }
127
128 DPat(GLucose glucose) {
129 super(glucose);
130
131 pSpark = addParam("Sprk", 0);
132 pWave = addParam("Wave", 0);
133 pTransX = addParam("TrnX", .5);
134 pTransY = addParam("TrnY", .5);
135 pRotX = addParam("RotX", .5);
136 pRotY = addParam("RotY", .5);
137 pRotZ = addParam("RotZ", .5);
138 pSpin = addParam("Spin", .5);
139
140 nPoints = model.points.size();
141 pXsym = new DBool("X-SYM", false, 48, 0); bools.add(pXsym );
142 pYsym = new DBool("Y-SYM", false, 48, 1); bools.add(pYsym );
143 pRsym = new DBool("R-SYM", false, 48, 2); bools.add(pRsym );
144 pXdup = new DBool("X-DUP", false, 48, 3); bools.add(pXdup );
145 pJog = new DBool("JOG" , false, 48, 4); bools.add(pJog );
146 pGrey = new DBool("GREY" , false, 48, 5); bools.add(pGrey );
147
148 modmin = new PVector(model.xMin, model.yMin, model.zMin);
149 mMax = new PVector(model.xMax, model.yMax, model.zMax); mMax.sub(modmin);
150 mCtr = new PVector(); mCtr.set(mMax); mCtr.mult(.5);
151 mHalf = new PVector(.5,.5,.5);
152 xWaveNz = new float[ceil(mMax.y)+1];
153 yWaveNz = new float[ceil(mMax.x)+1];
154
155 //println (model.xMin + " " + model.yMin + " " + model.zMin);
156 //println (model.xMax + " " + model.yMax + " " + model.zMax);
157 //for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}}
158 }
159
160 void setAPCOutput(MidiOutput output) {
161 APCOut = output;
162 }
163
164 void updateLights() { if (APCOut == null) return;
165 for (int i = 0; i < NumApcRows; ++i)
166 for (int j = 0; j < 8; ++j) APCOut.sendNoteOn(j, 53+i, 0);
167 for (int i=0; i<picks .size(); i++) APCOut.sendNoteOn(picks.get(i).CurCol, picks.get(i).CurRow, 3);
168 for (int i=0; i<bools .size(); i++) if (bools.get(i).b) APCOut.sendNoteOn (bools.get(i).col, bools.get(i).row, 1);
169 else APCOut.sendNoteOff (bools.get(i).col, bools.get(i).row, 0);
170 }
171
172 void run(double deltaMs)
173 {
174 if (deltaMs > 100) return;
175
176 if (this == midiEngine.getFocusedDeck().getActivePattern()) {
177 String Text1="", Text2="";
178 for (int i=0; i<bools.size(); i++) if (bools.get(i).b) Text1 += " " + bools.get(i).tag + " ";
179 for (int i=0; i<picks.size(); i++) Text1 += picks.get(i).tag + ": " + picks.get(i).CurDesc() + " ";
180 uiDebugText.setText(Text1, Text2);
181 }
182
183 NoiseMove += deltaMs; NoiseMove = NoiseMove % 1e7;
184 StartRun (deltaMs);
185 PVector P = new PVector(), tP = new PVector(), pSave = new PVector();
186 PVector pTrans = new PVector(val(pTransX)*200-100, val(pTransY)*100-50,0);
187 nPoint = 0;
188
189 if (pJog.b) {
190 float tRamp = (lx.tempo.rampf() % .25);
191 if (tRamp < LastJog) xyzJog.set(randctr(mMax.x*.2), randctr(mMax.y*.2), randctr(mMax.z*.2));
192 LastJog = tRamp;
193 }
194
195 // precalculate this stuff
196 float wvAmp = val(pWave), sprk = val(pSpark);
197 if (wvAmp > 0) {
198 for (int i=0; i<ceil(mMax.x)+1; i++)
199 yWaveNz[i] = wvAmp * (noise(i/(mMax.x*.3)-(2e3+NoiseMove)/1500.) - .5) * (mMax.y/2.);
200
201 for (int i=0; i<ceil(mMax.y)+1; i++)
202 xWaveNz[i] = wvAmp * (noise(i/(mMax.y*.3)-(1e3+NoiseMove)/1500.) - .5) * (mMax.x/2.);
203 }
204
205 for (Point p : model.points) { nPoint++;
206 setVec(P,p);
207 P.sub(modmin);
208 P.sub(pTrans);
209 if (sprk > 0) {P.y += sprk*randctr(50); P.x += sprk*randctr(50); P.z += sprk*randctr(50); }
210 if (wvAmp > 0) P.y += interpWv(p.x-modmin.x, yWaveNz);
211 if (wvAmp > 0) P.x += interpWv(p.y-modmin.y, xWaveNz);
212 if (pJog.b) P.add(xyzJog);
213
214
215 color cNew, cOld = colors[p.index];
216 { tP.set(P); cNew = CalcPoint(tP); }
217 if (pXsym.b) { tP.set(mMax.x-P.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
218 if (pYsym.b) { tP.set(P.x,mMax.y-P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
219 if (pRsym.b) { tP.set(mMax.x-P.x,mMax.y-P.y,mMax.z-P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
220 if (pXdup.b) { tP.set((P.x+mMax.x*.5)%mMax.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
221 if (pGrey.b) { cNew = lx.hsb(0, 0, lx.b(cNew)); }
222 colors[p.index] = cNew;
223 }
224 }
225 }
226 //----------------------------------------------------------------------------------------------------------------------------------
227 class dTurn {
228 dVertex v;
229 int pos0, pos1;
230 dTurn(int _pos0, dVertex _v, int _pos1) { v = _v; pos0 = _pos0; pos1 = _pos1; }
231 }
232
233 class dVertex {
234 dVertex c0, c1, c2, c3, // connections on the cube
235 opp, same; // opp - same strip, opp direction
236 // same - same strut, diff strip, dir
237 dTurn t0, t1, t2, t3;
238 Strip s;
239 int dir, ci; // dir -- 1 or -1.
240 // ci -- color index
241
242 dVertex(Strip _s, Point _p) { s = _s; ci = _p.index; }
243 Point getPoint(int i) { return s.points.get(dir>0 ? i : 15-i); }
244 void setOpp(dVertex _opp) { opp = _opp; dir = (ci < opp.ci ? 1 : -1); }
245 }
246 //----------------------------------------------------------------------------------------------------------------------------------
247 class dPixel { dVertex v; int pos; dPixel(dVertex _v, int _pos) { v=_v; pos=_pos; } }
248 class dLattice {
249 void addTurn (dVertex v0, int pos0, dVertex v1, int pos1) { dTurn t = new dTurn(pos0, v1, pos1);
250 if (v0.t0 == null) { v0.t0=t; return; }
251 if (v0.t1 == null) { v0.t1=t; return; }
252 if (v0.t2 == null) { v0.t2=t; return; }
253 if (v0.t3 == null) { v0.t3=t; return; }
254 }
255 float dist2 (Strip s1, int pos1, Strip s2, int pos2) { return pointDist(s1.points.get(pos1), s2.points.get(pos2)); }
256 float pd2 (Point p1, float x, float y, float z) { return dist(p1.x,p1.y,p1.z,x,y,z); }
257 boolean sameSame (Strip s1, Strip s2) { return max(dist2(s1, 0, s2, 0), dist2(s1,15, s2,15)) < 5 ; } // same strut, same direction
258 boolean sameOpp (Strip s1, Strip s2) { return max(dist2(s1, 0, s2,15), dist2(s1,15, s2,0 )) < 5 ; } // same strut, opp direction
259 boolean sameBar (Strip s1, Strip s2) { return sameSame(s1,s2) || sameOpp(s1,s2); } // 2 strips on same strut
260
261
262 void addJoint (dVertex v1, dVertex v2) {
263 // should probably replace parallel but further with the new one
264 if (v1.c0 != null && sameBar(v2.s, v1.c0.s)) return;
265 if (v1.c1 != null && sameBar(v2.s, v1.c1.s)) return;
266 if (v1.c2 != null && sameBar(v2.s, v1.c2.s)) return;
267 if (v1.c3 != null && sameBar(v2.s, v1.c3.s)) return;
268
269 if (v1.c0 == null) v1.c0 = v2;
270 else if (v1.c1 == null) v1.c1 = v2;
271 else if (v1.c2 == null) v1.c2 = v2;
272 else if (v1.c3 == null) v1.c3 = v2;
273 }
274
275 dVertex v0(Strip s) { return (dVertex)s.obj1; }
276 dVertex v1(Strip s) { return (dVertex)s.obj2; }
277
278 dPixel getClosest(PVector p) {
279 dVertex v = null; int pos=0; float d = 500;
280
281 for (Strip s : glucose.model.strips) {
282 float nd = pd2(s.points.get(0),p.x,p.y,p.z); if (nd < d) { v=v0(s); d=nd; pos=0; }
283 if (nd > 30) continue;
284 for (int k=0; k<=15; k++) {
285 nd = pd2(s.points.get(k),p.x,p.y,p.z); if (nd < d) { v =v0(s); d=nd; pos=k; }
286 }
287 }
288 return random(2) < 1 ? new dPixel(v,pos) : new dPixel(v.opp,15-pos);
289 }
290
291 dLattice() {
292 lattice=this;
293
294 for (Strip s : glucose.model.strips) {
295 dVertex vrtx0 = new dVertex(s,s.points.get(0 )); s.obj1=vrtx0;
296 dVertex vrtx1 = new dVertex(s,s.points.get(15)); s.obj2=vrtx1;
297 vrtx0.setOpp(vrtx1); vrtx1.setOpp(vrtx0);
298 }
299
300 for (Strip s1 : glucose.model.strips) { for (Strip s2 : glucose.model.strips) {
301 if (s1.points.get(0).index < s2.points.get(0).index) continue;
302 int c=0;
303 if (sameSame(s1,s2)) { v0(s1).same = v0(s2); v1(s1).same = v1(s2);
304 v0(s2).same = v0(s1); v1(s2).same = v1(s1); continue; } // parallel
305 if (sameOpp (s1,s2)) { v0(s1).same = v1(s2); v1(s1).same = v0(s2);
306 v0(s2).same = v1(s1); v1(s2).same = v0(s1); continue; } // parallel
307 if (dist2(s1, 0, s2, 0) < 5) { c++; addJoint(v1(s1), v0(s2)); addJoint(v1(s2), v0(s1)); }
308 if (dist2(s1, 0, s2,15) < 5) { c++; addJoint(v1(s1), v1(s2)); addJoint(v0(s2), v0(s1)); }
309 if (dist2(s1,15, s2, 0) < 5) { c++; addJoint(v0(s1), v0(s2)); addJoint(v1(s2), v1(s1)); }
310 if (dist2(s1,15, s2,15) < 5) { c++; addJoint(v0(s1), v1(s2)); addJoint(v0(s2), v1(s1)); }
311 if (c>0) continue;
312
313 // Are they touching at all?
314 int pos1=0, pos2=0; float d = 100;
315
316 while (pos1 < 15 || pos2 < 15) {
317 float oldD = d;
318 if (pos1<15) { float d2 = dist2(s1, pos1+1, s2, pos2+0); if (d2 < d) { d=d2; pos1++; } }
319 if (pos2<15) { float d2 = dist2(s1, pos1+0, s2, pos2+1); if (d2 < d) { d=d2; pos2++; } }
320 if (d > 50 || oldD == d) break ;
321 }
322
323 if (d>5) continue;
324 addTurn(v0(s1), pos1, v0(s2), pos2); addTurn(v1(s1), 15-pos1, v0(s2), pos2);
325 addTurn(v0(s2), pos2, v0(s1), pos1); addTurn(v1(s2), 15-pos2, v0(s1), pos1);
326 }}
327 }
328 }
329
330 dLattice lattice;
331 //----------------------------------------------------------------------------------------------------------------------------------