本文介绍了一次多个2D旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组(n)个几何形状,这些形状由固定数量(p)的2D点定义.这些形状是独立的,但出于效率原因,我将时间存储在单个numpy数组中.缩放或平移这些形状很容易,但是我想旋转它们,但我不确定该怎么做.我怀疑np.tensordot是我的朋友,但我找不到正确使用它的方法.

I've a set of (n) geometrical shapes that are defined by a fixed number (p) of 2D points. Those shapes are independent but for efficiency reason, I stored time in a single numpy array. Scaling or translating those shapes are easy, but I would like to rotate them and I'm not sure how to do that. I suspect np.tensordot is my friend but I can't find the way to properly use it.

n = 100 # Number of shape
p = 4   # Points per shape
P = np.random.uniform(0, 1, (n, p, 2))

# Scaling
S = 0.5*np.ones(n)
P *= S

# Translating
T = np.random.uniform(0, 1, (n, 1, 2))
P += T

# Rotating
A = np.random.uniform(0, 2*np.pi, n)
cosA, sinA = np.cos(A), np.sin(A)

R = np.empty((n,2,2))
R[:,0,0] = cosA
R[:,1,0] = sinA
R[:,0,1] = -sinA
R[:,1,1] = cosA

np.tensordot(P, R, axes=???)

推荐答案

似乎您要保持两个数组之间的第一个轴-PR对齐,并且sum-reducing分别与其余数组分开输入数组.因此,我们可以使用 np.einsum 将允许我们使用轴对齐标准.

It seems you are keeping the first axis between the two arrays - P and R aligned and sum-reducing one each off the remaining axes from the input arrays. So, we can use np.einsum as it will allow us the axis-alignment criteria.

您正在使用P中的最后一个轴进行求和.现在,根据R的哪条轴,您将失去sum-reduction的旋转计算,其中之一应该可以完成工作-

You are using the last axis from P for the sum-reduction. Now, depending on which axis of R you are losing with sum-reduction for the rotation calculation, one of these should do the job -

np.einsum('ijk,ilk->ijl',P,R) # Using last dim of R for sum-reduction
np.einsum('ijk,ikl->ijl',P,R) # Using second dim of R for sum-reduction

这篇关于一次多个2D旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:36