我有两个网格(称为A和B),我在python中使用open3d库进行交互(如果效果更好的话,我很乐意使用import trimesh
或其他库)。网格A是一个曲线型结构,而网格B近似于一个更平坦的曲面。
我需要旋转网格,以便我感兴趣的曲面面对相机。为了识别我需要的表面,我编写了一些函数来将网格转化为点云,并为网格A创建最适合的球体,为网格B创建最适合的平面。这些都非常好地工作,并且很好地匹配对象。
我需要旋转网格,并且遇到了如何执行旋转的问题。
我拥有的输入数据:
对于网格A:网格的质心A的坐标和其最佳拟合球面的坐标(+其半径)-我想旋转网格,使其垂直于上述数据所描述的矢量?
对于网格B:网格B的质心坐标和最佳拟合平面的法线的向量-我想旋转网格,使其垂直于上述数据所描述的向量
发布于 2020-10-30 21:28:09
解决了!创建了两个矢量作为numpy数组,矢量1和vector2,并使用open3d命令将网格居中并在两个矢量之间应用旋转矩阵。我认为这很容易受到万向节锁定的影响,但我无法理解四元数,对于我的使用场景,它工作得足够好。
#this function to create a rotation matrix was taken from https://stackoverflow.com/q/63525482/6010071
def rotation_matrix_from_vectors(vec1, vec2):
a, b = (vec1 / np.linalg.norm(vec1)).reshape(3), (vec2 / np.linalg.norm(vec2)).reshape(3)
v = np.cross(a, b)
c = np.dot(a, b)
s = np.linalg.norm(v)
kmat = np.array([[0, -v[2], v[1]], [v[2], 0, -v[0]], [-v[1], v[0], 0]])
rotation_matrix = np.eye(3) + kmat + kmat.dot(kmat) * ((1 - c) / (s ** 2))
return rotation_matrix
#to move the mesh to 0,0,0 (using open3d library)
mesh.translate(-mesh.get_center())
# to rotate the mesh to variable vector2, i set it to np.array([0,0,1]) = in line with z axis again using open3d library
mesh.rotate(rotation_matrix_from_vectors(vector1,vector2),center=(0,0,0))
发布于 2020-10-29 19:41:42
我不知道那个库,但也许你可以试着在Matplotlib的Affine2D()类中旋转对象。
具体来说,可以尝试使用这个函数:mtransforms.Affine2D().rotate()
首先,您必须导入它:matplotlib.transforms as mtransforms
https://stackoverflow.com/questions/64590271
复制相似问题