import controlP5.*; //loads slider library
ControlP5 controlP5;
PGraphics graph; //for displaying graph of remaining pedestrians and time in separate window
PGraphics window; //for displaying simulation in separate window
PGraphics values; //for displaying chosen variables in separate window
PImage floorPlan; /*for loading floor plan image:
pixels with brightness <=50 are walls,
pixels with green >=150 and red <=150 and blue <=150 are doors,
all others are space of rooms,*/
/********naming classes***************************************************************/
Pedestrian newPedestrian; //Pedestrian class
ArrayList pedestrianList; //List with all pedestrians
ArrayList graphList; //will contain values of graph, graphList.size() is current time frame
ArrayList doorList; //List with all doors
DynamicField dField; //field with traces of pedestrians
EdgeField eField; //field for reducing probability to walk toward corners and edges
ObstacleField oField; //field with probabilities zero for walls
PedestrianField pField; //field with probabilities zero for occupied cells
StaticField sField; //field with values for distance to door
/********variables*******************************************************************/
boolean simulationRun =false; //starts simulation if true,
boolean showDField =true; //displays dynamic field if true,
boolean showEField=false; //displays edge field if true
int gridColumns;
int gridLines;
int cellSize=4;
float density=0.2;
float weightD=0.5;
float weightE=0.3;
float weightS=0.5;
float diffusion=0.5; //range from 0 to 6
float decay=3; //range from 0 to 6
int windowPositionColumn;
int windowPositionLine;
int amount;
/********setup**************************************************************************************************/
void setup()
{
createSliders();
floorPlan = loadImage("GR_4.html"); //loading and reading floor plan
image(floorPlan, 0, 0);
loadPixels();
gridColumns=floorPlan.width;
gridLines=floorPlan.height;
windowPositionColumn=180;
windowPositionLine=(400-gridLines*cellSize);
dField=new DynamicField (gridColumns,gridLines); //creating fields
oField=new ObstacleField (gridColumns,gridLines,floorPlan);
eField=new EdgeField (gridColumns,gridLines,oField);
pField=new PedestrianField(gridColumns,gridLines);
createDoorList();
sField=new StaticField (gridColumns,gridLines,doorList,oField);
graphList= new ArrayList();
background(255,255,255); //drawing initial window,graph and values
window = createGraphics(gridColumns*cellSize,gridLines*cellSize, JAVA2D);
graph = createGraphics(720,130, JAVA2D);
values = createGraphics(160,130, JAVA2D);
PFont font = loadFont("SempliceRegular-8.vlw");
drawInitialGraph(font);
values.beginDraw();
values.textFont(font, 8);
values.endDraw();
window.beginDraw();
displayStaticField();
displayObstacleField();
window.endDraw();
image(window,windowPositionColumn,windowPositionLine);
}
/********draw**********************************************************************************************************/
void draw()
{
if (simulationRun==true) //true when start button clicked
{
if(pedestrianList.size()!=0 && graphList.size()<=695) //checks if all pedestrians evacuated or max time reached
{
/********simulation update in "window"************************************************/
image(window,windowPositionColumn,windowPositionLine);
window.beginDraw();
movePedestrianList();
if(showDField==true)
{
displayDynamicField();
}
if(showEField==true)
{
displayEdgeField();
}
displayPedestrianList();
window.endDraw();
/********graph update in "graph"*************************************************************/
GraphValue newgraphValue = new GraphValue(pedestrianList.size(),amount);
graphList.add(newgraphValue);
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
graph.beginDraw();
graph.stroke(0);
graph.line(25,105,graphList.size()+25,105);
graph.stroke(255);
graph.fill(255);
graph.rect(graphList.size()-70, 108, 100,12);
graph.fill(0);
graph.textAlign(RIGHT);
graph.text(nf.format(graphList.size()*0.3/60)+" MINUTES",graphList.size()+25,117);
displayGraph();
graph.endDraw();
image(graph,windowPositionColumn-25,420);
showValues();/********value display in "values"********************************/
} //end of timeout check
/********save image when finished **********************************************/
if(pedestrianList.size()==0 || graphList.size()>=695)
{
save("den"+density+"wS"+weightS+"wD"+weightD+"dif"+diffusion+"time"+graphList.size()+".jpg");
}
}//end of check "ifSimulationRun"
} //end of check draw
/*****************Methods***********************************************************************
void controlEvent(ControlEvent theEvent)
void drawInitialGraph()
void showValues()
void creadeSliders()
void displayPedestrianList()
void movePedestrianList()
void createRandomPedestrianList()
void createDoorList()
void displayObstacleField()
void displayStaticField()
void displayDynamicField()
void displayEdgeField()
void displayGraph()
*/
void controlEvent(ControlEvent theEvent)/***************************************************/
{
if (simulationRun==false)
{
switch(theEvent.controller().id())
{
case(1):
density = (float)(theEvent.controller().value());
density =density/100;
break;
case(2):
weightS = (float)(theEvent.controller().value());
break;
case(3):
weightD = (float)(theEvent.controller().value());
break;
case(4):
diffusion = (float)(theEvent.controller().value());
break;
case(5):
simulationRun=true;
createRandomPedestrianList();
break;
}
}
switch(theEvent.controller().id())
{
case(6):
if(showDField==true) showDField=false;
else showDField=true;
break;
case(7):
if(showEField==true) showEField=false;
else showEField=true;
break;
}
}
void drawInitialGraph(PFont font) //**************************************************************
{
graph.beginDraw();
graph.stroke(0);
graph.fill(0);
graph.textFont(font, 8);
graph.text("100%",0,9);
graph.text("80%", 4,29);
graph.text("60%", 4,49);
graph.text("40%", 4,69);
graph.text("20%", 4,89);
textFont(font, 8);
text(gridColumns*0.4+" x "+gridLines*0.4+" METER",185+gridColumns*cellSize,400);
graph.line(25,0,25,105);
graph.endDraw();
}
void showValues()
{
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
values.beginDraw();
values.fill(0);
values.textAlign(RIGHT);
values.text("PEDESTRIAN NUMBER = " +amount,130,45);
values.text("DENSITY = "+density,130,60);
values.text("STRENGTH STATICFIELD = " +nf.format(weightS),130,75);
values.text("STRENGTH DYNAMICFIELD = "+nf.format(weightD),130,90);
values.text("BLUR DYNAMICFIELD = "+nf.format(diffusion),130,105);
values.endDraw();
image(values,windowPositionColumn-160,420);
}
void createSliders()//*****************initializing sliders from imported library****************
{
size(950,550);
controlP5 = new ControlP5(this);
controlP5.setColorBackground(120);
controlP5.setColorForeground(180);
controlP5.setColorLabel(0);
controlP5.setColorValue(50);
controlP5.setColorActive(180);
controlP5.addSlider("pedestrian density%" ,0,50,20,20,335,50,7).setId(1);
controlP5.addSlider("sense of orientation" ,0,1,0.5,20,355,50,7).setId(2);
controlP5.addSlider("herd behavior" ,0,1,0.5,20,375,50,7).setId(3);
controlP5.addSlider("trace diffusion",0,6,3,20,395,50,7).setId(4);
controlP5.addButton("start",20,20,300,30,10).setId(5);
controlP5.addButton("trace",20,55,300,30,10).setId(6);
controlP5.addButton("edges",20,90,300,30,10).setId(7);
}
void displayPedestrianList()/*******************************************/
{
for (int i=0; i 300)
{
println("more than 400 agents!!");
amount = 300;
density = 300/(gridColumns*gridLines);
}
if (amount==0)
{
amount = 1;
}
for (int i=0; i< amount; i++)
{
int flag = 0; //flag for successful setting of new pedestrian
while (flag == 0)
{
int columnPos =(int)random(1,(gridColumns-1));
int linePos =(int)random(1,(gridLines-1));
if ((pField.isOccupied(columnPos, linePos)==false) && (oField.isObstacle(columnPos, linePos)==false)) //if cell unoccupied and no obstacle
{
Pedestrian newPedestrian= new Pedestrian(columnPos, linePos, pedestrianList, doorList, dField, oField, pField, sField,weightD,weightE,weightS);
pField.placePed(newPedestrian.getX(),newPedestrian.getY());
pedestrianList.add(newPedestrian);
flag = 1;
}
}
}
}
void createDoorList()/*******************************************/
{
doorList = new ArrayList();
for(int x=0;x=150 &&
red(pixels[y*width+x])<=150&&
blue(pixels[y*width+x])<=150)
{
Door newDoor= new Door(x,y,oField);
doorList.add(newDoor);
}
}
}
}
void displayObstacleField()/*******************************************/
{
for(int i=0; i