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