python - 从随机生成的法向量进行坐标变换

标签 python 3d coordinate-transformation rotational-matrices

我正在尝试为我用 python 编写的拟合例程随机生成坐标变换。我想围绕原点旋转我的数据(一堆 [x,y,z] 坐标),理想情况下使用我已经创建的一堆随机生成的法线向量来定义平面 - 我只想移动每个平面已定义,使其位于 z=0 平面内。

这是我的代码片段,一旦我有了转换矩阵,它就应该处理好事情。我只是不确定如何从法线向量中获取变换矩阵,以及是否需要比 numpy 更复杂的东西。

import matplotlib as plt
import numpy as np
import math

origin = np.array([35,35,35])
normal = np.array([np.random.uniform(-1,1),np.random.uniform(-1,1),np.random.uniform(0,1)])
mag = np.sum(np.multiply(normal,normal))
normal = normal/mag

a = normal[0]
b = normal[1]
c = normal[2]

#I know this is not the right transformation matrix but I'm not sure what is...
#Looking for the steps that will take me from the normal vector to this transformation matrix
rotation = np.array([[a, 0, 0], [0, b, 0], [0, 0, c]])

#Here v would be a datapoint I'm trying to shift?
v=(test_x,test_y,test_z)
s = np.subtract(v,origin) #shift points in the plane so that the center of rotation is at the origin
so = np.multiply(rotation,s) #apply the rotation about the origin
vo = np.add(so,origin) #shift again so the origin goes back to the desired center of rotation

x_new = vo[0]
y_new = vo[1]
z_new = vo[2]

fig = plt.figure(figsize=(9,9))
plt3d = fig.gca(projection='3d')
plt3d.scatter(x_new, y_new, z_new, s=50, c='g', edgecolor='none')

最佳答案

我认为你对旋转矩阵的概念有误。旋转矩阵定义一定角度的旋转,不能有对角结构。

如果您将每次旋转想象为围绕 X 轴、然后围绕 Y 轴、然后围绕 Z 轴的旋转的组合,您可以构建每个矩阵并将最终旋转组合为矩阵的乘积

R = Rz*Ry*Rx
Rotated_item = R*original_item

Rotated_item = np.multiply(R,original_item)

在此公式中,Rx 是第一个应用的旋转。
请注意

  • 您可以通过组合许多不同的 3 轮换来获得轮换
  • 顺序不是固定的,可以是 X-Y-Z 或 Z-Y-X 或 Z-X-Z 或任何组合。角度值可能会随着序列的变化而变化
  • 使用该矩阵进行临界值旋转(90-180-270-360 度)是“危险的”

如何围绕 1 个轴组成每个单个旋转矩阵?请参阅this image from wikipedia 。 Numpy 拥有您需要的所有东西。

现在您只需定义 3 个角度值。 当然,正如您在问题中所写的那样,您可以从随机归一化向量 (a,b,c) 导出 3 个角度值,但旋转是将一个向量转换为另一个向量的过程。也许你必须指定类似的东西 “我想找到绕原点的旋转 R,将 (0,0,1) 变换为 (a,b,c)”。 完全不同的旋转 R' 是将 (1 ,0,0) 转化为 (a,b,c)。

关于python - 从随机生成的法向量进行坐标变换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886503/

相关文章:

Python加入日期和文件名

c - 纹理映射到在 OpenGL 中使用 GL_QUAD_STRIP 制作的圆柱体

python - 如何在 PyOpenGL 中旋转二维线?

python-3.x - 使用 xarray 更改坐标系以进行切片操作

python - 如何通过bpy.types获取具体的修饰符属性信息?

python - 如何在azure应用程序服务中运行Python shell

java - 如何从 OpenERP 调用 xml-RPC 到 JavaServer?

c# - ILSurface 绘图参数

math - 如何计算两个 3d 圆上的一对最近点?

c++ - 如何进行笛卡尔变换