DirectedLanePosition.java

  1. package org.opentrafficsim.road.network.lane;

  2. import java.io.Serializable;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djutils.exceptions.Throw;
  5. import org.opentrafficsim.core.geometry.OTSLine3D;
  6. import org.opentrafficsim.core.gtu.GTUDirectionality;
  7. import org.opentrafficsim.core.gtu.GTUException;
  8. import org.opentrafficsim.core.network.LinkDirection;

  9. import nl.tudelft.simulation.language.d3.DirectedPoint;

  10. /**
  11.  * Store one position, direction and lane of a GTU.
  12.  * <p>
  13.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  14.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  15.  * </p>
  16.  * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  17.  * initial version Nov 11, 2015 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  */
  21. public class DirectedLanePosition implements Serializable
  22. {
  23.     /** */
  24.     private static final long serialVersionUID = 20151111L;

  25.     /** The lane for the position. */
  26.     private final Lane lane;

  27.     /** The position on the lane, relative to the cross section link (design line). */
  28.     private final Length position;

  29.     /** The direction the vehicle is driving to -- either in the direction of the design line, or against it. */
  30.     private final GTUDirectionality gtuDirection;

  31.     /** Link direction. */
  32.     private LinkDirection linkDirection = null;

  33.     /**
  34.      * Construct a new DirectedLanePosition.
  35.      * @param lane Lane; the lane for the position
  36.      * @param position Length; the position on the lane, relative to the cross section link (design line)
  37.      * @param gtuDirection GTUDirectionality; the direction the vehicle is driving to -- either in the direction of the design
  38.      *            line, or against it
  39.      * @throws GTUException when preconditions fail
  40.      */
  41.     public DirectedLanePosition(final Lane lane, final Length position, final GTUDirectionality gtuDirection)
  42.             throws GTUException
  43.     {
  44.         Throw.when(lane == null, GTUException.class, "lane is null");
  45.         Throw.when(position == null, GTUException.class, "position is null");
  46.         Throw.when(gtuDirection == null, GTUException.class, "gtuDirection is null");
  47.         this.lane = lane;
  48.         this.position = position;
  49.         this.gtuDirection = gtuDirection;
  50.     }

  51.     /**
  52.      * Retrieve the lane.
  53.      * @return Lane; the lane for the position
  54.      */
  55.     public final Lane getLane()
  56.     {
  57.         return this.lane;
  58.     }

  59.     /**
  60.      * Retrieve the position on the lane.
  61.      * @return Length; the position on the lane, relative to the cross section link (design line)
  62.      */
  63.     public final Length getPosition()
  64.     {
  65.         return this.position;
  66.     }

  67.     /**
  68.      * Retrieve the gtuDirection.
  69.      * @return GTUDirectionality; gtuDirection the direction the vehicle is driving to -- either in the direction of the design
  70.      *         line, or against it
  71.      */
  72.     public final GTUDirectionality getGtuDirection()
  73.     {
  74.         return this.gtuDirection;
  75.     }

  76.     /**
  77.      * Retrieve the location and direction of the GTU on the lane.
  78.      * @return DirectedPoint; the location and direction of the GTU on the lane
  79.      */
  80.     public final DirectedPoint getLocation()
  81.     {
  82.         // double fraction = this.position.si / this.lane.getParentLink().getLength().si;
  83.         OTSLine3D centerLine = this.lane.getCenterLine();
  84.         double centerLineLength = centerLine.getLengthSI();
  85.         double fraction = this.position.si / centerLineLength;
  86.         DirectedPoint p = centerLine.getLocationFractionExtended(fraction);
  87.         if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
  88.         {
  89.             return p;
  90.         }
  91.         return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
  92.     }

  93.     /**
  94.      * Returns the lane direction in the direction of this lane direction.
  95.      * @return lane direction in the direction of this lane direction
  96.      */
  97.     public final LaneDirection getLaneDirection()
  98.     {
  99.         return new LaneDirection(this.lane, this.gtuDirection);
  100.     }

  101.     /**
  102.      * Returns the link direction in the direction of this lane direction.
  103.      * @return link direction in the direction of this lane direction
  104.      */
  105.     public final LinkDirection getLinkDirection()
  106.     {
  107.         if (this.linkDirection == null)
  108.         {
  109.             this.linkDirection = new LinkDirection(this.lane.getParentLink(), this.gtuDirection);
  110.         }
  111.         return this.linkDirection;
  112.     }

  113.     /** {@inheritDoc} */
  114.     @Override
  115.     public int hashCode()
  116.     {
  117.         final int prime = 31;
  118.         int result = 1;
  119.         result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
  120.         result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
  121.         result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
  122.         return result;
  123.     }

  124.     /** {@inheritDoc} */
  125.     @SuppressWarnings("checkstyle:needbraces")
  126.     @Override
  127.     public boolean equals(final Object obj)
  128.     {
  129.         if (this == obj)
  130.             return true;
  131.         if (obj == null)
  132.             return false;
  133.         if (getClass() != obj.getClass())
  134.             return false;
  135.         DirectedLanePosition other = (DirectedLanePosition) obj;
  136.         if (this.gtuDirection != other.gtuDirection)
  137.             return false;
  138.         if (this.lane == null)
  139.         {
  140.             if (other.lane != null)
  141.                 return false;
  142.         }
  143.         else if (!this.lane.equals(other.lane))
  144.             return false;
  145.         if (this.position == null)
  146.         {
  147.             if (other.position != null)
  148.                 return false;
  149.         }
  150.         else if (!this.position.equals(other.position))
  151.             return false;
  152.         return true;
  153.     }

  154.     /** {@inheritDoc} */
  155.     @Override
  156.     public final String toString()
  157.     {
  158.         return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection=" + this.gtuDirection
  159.                 + "]";
  160.     }

  161. }