I have a cubic grid as shown in the picture below.
I would like to list the vertices of each sub-cube, so I would end up with a nested list of sub-cubes with their corresponding list of vertices.
My initial attempt was to use a generator,
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
dims = [9,9,9]
spacer = 3
subBoxCoords = np.array([(x, y, z) for x in range(0, dims[0], spacer) for y in range(0, dims[1], spacer) for z in range(0, dims[2], spacer)])
ax.scatter(subBoxCoords[:,0], subBoxCoords[:,1], subBoxCoords[:,2], c='k', marker='o')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
This does give me the desired shape but coordinates are ordered in a manner that vertices extraction of the sub-boxes is not straight forward. Also I would like to generalize this to boxes of arbitrary dimension so hard coding in intervals is not a solution.
So, then I thought I would use meshgrid
,
nx,ny, nz = (3,3,3)
x = np.linspace(0, 10, nx)
y = np.linspace(0, 10, ny)
z = np.linspace(0, 10, nz)
xv, yv, zv = np.meshgrid(x, y, z, indexing='xy')
ax.scatter(xv, yv, zv, c='g', marker='^')
This appears to be a very powerful way to achieve what I want but I am getting confused. Is there a direct way access vertices in the meshgrid
in the manner vertex(x,y,z)
? Or even a straight forward way to extract sub-cubes?
It seems to me that the solution is tantalizingly close but I just cant grasp it!