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-2020 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: 2020-04-01 12:03:44 +0200 (Wed, 01 Apr 2020) $, @version $Revision: 6424 $, by $Author: pknoppers $,
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      /** Center of gravity. */
55      public static final TYPE CENTER_GRAVITY = new TYPE("CENTER_GRAVITY");
56  
57      /** The reference position (always 0, 0, 0). */
58      public static final RelativePosition REFERENCE_POSITION =
59              new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.REFERENCE);
60  
61      /** the cached hash code. */
62      private final int hash;
63  
64      /**
65       * @param dx Length; positive x is in the normal direction of movement.
66       * @param dy Length; positive y is left compared to the normal direction of movement (seen from the top).
67       * @param dz Length; positive z is up.
68       * @param type TYPE; type of relative position (FRONT, BACK, etc.).
69       */
70      public RelativePosition(final Length dx, final Length dy, final Length dz, final TYPE type)
71      {
72          this.dx = dx;
73          this.dy = dy;
74          this.dz = dz;
75          this.type = type;
76  
77          this.hash = calcHashCode();
78      }
79  
80      /**
81       * @param p RelativePosition; a relative position to make a deep copy of.
82       */
83      public RelativePositionativePosition.html#RelativePosition">RelativePosition(final RelativePosition p)
84      {
85          this.dx = p.getDx();
86          this.dy = p.getDy();
87          this.dz = p.getDz();
88          this.type = p.getType();
89  
90          this.hash = calcHashCode();
91      }
92  
93      /**
94       * @return dx.
95       */
96      public final Length getDx()
97      {
98          return this.dx;
99      }
100 
101     /**
102      * @return dy.
103      */
104     public final Length getDy()
105     {
106         return this.dy;
107     }
108 
109     /**
110      * @return dz.
111      */
112     public final Length getDz()
113     {
114         return this.dz;
115     }
116 
117     /**
118      * @return type.
119      */
120     public final TYPE getType()
121     {
122         return this.type;
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public final String toString()
128     {
129         return "(" + this.dx + ", " + this.dy + ", " + this.dz + "): " + this.type;
130     }
131 
132     /**
133      * Calculate the hash code once.
134      * @return the hash code.
135      */
136     public final int calcHashCode()
137     {
138         final int prime = 31;
139         int result = 1;
140         result = prime * result + ((this.dx == null) ? 0 : this.dx.hashCode());
141         result = prime * result + ((this.dy == null) ? 0 : this.dy.hashCode());
142         result = prime * result + ((this.dz == null) ? 0 : this.dz.hashCode());
143         result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
144         return result;
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     @SuppressWarnings("checkstyle:designforextension")
150     public int hashCode()
151     {
152         return this.hash;
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
158     public boolean equals(final Object obj)
159     {
160         if (this == obj)
161             return true;
162         if (obj == null)
163             return false;
164         if (getClass() != obj.getClass())
165             return false;
166         RelativePosition../org/opentrafficsim/core/gtu/RelativePosition.html#RelativePosition">RelativePosition other = (RelativePosition) obj;
167         if (this.dx == null)
168         {
169             if (other.dx != null)
170                 return false;
171         }
172         else if (!this.dx.equals(other.dx))
173             return false;
174         if (this.dy == null)
175         {
176             if (other.dy != null)
177                 return false;
178         }
179         else if (!this.dy.equals(other.dy))
180             return false;
181         if (this.dz == null)
182         {
183             if (other.dz != null)
184                 return false;
185         }
186         else if (!this.dz.equals(other.dz))
187             return false;
188         if (this.type == null)
189         {
190             if (other.type != null)
191                 return false;
192         }
193         else if (!this.type.equals(other.type))
194             return false;
195         return true;
196     }
197 
198     /**
199      * The type of relative position, e.g., Front, Back, etc.
200      * <p>
201      * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
202      * All rights reserved. <br>
203      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
204      * <p>
205      * $LastChangedDate: 2020-04-01 12:03:44 +0200 (Wed, 01 Apr 2020) $, @version $Revision: 6424 $, by $Author: pknoppers $,
206      * initial version ec 31, 2014 <br>
207      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
208      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
209      */
210     public static class TYPE implements Serializable
211     {
212         /** */
213         private static final long serialVersionUID = 20141231L;
214 
215         /** The type name. */
216         private final String name;
217 
218         /** the cached hashcode. */
219         private final int hash;
220 
221         /**
222          * @param name String; the type name.
223          */
224         public TYPE(final String name)
225         {
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 }