org.opentrafficsim.road.gtu.lane.CollisionDetector Maven / Gradle / Ivy
package org.opentrafficsim.road.gtu.lane;
import java.util.Iterator;
import org.opentrafficsim.core.gtu.GtuException;
import org.opentrafficsim.core.gtu.plan.operational.OperationalPlanException;
import org.opentrafficsim.core.network.Network;
import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable.UnderlyingDistance;
import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGtu;
/**
* Checks for collisions.
*
* Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class CollisionDetector extends AbstractLaneBasedMoveChecker
{
/**
* Constructor.
* @param network Network; network
*/
public CollisionDetector(final Network network)
{
super(network);
}
/** {@inheritDoc} */
@Override
public void checkMove(final LaneBasedGtu gtu) throws Exception
{
try
{
NeighborsPerception neighbors =
gtu.getTacticalPlanner().getPerception().getPerceptionCategory(NeighborsPerception.class);
PerceptionCollectable leaders = neighbors.getLeaders(RelativeLane.CURRENT);
Iterator> gtus = leaders.underlyingWithDistance();
if (!gtus.hasNext())
{
return;
}
UnderlyingDistance leader = gtus.next();
if (leader.getDistance().lt0())
{
throw new CollisionException("GTU " + gtu.getId() + " collided with GTU " + leader.getObject().getId());
}
}
catch (OperationalPlanException exception)
{
throw new GtuException(exception);
}
}
}