Skip to content

Instantly share code, notes, and snippets.

@daemonfire300
Created January 31, 2015 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daemonfire300/b0043b8032f09535ce56 to your computer and use it in GitHub Desktop.
Save daemonfire300/b0043b8032f09535ce56 to your computer and use it in GitHub Desktop.
class Cell
{
Cell( PVector ul, PVector ur, PVector lr, PVector ll,
float vul, float vur, float vlr, float vll,
PVector v2Dvul, PVector v2Dvur, PVector v2Dvlr, PVector v2Dvll)
{
upperLeft = ul;
upperRight = ur;
lowerRight = lr;
lowerLeft = ll;
valueUpperLeft = vul;
valueUpperRight = vur;
valueLowerRight = vlr;
valueLowerLeft = vll;
v2D_valueUpperLeft = v2Dvul;
v2D_valueUpperRight = v2Dvur;
v2D_valueLowerRight = v2Dvlr;
v2D_valueLowerLeft = v2Dvll;
}
float width()
{
return PVector.sub(upperLeft,upperRight).mag();
}
float height()
{
return PVector.sub(upperLeft,lowerLeft).mag();
}
PVector upperLeft;
PVector upperRight;
PVector lowerRight;
PVector lowerLeft;
float valueUpperLeft;
float valueUpperRight;
float valueLowerRight;
float valueLowerLeft;
PVector v2D_valueUpperLeft;
PVector v2D_valueUpperRight;
PVector v2D_valueLowerRight;
PVector v2D_valueLowerLeft;
}
class Grid
{
Grid( Cell[] cells, int dimX, int dimY, float minScalar, float maxScalar, float minX, float maxX, float minY, float maxY , float minV, float maxV)
{
this.cells = cells;
this.dimX = dimX;
this.dimY = dimY;
this.minScalar = minScalar;
this.maxScalar = maxScalar;
this.minVector = minV;
this.maxVector = maxV;
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
}
Cell[] cells;
int dimX;
int dimY;
float minScalar;
float maxScalar;
float minVector;
float maxVector;
float minX; float maxX;
float minY; float maxY;
}
Grid grid;
Grid parseScalarData( String filename, String niederschlag )
{
String[] lines = loadStrings( filename );
String[] lines_ns = loadStrings( niederschlag );
int numX = 0;
int numY = 0;
int numScalars = 0;
int numVectors = 0;
float[] xCoordinates = null;
float[] yCoordinates = null;
float[] scalars = null;
PVector[] vectors = null;
/* -------------------- LOAD niederschlag data -------------------------------- */
for( int i = 0; i < lines_ns.length; i++ )
{
String[] m_ns = null;
if( ( m_ns = match( lines_ns[i], "X_COORDINATES ([0-9]+) (.+)") ) != null )
{
numX = Integer.parseInt( m_ns[1] );
String[] sCoordinates = splitTokens( lines_ns[i+1] );
xCoordinates = new float[ numX ];
for( int j = 0; j < sCoordinates.length; j++ )
xCoordinates[j] = Float.parseFloat( sCoordinates[j] );
++i;
}
else if( ( m_ns = match( lines_ns[i], "Y_COORDINATES ([0-9]+) (.+)") ) != null )
{
numY = Integer.parseInt( m_ns[1] );
String[] sCoordinates = splitTokens( lines_ns[i+1] );
yCoordinates = new float[ numY ];
for( int j = 0; j < sCoordinates.length; j++ )
yCoordinates[j] = Float.parseFloat( sCoordinates[j] );
++i;
}
else if( ( m_ns = match( lines_ns[i], "POINT_DATA ([0-9]+)") ) != null )
{
numScalars = Integer.parseInt( m_ns[1] );
//numVectors = Integer.parseInt( m[1] );
scalars = new float[ numScalars ];
//vectors = new PVector[ numVectors ];
float tmpx;
float tmpy;
float tmpz;
String[] tmp_xyz = null;
for( int j = i+3; j < numScalars; j++ )
{
scalars[j-i-3] = Float.parseFloat( lines_ns[j] );
}
}
}
/* ---------------------------------------------------- */
float minVector = 1000000000;
float maxVector = -1000000000;
for( int i = 0; i < lines.length; i++ )
{
String[] m = null;
if( ( m = match( lines[i], "X_COORDINATES ([0-9]+) (.+)") ) != null )
{
numX = Integer.parseInt( m[1] );
String[] sCoordinates = splitTokens( lines[i+1] );
xCoordinates = new float[ numX ];
for( int j = 0; j < sCoordinates.length; j++ )
xCoordinates[j] = Float.parseFloat( sCoordinates[j] );
++i;
}
else if( ( m = match( lines[i], "Y_COORDINATES ([0-9]+) (.+)") ) != null )
{
numY = Integer.parseInt( m[1] );
String[] sCoordinates = splitTokens( lines[i+1] );
yCoordinates = new float[ numY ];
for( int j = 0; j < sCoordinates.length; j++ )
yCoordinates[j] = Float.parseFloat( sCoordinates[j] );
++i;
}
else if( ( m = match( lines[i], "POINT_DATA ([0-9]+)") ) != null )
{
//numScalars = Integer.parseInt( m[1] );
numVectors = Integer.parseInt( m[1] );
//scalars = new float[ numScalars ];
vectors = new PVector[ numVectors ];
float tmpx;
float tmpy;
float tmpz;
String[] tmp_xyz = null;
print("Vlen ", vectors.length);
for( int j = i+3; j < numVectors; j++ )
{
//scalars[j-i-3] = Float.parseFloat( lines[j] );
tmp_xyz = splitTokens( lines[j] );
tmpx = Float.parseFloat(tmp_xyz[0]);
tmpy = Float.parseFloat(tmp_xyz[1]);
tmpz = Float.parseFloat(tmp_xyz[2]); // always 0 since 2D
vectors[j-i-3] = new PVector(tmpx, tmpy, tmpz);//Float.parseFloat( lines[j] );
if(minVector > vectors[j-i-3].mag()){
minVector = vectors[j-i-3].mag();
}
if(maxVector < vectors[j-i-3].mag()){
maxVector = vectors[j-i-3].mag();
}
}
}
}
print("Max Vector:", maxVector);
print("Min Vector:", minVector);
float minScalar = min(scalars);
float maxScalar = max(scalars);
println("Parsed " + xCoordinates.length + " x coordinates");
println("Parsed " + yCoordinates.length + " y coordinates" );
println("Parsed " + vectors.length + " vectors" );
println("Parsed " + scalars.length + " scalars" );
int dimX = numX-1;
int dimY = numY-1;
Cell[] cells = new Cell[dimX * dimY];
for( int j = 0; j < dimY; j++ )
{
for( int i = 0; i < dimX; i++ )
{
float x0 = xCoordinates[i];
float x1 = xCoordinates[i+1];
float y0 = yCoordinates[j];
float y1 = yCoordinates[j+1];
float vul = scalars[j*numX + i];// niederschlag daten hier, scalars[j*numX + i];
float vur = scalars[j*numX + i+1];// niederschlag daten hier, scalars[j*numX + i+1];
float vlr = scalars[(j+1)*numX + i+1];// niederschlag daten hier, scalars[(j+1)*numX + i+1];
float vll = scalars[(j+1)*numX + i];// niederschlag daten hier, scalars[(j+1)*numX + i];
PVector v2Dvul = vectors[j*numX + i];
PVector v2Dvur = vectors[j*numX + i+1];
PVector v2Dvlr = vectors[(j+1)*numX + i+1];
PVector v2Dvll = vectors[(j+1)*numX + i];
PVector ul = new PVector( x0, y0 );
PVector ur = new PVector( x1, y0 );
PVector lr = new PVector( x1, y1 );
PVector ll = new PVector( x0, y1 );
cells[j*dimX+i] = new Cell(ul, ur, lr, ll, vul, vur, vlr, vll, v2Dvul, v2Dvur, v2Dvlr, v2Dvll);
}
}
return new Grid( cells, dimX, dimY,
min(scalars), max(scalars),
min(xCoordinates), max(xCoordinates),
min(yCoordinates), max(yCoordinates), minVector, maxVector );
}
void setup()
{
//grid = parseScalarData("niederschlag.vtk");
grid = parseScalarData("wind.vtk", "niederschlag.vtk");
int a = 4;
size(a*int(grid.maxX-grid.minX), a*int(grid.maxY-grid.minY));
color from = color(239,243,255);
color to = color( 8, 69,148);
pushStyle();
for( int i = 0; i < grid.cells.length; i++ )
{
Cell cell = grid.cells[i];
float s = (cell.valueUpperLeft - grid.minScalar)/(grid.maxScalar - grid.minScalar);
float x = cell.upperLeft.x - grid.minX;
float y = (grid.maxY - grid.minY ) - (cell.upperLeft.y - grid.minY);
color c = lerpColor(from, to, s );
fill(c);
stroke(c);
rect( a*x, a*y, a*cell.width(), a*cell.height() );
}
popStyle();
}
void draw()
{
// cells in x direction size
int xs = width / grid.dimX;
int ys = height / grid.dimY;
// cells in y direction size
if(mousePressed){
int mx = int(map(mouseX, 0, width, 0, grid.dimX));
int my = int(map(mouseY, 0, height, 0, grid.dimY));
print("Current Position:", width, height, "X", mouseX, "Y", mouseY, "XS", xs, "YS", ys, "\n");
print("Current Cell:", grid.cells.length, (my-7/7)*grid.dimX+(mx/7), "\n\n"); // grid.cells.length, grid.cells[(mouseY-7/7)*grid.dimX+(mouseX/7)],
//print("Current Cell:", (mouseY/7)*grid.dimX+(mouseX/7));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment