【问题标题】:Translation and rotation of vector field矢量场的平移和旋转
【发布时间】:2021-02-09 17:02:27
【问题描述】:

我有一个形状为 (width, height, 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))

【问题讨论】:

标签: python numpy vector scipy


【解决方案1】:

根据你的描述,我假设

  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, y, 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, y, -px, -py);
  x, y = rotate(x, y, angle)
  x, y = translate(x, y, tx, 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, 1, 2))
field3 = np.zeros((1000, 1, 2))
plt.figure(figsize=(5, 5))
R = 10;
field1[:,0,0] = 1
field1[:,0,1] = 0
field2[:,0,0] = R+1
field2[:,0,1] = r
field3[:,0,0] = 0
field3[:,0,1] = R*r;
field4[:,0,0] = R;
field4[:,0,1] = r
plt.plot(*polar2cartesian(transform(field1, field3, field3[:,:,1], field4)))
field4[:,0,0] = R+2
field3[:,0,1] = -(R+2)*r + np.pi
plt.plot(*polar2cartesian(transform(field1, field3, field3[:,:,1], field4)))
plt.plot(*polar2cartesian(field2))

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 2013-12-05
    • 1970-01-01
    相关资源
    最近更新 更多