Skip to content

Commit 49ce57d

Browse files
committed
Code cleanup.
1 parent 20f4b80 commit 49ce57d

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

SLAM/iterative_closest_point/iterative_closest_point.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44
"""
55

66
import math
7-
import numpy as np
7+
88
import matplotlib.pyplot as plt
9+
import numpy as np
910

1011
# ICP parameters
1112
EPS = 0.0001
12-
MAXITER = 100
13+
MAX_ITER = 100
1314

1415
show_animation = True
1516

1617

17-
def ICP_matching(ppoints, cpoints):
18+
def icp_matching(previous_points, current_points):
1819
"""
1920
Iterative Closest Point matching
2021
- input
21-
ppoints: 2D points in the previous frame
22-
cpoints: 2D points in the current frame
22+
previous_points: 2D points in the previous frame
23+
current_points: 2D points in the current frame
2324
- output
2425
R: Rotation matrix
2526
T: Translation vector
@@ -35,17 +36,17 @@ def ICP_matching(ppoints, cpoints):
3536

3637
if show_animation: # pragma: no cover
3738
plt.cla()
38-
plt.plot(ppoints[0, :], ppoints[1, :], ".r")
39-
plt.plot(cpoints[0, :], cpoints[1, :], ".b")
39+
plt.plot(previous_points[0, :], previous_points[1, :], ".r")
40+
plt.plot(current_points[0, :], current_points[1, :], ".b")
4041
plt.plot(0.0, 0.0, "xr")
4142
plt.axis("equal")
4243
plt.pause(0.1)
4344

44-
inds, error = nearest_neighbor_assosiation(ppoints, cpoints)
45-
Rt, Tt = SVD_motion_estimation(ppoints[:, inds], cpoints)
45+
indexes, error = nearest_neighbor_association(previous_points, current_points)
46+
Rt, Tt = svd_motion_estimation(previous_points[:, indexes], current_points)
4647

4748
# update current points
48-
cpoints = (Rt @ cpoints) + Tt[:, np.newaxis]
49+
current_points = (Rt @ current_points) + Tt[:, np.newaxis]
4950

5051
H = update_homogeneous_matrix(H, Rt, Tt)
5152

@@ -56,7 +57,7 @@ def ICP_matching(ppoints, cpoints):
5657
if dError <= EPS:
5758
print("Converge", error, dError, count)
5859
break
59-
elif MAXITER <= count:
60+
elif MAX_ITER <= count:
6061
print("Not Converge...", error, dError, count)
6162
break
6263

@@ -85,31 +86,29 @@ def update_homogeneous_matrix(Hin, R, T):
8586
return Hin @ H
8687

8788

88-
def nearest_neighbor_assosiation(ppoints, cpoints):
89+
def nearest_neighbor_association(previous_points, current_points):
8990

9091
# calc the sum of residual errors
91-
dcpoints = ppoints - cpoints
92-
d = np.linalg.norm(dcpoints, axis=0)
92+
delta_points = previous_points - current_points
93+
d = np.linalg.norm(delta_points, axis=0)
9394
error = sum(d)
9495

9596
# calc index with nearest neighbor assosiation
96-
d = np.linalg.norm(
97-
np.repeat(cpoints, ppoints.shape[1], axis=1) - np.tile(ppoints, (1,
98-
cpoints.shape[1])), axis=0)
99-
inds = np.argmin(d.reshape(cpoints.shape[1], ppoints.shape[1]), axis=1)
100-
101-
return inds, error
97+
d = np.linalg.norm(np.repeat(current_points, previous_points.shape[1], axis=1)
98+
- np.tile(previous_points, (1, current_points.shape[1])), axis=0)
99+
indexes = np.argmin(d.reshape(current_points.shape[1], previous_points.shape[1]), axis=1)
102100

101+
return indexes, error
103102

104-
def SVD_motion_estimation(ppoints, cpoints):
105103

106-
pm = np.mean(ppoints, axis=1)
107-
cm = np.mean(cpoints, axis=1)
104+
def svd_motion_estimation(previous_points, current_points):
105+
pm = np.mean(previous_points, axis=1)
106+
cm = np.mean(current_points, axis=1)
108107

109-
pshift = ppoints - pm[:, np.newaxis]
110-
cshift = cpoints - cm[:, np.newaxis]
108+
p_shift = previous_points - pm[:, np.newaxis]
109+
c_shift = current_points - cm[:, np.newaxis]
111110

112-
W = cshift @ pshift.T
111+
W = c_shift @ p_shift.T
113112
u, s, vh = np.linalg.svd(W)
114113

115114
R = (u @ vh).T
@@ -133,16 +132,16 @@ def main():
133132
# previous points
134133
px = (np.random.rand(nPoint) - 0.5) * fieldLength
135134
py = (np.random.rand(nPoint) - 0.5) * fieldLength
136-
ppoints = np.vstack((px, py))
135+
previous_points = np.vstack((px, py))
137136

138137
# current points
139138
cx = [math.cos(motion[2]) * x - math.sin(motion[2]) * y + motion[0]
140139
for (x, y) in zip(px, py)]
141140
cy = [math.sin(motion[2]) * x + math.cos(motion[2]) * y + motion[1]
142141
for (x, y) in zip(px, py)]
143-
cpoints = np.vstack((cx, cy))
142+
current_points = np.vstack((cx, cy))
144143

145-
R, T = ICP_matching(ppoints, cpoints)
144+
R, T = icp_matching(previous_points, current_points)
146145
print("R:", R)
147146
print("T:", T)
148147

tests/test_LQR_planner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import sys
12
from unittest import TestCase
23

3-
import sys
44
sys.path.append("./PathPlanning/LQRPlanner")
55

66
from PathPlanning.LQRPlanner import LQRplanner as m
@@ -11,5 +11,5 @@
1111
class Test(TestCase):
1212

1313
def test1(self):
14-
m.show_animation = False
14+
m.SHOW_ANIMATION = False
1515
m.main()

0 commit comments

Comments
 (0)