Commit ab6c6906 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add AreaDensityGridCountingProcessor

parent 758330c4
Pipeline #222736 passed with stages
in 130 minutes and 24 seconds
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimeGridOutputFile;
import java.util.Objects;
@OutputFileMap(outputFileClass = TimeGridOutputFile.class)
public class TimeGridKey implements DataKey<TimeGridKey> {
private final int timestep;
private final double x;
private final double y;
private final double size;
public TimeGridKey(int timestep, double x, double y, double size) {
this.timestep = timestep;
this.x = x;
this.y = y;
this.size = size;
}
public int getTimestep() {
return timestep;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getSize() {
return size;
}
public static String[] getHeaders() {
return new String[] { TimestepKey.getHeader(), "x", "y", "size"};
}
public String[] toStrings(){
return new String[]{Integer.toString(timestep), Double.toString(x), Double.toString(y), Double.toString(size)};
}
@Override
public int compareTo(@NotNull TimeGridKey other) {
int ret;
if ((ret = Integer.compare(timestep, other.timestep))==0){
if ((ret = Double.compare(x, other.x)) == 0){
if ((ret = Double.compare(y, other.y)) == 0){
if ((ret = Double.compare(size, other.size)) == 0){
return 0;
}
return ret;
}
return ret;
}
return ret;
}
return ret;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimeGridKey that = (TimeGridKey) o;
return timestep == that.timestep &&
Double.compare(that.x, x) == 0 &&
Double.compare(that.y, y) == 0 &&
Double.compare(that.size, size) == 0;
}
@Override
public int hashCode() {
return Objects.hash(timestep, x, y, size);
}
}
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.outputfiles.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimeGridKey;
@OutputFileClass(dataKeyMapping = TimeGridKey.class)
public class TimeGridOutputFile extends OutputFile<TimeGridKey> {
public TimeGridOutputFile(String... dataIndices) {
super(TimeGridKey.getHeaders());
}
public String[] toStrings(TimeGridKey key) {
return key.toStrings();
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.annotation.factories.dataprocessors.DataProcessorClass;
import org.vadere.simulator.control.simulation.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimeGridKey;
import org.vadere.state.attributes.processor.AttributesAreaDensityGridCountingProcessor;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.util.geometry.LinkedCellsGrid;
@DataProcessorClass(label = "AreaDensityGridCountingProcessor")
public class AreaDensityGridCountingProcessor extends DataProcessor<TimeGridKey, Integer> {
public AreaDensityGridCountingProcessor() {
super("gridCount");
setAttributes(new AttributesAreaDensityGridCountingProcessor());
}
@Override
protected void doUpdate(SimulationState state) {
int step = state.getStep();
AttributesAreaDensityGridCountingProcessor attr = (AttributesAreaDensityGridCountingProcessor)getAttributes();
LinkedCellsGrid<Pedestrian> cellsElements = new LinkedCellsGrid<Pedestrian>(
state.getTopography().getBounds().x,
state.getTopography().getBounds().y,
state.getTopography().getBounds().width,
state.getTopography().getBounds().height,
attr.getCellSize());
state.getTopography().getPedestrianDynamicElements().getElements().forEach(cellsElements::addObject);
int[][] count = cellsElements.getCellObjectCount();
int pedCount;
for (int r = 0; r < count.length; r++) {
for (int c = 0; c < count[r].length; c++) {
pedCount = count[r][c];
this.putValue(new TimeGridKey(step,r*attr.getCellSize(),c*attr.getCellSize(), attr.getCellSize()), pedCount);
}
}
}
@Override
public void init(ProcessorManager manager) {
super.init(manager);
}
}
package org.vadere.state.attributes.processor;
public class AttributesAreaDensityGridCountingProcessor extends AttributesProcessor {
double cellSize = 5.0;
public double getCellSize() {
return cellSize;
}
public void setCellSize(double cellSize) {
checkSealed();
this.cellSize = cellSize;
}
}
......@@ -202,6 +202,16 @@ public class LinkedCellsGrid<T extends PointPositioned> implements Iterable<T> {
return new int[] {iX, iY};
}
public int[][] getCellObjectCount(){
int[][] count = new int[this.gridSize[0]][this.gridSize[1]];
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
count[r][c] = grid[r][c].objects.size();
}
}
return count;
}
/**
* Adds a given object to the grid at position of the object. The position is
* discretized automatically to fit in the cells.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment