Commit b1c0578d authored by Benedikt Zoennchen's avatar Benedikt Zoennchen
Browse files

Processor: Implementation of the TargetFloorFieldGridProcessor and the...

Processor: Implementation of the TargetFloorFieldGridProcessor and the refactoring of the PostVis such that the new data format can be read
parent c4236b74
......@@ -33,3 +33,6 @@ VadereModelTests/TestOVM/processed output/*
#deprecated: can potentially be removed
bin/
VadereGui/output/
#mac osx files
.DS_Store
......@@ -45,7 +45,9 @@ public abstract class SimulationRenderer extends DefaultRenderer {
protected void renderPreTransformation(Graphics2D graphics2D, int width, int height) {
if (model.isFloorFieldAvailable() && model.config.isShowPotentialField()) {
synchronized (model) {
renderPotentialField(graphics2D, width, height);
renderPotentialField(graphics2D,
(int)(Math.min(model.getTopographyBound().width, model.getViewportBound().width) * model.getScaleFactor()),
(int)(Math.min(model.getTopographyBound().height, model.getViewportBound().height) * model.getScaleFactor()));
}
}
super.renderPreTransformation(graphics2D, width, height);
......
......@@ -89,7 +89,7 @@ public class PostVisualizationConsole {
if (trajectoryFile.isPresent() && scenarioFile.isPresent()) {
TrajectoryReader reader = new TrajectoryReader(trajectoryFile.get().toPath());
model.init(reader.readFile(), IOOutput.readScenario(scenarioFile.get().toPath()));
model.init(reader.readFile(), IOOutput.readScenario(scenarioFile.get().toPath()), trajectoryFile.get().getParent());
} else {
System.err.println("could not find trajectory or scenario file in: " + outputDirectoryPath);
}
......
......@@ -82,7 +82,7 @@ public class ActionOpenFile extends ActionVisualization {
if (trajectoryFile.isPresent() && snapshotFile.isPresent()) {
ScenarioRunManager vadere = IOOutput.readScenario(snapshotFile.get().toPath());
model.init(IOOutput.readTrajectories(trajectoryFile.get().toPath(), vadere), vadere);
model.init(IOOutput.readTrajectories(trajectoryFile.get().toPath(), vadere), vadere, trajectoryFile.get().getParent());
model.notifyObservers();
dialog.dispose();
setLastDirectories(scenarioOutputDir);
......
......@@ -6,16 +6,11 @@ import org.vadere.gui.components.utils.Resources;
import org.vadere.gui.postvisualization.model.PostvisualizationModel;
import org.vadere.gui.postvisualization.utils.PotentialFieldContainer;
import org.vadere.gui.postvisualization.view.DialogFactory;
import org.vadere.gui.projectview.VadereApplication;
import org.vadere.state.scenario.Topography;
import org.vadere.util.potential.CellGrid;
import org.vadere.util.potential.CellGridConverter;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;
import java.util.prefs.Preferences;
public class ActionShowPotentialField extends ActionVisualization {
......@@ -30,8 +25,8 @@ public class ActionShowPotentialField extends ActionVisualization {
public void actionPerformed(final ActionEvent event) {
if (!model.config.isShowPotentialField()) {
final JFileChooser fc = new JFileChooser(resources.getProperty("View.outputDirectory.path"));
//final JFileChooser fc = new JFileChooser(resources.getProperty("View.outputDirectory.path"));
final JFileChooser fc = new JFileChooser(model.getOutputPath());
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
......@@ -47,7 +42,7 @@ public class ActionShowPotentialField extends ActionVisualization {
try {
Topography topography = model.getTopography();
PotentialFieldContainer container = new PotentialFieldContainer(file,
topography.getBounds().getWidth(), topography.getBounds().getHeight(), true);
topography.getBounds().getWidth(), topography.getBounds().getHeight(), false);
model.setPotentialFieldContainer(container);
model.config.setShowPotentialField(true);
model.notifyObservers();
......
......@@ -28,7 +28,6 @@ import org.vadere.state.scenario.Topography;
import org.vadere.state.scenario.TopographyIterator;
import org.vadere.state.simulation.Step;
import org.vadere.state.simulation.Trajectory;
import org.vadere.util.io.IOUtils;
import org.vadere.util.io.parser.JsonLogicParser;
import org.vadere.util.io.parser.VPredicate;
import org.vadere.util.potential.CellGrid;
......@@ -57,6 +56,8 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
private List<Step> steps;
private String outputPath;
// public Configuration config;
public PostvisualizationModel() {
......@@ -96,10 +97,11 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
});
}
public void init(final Map<Step, List<Agent>> agentsByStep, final ScenarioRunManager vadere) {
public void init(final Map<Step, List<Agent>> agentsByStep, final ScenarioRunManager vadere, final String projectPath) {
logger.info("start init postvis model");
this.vadere = vadere;
this.agentsByStep = agentsByStep;
this.outputPath = projectPath;
Map<Integer, Step> map = agentsByStep
.keySet().stream()
.sorted(stepComparator)
......@@ -134,12 +136,13 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
logger.info("finished init postvis model");
}
public void init(final ScenarioRunManager vadere) {
public void init(final ScenarioRunManager vadere, final String projectPath) {
this.vadere = vadere;
this.agentsByStep = new HashMap<>();
this.steps = new ArrayList<>();
this.trajectories = new HashMap<>();
this.selectedElement = null;
this.outputPath = projectPath;
}
public Optional<Step> getLastStep() {
......@@ -158,6 +161,10 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
}
}
public ScenarioRunManager getScenarioRunManager() {
return vadere;
}
@Override
public Optional<CellGrid> getPotentialField() {
if (potentialContainer == null || step == null) {
......@@ -326,6 +333,10 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
return topographyId;
}
public String getOutputPath() {
return outputPath;
}
private void clear() {
setVoronoiDiagram(null);
......
......@@ -112,14 +112,14 @@ public class PotentialFieldContainer {
while ((line = reader.readLine()) != null) {
y++;
String[] splitLine = line.split(seperator);
if (resolution == null) {
resolution = width / (splitLine.length - 2);
}
// first line is the headline
if (!firstLine) {
if (resolution == null) {
resolution = width / (splitLine.length - 2);
}
readStep = Integer.parseInt(splitLine[0]);
double time = Double.parseDouble(splitLine[1]);
int row = Integer.parseInt(splitLine[1]);
if (readStep - 1 == requestedStep) {
return grid;
......
......@@ -48,11 +48,11 @@ public class PostvisualizationWindow extends JPanel implements Observer {
private static Resources resources = Resources.getInstance("postvisualization");
private final ScenarioElementView textView;
public PostvisualizationWindow() {
this(false);
public PostvisualizationWindow(final String projectPath) {
this(false, projectPath);
}
public PostvisualizationWindow(final boolean loadTopographyInformationsOnly) {
public PostvisualizationWindow(final boolean loadTopographyInformationsOnly, final String projectPath) {
// 1. get data from the user screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
......@@ -328,13 +328,13 @@ public class PostvisualizationWindow extends JPanel implements Observer {
public void loadOutputFile(final File trajectoryFile, final ScenarioRunManager scenario) throws IOException {
Player.getInstance(model).stop();
model.init(IOOutput.readTrajectories(trajectoryFile.toPath(), scenario), scenario);
model.init(IOOutput.readTrajectories(trajectoryFile.toPath(), scenario), scenario, trajectoryFile.getParent());
model.notifyObservers();
}
public void loadOutputFile(final ScenarioRunManager scenario) throws IOException {
Player.getInstance(model).stop();
model.init(scenario);
model.init(scenario, model.getOutputPath());
model.notifyObservers();
}
......@@ -371,7 +371,7 @@ public class PostvisualizationWindow extends JPanel implements Observer {
EventQueue.invokeLater(() -> {
JFrame frame = new JFrame();
PostvisualizationWindow postVisWindow = new PostvisualizationWindow(true);
PostvisualizationWindow postVisWindow = new PostvisualizationWindow(true, "./");
frame.add(postVisWindow);
frame.setJMenuBar(postVisWindow.getMenu());
......
......@@ -78,7 +78,7 @@ public class ActionLoadProject extends AbstractAction {
public static void loadProjectByPath(ProjectViewModel projectViewModel, String projectFilePath) {
try {
VadereProject project = IOVadere.readProjectJson(projectFilePath);
VadereProject project = IOVadere. readProjectJson(projectFilePath);
projectViewModel.setCurrentProjectPath(projectFilePath);
projectViewModel.setProject(project);
......
......@@ -644,7 +644,7 @@ public class ProjectView extends JFrame implements ProjectFinishedListener, Sing
ScenarioNamePanel.add(scenarioName);
scenarioName.setHorizontalAlignment(SwingConstants.CENTER);
scenarioJPanel = new ScenarioJPanel(scenarioName);
scenarioJPanel = new ScenarioJPanel(scenarioName, model);
model.setScenarioNameLabel(scenarioName); // TODO [priority=low] [task=refactoring] breaking mvc pattern (?) - but I need access to refresh the scenarioName
model.addProjectChangeListener(scenarioJPanel);
rightSidePanel.add(scenarioJPanel, BorderLayout.CENTER);
......
......@@ -6,6 +6,7 @@ import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.onlinevisualization.OnlineVisualization;
import org.vadere.gui.postvisualization.view.PostvisualizationWindow;
import org.vadere.gui.projectview.control.IProjectChangeListener;
import org.vadere.gui.projectview.model.ProjectViewModel;
import org.vadere.gui.projectview.utils.ClassFinder;
import org.vadere.gui.topographycreator.view.TopographyWindow;
import org.vadere.simulator.projects.ProjectFinishedListener;
......@@ -36,6 +37,7 @@ public class ScenarioJPanel extends JPanel implements IProjectChangeListener, Pr
private JTabbedPane tabbedPane;
private final JLabel scenarioName;
private ProjectViewModel model;
// tabs
private List<JMenu> menusInTabs = new ArrayList<>();
......@@ -58,10 +60,11 @@ public class ScenarioJPanel extends JPanel implements IProjectChangeListener, Pr
private static String activeJsonParsingErrorMsg = null;
ScenarioJPanel(JLabel scenarioName) {
ScenarioJPanel(JLabel scenarioName, ProjectViewModel model) {
this.model = model;
this.scenarioName = scenarioName;
this.onlineVisualization = new OnlineVisualization(true);
this.postVisualizationView = new PostvisualizationWindow();
this.postVisualizationView = new PostvisualizationWindow(model.getCurrentProjectPath());
super.setBorder(new EmptyBorder(5, 5, 5, 5));
super.setLayout(new CardLayout(0, 0));
......
......@@ -56,7 +56,7 @@ public class OfflineSimulation {
.map(t -> t.getAgent(step))
.filter(opt -> opt.isPresent()).forEach(opt -> topography.addElement(opt.get()));
return new SimulationState(vadere.getName(), topography, vadere.getScenarioStore(),
step.getSimTimeInSec().orElse(Double.NaN), step.getStepNumber());
step.getSimTimeInSec().orElse(Double.NaN), step.getStepNumber(), null);
}
private void prepareOutput() {
......
......@@ -124,7 +124,7 @@ public class Simulation {
}
private void postLoop() {
simulationState = new SimulationState(name, topography, scenarioStore, simTimeInSec, step);
simulationState = new SimulationState(name, topography, scenarioStore, simTimeInSec, step, mainModel);
for (ActiveCallback ac : activeCallbacks) {
ac.postLoop(simTimeInSec);
......@@ -205,7 +205,7 @@ public class Simulation {
private SimulationState initialSimulationState() {
SimulationState state =
new SimulationState(name, topography.clone(), scenarioStore, simTimeInSec, step);
new SimulationState(name, topography.clone(), scenarioStore, simTimeInSec, step, mainModel);
return state;
}
......@@ -213,7 +213,7 @@ public class Simulation {
private void updateWriters(double simTimeInSec) {
SimulationState simulationState =
new SimulationState(name, topography, scenarioStore, simTimeInSec, step);
new SimulationState(name, topography, scenarioStore, simTimeInSec, step, mainModel);
this.simulationState = simulationState;
}
......
package org.vadere.simulator.control;
import org.jetbrains.annotations.Nullable;
import org.vadere.simulator.models.MainModel;
import org.vadere.simulator.projects.ScenarioStore;
import org.vadere.state.scenario.Car;
import org.vadere.state.scenario.Pedestrian;
......@@ -8,6 +10,7 @@ import org.vadere.util.geometry.shapes.VPoint;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class SimulationState {
private final Topography topography;
......@@ -16,18 +19,21 @@ public class SimulationState {
private final ScenarioStore scenarioStore;
private final int step;
private final String name;
private final MainModel mainModel;
protected SimulationState(final String name,
final Topography topography,
final ScenarioStore scenarioStore,
final double simTimeInSec,
final int step) {
final int step,
@Nullable final MainModel mainModel) {
this.name = name;
this.topography = topography;
this.simTimeInSec = simTimeInSec;
this.step = step;
this.pedestrianPositionMap = new HashMap<>();
this.scenarioStore = scenarioStore;
this.mainModel = mainModel;
for (Pedestrian pedestrian : topography.getElements(Pedestrian.class)) {
pedestrianPositionMap.put(pedestrian.getId(), pedestrian.getPosition());
......@@ -46,6 +52,7 @@ public class SimulationState {
this.step = step;
this.pedestrianPositionMap = pedestrianPositionMap;
this.scenarioStore = null;
this.mainModel = null;
}
// public access to getters
......@@ -73,4 +80,14 @@ public class SimulationState {
public String getName() {
return name;
}
/**
* Returns the main model of the simulation if it is an online simulation, otherwise the main model
* is unknown and therefore null.
*
* @return online simulation => main model, otherwise null.
*/
public Optional<MainModel> getMainModel() {
return Optional.ofNullable(mainModel);
}
}
......@@ -5,9 +5,11 @@ import org.vadere.simulator.models.Model;
import org.vadere.simulator.models.ode.IntegratorFactory;
import org.vadere.simulator.models.ode.ODEModel;
import org.vadere.simulator.models.potential.FloorGradientProviderFactory;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.fields.PotentialFieldTarget;
import org.vadere.state.attributes.Attributes;
import org.vadere.state.attributes.models.AttributesGNM;
import org.vadere.state.attributes.scenario.AttributesAgent;
......@@ -28,7 +30,7 @@ import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgent> {
public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgent> implements PotentialFieldModel {
private AttributesGNM attributes;
private GradientProvider floorGradient;
private Map<Integer, Target> targets;
......@@ -167,4 +169,18 @@ public class GradientNavigationModel extends ODEModel<Pedestrian, AttributesAgen
return activeCallbacks;
}
@Override
public PotentialFieldTarget getPotentialFieldTarget() {
return potentialFieldTarget;
}
@Override
public PotentialFieldObstacle getPotentialFieldObstacle() {
return potentialFieldObstacle;
}
@Override
public PotentialFieldAgent getPotentialFieldAgent() {
return potentialFieldPedestrian;
}
}
......@@ -17,6 +17,7 @@ import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerNelderMea
import org.vadere.simulator.models.osm.optimization.StepCircleOptimizerPowell;
import org.vadere.simulator.models.osm.updateScheme.ParallelWorkerOSM;
import org.vadere.simulator.models.osm.updateScheme.UpdateSchemeOSM.CallMethod;
import org.vadere.simulator.models.potential.PotentialFieldModel;
import org.vadere.simulator.models.potential.fields.IPotentialTargetGrid;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
......@@ -44,7 +45,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class OptimalStepsModel implements MainModel {
public class OptimalStepsModel implements MainModel, PotentialFieldModel {
/**
* Compares the time of the next possible move.
......@@ -330,4 +331,18 @@ public class OptimalStepsModel implements MainModel {
return activeCallbacks;
}
@Override
public PotentialFieldTarget getPotentialFieldTarget() {
return potentialFieldTarget;
}
@Override
public PotentialFieldObstacle getPotentialFieldObstacle() {
return potentialFieldObstacle;
}
@Override
public PotentialFieldAgent getPotentialFieldAgent() {
return potentialFieldPedestrian;
}
}
package org.vadere.simulator.models.potential;
import org.vadere.simulator.models.potential.fields.PotentialFieldAgent;
import org.vadere.simulator.models.potential.fields.PotentialFieldObstacle;
import org.vadere.simulator.models.potential.fields.PotentialFieldTarget;
public interface PotentialFieldModel {
PotentialFieldTarget getPotentialFieldTarget();
PotentialFieldObstacle getPotentialFieldObstacle();
PotentialFieldAgent getPotentialFieldAgent();
}
......@@ -349,4 +349,8 @@ public class ScenarioRunManager implements Runnable {
public void setDataProcessingJsonManager(final DataProcessingJsonManager manager) {
this.dataProcessingJsonManager = manager;
}
public Path getOutputPath() {
return outputPath;
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepRowKey;
import org.vadere.state.attributes.processor.AttributesFloorFieldProcessor;
import org.vadere.util.data.FloorFieldGridRow;
public class FloorFieldGridProcessor extends DataProcessor<TimestepRowKey, FloorFieldGridRow> {
private int targetId;
@Override
protected void doUpdate(SimulationState state) {
// First try, TODO: Implementation
for (int i = 0; i < 50; ++i) {
this.setValue(new TimestepRowKey(state.getStep(), i), new FloorFieldGridRow(50));
}
}
@Override
public void init(ProcessorManager manager) {
AttributesFloorFieldProcessor att = (AttributesFloorFieldProcessor) this.getAttributes();
this.targetId = att.getTargetId();
}
@Override
public String[] toStrings(TimestepRowKey key) {
return this.getValue(key).toStrings();
}
}
package org.vadere.simulator.projects.dataprocessing.processor;
import org.vadere.simulator.control.SimulationState;
import org.vadere.simulator.projects.dataprocessing.ProcessorManager;
import org.vadere.simulator.projects.dataprocessing.datakey.TimestepPositionKey;
import org.vadere.state.attributes.processor.AttributesFloorFieldProcessor;
import org.vadere.util.geometry.shapes.VPoint;
/**
* @author Mario Teixeira Parente
*/
public class FloorFieldProcessor extends DataProcessor<TimestepPositionKey, Double> {
private int targetId;
public FloorFieldProcessor() {
super("potential");
}
@Override
protected void doUpdate(SimulationState state) {
// First try, TODO: Implementation
for (int x = 0; x < 50; ++x) {
for (int y = 0; y < 50; ++y) {
this.setValue(new TimestepPositionKey(state.getStep(), new VPoint(x, y)), 0.0);
}
}
}
@Override
public void init(ProcessorManager manager) {
AttributesFloorFieldProcessor att = (AttributesFloorFieldProcessor) this.getAttributes();
this.targetId = att.getTargetId();
}
}
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