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-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  9.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  10.  * <p>
  11.  * $LastChangedDate: 2020-04-01 12:03:44 +0200 (Wed, 01 Apr 2020) $, @version $Revision: 6424 $, by $Author: pknoppers $,
  12.  * initial version Dec 30, 2014 <br>
  13.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  14.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  15.  */
  16. public class RelativePosition implements Serializable
  17. {
  18.     /** */
  19.     private static final long serialVersionUID = 20141231L;

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

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

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

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

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

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

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

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

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

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

  40.     /** Center of gravity. */
  41.     public static final TYPE CENTER_GRAVITY = new TYPE("CENTER_GRAVITY");

  42.     /** The reference position (always 0, 0, 0). */
  43.     public static final RelativePosition REFERENCE_POSITION =
  44.             new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.REFERENCE);

  45.     /** the cached hash code. */
  46.     private final int hash;

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

  59.         this.hash = calcHashCode();
  60.     }

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

  70.         this.hash = calcHashCode();
  71.     }

  72.     /**
  73.      * @return dx.
  74.      */
  75.     public final Length getDx()
  76.     {
  77.         return this.dx;
  78.     }

  79.     /**
  80.      * @return dy.
  81.      */
  82.     public final Length getDy()
  83.     {
  84.         return this.dy;
  85.     }

  86.     /**
  87.      * @return dz.
  88.      */
  89.     public final Length getDz()
  90.     {
  91.         return this.dz;
  92.     }

  93.     /**
  94.      * @return type.
  95.      */
  96.     public final TYPE getType()
  97.     {
  98.         return this.type;
  99.     }

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

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

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

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  130.     public boolean equals(final Object obj)
  131.     {
  132.         if (this == obj)
  133.             return true;
  134.         if (obj == null)
  135.             return false;
  136.         if (getClass() != obj.getClass())
  137.             return false;
  138.         RelativePosition other = (RelativePosition) obj;
  139.         if (this.dx == null)
  140.         {
  141.             if (other.dx != null)
  142.                 return false;
  143.         }
  144.         else if (!this.dx.equals(other.dx))
  145.             return false;
  146.         if (this.dy == null)
  147.         {
  148.             if (other.dy != null)
  149.                 return false;
  150.         }
  151.         else if (!this.dy.equals(other.dy))
  152.             return false;
  153.         if (this.dz == null)
  154.         {
  155.             if (other.dz != null)
  156.                 return false;
  157.         }
  158.         else if (!this.dz.equals(other.dz))
  159.             return false;
  160.         if (this.type == null)
  161.         {
  162.             if (other.type != null)
  163.                 return false;
  164.         }
  165.         else if (!this.type.equals(other.type))
  166.             return false;
  167.         return true;
  168.     }

  169.     /**
  170.      * The type of relative position, e.g., Front, Back, etc.
  171.      * <p>
  172.      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
  173.      * All rights reserved. <br>
  174.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  175.      * <p>
  176.      * $LastChangedDate: 2020-04-01 12:03:44 +0200 (Wed, 01 Apr 2020) $, @version $Revision: 6424 $, by $Author: pknoppers $,
  177.      * initial version ec 31, 2014 <br>
  178.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  179.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  180.      */
  181.     public static class TYPE implements Serializable
  182.     {
  183.         /** */
  184.         private static final long serialVersionUID = 20141231L;

  185.         /** The type name. */
  186.         private final String name;

  187.         /** the cached hashcode. */
  188.         private final int hash;

  189.         /**
  190.          * @param name String; the type name.
  191.          */
  192.         public TYPE(final String name)
  193.         {
  194.             this.name = name;
  195.             this.hash = 31 + ((this.name == null) ? 0 : this.name.hashCode());
  196.         }

  197.         /**
  198.          * @return name.
  199.          */
  200.         public final String getName()
  201.         {
  202.             return this.name;
  203.         }

  204.         /** {@inheritDoc} */
  205.         @Override
  206.         public final String toString()
  207.         {
  208.             return this.name;
  209.         }

  210.         /** {@inheritDoc} */
  211.         @Override
  212.         @SuppressWarnings("checkstyle:designforextension")
  213.         public int hashCode()
  214.         {
  215.             return this.hash;
  216.         }

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

  238.     }

  239. }