Code Monkey home page Code Monkey logo

two-dimensional-time-to-collision's Introduction

Two-Dimensional-Time-To-Collision

This repository allows for fast computation of two-dimensional Time-To-Collision (2D-TTC). This is particularly useful for evaluating the traffic conflict risk at intersections, but for sure can also be used in the scenario of highways. A document explaining the methods is provided here.

Note that this method follows the typical definition of TTC assuming constant velocity at the time moment of evaluation (a reference can be found here) as shown in the figure below. This clearly differs from alternative definitions of 2D-TTC such as time advantage or predicted PET (https://doi.org/10.1016/j.aap.2010.03.021). Due to the drawback of the constant-velocity assumption, this method may not suit well for slow and conscious interactions.

If you use this software in your work, please cite it using the following metadata:

@software{jiao2023ttc,
author = {Jiao, Yiru},
month = mar,
title = {{A fast calculation of two-dimensional Time-to-Collision}},
url = {https://github.com/Yiru-Jiao/Two-Dimensional-Time-To-Collision},
year = {2023}
}

Libarary requirements

Any versions of pandas and numpy.

Usage

Use function TTC(samples, 'dataframe') or TTC(samples, 'values') to compute two-dimensional Time-To-Collision.

For example,

import sys
sys.path.append('') # add the path where you save this `.py` file
import TwoDimTTC

# To return a dataframe with the input vehicle pair samples, where 2D-TTC are saved in a new column named 'TTC'
samples = TwoDimTTC.TTC(samples, 'dataframe')

# To return a numpy array of 2D-TTC values
ttc = TwoDimTTC.TTC(samples, 'values')

Input

The first input is a pandas dataframe of vehicle pair samples, which should include the following columns.

  • x_i : x coordinate of the ego vehicle $i$ (usually assumed to be centroid)
  • y_i : y coordinate of the ego vehicle $i$ (usually assumed to be centroid)
  • vx_i : x coordinate of the velocity of the ego vehicle $i$
  • vy_i : y coordinate of the velocity of the ego vehicle $i$
  • hx_i : x coordinate of the heading direction of the ego vehicle $i$
  • hy_i : y coordinate of the heading direction of the ego vehicle $i$
  • length_i : length of the ego vehicle $i$
  • width_i : width of the ego vehicle $i$
  • x_j : x coordinate of another vehicle $j$ (usually assumed to be centroid)
  • y_j : y coordinate of another vehicle $j$ (usually assumed to be centroid)
  • vx_j : x coordinate of the velocity of another vehicle $j$
  • vy_j : y coordinate of the velocity of another vehicle $j$
  • hx_j : x coordinate of the heading direction of another vehicle $j$
  • hy_j : y coordinate of the heading direction of another vehicle $j$
  • length_j : length of another vehicle $j$
  • width_j : width of another vehicle $j$

The second input allows outputing a dataframe with inputed samples plus a new column named 'TTC', or mere TTC values.

Output

If ttc==np.inf, the ego vehicle $i$ and another vehicle $j$ will never collide if they keep current speed.

A negative TTC means the boxes of the ego vehicle $i$ and another vehicle $j$ are overlapping. This is due to approximating the space occupied by a vehicle with a rectangular. In other words, ttc<0 in this computation means the collision between the two vehicles almost (or although seldom, already) occurred.

Note that mere TTC computation can give an extreme small positive value even when the vehivles are overlapping a bit. In order to improve the accuracy, please use function CurrentD(samples, 'dataframe') or CurrentD(samples, 'values') to further exclude overlapping vehicles. This function calculate current distance between the ego vehicle $i$ and another vehicle $j$, which indicate overlapping when the value is negative.

# Within pandas dataframe
samples = TwoDimTTC.TTC(samples, 'dataframe')
samples = TwoDimTTC.CurrentD(samples, 'dataframe')
samples.loc[(samples.CurrentD<0)&(samples.TTC<np.inf)&(samples.TTC>0),'TTC'] = -1

# Using numpy array of values
ttc = TwoDimTTC.TTC(samples, 'values')
current_dist = TwoDimTTC.CurrentD(samples, 'values')
ttc[(current_dist<0)&(ttc<np.inf)&(ttc>0)] = -1

Efficiency

Use function efficiency(samples, iterations) to test the computation efficiency.

For example,

print('Average time cost = {:.4f} second(s)'.format(TwoDimTTC.efficiency(samples, 10)

The following table shows approximately needed computation time (tested for 10 iterations of experiments).

number of vehicle pairs computation time (s)
1e4 0.0357
1e5 0.4342
1e6 7.1657

Copyright

Copyright (c) 2022 Yiru Jiao. All rights reserved.

This work is licensed under the terms of the MIT license. For a copy, see https://opensource.org/licenses/MIT.

two-dimensional-time-to-collision's People

Contributors

yiru-jiao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

two-dimensional-time-to-collision's Issues

hx_i : x coordinate of the heading direction of the ego vehicle

Hello, i am not sure that i understand what is this vector (or is it a point in the 2d space) : hx_i : x coordinate of the heading direction of the ego vehicle, isn't the direction i am heading toward the same as my current velocity vector?

Thanks for taking time to answer my questions =)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.