|
|
|
@ -4,19 +4,17 @@ Drawing-related methods for Grid class
|
|
|
|
|
from typing import List, Optional, Union, Sequence, Callable
|
|
|
|
|
|
|
|
|
|
import numpy # type: ignore
|
|
|
|
|
from numpy import diff, floor, ceil, zeros, hstack, newaxis
|
|
|
|
|
|
|
|
|
|
from float_raster import raster
|
|
|
|
|
|
|
|
|
|
from . import GridError, Direction
|
|
|
|
|
from ._helpers import is_scalar
|
|
|
|
|
from . import GridError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
eps_callable_t = Callable[[numpy.ndarray, numpy.ndarray, numpy.ndarray], numpy.ndarray]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_polygons(self,
|
|
|
|
|
surface_normal: Union[Direction, int],
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
surface_normal: int,
|
|
|
|
|
center: numpy.ndarray,
|
|
|
|
|
polygons: Sequence[numpy.ndarray],
|
|
|
|
|
thickness: float,
|
|
|
|
@ -26,8 +24,8 @@ def draw_polygons(self,
|
|
|
|
|
Draw polygons on an axis-aligned plane.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Can be a `Direction` or
|
|
|
|
|
integer in `range(3)`
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Integer in `range(3)`.
|
|
|
|
|
center: 3-element ndarray or list specifying an offset applied to all the polygons
|
|
|
|
|
polygons: List of Nx2 or Nx3 ndarrays, each specifying the vertices of a polygon
|
|
|
|
|
(non-closed, clockwise). If Nx3, the surface_normal coordinate is ignored. Each
|
|
|
|
@ -41,11 +39,6 @@ def draw_polygons(self,
|
|
|
|
|
Raises:
|
|
|
|
|
GridError
|
|
|
|
|
"""
|
|
|
|
|
# Turn surface_normal into its integer representation
|
|
|
|
|
if isinstance(surface_normal, Direction):
|
|
|
|
|
surface_normal = surface_normal.value
|
|
|
|
|
assert(isinstance(surface_normal, int))
|
|
|
|
|
|
|
|
|
|
if surface_normal not in range(3):
|
|
|
|
|
raise GridError('Invalid surface_normal direction')
|
|
|
|
|
|
|
|
|
@ -55,7 +48,7 @@ def draw_polygons(self,
|
|
|
|
|
surface = numpy.delete(range(3), surface_normal)
|
|
|
|
|
|
|
|
|
|
for i, polygon in enumerate(polygons):
|
|
|
|
|
malformed = 'Malformed polygon: (%i)' % i
|
|
|
|
|
malformed = f'Malformed polygon: ({i})'
|
|
|
|
|
if polygon.shape[1] not in (2, 3):
|
|
|
|
|
raise GridError(malformed + 'must be a Nx2 or Nx3 ndarray')
|
|
|
|
|
if polygon.shape[1] == 3:
|
|
|
|
@ -64,12 +57,12 @@ def draw_polygons(self,
|
|
|
|
|
if not polygon.shape[0] > 2:
|
|
|
|
|
raise GridError(malformed + 'must consist of more than 2 points')
|
|
|
|
|
if polygon.ndim > 2 and not numpy.unique(polygon[:, surface_normal]).size == 1:
|
|
|
|
|
raise GridError(malformed + 'must be in plane with surface normal %s'
|
|
|
|
|
% 'xyz'[surface_normal])
|
|
|
|
|
raise GridError(malformed + 'must be in plane with surface normal '
|
|
|
|
|
+ 'xyz'[surface_normal])
|
|
|
|
|
|
|
|
|
|
# Broadcast eps where necessary
|
|
|
|
|
if is_scalar(eps):
|
|
|
|
|
eps = [eps] * len(self.grids)
|
|
|
|
|
if numpy.size(eps) == 1:
|
|
|
|
|
eps = [eps] * len(cell_data)
|
|
|
|
|
elif isinstance(eps, numpy.ndarray):
|
|
|
|
|
raise GridError('ndarray not supported for eps')
|
|
|
|
|
|
|
|
|
@ -91,8 +84,8 @@ def draw_polygons(self,
|
|
|
|
|
s_max = self.shifts.max(axis=0)
|
|
|
|
|
bdi_min = self.pos2ind(bd_min + s_min, None, round_ind=False, check_bounds=False) - buf
|
|
|
|
|
bdi_max = self.pos2ind(bd_max + s_max, None, round_ind=False, check_bounds=False) + buf
|
|
|
|
|
bdi_min = numpy.maximum(floor(bdi_min), 0).astype(int)
|
|
|
|
|
bdi_max = numpy.minimum(ceil(bdi_max), self.shape - 1).astype(int)
|
|
|
|
|
bdi_min = numpy.maximum(numpy.floor(bdi_min), 0).astype(int)
|
|
|
|
|
bdi_max = numpy.minimum(numpy.ceil(bdi_max), self.shape - 1).astype(int)
|
|
|
|
|
|
|
|
|
|
# 3) Adjust polygons for center
|
|
|
|
|
polygons = [poly + center[surface] for poly in polygons]
|
|
|
|
@ -103,7 +96,7 @@ def draw_polygons(self,
|
|
|
|
|
return numpy.insert(v_2d, surface_normal, (val,))
|
|
|
|
|
|
|
|
|
|
# iterate over grids
|
|
|
|
|
for i, grid in enumerate(self.grids):
|
|
|
|
|
for i, grid in enumerate(cell_data):
|
|
|
|
|
# ## Evaluate or expand eps[i]
|
|
|
|
|
if callable(eps[i]):
|
|
|
|
|
# meshgrid over the (shifted) domain
|
|
|
|
@ -113,14 +106,14 @@ def draw_polygons(self,
|
|
|
|
|
# evaluate on the meshgrid
|
|
|
|
|
eps_i = eps[i](x0, y0, z0)
|
|
|
|
|
if not numpy.isfinite(eps_i).all():
|
|
|
|
|
raise GridError('Non-finite values in eps[%u]' % i)
|
|
|
|
|
elif not is_scalar(eps[i]):
|
|
|
|
|
raise GridError('Unsupported eps[{}]: {}'.format(i, type(eps[i])))
|
|
|
|
|
raise GridError(f'Non-finite values in eps[{i}]')
|
|
|
|
|
elif numpy.size(eps[i]) != 1:
|
|
|
|
|
raise GridError(f'Unsupported eps[{i}]: {type(eps[i])}')
|
|
|
|
|
else:
|
|
|
|
|
# eps[i] is scalar non-callable
|
|
|
|
|
eps_i = eps[i]
|
|
|
|
|
|
|
|
|
|
w_xy = zeros((bdi_max - bdi_min + 1)[surface].astype(int))
|
|
|
|
|
w_xy = numpy.zeros((bdi_max - bdi_min + 1)[surface].astype(int))
|
|
|
|
|
|
|
|
|
|
# Draw each polygon separately
|
|
|
|
|
for polygon in polygons:
|
|
|
|
@ -182,15 +175,16 @@ def draw_polygons(self,
|
|
|
|
|
w_z[zi_bot] = zi_top_f - zi_bot_f
|
|
|
|
|
|
|
|
|
|
# 3) Generate total weight function
|
|
|
|
|
w = (w_xy[:, :, newaxis] * w_z).transpose(numpy.insert([0, 1], surface_normal, (2,)))
|
|
|
|
|
w = (w_xy[:, :, None] * w_z).transpose(numpy.insert([0, 1], surface_normal, (2,)))
|
|
|
|
|
|
|
|
|
|
# ## Modify the grid
|
|
|
|
|
g_slice = (i,) + tuple(numpy.s_[bdi_min[a]:bdi_max[a] + 1] for a in range(3))
|
|
|
|
|
self.grids[g_slice] = (1 - w) * self.grids[g_slice] + w * eps_i
|
|
|
|
|
cell_data[g_slice] = (1 - w) * cell_data[g_slice] + w * eps_i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_polygon(self,
|
|
|
|
|
surface_normal: Union[Direction, int],
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
surface_normal: int,
|
|
|
|
|
center: numpy.ndarray,
|
|
|
|
|
polygon: numpy.ndarray,
|
|
|
|
|
thickness: float,
|
|
|
|
@ -200,20 +194,21 @@ def draw_polygon(self,
|
|
|
|
|
Draw a polygon on an axis-aligned plane.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Can be a Direction or
|
|
|
|
|
integer in range(3)
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Integer in `range(3)`.
|
|
|
|
|
center: 3-element ndarray or list specifying an offset applied to the polygon
|
|
|
|
|
polygon: Nx2 or Nx3 ndarray specifying the vertices of a polygon (non-closed,
|
|
|
|
|
clockwise). If Nx3, the surface_normal coordinate is ignored. Must have at
|
|
|
|
|
clockwise). If Nx3, the `surface_normal` coordinate is ignored. Must have at
|
|
|
|
|
least 3 vertices.
|
|
|
|
|
thickness: Thickness of the layer to draw
|
|
|
|
|
eps: Value to draw with ('epsilon'). See `draw_polygons()` for details.
|
|
|
|
|
"""
|
|
|
|
|
self.draw_polygons(surface_normal, center, [polygon], thickness, eps)
|
|
|
|
|
self.draw_polygons(cell_data, surface_normal, center, [polygon], thickness, eps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_slab(self,
|
|
|
|
|
surface_normal: Union[Direction, int],
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
surface_normal: int,
|
|
|
|
|
center: numpy.ndarray,
|
|
|
|
|
thickness: float,
|
|
|
|
|
eps: Union[List[Union[float, eps_callable_t]], float, eps_callable_t],
|
|
|
|
@ -222,24 +217,22 @@ def draw_slab(self,
|
|
|
|
|
Draw an axis-aligned infinite slab.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Can be a `Direction` or
|
|
|
|
|
integer in `range(3)`
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Integer in `range(3)`.
|
|
|
|
|
center: Surface_normal coordinate at the center of the slab
|
|
|
|
|
thickness: Thickness of the layer to draw
|
|
|
|
|
eps: Value to draw with ('epsilon'). See `draw_polygons()` for details.
|
|
|
|
|
"""
|
|
|
|
|
# Turn surface_normal into its integer representation
|
|
|
|
|
if isinstance(surface_normal, Direction):
|
|
|
|
|
surface_normal = surface_normal.value
|
|
|
|
|
if surface_normal not in range(3):
|
|
|
|
|
raise GridError('Invalid surface_normal direction')
|
|
|
|
|
|
|
|
|
|
if not is_scalar(center):
|
|
|
|
|
if numpy.size(center) != 1:
|
|
|
|
|
center = numpy.squeeze(center)
|
|
|
|
|
if len(center) == 3:
|
|
|
|
|
center = center[surface_normal]
|
|
|
|
|
else:
|
|
|
|
|
raise GridError('Bad center: {}'.format(center))
|
|
|
|
|
raise GridError(f'Bad center: {center}')
|
|
|
|
|
|
|
|
|
|
# Find center of slab
|
|
|
|
|
center_shift = self.center
|
|
|
|
@ -260,10 +253,11 @@ def draw_slab(self,
|
|
|
|
|
[xyz_max[0], xyz_min[1]],
|
|
|
|
|
[xyz_min[0], xyz_min[1]]], dtype=float)
|
|
|
|
|
|
|
|
|
|
self.draw_polygon(surface_normal, center_shift, p, thickness, eps)
|
|
|
|
|
self.draw_polygon(cell_data, surface_normal, center_shift, p, thickness, eps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_cuboid(self,
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
center: numpy.ndarray,
|
|
|
|
|
dimensions: numpy.ndarray,
|
|
|
|
|
eps: Union[List[Union[float, eps_callable_t]], float, eps_callable_t],
|
|
|
|
@ -272,6 +266,7 @@ def draw_cuboid(self,
|
|
|
|
|
Draw an axis-aligned cuboid
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
center: 3-element ndarray or list specifying the cuboid's center
|
|
|
|
|
dimensions: 3-element list or ndarray containing the x, y, and z edge-to-edge
|
|
|
|
|
sizes of the cuboid
|
|
|
|
@ -282,11 +277,12 @@ def draw_cuboid(self,
|
|
|
|
|
[+dimensions[0], -dimensions[1]],
|
|
|
|
|
[-dimensions[0], -dimensions[1]]], dtype=float) / 2.0
|
|
|
|
|
thickness = dimensions[2]
|
|
|
|
|
self.draw_polygon(Direction.z, center, p, thickness, eps)
|
|
|
|
|
self.draw_polygon(cell_data, 2, center, p, thickness, eps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_cylinder(self,
|
|
|
|
|
surface_normal: Union[Direction, int],
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
surface_normal: int,
|
|
|
|
|
center: numpy.ndarray,
|
|
|
|
|
radius: float,
|
|
|
|
|
thickness: float,
|
|
|
|
@ -297,8 +293,8 @@ def draw_cylinder(self,
|
|
|
|
|
Draw an axis-aligned cylinder. Approximated by a num_points-gon
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Can be a `Direction` or
|
|
|
|
|
integer in `range(3)`
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
surface_normal: Axis normal to the plane we're drawing on. Integer in `range(3)`.
|
|
|
|
|
center: 3-element ndarray or list specifying the cylinder's center
|
|
|
|
|
radius: cylinder radius
|
|
|
|
|
thickness: Thickness of the layer to draw
|
|
|
|
@ -308,13 +304,14 @@ def draw_cylinder(self,
|
|
|
|
|
theta = numpy.linspace(0, 2*numpy.pi, num_points, endpoint=False)
|
|
|
|
|
x = radius * numpy.sin(theta)
|
|
|
|
|
y = radius * numpy.cos(theta)
|
|
|
|
|
polygon = hstack((x[:, newaxis], y[:, newaxis]))
|
|
|
|
|
self.draw_polygon(surface_normal, center, polygon, thickness, eps)
|
|
|
|
|
polygon = numpy.hstack((x[:, None], y[:, None]))
|
|
|
|
|
self.draw_polygon(cell_data, surface_normal, center, polygon, thickness, eps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_extrude_rectangle(self,
|
|
|
|
|
cell_data: numpy.ndarray,
|
|
|
|
|
rectangle: numpy.ndarray,
|
|
|
|
|
direction: Union[Direction, int],
|
|
|
|
|
direction: int,
|
|
|
|
|
polarity: int,
|
|
|
|
|
distance: float,
|
|
|
|
|
) -> None:
|
|
|
|
@ -322,23 +319,19 @@ def draw_extrude_rectangle(self,
|
|
|
|
|
Extrude a rectangle of a previously-drawn structure along an axis.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cell_data: Cell data to modify (e.g. created by `Grid.allocate()`)
|
|
|
|
|
rectangle: 2x3 ndarray or list specifying the rectangle's corners
|
|
|
|
|
direction: Direction to extrude in. Direction enum or int in range(3)
|
|
|
|
|
direction: Direction to extrude in. Integer in `range(3)`.
|
|
|
|
|
polarity: +1 or -1, direction along axis to extrude in
|
|
|
|
|
distance: How far to extrude
|
|
|
|
|
"""
|
|
|
|
|
# Turn extrude_direction into its integer representation
|
|
|
|
|
if isinstance(direction, Direction):
|
|
|
|
|
direction = direction.value
|
|
|
|
|
assert(isinstance(direction, int))
|
|
|
|
|
|
|
|
|
|
s = numpy.sign(polarity)
|
|
|
|
|
|
|
|
|
|
rectangle = numpy.array(rectangle, dtype=float)
|
|
|
|
|
if s == 0:
|
|
|
|
|
raise GridError('0 is not a valid polarity')
|
|
|
|
|
if direction not in range(3):
|
|
|
|
|
raise GridError('Invalid direction: {}'.format(direction))
|
|
|
|
|
raise GridError(f'Invalid direction: {direction}')
|
|
|
|
|
if rectangle[0, direction] != rectangle[1, direction]:
|
|
|
|
|
raise GridError('Rectangle entries along extrusion direction do not match.')
|
|
|
|
|
|
|
|
|
@ -347,18 +340,18 @@ def draw_extrude_rectangle(self,
|
|
|
|
|
|
|
|
|
|
surface = numpy.delete(range(3), direction)
|
|
|
|
|
|
|
|
|
|
dim = numpy.fabs(diff(rectangle, axis=0).T)[surface]
|
|
|
|
|
dim = numpy.fabs(numpy.diff(rectangle, axis=0).T)[surface]
|
|
|
|
|
p = numpy.vstack((numpy.array([-1, -1, 1, 1], dtype=float) * dim[0]/2.0,
|
|
|
|
|
numpy.array([-1, 1, 1, -1], dtype=float) * dim[1]/2.0)).T
|
|
|
|
|
thickness = distance
|
|
|
|
|
|
|
|
|
|
eps_func = []
|
|
|
|
|
for i, grid in enumerate(self.grids):
|
|
|
|
|
for i, grid in enumerate(cell_data):
|
|
|
|
|
z = self.pos2ind(rectangle[0, :], i, round_ind=False, check_bounds=False)[direction]
|
|
|
|
|
|
|
|
|
|
ind = [int(floor(z)) if i == direction else slice(None) for i in range(3)]
|
|
|
|
|
ind = [int(numpy.floor(z)) if i == direction else slice(None) for i in range(3)]
|
|
|
|
|
|
|
|
|
|
fpart = z - floor(z)
|
|
|
|
|
fpart = z - numpy.floor(z)
|
|
|
|
|
mult = [1-fpart, fpart][::s] # reverses if s negative
|
|
|
|
|
|
|
|
|
|
eps = mult[0] * grid[tuple(ind)]
|
|
|
|
@ -375,5 +368,5 @@ def draw_extrude_rectangle(self,
|
|
|
|
|
|
|
|
|
|
eps_func.append(f_eps)
|
|
|
|
|
|
|
|
|
|
self.draw_polygon(direction, center, p, thickness, eps_func)
|
|
|
|
|
self.draw_polygon(cell_data, direction, center, p, thickness, eps_func)
|
|
|
|
|
|
|
|
|
|