2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit f2e2e557 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

Implemented class "AbsorbingAreaController" and integrated controller into "Simulation".

VadereSimulator/src/org/vadere/simulator/control/Simulation.java:
- Extracted new method "createControllers()" which is used in constructor.

VadereState/src/org/vadere/state/attributes/scenario/AttributesAbsorbingArea.java:
- Removed unused attribute "waitingTime".
- Changed default value for attribute "deletionDistance" from "0.1" to "0.0".
parent 2734c341
package org.vadere.simulator.control;
import org.vadere.state.scenario.*;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* Absorb agents which enter the given {@link AbsorbingArea}.
* <p>
* Take following attributes into account when absorbing agents:
* - {@link org.vadere.state.attributes.scenario.AttributesAbsorbingArea#getDeletionDistance()}
* <p>
*/
public class AbsorbingAreaController {
// Variables
private static final Logger log = Logger.getLogger(AbsorbingAreaController.class);
public final AbsorbingArea absorbingArea;
private Topography topography;
// Constructors
public AbsorbingAreaController(Topography topography, AbsorbingArea absorbingArea) {
this.absorbingArea = absorbingArea;
this.topography = topography;
}
// Other Methods
public void update(double simTimeInSec) {
for (DynamicElement element : getDynamicElementsNearAbsorbingArea()) {
final Agent agent;
if (element instanceof Agent) {
agent = (Agent) element;
} else {
log.error("The given object is not a subtype of Agent.");
continue;
}
if (hasAgentReachedAbsorbingArea(agent)) {
notifyListenersAbsorbingAreaReached(agent);
topography.removeElement(agent);
}
}
}
private Collection<DynamicElement> getDynamicElementsNearAbsorbingArea() {
final Rectangle2D absorbingAreaBounds = absorbingArea.getShape().getBounds2D();
final VPoint centerOfAbsorbingArea = new VPoint(absorbingAreaBounds.getCenterX(), absorbingAreaBounds.getCenterY());
final double deletionDistance = absorbingArea.getAttributes().getDeletionDistance();
final double deletionRadius = Math.max(absorbingAreaBounds.getHeight(), absorbingAreaBounds.getWidth()) + deletionDistance;
final Collection<DynamicElement> elementsNearAbsorbingArea = new LinkedList<>();
List<Pedestrian> pedestriansNearAbsorbingArea = topography.getSpatialMap(Pedestrian.class).getObjects(centerOfAbsorbingArea, deletionRadius);
elementsNearAbsorbingArea.addAll(pedestriansNearAbsorbingArea);
return elementsNearAbsorbingArea;
}
private boolean hasAgentReachedAbsorbingArea(Agent agent) {
final double deletionDistance = absorbingArea.getAttributes().getDeletionDistance();
final VPoint agentPosition = agent.getPosition();
final VShape absorbingAreaShape = absorbingArea.getShape();
return absorbingAreaShape.contains(agentPosition)
|| absorbingAreaShape.distance(agentPosition) < deletionDistance;
}
private void notifyListenersAbsorbingAreaReached(final Agent agent) {
for (AbsorbingAreaListener listener : absorbingArea.getAbsorbingAreaListeners()) {
listener.reachedAbsorbingArea(absorbingArea, agent);
}
}
}
...@@ -16,10 +16,7 @@ import org.vadere.simulator.projects.dataprocessing.ProcessorManager; ...@@ -16,10 +16,7 @@ import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.state.attributes.AttributesSimulation; import org.vadere.state.attributes.AttributesSimulation;
import org.vadere.state.attributes.scenario.AttributesAgent; import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.events.types.Event; import org.vadere.state.events.types.Event;
import org.vadere.state.scenario.Pedestrian; import org.vadere.state.scenario.*;
import org.vadere.state.scenario.Source;
import org.vadere.state.scenario.Target;
import org.vadere.state.scenario.Topography;
import org.vadere.util.logging.Logger; import org.vadere.util.logging.Logger;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
...@@ -37,6 +34,7 @@ public class Simulation { ...@@ -37,6 +34,7 @@ public class Simulation {
private final Collection<SourceController> sourceControllers; private final Collection<SourceController> sourceControllers;
private final Collection<TargetController> targetControllers; private final Collection<TargetController> targetControllers;
private final Collection<AbsorbingAreaController> absorbingAreaControllers;
private TeleporterController teleporterController; private TeleporterController teleporterController;
private TopographyController topographyController; private TopographyController topographyController;
private DynamicElementFactory dynamicElementFactory; private DynamicElementFactory dynamicElementFactory;
...@@ -84,6 +82,7 @@ public class Simulation { ...@@ -84,6 +82,7 @@ public class Simulation {
this.attributesAgent = scenarioStore.getTopography().getAttributesPedestrian(); this.attributesAgent = scenarioStore.getTopography().getAttributesPedestrian();
this.sourceControllers = new LinkedList<>(); this.sourceControllers = new LinkedList<>();
this.targetControllers = new LinkedList<>(); this.targetControllers = new LinkedList<>();
this.absorbingAreaControllers = new LinkedList<>();
this.topography = scenarioStore.getTopography(); this.topography = scenarioStore.getTopography();
this.runTimeInSec = attributesSimulation.getFinishTime(); this.runTimeInSec = attributesSimulation.getFinishTime();
this.startTimeInSec = startTimeInSec; this.startTimeInSec = startTimeInSec;
...@@ -98,11 +97,13 @@ public class Simulation { ...@@ -98,11 +97,13 @@ public class Simulation {
this.processorManager = processorManager; this.processorManager = processorManager;
this.passiveCallbacks = passiveCallbacks; this.passiveCallbacks = passiveCallbacks;
this.topographyController = new TopographyController(topography, dynamicElementFactory);
// "eventController" is final. Therefore, create object here and not in helper method.
this.eventController = new EventController(scenarioStore); this.eventController = new EventController(scenarioStore);
this.cognitionLayer = new CognitionLayer(); this.cognitionLayer = new CognitionLayer();
createControllers(topography, mainModel, random);
// ::start:: this code is to visualize the potential fields. It may be refactored later. // ::start:: this code is to visualize the potential fields. It may be refactored later.
if(attributesSimulation.isVisualizationEnabled()) { if(attributesSimulation.isVisualizationEnabled()) {
IPotentialFieldTarget pft = null; IPotentialFieldTarget pft = null;
...@@ -129,17 +130,25 @@ public class Simulation { ...@@ -129,17 +130,25 @@ public class Simulation {
for (PassiveCallback pc : this.passiveCallbacks) { for (PassiveCallback pc : this.passiveCallbacks) {
pc.setTopography(topography); pc.setTopography(topography);
} }
}
private void createControllers(Topography topography, MainModel mainModel, Random random) {
this.topographyController = new TopographyController(topography, mainModel);
// create source and target controllers
for (Source source : topography.getSources()) { for (Source source : topography.getSources()) {
SourceController sc = this.sourceControllerFactory SourceController sc = this.sourceControllerFactory
.create(topography, source, dynamicElementFactory, attributesAgent, random); .create(topography, source, dynamicElementFactory, attributesAgent, random);
sourceControllers.add(sc); sourceControllers.add(sc);
} }
for (Target target : topography.getTargets()) { for (Target target : topography.getTargets()) {
targetControllers.add(new TargetController(topography, target)); targetControllers.add(new TargetController(topography, target));
} }
for (AbsorbingArea absorbingArea : topography.getAbsorbingAreas()) {
absorbingAreaControllers.add(new AbsorbingAreaController(topography, absorbingArea));
}
if (topography.hasTeleporter()) { if (topography.hasTeleporter()) {
this.teleporterController = new TeleporterController( this.teleporterController = new TeleporterController(
topography.getTeleporter(), topography); topography.getTeleporter(), topography);
...@@ -298,6 +307,10 @@ public class Simulation { ...@@ -298,6 +307,10 @@ public class Simulation {
targetController.update(simTimeInSec); targetController.update(simTimeInSec);
} }
for (AbsorbingAreaController absorbingAreaController : this.absorbingAreaControllers) {
absorbingAreaController.update(simTimeInSec);
}
topographyController.update(simTimeInSec); //rebuild CellGrid topographyController.update(simTimeInSec); //rebuild CellGrid
step++; step++;
......
...@@ -15,14 +15,10 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape { ...@@ -15,14 +15,10 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape {
* Shape and position. * Shape and position.
*/ */
private VShape shape; private VShape shape;
/**
* Waiting time in seconds on this area before absorbing the element.
*/
private double waitingTime = 0;
/** /**
* Within this distance, pedestrians have reached the absorbing area. * Within this distance, pedestrians have reached the absorbing area.
*/ */
private double deletionDistance = 0.1; private double deletionDistance = 0.0;
// Constructors // Constructors
public AttributesAbsorbingArea() { public AttributesAbsorbingArea() {
...@@ -37,10 +33,9 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape { ...@@ -37,10 +33,9 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape {
this.id = id; this.id = id;
} }
public AttributesAbsorbingArea(final VShape shape, final int id, double waitingTime, double deletionDistance) { public AttributesAbsorbingArea(final VShape shape, final int id, double deletionDistance) {
this.shape = shape; this.shape = shape;
this.id = id; this.id = id;
this.waitingTime = waitingTime;
this.deletionDistance = deletionDistance; this.deletionDistance = deletionDistance;
} }
...@@ -54,10 +49,6 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape { ...@@ -54,10 +49,6 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape {
return shape; return shape;
} }
public double getWaitingTime() {
return waitingTime;
}
public double getDeletionDistance() { public double getDeletionDistance() {
return deletionDistance; return deletionDistance;
} }
...@@ -73,11 +64,6 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape { ...@@ -73,11 +64,6 @@ public class AttributesAbsorbingArea extends AttributesEmbedShape {
this.shape = shape; this.shape = shape;
} }
public void setWaitingTime(double waitingTime) {
checkSealed();
this.waitingTime = waitingTime;
}
public void setDeletionDistance(double deletionDistance) { public void setDeletionDistance(double deletionDistance) {
checkSealed(); checkSealed();
this.deletionDistance = deletionDistance; this.deletionDistance = deletionDistance;
......
...@@ -34,10 +34,6 @@ public class AbsorbingArea extends ScenarioElement implements Comparable<Absorbi ...@@ -34,10 +34,6 @@ public class AbsorbingArea extends ScenarioElement implements Comparable<Absorbi
} }
// Getters // Getters
public double getWaitingTime() {
return attributes.getWaitingTime();
}
public Map<Integer, Double> getEnteringTimes() { public Map<Integer, Double> getEnteringTimes() {
return enteringTimes; return enteringTimes;
} }
...@@ -114,7 +110,7 @@ public class AbsorbingArea extends ScenarioElement implements Comparable<Absorbi ...@@ -114,7 +110,7 @@ public class AbsorbingArea extends ScenarioElement implements Comparable<Absorbi
absorbingAreaListeners.add(listener); absorbingAreaListeners.add(listener);
} }
public boolean removeListener(TargetListener listener) { public boolean removeListener(AbsorbingAreaListener listener) {
return absorbingAreaListeners.remove(listener); return absorbingAreaListeners.remove(listener);
} }
......
package org.vadere.state.scenario; package org.vadere.state.scenario;
public interface AbsorbingAreaListener { public interface AbsorbingAreaListener {
void reachedAbsorbingArea(Target target, Agent agent); void reachedAbsorbingArea(AbsorbingArea absorbingArea, Agent agent);
} }
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