Friday, September 7, 2007

Hemostasis:(processing scripts)

Click these links to view the interactive applet:
Flocking Version (NEW)
Hemostasis V1
HemoStasis V2





Hemostasis:(processing scripts)

These two processing scripts were written by Francis Bitonti and Brian Osborn

Click these links to view the interactive applet:

Hemostasis V1
HemoStasis V2

<
/*
written by Francis Bitonti and Brian Osborn
Latest Update: 09102007
*/

//We will borrow a pre-built particle system from Traer physics library and the will use the pdf library for export
import traer.physics.*;
import processing.pdf.*;

//setup for pdf recording functions (snapshot on key command)
boolean recording;
PGraphicsPDF pdf;

//positons for lines
float[] posA;
float[] posB;

//we will use this variable to determine the size of the opening of the cut
float[] heal;
//we will use this variable to determine the attraction (collagen level) of the opening of the cut
float[] col;

//Set "cut" particles as attractor
Particle[] cut;

//set "platelet" particles as array
Particle[] platelets;

//set "redBloodCells" particles as array
Particle[] redBloodCells;

Attraction[][] pull;

//Establish number of particles in each system
ParticleSystem physics;
int gridSizePlatelet = 100;
int gridSizeCut = 20;
int gridSizeRedBloodCell = 100;
float[] x;
float[] y,yA;

void setup(){
size(600,300);

background(255);
frameRate(60);
smooth();
ellipseMode(CENTER);
//noCursor();

//declare floats x,y, and yA
x = new float[gridSizePlatelet];
y = new float[gridSizePlatelet];
yA = new float[gridSizePlatelet];

heal = new float[gridSizeCut];
col = new float[gridSizeCut];

for(int i= 0; i < gridSizeCut; i++){

heal[i] = 10;
col[i] = 10000;
}

posA = new float[gridSizePlatelet];
posB = new float[gridSizePlatelet];

for (int plateletNumber = 0; plateletNumber x[plateletNumber] = random(0,width);
y[plateletNumber] = random(0,height);
yA[plateletNumber] = random((height/2)-100,(height/2)+100);
}

//call physics library and set up the cut, platelet, and redBloodCells particles systems
physics = new ParticleSystem(0,1);
pull = new Attraction[gridSizeCut][gridSizePlatelet];
cut = new Particle[gridSizeCut];

for(int cutNumber = 0; cutNumber < gridSizeCut; cutNumber ++){
cut[cutNumber] = physics.makeParticle();
cut[cutNumber].makeFixed();
}

platelets = new Particle[gridSizePlatelet];
redBloodCells = new Particle[gridSizeRedBloodCell];

// draw platelets (set mass)
for(int i = 0; i < gridSizePlatelet; i++){
platelets[i] = physics.makeParticle(1,x[i],y[i],0.0);
}

for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
//this is an important setting
pull[cutAttraction][plateletAttraction] = physics.makeAttraction(cut[cutAttraction], platelets[plateletAttraction], 10000, 40);
}
}

for (int wound = 0; wound < gridSizePlatelet; wound ++){
for (int woundSub = 0; woundSub < gridSizePlatelet; woundSub ++){
physics.makeAttraction(platelets[wound],platelets[woundSub],-50,1);
}
}
}

void draw(){
for(int lineN = 0; lineN < gridSizePlatelet; lineN++){
posA[lineN] = platelets[lineN].position().x();
posB[lineN] = platelets[lineN].position().y();
}

//controls the fade
stroke(0,0,0);
//controles the fade
fill(255,255,255,5);
rect(0,0,width,height);

physics.tick();
for(int lineN = 0; lineN < gridSizePlatelet; lineN++){
if(platelets[lineN].velocity().x() > 10 || platelets[lineN].velocity().y() > 10){
stroke (236,0,140);
line(platelets[lineN].position().x(),platelets[lineN].position().y(),posA[lineN],posB[lineN]);
}
}

stroke(0,0,0,128);
fill(0,0,0,128);

//draw ellipses at platelets
for(int j = 0; j < gridSizePlatelet; j++){
ellipse( platelets[j].position().x(),platelets[j].position().y(), .7, .7);
}

//draw ellipses at red blood cells
for(int k = 0; k < gridSizeRedBloodCell; k++){
if(platelets[k].velocity().x() > 10 || platelets[k].velocity().y() > 10){
fill(236,0,140);
ellipse( platelets[k].position().x(),platelets[k].position().y(), 3, 3);
}
}

//find out if the cuts are being healed
for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
if(platelets[plateletAttraction].position().x() < cut[cutAttraction].position().x() + 50 && platelets[plateletAttraction].position().y() < cut[cutAttraction].position().y() + 50 && platelets[plateletAttraction].position().x() > cut[cutAttraction].position().x() - 50 && platelets[plateletAttraction].position().y() > cut[cutAttraction].position().y() - 50){
heal[cutAttraction] = heal[cutAttraction] - 0.001;
}
if(heal[cutAttraction] < 0){
heal[cutAttraction] = 0;
for (int plateletAttraction2 = 0; plateletAttraction2 < gridSizePlatelet; plateletAttraction2 ++){
pull[cutAttraction][plateletAttraction2].setMinimumDistance(0);
pull[cutAttraction][plateletAttraction2].setStrength(0);
}
}
}

