AnticipationDensity.java

  1. package org.opentrafficsim.road.gtu.lane.perception.categories;

  2. import java.util.function.Function;
  3. import java.util.function.Supplier;

  4. import org.djunits.value.vdouble.scalar.Length;
  5. import org.djunits.value.vdouble.scalar.LinearDensity;
  6. import org.opentrafficsim.core.gtu.Gtu;
  7. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.Intermediate;
  8. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionAccumulator;
  9. import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.PerceptionCollector;
  10. import org.opentrafficsim.road.gtu.lane.perception.categories.AnticipationDensity.CountAndDistance;

  11. /**
  12.  * Collector to determine density based on GTUs.
  13.  * <p>
  14.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  16.  * </p>
  17.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  18.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  19.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  20.  */
  21. public class AnticipationDensity implements PerceptionCollector<LinearDensity, Gtu, CountAndDistance>
  22. {

  23.     @Override
  24.     public Supplier<CountAndDistance> getIdentity()
  25.     {
  26.         return new Supplier<CountAndDistance>()
  27.         {
  28.             @Override
  29.             public CountAndDistance get()
  30.             {
  31.                 return new CountAndDistance();
  32.             }
  33.         };
  34.     }

  35.     @Override
  36.     public PerceptionAccumulator<Gtu, CountAndDistance> getAccumulator()
  37.     {
  38.         return new PerceptionAccumulator<Gtu, CountAndDistance>()
  39.         {
  40.             @Override
  41.             public Intermediate<CountAndDistance> accumulate(final Intermediate<CountAndDistance> intermediate,
  42.                     final Gtu object, final Length distance)
  43.             {
  44.                 intermediate.getObject().increaseCount();
  45.                 intermediate.getObject().setDistance(distance);
  46.                 return intermediate;
  47.             }
  48.         };
  49.     }

  50.     @Override
  51.     public Function<CountAndDistance, LinearDensity> getFinalizer()
  52.     {
  53.         return new Function<CountAndDistance, LinearDensity>()
  54.         {
  55.             @Override
  56.             public LinearDensity apply(final CountAndDistance intermediate)
  57.             {
  58.                 return LinearDensity.instantiateSI(intermediate.getDistance().si / intermediate.getCount());
  59.             }
  60.         };
  61.     }

  62.     /**
  63.      * Intermediate data to determine density.
  64.      * <p>
  65.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  66.      * <br>
  67.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  68.      * </p>
  69.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  70.      * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  71.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  72.      */
  73.     public static class CountAndDistance
  74.     {

  75.         /** Count. */
  76.         private int count;

  77.         /** Distance. */
  78.         private Length distance;

  79.         /**
  80.          * @return count.
  81.          */
  82.         public int getCount()
  83.         {
  84.             return this.count;
  85.         }

  86.         /**
  87.          * Increases the GTU count by 1.
  88.          */
  89.         public void increaseCount()
  90.         {
  91.             this.count++;
  92.         }

  93.         /**
  94.          * @return distance.
  95.          */
  96.         public Length getDistance()
  97.         {
  98.             return this.distance;
  99.         }

  100.         /**
  101.          * @param distance set distance.
  102.          */
  103.         public void setDistance(final Length distance)
  104.         {
  105.             this.distance = distance;
  106.         }
  107.     }

  108. }