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