View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   
7   /**
8    * A RelativePosition is a position on a GTU; e.g. the front, rear, position of the driver, etc. <br>
9    * A RelativePosition stores the offset of the position from the reference position of the GTU.
10   * <p>
11   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * <p>
14   * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
15   * initial version Dec 30, 2014 <br>
16   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
18   */
19  public class RelativePosition implements Serializable
20  {
21      /** */
22      private static final long serialVersionUID = 20141231L;
23  
24      /** Positive x is in the normal direction of movement. */
25      private final Length dx;
26  
27      /** Positive y is left compared to the normal direction of movement (seen from the top). */
28      private final Length dy;
29  
30      /** Positive z is up. */
31      private final Length dz;
32  
33      /** Type of relative position (FRONT, BACK, etc.). */
34      private final TYPE type;
35  
36      /** Standard relative position type FRONT. */
37      public static final TYPE FRONT = new TYPE("FRONT");
38  
39      /** Standard relative position type BACK. */
40      public static final TYPE REAR = new TYPE("REAR");
41  
42      /** Standard relative position type CENTER. */
43      public static final TYPE CENTER = new TYPE("CENTER");
44  
45      /** Standard relative position type REFERENCE. */
46      public static final TYPE REFERENCE = new TYPE("REFERENCE");
47  
48      /** Standard relative position type DRIVER. */
49      public static final TYPE DRIVER = new TYPE("DRIVER");
50  
51      /** Standard relative position type CONTOUR. There can be multiple points of type CONTOUR for one GTU. */
52      public static final TYPE CONTOUR = new TYPE("CONTOUR");
53  
54      /** The reference position (always 0, 0, 0). */
55      public static final RelativePosition REFERENCE_POSITION =
56              new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.REFERENCE);
57  
58      /** the cached hash code. */
59      private final int hash;
60  
61      /**
62       * @param dx positive x is in the normal direction of movement.
63       * @param dy positive y is left compared to the normal direction of movement (seen from the top).
64       * @param dz positive z is up.
65       * @param type type of relative position (FRONT, BACK, etc.).
66       */
67      public RelativePosition(final Length dx, final Length dy, final Length dz, final TYPE type)
68      {
69          super();
70          this.dx = dx;
71          this.dy = dy;
72          this.dz = dz;
73          this.type = type;
74  
75          this.hash = calcHashCode();
76      }
77  
78      /**
79       * @param p a relative position to make a deep copy of.
80       */
81      public RelativePosition(final RelativePosition p)
82      {
83          super();
84          this.dx = p.getDx();
85          this.dy = p.getDy();
86          this.dz = p.getDz();
87          this.type = p.getType();
88  
89          this.hash = calcHashCode();
90      }
91  
92      /**
93       * @return dx.
94       */
95      public final Length getDx()
96      {
97          return this.dx;
98      }
99  
100     /**
101      * @return dy.
102      */
103     public final Length getDy()
104     {
105         return this.dy;
106     }
107 
108     /**
109      * @return dz.
110      */
111     public final Length getDz()
112     {
113         return this.dz;
114     }
115 
116     /**
117      * @return type.
118      */
119     public final TYPE getType()
120     {
121         return this.type;
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public final String toString()
127     {
128         return "(" + this.dx + ", " + this.dy + ", " + this.dz + "): " + this.type;
129     }
130 
131     /**
132      * Calculate the hash code once.
133      * @return the hash code.
134      */
135     public final int calcHashCode()
136     {
137         final int prime = 31;
138         int result = 1;
139         result = prime * result + ((this.dx == null) ? 0 : this.dx.hashCode());
140         result = prime * result + ((this.dy == null) ? 0 : this.dy.hashCode());
141         result = prime * result + ((this.dz == null) ? 0 : this.dz.hashCode());
142         result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
143         return result;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     @SuppressWarnings("checkstyle:designforextension")
149     public int hashCode()
150     {
151         return this.hash;
152     }
153 
154     /** {@inheritDoc} */
155     @Override
156     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
157     public boolean equals(final Object obj)
158     {
159         if (this == obj)
160             return true;
161         if (obj == null)
162             return false;
163         if (getClass() != obj.getClass())
164             return false;
165         RelativePosition other = (RelativePosition) obj;
166         if (this.dx == null)
167         {
168             if (other.dx != null)
169                 return false;
170         }
171         else if (!this.dx.equals(other.dx))
172             return false;
173         if (this.dy == null)
174         {
175             if (other.dy != null)
176                 return false;
177         }
178         else if (!this.dy.equals(other.dy))
179             return false;
180         if (this.dz == null)
181         {
182             if (other.dz != null)
183                 return false;
184         }
185         else if (!this.dz.equals(other.dz))
186             return false;
187         if (this.type == null)
188         {
189             if (other.type != null)
190                 return false;
191         }
192         else if (!this.type.equals(other.type))
193             return false;
194         return true;
195     }
196 
197     /**
198      * The type of relative position, e.g., Front, Back, etc.
199      * <p>
200      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
201      * All rights reserved. <br>
202      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
203      * <p>
204      * $LastChangedDate: 2018-09-19 13:55:45 +0200 (Wed, 19 Sep 2018) $, @version $Revision: 4006 $, by $Author: averbraeck $,
205      * initial version ec 31, 2014 <br>
206      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
207      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
208      */
209     public static class TYPE implements Serializable
210     {
211         /** */
212         private static final long serialVersionUID = 20141231L;
213 
214         /** The type name. */
215         private final String name;
216 
217         /** the cached hashcode. */
218         private final int hash;
219 
220         /**
221          * @param name the type name.
222          */
223         public TYPE(final String name)
224         {
225             super();
226             this.name = name;
227             this.hash = 31 + ((this.name == null) ? 0 : this.name.hashCode());
228         }
229 
230         /**
231          * @return name.
232          */
233         public final String getName()
234         {
235             return this.name;
236         }
237 
238         /** {@inheritDoc} */
239         @Override
240         public final String toString()
241         {
242             return this.name;
243         }
244 
245         /** {@inheritDoc} */
246         @Override
247         @SuppressWarnings("checkstyle:designforextension")
248         public int hashCode()
249         {
250             return this.hash;
251         }
252 
253         /** {@inheritDoc} */
254         @Override
255         @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
256         public boolean equals(final Object obj)
257         {
258             if (this == obj)
259                 return true;
260             if (obj == null)
261                 return false;
262             if (getClass() != obj.getClass())
263                 return false;
264             TYPE other = (TYPE) obj;
265             if (this.name == null)
266             {
267                 if (other.name != null)
268                     return false;
269             }
270             else if (!this.name.equals(other.name))
271                 return false;
272             return true;
273         }
274 
275     }
276 
277 }