package org.opentrafficsim.draw.road;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.ImageObserver;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import org.djutils.draw.bounds.Bounds3d;
import org.djutils.draw.line.PolyLine3d;
import org.djutils.draw.line.Ray3d;
import org.djutils.draw.point.OrientedPoint3d;
import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
import org.opentrafficsim.draw.core.PaintLine;
import org.opentrafficsim.draw.core.PaintPolygons;
import org.opentrafficsim.draw.core.TextAlignment;
import org.opentrafficsim.draw.core.TextAnimation;
import org.opentrafficsim.road.network.lane.Lane;
import nl.tudelft.simulation.dsol.animation.Locatable;
import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
import nl.tudelft.simulation.dsol.simulators.SimulatorInterface.TimeDoubleUnit;
import nl.tudelft.simulation.language.d2.Angle;
/**
*
* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
* initial version Oct 17, 2014
* @author Alexander Verbraeck
*/
public class LaneAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
{
/** */
private static final long serialVersionUID = 20141017L;
/** Color of the lane. */
private final Color color;
/** the Text object to destroy when the animation is destroyed. */
private final Text text;
/**
* Animate a Lane.
* @param lane Lane; the lane
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
* @param color Color; Color of the lane.
* @throws NamingException in case of registration failure of the animation
* @throws RemoteException on communication failure
*/
public LaneAnimation(final Lane lane, final SimulatorInterface.TimeDoubleUnit simulator, final Color color)
throws NamingException, RemoteException
{
super(lane, simulator);
this.color = color;
this.text = new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER,
Color.BLACK, simulator);
// System.out.println("Created lane animation for " + lane.getId() + " of link " + lane.getParentLink().getId()
// + "\n\tcontour " + lane.getContour().project().toPlot() + "\tlocation " + lane.getLocation() + "\n\tbounds "
// + lane.getContour().getBounds() + "\n\tcenterLine " + lane.getCenterLine().project().toPlot());
new CenterLineAnimation(new CenterLine(lane.getCenterLine()), simulator);
}
/**
* @return text.
*/
public final Text getText()
{
return this.text;
}
/** {@inheritDoc} */
@Override
public final void paint(final Graphics2D graphics, final ImageObserver observer)
{
Lane lane = getSource();
// System.out.println("Painting " + lane.getId() + " of link " + lane.getParentLink().getId() + "\n\tcontour "
// + lane.getContour() + "\n\tlocation " + lane.getLocation() + "\n\tbounds " + lane.getContour().getBounds());
if (this.color != null)
{
PaintPolygons.paintPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
}
}
/** {@inheritDoc} */
@Override
public final void destroy(final SimulatorInterface, ?, ?> simulator)
{
super.destroy(simulator);
this.text.destroy(simulator);
}
/** {@inheritDoc} */
@Override
public ClonableRenderable2DInterface clone(final Lane newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
throws NamingException, RemoteException
{
// the constructor also constructs the corresponding Text object
return new LaneAnimation(newSource, newSimulator, this.color);
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
}
/**
* Draw center line of a lane.
*/
public static class CenterLine implements Locatable
{
/** The center line. */
private final PolyLine3d centerLine;
/**
* Construct a new CenterLine.
* @param centerLine OTSLine3D; the center line of a lane
*/
CenterLine(final PolyLine3d centerLine)
{
this.centerLine = centerLine;
}
@Override
public final OrientedPoint3d getLocation()
{
return new OrientedPoint3d(0, 0, 0, 0, 0, 0);
}
@Override
public final Bounds3d getBounds() throws RemoteException
{
return this.centerLine.getBounds();
}
/**
* Retrieve the center line.
* @return OTSLine3D; the center line
*/
public PolyLine3d getCenterLine()
{
return this.centerLine;
}
}
/**
* Animation for center line of a lane.
*/
public static class CenterLineAnimation extends Renderable2D
implements ClonableRenderable2DInterface, Serializable
{
/** Drawing color for the center line. */
private static final Color COLOR = Color.MAGENTA.darker().darker();
/** */
private static final long serialVersionUID = 20180426L;
/**
* Construct a new CenterLineAnimation.
* @param centerLine CenterLine; the center line of a lane
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
* @throws NamingException when the name of this object is not unique
* @throws RemoteException when communication with a remote process fails
*/
public CenterLineAnimation(final CenterLine centerLine, final SimulatorInterface.TimeDoubleUnit simulator)
throws NamingException, RemoteException
{
super(centerLine, simulator);
}
@Override
public final ClonableRenderable2DInterface clone(final CenterLine newSource,
final TimeDoubleUnit newSimulator) throws NamingException, RemoteException
{
return null;
}
@Override
public final void paint(final Graphics2D graphics, final ImageObserver observer)
{
PaintLine.paintLine(graphics, COLOR, 0.1, getSource().getLocation(), getSource().getCenterLine());
}
}
/**
* Text animation for the Node. Separate class to be able to turn it on and off...
*
* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
*
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
* initial version Dec 11, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class Text extends TextAnimation
{
/** */
private static final long serialVersionUID = 20161211L;
/**
* @param source Locatable; the object for which the text is displayed
* @param text String; the text to display
* @param dx float; the horizontal movement of the text, in meters
* @param dy float; the vertical movement of the text, in meters
* @param textPlacement TextAlignment; where to place the text
* @param color Color; the color of the text
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
* @throws NamingException when animation context cannot be created or retrieved
* @throws RemoteException - when remote context cannot be found
*/
public Text(final Locatable source, final String text, final float dx, final float dy,
final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
throws RemoteException, NamingException
{
super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
}
/** {@inheritDoc} */
@Override
public OrientedPoint3d getLocation() throws RemoteException
{
// draw always on top.
Ray3d p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
double a = Angle.normalizePi(p.getPhi());
if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
{
a += Math.PI;
}
return new OrientedPoint3d(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
}
/** {@inheritDoc} */
@Override
public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
throws RemoteException, NamingException
{
return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "Text []";
}
}
}