View Javadoc
1   package org.opentrafficsim.core.gtu;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   
5   /**
6    * A RelativePosition is a position on a GTU; e.g. the front, rear, position of the driver, etc. <br>
7    * A RelativePosition stores the offset of the position from the reference position of the GTU.
8    * <p>
9    * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
11   * <p>
12   * $LastChangedDate$, @version $Revision$, by $Author$, initial version Dec 30, 2014 <br>
13   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
15   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
16   * @param dx positive x is in the normal direction of movement.
17   * @param dy positive y is left compared to the normal direction of movement (seen from the top).
18   * @param dz positive z is up.
19   * @param type type of relative position (FRONT, BACK, etc.).
20   */
21  public record RelativePosition(Length dx, Length dy, Length dz, Type type)
22  {
23  
24      /** Standard relative position type FRONT. */
25      public static final Type FRONT = new Type("FRONT");
26  
27      /** Standard relative position type BACK. */
28      public static final Type REAR = new Type("REAR");
29  
30      /** Standard relative position type CENTER. */
31      public static final Type CENTER = new Type("CENTER");
32  
33      /** Standard relative position type REFERENCE. */
34      public static final Type REFERENCE = new Type("REFERENCE");
35  
36      /** Standard relative position type DRIVER. */
37      public static final Type DRIVER = new Type("DRIVER");
38  
39      /** Standard relative position type CONTOUR. There can be multiple points of type CONTOUR for one GTU. */
40      public static final Type CONTOUR = new Type("CONTOUR");
41  
42      /** The reference position (always 0, 0, 0). */
43      public static final RelativePosition REFERENCE_POSITION =
44              new RelativePosition(Length.ZERO, Length.ZERO, Length.ZERO, RelativePosition.REFERENCE);
45  
46      /**
47       * Constructor.
48       * @param p a relative position to make a deep copy of.
49       */
50      public RelativePosition(final RelativePosition p)
51      {
52          this(p.dx(), p.dy(), p.dz(), p.type());
53      }
54  
55      /**
56       * The type of relative position, e.g., Front, Back, etc.
57       * <p>
58       * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
59       * All rights reserved. <br>
60       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
61       * <p>
62       * $LastChangedDate$, @version $Revision$, by $Author$, initial version ec 31, 2014 <br>
63       * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
64       * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
65       */
66      public static class Type
67      {
68          /** The type name. */
69          private final String name;
70  
71          /** the cached hashcode. */
72          private final int hash;
73  
74          /**
75           * Constructor.
76           * @param name the type name.
77           */
78          public Type(final String name)
79          {
80              this.name = name;
81              this.hash = 31 + ((this.name == null) ? 0 : this.name.hashCode());
82          }
83  
84          /**
85           * Return name.
86           * @return name.
87           */
88          public final String getName()
89          {
90              return this.name;
91          }
92  
93          @Override
94          public final String toString()
95          {
96              return this.name;
97          }
98  
99          @Override
100         @SuppressWarnings("checkstyle:designforextension")
101         public int hashCode()
102         {
103             return this.hash;
104         }
105 
106         @Override
107         @SuppressWarnings({"checkstyle:designforextension", "checkstyle:needbraces"})
108         public boolean equals(final Object obj)
109         {
110             if (this == obj)
111                 return true;
112             if (obj == null)
113                 return false;
114             if (getClass() != obj.getClass())
115                 return false;
116             Type other = (Type) obj;
117             if (this.name == null)
118             {
119                 if (other.name != null)
120                     return false;
121             }
122             else if (!this.name.equals(other.name))
123                 return false;
124             return true;
125         }
126 
127     }
128 
129 }