View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.core.unit.LengthUnit;
6   import org.opentrafficsim.core.value.vdouble.scalar.DoubleScalar;
7   
8   /**
9    * <p>
10   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
12   * <p>
13   * @version Dec 30, 2014 <br>
14   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16   */
17  public class RelativePosition implements Serializable
18  {
19      /** */
20      private static final long serialVersionUID = 20141231L;
21  
22      /** positive x is in the normal direction of movement. */
23      private final DoubleScalar.Rel<LengthUnit> dx;
24  
25      /** positive y is left compared to the normal direction of movement (seen from the top). */
26      private final DoubleScalar.Rel<LengthUnit> dy;
27  
28      /** positive z is up. */
29      private final DoubleScalar.Rel<LengthUnit> dz;
30  
31      /** type of relative position (FRONT, BACK, etc.). */
32      private final TYPE type;
33  
34      /** Standard relative position type FRONT. */
35      public static final TYPE FRONT = new TYPE("FRONT");
36  
37      /** Standard relative position type BACK. */
38      public static final TYPE REAR = new TYPE("REAR");
39  
40      /** Standard relative position type CENTER. */
41      public static final TYPE CENTER = new TYPE("CENTER");
42  
43      /** Standard relative position type REFERENCE. */
44      public static final TYPE REFERENCE = new TYPE("REFERENCE");
45  
46      /** Standard relative position type DRIVER. */
47      public static final TYPE DRIVER = new TYPE("DRIVER");
48  
49      /** the reference position (always 0, 0, 0). */
50      public static final RelativePosition REFERENCE_POSITION = new RelativePosition(new DoubleScalar.Rel<LengthUnit>(0.0d,
51          LengthUnit.METER), new DoubleScalar.Rel<LengthUnit>(0.0d, LengthUnit.METER), new DoubleScalar.Rel<LengthUnit>(0.0d,
52          LengthUnit.METER), RelativePosition.REFERENCE);
53  
54      /**
55       * @param dx positive x is in the normal direction of movement.
56       * @param dy positive y is left compared to the normal direction of movement (seen from the top).
57       * @param dz positive z is up.
58       * @param type type of relative position (FRONT, BACK, etc.).
59       */
60      public RelativePosition(final DoubleScalar.Rel<LengthUnit> dx, final DoubleScalar.Rel<LengthUnit> dy,
61          final DoubleScalar.Rel<LengthUnit> 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().copy();
77          this.dy = p.getDy().copy();
78          this.dz = p.getDz().copy();
79          this.type = p.getType();
80      }
81  
82      /**
83       * @return dx.
84       */
85      public final DoubleScalar.Rel<LengthUnit> getDx()
86      {
87          return this.dx;
88      }
89  
90      /**
91       * @return dy.
92       */
93      public final DoubleScalar.Rel<LengthUnit> getDy()
94      {
95          return this.dy;
96      }
97  
98      /**
99       * @return dz.
100      */
101     public final DoubleScalar.Rel<LengthUnit> 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-2014 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/node/13">OpenTrafficSim License</a>.
184      * <p>
185      * @version Dec 31, 2014 <br>
186      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
187      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
188      */
189     public static class TYPE implements Serializable
190     {
191         /** */
192         private static final long serialVersionUID = 20141231L;
193 
194         /** the type name. */
195         private final String name;
196 
197         /**
198          * @param name the type name.
199          */
200         public TYPE(final String name)
201         {
202             super();
203             this.name = name;
204         }
205 
206         /**
207          * @return name.
208          */
209         public final String getName()
210         {
211             return this.name;
212         }
213 
214         /** {@inheritDoc} */
215         @Override
216         public final String toString()
217         {
218             return this.name;
219         }
220 
221         /** {@inheritDoc} */
222         @Override
223         @SuppressWarnings("checkstyle:designforextension")
224         public int hashCode()
225         {
226             final int prime = 31;
227             int result = 1;
228             result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
229             return result;
230         }
231 
232         /** {@inheritDoc} */
233         @Override
234         @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
235         public boolean equals(final Object obj)
236         {
237             if (this == obj)
238                 return true;
239             if (obj == null)
240                 return false;
241             if (getClass() != obj.getClass())
242                 return false;
243             TYPE other = (TYPE) obj;
244             if (this.name == null)
245             {
246                 if (other.name != null)
247                     return false;
248             }
249             else if (!this.name.equals(other.name))
250                 return false;
251             return true;
252         }
253 
254     }
255 }