View Javadoc
1   package org.opentrafficsim.draw.graphs;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.opentrafficsim.kpi.sampling.Trajectory;
5   import org.opentrafficsim.kpi.sampling.data.ExtendedDataType;
6   
7   /**
8    * Class containing a trajectory with an offset. Takes care of bits that are before and beyond the lane without affecting the
9    * trajectory itself.
10   * <p>
11   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
13   * </p>
14   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
15   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
16   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
17   */
18  public class OffsetTrajectory
19  {
20  
21      /** The trajectory. */
22      private final Trajectory<?> trajectory;
23  
24      /** The offset. */
25      private final double offset;
26  
27      /** Scale factor for space dimension. */
28      private final double scaleFactor;
29  
30      /**
31       * Construct a new TrajectoryAndLengthOffset object.
32       * @param trajectory the trajectory
33       * @param offset the length from the beginning of the sampled path to the start of the lane to which the trajectory belongs
34       * @param scaleFactor scale factor for space dimension
35       */
36      OffsetTrajectory(final Trajectory<?> trajectory, final Length offset, final double scaleFactor)
37      {
38          this.trajectory = trajectory;
39          this.offset = offset.si;
40          this.scaleFactor = scaleFactor;
41      }
42  
43      /**
44       * Returns the number of measurements in the trajectory.
45       * @return number of measurements in the trajectory
46       */
47      public int size()
48      {
49          return this.trajectory.size();
50      }
51  
52      /**
53       * Returns the location, including offset, of an item.
54       * @param item item (sample) number
55       * @return location, including offset, of an item
56       */
57      public double getX(final int item)
58      {
59          return this.offset + this.trajectory.getX(item) * this.scaleFactor;
60      }
61  
62      /**
63       * Returns the time of an item.
64       * @param item item (sample) number
65       * @return time of an item
66       */
67      public double getT(final int item)
68      {
69          return (double) this.trajectory.getT(item);
70      }
71  
72      /**
73       * Returns the speed of an item.
74       * @param item item (sample) number
75       * @return speed of an item
76       */
77      public double getV(final int item)
78      {
79          return (double) this.trajectory.getV(item);
80      }
81  
82      /**
83       * Returns the acceleration of an item.
84       * @param item item (sample) number
85       * @return acceleration of an item
86       */
87      public double getA(final int item)
88      {
89          return (double) this.trajectory.getA(item);
90      }
91  
92      /**
93       * Returns value of an extended data type.
94       * @param <T> value type
95       * @param item item (sample) number
96       * @param dataType extended data type
97       * @return value of extended data type
98       */
99      public <T> T getValue(final int item, final ExtendedDataType<? extends T, ?, ?, ?> dataType)
100     {
101         return this.trajectory.getExtendedData(dataType, item);
102     }
103 
104     /**
105      * Returns the ID of the GTU of this trajectory.
106      * @return the ID of the GTU of this trajectory
107      */
108     public String getGtuId()
109     {
110         return this.trajectory.getGtuId();
111     }
112 
113     @Override
114     public String toString()
115     {
116         return "OffsetTrajectory [trajectory=" + this.trajectory + ", offset=" + this.offset + "]";
117     }
118 
119     /**
120      * Section in trajectory for which a color is required.
121      * @param trajectory trajectory
122      * @param section section index in trajectory
123      */
124     public record TrajectorySection(OffsetTrajectory trajectory, Integer section)
125     {
126     };
127 
128 }