-
Notifications
You must be signed in to change notification settings - Fork 0
/
visu.py
100 lines (79 loc) · 3.41 KB
/
visu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Implementation inspired by Hugues Thomas
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import collections as mc
from mpl_toolkits.mplot3d import Axes3D
def show_ICP(data, ref, R_list, T_list, neighbors_list):
'''
Show a succession of transformation obtained by ICP.
Inputs :
data = (d x N1) matrix where "N1" is the number of point and "d" the dimension
ref = (d x N2) matrix where "N2" is the number of point and "d" the dimension
R = list of (d x d) rotation matrix
T = list of (d x 1) translation vector
neighbors_list = list of N1 indices. The neighbors of data in ref
This function works if R_i and T_i represent the tranformation of the original cloud
at iteration i, such that data_(i) = R_i * data + T_i
If you save incremental transformations such that data_(i) = R_i * data_(i-1) + T_i,
you will need to modify your R_list and T_list
'''
# Get the number of iteration
max_iter = len(R_list)
# Get data dimension
dim = data.shape[0]
# Insert identity as first transformation
R_list.insert(0, np.eye(dim))
T_list.insert(0, np.zeros((dim, 1)))
# Create global variable for the graph plot
global iteration, show_neighbors
iteration = 0
show_neighbors = 0
# Define the function drawing the points
def draw_event():
data_aligned = R_list[iteration].dot(data) + T_list[iteration]
plt.cla()
if dim == 2:
ax.plot(ref[0], ref[1], '.')
ax.plot(data_aligned[0], data_aligned[1], '.')
if show_neighbors and iteration < max_iter:
lines = [[data_aligned[:, ind1], ref[:, ind2]] for ind1, ind2 in enumerate(neighbors_list[iteration])]
lc = mc.LineCollection(lines, colors=[0, 1, 0, 0.5], linewidths=1)
ax.add_collection(lc)
plt.axis('equal')
if dim == 3:
ax.plot(ref[0], ref[1], ref[2], '.')
ax.plot(data_aligned[0], data_aligned[1], data_aligned[2], '.')
#plt.axis('equal')
if show_neighbors and iteration < max_iter:
ax.set_title('Iteration {:d} ===> press right / left to change\nNeighbors ON ===> Press n to change (only in 2D)'.format(iteration))
else:
ax.set_title('Iteration {:d} ===> press right / left to change\nNeighbors OFF ===> Press n to change (only in 2D)'.format(iteration))
plt.draw()
# Define the function getting keyborad inputs
def press(event):
global iteration, show_neighbors
if event.key == 'right':
if iteration < max_iter:
iteration += 1
if event.key == 'left':
if iteration > 0:
iteration -= 1
if event.key == 'n':
if dim < 3:
show_neighbors = 1 - show_neighbors
draw_event()
# Create figure
fig = plt.figure()
# Intitiate graph for 3D data
if dim == 2:
ax = fig.add_subplot(111)
elif dim == 3:
ax = fig.add_subplot(111, projection='3d')
else:
print('wrong data dimension')
# Connect keyboard function to the figure
fig.canvas.mpl_connect('key_press_event', press)
# Plot first iteration
draw_event()
# Start figure
plt.show()