def rotate(a, args):
"""
Rotate panel layout by an arbitrary degree around the origin.
"""
# Create a matrix that fits all panels and puts the origin at the center.
# Downscale the matrix by 10x to reduce computation. Also scipy.ndimage.rotate
# seems to have issues with large matrices where some elements get duplicated
panels = {p['panelId']: p for p in a.panel_positions}
for pid in panels:
panels[pid]['x'] //= 10
panels[pid]['y'] //= 10
# calculate the max dimension of our bounding box, and make sure that our
# resulting image can handle 2x the image size, in case we rotate 45 degrees
dim = 2 * max([max(abs(p['x']), abs(p['y'])) for p in panels.values()])
image = numpy.zeros(shape=(2 * dim, 2 * dim))
# Put all panels in the matrix and rotate
for (k, v) in panels.items():
image[v['y'] + dim][v['x'] + dim] = k
# Rotate
r = args.rotate % 360
rotated = ndimage.rotate(image, r, order=0, reshape=False)
for (y, x) in numpy.transpose(numpy.nonzero(rotated)):
p = panels[rotated[y][x]]
p['x'] = (int(x) - dim) * 10
p['y'] = (int(y) - dim) * 10
p['o'] = (p['o'] + r) % 360
# Cache the result for the future, along with the rotation we used
config = configparser.ConfigParser()
config.read('aurora.ini')
config['device']['rotation'] = str(args.rotate % 360)
config['device']['panel_positions'] = json.dumps(list(panels.values()))
config.write(open('aurora.ini', 'w'))
评论列表
文章目录