本文介绍了如何将 3d 数组 (nxnxn) 围绕 x、y 和 z 轴旋转 x 度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 numpy 创建的 3d 数组,我想知道如何按自定义角度旋转它,而不仅仅是 numpy 具有的 rot90 函数.有人可以帮忙吗?

I have a 3d array created with numpy, and I was wondering how I can rotate it by a custom angle, not just the rot90 function that numpy has. Can anyone help?

3d 矩阵表示图像(如立方体或其他形状)即

The 3d matrix represents an image (such as a cube, or some other shape) ie

0:
1 1 1
1   1
1 1 1

1:
1   1

1   1

2:
1 1 1
1   1
1 1 1

移动解决方案来回答

推荐答案

经过反复试验,我想出了一些用于我的目的的代码(0 表示数组中的空,另一个数字表示填充的体素.

After some trial and error I came up with some code for my purposes (0 means empty in the array, another number will mean a filled voxel.

def rotate(self, deg_angle, axis):
        d = len(self.matrix)
        h = len(self.matrix[0])
        w = len(self.matrix[0][0])
        min_new_x = 0
        max_new_x = 0
        min_new_y = 0
        max_new_y = 0
        min_new_z = 0
        max_new_z = 0
        new_coords = []
        angle = radians(deg_angle)

        for z in range(d):
            for y in range(h):
                for x in range(w):

                    new_x = None
                    new_y = None
                    new_z = None

                    if axis == "x":
                        new_x = int(round(x))
                        new_y = int(round(y*cos(angle) - z*sin(angle)))
                        new_z = int(round(y*sin(angle) + z*cos(angle)))
                    elif axis == "y":
                        new_x = int(round(z*sin(angle) + x*cos(angle)))
                        new_y = int(round(y))
                        new_z = int(round(z*cos(angle) - x*sin(angle)))
                    elif axis == "z":
                        new_x = int(round(x*cos(angle) - y*sin(angle)))
                        new_y = int(round(x*sin(angle) + y*cos(angle)))
                        new_z = int(round(z))

                    val = self.matrix.item((z, y, x))
                    new_coords.append((val, new_x, new_y, new_z))
                    if new_x < min_new_x: min_new_x = new_x
                    if new_x > max_new_x: max_new_x = new_x
                    if new_y < min_new_y: min_new_y = new_y
                    if new_y > max_new_y: max_new_y = new_y
                    if new_z < min_new_z: min_new_z = new_z
                    if new_z > max_new_z: max_new_z = new_z

        new_x_offset = abs(min_new_x)
        new_y_offset = abs(min_new_y)
        new_z_offset = abs(min_new_z)

        new_width = abs(min_new_x - max_new_x)
        new_height = abs(min_new_y - max_new_y)
        new_depth = abs(min_new_z - max_new_z)

        rotated = np.empty((new_depth + 1, new_height + 1, new_width + 1))
        rotated.fill(0)
        for coord in new_coords:
            val = coord[0]
            x = coord[1]
            y = coord[2]
            z = coord[3]

            if rotated[new_z_offset + z][new_y_offset + y][new_x_offset + x] == 0:
                rotated[new_z_offset + z][new_y_offset + y][new_x_offset + x] = val

        self.matrix = rotated

我使用上面代码的方式是:

The way I use the above code is:

cube = Rect_Prism(20, 20, 20) # creates a 3d array similar to above example, just bigger
cube.rotate(20, "x")
cube.rotate(60, "y")

Rect_Prism 创建一个 MxNxD 矩阵,但在本例中为 NxNxN.

Rect_Prism creates a MxNxD matrix, but in this case NxNxN.

打印结果:

                            # # # # # # # # # # # #
                      # # #     #         # #       #
                  # #           #   # # #           #
              # #               # #                 #
        # # #               # # # #                   #
    # #               # # #       #                   #
# # # # # # # # # # #             #                   #
#                   #               #                   #
  #                 #               #                   #
  #                 #               #                   #
  #                 #                 #                   #
    #                 #               #                   #
    #                 #               #                   #
    #                 #                 #                 #
      #                 #               #                 #
      #                 #               # #               # #
      #                   #               #                 #
      #                   #               # # # # # # # # # #
      #                   #           # #                 #
        #                   #   # # #               # # #
        #                   # # #             # # #
        #             # # # #             # #
          #       # #         #     # # #
          #   # #             # # #
          # # # # # # # # # # # #

这篇关于如何将 3d 数组 (nxnxn) 围绕 x、y 和 z 轴旋转 x 度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:39