Commit 2a1297e0 authored by Benedikt Kleinmeier's avatar Benedikt Kleinmeier
Browse files

In "ActionRandomPedestrianDialog.java", introduced radio buttons to select...

In "ActionRandomPedestrianDialog.java", introduced radio buttons to select target option for created pedestrians
parent dceda168
Pipeline #253876 passed with stages
in 132 minutes and 24 seconds
......@@ -354,6 +354,9 @@ TopographyCreator.btnLine.label=Line
TopographyCreator.btnRectangle.label=Rectangle
TopographyCreator.btnSubtractMeasurementArea.label=Subtract Obstacle from Area
TopographyCreator.PlaceRandomPedestrians.label=Place Random Pedestrians
TopographyCreator.PlaceRandomPedestrians.targetEmptyOption.label=Empty
TopographyCreator.PlaceRandomPedestrians.targetRandomOption.label=Random
TopographyCreator.PlaceRandomPedestrians.targetListOption.label=Use list
TopographyCreator.btnChecker.tooltip=Show Topography Checker Messages
TopographyCreator.btnGenerateIds.tooltip=Generate missing Ids for sources, targets, ...
......
......@@ -349,6 +349,9 @@ TopographyCreator.btnLine.label=Linie
TopographyCreator.btnRectangle.label=Rechteck
TopographyCreator.btnSubtractMeasurementArea.label=Subtrahiere Hindernis von Messfl\u00e4che
TopographyCreator.PlaceRandomPedestrians.label=Plaziere Zuf\u00e4llige Fu\u00dfg\u00e4nger
TopographyCreator.PlaceRandomPedestrians.targetEmptyOption.label=Kein Ziel
TopographyCreator.PlaceRandomPedestrians.targetRandomOption.label=Zuf\u00e4llig
TopographyCreator.PlaceRandomPedestrians.targetListOption.label=Nutze Liste
TopographyCreator.btnChecker.tooltip=Topography Linter Nachrichten
TopographyCreator.btnGenerateIds.tooltip=Erzeuge fehlende Ids f\u00fcr Quellen, Ziele, ...
......
package org.vadere.gui.topographycreator.control;
import org.apache.commons.math3.distribution.UniformIntegerDistribution;
import org.apache.commons.math3.random.JDKRandomGenerator;
import org.vadere.gui.topographycreator.model.AgentWrapper;
import org.vadere.gui.topographycreator.model.IDrawPanelModel;
import org.vadere.gui.topographycreator.model.TopographyElementFactory;
import org.vadere.gui.topographycreator.view.ActionRandomPedestrianDialog;
import org.vadere.gui.topographycreator.view.ActionRandomPedestrianDialog.TARGET_OPTION;
import org.vadere.state.attributes.scenario.AttributesAgent;
import org.vadere.state.scenario.Agent;
import org.vadere.state.scenario.Pedestrian;
......@@ -14,20 +13,22 @@ import org.vadere.state.types.ScenarioElementType;
import org.vadere.util.geometry.shapes.IPoint;
import org.vadere.util.geometry.shapes.VCircle;
import org.vadere.util.geometry.shapes.VShape;
import org.vadere.util.logging.Logger;
import org.vadere.util.random.SimpleReachablePointProvider;
import javax.swing.*;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
import javax.swing.*;
import javax.swing.undo.UndoableEdit;
import javax.swing.undo.UndoableEditSupport;
public class ActionPlaceRandomPedestrians extends TopographyAction {
private static Logger logger = Logger.getLogger(ActionPlaceRandomPedestrians.class);
private final UndoableEditSupport undoSupport;
private final double dotRadius;
......@@ -46,7 +47,7 @@ public class ActionPlaceRandomPedestrians extends TopographyAction {
IDrawPanelModel model = getScenarioPanelModel();
int numOfPeds = dialog.getNumOfPeds();
Supplier<LinkedList<Integer>> targetSupplier = getTargetSupplier(dialog);
Random random = dialog.getRandom();
Rectangle2D.Double legalBound = dialog.getBoundaryRectangle();
......@@ -68,7 +69,7 @@ public class ActionPlaceRandomPedestrians extends TopographyAction {
model.getCurrentType();
model.hideSelection();
AgentWrapper element = (AgentWrapper)TopographyElementFactory.getInstance().createScenarioShape(model.getCurrentType(), model.getSelectionShape());
element.getAgentInitialStore().setTargets(targetSupplier.get());
element.getAgentInitialStore().setTargets(getTargetList(dialog.getTargetOption(), dialog.getTargetList()));
model.addShape(element);
model.setSelectedElement(element);
......@@ -81,27 +82,23 @@ public class ActionPlaceRandomPedestrians extends TopographyAction {
getScenarioPanelModel().notifyObservers();
}
// if no target is given via the dialog select a single random target existing in the topography.
private Supplier<LinkedList<Integer>> getTargetSupplier(ActionRandomPedestrianDialog dialog){
if (dialog.useRandomTargets()){
Integer[] targets = getScenarioPanelModel().getTopography().getTargets()
.stream()
.map(Target::getId)
.toArray(Integer[]::new);
return new Supplier<LinkedList<Integer>>() {
UniformIntegerDistribution dist =
new UniformIntegerDistribution(new JDKRandomGenerator(dialog.getRandom().nextInt()), 0, targets.length -1);
@Override
public LinkedList<Integer> get() {
LinkedList<Integer> ret = new LinkedList<>();
ret.add(targets[dist.sample()]);
return ret;
}
};
} else {
return dialog::getSelectedTargets;
private LinkedList<Integer> getTargetList(TARGET_OPTION selectedOption, LinkedList<Integer> dialogList) {
LinkedList<Integer> targetList = new LinkedList<>();
if (selectedOption == TARGET_OPTION.EMPTY) {
// Nothing to do here.
} else if (selectedOption == TARGET_OPTION.RANDOM) {
List<Target> targets = getScenarioPanelModel().getTopography().getTargets();
Random random = new Random();
Target randomTarget = targets.get(random.nextInt(targets.size()));
targetList.add(randomTarget.getId());
} else if (selectedOption == TARGET_OPTION.USE_LIST) {
targetList.addAll(dialogList);
}
return targetList;
}
private boolean checkOverlap(VShape newPedestrian){
......
package org.vadere.gui.topographycreator.view;
import org.jetbrains.annotations.NotNull;
import org.vadere.gui.components.utils.Messages;
import org.vadere.gui.projectview.view.ProjectView;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.geom.Rectangle2D;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
public class ActionRandomPedestrianDialog {
public enum TARGET_OPTION { EMPTY, RANDOM, USE_LIST };
private JTextField numberOfPeds_field;
private JTextField boundaryRectangle_field;
private JTextField targets_field;
private JTextField seed_field;
private JRadioButton rbTargetEmpty;
private JRadioButton rbTargetRandom;
private JRadioButton rbTargetUseList;
private JPanel panel;
private boolean valid;
private int numOfPeds;
private Rectangle2D.Double boundaryRectangle;
private LinkedList<Integer> selectedTargets;
private LinkedList<Integer> targetList;
private int seed;
private Random random;
......@@ -78,16 +87,12 @@ public class ActionRandomPedestrianDialog {
targets_field.getDocument().addDocumentListener(new SimpleDocumentListener() {
@Override
public void handle(DocumentEvent e) {
String text = targets_field.getText().replace(" ", "");
String[] tmp = text.split(",");
try{
selectedTargets= Arrays.stream(tmp).mapToInt(Integer::parseInt).boxed().collect(Collectors.toCollection(LinkedList::new));
if (selectedTargets.size() > 1 && selectedTargets.contains(-1)){
selectedTargets.removeIf(i-> i==-1);
StringJoiner j = new StringJoiner(",");
selectedTargets.forEach(i -> j.add(Integer.toString(i)));
SwingUtilities.invokeLater(() -> targets_field.setText(j.toString()));
}
String strippedText = targets_field.getText().replace(" ", "");
String[] splittedText = strippedText.split(",");
try {
targetList = Arrays.stream(splittedText).mapToInt(Integer::parseInt).boxed().collect(Collectors.toCollection(LinkedList::new));
valid = true;
targets_field.setForeground(Color.BLACK);
}catch (Exception ex){
valid = false;
......@@ -95,6 +100,9 @@ public class ActionRandomPedestrianDialog {
}
}
});
JPanel panelRadioButtons = createTargetRadioButtonPanel();
seed_field = new JTextField("-1", 15);
seed_field.setHorizontalAlignment(JTextField.RIGHT);
seed_field.getDocument().addDocumentListener(new SimpleDocumentListener() {
......@@ -119,20 +127,54 @@ public class ActionRandomPedestrianDialog {
panel.add(numberOfPeds_field, c(GridBagConstraints.HORIZONTAL, 1, 0));
panel.add(new JLabel("In Boundary Rectangle"), c(GridBagConstraints.HORIZONTAL, 0, 1));
panel.add(boundaryRectangle_field, c(GridBagConstraints.HORIZONTAL, 1, 1));
panel.add(new JLabel("Set Targets (-1 for random)"), c(GridBagConstraints.HORIZONTAL, 0, 2));
panel.add(targets_field, c(GridBagConstraints.HORIZONTAL, 1, 2));
panel.add(new JLabel("Set Random Seed (-1 for random)"), c(GridBagConstraints.HORIZONTAL, 0, 3));
panel.add(seed_field, c(GridBagConstraints.HORIZONTAL, 1, 3));
panel.add(new JLabel("Set Targets"), c(GridBagConstraints.HORIZONTAL, 0, 2));
panel.add(panelRadioButtons, c(GridBagConstraints.HORIZONTAL, 1, 2));
panel.add(targets_field, c(GridBagConstraints.HORIZONTAL, 1, 3));
panel.add(new JLabel("Set Random Seed (-1 for random)"), c(GridBagConstraints.HORIZONTAL, 0, 4));
panel.add(seed_field, c(GridBagConstraints.HORIZONTAL, 1, 4));
panel.add(new JLabel("May take a while because intelligent distance function not available yet..."),
c(GridBagConstraints.HORIZONTAL, 0,4,2));
c(GridBagConstraints.HORIZONTAL, 0,5,2));
numOfPeds = 10;
selectedTargets = new LinkedList<>();
selectedTargets.add(-1);
targetList = new LinkedList<>();
targetList.add(-1);
seed = -1;
valid = false;
}
@NotNull
private JPanel createTargetRadioButtonPanel() {
rbTargetEmpty = new JRadioButton(Messages.getString("TopographyCreator.PlaceRandomPedestrians.targetEmptyOption.label"), true);
rbTargetRandom = new JRadioButton(Messages.getString("TopographyCreator.PlaceRandomPedestrians.targetRandomOption.label"), false);
rbTargetUseList = new JRadioButton(Messages.getString("TopographyCreator.PlaceRandomPedestrians.targetListOption.label"), false);
targets_field.setEditable(false);
ButtonGroup buttonGroupTarget = new ButtonGroup();
buttonGroupTarget.add(rbTargetEmpty);
buttonGroupTarget.add(rbTargetRandom);
buttonGroupTarget.add(rbTargetUseList);
rbTargetUseList.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
boolean targetsFieldState = e.getStateChange() == ItemEvent.SELECTED;
targets_field.setEditable(targetsFieldState);
}
});
JPanel panelRadioButtons = new JPanel();
panelRadioButtons.setLayout(new FlowLayout());
int x = 0;
int y = 0;
panelRadioButtons.add(rbTargetEmpty, c(GridBagConstraints.HORIZONTAL, x, y++));
panelRadioButtons.add(rbTargetRandom, c(GridBagConstraints.HORIZONTAL, x, y++));
panelRadioButtons.add(rbTargetUseList, c(GridBagConstraints.HORIZONTAL, x, y++));
return panelRadioButtons;
}
private GridBagConstraints c(int fill, int gridx, int gridy, int width){
......@@ -160,8 +202,20 @@ public class ActionRandomPedestrianDialog {
public Rectangle2D.Double getBoundaryRectangle() { return boundaryRectangle; }
public boolean useRandomTargets(){
return selectedTargets.isEmpty() || selectedTargets.peekFirst() == -1;
public TARGET_OPTION getTargetOption() {
TARGET_OPTION selectedOption;
if (rbTargetEmpty.isSelected()) {
selectedOption = TARGET_OPTION.EMPTY;
} else if (rbTargetRandom.isSelected()) {
selectedOption = TARGET_OPTION.RANDOM;
} else if (rbTargetUseList.isSelected()) {
selectedOption = TARGET_OPTION.USE_LIST;
} else {
throw new IllegalArgumentException("No valid target option selected!");
}
return selectedOption;
}
public Random getRandom(){
......@@ -175,8 +229,8 @@ public class ActionRandomPedestrianDialog {
return random;
}
public LinkedList<Integer> getSelectedTargets() {
return selectedTargets;
public LinkedList<Integer> getTargetList() {
return targetList;
}
public boolean showDialog(){
......@@ -190,4 +244,5 @@ public class ActionRandomPedestrianDialog {
public boolean isValid() {
return valid;
}
}
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