LaneGeometryUtil.java

  1. package org.opentrafficsim.road.network.lane;

  2. import java.util.Map;

  3. import org.djunits.value.vdouble.scalar.Length;
  4. import org.djunits.value.vdouble.scalar.Speed;
  5. import org.djutils.draw.line.PolyLine2d;
  6. import org.djutils.draw.line.Polygon2d;
  7. import org.djutils.draw.point.Point2d;
  8. import org.djutils.exceptions.Try;
  9. import org.opentrafficsim.core.geometry.ContinuousLine;
  10. import org.opentrafficsim.core.geometry.ContinuousLine.ContinuousDoubleFunction;
  11. import org.opentrafficsim.core.geometry.ContinuousStraight;
  12. import org.opentrafficsim.core.geometry.FractionalLengthData;
  13. import org.opentrafficsim.core.gtu.GtuType;

  14. /**
  15.  * This class is an extension (conceptually, not an actual java extension) of {@code OtsGeometryUtil}. This utility has access
  16.  * to classes that are specific to the ots-road project, and required to define geometry of objects in this context.
  17.  * <p>
  18.  * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  19.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  20.  * </p>
  21.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  22.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  23.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  24.  */
  25. public final class LaneGeometryUtil
  26. {

  27.     /**
  28.      * Utility class.
  29.      */
  30.     private LaneGeometryUtil()
  31.     {
  32.         //
  33.     }

  34.     /**
  35.      * Returns the contour based on left and right edge.
  36.      * @param leftEdge left edge, in design line direction.
  37.      * @param rightEdge right edge, in design line direction.
  38.      * @return a closed loop of both edges.
  39.      */
  40.     public static Polygon2d getContour(final PolyLine2d leftEdge, final PolyLine2d rightEdge)
  41.     {
  42.         Point2d[] points = new Point2d[leftEdge.size() + rightEdge.size() + 1];
  43.         System.arraycopy(leftEdge.getPointList().toArray(), 0, points, 0, leftEdge.size());
  44.         System.arraycopy(rightEdge.reverse().getPointList().toArray(), 0, points, leftEdge.size(), rightEdge.size());
  45.         points[points.length - 1] = points[0]; // close loop
  46.         return new Polygon2d(true, points);
  47.     }

  48.     /**
  49.      * Creates a simple straight lane. This method exists to create lanes for simple tests.
  50.      * @param link link.
  51.      * @param id id.
  52.      * @param offset end offset.
  53.      * @param width end width.
  54.      * @param laneType lane type.
  55.      * @param speedLimits speed limit map.
  56.      * @return lane.
  57.      */
  58.     public static Lane createStraightLane(final CrossSectionLink link, final String id, final Length offset, final Length width,
  59.             final LaneType laneType, final Map<GtuType, Speed> speedLimits)
  60.     {
  61.         return createStraightLane(link, id, offset, offset, width, width, laneType, speedLimits);
  62.     }

  63.     /**
  64.      * Creates a simple straight lane. This method exists to create lanes for simple tests.
  65.      * @param link link.
  66.      * @param id id.
  67.      * @param startOffset start offset.
  68.      * @param endOffset end offset.
  69.      * @param startWidth start width.
  70.      * @param endWidth end width.
  71.      * @param laneType lane type.
  72.      * @param speedLimits speed limit map.
  73.      * @return lane.
  74.      */
  75.     public static Lane createStraightLane(final CrossSectionLink link, final String id, final Length startOffset,
  76.             final Length endOffset, final Length startWidth, final Length endWidth, final LaneType laneType,
  77.             final Map<GtuType, Speed> speedLimits)
  78.     {
  79.         ContinuousDoubleFunction offset = FractionalLengthData.of(0.0, startOffset.si, 1.0, endOffset.si);
  80.         ContinuousDoubleFunction width = FractionalLengthData.of(0.0, startWidth.si, 1.0, endWidth.si);
  81.         return createStraightLane(link, id, offset, width, laneType, speedLimits);
  82.     }

  83.     /**
  84.      * Creates a simple straight lane. This method exists to create lanes for simple tests.
  85.      * @param link link
  86.      * @param id id
  87.      * @param offset offset information
  88.      * @param width offset information
  89.      * @param laneType lane type
  90.      * @param speedLimits speed limit map
  91.      * @return lane
  92.      */
  93.     public static Lane createStraightLane(final CrossSectionLink link, final String id, final ContinuousDoubleFunction offset,
  94.             final ContinuousDoubleFunction width, final LaneType laneType, final Map<GtuType, Speed> speedLimits)
  95.     {
  96.         ContinuousLine designLine = new ContinuousStraight(
  97.                 Try.assign(() -> link.getDesignLine().getLocationPointFraction(0.0), "Link should have a valid design line."),
  98.                 link.getLength().si);
  99.         return Try.assign(
  100.                 () -> new Lane(link, id, CrossSectionGeometry.of(designLine, null, offset, width), laneType, speedLimits),
  101.                 "Network exception.");
  102.     }

  103.     /**
  104.      * Creates a simple straight lane. This method exists to create lanes for simple tests.
  105.      * @param type stripe data.
  106.      * @param id id
  107.      * @param link link.
  108.      * @param offset end offset.
  109.      * @param width end width.
  110.      * @return lane.
  111.      */
  112.     public static Stripe createStraightStripe(final StripeData type, final String id, final CrossSectionLink link,
  113.             final Length offset, final Length width)
  114.     {
  115.         ContinuousLine designLine = new ContinuousStraight(
  116.                 Try.assign(() -> link.getDesignLine().getLocationPointFraction(0.0), "Link should have a valid design line."),
  117.                 link.getLength().si);
  118.         ContinuousDoubleFunction offsetFunc = FractionalLengthData.of(0.0, offset.si, 1.0, offset.si);
  119.         ContinuousDoubleFunction widthFunc = FractionalLengthData.of(0.0, width.si, 1.0, width.si);
  120.         return Try.assign(() -> new Stripe(id, type, link, CrossSectionGeometry.of(designLine, null, offsetFunc, widthFunc)),
  121.                 "Network exception.");
  122.     }

  123.     /**
  124.      * Creates a simple straight shoulder. This method exists to create shoulders for simple tests.
  125.      * @param link link.
  126.      * @param id id.
  127.      * @param startOffset start offset.
  128.      * @param endOffset end offset.
  129.      * @param startWidth start width.
  130.      * @param endWidth end width.
  131.      * @param laneType lane type.
  132.      * @return lane.
  133.      */
  134.     public static Object createStraightShoulder(final CrossSectionLink link, final String id, final Length startOffset,
  135.             final Length endOffset, final Length startWidth, final Length endWidth, final LaneType laneType)
  136.     {
  137.         ContinuousLine designLine = new ContinuousStraight(
  138.                 Try.assign(() -> link.getDesignLine().getLocationPointFraction(0.0), "Link should have a valid design line."),
  139.                 link.getLength().si);
  140.         ContinuousDoubleFunction offset = FractionalLengthData.of(0.0, startOffset.si, 1.0, endOffset.si);
  141.         ContinuousDoubleFunction width = FractionalLengthData.of(0.0, startWidth.si, 1.0, endWidth.si);
  142.         return Try.assign(() -> new Shoulder(link, id, CrossSectionGeometry.of(designLine, null, offset, width), laneType),
  143.                 "Network exception.");
  144.     }
  145. }