Commit a8423fe1 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add INET environment export

parent 5416a896
......@@ -248,6 +248,7 @@ ProjectView.btnSnapshot.tooltip=Snapshot
ProjectView.btnPNGSnapshot.tooltip=PNG Snapshot
ProjectView.btnSVGSnapshot.tooltip=SVG Snapshot
ProjectView.btnTikZSnapshot.tooltip=TikZ Snapshot
ProjectView.btnINETSnapshot.tooltip=INET Env
ProjectView.btnPolySnapshot.tooltip=Poly Snapshot
PostVis.menuFile.title=File
PostVis.menuSettings.title=Setting
......@@ -269,7 +270,7 @@ TopographyBoundDialog.tooltip=Set topography bounds
InformationDialogError.title=Internal Error
InformationDialogFileError=Could not load file!
LoadingDialog.title=Loading...
FileDialog.filenamePrefix=vadere_
FileDialog.filenamePrefix=vadexre_
SettingsDialog.title=Settings
SettingsDialog.colors.border.text=Element coloring
......
......@@ -244,6 +244,7 @@ ProjectView.btnSnapshot.tooltip=Snapshot
ProjectView.btnPNGSnapshot.tooltip=PNG-Snapshot
ProjectView.btnSVGSnapshot.tooltip=SVG-Snapshot
ProjectView.btnTikZSnapshot.tooltip=TikZ-Snapshot
ProjectView.btnINETSnapshot.tooltip=INET Env
ProjectView.btnPolySnapshot.tooltip=Poly-Snapshot
PostVis.menuFile.title=Datei
PostVis.menuSettings.title=Einstellungen
......
package org.vadere.gui.components.control.simulation;
import org.apache.commons.configuration2.Configuration;
import org.vadere.gui.components.model.DefaultSimulationConfig;
import org.vadere.gui.components.model.SimulationModel;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.components.view.SimulationRenderer;
import org.vadere.gui.onlinevisualization.view.IRendererChangeListener;
import org.vadere.gui.postvisualization.utils.InetEnvironmentGenerator;
import org.vadere.util.config.VadereConfig;
import org.vadere.util.logging.Logger;
import java.awt.event.ActionEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
public class ActionGenerateINETenv extends AbstractAction implements IRendererChangeListener {
private static Logger logger = Logger.getLogger(ActionGenerateINETenv.class);
private static final Configuration CONFIG = VadereConfig.getConfig();
private final InetEnvironmentGenerator generator;
private final SimulationModel<? extends DefaultSimulationConfig> model;
public ActionGenerateINETenv(final String name, final Icon icon, final SimulationRenderer renderer,
final SimulationModel<? extends DefaultSimulationConfig> model) {
super(name, icon);
this.generator = new InetEnvironmentGenerator(renderer, model);
this.model = model;
}
@Override
public void actionPerformed(ActionEvent e) {
Date todaysDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(CONFIG.getString("SettingsDialog.dataFormat"));
String formattedDate = formatter.format(todaysDate);
JFileChooser fileChooser = new JFileChooser(VadereConfig.getConfig().getString("SettingsDialog.snapshotDirectory.path", "."));
File outputFile = new File(Messages.getString("FileDialog.filenamePrefix") + formattedDate + ".xml");
fileChooser.setSelectedFile(outputFile);
int returnVal = fileChooser.showDialog(null, "Save");
if (returnVal == JFileChooser.APPROVE_OPTION) {
outputFile = fileChooser.getSelectedFile().toString().endsWith(".xml") ? fileChooser.getSelectedFile()
: new File(fileChooser.getSelectedFile().toString() + ".xml");
generator.generateInetEnvironment(outputFile);
VadereConfig.getConfig().setProperty("SettingsDialog.snapshotDirectory.path", outputFile.getParentFile().getAbsolutePath());
}
}
@Override
public void update(SimulationRenderer renderer) {
}
}
......@@ -2,9 +2,19 @@ package org.vadere.gui.onlinevisualization.view;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.apache.commons.configuration2.Configuration;
import org.vadere.gui.components.control.*;
import org.vadere.gui.components.control.simulation.*;
import org.vadere.gui.components.control.ActionGeneratePoly;
import org.vadere.gui.components.control.IViewportChangeListener;
import org.vadere.gui.components.control.JViewportChangeListener;
import org.vadere.gui.components.control.PanelResizeListener;
import org.vadere.gui.components.control.ViewportChangeListener;
import org.vadere.gui.components.control.simulation.ActionGenerateINETenv;
import org.vadere.gui.components.control.simulation.ActionGeneratePNG;
import org.vadere.gui.components.control.simulation.ActionGenerateSVG;
import org.vadere.gui.components.control.simulation.ActionGenerateTikz;
import org.vadere.gui.components.control.simulation.ActionSwapSelectionMode;
import org.vadere.gui.components.control.simulation.ActionVisualization;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.components.utils.Resources;
import org.vadere.gui.components.utils.SwingUtils;
......@@ -18,13 +28,14 @@ import org.vadere.gui.onlinevisualization.control.ActionShowPotentialField;
import org.vadere.gui.onlinevisualization.model.OnlineVisualizationModel;
import org.vadere.util.config.VadereConfig;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.*;
public class OnlineVisualisationWindow extends JPanel implements Observer {
private static final long serialVersionUID = 3522170593760789565L;
......@@ -180,6 +191,12 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
renderer,
model);
ActionGenerateINETenv generateINETenv = new ActionGenerateINETenv(
Messages.getString("ProjectView.btnINETSnapshot.tooltip"),
resources.getIcon("camera_tikz.png", iconWidth, iconHeight),
renderer,
model);
ActionGeneratePoly generatePoly = new ActionGeneratePoly(
Messages.getString("ProjectView.btnPolySnapshot.tooltip"),
resources.getIcon("camera_poly.png", iconWidth, iconHeight),
......@@ -193,6 +210,7 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
mainPanel.addRendererChangeListener(generatePNG);
mainPanel.addRendererChangeListener(generateSVG);
mainPanel.addRendererChangeListener(generateTikz);
mainPanel.addRendererChangeListener(generateINETenv);
mainPanel.addRendererChangeListener(showPotentialField);
......@@ -220,6 +238,7 @@ public class OnlineVisualisationWindow extends JPanel implements Observer {
imgOptions.add(generatePNG);
imgOptions.add(generateSVG);
imgOptions.add(generateTikz);
imgOptions.add(generateINETenv);
imgOptions.add(generatePoly);
ActionOnlineVisMenu imgDialog = new ActionOnlineVisMenu(
......
package org.vadere.gui.postvisualization.utils;
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.Obstacle;
import org.vadere.state.scenario.Topography;
import org.vadere.util.geometry.Vector3D;
import org.vadere.util.geometry.shapes.VPoint;
import org.vadere.util.geometry.shapes.VPolygon;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Path2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
public class InetEnvironmentGenerator {
private final static Logger logger = Logger.getLogger(InetEnvironmentGenerator.class);
private final SimulationRenderer renderer;
private final SimulationModel<? extends DefaultSimulationConfig> model;
public InetEnvironmentGenerator(final SimulationRenderer renderer,
final SimulationModel<? extends DefaultSimulationConfig> model){
this.renderer = renderer;
this.model = model;
}
public void generateInetEnvironment(final File file){
AffineTransform affTransformation = new AffineTransform();
affTransformation.scale(1.0, -1.0);
affTransformation.translate(0.0, -1*model.getTopography().getBounds().getHeight());
StringBuilder sb = new StringBuilder();
sb.append("<environment>").append("\n");
createObjectsWithShape(affTransformation, sb);
sb.append("</environment>").append("\n");
// TODO: maybe uses Java's resources notation (in general, writing the file should be done by the caller not here).
try {
file.createNewFile();
Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
out.write(sb.toString());
out.flush();
logger.info("generate new INET environment file: " + file.getAbsolutePath());
} catch (IOException e1) {
logger.error(e1.getMessage());
e1.printStackTrace();
}
}
private void createObjectsWithShape(AffineTransform affTransformation , StringBuilder sb){
List<Obstacle> obstacles = new ArrayList<>();
if (!model.getTopography().hasBoundary()){
obstacles.addAll(Topography.createObstacleBoundary(model.getTopography()));
}
obstacles.addAll(model.getTopography().getObstacles());
int maxId = obstacles.stream().map(Obstacle::getId).max(Integer::compareTo).get() + 1;
for(Obstacle o : obstacles){
// set id for obstacles without any id.
if (o.getId() == -1){
o.setId(maxId);
maxId++;
}
// flip y-axis of shapes
VShape transformedShape = new VPolygon(new Path2D.Double(o.getShape(), affTransformation));
EnvObject envObject = EnvObject.createPrism(o.getId(), transformedShape, 5.0, new Color(200,0,0));
sb.append(envObject.xml()).append("\n");
}
}
enum PosType {
MIN("min"),
MAX("max"),
CENTER("center");
private String tag;
PosType(String tag){
this.tag = tag;
}
}
enum ShapeType{
SPHERE("sphere"),
CUBIOD("cuboid"),
PRISM("prism"),
POLYH("polyhedron"),
;
private String tag;
ShapeType(String tag){
this.tag = tag;
}
}
private static class EnvObject{
private int id;
private Vector3D orientation;
private Vector3D position;
private PosType positionType;
private double[] shape;
private ShapeType shapeType;
private String material;
private Color fillColor;
private double opacity;
private String texture;
static EnvObject createPrism(int id, VShape shape, double height, Color fillColor){
EnvObject envObject = new EnvObject();
envObject.id = id;
envObject.shapeType = ShapeType.PRISM;
envObject.positionType = PosType.MIN;
envObject.orientation = new Vector3D(0.0, 0.0, 0.0);
envObject.material = "brick";
envObject.fillColor = fillColor;
envObject.opacity = 0.75;
envObject.texture = "brick.jpg";
envObject.buildPrismShape(shape, height);
return envObject;
}
private VPoint pos2D(){
return new VPoint(position.x, position.y);
}
private void buildPrismShape(VShape transformedShape, double height){
List<VPoint> points = transformedShape.getPath();
VPoint base = new VPoint(transformedShape.getBounds2D().getMinX(), transformedShape.getBounds2D().getMinY());
position = new Vector3D(base.x, base.y, 0.0);
// get 2D representation of position point. Negative to use as base.
VPoint negPos2d = pos2D().scalarMultiply(-1.0);
this.shape = new double[points.size() *2 + 1];
this.shape[0] = height;
int idx = 1;
for (VPoint p : points){
VPoint relPoint = negPos2d.addPrecise(p);
this.shape[idx] = relPoint.x;
this.shape[idx+1] = relPoint.y;
idx+=2;
}
}
public String xml(){
String sb = "<object " +
"id=\"" + id + "\" " +
"orientation=\"" + str(orientation) + "\" " +
"position=\"" + positionType.tag + " " + str(position) + "\" " +
"shape=\"" + shapeType.tag + " " + str(shape) + "\" " +
"material=\"" + material + "\" " +
"fill-color=\"" + str(fillColor) + "\" " +
"opacity=\"" + str(opacity) + "\" " +
"texture=\"" + texture + "\" " +
"/>";
return sb;
}
private String str(Vector3D v){
return String.format("%f %f %f", v.x, v.y, v.z);
}
private String str(double d){
return String.format("%f", d);
}
private String str(double[] data){
StringBuilder sb = new StringBuilder();
for (double datum : data) {
sb.append(String.format("%f", datum)).append(" ");
}
sb.delete(sb.length()-1, sb.length());
return sb.toString();
}
private String str(Color col){
return col.getRed() + " " + col.getBlue() + " " + col.getGreen();
}
private EnvObject(){
}
}
}
......@@ -2,17 +2,35 @@ package org.vadere.gui.postvisualization.view;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import org.apache.commons.configuration2.Configuration;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.control.*;
import org.vadere.gui.components.control.simulation.*;
import org.vadere.gui.components.control.ActionGeneratePoly;
import org.vadere.gui.components.control.IViewportChangeListener;
import org.vadere.gui.components.control.JViewportChangeListener;
import org.vadere.gui.components.control.PanelResizeListener;
import org.vadere.gui.components.control.ViewportChangeListener;
import org.vadere.gui.components.control.simulation.ActionGenerateINETenv;
import org.vadere.gui.components.control.simulation.ActionGeneratePNG;
import org.vadere.gui.components.control.simulation.ActionGenerateSVG;
import org.vadere.gui.components.control.simulation.ActionGenerateTikz;
import org.vadere.gui.components.control.simulation.ActionSwapSelectionMode;
import org.vadere.gui.components.control.simulation.ActionVisualization;
import org.vadere.gui.components.model.IDefaultModel;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.components.utils.Resources;
import org.vadere.gui.components.utils.SwingUtils;
import org.vadere.gui.components.view.DialogFactory;
import org.vadere.gui.components.view.ScenarioElementView;
import org.vadere.gui.postvisualization.control.*;
import org.vadere.gui.postvisualization.control.ActionOpenFile;
import org.vadere.gui.postvisualization.control.ActionPause;
import org.vadere.gui.postvisualization.control.ActionPlay;
import org.vadere.gui.postvisualization.control.ActionRecording;
import org.vadere.gui.postvisualization.control.ActionRemoveFloorFieldFile;
import org.vadere.gui.postvisualization.control.ActionShowPotentialField;
import org.vadere.gui.postvisualization.control.ActionStop;
import org.vadere.gui.postvisualization.control.ActionVisualizationMenu;
import org.vadere.gui.postvisualization.control.Player;
import org.vadere.gui.postvisualization.model.PostvisualizationModel;
import org.vadere.gui.projectview.control.ActionDeselect;
import org.vadere.simulator.projects.Scenario;
......@@ -20,10 +38,14 @@ import org.vadere.simulator.projects.io.IOOutput;
import org.vadere.util.config.VadereConfig;
import org.vadere.util.io.IOUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.*;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
......@@ -32,6 +54,8 @@ import java.util.List;
import java.util.Observer;
import java.util.Optional;
import javax.swing.*;
/**
* Main Window of the new post visualization.
*/
......@@ -246,6 +270,9 @@ public class PostvisualizationWindow extends JPanel implements Observer, DropTar
renderer, model);
AbstractAction tikzImg = new ActionGenerateTikz(Messages.getString("ProjectView.btnTikZSnapshot.tooltip"), resources.getIcon("camera_tikz.png", iconWidth, iconHeight),
renderer, model);
AbstractAction inetImg = new ActionGenerateINETenv(Messages.getString("ProjectView.btnINETSnapshot.tooltip"), resources.getIcon("camera_tikz.png", iconWidth, iconHeight),
renderer, model);
AbstractAction polyImg = new ActionGeneratePoly(Messages.getString("ProjectView.btnPolySnapshot.tooltip"), resources.getIcon("camera_poly.png", iconWidth, iconHeight),
model);
......@@ -255,6 +282,7 @@ public class PostvisualizationWindow extends JPanel implements Observer, DropTar
imgOptions.add(pngImg);
imgOptions.add(svgImg);
imgOptions.add(tikzImg);
imgOptions.add(inetImg);
imgOptions.add(polyImg);
// add Action to List ....
......
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