package org.opentrafficsim.kpi.sampling;
import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.djutils.exceptions.Throw;
import org.djutils.immutablecollections.ImmutableIterator;
import org.opentrafficsim.kpi.interfaces.LaneDataInterface;
import org.opentrafficsim.kpi.interfaces.LinkDataInterface;
/**
* A cross sections contains locations on lanes that together make up a cross section. It is not required that this is on a
* single road, i.e. the cross section may any section in space.
*
* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version Sep 29, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class CrossSection implements Serializable
{
/** */
private static final long serialVersionUID = 20160929L;
/** Set of lane locations. */
private final Set directedLanePositions;
/**
* Constructor with set of directed lane positions.
* @param directedLanePositions Set<KpiDirectedLanePosition>; set of lane locations
*/
public CrossSection(final Set directedLanePositions)
{
Throw.whenNull(directedLanePositions, "Directed lane positions may not be null.");
this.directedLanePositions = new LinkedHashSet<>(directedLanePositions);
}
/**
* Constructor with link and direction.
* @param link LinkDataInterface; link
* @param fraction double; fraction on link
* @throws SamplingException if an input is null
*/
public CrossSection(final LinkDataInterface link, final double fraction) throws SamplingException
{
Throw.whenNull(link, "Link lane positions may not be null.");
this.directedLanePositions = new LinkedHashSet<>();
for (LaneDataInterface lane : link.getLaneDatas())
{
KpiLanePosition directedLanePosition = new KpiLanePosition(lane, lane.getLength().times(fraction));
this.directedLanePositions.add(directedLanePosition);
}
}
/**
* @return number of directed lane positions
*/
public final int size()
{
return this.directedLanePositions.size();
}
/**
* @return safe copy of directed lane positions
*/
public final Set getDirectedLanePositions()
{
return new LinkedHashSet<>(this.directedLanePositions);
}
/**
* @return iterator over directed lane positions
*/
public final Iterator getIterator()
{
return new ImmutableIterator<>(this.directedLanePositions.iterator());
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "CrossSection [directedLanePositions=" + this.directedLanePositions + "]";
}
}