//draw the cut ellipses; size and color based on state in healing process
for (int cutLocation = 0; cutLocation < gridSizeCut; cutLocation ++){
cut[cutLocation].moveTo(x[cutLocation], yA[cutLocation], 0);
stroke (400-(25.5*heal[cutLocation]),0,140);
fill(255);
ellipse(cut[cutLocation].position().x(), cut[cutLocation].position().y(), heal[cutLocation], heal[cutLocation]);
}
}
}

//this is for relocating each individual cut in real time
void keyPressed() {
if (key == '0') {
x[0] = mouseX;
yA[0] = mouseY;
}
if (key == '1') {
x[1] = mouseX;
yA[1] = mouseY;
}
if (key == '2') {
x[2] = mouseX;
yA[2] = mouseY;
}
if (key == '3') {
x[3] = mouseX;
yA[3] = mouseY;
}
if (key == '4') {
x[4] = mouseX;
yA[4] = mouseY;
}
if (key == '5') {
x[5] = mouseX;
yA[5] = mouseY;
}
if (key == '6') {
x[6] = mouseX;
yA[6] = mouseY;
}
if (key == '7') {
x[7] = mouseX;
yA[7] = mouseY;
}
if (key == '8') {
x[8] = mouseX;
yA[8] = mouseY;
}
if (key == '9') {
x[9] = mouseX;
yA[9] = mouseY;
}
if (key == '0' && key == 'q') {
x[10] = mouseX;
yA[10] = mouseY;
}
if (key == '1' && key == 'q') {
x[11] = mouseX;
yA[11] = mouseY;
}
if (key == '2' && key == 'q') {
x[12] = mouseX;
yA[12] = mouseY;
}
if (key == '3' && key == 'q') {
x[13] = mouseX;
yA[13] = mouseY;
}
if (key == '4' && key == 'q') {
x[14] = mouseX;
yA[14] = mouseY;
}
if (key == '5' && key == 'q') {
x[15] = mouseX;
yA[15] = mouseY;
}
if (key == '6' && key == 'q') {
x[16] = mouseX;
yA[16] = mouseY;
}
if (key == '7' && key == 'q') {
x[17] = mouseX;
yA[17] = mouseY;
}
if (key == '8' && key == 'q') {
x[18] = mouseX;
yA[18] = mouseY;
}
if (key == '9' && key == 'q') {
x[19] = mouseX;
yA[19] = mouseY;
}
if (key == 'w') {
stroke(0,0,0);
fill(255,255,255,255);
rect(0,0,width,height);
}
if (key == 'e'){
// draw platelets
for(int i = 0; i < gridSizePlatelet; i++){
platelets[i] = physics.makeParticle(1,x[i],y[i],0.0);
}

for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
//this is an important setting
physics.makeAttraction(cut[cutAttraction], platelets[plateletAttraction], 10000, 40);
}
}
for (int wound = 0; wound < gridSizePlatelet; wound ++){
for (int woundSub = 0; woundSub < gridSizePlatelet; woundSub ++){
physics.makeAttraction(platelets[wound],platelets[woundSub],-50,1);
}
}
}

//This is for the record on keypress function
if (key == 'a') {
if (recording) {
endRecord();
println("Recording stopped.");
recording = false;
}
else {
beginRecord(PDF, "frame-####.pdf");
println("Recording started.");
recording = true;
}
}
else if (key == 's') {
if (recording) {
endRecord();
}
exit();
}

if (key == 'z') {
// pull[0].setMinimumDistance(500);
// pull[1].setMinimumDistance(500);
// pull[2].setMinimumDistance(500);
println("reset Distance");

println(physics.getAttraction(1));

for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
pull[cutAttraction][plateletAttraction].setStrength(-1000);
}
}
}

if (key == 'x') {
println("reset Distance");
println(physics.getAttraction(1));
for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
pull[cutAttraction][plateletAttraction].setStrength(10000);
}
}
}

if (key == 'c') {
println("reset Distance");
println(physics.getAttraction(1));

for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
pull[cutAttraction][plateletAttraction].setMinimumDistance(80);
}
}
}

if (key == 'v') {
println("reset Distance");
println(physics.getAttraction(1));
for (int cutAttraction = 0; cutAttraction < gridSizeCut; cutAttraction ++){
for (int plateletAttraction = 0; plateletAttraction < gridSizePlatelet; plateletAttraction ++){
pull[cutAttraction][plateletAttraction].setMinimumDistance(40);
}
}
}

if (key == 'r') {
// draw platelets (set mass)
for(int i = 0; i < gridSizePlatelet; i++){
platelets[i].moveTo(x[i],y[i],0.0);
}
}
}