View Javadoc
1   package org.opentrafficsim.animation.data;
2   
3   import java.awt.Color;
4   import java.util.List;
5   
6   import org.djunits.value.vdouble.scalar.Length;
7   import org.djutils.draw.point.OrientedPoint2d;
8   import org.djutils.draw.point.Point2d;
9   import org.opentrafficsim.base.geometry.OtsBounds2d;
10  import org.opentrafficsim.draw.road.ConflictAnimation.ConflictData;
11  import org.opentrafficsim.road.network.lane.conflict.Conflict;
12  
13  /**
14   * Animation data of a Conflict.
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 AnimationConflictData implements ConflictData
22  {
23  
24      /** Conflict. */
25      private final Conflict conflict;
26  
27      /** Contour. */
28      private List<Point2d> contour = null;
29  
30      /**
31       * Constructor.
32       * @param conflict Conflict; conflict.
33       */
34      public AnimationConflictData(final Conflict conflict)
35      {
36          this.conflict = conflict;
37      }
38  
39      /** {@inheritDoc} */
40      @Override
41      public Length getLaneWidth()
42      {
43          return this.conflict.getLane().getWidth(this.conflict.getLongitudinalPosition());
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public OrientedPoint2d getLocation()
49      {
50          return this.conflict.getLocation();
51      }
52  
53      /** {@inheritDoc} */
54      @Override
55      public OtsBounds2d getBounds()
56      {
57          return this.conflict.getBounds();
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public String getId()
63      {
64          return this.conflict.getFullId();
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public Color getColor()
70      {
71          switch (this.conflict.conflictPriority())
72          {
73              case SPLIT:
74                  return Color.BLUE;
75              case PRIORITY:
76                  return Color.GREEN;
77              case YIELD:
78                  return Color.ORANGE;
79              default:
80                  return Color.RED;
81          }
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public List<Point2d> getContour()
87      {
88          if (this.contour == null)
89          {
90              // this creates a new list every time, so we cache it
91              this.contour = this.conflict.getGeometry().getPointList();
92          }
93          return this.contour;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public boolean isCrossing()
99      {
100         return this.conflict.getConflictType().isCrossing();
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public boolean isPermitted()
106     {
107         return this.conflict.isPermitted();
108     }
109 
110     /**
111      * Returns the Conflict.
112      * @return Conflict; GTU.
113      */
114     public Conflict getConflict()
115     {
116         return this.conflict;
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public String toString()
122     {
123         return "Conflict " + this.conflict.getLane().getFullId() + " " + this.conflict.getLongitudinalPosition();
124     }
125 
126 }