Skip to content

Commit

Permalink
bacterial cloud prototype
Browse files Browse the repository at this point in the history
testing the representation of bacteria as clouds of 'process', to work
with compounds as clouds of 'stuff'
  • Loading branch information
NelisDrost committed Feb 24, 2015
1 parent 1a03612 commit 2a04c65
Show file tree
Hide file tree
Showing 11 changed files with 573 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ $tf*/
# Compiled Python
*.pyc

# Compiled Processing
application.*/
web-export/

# CMake/build directory
[Bb]uild/
build-win/
Expand Down
26 changes: 26 additions & 0 deletions bacterial_cloud/Agent.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
//class Agent {
//
// PVector location;
//
//
//
// void display() {
// //fill(0,150);
// //stroke(0);
// //ellipse(location.x,location.y,r,r);
// float theta = velocity.heading() + PI/2;
// fill(200, 100);
// stroke(0);
// pushMatrix();
// translate(location.x, location.y);
// rotate(theta);
// beginShape(TRIANGLES);
// vertex(0, -r*2);
// vertex(-r, r*2);
// vertex(r, r*2);
// endShape();
// popMatrix();
// }
//}

137 changes: 137 additions & 0 deletions bacterial_cloud/Arr2d.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Class for 2d arrays, including element wise math

class Arr2d
{
float[][] data;
int _xs, _ys;

Arr2d(int xs, int ys)
{
this(xs,ys,0);
}

Arr2d(int xs, int ys, float val)
{
_xs = xs; _ys = ys;
data = new float[_xs][_ys];
Set(val);
}

Arr2d(float[][] val)
{
_xs = val.length;
_ys = val[0].length;
data = new float[_xs][_ys];
Set(val);
}

Arr2d(Arr2d clone)
{
this(clone.data);
// data = new float[clone._xs][clone._ys];
// Set(clone);
}

void Set(float val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] = val;
}

void Set(float[][] val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] = val[x][y];

}

void Set(Arr2d val)
{ Set(val.data); }

void Mult(float val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] *= val;
}

void Mult(Arr2d val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] *= val.data[x][y];
}

void Add(float val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] += val;
}

void Add(Arr2d val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] += val.data[x][y];
}

void Sub(float val)
{ Add(-val); }

void Sub(Arr2d val)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] -= val.data[x][y];

}

void Conv(Arr2d mask)
{
int mx = mask._xs;
int my = mask._ys;
int mcx = mx/2;
int mcy = my/2;

float[][] out = new float[_xs][_ys];
for (int x = 0; x < _xs; x++)
for (int y = 0; y < _ys; y++)
out[x][y] = 0;

for (int x = 0; x < _xs; x++)
for (int y = 0; y < _ys; y++)
for (int i = 0 - mcx; i < (mx - mcx); i++)
for (int j = 0 - mcy; j < (my - mcy); j++)
{
if ((x+i) < 0 || (x+i) >= _xs || (y+j) < 0 || (y+j) >= _ys)
continue;
else
{
out[x][y] += data[x + i][y + j] * mask.data[i+mcx][j+mcy];
}
}
data = out;
}

float Sum()
{
float total = 0;
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
total += data[x][y];

return total;
}

void Clamp(float min, float max)
{
for(int x = 0; x < _xs; x++)
for(int y = 0; y < _ys; y++)
data[x][y] = constrain(data[x][y], min, max);
}
}


107 changes: 107 additions & 0 deletions bacterial_cloud/Bacteria.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Includes

int time = 0;

// Grid setup
int xs = 50;
int ys = 50;
int w = 8;

Vehicle[] agents;
boolean debug = true;

Arr2d mask;
GridLayers compounds;
GridLayers bacteria;

void setup(){
colorMode(RGB, 1);

size(xs*w,ys*w);

agents = new Vehicle[3];
for(int i = 0; i < 3; i++)
agents[i] = new Vehicle(w * xs/2, (w * ys) * (i + 1) / 4);

mask = new Arr2d(new float[][]{{0.707,1,0.707},{1,0,1},{0.707,1,0.707}});
mask.Mult(0.01);
mask.data[1][1] = -mask.Sum();

compounds = new GridLayers();
compounds.Add(new Grid(xs,ys,w), color(1,0,0));
compounds.Add(new Grid(xs,ys,w), color(0,1,0));
compounds.Add(new Grid(xs,ys,w), color(0,0,1));

bacteria = new GridLayers();
bacteria.Add(new Grid(xs,ys,w), color(0,1,1));

// PrintArr2d(mask.data);
// Arr2d co = new Arr2d(mask.data);
// PrintArr2d(co.data);
// co.data[1][1] = 5;
// PrintArr2d(co.data);
// PrintArr2d(mask.data);

// mask.Mult(mask);
// print("\n");
// PrintArr2d(mask.data);
}

int i = 0;

void draw(){
if(millis() > time)
{ // update at most 20 times per second
// time = millis() + 50;
background(0);

// Diffuse and display compounds
compounds.Diffuse(mask);
compounds.Display();

// React, diffuse, and display bacteria
bacteria.get(0).data.data[xs/4][ys/2] = 1; // Seed population
bacteria.get(0).React(compounds.get(0), compounds.get(1), compounds.get(2));
bacteria.Diffuse(mask);
bacteria.DisplayEdges();

// Arr2d temp = new Arr2d(compounds.get(0).data);
//// temp.Add(compounds.get(1).data);
// Grid g = new Grid(temp, w);
// g.DisplayEdges(color(1,1,1));

// Move and display agents
for(int i = 0; i < 3; i++)
{
agents[i].wander();
agents[i].run();

// Agents leak compounds
compounds.get(i).AddAt(agents[i].location, 1);
if( i < 2)
compounds.get(2).AddAt(agents[i].location, -1);
}

// Test gradient at current mouse position
PVector m = new PVector(mouseX, mouseY);
PVector g = compounds.get(2).GradAt(m);

noFill();
stroke(1);
line(m.x,m.y,m.x + (5* g.x), m.y + (5 * g.y));
}
}

void PrintArr2d(float[][] data)
{
for(int i = 0; i < data.length; i++)
{
print("\n");
for(int j = 0; j < data[i].length; j++)
if(j == 0)
print(data[i][j]);
else
print(", ", data[i][j]);
}
}

9 changes: 9 additions & 0 deletions bacterial_cloud/Color.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Color
{
public color data;

Color(int c)
{
data = c;
}
}
Loading

0 comments on commit 2a04c65

Please sign in to comment.