Reputation: 61
I have n
points in space:
points.shape == (n,3)
I have a new coordinate system defined by a point O = [ox, oy, oz]
and 3 orthogonal vectors of different lengths: Ox = [oxx, oxy, oxz], Oy = [oyx, oyy, oyz], Oz = [ozx, ozy, ozz]
.
How can I write a function like that?
def change_coord_system(points, O, Ox, Oy, Oz)
return # points in new coordinate system
Upvotes: 0
Views: 5974
Reputation: 80107
You have 4 non-coplanar points in original system (where lx
is length of the first vector and so on):
(0,0,0), (lx,0,0), (0,ly,0), (0,0,lz)
and their twins in new system
[ox, oy, oz]
[oxx + ox, oxy + oy, oxz + oz]
[oyx + ox, oyy + oy, oyz + oz]
[ozx + ox, ozy + oy, ozz + oz]
Affine transformation matrix A should transform initial points into their pair points
A * P = P'
make matrix with point column vectors:
|x1 x2 x3 x4| |x1' x2' x3' x4'|
A *|y1 y2 y3 y4| = |y1' y2' y3' y4'|
|z1 z2 z3 z4| |z1' z2' z3' z4'|
|1 1 1 1| |1 1 1 1|
|0 lx 0 0| |ox oxx + ox . .|
A *|0 0 ly 0| = |oy oxy + oy . .| // lazy to make last columns
|0 0 0 lz| |oz oxz + oz . .|
|1 1 1 1| |1 1 1 1|
To calculate A, it is needed to multiply both sudes by inverse of P matrix
A * P * P-1 = P' * Pinverse
A * E = P' * Pinverse
A = P' * Pinverse
So calculate inverse matrix for P and multiply it with right-side matrix.
Edit: inverse matrix calculated by Maple is
[[-1/lx, -1/ly, -1/lz, 1],
[1/lx, 0, 0, 0],
[0, 1/ly, 0, 0],
[0, 0, 1/lz, 0]]
And resulting affine transformation matrix is
[[-ox/lx+(oxx+ox)/lx, -ox/ly+(oyx+ox)/ly, -ox/lz+(ozx+ox)/lz, ox],
[-oy/lx+(oxy+oy)/lx, -oy/ly+(oyy+oy)/ly, -oy/lz+(ozy+oy)/lz, oy],
[-oz/lx+(oxz+oz)/lx, -oz/ly+(oyz+oz)/ly, -oz/lz+(ozz+oz)/lz, oz],
[0, 0, 0, 1]]
Maple sheet view for reference
Edit:
Just have noticed: Maple did not remove excessive summands, so result should be simpler:
[[(oxx)/lx, (oyx)/ly, (ozx)/lz, ox],
[(oxy)/lx, (oyy)/ly, (ozy)/lz, oy],
[(oxz)/lx, (oyz)/ly, (ozz)/lz, oz],
[0, 0, 0, 1]]
Upvotes: 3
Reputation: 357
Suppose that we have two points, P=[2, 4, 5]
and Q=[7, 2, 5]
. First you have to find the matrix for the rotation transformation A and the matrix B for transport and apply the below equation
The code using numpy is
import numpy as np
# points P and Q
points = np.array([[2,4,5], [7,2,5]])
# suppose that the matrices are
rotation_matrix = np.matrix('1 2 1; 1 2 1; 1 2 1')
b = np.array([1, 1, 1])
def transformation(points, rotation_matrix, b):
for n in range(points.shape[0]):
points[n,0] = rotation_matrix[0,0] * points[n, 0] + rotation_matrix[0,1] * points[n, 1] + rotation_matrix[0,2] * points[n, 2] + b[0]
points[n,1] = rotation_matrix[1,0] * points[n, 0] + rotation_matrix[1,1] * points[n, 1] + rotation_matrix[1,2] * points[n, 2] + b[1]
points[n,2] = rotation_matrix[2,0] * points[n, 0] + rotation_matrix[2,1] * points[n, 1] + rotation_matrix[2,2] * points[n, 2] + b[2]
Output: array([[16, 30, 82],
[17, 27, 77]])
I think that the above function gives the new points. You can check it. Of course, you can perform matrix multiplication with numpy although, you need to reshape the np.arrays.
Upvotes: 1