Skip to content

Instantly share code, notes, and snippets.

@Kyungpyo-Kim
Last active June 18, 2020 06:22
Show Gist options
  • Select an option

  • Save Kyungpyo-Kim/a266d4355a40f6c2d83f3fafd52d0fe0 to your computer and use it in GitHub Desktop.

Select an option

Save Kyungpyo-Kim/a266d4355a40f6c2d83f3fafd52d0fe0 to your computer and use it in GitHub Desktop.
curvature calcuation
import numpy as np
def fn_radius_cal(x, y):
"""
@function: calcuate radius from x, y
@input: x, y of trajectory
@internal: curvature
@output: radius of trajectory
reference: https://stackoverflow.com/questions/28269379/curve-curvature-in-numpy
"""
dx_dt = np.gradient(x)
dy_dt = np.gradient(y)
velocity = np.array([ [dx_dt[i], dy_dt[i]] for i in range(dx_dt.size)])
ds_dt = np.sqrt(dx_dt * dx_dt + dy_dt * dy_dt)
tangent = np.array([1/ds_dt] * 2).transpose() * velocity
tangent_x = tangent[:, 0]
tangent_y = tangent[:, 1]
deriv_tangent_x = np.gradient(tangent_x)
deriv_tangent_y = np.gradient(tangent_y)
dT_dt = np.array([ [deriv_tangent_x[i], deriv_tangent_y[i]] for i in range(deriv_tangent_x.size)])
length_dT_dt = np.sqrt(deriv_tangent_x * deriv_tangent_x + deriv_tangent_y * deriv_tangent_y)
normal = np.array([1/length_dT_dt] * 2).transpose() * dT_dt
d2s_dt2 = np.gradient(ds_dt)
d2x_dt2 = np.gradient(dx_dt)
d2y_dt2 = np.gradient(dy_dt)
curvature = np.abs(d2x_dt2 * dy_dt - dx_dt * d2y_dt2) / (dx_dt * dx_dt + dy_dt * dy_dt)**1.5
t_component = np.array([d2s_dt2] * 2).transpose()
n_component = np.array([curvature * ds_dt * ds_dt] * 2).transpose()
acceleration = t_component * tangent + n_component * normal
return 1/curvature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment