简体   繁体   English

np.tensordot旋转点云?

[英]np.tensordot for rotation of point clouds?

Rotation about the origin is a matrix product that can be done with numpy's dot function, 绕原点旋转是可以使用numpy的点函数完成的矩阵乘积,

import numpy as np
points = np.random.rand(100,3)  # 100 X, Y, Z tuples.  shape = (100,3)
rotation = np.identity(3)  # null rotation for example
out = np.empty(points.shape)
for idx, point in enumerate(points):
    out[idx,:] = np.dot(rotation, point)

This involves a for loop, or numpy tile could be used to vectorize. 这涉及到for循环,或者可以使用numpy tile进行矢量化。 I think there is an implementation involving np.tensordot, but the function is witchcraft to me. 我认为有一个涉及np.tensordot的实现,但是功能对我来说却是巫术。 Is this possible? 这可能吗?

There are several ways you can do that. 有几种方法可以做到这一点。 With np.matmul you can do: 使用np.matmul您可以执行以下操作:

out = np.matmul(rotation, points[:, :, np.newaxis])[:, :, 0]

Or, equivalently, if you are using Python 3.5 or later: 或者,等效地,如果您使用的是Python 3.5或更高版本:

out = (rotation @ points[:, :, np.newaxis])[:, :, 0]

Another way is with np.einsum : 另一种方法是使用np.einsum

out = np.einsum('ij,nj->ni', rotation, points)

Finally, as you suggested, you can also use np.tensordot : 最后,按照您的建议,您还可以使用np.tensordot

out = np.tensordot(points, rotation, axes=[1, 1])

Note that in this case points is the first argument and rotation the second, otherwise the dimensions at the output would be reversed. 请注意,在这种情况下, points是第一个参数, rotation是第二个参数,否则输出的尺寸将相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM