简体   繁体   English

从meshgrid数据中提取坐标

[英]Extracting coordinates from meshgrid data

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 , 那么,我想我会用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) ? 有没有直接的方式以vertex(x,y,z)方式访问meshgrid中的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! 在我看来,解决方案非常接近,但我无法掌握它!

立方网格

meshgrid is probably what you need, but the shape of the array returned by meshgrid is where it gets confusing. meshgrid可能就是你所需要的,但是meshgrid返回的数组的形状让人感到困惑。 Meshgrid returns three coordinate arrays, all the same shape. Meshgrid返回三个坐标数组,形状相同。 The shape of each of xv, yv, zv is (len(x), len(y), len(z)) . xv, yv, zv的每一个的形状是(len(x), len(y), len(z)) So, to extract the coordinate at the corner (0, 2, 1) , you would write xv[0, 2, 1], yv[0, 2, 1], zv[0, 2, 1] 因此,要提取角落(0, 2, 1) xv[0, 2, 1], yv[0, 2, 1], zv[0, 2, 1] (0, 2, 1)处的坐标,您可以编写xv[0, 2, 1], yv[0, 2, 1], zv[0, 2, 1]

To extract all of the subcubes' corners' coordinates, it helps to observe that, because of the way the arrays returned by meshgrid are ordered sequentially, xv[:-1, :-1, :-1] returns only the x-coordinates of the near-left-bottom corners of each subcube. 要提取所有子立方体的“角”坐标,有助于观察到,由于meshgrid返回的数组按顺序排序, xv[:-1, :-1, :-1]仅返回x坐标每个子立方体的近左下角。 Likewise, xv[1:, 1:, 1:] returns the far-right-top corners of each subcube. 同样, xv[1:, 1:, 1:]返回每个子立方体的最右上角。 The other six corners are given by the other six combinations of the slices :-1 and 1: ( xv[:-1, 1:, :-1] gives the far-left-top corner, for example). 其他六个角由切片的其他六个组合给出:-11:xv[:-1, 1:, :-1]给出例如远左上角。

So, iterate through all eight combinations of :-1 and 1: to get eight parallel arrays of three parallel arrays of x, y, z coordinates for the eight corners of all len(x)-1 * len(y-1) * len(z-1) subcubes. 因此,遍历所有八个组合:-11:得到所有len(x)-1 * len(y-1) * len(z-1)的八个角的x,y,z坐标的三个平行数组的八个并行数组len(x)-1 * len(y-1) * len(z-1)多维数据集。 (If you need your subcube corner coordinate arrays to be in a particular shape or axis order, or if you want to use a single index to specify the subcube rather than three, use rollaxis , swapaxis and shape as needed.) (如果您需要将子多维数据集角坐标数组设置为特定的形状或轴顺序,或者如果要使用单个索引来指定子多维数据集而不是三个,请根据需要使用rollaxisswapaxisshape 。)

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

在此输入图像描述

There's invariably a way to get the indexes into xv, yv, zv instead of getting the values, since the values are duplicated quite a few times in the corners array. 总有一种方法可以将索引转换为xv, yv, zv而不是获取值,因为这些值在corners数组中重复了很多次。 It would involve slicing arrays of indexes into xv, yv, zv instead of slicing the arrays themselves. 它将涉及将索引数组切片为xv, yv, zv而不是切片数组本身。 My head is already spinning after getting this far into the ndarray voodoo, so I'll leave that as an exercise. 在把这个进入ndarray伏都教之后,我的头已经旋转了,所以我将把它留作练习。

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

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