我正在尝试构建一个可以在3D网格上生成(x,y,z)10x10x10阵列(单元格)的程序。示例:一个立方体位于点(0,10)x(0,10)x(0,10),而另一个立方体位于(0,10)x(10,20)x(0,10)。到目前为止,我有一个功能,使其中的4个,但我如何自动化它几十甚至几百?
import math
import numpy as np
def cubeplot():
count = 0
count2 = 0
x = 11
y = 11
z = 11
c = 11
parameter = np.arange(0,11,1)
xx, yy, zz = np.meshgrid(parameter, parameter, parameter)
valuesrange = np.zeros((11, 11, 11))
parameter2 = np.arange(c, y+10,1)
xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
valuesrange2 = np.zeros((x+10, y+10 , z + 10))
parameter3 = np.arange(c + 10, y+20,1)
xx2, yy2, zz2 = np.meshgrid(parameter2, parameter2, parameter2)
valuesrange2 = np.zeros((x+20, y+20 , z +20))
print('POINT 1')
while (count < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(10,c, 22)
x = 10
else:
xint = np.random.randint(0,1, 22)
x = 0
if yint >0:
yint = np.random.randint(10,c, 22)
y = 10
else:
yint = np.random.randint(0,1, 22)
y = 0
if zint > 0:
zint = np.random.randint(10,c, 22)
z = 10
else:
zint = np.random.randint(0,1, 22)
z = 0
count = count + 1
print('x = ' + str(x))
print('y = ' + str(y))
print('z = ' + str(z))
# print('Distance = ' + str(zint))
print('POINT 2')
while (count2 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(20,c + 10, 22)
x2 = 20
else:
xint = np.random.randint(10,11, 22)
x2 = 10
if yint >0:
yint = np.random.randint(10,c, 22)
y2 = 10
else:
yint = np.random.randint(0,1, 22)
y2 = 0
if zint > 0:
zint = np.random.randint(10,c, 22)
z2 = 10
else:
zint = np.random.randint(0,1, 22)
z2 = 0
count2 = count2 + 1
print('x = ' + str(x2))
print('y = ' + str(y2))
print('z = ' + str(z2))
distance = ((x2-x)**2 + (y2 - y)**2 + (z2 - z)**2)**.5
print ('POINT 1: x,y,z: ' + str(x) +',' + str(y) + ','+ str(z))
print ('POINT 2: x,y,z: ' + str(x2) +',' + str(y2)+ ',' + str(z2))
print('Distance = ' + str(distance))
print('POINT 3')
count3 = 0
while (count3 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(10,c, 22)
x3 = 10
else:
xint = np.random.randint(0,1, 22)
x3 = 0
if yint >0:
yint = np.random.randint(10,c, 22)
y3 = 10
else:
yint = np.random.randint(0,1, 22)
y3 = 0
if zint > 0:
zint = np.random.randint(20,c + 10, 22)
z3 = 20
else:
zint = np.random.randint(10,c, 22)
z3 = 10
count3 = count3 + 1
print('x = ' + str(x3))
print('y = ' + str(y3))
print('z = ' + str(z3))
print('POINT 4')
count4 = 0
while (count4 < 1):
xint = np.random.randint(0,2)
yint = np.random.randint(0,2)
zint = np.random.randint(0,2)
if xint > 0:
xint = np.random.randint(20,c+10, 22)
x4 = 20
else:
xint = np.random.randint(10,c, 22)
x4 = 10
if yint >0:
yint = np.random.randint(10,c, 22)
y4 = 10
else:
yint = np.random.randint(0,1, 22)
y4 = 0
if zint > 0:
zint = np.random.randint(20,c + 10, 22)
z4 = 20
else:
zint = np.random.randint(10,c, 22)
z4 = 10
count4 = count4 + 1
print('x = ' + str(x4))
print('y = ' + str(y4))
print('z = ' + str(z4))
print ('POINT 3: x,y,z: ' + str(x3) +',' + str(y3) + ','+ str(z3))
print ('POINT 4: x,y,z: ' + str(x4) +',' + str(y4)+ ',' + str(z4))
print('SET END')
print('')
print('')
print('')
runtime = int (input("How many times would you like to run the program?: "))
maincount = 0
print ('The program will run', runtime, 'times')
while (maincount < runtime):
cubeplot()
maincount = maincount + 1
答案 0 :(得分:0)
numpy arrays比你想象的要强大一些。如果您希望3D网格的每个单元格都有另一个3D网格,那么您要寻找的是6D网格。请检查以下示例:
import numpy as np
# Lets make a 3D grid in which each cell has 3 dimensions
# (making it a 6D grid)
grid = np.zeros((3, 2, 4, 10, 10, 10))
# So for every arbitrary 3D coordinate of the grid I have
# another 3D grid (result: (10, 10, 10))
print(grid[2, 1, 3].shape)
# If a make a grid for a single cell:
# the randint function is creating a (10, 10, 10) grid
local_grid = np.random.randint(0, 10, (10, 10, 10))
# I can put it inside my big grid:
grid[2, 1, 3] = local_grid
# If I need to this for all (3, 2, 4) shape of my big grid
# (meaning its first 3 dimensions) I can loop each of those
# dimensions:
for x in range(grid.shape[0]):
for y in range(grid.shape[1]):
for z in range(grid.shape[2]):
grid[x, y, z] = np.random.randint(0, 10, (10, 10, 10))
根据您的需要,还有其他方法可以填充网格(可能更优化),但除非您确实需要,否则您几乎不需要使用索引编号。这是你在寻找什么(逻辑明智)?
编辑:因此,如果你真的只想要一个3D网格,你可以将你的单元格尺寸乘以网格尺寸并循环它以索引内部的一些内容。例如:
target = (3, 2, 4)
cell = (3, 3, 3)
grid = np.zeros((target[0]*cell[0], target[1]*cell[1], target[2]*cell[2]))
counter = 1
for x in range(target[0]):
for y in range(target[1]):
for z in range(target[2]):
grid[x*cell[0]:x*cell[0]+cell[0], y*cell[0]:y*cell[1]+cell[1], z*cell[2]:z*cell[2]+cell[2]] = counter
counter += 1
在这种情况下,grid[0:cell[0], 0:cell[1], 0:cell[2]]
将等于内容为cell
的{{1}}维网格。