-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grids.py
446 lines (312 loc) · 11.5 KB
/
Grids.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from abc import ABC, abstractmethod
import numpy as np
import pyvista as pv
import geopandas as gpd
from shapely.geometry import MultiLineString
class BaseGrid(ABC):
"""
Abstract class for grids:
1. TriGrid
2. QuadGrid
3. HexGrid
4. GridCollection
"""
def __init__(self, radius: float, bounds, n_sides: int, orientation: str):
"""
Init the entity.
"""
self._radius = radius
if isinstance(bounds, np.ndarray):
self._boundary = bounds
elif isinstance(bounds, str):
boundary_shp = gpd.read_file(bounds)['geometry'][0]
self._boundary = np.array(boundary_shp.bounds)
self._n_sides = n_sides
self._orientation = orientation
self._centers = None
self._nx = self._ny = 0
self._grid_object = self.gen_grid()
@property
def radius(self) -> float:
"""
Get the radius of the circumscribed circle.
:return: float
"""
return self._radius
def side(self) -> float:
"""
Get the length of the side of a cell
:return: float
"""
theta_rad = self.theta(rad_only=True)
side = 2*self.radius*np.sin(theta_rad/2)
return side
def height(self) -> float:
"""
Get the distance between the center of the cell and a side
:return: float
"""
theta_rad = self.theta(rad_only=True)
height = self.radius * np.cos(theta_rad/2)
return height
@property
def bounds(self) -> np.ndarray:
"""
Get the boundaries of the grid (xmin, ymin, xmax, ymax).
:return: numpy array
"""
return self._boundary
@property
def boundary_object(self) -> pv.PolyData:
"""
Get the representation of the shp boundary.
:return: Polydata object
"""
return self._boundary_object
@property
def number_of_sides(self) -> int:
"""
Get the number of sides of the cell of the grid
:return: integer
"""
return self._n_sides
@property
def cell_orientation(self) -> str:
"""
Get the orientation for hex and square grids ("up" or "flat")
:return: string
"""
return self._orientation
@property
def cell_type(self) -> str:
"""
Get the name of the cell of the grid.
+ 3: Triangle
+ 4:
+ Diamond if orientation is up
+ Square if orientation is flat
+ 6:
+ Hexagon if orientation is up
+ Flaxagon if orientation is down
:return: string
"""
if self.number_of_sides == 3:
return "triangle"
elif self.number_of_sides == 4:
if self.cell_orientation == 'up':
return "diamond"
else:
return "square"
else:
if self.cell_orientation == 'up':
return "hexagon"
else:
return "flaxagon"
@property
def spacing(self) -> list:
"""
Get the spacing between center points of the grid [nx, ny]
:return: list
"""
return [self._nx, self._ny]
@property
def centers(self) -> np.ndarray:
"""
Get the grid cell center coordinates
:return: numpy array
"""
centers = self._centers[:, :-1]
return centers.reshape(-1, 3)
@abstractmethod
def gen_grid(self) -> pv.PolyData:
"""
Generate the grid given the different options. A pyvista polydata object is created with m cells of sides n_sides
of the grid.
Each grid has a different way to generate the grid so this needs to be implemented in the different grid classes
:return: pyvista Polydata
"""
pass
@abstractmethod
def area(self) -> float:
"""
Calculate the area of the given cell. Each grid will have its own area calculation
:return: float
"""
pass
def grid(self) -> pv.PolyData:
"""
Get the grid pyvista entity.
:return: pyvista PolyData
"""
return self._grid_object
def theta(self, rad_only: bool = False) -> list:
"""
Get a list of angular values [degree, radians] of angle between two points in the grid
:return: list
"""
theta = 360 / self.number_of_sides
theta_rad = np.deg2rad(theta)
if rad_only:
return theta_rad
else:
return [theta, theta_rad]
def matrix(self, angles: np.ndarray) -> np.ndarray:
n_angles = len(angles)
r = self.radius
trans_matrix = np.zeros((n_angles, 4, 4))
for i, angle in enumerate(angles):
trans_matrix[i] = np.array([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[r * np.cos(angle), r * np.sin(angle), 0, 1]
])
return trans_matrix
class TriGrid(BaseGrid):
"""
Class for tri grid specific
"""
def __init__(self, radius: float, bounds, n_sides: int = 6, orientation: str= 'up'):
super().__init__(radius, bounds, n_sides, orientation)
def gen_grid(self) -> pv.PolyData:
theta, theta_rad = self.theta()
r = self.radius
self.bounds[:2] = self.bounds[:2][::-1]
self.bounds[2:] = self.bounds[2:][::-1]
angles = np.deg2rad(np.arange((90 - theta), 360, theta))
self._nx = np.sin(theta_rad) * 2 * r
self._ny = r + np.absolute(np.cos(theta_rad)) * r
n_angles = len(angles)
trans_matrix = self.matrix(angles)
x_min, y_min, x_max, y_max = self.bounds
x = np.arange(x_min, x_max, self._nx)
y = np.arange(y_min, y_max, self._ny)
xv, yv = np.meshgrid(x, y)
n_col, n_rows = np.shape(yv)
zv = np.zeros_like(xv)
ones = np.ones_like(xv)
xv[::2, :] -= np.sin(theta_rad) * r
self._centers = np.hstack((xv.reshape(-1, 1), yv.reshape(-1, 1), zv.reshape(-1, 1), ones.reshape(-1, 1)))
hex_grid = self._centers.dot(trans_matrix)[:, :, :-1]
n_points = np.shape(hex_grid)[0] * np.shape(hex_grid)[1]
hex_grid = hex_grid.reshape(-1, 3)
new_hexgrid = hex_grid.reshape(n_col, n_rows, 18)
new_hexgrid[:, 0][::2][:, 6:12] = -1
new_hexgrid[:, -1][1::2][:, :3] = -1
new_hexgrid[:, -1][1::2][:, 15:] = -1
new_hexgrid = new_hexgrid.reshape(-1, 3)
rem_index = np.where(np.all(new_hexgrid == -1, axis=1) == 1)
vert_idx = np.arange(0, n_points).reshape(-1, 6)
vert_idx = np.roll(np.repeat(vert_idx, 2, axis=1), -1, axis=1).reshape(-1, 2)
zeros_vert = np.zeros((n_points, 1), dtype=int)
part1 = np.append(zeros_vert, vert_idx, axis=1).flatten()
center_grid = np.append(new_hexgrid, self.centers, axis=0)
centers_idx = np.repeat(np.arange(n_points, n_points + len(self.centers)), 6).reshape(-1, 1)
zeros_centers = np.zeros((len(centers_idx), 2), dtype=int)
part2 = np.append(centers_idx, zeros_centers, axis=1).flatten()
tri_conn = (part1 + part2).reshape(-1, 3)
tri_conn = np.insert(tri_conn, np.arange(0, len(tri_conn.flatten()), 3), 3)
vtk_obj = pv.PolyData(center_grid, faces=tri_conn)
vtk_obj.remove_points(rem_index[0], inplace=True)
vtk_obj.rotate_z(angle=90, point=vtk_obj.center, inplace=True)
vtk_obj.flip_x(inplace=True)
vtk_obj.cell_data['cellid'] = np.arange(vtk_obj.n_cells)
return vtk_obj
def area(self) -> float:
"""
The area of the hexagon
:return: float
"""
side = self.side()
height = self.height()
area = side*height/2
return area
class QuadGrid(BaseGrid):
"""
Class for quad grid specific
"""
def __init__(self, radius: float, bounds, n_sides: int = 4, orientation: str= 'up'):
super().__init__(radius, bounds, n_sides, orientation)
def gen_grid(self) -> pv.PolyData:
theta, theta_rad = self.theta()
r = self.radius
if self.cell_orientation == 'up':
angles = np.deg2rad(np.arange((90 - theta), 360, theta))
self._nx = np.sin(theta_rad) * 2 * r
self._ny = r + np.absolute(np.cos(theta_rad)) * r
else:
angles = np.deg2rad(np.arange(45, 360, theta))
self._nx = self._ny = 2*r/np.sqrt(2)
n_angles = len(angles)
trans_matrix = self.matrix(angles)
x_min, y_min, x_max, y_max = self.bounds
x = np.arange(x_min, x_max, self._nx)
y = np.arange(y_min, y_max, self._ny)
xv, yv = np.meshgrid(x, y)
n_col, n_rows = np.shape(yv)
zv = np.zeros_like(xv)
ones = np.ones_like(xv)
if self.cell_orientation == 'up':
xv[::2, :] -= np.sin(theta_rad) * r
self._centers = np.hstack((xv.reshape(-1, 1), yv.reshape(-1, 1), zv.reshape(-1, 1), ones.reshape(-1, 1)))
hex_grid = self._centers.dot(trans_matrix)[:, :, :-1]
n_points = np.shape(hex_grid)[0] * np.shape(hex_grid)[1]
hex_grid = hex_grid.reshape(-1, 3)
conn = np.insert(np.arange(0, n_points), np.arange(0, n_points, n_angles), n_angles)
vtk_obj = pv.PolyData(hex_grid, faces=conn)
vtk_obj.cell_data['cellid'] = np.arange(vtk_obj.n_cells)
return vtk_obj
def area(self) -> float:
"""
The area of the hexagon
:return: float
"""
side = self.side()
area = np.power(side, 2)
return area
class HexGrid(BaseGrid):
"""
Class for hex grid specific
"""
def __init__(self, radius: float, bounds, n_sides: int = 6, orientation: str= 'up'):
super().__init__(radius, bounds, n_sides, orientation)
def gen_grid(self) -> pv.PolyData:
theta, theta_rad = self.theta()
r = self.radius
if self.cell_orientation == 'up':
angles = np.deg2rad(np.arange((90 - theta), 360, theta))
self._nx = np.sin(theta_rad) * 2 * r
self._ny = r + np.absolute(np.cos(theta_rad)) * r
else:
angles = np.deg2rad(np.arange(0, 360, theta))
self._nx = r + np.absolute(np.cos(theta_rad))*r
self._ny = np.sin(theta_rad)*2*r
n_angles = len(angles)
trans_matrix = self.matrix(angles)
x_min, y_min, x_max, y_max = self.bounds
x = np.arange(x_min, x_max, self._nx)
y = np.arange(y_min, y_max, self._ny)
xv, yv = np.meshgrid(x, y)
n_col, n_rows = np.shape(yv)
zv = np.zeros_like(xv)
ones = np.ones_like(xv)
if self.cell_orientation == 'up':
xv[::2, :] -= np.sin(theta_rad) * r
else:
yv[:, ::2] -= np.sin(theta_rad) * r
self._centers = np.hstack((xv.reshape(-1, 1), yv.reshape(-1, 1), zv.reshape(-1, 1), ones.reshape(-1, 1)))
hex_grid = self._centers.dot(trans_matrix)[:, :, :-1]
n_points = np.shape(hex_grid)[0] * np.shape(hex_grid)[1]
hex_grid = hex_grid.reshape(-1, 3)
conn = np.insert(np.arange(0, n_points), np.arange(0, n_points, n_angles), n_angles)
vtk_obj = pv.PolyData(hex_grid, faces=conn)
vtk_obj.cell_data['cellid'] = np.arange(vtk_obj.n_cells)
return vtk_obj
def area(self) -> float:
"""
The area of the hexagon
:return: float
"""
side = self.side()
area = np.power(side, 2)*3*np.sqrt(3)/2
return area