Commit | Line | Data |
---|---|---|
e28f168c | 1 | //---------------------------------------------------------------------------------------------------------------------------------- |
4404e6b8 | 2 | xyz mMax, mCtr, mHalf; |
dd00b10f | 3 | int NumApcRows = 5, NumApcCols = 8; |
59b99905 | 4 | DGlobals DG = new DGlobals(); |
e28f168c | 5 | |
4404e6b8 | 6 | boolean btwn (int a,int b,int c) { return a >= b && a <= c; } |
7 | boolean btwn (double a,double b,double c) { return a >= b && a <= c; } | |
8 | float interp (float a, float b, float c) { return (1-a)*b + a*c; } | |
9 | float randctr (float a) { return random(a) - a*.5; } | |
10 | float min (float a, float b, float c, float d) { return min(min(a,b),min(c,d)); } | |
11 | ||
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 | ||
e28f168c AG |
25 | |
26 | public class Pick { | |
3661fcee | 27 | int NumPicks, Default , CurRow , CurCol , |
6680a94a | 28 | StartRow, EndRow ; |
29 | String tag , Desc[] ; | |
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 | |
49 | public 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 | ||
60 | public 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 | 68 | public 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 | |
88 | void RotateZ (xyz o, float nSin, float nCos) { | |
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 | |
c9e7c456 | 94 | void RotateX (xyz o, float nSin, float nCos) { |
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 | ||
100 | void RotateY (xyz o, float nSin, float nCos) { | |
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 | //---------------------------------------------------------------------------------------------------------------------------------- | |
dd00b10f | 111 | public class DGlobals { |
112 | boolean bInit = false; | |
113 | MidiOutput APCOut = null; | |
114 | MidiInput APCIn = null, OxygenIn = null; | |
6680a94a | 115 | DPat CurPat = null; |
af83f61c | 116 | int KeyPressed = -1; |
73687629 | 117 | boolean bSustain = false; |
e28f168c | 118 | |
d5ce8c1c | 119 | |
6680a94a | 120 | float Sliders[] = new float [] {1,0,0,0,0,0,0,0}; |
e5308763 | 121 | String SliderText[] = new String[] {"Level", "??", "Spark", "Xwave", "Ywave", "??", "??", "??", "??"}; |
3661fcee | 122 | |
123 | void SetNoteOn (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn (col, row, clr); } | |
124 | void SetNoteOff (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOff (col, row, clr); } | |
125 | void SetKnob (int cc , int c , int v ){ if (APCOut != null) APCOut.sendController (cc , c, v); } | |
e28f168c | 126 | |
3661fcee | 127 | DBool GetBool (int i) { return (DBool)CurPat.bools .get(i); } |
128 | Pick GetPick (int i) { return (Pick) CurPat.picks .get(i); } | |
129 | DParam GetParam(int i) { return (DParam) CurPat.params.get(i); } | |
6680a94a | 130 | |
af83f61c | 131 | float _Level () { return Sliders[0]; } |
3661fcee | 132 | float _Spark () { return Sliders[2]; } |
32986078 | 133 | float _XWave () { return Sliders[3]; } |
134 | float _YWave () { return Sliders[4]; } | |
e28f168c | 135 | |
3661fcee | 136 | void Init () { |
dd00b10f | 137 | if (bInit) return; bInit=true; |
6680a94a | 138 | for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}} |
139 | for (MidiInputDevice i: RWMidi.getInputDevices ()) { if (i.toString().contains("APC")) { i.createInput (this); break;}} | |
c9e7c456 | 140 | } |
6680a94a | 141 | |
3661fcee | 142 | boolean isFocused () { return CurPat != null && CurPat == midiEngine.getFocusedDeck().getActivePattern(); } |
143 | void Deactivate (DPat p) { if (p != CurPat) return; uiDebugText.setText(""); CurPat = null; } | |
144 | void Activate (DPat p) { | |
73687629 | 145 | bSustain = false; |
59b99905 | 146 | CurPat = p; |
dd00b10f | 147 | while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2); |
6680a94a | 148 | for (int i=0; i<p.params.size(); i++) GetParam(i).reset(); |
149 | for (int i=0; i<p.bools .size(); i++) GetBool (i).reset(); | |
150 | for (int i=0; i<p.picks .size(); i++) GetPick (i).reset(); | |
dd00b10f | 151 | UpdateLights(); |
e28f168c AG |
152 | } |
153 | ||
3661fcee | 154 | void SetText() { |
6680a94a | 155 | if (!isFocused()) return; |
156 | String Text1="", Text2=""; | |
157 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).b) Text1 += " " + GetBool(i).tag + " "; | |
158 | for (int i=0; i<CurPat.picks.size(); i++) Text1 += GetPick(i).tag + ": " + GetPick(i).CurDesc() + " "; | |
e5308763 | 159 | for (int i=0; i<5; i++) Text2 += SliderText[i] + ": " + round(100*Sliders[i]) + " "; |
6680a94a | 160 | uiDebugText.setText(Text1, Text2); |
161 | } | |
162 | ||
dd00b10f | 163 | void UpdateLights() { |
3661fcee | 164 | if (!isFocused() || APCOut == null) return; |
165 | for (int i=53;i< 58; i++) for (int j=0; j<NumApcCols; j++) SetNoteOn(i, j, 0); | |
166 | for (int i=48;i< 56; i++) SetKnob(0, i, 0); | |
167 | for (int i=16;i< 20; i++) SetKnob(0, i, 0); | |
168 | ||
32986078 | 169 | for (int i=0; i<CurPat.params.size(); i++) SetKnob ( 0, i<8 ? 48+i : 16 + i - 8, round(GetParam(i).Val()*127) ); |
3661fcee | 170 | for (int i=0; i<CurPat.picks .size(); i++) SetNoteOn (GetPick(i).CurRow, GetPick(i).CurCol, 3); |
171 | for (int i=0; i<CurPat.bools .size(); i++) if (GetBool(i).b) SetNoteOn (GetBool(i).row, GetBool(i).col, 1); | |
172 | else SetNoteOff (GetBool(i).row, GetBool(i).col, 0); | |
e28f168c | 173 | } |
3661fcee | 174 | |
59b99905 | 175 | void controllerChangeReceived(rwmidi.Controller cc) { |
176 | if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; } | |
177 | } | |
178 | ||
3661fcee | 179 | void noteOffReceived(Note note) { if (!isFocused()) return; |
180 | int row = note.getPitch(), col = note.getChannel(); | |
181 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, false)) return; | |
e28f168c | 182 | UpdateLights(); |
73687629 | 183 | bSustain=false; |
e28f168c AG |
184 | } |
185 | ||
3661fcee | 186 | void noteOnReceived (Note note) { if (!isFocused()) return; |
187 | int row = note.getPitch(), col = note.getChannel(); | |
188 | for (int i=0; i<CurPat.picks.size(); i++) if (GetPick(i).set(row, col)) return; | |
189 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, true)) return; | |
73687629 | 190 | |
191 | if (row == 84 && col==0) { Activate(CurPat); CurPat.StartPattern(); return; } | |
192 | if (row == 85 && col==0) { bSustain=true; return; } | |
193 | ||
af83f61c | 194 | if (row == 52) { KeyPressed = col; return; } |
195 | println("row: " + row + " col: " + col); | |
e28f168c | 196 | } |
dd00b10f | 197 | } |
198 | //---------------------------------------------------------------------------------------------------------------------------------- | |
199 | public class DPat extends SCPattern | |
200 | { | |
d5ce8c1c | 201 | ArrayList picks = new ArrayList(); // should be ArrayList<Pick> picks = new ArrayList<Pick>(); |
6680a94a | 202 | ArrayList params = new ArrayList(); |
203 | ArrayList bools = new ArrayList(); | |
3661fcee | 204 | int nMaxRow = 53; |
6680a94a | 205 | float LastQuant = -1, LastJog = -1; |
32986078 | 206 | float[] xWaveNz, yWaveNz; |
dd00b10f | 207 | int nPoint , nPoints; |
4404e6b8 | 208 | xyz xyzJog = new xyz(), vT1 = new xyz(), vT2 = new xyz(); |
209 | xyz modmin; | |
af83f61c | 210 | |
dd00b10f | 211 | float NoiseMove = random(10000); |
e5308763 | 212 | DParam pBlank, pBlank2, pRotX, pRotY, pRotZ, pSpin, pTransX, pTransY; |
213 | ||
214 | DBool pXsym, pYsym, pRsym, pXdup, pXtrip, pJog, pKey; | |
215 | float lxh () { return lx.getBaseHuef(); } | |
dd00b10f | 216 | float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); } |
32986078 | 217 | int c1c (float a) { return round(100*constrain(a,0,1)); } |
218 | float interpWv(float i, float[] vals) { return interp(i-floor(i), vals[floor(i)], vals[ceil(i)]); } | |
219 | ||
4404e6b8 | 220 | float CalcCone (xyz v1, xyz v2, xyz c) { vT1.set(v1); vT2.set(v2); vT1.subtract(c); vT2.subtract(c); |
221 | return degrees( acos ( vT1.dot(vT2) / (sqrt(vT1.dot(vT1)) * sqrt(vT2.dot(vT2)) ) )); } | |
32986078 | 222 | |
223 | void StartPattern() { } | |
dd00b10f | 224 | void StartRun(double deltaMs) { } |
e5308763 | 225 | color CalcPoint(xyz p) { return lx.hsb(0,0,0); } |
59b99905 | 226 | boolean IsActive() { return this == DG.CurPat; } |
ee1206f6 | 227 | boolean IsFocused() { return midiEngine != null && midiEngine.getFocusedDeck() != null && |
228 | this == midiEngine.getFocusedDeck().getActivePattern(); } | |
59b99905 | 229 | void onInactive() { UpdateState(); } |
32986078 | 230 | void onActive () { UpdateState(); StartPattern(); } |
59b99905 | 231 | void UpdateState() { if (IsFocused() != IsActive()) { if (IsFocused()) DG.Activate(this); else DG.Deactivate(this); } } |
4404e6b8 | 232 | color blend3(color c1, color c2, color c3){ return blendColor(c1,blendColor(c2,c3,ADD),ADD); } |
e28f168c | 233 | |
6680a94a | 234 | DParam addParam(String label, double value) { |
235 | DParam P = new DParam(label, value); | |
e28f168c | 236 | super.addParameter(P); |
6680a94a | 237 | params.add(P); return P; |
e28f168c | 238 | } |
af83f61c | 239 | |
32986078 | 240 | Pick addPick(String name, int def, int _max, String[] desc) { |
241 | Pick P = new Pick(name, def, _max+1, nMaxRow, desc); | |
e28f168c | 242 | nMaxRow = P.EndRow + 1; |
dd00b10f | 243 | picks.add(P); |
e28f168c AG |
244 | return P; |
245 | } | |
3661fcee | 246 | |
dd00b10f | 247 | DPat(GLucose glucose) { |
248 | super(glucose); | |
af83f61c | 249 | |
e5308763 | 250 | pBlank = addParam("", 0); |
251 | pBlank2 = addParam("" , .5); | |
af83f61c | 252 | pTransX = addParam("TrnX", .5); |
253 | pTransY = addParam("TrnY", .5); | |
254 | pRotX = addParam("RotX", .5); | |
255 | pRotY = addParam("RotY", .5); | |
256 | pRotZ = addParam("RotZ", .5); | |
257 | pSpin = addParam("Spin", .5); | |
258 | ||
4404e6b8 | 259 | nPoints = model.points.size(); |
260 | pXsym = new DBool("X-SYM", false, 49, 0); bools.add(pXsym ); | |
261 | pYsym = new DBool("Y-SYM", false, 49, 1); bools.add(pYsym ); | |
262 | pRsym = new DBool("R-SYM", false, 49, 2); bools.add(pRsym ); | |
263 | pXdup = new DBool("X-DUP", false, 49, 3); bools.add(pXdup ); | |
e5308763 | 264 | pJog = new DBool("JOG" , false, 49, 4); bools.add(pJog ); |
265 | pKey = new DBool("KBD" , false, 49, 5); bools.add(pKey ); | |
73687629 | 266 | |
4404e6b8 | 267 | modmin = new xyz(model.xMin, model.yMin, model.zMin); |
268 | mMax = new xyz(model.xMax, model.yMax, model.zMax); mMax.subtract(modmin); | |
269 | mCtr = new xyz(mMax); mCtr.scale(.5); | |
270 | mHalf = new xyz(.5,.5,.5); | |
32986078 | 271 | xWaveNz = new float[ceil(mMax.y)+1]; |
272 | yWaveNz = new float[ceil(mMax.x)+1]; | |
73687629 | 273 | |
4404e6b8 | 274 | //println (model.xMin + " " + model.yMin + " " + model.zMin); |
275 | //println (model.xMax + " " + model.yMax + " " + model.zMax); | |
dd00b10f | 276 | DG.Init(); |
dd00b10f | 277 | } |
278 | ||
279 | void run(double deltaMs) | |
280 | { | |
59b99905 | 281 | UpdateState(); |
32986078 | 282 | NoiseMove += deltaMs; NoiseMove = NoiseMove % 1e7; |
dd00b10f | 283 | StartRun (deltaMs); |
3661fcee | 284 | xyz P = new xyz(), tP = new xyz(), pSave = new xyz(); |
af83f61c | 285 | xyz pTrans = new xyz(pTransX.Val()*200-100, pTransY.Val()*100-50,0); |
6680a94a | 286 | |
dd00b10f | 287 | DG.SetText(); |
288 | nPoint = 0; | |
af83f61c | 289 | |
6680a94a | 290 | if (pJog.b) { |
291 | float tRamp = (lx.tempo.rampf() % .25); | |
4404e6b8 | 292 | if (tRamp < LastJog) xyzJog.set(randctr(mMax.x*.2), randctr(mMax.y*.2), randctr(mMax.z*.2)); |
6680a94a | 293 | LastJog = tRamp; |
294 | } | |
295 | ||
32986078 | 296 | // precalculate this stuff |
297 | float yWv = DG._YWave(), xWv = DG._XWave(), sprk = DG._Spark(); | |
298 | if (yWv > 0) for (int i=0; i<ceil(mMax.x)+1; i++) | |
299 | yWaveNz[i] = yWv * (noise(i/(mMax.x*.3)-(2e3+NoiseMove)/1500.) - .5) * (mMax.y/2.); | |
300 | ||
301 | if (xWv > 0) for (int i=0; i<ceil(mMax.y)+1; i++) | |
302 | xWaveNz[i] = xWv * (noise(i/(mMax.y*.3)-(1e3+NoiseMove)/1500.) - .5) * (mMax.x/2.); | |
e5308763 | 303 | |
32986078 | 304 | for (Point p : model.points) { nPoint++; |
dd00b10f | 305 | P.set(p); |
4404e6b8 | 306 | P.subtract(modmin); |
af83f61c | 307 | P.subtract(pTrans); |
32986078 | 308 | if (sprk > 0) { P.y += sprk*randctr(50); P.x += sprk*randctr(50); P.z += sprk*randctr(50); } |
309 | if (yWv > 0) P.y += interpWv(p.x-modmin.x, yWaveNz); | |
310 | if (xWv > 0) P.x += interpWv(p.y-modmin.y, xWaveNz); | |
311 | if (pJog.b) P.add(xyzJog); | |
312 | ||
dd00b10f | 313 | |
4404e6b8 | 314 | color cNew, cOld = colors[p.index]; |
315 | { tP.set(P); cNew = CalcPoint(tP); } | |
316 | if (pXsym.b) { tP.set(mMax.x-P.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
317 | if (pYsym.b) { tP.set(P.x,mMax.y-P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
318 | if (pRsym.b) { tP.set(mMax.x-P.x,mMax.y-P.y,mMax.z-P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
319 | if (pXdup.b) { tP.set((P.x+mMax.x*.5)%mMax.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
6680a94a | 320 | |
e5308763 | 321 | float s = lx.s(cNew); |
a41f334c | 322 | float b = lx.b(cNew)/100.; |
a41f334c | 323 | if (DG.bSustain == true) b = max(b, (float) (lx.b(cOld)/100.)); |
73687629 | 324 | |
e5308763 | 325 | colors[p.index] = lx.hsb(lx.h(cNew), s, 100 * b * DG._Level()); |
dd00b10f | 326 | } |
327 | } | |
e28f168c | 328 | } |
190d91c2 | 329 | //---------------------------------------------------------------------------------------------------------------------------------- |
0fbf977b | 330 | class dTurn { |
331 | dVertex v; | |
e5308763 | 332 | int pos0, pos1; |
0fbf977b | 333 | dTurn(int _pos0, dVertex _v, int _pos1) { v = _v; pos0 = _pos0; pos1 = _pos1; } |
334 | } | |
335 | ||
336 | class dVertex { | |
e5308763 | 337 | dVertex c0, c1, // connections on the cube |
338 | opp, same; // opp - same strip, opp direction | |
339 | // same - same strut, diff strip, dir | |
0fbf977b | 340 | dTurn t0, t1; |
341 | dStrip s; | |
e5308763 | 342 | int dir, ci; // dir -- 1 or -1. |
343 | // ci -- color index | |
0fbf977b | 344 | |
345 | dVertex(dStrip _s, Point _p) { s = _s; ci = _p.index; } | |
346 | Point getPoint(int i) { return s.s.points.get(dir>0 ? i : 15-i); } | |
347 | void setOpp(dVertex _opp) { opp = _opp; dir = (ci < opp.ci ? 1 : -1); } | |
348 | } | |
349 | ||
350 | class dStrip { | |
351 | dVertex v0, v1; | |
352 | int row, col; | |
353 | Strip s; | |
41f26e8f | 354 | String desc() { return "r:" + row + " c:" + col + "i:" + floor(v0.ci/16); } |
0fbf977b | 355 | dStrip(Strip _s, int _i, int _row, int _col) { s = _s; row = _row; col = _col; } |
356 | } | |
357 | //---------------------------------------------------------------------------------------------------------------------------------- | |
358 | float PointDist(Point p1, Point p2) { return dist(p1.x,p1.y,p1.z,p2.x,p2.y,p2.z); } | |
359 | ||
e5308763 | 360 | class dPixel { dVertex v; int pos; dPixel(dVertex _v, int _pos) { v=_v; pos=_pos; } } |
0fbf977b | 361 | class dLattice { |
362 | private int iTowerStrips=0; | |
363 | ||
364 | dStrip[] DS = new dStrip[glucose.model.strips.size()]; | |
e5308763 | 365 | //int[][] DQ = new int[NumBackTowers][MaxCubeHeight*2]; |
366 | //dStrip GetStrip (int row, int col, int off) { | |
367 | // return (!btwn(off,0,15) || !btwn(row,0,MaxCubeHeight*2-1) || !btwn(col,0,NumBackTowers-1) || DQ[col][row]<0) ? null : | |
368 | // DS[DQ[col][row]+off]; | |
369 | //} | |
0fbf977b | 370 | |
371 | 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; } | |
0fbf977b | 372 | float Dist2 (Strip s1, int pos1, Strip s2, int pos2) { return PointDist(s1.points.get(pos1), s2.points.get(pos2)); } |
41f26e8f | 373 | float PD2 (Point p1, float x, float y, float z) { return dist(p1.x,p1.y,p1.z,x,y,z); } |
e5308763 | 374 | boolean SameSame (Strip s1, Strip s2) { return max(Dist2(s1, 0, s2, 0), Dist2(s1,15, s2,15)) < 5 ; } // same strut, same direction |
375 | boolean SameOpp (Strip s1, Strip s2) { return max(Dist2(s1, 0, s2,15), Dist2(s1,15, s2,0 )) < 5 ; } // same strut, opp direction | |
376 | boolean SameBar (Strip s1, Strip s2) { return SameSame(s1,s2) || SameOpp(s1,s2); } // 2 strips on same strut | |
0fbf977b | 377 | void AddJoint (dVertex v1, dVertex v2) { |
378 | // should probably replace parallel but further with the new one | |
379 | if (v1.c0 != null && SameBar(v2.s.s, v1.c0.s.s)) return; | |
380 | if (v1.c1 != null && SameBar(v2.s.s, v1.c1.s.s)) return; | |
381 | if (v1.c0 == null) v1.c0 = v2; | |
382 | else if (v1.c1 == null) v1.c1 = v2; | |
383 | } | |
384 | ||
41f26e8f | 385 | dPixel getRand() { return new dPixel(DS[floor(random(iTowerStrips))].v0,floor(random(15))); } |
386 | dPixel getClosest(xyz p) { | |
387 | dVertex v = null; int pos=0; float d = 500; | |
388 | for (int j=0; j<iTowerStrips; j++) { | |
389 | dStrip s = DS[j]; | |
390 | float nd = PD2(s.s.points.get(0),p.x,p.y,p.z); if (nd < d) { v=s.v0; d=nd; pos=0; } | |
391 | if (nd > 30) continue; | |
392 | for (int k=0; k<=15; k++) { | |
393 | nd = PD2(s.s.points.get(k),p.x,p.y,p.z); if (nd < d) { v = s.v0; d=nd; pos=k; } | |
394 | } | |
395 | } | |
396 | return random(2) < 1 ? new dPixel(v,pos) : new dPixel(v.opp,15-pos); | |
397 | } | |
398 | ||
0fbf977b | 399 | dLattice() { |
41f26e8f | 400 | lattice=this; |
e5308763 | 401 | //for (int i=0;i<NumBackTowers;i++) for (int j=0;j<MaxCubeHeight*2;j++) DQ[i][j]=-1; |
0fbf977b | 402 | |
403 | int col = 0, row = -2, i=-1; | |
404 | for (Strip strip : glucose.model.strips ) { i++; | |
405 | if (i % 16 == 0) row+=2; | |
406 | if (row >= MaxCubeHeight*2-1) { col++; row = (col%2==1)?1:0; } // only include lattice parts! | |
407 | iTowerStrips++; | |
408 | dStrip s = DS[iTowerStrips-1] = new dStrip(strip, iTowerStrips-1, row, col); | |
409 | s.v0 = new dVertex(s,strip.points.get(0 )); | |
410 | s.v1 = new dVertex(s,strip.points.get(15)); | |
411 | s.v0.setOpp(s.v1); s.v1.setOpp(s.v0); | |
e5308763 | 412 | //if (col < NumBackTowers) DQ[col][row] = 16*floor((iTowerStrips-1)/16); |
413 | //else s.row=-1; | |
0fbf977b | 414 | } |
415 | ||
416 | for (int j=0; j<iTowerStrips; j++) { for (int k=j+1; k<iTowerStrips; k++) { | |
417 | dStrip s1 = DS[j], s2 = DS[k]; | |
418 | int c=0; | |
419 | if (SameSame(s1.s,s2.s)) { s1.v0.same = s2.v0; s1.v1.same = s2.v1; | |
420 | s2.v0.same = s1.v0; s2.v1.same = s1.v1; continue; } // parallel | |
421 | if (SameOpp (s1.s,s2.s)) { s1.v0.same = s2.v1; s1.v1.same = s2.v0; | |
422 | s2.v0.same = s1.v1; s2.v1.same = s1.v0; continue; } // parallel | |
423 | if (Dist2(s1.s, 0, s2.s, 0) < 5) { c++; AddJoint(s1.v1, s2.v0); AddJoint(s2.v1, s1.v0); } | |
424 | if (Dist2(s1.s, 0, s2.s,15) < 5) { c++; AddJoint(s1.v1, s2.v1); AddJoint(s2.v0, s1.v0); } | |
425 | if (Dist2(s1.s,15, s2.s, 0) < 5) { c++; AddJoint(s1.v0, s2.v0); AddJoint(s2.v1, s1.v1); } | |
426 | if (Dist2(s1.s,15, s2.s,15) < 5) { c++; AddJoint(s1.v0, s2.v1); AddJoint(s2.v0, s1.v1); } | |
427 | if (c>0) continue; | |
428 | ||
429 | // Are they touching at all? | |
430 | int pos1=0, pos2=0; float d = 100; | |
431 | ||
432 | while (pos1 < 15 || pos2 < 15) { | |
433 | float oldD = d; | |
434 | if (pos1<15) { float d2 = Dist2(s1.s, pos1+1, s2.s, pos2+0); if (d2 < d) { d=d2; pos1++; } } | |
435 | if (pos2<15) { float d2 = Dist2(s1.s, pos1+0, s2.s, pos2+1); if (d2 < d) { d=d2; pos2++; } } | |
436 | if (d > 50 || oldD == d) break ; | |
437 | } | |
438 | ||
439 | if (d>5) continue; | |
440 | addTurn(s1.v0, pos1, s2.v0, pos2); addTurn(s1.v1, 15-pos1, s2.v0, pos2); | |
441 | addTurn(s2.v0, pos2, s1.v0, pos1); addTurn(s2.v1, 15-pos2, s1.v0, pos1); | |
442 | ||
443 | }} | |
444 | } | |
445 | } | |
446 | ||
41f26e8f | 447 | dLattice lattice; |
0fbf977b | 448 | //---------------------------------------------------------------------------------------------------------------------------------- |