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