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

#306 added post vis compatibility for PedestriansNearby Processor.

To activate the post visualization add a contacts.traj output file with TimestepPedestriansNearbyIdKey as key and a corresponding PedestriansNearby Processor with attribute printForPostVis set true
parent 8191387b
Pipeline #251545 failed with stages
in 3 minutes and 28 seconds
......@@ -76,14 +76,31 @@ public class ActionOpenFile extends ActionVisualization {
Player.getInstance(model).stop();
File scenarioOutputDir = threadFile.isDirectory() ? threadFile : threadFile.getParentFile();
Optional<File> trajectoryFile =
IOUtils.getFirstFile(scenarioOutputDir, IOUtils.TRAJECTORY_FILE_EXTENSION);
File[] trajFiles = IOUtils.getFileList(scenarioOutputDir, IOUtils.TRAJECTORY_FILE_EXTENSION);
Optional<File> trajectoryFile = Optional.empty();
Optional<File> contactsTrajectoryFile = Optional.empty();
for (File f :trajFiles) {
logger.info("file read with filename "+ f.getName());
if (f.getName().contains("contacts")) {
logger.info("contactsTrajectoryFile is "+ f.getName());
contactsTrajectoryFile = Optional.of(f);
} else {
logger.info("trajectoryFile is "+ f.getName());
trajectoryFile = Optional.of(f);
}
}
//Optional<File> trajectoryFile =
// IOUtils.getFirstFile(scenarioOutputDir, IOUtils.TRAJECTORY_FILE_EXTENSION);
Optional<File> snapshotFile =
IOUtils.getFirstFile(scenarioOutputDir, IOUtils.SCENARIO_FILE_EXTENSION);
if (trajectoryFile.isPresent() && snapshotFile.isPresent()) {
Scenario vadere = IOOutput.readScenario(snapshotFile.get().toPath());
model.init(IOOutput.readTrajectories(trajectoryFile.get().toPath()), vadere, trajectoryFile.get().getParent());
if (contactsTrajectoryFile.isPresent()) {
model.init(IOOutput.readTrajectories(trajectoryFile.get().toPath()), IOOutput.readTrajectories(contactsTrajectoryFile.get().toPath()), vadere, contactsTrajectoryFile.get().getParent());
} else {
model.init(IOOutput.readTrajectories(trajectoryFile.get().toPath()), vadere, trajectoryFile.get().getParent());
}
model.notifyObservers();
dialog.dispose();
setLastDirectories(scenarioOutputDir);
......
......@@ -63,6 +63,8 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
private TableTrajectoryFootStep trajectories;
private TableTrajectoryFootStep contactTrajectories;
private String outputPath;
private AttributesAgent attributesAgent;
......@@ -72,6 +74,7 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
public PostvisualizationModel() {
super(new PostvisualizationConfig());
this.trajectories = new TableTrajectoryFootStep(Table.create());
this.contactTrajectories = new TableTrajectoryFootStep(Table.create());
this.scenario = new Scenario("");
this.topographyId = 0;
this.potentialContainer = null;
......@@ -82,8 +85,11 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
this.outputChanged = false;
}
public synchronized void init(final Table trajectories, final Table contactTrajectories, final Scenario scenario, final String projectPath) {
init(trajectories, contactTrajectories, scenario, projectPath, new AttributesAgent());
}
public synchronized void init(final Table trajectories, final Scenario scenario, final String projectPath) {
init(trajectories, scenario, projectPath, new AttributesAgent());
init(trajectories, null, scenario, projectPath, new AttributesAgent());
}
/**
......@@ -93,10 +99,15 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
* This scenario will not contain any agents.
* @param projectPath the path to the project.
*/
public synchronized void init(final Table trajectories, final Scenario scenario, final String projectPath, final AttributesAgent attributesAgent) {
public synchronized void init(final Table trajectories, final Table contactTrajectories, final Scenario scenario, final String projectPath, final AttributesAgent attributesAgent) {
this.scenario = scenario;
this.simTimeStepLength = scenario.getAttributesSimulation().getSimTimeStepLength();
this.trajectories = new TableTrajectoryFootStep(trajectories);
if (contactTrajectories != null) {
this.contactTrajectories = new TableTrajectoryFootStep(contactTrajectories);
} else {
this.contactTrajectories = null;
}
this.visTime = 0;
this.attributesAgent = attributesAgent;
this.outputPath = projectPath;
......@@ -193,6 +204,9 @@ public class PostvisualizationModel extends SimulationModel<PostvisualizationCon
public synchronized TableTrajectoryFootStep getTrajectories() {
return trajectories;
}
public synchronized TableTrajectoryFootStep getContactTrajectories() {
return contactTrajectories;
}
public synchronized Table getAgentTable() {
return trajectories.getAgentsWithDisappearedAgents(getSimTimeInSec());
......
......@@ -55,6 +55,9 @@ public class PostvisualizationRenderer extends SimulationRenderer {
if (!model.isEmpty()) {
renderTrajectories(g);
}
if (model.getContactTrajectories() != null) {
renderContactTrajectories(g);
}
}
private void renderTrajectories(final Graphics2D g) {
......@@ -161,4 +164,99 @@ public class PostvisualizationRenderer extends SimulationRenderer {
}
g.setColor(color);
}
private void renderContactTrajectories(final Graphics2D g) {
Color color = g.getColor();
AgentRender agentRender = getAgentRender();
// sorted (by ped id) agent table
TableTrajectoryFootStep trajectories = model.getContactTrajectories();
trajectories.setSlice(0, model.getSimTimeInSec());
Table slice = trajectories.getCurrentSlice();
/*if (model.config.isShowAllTrajectories()) {
slice = model.getAppearedPedestrians();
} else {
slice = model.getAlivePedestrians();
}*/
//Collection<Pedestrian> agents = model.getPedestrians();
//Map<Integer, Color> agentColors = new HashMap<>();
//agents.forEach(agent -> agentColors.put(agent.getId(), Color.red));
Color c = g.getColor();
Stroke stroke = g.getStroke();
if (model.config.isShowTrajectories()) {
for(Row row : slice) {
boolean isLastStep = row.getDouble(trajectories.endTimeCol) > model.getSimTimeInSec();
double startX = row.getDouble(trajectories.startXCol);
double startY = row.getDouble(trajectories.startYCol);
double endX = row.getDouble(trajectories.endXCol);
double endY = row.getDouble(trajectories.endYCol);
if(isLastStep && model.config.isInterpolatePositions()) {
break;
/*VPoint interpolatedPos = FootStep.interpolateFootStep(startX, startY, endX, endY, row.getDouble(trajectories.startTimeCol), row.getDouble(trajectories.endTimeCol), model.getSimTimeInSec());
endX = interpolatedPos.getX();
endY = interpolatedPos.getY();*/
}
Path2D.Double path = new Path2D.Double();
path.moveTo(startX, startY);
path.lineTo(endX, endY);
g.setColor(Color.red);
g.setStroke(new BasicStroke(getLineWidth() / 4.0f));
draw(path, g);
}
}
g.setColor(c);
g.setStroke(stroke);
/*// render agents i.e. circles
if (model.config.isShowPedestrians()) {
for(Pedestrian agent : agents) {
if (model.config.isShowFaydedPedestrians() || model.isAlive(agent.getId())) {
agentRender.render(agent, agentColors.get(agent.getId()), g);
if (model.config.isShowPedestrianIds()) {
DefaultRenderer.paintAgentId(g, agent);
}
}
// renderImage the arrows indicating the walking direction
if (model.config.isShowWalkdirection() &&
(model.config.isShowFaydedPedestrians() || trajectories.getDeathTime(agent.getId()) > model.getSimTimeInSec())) {
int pedestrianId = agent.getId();
VPoint lastPosition = lastPedestrianPositions.get(pedestrianId);
VPoint position = agent.getPosition();
lastPedestrianPositions.put(pedestrianId, position);
if (lastPosition != null) {
VPoint direction;
if (lastPosition.distance(position) < MIN_ARROW_LENGTH) {
direction = pedestrianDirections.get(pedestrianId);
} else {
direction = new VPoint(lastPosition.getX() - position.getX(),
lastPosition.getY() - position.getY());
direction = direction.norm();
pedestrianDirections.put(pedestrianId, direction);
}
if (!pedestrianDirections.containsKey(pedestrianId)) {
pedestrianDirections.put(pedestrianId, direction);
}
if (direction != null) {
double theta = Math.atan2(-direction.getY(), -direction.getX());
DefaultRenderer.drawArrow(g, theta,
position.getX() - agent.getRadius() * 2 * direction.getX(),
position.getY() - agent.getRadius() * 2 * direction.getY());
}
}
}
}
}
g.setColor(color);*/
}
}
......@@ -381,17 +381,25 @@ public class PostvisualizationWindow extends JPanel implements Observer, DropTar
return menuBar;
}
public void loadOutputFile(final File trajectoryFile, final Scenario scenario) throws IOException {
public void loadOutputFile(final File trajectoryFile, final File contactsTrajectoryFile, final Scenario scenario) throws IOException {
Player.getInstance(model).stop();
try {
model.init(IOOutput.readTrajectories(trajectoryFile.toPath()), scenario, trajectoryFile.getParent());
if (contactsTrajectoryFile != null) {
model.init(IOOutput.readTrajectories(trajectoryFile.toPath()), IOOutput.readTrajectories(contactsTrajectoryFile.toPath()), scenario, contactsTrajectoryFile.getParent());
} else {
model.init(IOOutput.readTrajectories(trajectoryFile.toPath()), scenario, trajectoryFile.getParent());
}
model.notifyObservers();
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, ex.getMessage(), Messages.getString("Error.text"), JOptionPane.ERROR_MESSAGE);
}
}
public void loadOutputFile(final File trajectoryFile, final Scenario scenario) throws IOException {
loadOutputFile(trajectoryFile, null, scenario);
}
public void loadOutputFile(final Scenario scenario) {
Player.getInstance(model).stop();
model.init(scenario, model.getOutputPath());
......
......@@ -645,10 +645,25 @@ public class ProjectView extends JFrame implements ProjectFinishedListener, Sing
private void loadScenarioIntoGui(OutputBundle bundle) throws IOException {
Scenario scenarioRM = bundle.getScenarioRM();
Optional<File> optionalTrajectoryFile = IOUtils
.getFirstFile(bundle.getDirectory(), IOUtils.TRAJECTORY_FILE_EXTENSION);
if (optionalTrajectoryFile.isPresent()) {
scenarioJPanel.loadOutputFileForPostVis(optionalTrajectoryFile.get(), scenarioRM);
/*Optional<File> optionalTrajectoryFile = IOUtils
.getFirstFile(bundle.getDirectory(), IOUtils.TRAJECTORY_FILE_EXTENSION);*/
File[] trajFiles = IOUtils.getFileList(bundle.getDirectory(), IOUtils.TRAJECTORY_FILE_EXTENSION);
Optional<File> trajectoryFile = Optional.empty();
Optional<File> contactsTrajectoryFile = Optional.empty();
for (File f :trajFiles) {
if (f.getName().contains("contacts")) {
contactsTrajectoryFile = Optional.of(f);
} else {
trajectoryFile = Optional.of(f);
}
}
if (trajectoryFile.isPresent()) {
if (contactsTrajectoryFile.isPresent()) {
scenarioJPanel.loadOutputFileForPostVis(trajectoryFile.get(), contactsTrajectoryFile.get(), scenarioRM);
} else {
scenarioJPanel.loadOutputFileForPostVis(trajectoryFile.get(), scenarioRM);
}
} else {
scenarioJPanel.loadOutputFileForPostVis(scenarioRM);
logger.error("could not find trajectory file in : "
......
......@@ -351,7 +351,10 @@ public class ScenarioPanel extends JPanel implements IProjectChangeListener, Pro
}
public void loadOutputFileForPostVis(File trajectoryFile, Scenario scenarioRM) throws IOException {
postVisualizationView.loadOutputFile(trajectoryFile, scenarioRM);
postVisualizationView.loadOutputFile(trajectoryFile, null, scenarioRM);
}
public void loadOutputFileForPostVis(File trajectoryFile, File contactsTrajectoryFile, Scenario scenarioRM) throws IOException {
postVisualizationView.loadOutputFile(trajectoryFile, contactsTrajectoryFile, scenarioRM);
}
public static void setActiveTopographyErrorMsg(JEditorPane msg){
......
......@@ -95,7 +95,7 @@ public class PedestriansNearbyData {
r2 = r2*0.3 - 0.15;
ret.append(hashCode()).append(" ").append(startTimestep*0.4 + i*0.4).append(" ").append(startTimestep*0.4 + (i+1)*0.4).append(" ").append(p.x + r2).append(" ").append(p.y + r1).append(" ").append(pNext.x + r1).append(" ").append(pNext.y + r2).append(" 9999");
if (i != traj.size() -1) {
ret.append("\n");
ret.append("\r\n");
}
}
return new String[]{ret.toString()};
......
......@@ -14,6 +14,7 @@ public class TimestepPedestriansNearbyIdKey implements DataKey<TimestepPedestria
private final int pedId1; //smaller id
private final int pedId2; //bigger id
private final boolean printForPostVis;
private static boolean staticPrintForPostVis;
public TimestepPedestriansNearbyIdKey(int timeStep, int pedA, int pedB, boolean printForPostVis) {
......@@ -21,11 +22,13 @@ public class TimestepPedestriansNearbyIdKey implements DataKey<TimestepPedestria
this.pedId1 = Math.min(pedA, pedB);
this.pedId2 = Math.max(pedA, pedB);
this.printForPostVis = printForPostVis; // is an ugly one time thing that shouldn't be merged
staticPrintForPostVis = printForPostVis;
}
public static String[] getHeaders(){
return new String[]{TimestepKey.getHeader(), PedestrianIdKey.getHeader(), "pedestrianNearbyId"};
//return new String[]{TimestepKey.getHeader(), PedestrianIdKey.getHeader(), "pedestrianNearbyId"};
return new String[]{"pedestrianId", "simTime"};
}
public int getTimeStep() {
......
......@@ -27,11 +27,13 @@ public class PedestriansNearbyProcessor extends DataProcessor<TimestepPedestrian
private int minTimespanOfContactTimesteps;
private boolean printContacTrajectories;
private boolean printForPostVis;
private static boolean staticPrintForPostVis;
public PedestriansNearbyProcessor() {
super("durationTimesteps", "xCoord-Contact", "yCoord-Contact");
//super("durationTimesteps", "xCoord-Contact", "yCoord-Contact");
super("endTime", "startX", "startY", "endX", "endY", "targetId");
setAttributes(new AttributesPedestrianNearbyProcessor());
}
......@@ -105,7 +107,7 @@ public class PedestriansNearbyProcessor extends DataProcessor<TimestepPedestrian
minTimespanOfContactTimesteps = att.getMinTimespanOfContactTimesteps();
printContacTrajectories = att.getPrintContactTrajectories();
printForPostVis = att.getPrintForPostVis();
staticPrintForPostVis = printForPostVis;
}
private List<DynamicElement> getDynElementsAtPosition(final Topography topography, VPoint sourcePosition, double radius) {
......
......@@ -293,8 +293,8 @@ public abstract class IOOutput {
if (files == null || files.length < 1) {
throw new IOException("missing trajectory file with the extension " + fileExtension);
} else if (files.length > 1) {
throw new IOException("multiply trajectory files with the extension " + fileExtension);
//} else if (files.length > 1) {
// throw new IOException("multiply trajectory files with the extension " + fileExtension);
} else {
return files[0].toPath();
}
......
......@@ -5,6 +5,7 @@ import org.vadere.simulator.projects.dataprocessing.processor.FootStepProcessor;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import tech.tablesaw.api.Table;
import tech.tablesaw.io.csv.CsvReadOptions;
......@@ -41,6 +42,7 @@ public class TrajectoryReader {
dataFrame = Table.read().usingOptions(options);
ColumnNames columnNames = ColumnNames.getInstance();
if(columnNames.hasDuplicates(dataFrame)) {
//System.out.println("The header of table " + dataFrame + " is contains duplicates which can lead to unwanted side effects");
throw new IOException("The header of table " + dataFrame + " is contains duplicates which can lead to unwanted side effects");
}
return dataFrame;
......
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