-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernels.py
More file actions
344 lines (306 loc) · 10.5 KB
/
kernels.py
File metadata and controls
344 lines (306 loc) · 10.5 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import warnings
from numba.core.errors import NumbaPerformanceWarning
from numba import cuda, types
import math
import numpy as np
threads_n = 256
EARTH_RADIUS = 6371
EARTH_RADIUS_SQUARED = 40589641
CORNER_OVERLAP = 16
warnings.simplefilter('ignore', category=NumbaPerformanceWarning)
@cuda.jit
def take_values(viewshed, positions, intervisibility_mat, index):
i = cuda.grid(1)
if i < positions.shape[0] and i != index:
x, y = positions[i]
intervisibility_mat[index, i] = viewshed[x, y]
@cuda.jit
def memset_k(array, val):
if len(array.shape) >= 2:
# 2d
i, j = cuda.grid(2)
if i < array.shape[0] and j < array.shape[1]:
array[i, j] = val
else:
# 1d
i = cuda.grid(1)
if i < array.shape[0]:
array[i] = val
@cuda.jit
def sum_k(viewshed, cumulative):
i, j = cuda.grid(2)
if i < cumulative.shape[0] and j < cumulative.shape[1]:
cumulative[i, j] += viewshed[i, j]
viewshed[i, j] = 0
@cuda.jit
def viewshed_k(dsm, out, poi, max_dist, width_resol, height_resol, poi_elev, tgt_elev, poi_elev_type):
if max_dist == 0:
max_dist = 65536
p_offset = cuda.shared.array(shape=(threads_n), dtype=types.float32) # TODO: fix data type
p_id = cuda.shared.array(shape=(threads_n, 2), dtype=types.int32)
tid = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x - cuda.blockIdx.x * 2
p_offset[cuda.threadIdx.x] = 1
p_id[cuda.threadIdx.x] = (-1, -1)
x_step = np.float32(1)
y_step = np.float32(1)
# INITIALIZE DATA WRT to DIRECTION OF TRACING
if(cuda.blockIdx.y == 0):
# NORTH
if(tid >= dsm.shape[0]+CORNER_OVERLAP):
return
x_end = tid - CORNER_OVERLAP/2.0
y_end = np.float32(0)
xs = x_end - poi[0]
ys = y_end - poi[1]
x_step = abs(xs/ys)
if x_step > 1.0:
x_step = np.float32(1)
y_step = ys/xs
north_south = True
elif(cuda.blockIdx.y == 1):
# SOUTH
if(tid >= dsm.shape[0]+CORNER_OVERLAP):
return
x_end = tid - CORNER_OVERLAP/2.0
y_end = dsm.shape[1]
xs = x_end - poi[0]
ys = y_end - poi[1]
x_step = abs(xs/ys)
if x_step > 1.0:
x_step = np.float32(1)
y_step = ys/xs
north_south = True
elif(cuda.blockIdx.y == 2):
# WEST
if(tid >= dsm.shape[1]+CORNER_OVERLAP):
return
x_end = np.float32(0)
y_end = tid - CORNER_OVERLAP/2.0
xs = x_end - poi[0]
ys = y_end - poi[1]
y_step = abs(ys/xs)
if y_step > 1.0:
y_step = np.float32(1)
x_step = xs/ys
north_south = False
elif(cuda.blockIdx.y == 3):
# EAST
if(tid >= dsm.shape[1]+CORNER_OVERLAP):
return
x_end = dsm.shape[0]
y_end = tid - CORNER_OVERLAP/2.0
xs = x_end - poi[0]
ys = y_end - poi[1]
y_step = abs(ys/xs)
if y_step > 1.0:
y_step = np.float32(1)
x_step = xs/ys
north_south = False
max_tilt = np.float32(np.inf)
if(x_step < 0.0):
x_step = -x_step
if(xs < 0.0):
x_step = -x_step
if(y_step < 0.0):
y_step = -y_step
if(ys < 0.0):
y_step = -y_step
x = np.float32(0)
y = np.float32(0)
colsd = dsm.shape[0]
rowsd = dsm.shape[1]
if poi_elev_type == 0:
# normal old type (origin point + elev)
h0 = dsm[poi[0], poi[1]] + poi_elev
elif poi_elev_type == 1:
# use this to specify the height as third column in coords
h0 = poi[2] + poi_elev
step = -1
while(True):
step += 1
x = x_step * step + poi[0] + 0.5
y = y_step * step + poi[1] + 0.5
# Exit conditions
if(x < 0.5):
break
if((x+1.0) >= colsd):
break
if(y < 0.5):
break
if((y+1.0) >= rowsd):
break
# distance from poi
xd = x-poi[0]
yd = y-poi[1]
#xd *= width_resolution
# yd *= height_resolution #useless mult by 1
# 2-d distance
distance = math.sqrt(xd*xd + yd*yd)
# take neighbor point for closest trace interpolation (see section 4.5 of paper)
p1 = (int(x), int(y))
if distance < 0.01:
out[p1] = 1
continue # too short distance
if distance > max_dist:
return
if(north_south):
offset = x-int(x)
else:
offset = y-int(y)
p_offset[cuda.threadIdx.x] = offset
p_id[cuda.threadIdx.x] = p1
cuda.syncthreads()
point_consider = 1
# If the left or right sweep is more precise do not consider this one
if (((cuda.threadIdx.x < (cuda.blockDim.x-1)) and
(cuda.threadIdx.x != 0)) or tid == 0):
if(cuda.threadIdx.x != cuda.blockDim.x):
if((p_id[cuda.threadIdx.x+1, 0] == p1[0]) and
(p_id[cuda.threadIdx.x+1, 1] == p1[1]) and
(p_offset[cuda.threadIdx.x+1] <= offset)):
point_consider = 0
if(cuda.threadIdx.x != 0):
if((p_id[cuda.threadIdx.x-1, 0] == p1[0]) and
(p_id[cuda.threadIdx.x-1, 1] == p1[1]) and
(p_offset[cuda.threadIdx.x-1] < offset)):
point_consider = 0
small_dist = distance/1000
h_corr = (math.sqrt(small_dist*small_dist + EARTH_RADIUS_SQUARED) - EARTH_RADIUS)*1000
h1 = dsm[p1] - h_corr
h_diff = h0 - h1
tilt_land = (h_diff) / distance
tilt_ant = (h_diff - tgt_elev) / distance
if(tilt_ant <= max_tilt):
if point_consider:
out[p1] = 1
if(tilt_land < max_tilt):
max_tilt = tilt_land
@cuda.jit
def los_k(dsm, ordered_coordinates, out, width_resol, height_resol, poi_elev, tgt_elev):
tid_x, tid_y = cuda.grid(2)
if tid_x >= ordered_coordinates.shape[0] or \
tid_y >= ordered_coordinates.shape[0] or \
tid_x == tid_y:
return
x_step = types.float32(1.0)
y_step = types.float32(1.0)
poi = ordered_coordinates[tid_y]
tgt = ordered_coordinates[tid_x]
xs = tgt[0] - poi[0]
ys = tgt[1] - poi[1]
xs_a = abs(xs)
ys_a = abs(ys)
if(xs_a >= ys_a):
# x dominant axis
x_step = 1*math.copysign(1, xs)
y_step = ys/xs_a
dist = xs_a
else:
# y dominant axis
y_step = 1*math.copysign(1, ys)
x_step = xs/ys_a
dist = ys_a
h_poi = dsm[poi[0], poi[1]]
h_tgt = dsm[tgt[0], tgt[1]]
distance = math.sqrt(float(xs**2 + ys**2))
h_ant_corr = (math.sqrt((distance/1000)**2 + EARTH_RADIUS_SQUARED) - EARTH_RADIUS)*1000
tilt_ant = (-h_poi - poi_elev + h_tgt + tgt_elev) / distance
for j in range(1, dist):
x = x_step * j + poi[0]
y = y_step * j + poi[1]
xd = x-poi[0]
yd = y-poi[1]
# 2-d distance from poi
d_poi = math.sqrt(xd*xd + yd*yd)
# take point elevation
p1 = (int(x), int(y))
# calc earth radius correction
small_dist = d_poi/1000
h_corr = (math.sqrt(small_dist**2 + EARTH_RADIUS_SQUARED) - EARTH_RADIUS)*1000
# height of LOS
h_los = tilt_ant * d_poi + h_poi + poi_elev
if dsm[p1] > h_los:
return
out[tid_y, tid_x] = 1
@cuda.jit
def dist_k(tgt_list, out, width_resol, height_resol):
# threads are mapped to tgt_list which is an array of x, y, id
poi = tgt_list[0]
tid = cuda.grid(1)
if tid >= tgt_list.shape[0] - 1: # because in the list we also have poi in position 0
return
tgt = tgt_list[tid + 1]
if poi[2] == tgt[2]:
out[poi[2], tgt[2]] = 0
return # not testing self loop
xs = tgt[0] - poi[0]
ys = tgt[1] - poi[1]
distance = math.sqrt(float(xs**2 + ys**2))
out[poi[2], tgt[2]] = distance
@cuda.jit
def knife_k(dsm, tgt_list, out, angles_mat, width_resol, height_resol, poi_elev, tgt_elev, ple, lmb):
# threads are mapped to tgt_list which is an array of x, y, id
poi = tgt_list[0]
tid = cuda.grid(1)
if tid >= tgt_list.shape[0] - 1: # because in the list we also have poi in position 0
return
tgt = tgt_list[tid + 1]
if poi[2] == tgt[2]:
out[poi[2], tgt[2]] = 0
return # not testing self loop
x_step = types.float32(1.0)
y_step = types.float32(1.0)
xs = tgt[0] - poi[0]
xs_a = abs(xs)
ys = tgt[1] - poi[1]
ys_a = abs(ys)
if(xs_a >= ys_a):
# x dominant axis
x_step = 1*math.copysign(1, xs)
y_step = ys/xs_a
dist = xs_a
else:
# y dominant axis
y_step = 1*math.copysign(1, ys)
x_step = xs/ys_a
dist = ys_a
x = poi[0]
y = poi[1]
h_poi = dsm[poi[0], poi[1]]
h_tgt = dsm[tgt[0], tgt[1]]
distance = math.sqrt(float(xs**2 + ys**2))
h_ant_corr = (math.sqrt((distance/1000)**2 + EARTH_RADIUS_SQUARED) - EARTH_RADIUS)*1000
tilt_ant = -(h_poi + poi_elev - h_tgt - tgt_elev) / distance
knife_max1 = 0
d_poi_max1 = 0
for i in range(1, dist):
x = x_step * i + poi[0]
y = y_step * i + poi[1]
xd = x-poi[0]
yd = y-poi[1]
# 2-d distance from poi
d_poi = math.sqrt(xd*xd + yd*yd)
# take point elevation
p1 = (int(x), int(y))
# calc earth radius correction
small_dist = d_poi/1000
h_corr = (math.sqrt(small_dist**2 + EARTH_RADIUS_SQUARED) - EARTH_RADIUS)*1000
# height of LOS
h_los = tilt_ant * d_poi + h_poi + poi_elev - h_ant_corr
# radius of fresnel zone
r_fresnel = math.sqrt(d_poi * (distance - d_poi) / distance * lmb) * math.cos(tilt_ant)
# calc height of knife inside fresnel zone
knife = dsm[p1] - h_corr - h_los + r_fresnel
if knife > knife_max1:
knife_max1 = knife
d_poi_max1 = d_poi
knife1_loss = 0
knife2_loss = 0
if knife_max1 > 0.0:
v = knife_max1 * math.sqrt(2 / lmb * (1 / d_poi_max1 + 1 / (distance - d_poi_max1)))
knife1_loss = 6.9 + 10 * ple * math.log10(math.sqrt((v - 0.1)**2 + 1) + v - 0.1)
fspl = 10 * ple * math.log10(4*math.pi * distance / lmb)
val = int(fspl + knife1_loss + knife2_loss)
out[poi[2], tgt[2]] = val
angles_mat[poi[2], tgt[2], 0] = int((math.degrees(math.atan2(float(ys), float(xs))) + 180)*100) # yaw/az
angles_mat[poi[2], tgt[2], 1] = int((math.degrees(math.atan(tilt_ant)) + 180)*100) # pitch/elev #multiply by 100 and use int16 to save memory