-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid_analytics.py
More file actions
329 lines (245 loc) · 8.59 KB
/
grid_analytics.py
File metadata and controls
329 lines (245 loc) · 8.59 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
import numpy as np
from .tools import Isovist
from .tools import Shortestpath
from .tools import get_neighbors2D
from .tools import get_neighbors3D
from .tools import calculate_distance_from_solids2D
from .tools import calculate_voronois_from_solids2D
from .tools import analyse_shadow_Bresenham_sorted
__all__ = [
'analyse_neighbours2D',
'analyse_neighbours3D',
'analyse_isovist_map2D',
'analyse_isovist2D',
'analyse_shortestpath2D',
'analyse_centrality2D',
'analyse_shadow',
'analyse_distances2D',
'analyse_voronoi2D'
]
def analyse_neighbours2D(array):
"""
Returns the amount of 2d neighbours per cell for any 2D or 3D array
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with 0 for void cells, 1 for solid cells
Returns
-------
numpy array
2D or 3D numpy array of values representing how many solid are seen per cell.
Examples
--------
>>>
>>>
>>>
"""
if array.ndim == 2:
values = np.full(array.shape, 0)
for x, y in np.ndindex(array.shape):
# only evaluate solid voxels
if array[x, y] > 0:
values[x][y] = get_neighbors2D(array, x, y)
return values
elif array.ndim == 3:
values = np.full(array.shape, 0)
for x, y, z in np.ndindex(array.shape):
# only evaluate solid voxels
if array[x, y, z] > 0:
array_slice = array[:, :, z].reshape(array.shape[:2])
values[x][y][z] = get_neighbors2D(array_slice, x, y)
return values
else:
raise Exception('array has to be 2D or 3D!!')
def analyse_neighbours3D(array):
if array.ndim == 2:
values = np.full(array.shape, 0)
for x, y in np.ndindex(array.shape):
# only evaluate solid voxels
if array[x, y] > 0:
values[x][y] = get_neighbors2D(array, x, y)
return values
elif array.ndim == 3:
values = np.full(array.shape, 0)
for x, y, z in np.ndindex(array.shape):
# only evaluate solid voxels
if array[x, y, z] > 0:
values[x][y][z] = get_neighbors3D(array, x, y, z)
return values
else:
raise Exception('array has to be 2D or 3D!!')
def analyse_isovist_map2D(array, radius=None, mode='void'):
"""
Analyses 2D visibility for any numpy array >= 2 Dimensions.
If 3D, XY layers will be analysed
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with 0 for void cells, 1 for solid cells
mode: string
string 'void' or 'solid'. 'void' returns isovist map of all void cells.
'solid' returns isovist map of all solid cells
Returns
-------
numpy array
2D or 3D numpy array of values representing how many cells are seen per cell.
Examples
--------
>>> import numpy as np
>>> array = np.random.randint(2, size=(2, 4))
>>> isovist_map = analyse_isovist_map2D(array, mode='solid')
"""
if array.ndim == 2:
return _analyse_isovist_map_xy(array, radius, mode)
elif array.ndim == 3:
values = np.full(array.shape, 0)
for z in range(values.shape[2]):
values[:, :, z] = _analyse_isovist_map_xy(array[:, :, z], radius, mode)
return values
else:
raise Exception('array has to be 2D or 3D!!')
def _analyse_isovist_map_xy(array, radius=None, mode='void'):
isovist = Isovist(array * -1, radius)
if mode == 'void':
return isovist.isovist_map(format=1)
elif mode == 'solid':
return isovist.isovist_map_collision(format=1)
else:
return
def analyse_isovist2D(array, radius=None, view_point=[0,0]):
""" Analyses 2D visibility for any numpy array >= 2 Dimensions.
Based on a given viewpoint.
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with 0 for void cells, 1 for solid cells
view_point: list or tuple
the index of the viewpoint in the array.
It needs to be inside of the array and have same dimension with the array
Returns
-------
numpy ndarray
2D or 3D numpy array with 1 for visible cells, 0 for invisible cells, -1 for solid cells.
"""
if array.ndim == 2:
return _analyse_isovist_map_xy(array, radius, view_point)
elif array.ndim == 3:
raise NotImplementedError
else:
raise Exception('array has to be 2D or 3D!!')
def _analyse_isovist_xy(array, radius, view_point):
isovist = Isovist(array * -1, radius)
return isovist.isovist_from_point(view_point, format=1)
def analyse_shortestpath2D(array, sp, ep):
""" Analyses the shortest path in a 2D numpy array.
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with values of 0 and 1
sp: list or tuple
the index of the starting point in the array.
It needs to be inside of the array and have same dimension with the array
ep: list or tuple
the index of the ending point in the array.
It needs to be inside of the array and have same dimension with the array
Returns
-------
numpy ndarray
2D or 3D numpy array with 1 for path cells, 0 for the rest of void cells, -1 for solid cells.
"""
if array.ndim == 2:
return _analyse_shortestpath_xy(array, sp, ep)
elif array.ndim == 3:
raise NotImplementedError
else:
raise Exception('array has to be 2D or 3D!!')
def _analyse_shortestpath_xy(array, sp, ep):
shortest_path = Shortestpath(array * -1)
return shortest_path.get_shortest_path(sp, ep, format=1)
def analyse_centrality2D(array):
"""
Returns centrality map
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with values of 0 and 1
Returns
-------
numpy ndarray:
numpy array with centrality percentage for each cell
"""
if array.ndim == 2:
return _analyse_centrality_xy(array)
elif array.ndim == 3:
raise NotImplementedError
else:
raise Exception('array has to be 2D or 3D!!')
def _analyse_centrality_xy(array):
shortest_path = Shortestpath(array * -1)
return shortest_path.get_centrality(format=1)
def analyse_shadow(array, light_vectors):
"""Analyses shadow for any 3D array
Parameters
----------
array: numpy ndarray
2D or 3D numpy array with values of 0 and 1
light_vectors: list of vectors(tuple of 3 float)
vectors represent light direction
Returns
-------
numpy ndarray:
numpy array with 1 as shadow, 0 as not in shadow
"""
shadow_map = np.zeros(array.shape, dtype=int)
for vec in light_vectors:
light = np.array(vec, dtype=np.float)
shadow_map += analyse_shadow_Bresenham_sorted(array, light)
return shadow_map
def analyse_distances2D(array):
"""
Returns the distances of void cells to their closest solid cells
Parameters
----------
array: numpy ndarray
2D or 3d numpy array with 0 for void cells, and >1 for solid cells
Returns
-------
numpy array
2D or 3d numpy array of values representing the index of a solid cell which is closest to a void
"""
if array.ndim == 2:
values = calculate_distance_from_solids2D(array)
return values
elif array.ndim == 3:
values = np.full(array.shape, 0)
for z in range(values.shape[2]):
array_slice = array[:, :, z].reshape(array.shape[:2])
values[:, :, z] = calculate_distance_from_solids2D(array_slice)
return values
else:
raise Exception('array has to be 2D or 3D!!')
def analyse_voronoi2D(array):
"""
Returns the indices of the solid cell which is closer to every void cell
Parameters
----------
array: numpy ndarray
2D or 3d numpy array with 0 for void cells, and >1 for solid cells
Returns
-------
numpy array
2D or 3d numpy array of values representing the index of a solid cell which is closer every void cell
"""
if array.ndim == 2:
values = calculate_voronois_from_solids2D(array)
return values
elif array.ndim == 3:
values = np.full(array.shape, 0)
for z in range(values.shape[2]):
array_slice = array[:, :, z].reshape(array.shape[:2])
values[:, :, z] = calculate_voronois_from_solids2D(array_slice)
return values
else:
raise Exception('array has to be 2D or 3D!!')
if __name__ == '__main__':
pass