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