Skip to content

Instantly share code, notes, and snippets.

@kvas7andy
Created March 6, 2017 21:51
Show Gist options
  • Save kvas7andy/b593dea6f5d32506cab098138eb9fcac to your computer and use it in GitHub Desktop.
Save kvas7andy/b593dea6f5d32506cab098138eb9fcac to your computer and use it in GitHub Desktop.
Only numpy version of scan matching, no cycles. Function syntax differs
import numpy as np
import scipy
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = (10, 10)
def convert2xy(scans):
angles_loc = np.deg2rad(np.linspace(-120, 120, 682))
valid_obj = scans > 0.02
return (np.array([scans*np.cos(angles_loc), scans*np.sin(angles_loc)]).swapaxes(0, 1).swapaxes(1, 2).reshape(-1, 2)
, valid_obj.ravel())
def rot(points, angle):
return np.append([np.cos(angle)*points[:, 0]-np.sin(angle)*points[:, 1]],
[np.sin(angle)*points[:, 0]+np.cos(angle)*points[:, 1]], axis = 0).T
# pass # rotated_points
def integr(deltas):
angles = np.cumsum(np.append([0], deltas[:, 2], axis=0), axis=0)
return np.append(np.cumsum(np.append([[0, 0]],
rot(deltas[:, :2], angles[1:]), axis = 0), axis=0),
angles.reshape(-1, 1), axis=-1)#trajectory
def plot_points_cloud(trajectory, scans, valid, verbose=True):
plt.figure()
x, y, angles = trajectory[:, 0], trajectory[:, 1], trajectory[:, 2]
if verbose:
plt.plot(x, y, lw=3, c='blue')
clouds = rot(scans[valid], np.repeat(angles, 682)[valid]) + (np.repeat(trajectory[:, :2], 682, axis=0))[valid]
if verbose:
plt.scatter(clouds[:, 0],
clouds[:, 1], s=0.5, c='r')
# pass # figure
if __name__ == "__main__":
scans = np.load("scans.npy")
deltas = np.load("deltas.npy")
## No cycles, only numpy, but different function syntax: convert2xy, plot_points_cloud
import time
tm = time.time()
trajectory = integr(deltas)
xy_scans, valid = convert2xy(scans)
plot_points_cloud(trajectory, xy_scans, valid, verbose=False)
print("Past: {} sec".format(time.time() - tm))
# 0.7 sec if verbose=False -> no plotting only computations
# 11 sec with plotting ()a lot of points
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment