-
Notifications
You must be signed in to change notification settings - Fork 0
/
rFunctions.R
130 lines (115 loc) · 4.4 KB
/
rFunctions.R
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
Rcpp::sourceCpp('cloud2grid.cpp')
Rcpp::sourceCpp('eigenFeatures.cpp')
cloud2grid = function(xyz, vars=NULL, rx, ry, rz, n_threads=1){
x_steps=(abs(max(xyz[,1])-min(xyz[,1]))%/%rx)+1
y_steps=(abs(max(xyz[,2])-min(xyz[,2]))%/%ry)+1
z_steps=(abs(max(xyz[,3])-min(xyz[,3]))%/%rz)+1
x_grid = seq(min(xyz[,1])-0.01,max(xyz[,1])+0.01,length=x_steps)
y_grid = seq(min(xyz[,2])-0.01,max(xyz[,2])+0.01,length=y_steps)
z_grid = seq(min(xyz[,3])-0.01,max(xyz[,3])+0.01,length=z_steps)
resx=x_grid[3]-x_grid[2]
resy=y_grid[3]-y_grid[2]
resz=z_grid[3]-z_grid[2]
minx=min(xyz[,1])
miny=min(xyz[,2])
minz=min(xyz[,3])
if(!is.null(vars)){
vars=as.matrix(vars)
cols = ncol(vars)
}else{
cols=1
vars = matrix(data=1,ncol=1,nrow=nrow(xyz))
}
out=matrix(data=0,ncol=cols+1,nrow=(x_steps*y_steps*z_steps))
cloud2grid_(as.matrix(xyz),
vars,
out,
minx,miny,minz,
resx,resy,resz,
x_grid,y_grid,z_grid,
x_steps,y_steps,z_steps,
n_threads)
list(weights = out[,1],
vars=out[,2:(ncol(vars)+1)],
steps=list(x=x_steps,
y=y_steps,
z=z_steps),
res=list(x=resx,
y=resy,
z=resz),
grid=list(x=x_grid,
y=y_grid,
z=z_grid),
min=list(x=minx,
y=miny,
z=minz))
}
eigenFeatures = function(weight_grid,kernel,n_threads=1){
out = list()
ind = weight_grid$weights>0
for(i in seq_along(kernel)){
if(i==1){
out[[i]] = matrix(data=0,
ncol=13,
nrow=length(weight_grid$weights))
colnames(out[[i]])=c('x','y','z',
paste('linearity',as.character(kernel[i]),sep='_'),
paste('planarity',as.character(kernel[i]),sep='_'),
paste('scattering',as.character(kernel[i]),sep='_'),
paste('surface_variation',as.character(kernel[i]),sep='_'),
paste('omnivariance',as.character(kernel[i]),sep='_'),
paste('anisotropy',as.character(kernel[i]),sep='_'),
paste('sum',as.character(kernel[i]),sep='_'),
paste('Xn',as.character(kernel[i]),sep='_'),
paste('Yn',as.character(kernel[i]),sep='_'),
paste('Zn',as.character(kernel[i]),sep='_'))
}else{
out[[i]] = matrix(data=0,
ncol=10,
nrow=length(weight_grid$weights))
colnames(out[[i]])=c(paste('linearity',as.character(kernel[i]),sep='_'),
paste('planarity',as.character(kernel[i]),sep='_'),
paste('scattering',as.character(kernel[i]),sep='_'),
paste('surface_variation',as.character(kernel[i]),sep='_'),
paste('omnivariance',as.character(kernel[i]),sep='_'),
paste('anisotropy',as.character(kernel[i]),sep='_'),
paste('sum',as.character(kernel[i]),sep='_'),
paste('Xn',as.character(kernel[i]),sep='_'),
paste('Yn',as.character(kernel[i]),sep='_'),
paste('Zn',as.character(kernel[i]),sep='_'))
}
}
for(i in seq_along(kernel)){
if(i==1){
eigenFeatures_(weight_grid$weights,
out[[i]],
weight_grid$steps$x,
weight_grid$steps$y,
weight_grid$steps$z,
weight_grid$res$x,
weight_grid$res$y,
weight_grid$res$z,
kernel[i],
n_threads,
new_coords = T)
}else{
eigenFeatures_(weight_grid$weights,
out[[i]],
weight_grid$steps$x,
weight_grid$steps$y,
weight_grid$steps$z,
weight_grid$res$x,
weight_grid$res$y,
weight_grid$res$z,
kernel[i],
n_threads,
new_coords = F)
}
}
out = do.call(cbind, out)
out=out[ind,]
out[,1]=out[,1]+weight_grid$min$x
out[,2]=out[,2]+weight_grid$min$y
out[,3]=out[,3]+weight_grid$min$z
out
}