-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathsimpleicp.jl
301 lines (211 loc) · 8.71 KB
/
simpleicp.jl
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using Dates
using DelimitedFiles
using LinearAlgebra
using MultivariateStats
using NearestNeighbors # https://github.com/KristofferC/NearestNeighbors.jl
using Printf
using Statistics
using StatsBase
mutable struct PointCloud
x::Vector{Float64}
y::Vector{Float64}
z::Vector{Float64}
nx::Vector{Float64}
ny::Vector{Float64}
nz::Vector{Float64}
planarity::Vector{Float64}
no_points::Int64
sel::Vector{Int64}
function PointCloud(x, y, z)
no_points = length(x)
new(x,
y,
z,
fill(NaN, no_points),
fill(NaN, no_points),
fill(NaN, no_points),
fill(NaN, no_points),
no_points,
collect(1:no_points))
end
end
function select_in_range!(pc::PointCloud, X::Array, max_range::Number)
size(X)[2] == 3 || error(""""X" must have 3 columns""")
max_range > 0 || error(""""max_range" must be > 0""")
kdtree = KDTree(X')
query_points = [pc.x[pc.sel]'; pc.y[pc.sel]'; pc.z[pc.sel]']
_, distances = nn(kdtree, query_points)
keep = [d <= max_range for d in distances]
pc.sel = pc.sel[keep]
end
function select_n_points!(pc::PointCloud, n)
no_selected_points = length(pc.sel)
if no_selected_points > n
idx = round.(Int, range(1, no_selected_points, length=n))
pc.sel = pc.sel[idx]
end
end
function estimate_normals!(pc::PointCloud, neighbors)
kdtree = KDTree([pc.x'; pc.y'; pc.z'])
query_points = [pc.x[pc.sel]'; pc.y[pc.sel]'; pc.z[pc.sel]']
idxNN_all_qp, = knn(kdtree, query_points, neighbors, false)
for (i, idxNN) in enumerate(idxNN_all_qp)
selected_points = [pc.x[idxNN]'; pc.y[idxNN]'; pc.z[idxNN]']
# P = fit(PCA, selected_points, pratio=1.0)
# pc.nx[pc.sel[i]] = projection(P)[1,3]
# pc.ny[pc.sel[i]] = projection(P)[2,3]
# pc.nz[pc.sel[i]] = projection(P)[3,3]
C = cov(selected_points, dims=2)
F = eigen(C) # eigenvalues are in ascending order
pc.nx[pc.sel[i]] = F.vectors[1,1]
pc.ny[pc.sel[i]] = F.vectors[2,1]
pc.nz[pc.sel[i]] = F.vectors[3,1]
pc.planarity[pc.sel[i]] = (F.values[2]-F.values[1])/F.values[3];
end
end
function transform!(pc, H)
XInH = euler_coord_to_homogeneous_coord([pc.x pc.y pc.z])
XOutH = transpose(H*XInH')
XOut = homogeneous_coord_to_euler_coord(XOutH)
pc.x = XOut[:,1]
pc.y = XOut[:,2]
pc.z = XOut[:,3]
return pc
end
function matching!(pcmov::PointCloud, pcfix)
kdtree = KDTree([pcmov.x'; pcmov.y'; pcmov.z'])
query_points = [pcfix.x[pcfix.sel]'
pcfix.y[pcfix.sel]'
pcfix.z[pcfix.sel]']
idxNN, = knn(kdtree, query_points, 1)
pcmov.sel = vcat(idxNN...)
dx = pcmov.x[pcmov.sel] - pcfix.x[pcfix.sel]
dy = pcmov.y[pcmov.sel] - pcfix.y[pcfix.sel]
dz = pcmov.z[pcmov.sel] - pcfix.z[pcfix.sel]
nx = pcfix.nx[pcfix.sel]
ny = pcfix.ny[pcfix.sel]
nz = pcfix.nz[pcfix.sel]
distances = [dx[i]*nx[i] + dy[i]*ny[i] + dz[i]*nz[i] for i in 1:length(pcmov.sel)]
return distances
end
function reject!(pcmov::PointCloud, pcfix::PointCloud, min_planarity, distances)
planarity = pcfix.planarity[pcfix.sel]
med = median(distances)
sigmad = mad(distances, normalize=true)
keep_distance = [abs(d-med) <= 3*sigmad for d in distances]
keep_planarity = [p > min_planarity for p in planarity]
keep = keep_distance .& keep_planarity
pcmov.sel = pcmov.sel[keep]
pcfix.sel = pcfix.sel[keep]
deleteat!(distances, .!keep)
return nothing
end
function estimate_rigid_body_transformation(x_fix, y_fix, z_fix, nx_fix, ny_fix, nz_fix,
x_mov, y_mov, z_mov)
A = hcat(-z_mov.*ny_fix + y_mov.*nz_fix,
z_mov.*nx_fix - x_mov.*nz_fix,
-y_mov.*nx_fix + x_mov.*ny_fix,
nx_fix,
ny_fix,
nz_fix)
l = nx_fix.*(x_fix-x_mov) + ny_fix.*(y_fix-y_mov) + nz_fix.*(z_fix-z_mov)
x = A\l
residuals = A*x-l
R = euler_angles_to_linearized_rotation_matrix(x[1], x[2], x[3])
t = x[4:6]
H = create_homogeneous_transformation_matrix(R, t)
return H, residuals
end
function euler_angles_to_linearized_rotation_matrix(α1, α2, α3)
dR = [ 1 -α3 α2
α3 1 -α1
-α2 α1 1]
end
function create_homogeneous_transformation_matrix(R, t)
H = [R t
zeros(1,3) 1]
end
function euler_coord_to_homogeneous_coord(XE)
no_points = size(XE, 1)
XH = [XE ones(no_points,1)]
end
function homogeneous_coord_to_euler_coord(XH)
XE = XH[:,1:3]./XH[:,4]
end
function check_convergence_criteria(distances_new, distances_old, min_change)
change(new, old) = abs((new-old)/old*100)
change_of_mean = change(mean(distances_new), mean(distances_old))
change_of_std = change(std(distances_new), std(distances_old))
return change_of_mean < min_change && change_of_std < min_change ? true : false
end
function simpleicp(X_fix::Array, X_mov::Array;
correspondences::Integer=1000,
neighbors::Integer=10,
min_planarity::Number=0.3,
max_overlap_distance::Number=Inf,
min_change::Number=3,
max_iterations::Integer=100)
size(X_fix)[2] == 3 || error(""""X_fix" must have 3 columns""")
size(X_mov)[2] == 3 || error(""""X_mov" must have 3 columns""")
correspondences >= 10 || error(""""correspondences" must be >= 10""")
min_planarity >= 0 && min_planarity < 1 || error(""""min_planarity" must be >= 0 and < 1""")
neighbors >= 2 || error(""""neighbors" must be >= 2""")
min_change > 0 || error(""""min_change" must be > 0""")
max_iterations > 0 || error(""""max_iterations" must be > 0""")
dt = @elapsed begin
@info "Create point cloud objects ..."
pcfix = PointCloud(X_fix[:,1], X_fix[:,2], X_fix[:,3])
pcmov = PointCloud(X_mov[:,1], X_mov[:,2], X_mov[:,3])
if isfinite(max_overlap_distance)
@info "Consider partial overlap of point clouds ..."
select_in_range!(pcfix, X_mov, max_overlap_distance)
if length(pcfix.sel) == 0
error(@sprintf("Point clouds do not overlap within max_overlap_distance = %.5f! ",
max_overlap_distance) * "Consider increasing the value of max_overlap_distance.")
end
end
@info "Select points for correspondences in fixed point cloud ..."
select_n_points!(pcfix, correspondences)
sel_orig = pcfix.sel
@info "Estimate normals of selected points ..."
estimate_normals!(pcfix, neighbors)
H = Matrix{Float64}(I,4,4)
residual_distances = Any[]
@info "Start iterations ..."
for i in 1:max_iterations
initial_distances = matching!(pcmov, pcfix)
reject!(pcmov, pcfix, min_planarity, initial_distances)
dH, residuals = estimate_rigid_body_transformation(
pcfix.x[pcfix.sel], pcfix.y[pcfix.sel], pcfix.z[pcfix.sel],
pcfix.nx[pcfix.sel], pcfix.ny[pcfix.sel], pcfix.nz[pcfix.sel],
pcmov.x[pcmov.sel], pcmov.y[pcmov.sel], pcmov.z[pcmov.sel])
push!(residual_distances, residuals)
transform!(pcmov, dH)
H = dH*H
pcfix.sel = sel_orig
if i > 1
if check_convergence_criteria(residual_distances[i], residual_distances[i-1],
min_change)
@info "Convergence criteria fulfilled -> stop iteration!"
break
end
end
if i == 1
@info @sprintf(" %9s | %15s | %15s | %15s", "Iteration", "correspondences",
"mean(residuals)", "std(residuals)")
@info @sprintf(" %9s | %15d | %15.4f | %15.4f", "orig:0", length(initial_distances),
mean(initial_distances), std(initial_distances))
end
@info @sprintf(" %9d | %15d | %15.4f | %15.4f", i, length(residual_distances[i]),
mean(residual_distances[i]), std(residual_distances[i]))
end
end
@info "Estimated transformation matrix H:\n" *
@sprintf("[%12.6f %12.6f %12.6f %12.6f]\n", H[1,1], H[1,2], H[1,3], H[1,4]) *
@sprintf("[%12.6f %12.6f %12.6f %12.6f]\n", H[2,1], H[2,2], H[2,3], H[2,4]) *
@sprintf("[%12.6f %12.6f %12.6f %12.6f]\n", H[3,1], H[3,2], H[3,3], H[3,4]) *
@sprintf("[%12.6f %12.6f %12.6f %12.6f]\n", H[4,1], H[4,2], H[4,3], H[4,4])
@info "Finished in " * @sprintf("%.3f", dt) * " seconds!"
X_mov_transformed = [pcmov.x pcmov.y pcmov.z]
return H, X_mov_transformed
end