创建可旋转的 3D RGB 立方体

问题描述 投票:0回答:1

我想创建/绘制一个可以旋转的 3D RGB 立方体。

类似这样的:

picture of 3D RGB Cube

我认为 matplotlib 是创建它的一个不错的选择,因为它具有内置的旋转功能。

我尝试过这样的事情:

import numpy as np
import matplotlib.pyplot as plt

# Full 8-bit RGB space
bits = 8
cube_dimension = 2**bits
full_rgb_space = np.zeros((cube_dimension, cube_dimension, cube_dimension, 3), dtype=np.uint8)

# Fill the 3D RGB cube
for i in range(cube_dimension):
    for j in range(cube_dimension):
        for k in range(cube_dimension):
            color = (i, j, k)
            full_rgb_space[i, j, k] = color

# Create a figure and 3D axis
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# # Extract the RGB components
r, g, b = full_rgb_space[:, :, :, 0], full_rgb_space[:, :, :, 1], full_rgb_space[:, :, :, 2]
#
# # Reshape the RGB arrays to match the dimensions of the scatter plot
r = r.flatten()
g = g.flatten()
b = b.flatten()
#
# # Create an array of colors for each point
colors = full_rgb_space / 255.0
colors = colors.reshape(-1, 3)
#
# # Display the RGB cube using scatter plot
ax.scatter(r, g, b, c=colors, marker='s')


ax.axis('off')
plt.show()

它甚至在某种程度上渲染了我想要的东西,但问题是它太慢/没有优化,即使简单的旋转也需要很长时间。

恐怕我不幸地采取了错误的方法,但我不知道应该如何正确地做到这一点。

python matplotlib rgb cube matplotlib-3d
1个回答
0
投票

我知道这不是最好的解决方案,但至少它比以前的版本更好。

import numpy as np
import matplotlib.pyplot as plt

numberOfPointsInDimension = 64
skip = 256 / numberOfPointsInDimension
cube_dimension = int(256 / skip)
count = 0
# to speed process it replaced counting of elements with constant for some specific dimensions values
if numberOfPointsInDimension == 256:
    count = 390152
elif numberOfPointsInDimension == 128:
    count = 96776
elif numberOfPointsInDimension == 64:
    count = 23816
else:
    for i in range(cube_dimension):
        for j in range(cube_dimension):
            for k in range(cube_dimension):
                if i == 0 or i == cube_dimension-1 or j == 0 or j == cube_dimension-1 or k == 0 or k == cube_dimension-1:
                    count += 1

points = np.zeros((count, 3), dtype=np.uint8)
count = 0
for i in range(cube_dimension):
    for j in range(cube_dimension):
        for k in range(cube_dimension):
            if i == 0 or i == cube_dimension-1 or j == 0 or j == cube_dimension-1 or k == 0 or k == cube_dimension-1:
                color = (i * skip, j * skip, k * skip)
                points[count] = color
                count += 1

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Set equal aspect ratio for the 3D plot
ax.set_box_aspect([1, 1, 1])
# # Extract the RGB components
r, g, b = points[:, 0], points[:, 1], points[:, 2]
# # Reshape the RGB arrays to match the dimensions of the scatter plot
r = np.ravel(r)
g = np.ravel(g)
b = np.ravel(b)
# # Create an array of colors for each point
colors = points / 255.0
# Display the RGB cube using scatter plot
scaling = 200
ax.scatter(r, g, b, c=colors, marker='s', s=scaling, alpha=1)
ax.axis('off')

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.