矢量场的平移和旋转

问题描述

我有一个形状为(宽度、高度、2)的矢量场,其中第三个轴保存每个矢量的大小和角度。

我想围绕给定的枢轴点执行转换(旋转和平移)。我希望 numpy 或 scipy 有一个计算变换矩阵的函数,但到目前为止一无所获。

顺便说一句,这就是我到目前为止所经历的。只需遍历每个坐标并计算新的幅度。向量在极坐标中。

pivot = (magnitude.shape[1]/2,magnitude.shape[0])
for c in range(magnitude.shape[1]):
    for r in range(magnitude.shape[0]):
        magnitude[r,c] -= dist + 2*np.linalg.norm((pivot[0]-r,pivot[1]-c))*np.sin(np.abs(angle/2))

解决方法

根据你的描述,我假设

  field_x = field[:,:,0] * np.sin(field[:,1])
  field_y = field[:,1] * np.cos(field[:,1])
  1. 如果您想绕原点旋转而不进行平移,只需将旋转角度添加到 field[:,1]

  2. 当您想对所有仿射形式的点应用相同的变换时,旋转矩阵很有用。 (你有一个极坐标表示)。

  3. 我不清楚你到底想如何进行转换,所以我会给你一个函数,它围绕给定的枢轴旋转点,然后平移。

要围绕给定的枢轴平移,您必须首先将旋转中心平移到原点,应用旋转,然后将旋转中心平移回原始位置。

def translate(x,y,tx,ty):
  return (x + tx,y + ty)

def rotate(x,angle):
  c = np.cos(angle)
  s = np.sin(angle)
  return x * c - y * s,x * s + y * c

def polar2cartesian(field):
  c = np.sin(field[:,1])
  s = np.cos(field[:,1])
  r = field[:,0]
  return r*c,r*s
def cartesian2polar(x,y):
  result = np.empty(x.shape + (2,))
  result[:,0] = np.sqrt(x**2 + y**2)
  result[:,1] = np.arctan2(y,x); 
  return result;

def transform(field1,pivot,angle,translation):
  x,y = polar2cartesian(field1)
  px,py = polar2cartesian(pivot)
  tx,ty = polar2cartesian(translation)
  # move the center of rotation to the origin
  x,y = translate(x,-px,-py);
  x,y = rotate(x,angle)
  x,ty)
  return cartesian2polar(x,y)
  

为了说明使用,我将使用一些具有恒定半径和恒定旋转的 1000 x 1 场。

r = np.linspace(0,2*np.pi,1000)
field1 = np.zeros((1000,1,2))
field2 = np.zeros((1000,2))
field3 = np.zeros((1000,2))
plt.figure(figsize=(5,5))
R = 10;
field1[:,0] = 1
field1[:,1] = 0
field2[:,0] = R+1
field2[:,1] = r
field3[:,0] = 0
field3[:,1] = R*r;
field4[:,0] = R;
field4[:,1] = r
plt.plot(*polar2cartesian(transform(field1,field3,field3[:,1],field4)))
field4[:,0] = R+2
field3[:,1] = -(R+2)*r + np.pi
plt.plot(*polar2cartesian(transform(field1,field4)))
plt.plot(*polar2cartesian(field2))

解释 第一个图是半径为 R+1 的圆,第二个图是半径为 1 的圆中的点在半径为 R+1 的圆内滚动的轨迹,第三个图是半径为 1 的圆中一点在半径为 R+1

的圆外滚动的轨迹

output of the above example