Commit dc10c021 authored by Maxim Dudin's avatar Maxim Dudin
Browse files

added classes-structure for the PedestiansNearbyProcessor.java

parent 85f5d96d
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.jetbrains.annotations.NotNull;
import org.vadere.state.scenario.DynamicElement;
public class PedestiansNearbyData {
public static final PedestiansNearbyData noOverLap = new PedestiansNearbyData();
private final int ped1Id;
private final int ped2Id;
private final Double dist;
private final Double overlap;
private PedestiansNearbyData() {
this.ped1Id = -1;
this.ped2Id = -1;
this.dist = Double.POSITIVE_INFINITY;
this.overlap = 0.0;
}
public PedestiansNearbyData(final DynamicElement ped1, final DynamicElement ped2, final double minDist){
this.ped1Id = ped1.getId();
this.ped2Id = ped2.getId();
this.dist = ped1.getPosition().distance(ped2.getPosition());
this.overlap = minDist - dist;
}
public boolean isOverlap(){
return overlap > 0;
}
public boolean isNotSelfOverlap(){
return !(ped1Id == ped2Id);
}
public int getPed1Id() {
return ped1Id;
}
public int getPed2Id() {
return ped2Id;
}
public Double getOverlap() {
return overlap;
}
public String[] toStrings(){
return new String[]{Double.toString(dist), Double.toString(overlap)};
}
public int maxDist(@NotNull PedestiansNearbyData o) {
return Double.compare(overlap, o.getOverlap());
}
}
package org.vadere.simulator.projects.dataprocessing.datakey;
import org.jetbrains.annotations.NotNull;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimestepPedestrianIdOverlapOutputFile;
import org.vadere.simulator.projects.dataprocessing.outputfile.TimestepPedestriansNearbyIdOutputFile;
import java.util.Objects;
@OutputFileMap(outputFileClass = TimestepPedestriansNearbyIdOutputFile.class)
public class TimestepPedestriansNearbyIdKey implements DataKey<TimestepPedestriansNearbyIdKey> {
private final int timeStep;
private final int pedId1; //smaller id
private final int pedId2; //bigger id
public TimestepPedestriansNearbyIdKey(int timeStep, int pedA, int pedB) {
this.timeStep = timeStep;
this.pedId1 = (pedA <= pedB) ? pedA : pedB;
this.pedId2 = (pedA > pedB) ? pedA : pedB;
}
public static String[] getHeaders(){
return new String[]{TimestepKey.getHeader(), PedestrianIdKey.getHeader(), "overlapPedestrianId"};
}
public int getTimeStep() {
return timeStep;
}
public int getPedId1() {
return pedId1;
}
public int getPedId2() {
return pedId2;
}
public String[] toStrings(){
return new String[]{Integer.toString(timeStep), Integer.toString(pedId1), Integer.toString(pedId2)};
}
@Override
public int compareTo(@NotNull TimestepPedestriansNearbyIdKey o) {
int result = Integer.compare(this.timeStep, o.timeStep);
if (result == 0) {
result = Integer.compare(this.pedId1, o.pedId1);
if (result == 0){
result = Integer.compare(this.pedId2, o.pedId2);
}
}
return result;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TimestepPedestriansNearbyIdKey that = (TimestepPedestriansNearbyIdKey) o;
return timeStep == that.timeStep &&
pedId1 == that.pedId1 &&
pedId2 == that.pedId2;
}
@Override
public int hashCode() {
return Objects.hash(timeStep, pedId1, pedId2);
}
@Override
public String toString() {
return "TimestepPedestrianIdOverlapKey{" +
"timeStep=" + timeStep +
", pedId1=" + pedId1 +
", pedId2=" + pedId2 +
'}';
}
}
package org.vadere.simulator.projects.dataprocessing.outputfile;
import org.vadere.annotation.factories.outputfiles.OutputFileClass;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdOverlapKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestriansNearbyIdKey;
@OutputFileClass(dataKeyMapping = TimestepPedestriansNearbyIdKey.class)
public class TimestepPedestriansNearbyIdOutputFile extends OutputFile<TimestepPedestriansNearbyIdKey>{
public TimestepPedestriansNearbyIdOutputFile() {
super(TimestepPedestriansNearbyIdKey.getHeaders());
}
@Override
public String[] toStrings(final TimestepPedestriansNearbyIdKey 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.models.osm.CellularAutomaton;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.OverlapData;
import org.vadere.simulator.projects.dataprocessing.datakey.PedestiansNearbyData;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestrianIdOverlapKey;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPedestriansNearbyIdKey;
import org.vadere.state.scenario.DynamicElement;
import org.vadere.state.scenario.Pedestrian;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.LinkedCellsGrid;
import org.vadere.util.geometry.shapes.VPoint;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Maxim Dudin
*/
@DataProcessorClass()
public class PedestiansNearbyProcessor extends DataProcessor<TimestepPedestriansNearbyIdKey, PedestiansNearbyData> {
private double minDist;
private double SPAWN_BUFFER = 0.001;
public PedestiansNearbyProcessor() {
super("distance", "overlaps");
}
@Override
protected void doUpdate(final SimulationState state) {
/*double pedRadius = state.getTopography().getAttributesPedestrian().getRadius();
Collection<Pedestrian> peds = state.getTopography().getElements(Pedestrian.class);
String mainModelstring =state.getScenarioStore().getMainModel();
if(mainModelstring != null && mainModelstring.equals(CellularAutomaton.class.getName())) {
minDist = pedRadius * 2 - SPAWN_BUFFER; // allow touching agents for Cellular Automaton
}else{
minDist = pedRadius * 2;
}
int timeStep = state.getStep();
for (Pedestrian ped : peds) {
// get all Pedestrians with at most pedRadius*2.5 distance away
// this reduces the amount of overlap tests
VPoint pedPos = ped.getPosition();
List<DynamicElement> neighbours = getDynElementsAtPosition(state.getTopography(), ped.getPosition(), pedRadius *2.5);
// collect pedIds and distance of all overlaps for the current ped in the current timestep
List<OverlapData> overlaps = neighbours
.parallelStream()
.map(p -> new OverlapData(ped, p, minDist))
.filter(OverlapData::isNotSelfOverlap)
.filter(OverlapData::isOverlap)
.collect(Collectors.toList());
overlaps.forEach(o -> this.putValue(new TimestepPedestrianIdOverlapKey(timeStep, o.getPed1Id(), o.getPed2Id()), o));
}*/
}
public String[] toStrings(final TimestepPedestriansNearbyIdKey key) {
return this.hasValue(key) ? this.getValue(key).toStrings() : new String[]{"N/A", "N/A"};
}
@Override
public void init(final ProcessorManager manager) {
super.init(manager);
}
private List<DynamicElement> getDynElementsAtPosition(final Topography topography, VPoint sourcePosition, double radius) {
LinkedCellsGrid<DynamicElement> dynElements = topography.getSpatialMap(DynamicElement.class);
return dynElements.getObjects(sourcePosition, radius);
}
}
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