Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

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

In "TikzGenerator", added method "drawTrajectories()".

Also refactored code a little bit:
- Now, "generatePathForScenarioElement()" does not close the path with "to cycle;".
  Instead, the caller must close the path by appending "to cycle" or simply ";" (which is required for trajectories).
parent ac78f73f
Pipeline #62776 failed with stages
in 2 minutes and 55 seconds
......@@ -6,13 +6,22 @@ import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.model.DefaultSimulationConfig;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.components.view.SimulationRenderer;
import org.vadere.gui.postvisualization.model.PostvisualizationModel;
import org.vadere.simulator.projects.dataprocessing.processor.PedestrianOSMStrideLengthProcessor;
import org.vadere.state.scenario.*;
import org.vadere.state.simulation.Step;
import org.vadere.state.simulation.Trajectory;
import org.vadere.util.geometry.shapes.VPoint;
import javax.swing.text.html.Option;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.awt.geom.PathIterator.*;
......@@ -57,7 +66,7 @@ public class TikzGenerator {
translationTable[SEG_LINETO] = "to (%f,%f) ";
translationTable[SEG_QUADTO] = ".. controls (%f,%f) .. (%f,%f) ";
translationTable[SEG_CUBICTO] = ".. controls (%f,%f) and (%f,%f) .. (%f,%f) ";
translationTable[SEG_CLOSE] = "to cycle;";
translationTable[SEG_CLOSE] = "";
}
public void generateTikz(final File file, final boolean generateCompleteDocument) {
......@@ -118,7 +127,7 @@ public class TikzGenerator {
DefaultSimulationConfig config = model.getConfig();
Topography topography = model.getTopography();
// Draw background elements first, then agents.
// Draw background elements first, then other scenario elements on toph.
generatedCode += "% Ground\n";
String groundTextPattern = (config.isShowGrid()) ? "\\draw[help lines] (%f,%f) grid (%f,%f);\n" : "\\fill[white] (%f,%f) rectangle (%f,%f);\n";
generatedCode += String.format(groundTextPattern,
......@@ -130,7 +139,7 @@ public class TikzGenerator {
if (config.isShowSources()) {
generatedCode += "% Sources\n";
for (Source source : topography.getSources()) {
generatedCode += String.format("\\fill[SourceColor] %s\n", generatePathForScenarioElement(source));
generatedCode += String.format("\\fill[SourceColor] %s;\n", generatePathForScenarioElement(source));
}
} else {
generatedCode += "% Sources (not enabled in config)\n";
......@@ -139,7 +148,7 @@ public class TikzGenerator {
if (config.isShowTargets()) {
generatedCode += "% Targets\n";
for (Target target : topography.getTargets()) {
generatedCode += String.format("\\fill[TargetColor] %s\n", generatePathForScenarioElement(target));
generatedCode += String.format("\\fill[TargetColor] %s;\n", generatePathForScenarioElement(target));
}
} else {
generatedCode += "% Targets (not enabled in config)\n";
......@@ -148,7 +157,7 @@ public class TikzGenerator {
if (config.isShowObstacles()) {
generatedCode += "% Obstacles\n";
for (Obstacle obstacle : topography.getObstacles()) {
generatedCode += String.format("\\fill[ObstacleColor] %s\n", generatePathForScenarioElement(obstacle));
generatedCode += String.format("\\fill[ObstacleColor] %s;\n", generatePathForScenarioElement(obstacle));
}
} else {
generatedCode += "% Obstacles (not enabled in config)\n";
......@@ -163,7 +172,18 @@ public class TikzGenerator {
generatedCode += "% Stairs (not enabled in config)\n";
}
// TODO: maybe, draw also trajectories.
if (config.isShowTrajectories()) {
generatedCode += "% Trajectories\n";
if (model instanceof PostvisualizationModel) {
generatedCode += drawTrajectories((PostvisualizationModel)model);
} else {
generatedCode += String.format("%% Passed model %s does not contain trajectories\n", model.getClass().getSimpleName());
}
} else {
generatedCode += "% Trajectories (not enabled in config)\n";
}
if (config.isShowPedestrians()) {
generatedCode += "% Agents\n";
generatedCode += drawAgents(config);
......@@ -174,6 +194,51 @@ public class TikzGenerator {
return generatedCode;
}
private String drawTrajectories(PostvisualizationModel model) {
// Use a thread-safe string implementation because streams are used here.
final StringBuffer generatedCode = new StringBuffer("");
Stream<Trajectory> trajectoryStream = model.getAlivePedestrians();
Step currentTimeStep = model.getStep().orElseGet(null);
if (currentTimeStep != null) {
trajectoryStream.forEach(trajectory -> {
Stream<VPoint> trajectoryPoints = trajectory.getPositionsReverse(currentTimeStep);
// Use a newline ("to\n") for joining because TeX could possibly choke up on long lines.
String trajectoryAsTikzString = trajectoryPoints
.map(point -> String.format("(%f,%f)", point.x, point.y))
.collect(Collectors.joining(" to\n"));
String coloredTrajectory = applyAgentColorToTrajectory(trajectoryAsTikzString, trajectory.getAgent(currentTimeStep));
int pedestrianId = trajectory.getPedestrianId();
Optional<Step> trajectoryEndStep = trajectory.getEndStep();
String trajectoryEndStepAsString = (trajectoryEndStep.isPresent()) ? "" + trajectoryEndStep.get().toString() : "unknown end step" ;
String currentTimeStepAsString = currentTimeStep.toString();
generatedCode.append(String.format("%% Trajectory Agent %d @ step %s of %s\n", pedestrianId, currentTimeStepAsString, trajectoryEndStepAsString));
generatedCode.append(coloredTrajectory);
});
}
return generatedCode.toString();
}
private String applyAgentColorToTrajectory(String trajectory, Optional<Agent> agent) {
String colorString = "AgentColor";
if (agent.get() instanceof Pedestrian) {
Pedestrian pedestrian = (Pedestrian)agent.get();
Color pedestrianColor = renderer.getAgentRender().getColor(pedestrian);
colorString = String.format("{rgb,255: red,%d; green,%d; blue,%d}", pedestrianColor.getRed(), pedestrianColor.getGreen(), pedestrianColor.getBlue());
}
return String.format("\\draw[draw=%s]\n%s;\n", colorString, trajectory);
}
@NotNull
private String drawAgents(DefaultSimulationConfig config) {
String generatedCode = "";
......@@ -185,8 +250,8 @@ public class TikzGenerator {
Color pedestrianColor = renderer.getAgentRender().getColor(pedestrian);
Shape pedestrianShape = renderer.getAgentRender().getShape(pedestrian);
String colorString = String.format("{rgb,255:red,%d; green,%d; blue,%d}", pedestrianColor.getRed(), pedestrianColor.getGreen(), pedestrianColor.getBlue());
generatedCode += String.format("\\fill[fill=%s] %s\n", colorString, generatePathForShape(pedestrianShape));
String colorString = String.format("{rgb,255: red,%d; green,%d; blue,%d}", pedestrianColor.getRed(), pedestrianColor.getGreen(), pedestrianColor.getBlue());
generatedCode += String.format("\\fill[fill=%s] %s;\n", colorString, generatePathForShape(pedestrianShape));
} catch (ClassCastException cce) {
logger.error("Error casting to Pedestrian");
cce.printStackTrace();
......@@ -222,7 +287,7 @@ public class TikzGenerator {
pathIterator.next();
}
return generatedPath;
return generatedPath.trim();
}
private String generatePathForShape(Shape shape) {
......@@ -239,7 +304,7 @@ public class TikzGenerator {
pathIterator.next();
}
return generatedPath;
return generatedPath.trim();
}
private String convertJavaToTikzPath(int type, float[] coords) {
......
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