ContourPlotAcceleration.java

package org.opentrafficsim.animation.graphs;

import java.awt.Color;
import java.util.List;

import org.djunits.unit.AccelerationUnit;
import org.djunits.value.ValueRuntimeException;
import org.djunits.value.vdouble.matrix.AccelerationMatrix;
import org.djunits.value.vdouble.scalar.Acceleration;
import org.djunits.value.vdouble.scalar.Duration;
import org.djunits.value.vdouble.scalar.Length;
import org.djutils.math.means.ArithmeticMean;
import org.opentrafficsim.animation.BoundsPaintScale;
import org.opentrafficsim.animation.Colors;
import org.opentrafficsim.animation.egtf.Converter;
import org.opentrafficsim.animation.egtf.Quantity;
import org.opentrafficsim.animation.graphs.ContourDataSource.ContourAdditionalDataType;
import org.opentrafficsim.animation.graphs.ContourDataSource.ContourDataType;
import org.opentrafficsim.base.OtsRuntimeException;
import org.opentrafficsim.kpi.sampling.Trajectory;
import org.opentrafficsim.kpi.sampling.TrajectoryGroup;

/**
 * Contour plot for acceleration.
 * <p>
 * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
 * </p>
 * @author Alexander Verbraeck
 * @author Peter Knoppers
 * @author Wouter Schakel
 */
public class ContourPlotAcceleration extends AbstractContourPlot<Acceleration>
{

    /** Quantity for the EGTF. */
    private static final Quantity<Acceleration, AccelerationMatrix> QUANTITY =
            new Quantity<>("acceleration", new Converter<AccelerationMatrix>()
            {
                @Override
                public AccelerationMatrix convert(final double[][] filteredData)
                {
                    try
                    {
                        return new AccelerationMatrix(filteredData, AccelerationUnit.SI);
                    }
                    catch (ValueRuntimeException exception)
                    {
                        // should not happen as filtered data comes from the EGTF
                        throw new OtsRuntimeException("Unexpected exception while converting acceleration to output format.",
                                exception);
                    }
                }
            });

    /** Contour data type. */
    private static final ContourAdditionalDataType<Acceleration, ArithmeticMean<Double, Double>> CONTOUR_DATA_TYPE =
            new ContourAdditionalDataType<Acceleration, ArithmeticMean<Double, Double>>()
            {
                @Override
                public ArithmeticMean<Double, Double> identity()
                {
                    return new ArithmeticMean<>();
                }

                @Override
                public ArithmeticMean<Double, Double> processSeries(final ArithmeticMean<Double, Double> intermediate,
                        final List<TrajectoryGroup<?>> trajectories, final List<Length> xFrom, final List<Length> xTo,
                        final Duration tFrom, final Duration tTo)
                {
                    for (int i = 0; i < trajectories.size(); i++)
                    {
                        TrajectoryGroup<?> trajectoryGroup = trajectories.get(i);
                        for (Trajectory<?> trajectory : trajectoryGroup.getTrajectories())
                        {
                            if (GraphUtil.considerTrajectory(trajectory, tFrom, tTo))
                            {
                                trajectory = trajectory.subSet(xFrom.get(i), xTo.get(i), tFrom, tTo);
                                ContourDataType.weighted(trajectory.getA(), trajectory.getX(), intermediate);
                            }
                        }
                    }
                    return intermediate;
                }

                @Override
                public Acceleration finalize(final ArithmeticMean<Double, Double> intermediate)
                {
                    return Acceleration.ofSI(intermediate.getMean());
                }

                @Override
                public Quantity<Acceleration, ?> getQuantity()
                {
                    return QUANTITY;
                }

                @Override
                public boolean normalize()
                {
                    return false;
                }

            };

    /**
     * Constructor.
     * @param caption caption
     * @param source data source
     */
    public ContourPlotAcceleration(final String caption, final ContourDataSource source)
    {
        super(caption, source, CONTOUR_DATA_TYPE, createPaintScale(),
                new LabelData<>(Acceleration.ofSI(1.0), "%.1fm/s\u00B2", "acceleration %.2f m/s\u00B2"));
    }

    /**
     * Creates a paint scale from red, via yellow to green.
     * @return paint scale
     */
    private static BoundsPaintScale createPaintScale()
    {
        double[] boundaries = {-3.0, -1.5, 0.0, 1.0, 2.0};
        Color[] colorValues = Colors.reverse(Colors.GREEN_RED_DARK);
        return new BoundsPaintScale(boundaries, colorValues);
    }

    @Override
    public GraphType getGraphType()
    {
        return GraphType.ACCELERATION_CONTOUR;
    }

    @Override
    protected double scale(final double si)
    {
        return si;
    }

    @Override
    public String toString()
    {
        return "ContourPlotAcceleration [" + getCaption() + "]";
    }

}