RelativePosition.java

  1. package org.opentrafficsim.core.gtu;

  2. import java.io.Serializable;

  3. import org.djunits.unit.LengthUnit;
  4. import org.djunits.value.vdouble.scalar.Length;

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

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

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

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

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

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

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

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

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

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

  39.     /** Standard relative position type CONTOUR. */
  40.     public static final TYPE CONTOUR = new TYPE("CONTOUR");

  41.     /** The reference position (always 0, 0, 0). */
  42.     public static final RelativePosition REFERENCE_POSITION = new RelativePosition(new Length(0.0d, LengthUnit.SI),
  43.         new Length(0.0d, LengthUnit.SI), new Length(0.0d, LengthUnit.SI), RelativePosition.REFERENCE);

  44.     /**
  45.      * @param dx positive x is in the normal direction of movement.
  46.      * @param dy positive y is left compared to the normal direction of movement (seen from the top).
  47.      * @param dz positive z is up.
  48.      * @param 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.         super();
  53.         this.dx = dx;
  54.         this.dy = dy;
  55.         this.dz = dz;
  56.         this.type = type;
  57.     }

  58.     /**
  59.      * @param p a relative position to make a deep copy of.
  60.      */
  61.     public RelativePosition(final RelativePosition p)
  62.     {
  63.         super();
  64.         this.dx = p.getDx();
  65.         this.dy = p.getDy();
  66.         this.dz = p.getDz();
  67.         this.type = p.getType();
  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.     /** {@inheritDoc} */
  104.     @Override
  105.     @SuppressWarnings("checkstyle:designforextension")
  106.     public int hashCode()
  107.     {
  108.         final int prime = 31;
  109.         int result = 1;
  110.         result = prime * result + ((this.dx == null) ? 0 : this.dx.hashCode());
  111.         result = prime * result + ((this.dy == null) ? 0 : this.dy.hashCode());
  112.         result = prime * result + ((this.dz == null) ? 0 : this.dz.hashCode());
  113.         result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
  114.         return result;
  115.     }

  116.     /** {@inheritDoc} */
  117.     @Override
  118.     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
  119.     public boolean equals(final Object obj)
  120.     {
  121.         if (this == obj)
  122.             return true;
  123.         if (obj == null)
  124.             return false;
  125.         if (getClass() != obj.getClass())
  126.             return false;
  127.         RelativePosition other = (RelativePosition) obj;
  128.         if (this.dx == null)
  129.         {
  130.             if (other.dx != null)
  131.                 return false;
  132.         }
  133.         else if (!this.dx.equals(other.dx))
  134.             return false;
  135.         if (this.dy == null)
  136.         {
  137.             if (other.dy != null)
  138.                 return false;
  139.         }
  140.         else if (!this.dy.equals(other.dy))
  141.             return false;
  142.         if (this.dz == null)
  143.         {
  144.             if (other.dz != null)
  145.                 return false;
  146.         }
  147.         else if (!this.dz.equals(other.dz))
  148.             return false;
  149.         if (this.type == null)
  150.         {
  151.             if (other.type != null)
  152.                 return false;
  153.         }
  154.         else if (!this.type.equals(other.type))
  155.             return false;
  156.         return true;
  157.     }

  158.     /**
  159.      * The type of relative position, e.g., Front, Back, etc.
  160.      * <p>
  161.      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
  162.      * All rights reserved. <br>
  163.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  164.      * <p>
  165.      * $LastChangedDate: 2016-07-29 15:43:40 +0200 (Fri, 29 Jul 2016) $, @version $Revision: 2102 $, by $Author: averbraeck $,
  166.      * initial version ec 31, 2014 <br>
  167.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  168.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  169.      */
  170.     public static class TYPE implements Serializable
  171.     {
  172.         /** */
  173.         private static final long serialVersionUID = 20141231L;

  174.         /** The type name. */
  175.         private final String name;

  176.         /**
  177.          * @param name the type name.
  178.          */
  179.         public TYPE(final String name)
  180.         {
  181.             super();
  182.             this.name = name;
  183.         }

  184.         /**
  185.          * @return name.
  186.          */
  187.         public final String getName()
  188.         {
  189.             return this.name;
  190.         }

  191.         /** {@inheritDoc} */
  192.         @Override
  193.         public final String toString()
  194.         {
  195.             return this.name;
  196.         }

  197.         /** {@inheritDoc} */
  198.         @Override
  199.         @SuppressWarnings("checkstyle:designforextension")
  200.         public int hashCode()
  201.         {
  202.             final int prime = 31;
  203.             int result = 1;
  204.             result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
  205.             return result;
  206.         }

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

  228.     }

  229. }