View Javadoc
1   package org.opentrafficsim.editor.extensions.map;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djutils.draw.line.PolyLine2d;
5   import org.djutils.draw.line.Polygon2d;
6   import org.djutils.draw.line.Ray2d;
7   import org.djutils.draw.point.OrientedPoint2d;
8   import org.opentrafficsim.base.geometry.BoundingPolygon;
9   import org.opentrafficsim.base.geometry.OtsBounds2d;
10  import org.opentrafficsim.draw.road.CrossSectionElementAnimation.CrossSectionElementData;
11  import org.opentrafficsim.editor.XsdTreeNode;
12  import org.opentrafficsim.road.network.lane.SliceInfo;
13  
14  /**
15   * Cross section element data for in the editor.
16   * <p>
17   * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
18   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
19   * </p>
20   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
21   */
22  public class MapCrossSectionData implements CrossSectionElementData
23  {
24  
25      /** Node representing the element. */
26      private final XsdTreeNode linkNode;
27  
28      /** Location. */
29      private final OrientedPoint2d location;
30      
31      /** Center line. */
32      protected final PolyLine2d centerLine;
33  
34      /** Bounds. */
35      private final OtsBounds2d bounds;
36  
37      /** Slice info. */
38      private SliceInfo sliceInfo;
39  
40      /**
41       * Constructor.
42       * @param linkNode XsdTreeNode; node representing the element.
43       * @param centerLine PolyLine2d; center line.
44       * @param contour PolyLine2d; contour.
45       * @param sliceInfo SliceInfo; slice info.
46       */
47      public MapCrossSectionData(final XsdTreeNode linkNode, final PolyLine2d centerLine, final Polygon2d contour,
48              final SliceInfo sliceInfo)
49      {
50          this.linkNode = linkNode;
51          Ray2d ray = centerLine.getLocationFractionExtended(0.5);
52          this.location = new OrientedPoint2d(ray.x, ray.y, ray.phi);
53          this.centerLine = centerLine;
54          this.bounds = BoundingPolygon.geometryToBounds(this.location, contour);
55          this.sliceInfo = sliceInfo;
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public OtsBounds2d getBounds()
61      {
62          return this.bounds;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public OrientedPoint2d getLocation()
68      {
69          return this.location;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public PolyLine2d getCenterLine()
75      {
76          return this.centerLine;
77      }
78  
79      /**
80       * Returns the link id.
81       * @return String; link id.
82       */
83      @Override
84      public String getLinkId()
85      {
86          return this.linkNode.getId();
87      }
88  
89      /**
90       * Returns the lane width at the give position.
91       * @param position Length; position along the lane.
92       * @return Length; lane width at the position.
93       */
94      public Length getWidth(final Length position)
95      {
96          return this.sliceInfo.getWidth(position.si / this.centerLine.getLength());
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public String toString()
102     {
103         return "Cross section element of " + getLinkId();
104     }
105 
106 }