将3d点转换为具有numpy的新坐标系的功能

问题描述 投票:-1回答:2

我在太空中有n点:points.shape == (n,3)

我有一个由点O = [ox, oy, oz]和3个不同长度的正交向量定义的新坐标系:Ox = [oxx, oxy, oxz], Oy = [oyx, oyy, oyz], Oz = [ozx, ozy, ozz]

我该怎么写这样的函数?

def change_coord_system(points, O, Ox, Oy, Oz)
    return # points in new coordinate system
python numpy math 3d geometry
2个回答
2
投票

你在原始系统中有4个非共面点(whele lx是第一个向量的长度,依此类推):

(0,0,0), (lx,0,0), (0,ly,0), (0,0,lz)

他们的双胞胎在新系统中

 [ox, oy, oz]
 [oxx + ox, oxy + oy, oxz + oz]
 [oyx + ox, oyy + oy, oyz + oz]
 [ozx + ox, ozy + oy, ozz + oz]

仿射变换矩阵A应该将初始点转换为它们的对点

   A * P = P' 

使用点列向量制作矩阵:

      |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|

要计算A,需要将两边乘以P矩阵的倒数

A * P * P-1 = P' * Pinverse
A * E = P' * Pinverse
A = P' * Pinverse

因此,计算P的逆矩阵并将其与右侧矩阵相乘。

编辑:由Maple计算的逆矩阵

 [[-1/lx, -1/ly, -1/lz, 1], 
  [1/lx, 0, 0, 0], 
  [0, 1/ly, 0, 0], 
  [0, 0, 1/lz, 0]]

由此产生的仿射变换矩阵是

[[-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

编辑: 刚刚注意到:Maple没有删除过多的summands,所以结果应该更简单:

[[(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]]

1
投票

假设我们有两个点,P=[2, 4, 5]Q=[7, 2, 5]。首先,您必须找到用于旋转变换A的矩阵和用于传输的矩阵B,并应用以下等式

enter image description here

使用numpy的代码是

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]])

我认为上面的函数给出了新的观点。你可以检查一下。当然,您可以使用numpy执行矩阵乘法,但是,您需要重塑np.arrays。

© www.soinside.com 2019 - 2024. All rights reserved.