View Javadoc
1   package org.opentrafficsim.editor.extensions.map;
2   
3   import java.util.List;
4   
5   import org.djunits.value.vdouble.scalar.Direction;
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djutils.draw.line.PolyLine2d;
8   import org.opentrafficsim.base.StripeElement;
9   import org.opentrafficsim.base.StripeElement.StripeLateralSync;
10  import org.opentrafficsim.base.geometry.DirectionalPolyLine;
11  import org.opentrafficsim.base.geometry.OtsLocatable;
12  import org.opentrafficsim.draw.road.StripeAnimation.StripeData;
13  import org.opentrafficsim.editor.XsdTreeNode;
14  import org.opentrafficsim.editor.extensions.map.MapLinkData.MiddleOffset;
15  import org.opentrafficsim.road.network.lane.CrossSectionGeometry;
16  
17  /**
18   * Stripe data for in the editor.
19   * <p>
20   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
21   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
22   * </p>
23   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
24   */
25  public class MapStripeData extends MapCrossSectionData implements StripeData
26  {
27  
28      /** Width. */
29      private final Length width;
30  
31      /** Dash offset. */
32      private Length dashOffset;
33  
34      /** Start offset. */
35      private final Length laterealStartOffset;
36  
37      /** Stripe elements. */
38      private final List<StripeElement> elements;
39  
40      /** Lateral synchronization. */
41      private final StripeLateralSync lateralSync;
42  
43      /** Link line. */
44      private final PolyLine2d linkLine;
45  
46      /** Link reference line. */
47      private PolyLine2d linkReferenceLine = null;
48  
49      /** Middle offsets to determine link reference line. */
50      private final MiddleOffset middleOffset;
51  
52      /** Start direction. */
53      private final Direction startDirection;
54  
55      /** End direction. */
56      private final Direction endDirection;
57  
58      /** Center line with direction. */
59      private DirectionalPolyLine centerLine;
60  
61      /**
62       * Constructor.
63       * @param dashOffset dash offset
64       * @param linkNode node representing the element
65       * @param geometry geometry
66       * @param elements elements
67       * @param lateralSync lateral synchronization
68       * @param linkLine link line
69       * @param middleOffset middle offsets to determine link reference line
70       * @param startDirection start direction
71       * @param endDirection end direction
72       */
73      public MapStripeData(final Length dashOffset, final XsdTreeNode linkNode, final CrossSectionGeometry geometry,
74              final List<StripeElement> elements, final StripeLateralSync lateralSync, final PolyLine2d linkLine,
75              final MiddleOffset middleOffset, final Direction startDirection, final Direction endDirection)
76      {
77          super(linkNode, geometry);
78          Length w = Length.ZERO;
79          for (StripeElement element : elements)
80          {
81              w = w.plus(element.width());
82          }
83          this.width = w;
84          this.dashOffset = dashOffset;
85          this.laterealStartOffset = Length.instantiateSI(geometry.offset().apply(0.0));
86          this.elements = elements;
87          this.lateralSync = lateralSync;
88          this.linkLine = linkLine;
89          this.middleOffset = middleOffset;
90          this.startDirection = startDirection;
91          this.endDirection = endDirection;
92      }
93  
94      @Override
95      public PolyLine2d getLine()
96      {
97          return OtsLocatable.transformLine(getCenterLine(), getLocation());
98      }
99  
100     @Override
101     public Length getWidth(final Length position)
102     {
103         return this.width;
104     }
105 
106     @Override
107     public PolyLine2d getReferenceLine()
108     {
109         return this.lateralSync.equals(StripeLateralSync.NONE) ? this.getCenterLine() : getLinkReferenceLine();
110     }
111 
112     @Override
113     public DirectionalPolyLine getCenterLine()
114     {
115         if (this.centerLine == null)
116         {
117             this.centerLine = new DirectionalPolyLine(super.getCenterLine(), this.startDirection, this.endDirection);
118         }
119         return this.centerLine;
120     }
121 
122     /**
123      * Return link reference line.
124      * @return link reference line
125      */
126     private PolyLine2d getLinkReferenceLine()
127     {
128         if (this.linkReferenceLine == null)
129         {
130             this.linkReferenceLine = new DirectionalPolyLine(this.linkLine, this.startDirection, this.endDirection)
131                     .directionalOffsetLine(this.middleOffset.getStartOffset(), this.middleOffset.getEndOffset());
132         }
133         return this.linkReferenceLine;
134     }
135 
136     @Override
137     public List<StripeElement> getElements()
138     {
139         return this.elements;
140     }
141 
142     @Override
143     public Length getDashOffset()
144     {
145         return this.dashOffset;
146     }
147     
148     /**
149      * Returns the lateral sync.
150      * @return lateral sync.
151      */
152     public StripeLateralSync getLateralSync()
153     {
154         return this.lateralSync;
155     }
156 
157     /**
158      * Sets a new dash offset, which should only occur due to synchronization.
159      * @param dashOffset dash offset
160      */
161     public void setDashOffset(final Length dashOffset)
162     {
163         this.dashOffset = dashOffset;
164     }
165     
166 
167     @Override
168     public String toString()
169     {
170         return "Stripe " + getLinkId() + " " + this.laterealStartOffset;
171     }
172 
173 }