Commit 0a7ec7eb authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier

Renamed "TikZGenerator.java" to "TikzGenerator.java" and draw TikZ paths...

Renamed "TikZGenerator.java" to "TikzGenerator.java" and draw TikZ paths instead of pre-defined shapes for scenario elements.
parent 4fc22b68
Pipeline #57971 passed with stage
in 49 seconds
......@@ -4,31 +4,24 @@ import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.gui.components.utils.Resources;
import org.vadere.gui.postvisualization.PostVisualisation;
import org.vadere.gui.postvisualization.utils.SVGGenerator;
import org.vadere.gui.postvisualization.utils.TikZGenerator;
import org.vadere.gui.postvisualization.utils.TikzGenerator;
import org.vadere.gui.postvisualization.view.PostvisualizationRenderer;
import org.vadere.state.scenario.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.prefs.Preferences;
public class ActionGenerateTikZ extends ActionVisualization {
private static Logger logger = LogManager.getLogger(ActionGenerateTikZ.class);
public class ActionGenerateTikz extends ActionVisualization {
private static Logger logger = LogManager.getLogger(ActionGenerateTikz.class);
private static Resources resources = Resources.getInstance("postvisualization");
private final TikZGenerator tikZGenerator;
private final TikzGenerator tikzGenerator;
public ActionGenerateTikZ(final String name, final Icon icon, final PostvisualizationRenderer renderer) {
public ActionGenerateTikz(final String name, final Icon icon, final PostvisualizationRenderer renderer) {
super(name, icon, renderer.getModel());
this.tikZGenerator = new TikZGenerator(renderer, renderer.getModel());
this.tikzGenerator = new TikzGenerator(renderer, renderer.getModel());
}
@Override
......@@ -50,7 +43,7 @@ public class ActionGenerateTikZ extends ActionVisualization {
: new File(fileChooser.getSelectedFile().toString() + ".tex");
boolean completeDocument = true;
tikZGenerator.generateTikZ(outputFile, completeDocument);
tikzGenerator.generateTikz(outputFile, completeDocument);
}
}
}
package org.vadere.gui.postvisualization.utils;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.gui.components.model.DefaultSimulationConfig;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.components.view.SimulationRenderer;
import org.vadere.state.scenario.*;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.io.*;
import java.util.Arrays;
public class TikZGenerator {
private static Logger logger = LogManager.getLogger(TikZGenerator.class);
private SimulationRenderer renderer;
private final SimulationModel<? extends DefaultSimulationConfig> model;
public TikZGenerator(final SimulationRenderer renderer,
final SimulationModel<? extends DefaultSimulationConfig> model) {
this.renderer = renderer;
this.model = model;
}
public void generateTikZ(final File file, final boolean generateCompleteDocument) {
String template = "" +
"\\documentclass{standalone}\n" +
"\\usepackage{tikz}\n\n" +
"\\begin{document}\n" +
"\\begin{tikzpicture}\n" +
"%s" +
"\\end{tikzpicture}\n" +
"\\end{document}\n";
String tikZCode = generateTikZCode();
String output = (generateCompleteDocument) ? String.format(template, tikZCode) : tikZCode ;
try {
file.createNewFile();
Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
out.write(output);
out.flush();
logger.info("generate new TikZ: " + file.getAbsolutePath());
} catch (IOException e1) {
logger.error(e1.getMessage());
e1.printStackTrace();
}
}
// TODO: draw path(!) of specified object and not a pre-defined form like a circle by extending method "generatePathForScenarioElement()".
private String generateTikZCode() {
String generatedCode = "";
// TODO: maybe, use StringBuilder to be thread-safe.
// Generate agents.
String agentTextPattern = "\\fill[blue] (%f,%f) circle [radius=%fcm];\n";
String agents = "% Agents\n";
for (Agent agent : model.getAgents()) {
agents += String.format(agentTextPattern, agent.getPosition().x, agent.getPosition().y, agent.getRadius());
}
String sourceTextPattern = "\\fill[green] (%f,%f) rectangle (%f,%f);\n";
String sources = "% Sources\n";
for (Source source : model.getTopography().getSources()) {
Rectangle2D sourceBounds = source.getShape().getBounds2D();
sources += String.format(sourceTextPattern, sourceBounds.getX(), sourceBounds.getY(), sourceBounds.getX() + sourceBounds.getWidth(), sourceBounds.getY() + sourceBounds.getHeight());
}
String targetTextPattern = "\\fill[orange] (%f,%f) rectangle (%f,%f);\n";
String targets = "% Targets\n";
for (Target target : model.getTopography().getTargets()) {
Rectangle2D targetBounds = target.getShape().getBounds2D();
targets += String.format(targetTextPattern, targetBounds.getX(), targetBounds.getY(), targetBounds.getX() + targetBounds.getWidth(), targetBounds.getY() + targetBounds.getHeight());
}
String obstacleTextPattern = "\\fill[black] (%f,%f) rectangle (%f,%f);\n";
String obstacles = "% Obstacles\n";
for (Obstacle obstacle : model.getTopography().getObstacles()) {
Rectangle2D obstacleBounds = obstacle.getShape().getBounds2D();
obstacles += String.format(obstacleTextPattern, obstacleBounds.getX(), obstacleBounds.getY(), obstacleBounds.getX() + obstacleBounds.getWidth(), obstacleBounds.getY() + obstacleBounds.getHeight());
}
generatedCode = sources + targets + obstacles + agents;
return generatedCode;
}
private void generatePathForScenarioElement(DynamicElement element) {
float[] coords = new float[6];
AffineTransform noTransformation = new AffineTransform();
PathIterator it = element.getShape().getPathIterator(noTransformation);
int n = 0;
while (!it.isDone()) {
int type = it.currentSegment(coords);
System.out.println("segment type= " + type + " with coords: " + Arrays.toString(coords));
n++;
it.next();
}
System.out.println("path elements = " + n);
}
}
package org.vadere.gui.postvisualization.utils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.vadere.gui.components.model.DefaultSimulationConfig;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.components.view.SimulationRenderer;
import org.vadere.state.scenario.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.io.*;
import java.util.*;
import static java.awt.geom.PathIterator.*;
/**
* Convert the (Java) scenario description into a TikZ representation.
*
* Usually, each (Java) scenario element is represented as a path from
* @see PathIterator This PathSeparator must be converted into its TikZ
* representation.
*
* Also use configured colors from GUI settings.
*/
public class TikzGenerator {
private static Logger logger = LogManager.getLogger(TikzGenerator.class);
private SimulationRenderer renderer;
private final SimulationModel<? extends DefaultSimulationConfig> model;
private String[] translationTable;
public TikzGenerator(final SimulationRenderer renderer,
final SimulationModel<? extends DefaultSimulationConfig> model) {
this.renderer = renderer;
this.model = model;
translationTable = new String[SEG_CLOSE + 1];
initializeTranslationTable(translationTable);
}
private void initializeTranslationTable(String[] translationTable) {
// Map Java path commands to TikZ commands.
translationTable[SEG_MOVETO] = "(%f,%f) ";
translationTable[SEG_LINETO] = "to (%f,%f) ";
translationTable[SEG_QUADTO] = "How to convert SEG_QUADTO to TikZ?";
translationTable[SEG_CUBICTO] = "How to convert SEG_CUBICTO to TikZ?";
translationTable[SEG_CLOSE] = "to cycle;";
}
public void generateTikz(final File file, final boolean generateCompleteDocument) {
String texTemplate = "" +
"\\documentclass{standalone}\n" +
"\\usepackage{tikz}\n\n" +
"\\begin{document}\n" +
"\\begin{tikzpicture}\n" +
"%s" +
"\\end{tikzpicture}\n" +
"\\end{document}\n";
String tikzCode = convertScenarioElementsToTikz();
String output = (generateCompleteDocument) ? String.format(texTemplate, tikzCode) : tikzCode ;
// TODO: maybe uses Java's resources notation.
try {
file.createNewFile();
Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
out.write(output);
out.flush();
logger.info("generate new TikZ: " + file.getAbsolutePath());
} catch (IOException e1) {
logger.error(e1.getMessage());
e1.printStackTrace();
}
}
private String convertScenarioElementsToTikz() {
String generatedCode = "";
Topography topography = model.getTopography();
generatedCode += "% Boundary Box\n";
String boundaryBoxTextPattern = "\\fill[white] (%f,%f) rectangle (%f,%f);\n";
generatedCode += String.format(boundaryBoxTextPattern,
topography.getBounds().x,
topography.getBounds().y,
topography.getBounds().x + topography.getBounds().width,
topography.getBounds().y + topography.getBounds().height);
// TODO: use colors from GUI config and maybe also draw trajectories.
generatedCode += "% Sources\n";
for (Source source : topography.getSources()) {
generatedCode += String.format("\\fill[green] %s\n", generatePathForScenarioElement(source));
}
generatedCode += "% Targets\n";
for (Target target : topography.getTargets()) {
generatedCode += String.format("\\fill[orange] %s\n", generatePathForScenarioElement(target));
}
generatedCode += "% Obstacles\n";
for (Obstacle obstacle : topography.getObstacles()) {
generatedCode += String.format("\\fill[black] %s\n", generatePathForScenarioElement(obstacle));
}
// TODO: add agents as path NOT as pre-defined form (they require cubic splines).
generatedCode += "% Agents\n";
for (Agent agent : model.getAgents()) {
String agentTextPattern = "\\fill[blue] (%f,%f) circle [radius=%fcm];\n";
generatedCode += String.format(agentTextPattern, agent.getPosition().x, agent.getPosition().y, agent.getRadius());
}
return generatedCode;
}
private String generatePathForScenarioElement(ScenarioElement element) {
String generatedPath = "";
AffineTransform noTransformation = new AffineTransform();
PathIterator pathIterator = element.getShape().getPathIterator(noTransformation);
while (!pathIterator.isDone()) {
float[] coords = new float[6];
int type = pathIterator.currentSegment(coords);
generatedPath += convertJavaToTikzPath(type, coords);
// System.out.println("segment type= " + type + " with coords: " + Arrays.toString(coords));
pathIterator.next();
}
// System.out.println("TikZ path: " + generatedPath);
return generatedPath;
}
private String convertJavaToTikzPath(int type, float[] coords) {
String convertedPath = "";
if (type > SEG_CLOSE + 1) {
throw new IllegalStateException(String.format("Cannot process path segment type: %d (coordinates: %s)", type, Arrays.toString(coords)));
}
convertedPath = translationTable[type];
if (type == SEG_MOVETO) {
convertedPath = String.format(convertedPath, coords[0], coords[1]);
} else if (type == SEG_LINETO) {
convertedPath = String.format(convertedPath, coords[0], coords[1]);
} else if (type == SEG_QUADTO) {
// TODO
} else if (type == SEG_CUBICTO) {
// TODO
}
return convertedPath;
}
}
......@@ -19,12 +19,10 @@ import org.vadere.gui.projectview.control.ActionDeselect;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.io.HashGenerator;
import org.vadere.simulator.projects.io.IOOutput;
import org.vadere.util.geometry.shapes.VRectangle;
import org.vadere.util.io.IOUtils;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
......@@ -250,7 +248,7 @@ public class PostvisualizationWindow extends JPanel implements Observer {
"PostVis.btnSVGSnapshot.tooltip");
addActionToToolbar(
toolbar,
new ActionGenerateTikZ("tikz_snapshot", resources.getIcon("tikz_icon.png", iconWidth, iconHeight),
new ActionGenerateTikz("tikz_snapshot", resources.getIcon("tikz_icon.png", iconWidth, iconHeight),
renderer),
"PostVis.btnTikZSnapshot.tooltip");
......
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