使用numpy将3D点转换到新坐标系的函数

2024-04-20 14:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我在空间中有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

Tags: 空间system向量pointsox坐标系shapeoy
2条回答

假设我们有两个点,P=[2, 4, 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.数组. 在

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

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

以及他们在新系统中的双胞胎

^{pr2}$

仿射变换矩阵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,需要将两个sudes乘以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没有删除过多的总和,所以结果应该更简单:

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

相关问题 更多 >