RelativePosition.java

  1. package org.opentrafficsim.core.gtu;

  2. import java.io.Serializable;

  3. import org.djunits.value.vdouble.scalar.Length;

  4. /**
  5.  * A RelativePosition is a position on a GTU; e.g. the front, rear, position of the driver, etc. <br>
  6.  * A RelativePosition stores the offset of the position from the reference position of the GTU.
  7.  * <p>
  8.  * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  10.  * <p>
  11.  * $LastChangedDate$, @version $Revision$, by $Author$, initial version Dec 30, 2014 <br>
  12.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  13.  * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  14.  */
  15. public class RelativePosition implements Serializable
  16. {
  17.     /** */
  18.     private static final long serialVersionUID = 20141231L;

  19.     /** Positive x is in the normal direction of movement. */
  20.     private final Length dx;

  21.     /** Positive y is left compared to the normal direction of movement (seen from the top). */
  22.     private final Length dy;

  23.     /** Positive z is up. */
  24.     private final Length dz;

  25.     /** Type of relative position (FRONT, BACK, etc.). */
  26.     private final TYPE type;

  27.     /** Standard relative position type FRONT. */
  28.     public static final TYPE FRONT = new TYPE("FRONT");

  29.     /** Standard relative position type BACK. */
  30.     public static final TYPE REAR = new TYPE("REAR");

  31.     /** Standard relative position type CENTER. */
  32.     public static final TYPE CENTER = new TYPE("CENTER");

  33.     /** Standard relative position type REFERENCE. */
  34.     public static final TYPE REFERENCE = new TYPE("REFERENCE");

  35.     /** Standard relative position type DRIVER. */
  36.     public static final TYPE DRIVER = new TYPE("DRIVER");

  37.     /** Standard relative position type CONTOUR. There can be multiple points of type CONTOUR for one GTU. */
  38.     public static final TYPE CONTOUR = new TYPE("CONTOUR");

  39.     /** The reference position (always 0, 0, 0). */
  40.     public static final RelativePosition REFERENCE_POSITION =
  41.             new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.REFERENCE);

  42.     /** the cached hash code. */
  43.     private final int hash;

  44.     /**
  45.      * @param dx Length; positive x is in the normal direction of movement.
  46.      * @param dy Length; positive y is left compared to the normal direction of movement (seen from the top).
  47.      * @param dz Length; positive z is up.
  48.      * @param type TYPE; type of relative position (FRONT, BACK, etc.).
  49.      */
  50.     public RelativePosition(final Length dx, final Length dy, final Length dz, final TYPE type)
  51.     {
  52.         this.dx = dx;
  53.         this.dy = dy;
  54.         this.dz = dz;
  55.         this.type = type;

  56.         this.hash = calcHashCode();
  57.     }

  58.     /**
  59.      * @param p RelativePosition; a relative position to make a deep copy of.
  60.      */
  61.     public RelativePosition(final RelativePosition p)
  62.     {
  63.         this.dx = p.getDx();
  64.         this.dy = p.getDy();
  65.         this.dz = p.getDz();
  66.         this.type = p.getType();

  67.         this.hash = calcHashCode();
  68.     }

  69.     /**
  70.      * @return dx.
  71.      */
  72.     public final Length getDx()
  73.     {
  74.         return this.dx;
  75.     }

  76.     /**
  77.      * @return dy.
  78.      */
  79.     public final Length getDy()
  80.     {
  81.         return this.dy;
  82.     }

  83.     /**
  84.      * @return dz.
  85.      */
  86.     public final Length getDz()
  87.     {
  88.         return this.dz;
  89.     }

  90.     /**
  91.      * @return type.
  92.      */
  93.     public final TYPE getType()
  94.     {
  95.         return this.type;
  96.     }

  97.     /** {@inheritDoc} */
  98.     @Override
  99.     public final String toString()
  100.     {
  101.         return "(" + this.dx + ", " + this.dy + ", " + this.dz + "): " + this.type;
  102.     }

  103.     /**
  104.      * Calculate the hash code once.
  105.      * @return the hash code.
  106.      */
  107.     public final int calcHashCode()
  108.     {
  109.         final int prime = 31;
  110.         int result = 1;
  111.         result = prime * result + ((this.dx == null) ? 0 : this.dx.hashCode());
  112.         result = prime * result + ((this.dy == null) ? 0 : this.dy.hashCode());
  113.         result = prime * result + ((this.dz == null) ? 0 : this.dz.hashCode());
  114.         result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
  115.         return result;
  116.     }

  117.     /** {@inheritDoc} */
  118.     @Override
  119.     @SuppressWarnings("checkstyle:designforextension")
  120.     public int hashCode()
  121.     {
  122.         return this.hash;
  123.     }

  124.     /** {@inheritDoc} */
  125.     @Override
  126.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  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.         RelativePosition other = (RelativePosition) obj;
  136.         if (this.dx == null)
  137.         {
  138.             if (other.dx != null)
  139.                 return false;
  140.         }
  141.         else if (!this.dx.equals(other.dx))
  142.             return false;
  143.         if (this.dy == null)
  144.         {
  145.             if (other.dy != null)
  146.                 return false;
  147.         }
  148.         else if (!this.dy.equals(other.dy))
  149.             return false;
  150.         if (this.dz == null)
  151.         {
  152.             if (other.dz != null)
  153.                 return false;
  154.         }
  155.         else if (!this.dz.equals(other.dz))
  156.             return false;
  157.         if (this.type == null)
  158.         {
  159.             if (other.type != null)
  160.                 return false;
  161.         }
  162.         else if (!this.type.equals(other.type))
  163.             return false;
  164.         return true;
  165.     }

  166.     /**
  167.      * The type of relative position, e.g., Front, Back, etc.
  168.      * <p>
  169.      * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
  170.      * All rights reserved. <br>
  171.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  172.      * <p>
  173.      * $LastChangedDate$, @version $Revision$, by $Author$, initial version ec 31, 2014 <br>
  174.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  175.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  176.      */
  177.     public static class TYPE implements Serializable
  178.     {
  179.         /** */
  180.         private static final long serialVersionUID = 20141231L;

  181.         /** The type name. */
  182.         private final String name;

  183.         /** the cached hashcode. */
  184.         private final int hash;

  185.         /**
  186.          * @param name String; the type name.
  187.          */
  188.         public TYPE(final String name)
  189.         {
  190.             this.name = name;
  191.             this.hash = 31 + ((this.name == null) ? 0 : this.name.hashCode());
  192.         }

  193.         /**
  194.          * @return name.
  195.          */
  196.         public final String getName()
  197.         {
  198.             return this.name;
  199.         }

  200.         /** {@inheritDoc} */
  201.         @Override
  202.         public final String toString()
  203.         {
  204.             return this.name;
  205.         }

  206.         /** {@inheritDoc} */
  207.         @Override
  208.         @SuppressWarnings("checkstyle:designforextension")
  209.         public int hashCode()
  210.         {
  211.             return this.hash;
  212.         }

  213.         /** {@inheritDoc} */
  214.         @Override
  215.         @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  216.         public boolean equals(final Object obj)
  217.         {
  218.             if (this == obj)
  219.                 return true;
  220.             if (obj == null)
  221.                 return false;
  222.             if (getClass() != obj.getClass())
  223.                 return false;
  224.             TYPE other = (TYPE) obj;
  225.             if (this.name == null)
  226.             {
  227.                 if (other.name != null)
  228.                     return false;
  229.             }
  230.             else if (!this.name.equals(other.name))
  231.                 return false;
  232.             return true;
  233.         }

  234.     }

  235. }