Python 从网格数据中提取坐标

Python 从网格数据中提取坐标,python,numpy,mesh,Python,Numpy,Mesh,我有一个立方体网格,如下图所示 我想列出每个子立方体的顶点,因此我将得到一个嵌套的子立方体列表及其相应的顶点列表 我最初的尝试是使用发电机 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

我有一个立方体网格,如下图所示

我想列出每个子立方体的顶点,因此我将得到一个嵌套的子立方体列表及其相应的顶点列表

我最初的尝试是使用发电机

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')
这确实为我提供了所需的形状,但坐标的排序方式使子框的顶点提取不是直接向前的。我还想把它推广到任意维的盒子,所以区间硬编码不是一个解决方案

所以,我想我应该使用
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='^')
这似乎是一个非常强大的方式来实现我想要的,但我感到困惑。是否有一种直接访问
网格中顶点的方式,即
顶点(x,y,z)
?或者甚至是提取子立方体的直接方法

在我看来,这个解决方案非常接近,但我就是抓不住


meshgrid
可能是您所需要的,但是
meshgrid
返回的数组的形状让人感到困惑。Meshgrid返回三个相同形状的坐标数组。
xv、yv、zv中每一个的形状是
(len(x)、len(y)、len(z))
。因此,要提取拐角处的坐标
(0,2,1)
,您需要编写
xv[0,2,1],yv[0,2,1],zv[0,2,1]

要提取所有子多维数据集的“角点”坐标,有助于观察到,由于
meshgrid
返回的数组按顺序排列,
xv[:-1,:-1,:-1]
只返回每个子多维数据集近左下角的x坐标。同样,
xv[1:,1:,1:
返回每个子多维数据集的最右上角。其他六个角由片
:-1
1:
的其他六个组合给出(例如,
xv[:-1,1:,:-1]
给出了最左边的上角)

因此,迭代
:-1
1:
的所有八个组合,以获得所有
len(x)-1*len(y-1)*len(z-1)
子立方体八个角的x、y、z坐标的三个平行阵列的八个平行阵列。(如果需要子多维数据集角点坐标阵列处于特定的形状或轴顺序,或者如果希望使用单个索引而不是三个索引来指定子多维数据集,请根据需要使用
滚动轴
旋转轴
形状


总是有一种方法将索引放入
xv,yv,zv
而不是获取值,因为值在
角点
数组中重复了很多次。这将涉及将索引数组切片为
xv、yv、zv
,而不是将数组本身切片。在进入恩达里巫毒教这么远之后,我的头已经开始旋转了,所以我将把它作为一个练习。

非常需要这个展览,谢谢。我想知道
meshgrid
的内存问题?(我的电网可能非常大,这就是我最初选择发电机路线的原因)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import itertools

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')


slices = slice(None, -1), slice(1, None)
cornerSlices = list(itertools.product(slices, slices, slices))
corners = np.array([(xv[s], yv[s], zv[s]) for s in cornerSlices])

# The shape of `corners` is `(len(cornerSlices), 3, len(x-1), len(y-1), len(z-1)`
# The axes of `corners` represent, in the same order: the corner index; the cartesian 
# coordinate axis (the index into [x, y, z]); the x, y, and z indexes of the subcube. 

# Plot the first subcube (subcube 0, 0, 0)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

subcube = corners[:, :, 0, 0, 0]
subcubeX = subcube [:, 0]
subcubeY = subcube [:, 1]
subcubeZ = subcube [:, 2]

ax.scatter(subcubeX , subcubeY , subcubeZ , c='g', marker='^')