Reputation: 1
I have a vector field of shape (width, height, 2) where the third axis holds the magnitude and angle of each vector.
I want to perform a transformation (rotation and translation) around a given pivot point. I would expect that either numpy or scipy have a function for calculating the transformation matrix, but have so far found nothing.
This is what I've tride so far btw. Just iterating through each coordinate and calculating the new magnitude. The vectors are in polar coordinates.
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))
Upvotes: 0
Views: 2208
Reputation: 14654
From your description I am assuming that
field_x = field[:,:,0] * np.sin(field[:,:,1])
field_y = field[:,:,1] * np.cos(field[:,:,1])
If you want to rotate around the origin with no translation it is just add the rotation angle to field[:,:,1]
.
Rotation matrix is useful when you want to apply the same transformation to all the points that are in an affine form. (you have a polar representation).
It is not clear to me how exactly you want to do the transformation, so I will give you a function that rotates the point around a given pivot and then translate.
To translate around a given pivot you have to first translate the center of rotation to the origin, apply the rotation and, then you translate the center of rotation back to the original position.
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)
To illustrate the use I will use some 1000 x 1 field with constant radius and constant rotation.
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))
Interpretation The first plot is a circle of radius R+1
the second is the trajectory of a point in a circle of radius one rolling inside the circle of radius R+1
, and the third plot is the trajectory of a point in a circle of radius one rolling outside the circle of radius R+1
Upvotes: 1