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