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-2019 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: 2019-01-18 22:40:31 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 4886 $, 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      /** 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          super();
73          this.dx = dx;
74          this.dy = dy;
75          this.dz = dz;
76          this.type = type;
77  
78          this.hash = calcHashCode();
79      }
80  
81      /**
82       * @param p RelativePosition; a relative position to make a deep copy of.
83       */
84      public RelativePosition(final RelativePosition p)
85      {
86          super();
87          this.dx = p.getDx();
88          this.dy = p.getDy();
89          this.dz = p.getDz();
90          this.type = p.getType();
91  
92          this.hash = calcHashCode();
93      }
94  
95      /**
96       * @return dx.
97       */
98      public final Length getDx()
99      {
100         return this.dx;
101     }
102 
103     /**
104      * @return dy.
105      */
106     public final Length getDy()
107     {
108         return this.dy;
109     }
110 
111     /**
112      * @return dz.
113      */
114     public final Length getDz()
115     {
116         return this.dz;
117     }
118 
119     /**
120      * @return type.
121      */
122     public final TYPE getType()
123     {
124         return this.type;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public final String toString()
130     {
131         return "(" + this.dx + ", " + this.dy + ", " + this.dz + "): " + this.type;
132     }
133 
134     /**
135      * Calculate the hash code once.
136      * @return the hash code.
137      */
138     public final int calcHashCode()
139     {
140         final int prime = 31;
141         int result = 1;
142         result = prime * result + ((this.dx == null) ? 0 : this.dx.hashCode());
143         result = prime * result + ((this.dy == null) ? 0 : this.dy.hashCode());
144         result = prime * result + ((this.dz == null) ? 0 : this.dz.hashCode());
145         result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
146         return result;
147     }
148 
149     /** {@inheritDoc} */
150     @Override
151     @SuppressWarnings("checkstyle:designforextension")
152     public int hashCode()
153     {
154         return this.hash;
155     }
156 
157     /** {@inheritDoc} */
158     @Override
159     @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
160     public boolean equals(final Object obj)
161     {
162         if (this == obj)
163             return true;
164         if (obj == null)
165             return false;
166         if (getClass() != obj.getClass())
167             return false;
168         RelativePosition other = (RelativePosition) obj;
169         if (this.dx == null)
170         {
171             if (other.dx != null)
172                 return false;
173         }
174         else if (!this.dx.equals(other.dx))
175             return false;
176         if (this.dy == null)
177         {
178             if (other.dy != null)
179                 return false;
180         }
181         else if (!this.dy.equals(other.dy))
182             return false;
183         if (this.dz == null)
184         {
185             if (other.dz != null)
186                 return false;
187         }
188         else if (!this.dz.equals(other.dz))
189             return false;
190         if (this.type == null)
191         {
192             if (other.type != null)
193                 return false;
194         }
195         else if (!this.type.equals(other.type))
196             return false;
197         return true;
198     }
199 
200     /**
201      * The type of relative position, e.g., Front, Back, etc.
202      * <p>
203      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
204      * All rights reserved. <br>
205      * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
206      * <p>
207      * $LastChangedDate: 2019-01-18 22:40:31 +0100 (Fri, 18 Jan 2019) $, @version $Revision: 4886 $, by $Author: averbraeck $,
208      * initial version ec 31, 2014 <br>
209      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
210      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
211      */
212     public static class TYPE implements Serializable
213     {
214         /** */
215         private static final long serialVersionUID = 20141231L;
216 
217         /** The type name. */
218         private final String name;
219 
220         /** the cached hashcode. */
221         private final int hash;
222 
223         /**
224          * @param name String; the type name.
225          */
226         public TYPE(final String name)
227         {
228             super();
229             this.name = name;
230             this.hash = 31 + ((this.name == null) ? 0 : this.name.hashCode());
231         }
232 
233         /**
234          * @return name.
235          */
236         public final String getName()
237         {
238             return this.name;
239         }
240 
241         /** {@inheritDoc} */
242         @Override
243         public final String toString()
244         {
245             return this.name;
246         }
247 
248         /** {@inheritDoc} */
249         @Override
250         @SuppressWarnings("checkstyle:designforextension")
251         public int hashCode()
252         {
253             return this.hash;
254         }
255 
256         /** {@inheritDoc} */
257         @Override
258         @SuppressWarnings({ "checkstyle:designforextension", "checkstyle:needbraces" })
259         public boolean equals(final Object obj)
260         {
261             if (this == obj)
262                 return true;
263             if (obj == null)
264                 return false;
265             if (getClass() != obj.getClass())
266                 return false;
267             TYPE other = (TYPE) obj;
268             if (this.name == null)
269             {
270                 if (other.name != null)
271                     return false;
272             }
273             else if (!this.name.equals(other.name))
274                 return false;
275             return true;
276         }
277 
278     }
279 
280 }