Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
9.3 kB
2
Indexable
Never
     }
    }
    setBoundary(b, _d);
  }
      
  protected void advectRGB(int bound, float[] du, float[] dv) {
    int i0, j0, i1, j1;
    float x, y, s0, t0, s1, t1, dt0;
        
    dt0 = _dt * _NX;
        
    for (int i = 1; i <= _NX; i++) {
      for (int j = 1; j <= _NY; j++) {
        x = i - dt0 * du[FLUID_IX(i, j)];
        y = j - dt0 * dv[FLUID_IX(i, j)];
            
        if (x > _NX + 0.5) x = _NX + 0.5f;
        if (x < 0.5)     x = 0.5f;
            
        i0 = (int) x;
        i1 = i0 + 1;
            
        if (y > _NY + 0.5) y = _NY + 0.5f;
        if (y < 0.5)     y = 0.5f;
            
        j0 = (int) y;
        j1 = j0 + 1;
            
        s1 = x - i0;
        s0 = 1 - s1;
        t1 = y - j0;
        t0 = 1 - t1;
            
        r[FLUID_IX(i, j)] = s0 * (t0 * rOld[FLUID_IX(i0, j0)] + t1 * rOld[FLUID_IX(i0, j1)])  + s1 * (t0 * rOld[FLUID_IX(i1, j0)] + t1 * rOld[FLUID_IX(i1, j1)]);
        g[FLUID_IX(i, j)] = s0 * (t0 * gOld[FLUID_IX(i0, j0)] + t1 * gOld[FLUID_IX(i0, j1)])  + s1 * (t0 * gOld[FLUID_IX(i1, j0)] + t1 * gOld[FLUID_IX(i1, j1)]);  
        b[FLUID_IX(i, j)] = s0 * (t0 * bOld[FLUID_IX(i0, j0)] + t1 * bOld[FLUID_IX(i0, j1)])  + s1 * (t0 * bOld[FLUID_IX(i1, j0)] + t1 * bOld[FLUID_IX(i1, j1)]);    
      }
    }
    setBoundaryRGB(bound);
  }
      
      
      
  protected void diffuse(int b, float[] c, float[] c0, float _diff) {
    float a = _dt * _diff * _NX * _NY;
    linearSolver(b, c, c0, a, 1.0f + 4 * a);
  }
      
  protected void diffuseRGB(int b, float _diff) {
    float a = _dt * _diff * _NX * _NY;
    linearSolverRGB(b, a, 1.0f + 4 * a);
  }
      
  protected void diffuseUV(int b, float _diff) {
    float a = _dt * _diff * _NX * _NY;
    linearSolverUV(b, a, 1.0f + 4 * a);
  }
      
      
  protected void project(float[] x, float[] y, float[] p, float[] div)  {
    for (int i = 1; i <= _NX; i++) {
      for (int j = 1; j <= _NY; j++) {
        div[FLUID_IX(i, j)] = (x[FLUID_IX(i+1, j)] - x[FLUID_IX(i-1, j)] + y[FLUID_IX(i, j+1)] - y[FLUID_IX(i, j-1)])
        * - 0.5f / _NX;
        p[FLUID_IX(i, j)] = 0;
      }
    }
        
    setBoundary(0, div);
    setBoundary(0, p);
        
    linearSolver(0, p, div, 1, 4);
        
    for (int i = 1; i <= _NX; i++) {
      for (int j = 1; j <= _NY; j++) {
        x[FLUID_IX(i, j)] -= 0.5f * _NX * (p[FLUID_IX(i+1, j)] - p[FLUID_IX(i-1, j)]);
        y[FLUID_IX(i, j)] -= 0.5f * _NX * (p[FLUID_IX(i, j+1)] - p[FLUID_IX(i, j-1)]);
      }
    }
        
    setBoundary(1, x);
    setBoundary(2, y);
  }
      
      
      
  protected void linearSolver(int b, float[] x, float[] x0, float a, float c) {
    for (int k = 0; k < _solverIterations; k++) {
      for (int i = 1; i <= _NX; i++) {
        for (int j = 1; j <= _NY; j++) {
          x[FLUID_IX(i, j)] = (a * ( x[FLUID_IX(i-1, j)] + x[FLUID_IX(i+1, j)]  +   x[FLUID_IX(i, j-1)] + x[FLUID_IX(i, j+1)])  +  x0[FLUID_IX(i, j)]) / c;
        }
      }
      setBoundary(b, x);
    }
  }
      
  //#define LINEAR_SOLVE_EQ  (x, x0)      (a * ( x[] + x[]  +  x[] + x[])  +  x0[]) / c;
      
  protected void linearSolverRGB(int bound, float a, float c) {
    int index1, index2, index3, index4, index5;
    for (int k = 0; k < _solverIterations; k++) {    // MEMO
      for (int i = 1; i <= _NX; i++) {
        for (int j = 1; j <= _NY; j++) {
          index5 = FLUID_IX(i, j);
          index1 = index5 - 1;//FLUID_IX(i-1, j);
          index2 = index5 + 1;//FLUID_IX(i+1, j);
          index3 = index5 - (_NX + 2);//FLUID_IX(i, j-1);
          index4 = index5 + (_NX + 2);//FLUID_IX(i, j+1);
              
          r[index5] = (a * ( r[index1] + r[index2]  +  r[index3] + r[index4])  +  rOld[index5]) / c;
          g[index5] = (a * ( g[index1] + g[index2]  +  g[index3] + g[index4])  +  gOld[index5]) / c;
          b[index5] = (a * ( b[index1] + b[index2]  +  b[index3] + b[index4])  +  bOld[index5]) / c;    
          //        x[FLUID_IX(i, j)] = (a * ( x[FLUID_IX(i-1, j)] + x[FLUID_IX(i+1, j)]  +  x[FLUID_IX(i, j-1)] + x[FLUID_IX(i, j+1)])  +  x0[FLUID_IX(i, j)]) / c;
        }
      }
      setBoundaryRGB(bound);
    }
  }
      
  protected void linearSolverUV(int bound, float a, float c) {
    int index1, index2, index3, index4, index5;
    for (int k = 0; k < _solverIterations; k++) {    // MEMO
      for (int i = 1; i <= _NX; i++) {
        for (int j = 1; j <= _NY; j++) {
          index5 = FLUID_IX(i, j);
          index1 = index5 - 1;//FLUID_IX(i-1, j);
          index2 = index5 + 1;//FLUID_IX(i+1, j);
          index3 = index5 - (_NX + 2);//FLUID_IX(i, j-1);
          index4 = index5 + (_NX + 2);//FLUID_IX(i, j+1);
              
          u[index5] = (a * ( u[index1] + u[index2]  +  u[index3] + u[index4])  +  uOld[index5]) / c;
          v[index5] = (a * ( v[index1] + v[index2]  +  v[index3] + v[index4])  +  vOld[index5]) / c;
          //        x[FLUID_IX(i, j)] = (a * ( x[FLUID_IX(i-1, j)] + x[FLUID_IX(i+1, j)]  +  x[FLUID_IX(i, j-1)] + x[FLUID_IX(i, j+1)])  +  x0[FLUID_IX(i, j)]) / c;
        }
      }
      setBoundaryRGB(bound);
    }
  }
      
      
      
  protected void setBoundary(int b, float[] x) {
    //return;
    for (int i = 1; i <= _NX; i++) {
      if(i<= _NY) {
        x[FLUID_IX(  0, i  )] = b == 1 ? -x[FLUID_IX(1, i)] : x[FLUID_IX(1, i)];
        x[FLUID_IX(_NX+1, i  )] = b == 1 ? -x[FLUID_IX(_NX, i)] : x[FLUID_IX(_NX, i)];
      }
          
      x[FLUID_IX(  i, 0  )] = b == 2 ? -x[FLUID_IX(i, 1)] : x[FLUID_IX(i, 1)];
      x[FLUID_IX(  i, _NY+1)] = b == 2 ? -x[FLUID_IX(i, _NY)] : x[FLUID_IX(i, _NY)];
    }
        
    x[FLUID_IX(  0,   0)] = 0.5f * (x[FLUID_IX(1, 0  )] + x[FLUID_IX(  0, 1)]);
    x[FLUID_IX(  0, _NY+1)] = 0.5f * (x[FLUID_IX(1, _NY+1)] + x[FLUID_IX(  0, _NY)]);
    x[FLUID_IX(_NX+1,   0)] = 0.5f * (x[FLUID_IX(_NX, 0  )] + x[FLUID_IX(_NX+1, 1)]);
    x[FLUID_IX(_NX+1, _NY+1)] = 0.5f * (x[FLUID_IX(_NX, _NY+1)] + x[FLUID_IX(_NX+1, _NY)]);
  }
      
    
  protected void setBoundaryRGB(int bound) {
    int index1, index2;
    for (int i = 1; i <= _NX; i++) {
      if(i<= _NY) {
        index1 = FLUID_IX(0, i);
        index2 = FLUID_IX(1, i);
        r[index1] = bound == 1 ? -r[index2] : r[index2];
        g[index1] = bound == 1 ? -g[index2] : g[index2];
        b[index1] = bound == 1 ? -b[index2] : b[index2];
            
        index1 = FLUID_IX(_NX+1, i );
        index2 = FLUID_IX(_NX, i);
        r[index1] = bound == 1 ? -r[index2] : r[index2];
        g[index1] = bound == 1 ? -g[index2] : g[index2];
        b[index1] = bound == 1 ? -b[index2] : b[index2];
      }
          
      index1 = FLUID_IX(i, 0);
      index2 = FLUID_IX(i, 1);
      r[index1] = bound == 2 ? -r[index2] : r[index2];
      g[index1] = bound == 2 ? -g[index2] : g[index2];
      b[index1] = bound == 2 ? -b[index2] : b[index2];
          
      index1 = FLUID_IX(i, _NY+1);
      index2 = FLUID_IX(i, _NY);
      r[index1] = bound == 2 ? -r[index2] : r[index2];
      g[index1] = bound == 2 ? -g[index2] : g[index2];
      b[index1] = bound == 2 ? -b[index2] : b[index2];
          
    }
        
    //  x[FLUID_IX(  0,   0)] = 0.5f * (x[FLUID_IX(1, 0  )] + x[FLUID_IX(  0, 1)]);
    //  x[FLUID_IX(  0, _NY+1)] = 0.5f * (x[FLUID_IX(1, _NY+1)] + x[FLUID_IX(  0, _NY)]);
    //  x[FLUID_IX(_NX+1,   0)] = 0.5f * (x[FLUID_IX(_NX, 0  )] + x[FLUID_IX(_NX+1, 1)]);
    //  x[FLUID_IX(_NX+1, _NY+1)] = 0.5f * (x[FLUID_IX(_NX, _NY+1)] + x[FLUID_IX(_NX+1, _NY)]);
        
  }
      
      
  protected void swapU() {
    _tmp = u;
    u = uOld;
    uOld = _tmp;
  }
  protected void swapV(){
  // These were #defines in c++ version
  protected int FLUID_IX(int i, int j)  { return ((i) + (_NX + 2)  *(j)); }
//  protected void ADD_SOURCE_UV()      { addSource(u, uOld);  addSource(v, vOld); }
//  protected void ADD_SOURCE_RGB()      { addSource(r, rOld);  addSource(g, gOld);  addSource(b, bOld); }
//  protected void DIFFUSE_RGB()      { diffuse(0, r, rOld, 0);  diffuse(0, g, gOld, 0);  diffuse(0, b, bOld, 0); }
//  protected void ADVECT_RGB()        { advect(0, r, rOld, u, v);  advect(0, g, gOld, u, v);  advect(0, b, bOld, u, v); }
//  protected void DIFFUSE_UV()        { diffuse(0, u, uOld, visc);  diffuse(0, v, vOld, visc); }
}    _tmp = v;
    v = vOld;
    vOld = _tmp;
  }
  protected void swapR(){
    _tmp = r;
    r = rOld;
    rOld = _tmp;
  }
      
  protected void swapRGB(){
    _tmp = r;
    r = rOld;
    rOld = _tmp;
        
    _tmp = g;
    g = gOld;
    gOld = _tmp;
        
    _tmp = b;
    b = bOld;
    bOld = _tmp;
  }
    
      
      
  protected float width;
  protected float height;
  protected float invWidth;
  protected float invHeight;
      
  protected int    _NX, _NY, _numCells;
  protected float  _invNX, _invNY, _invNumCells;
  protected float  _dt;
  protected boolean  _isInited;
  protected boolean  _isRGB;        // for monochrome, only update r
  protected int    _solverIterations;
      
  protected float  visc;
  protected float  _fadeSpeed;
      
  protected float[] _tmp;
      
  protected float  _avgDensity;      // this will hold the average color of the last frame (how full it is)
  protected float  uniformity;      // this will hold the uniformity of the last frame (how uniform the color is);
  protected float  _avgSpeed;
Leave a Comment