package org.opentrafficsim.trafficcontrol.trafcod; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import nl.tudelft.simulation.language.Throw; import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightException; import org.opentrafficsim.trafficcontrol.TrafficController; /** * Functions that can draw a schematic diagram of an intersection given the list of traffic streams. The traffic stream numbers * must follow the Dutch conventions. *
* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version Dec 1, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class Diagram
{
/** Numbering of the lateral objects/positions from the median to the shoulder. */
/** Central divider. */
final static int DIVIDER_1 = 0;
/** Left turn area on roundabout. */
final static int CAR_ROUNDABOUT_LEFT = 1;
/** Public transit between divider and left turn lane. */
final static int PT_DIV_L = 3;
/** Divider between center public transit and left turn lane. */
final static int DIVIDER_2 = 4;
/** Left turn lane(s). */
final static int CAR_LEFT = 5;
/** No turn (center) lane(s). */
final static int CAR_CENTER = 7;
/** Right turn lane(s). */
final static int CAR_RIGHT = 9;
/** Divider between right turn lane and bicycle lane. */
final static int DIVIDER_3 = 10;
/** Public transit between right turn lane and bicycle lane. */
final static int PT_RIGHT_BICYCLE = 11;
/** Divider. */
final static int DIVIDER_4 = 12;
/** Bicycle lane. */
final static int BICYCLE = 13;
/** Divider. */
final static int DIVIDER_5 = 14;
/** Public transit between bicycle lane and right sidewalk. */
final static int PT_BICYCLE_SIDEWALK = 15;
/** Divider. */
final static int DIVIDER_6 = 16;
/** Sidewalk. */
final static int SIDEWALK = 17;
/** Divider. */
final static int DIVIDER_7 = 18;
/** Public transit right of right sidewalk. */
final static int PT_SIDEWALK_SHOULDER = 19;
/** Shoulder right of right sidewalk. */
final static int SHOULDER = 20;
/** Boundary of schematic intersection. */
final static int BOUNDARY = 21;
/** The streams crossing the intersection. */
final ListCommand.ELSE
or Command.END_IF
* @throws TrafficLightException when the Command is not ELSE or END_IF
*/
public RouteStep(final Command command) throws TrafficLightException
{
Throw.when(Command.ELSE != command && Command.END_IF != command, TrafficLightException.class,
"RouteStep constructor with single command parameter requires ELSE or END_IF command");
this.x = TrafficController.NO_STREAM;
this.y = TrafficController.NO_STREAM;
this.command = command;
this.streamCondition = TrafficController.NO_STREAM;
}
/**
* Retrieve the X object.
* @return int; the X object
*/
public int getX()
{
return this.x;
}
/**
* Retrieve the Y object.
* @return int; the Y object
*/
public int getY()
{
return this.y;
}
/**
* Retrieve the command.
* @return Command
*/
public Command getCommand()
{
return this.command;
}
/**
* Retrieve the stream condition.
* @return int; the streamCondition
*/
public int getStreamCondition()
{
return this.streamCondition;
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "RouteStep [x=" + this.x + ", y=" + this.y + ", command=" + this.command + ", streamCondition="
+ this.streamCondition + "]";
}
}
/**
* Pack two integer coordinates in one object.
*/
class XYPair
{
/** X. */
private final int x;
/** Y. */
private final int y;
/**
* Construct a new XY pair.
* @param x int; the X value
* @param y int; the Y value
*/
public XYPair(final int x, final int y)
{
this.x = x;
this.y = y;
}
/**
* Construct a new XY pair from a route step.
* @param routeStep RouteStep; the route step
*/
public XYPair(final RouteStep routeStep)
{
this.x = routeStep.getX();
this.y = routeStep.getY();
}
/**
* Construct a rotated version of an XYPair.
* @param in XYPair; the initial version
* @param quadrant int; the quadrant
*/
public XYPair(final XYPair in, final int quadrant)
{
this.x = rotatedX(in, quadrant);
this.y = rotatedY(in, quadrant);
}
/**
* Retrieve the X value.
* @return int; the X value
*/
public int getX()
{
return this.x;
}
/**
* Retrieve the Y value.
* @return int; the Y value
*/
public int getY()
{
return this.y;
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "XYPair [x=" + this.x + ", y=" + this.y + "]";
}
}
/**
* Construct a route.
* @param quadrant int; the quadrant to assemble the route for
* @param steps RouteStep...; an array, or series of arguments of type RouteStep
* @return XYPair[]; an array of XY pairs describing the route through the intersection
* @throws TrafficLightException when the route contains commands other than NO_OP and STOP_LINE
*/
private XYPair[] rotateRoute(final int quadrant, final RouteStep... steps) throws TrafficLightException
{
ListpointNo
steps
* @throws TrafficLightException when the command in a routestep is not recognized
*/
private RouteStep routePoint(final int pointNo, final RouteStep... steps) throws TrafficLightException
{
boolean active = true;
boolean beenActive = false;
int index = 0;
for (RouteStep routeStep : steps)
{
switch (routeStep.getCommand())
{
case NO_OP:
case STOP_LINE:
case ICON:
case STOP_LINE_AND_ICON:
if (active)
{
if (index++ == pointNo)
{
return routeStep;
}
}
break;
case IF:
active = streamExists((short) routeStep.getStreamCondition());
beenActive = active;
break;
case ELSE_IF:
if (active)
{
active = false;
}
else if (!beenActive)
{
active = this.streams.contains(routeStep.getStreamCondition());
}
if (active)
{
beenActive = true;
}
break;
case ELSE:
active = !beenActive;
break;
case END_IF:
active = true;
break;
default:
throw new TrafficLightException("Bad switch: " + routeStep);
}
}
return null;
}
/**
* Create a BufferedImage and render the schematic on it.
* @return BufferedImage
*/
public BufferedImage render()
{
int range = 2 * BOUNDARY + 1;
int cellSize = 10;
BufferedImage result = new BufferedImage(range * cellSize, range * cellSize, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) result.getGraphics();
graphics.setColor(Color.GREEN);
graphics.fillRect(0, 0, result.getWidth(), result.getHeight());
for (Short stream : this.streams)
{
switch (laneType(stream))
{
case BICYCLE_LANE:
graphics.setColor(Color.RED);
break;
case CAR_LANE:
graphics.setColor(Color.BLACK);
break;
case PEDESTRIAN_LANE:
graphics.setColor(Color.BLUE);
break;
case PUBLIC_TRANSIT_LANE:
graphics.setColor(Color.BLACK);
break;
default:
graphics.setColor(Color.WHITE);
break;
}
XYPair[] path = this.routes.get(stream);
if (null == path)
{
System.err.println("Cannot find path for stream " + stream);
continue;
}
XYPair prevPair = null;
for (XYPair xyPair : path)
{
if (null != prevPair)
{
int dx = (int) Math.signum(xyPair.getX() - prevPair.getX());
int dy = (int) Math.signum(xyPair.getY() - prevPair.getY());
int x = prevPair.getX() + dx;
int y = prevPair.getY() + dy;
while (x != xyPair.getX() || y != xyPair.getY())
{
fillXYPair(graphics, new XYPair(x, y));
if (x != xyPair.getX())
{
x += dx;
}
if (y != xyPair.getY())
{
y += dy;
}
}
}
fillXYPair(graphics, xyPair);
prevPair = xyPair;
}
}
return result;
}
/**
* Fill one box taking care to rotate to display conventions.
* @param graphics Graphics2D; the graphics environment
* @param xyPair XYPair; the box to fill
*/
private void fillXYPair(final Graphics2D graphics, final XYPair xyPair)
{
int cellSize = 10;
graphics.fillRect(cellSize * (BOUNDARY - xyPair.getX()), cellSize * (BOUNDARY - xyPair.getY()), cellSize, cellSize);
}
/**
* Test the Diagram code.
* @param args String[]; the command line arguments (not used)
*/
public static void main(final String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Diagram test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(1000, 1000));
JPanel mainPanel = new JPanel(new BorderLayout());
frame.add(mainPanel);
checkBoxPanel = new JPanel();
checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.Y_AXIS));
JScrollPane scrollPane = new JScrollPane(checkBoxPanel);
scrollPane.setPreferredSize(new Dimension(150, 1000));
mainPanel.add(scrollPane, BorderLayout.LINE_START);
for (int stream = 1; stream <= 12; stream++)
{
checkBoxPanel.add(makeCheckBox(stream, stream % 3 == 2));
}
for (int stream = 21; stream <= 28; stream++)
{
checkBoxPanel.add(makeCheckBox(stream, false));
}
for (int stream = 31; stream <= 38; stream++)
{
checkBoxPanel.add(makeCheckBox(stream, false));
}
for (int stream = 41; stream <= 52; stream++)
{
checkBoxPanel.add(makeCheckBox(stream, false));
}
for (int stream = 61; stream <= 72; stream++)
{
if (stream % 3 == 1)
{
continue;
}
checkBoxPanel.add(makeCheckBox(stream, false));
}
testPanel = new JPanel();
rebuildTestPanel();
mainPanel.add(testPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
});
}
/**
* Make a check box to switch a particular stream number on or off.
* @param stream int; the stream number
* @param initialState boolean; if true; the check box will be checked
* @return JCheckBox
*/
public static JCheckBox makeCheckBox(final int stream, final boolean initialState)
{
JCheckBox result = new JCheckBox(String.format("Stream %02d", stream));
result.setSelected(initialState);
result.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
rebuildTestPanel();
}
});
return result;
}
/** JPanel used to render the intersection for testing. */
static JPanel testPanel = null;
/** JPanel that holds all the check boxes. */
static JPanel checkBoxPanel = null;
/**
* Render the intersection.
*/
static void rebuildTestPanel()
{
testPanel.removeAll();
Set