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