在python上制作泊松球体分布,但无法找出错误所在

在python上制作泊松球体分布,但无法找出错误所在,python,poisson,povray,Python,Poisson,Povray,我是编程新手,所以我希望我愚蠢的问题不会困扰你。 我现在尝试使用python计算泊松球分布(泊松盘的3D版本),然后将结果插入到POV-RAY中,以便生成一些随机分布的堆积岩。 我关注以下两个链接: [ [ 太长,读不下去了 0.创建一个n维网格阵列,单元大小=r/sqrt(n),其中r是每个球体之间的最小距离。所有阵列都设置为默认值-1,表示“无点” 1.创建一个初始样本。(应该随机放置,但我选择将其放在中间)。将其放在网格数组中。此外,初始化一个活动数组。将初始样本放在活动数组中。 2.当活

我是编程新手,所以我希望我愚蠢的问题不会困扰你。 我现在尝试使用python计算泊松球分布(泊松盘的3D版本),然后将结果插入到POV-RAY中,以便生成一些随机分布的堆积岩。 我关注以下两个链接: [ [ 太长,读不下去了 0.创建一个n维网格阵列,单元大小=r/sqrt(n),其中r是每个球体之间的最小距离。所有阵列都设置为默认值-1,表示“无点”
1.创建一个初始样本。(应该随机放置,但我选择将其放在中间)。将其放在网格数组中。此外,初始化一个活动数组。将初始样本放在活动数组中。 2.当活动列表不为空时,选择一个随机索引。在其附近生成点,并确保点与附近的点不重叠(仅使用附近的数组进行测试)。如果在“随机索引”附近无法创建样本,则将“随机索引”踢出。循环此过程

这是我的代码:

import math
from random import uniform
import numpy
import random
radius = 1      #you can change the size of each sphere
mindis = 2 * radius
maxx = 10         #you can change the size of the container
maxy = 10
maxz = 10
k = 30
cellsize = mindis / math.sqrt(3)
nrofx = math.floor(maxx / cellsize)
nrofy = math.floor(maxy / cellsize)
nrofz = math.floor(maxz / cellsize)
grid = []
active = []
default = numpy.array((-1, -1, -1))
for fillindex in range(nrofx * nrofy * nrofz):
    grid.append(default)
x = uniform(0, maxx)
y = uniform(0, maxy)
z = uniform(0, maxz)
firstpos = numpy.array((x, y,  z))
firsti = maxx // 2
firstj = maxy // 2
firstk = maxz // 2
grid[firsti + nrofx * (firstj + nrofy * firstk)] = firstpos
active.append(firstpos)
while (len(active) > 0) :
    randindex = math.floor(uniform(0,len(active)))
    pos = active[randindex]
    found = False
    for attempt in range(k):
        offsetx = uniform(mindis, 2 * mindis)
        offsety = uniform(mindis, 2 * mindis)
        offsetz = uniform(mindis, 2 * mindis)
        samplex = offsetx * random.choice([1,-1])
        sampley = offsety * random.choice([1,-1])
        samplez = offsetz * random.choice([1,-1])
        sample = numpy.array((samplex, sampley, samplez))
        sample = numpy.add(sample, pos)
        xcoor = math.floor(sample.item(0) / cellsize)
        ycoor = math.floor(sample.item(1) / cellsize)
        zcoor = math.floor(sample.item(2) / cellsize)
        attemptindex = xcoor + nrofx * (ycoor + nrofy * zcoor)
        if attemptindex >= 0 and attemptindex < nrofx * nrofy * nrofz and     numpy.all([sample, default]) == True and xcoor > 0 and ycoor > 0 and zcoor > 0 :
            test = True
            for testx in range(-1,2):
                for testy in range(-1, 2):
                    for testz in range(-1, 2):
                        testindex = (xcoor + testx) + nrofx * ((ycoor + testy) + nrofy * (zcoor + testz))
                        if testindex >=0 and testindex < nrofx * nrofy * nrofz :
                            neighbour = grid[testindex]
                            if numpy.all([neighbour, sample]) == False:
                                if numpy.all([neighbour, default]) == False:
                                    distance = numpy.linalg.norm(sample - neighbour)
                                    if distance > mindis:
                                        test = False
            if test == True and len(active)<len(grid):
                found = True
                grid[attemptindex] = sample
                active.append(sample)
    if found == False:
        del active[randindex]

for printout in range(len(grid)):
    print("<" + str(active[printout][0]) + "," + str(active[printout][1]) + "," + str(active[printout][2]) + ">")
print(len(grid))
导入数学
从随机导入制服
进口numpy
随机输入
半径=1#您可以更改每个球体的大小
mindis=2*半径
maxx=10#您可以更改容器的大小
最大值=10
最大值=10
k=30
cellsize=mindis/math.sqrt(3)
nrofx=数学地板(最大尺寸/单元尺寸)
nrofy=数学楼层(最大/单元尺寸)
nrofz=数学地板(最大尺寸/单元尺寸)
网格=[]
活动=[]
默认值=numpy.array(-1,-1,-1))
对于范围内的填充索引(nrofx*nrofy*nrofz):
grid.append(默认)
x=均匀(0,最大)
y=均匀(0,最大值)
z=均匀(0,最大值)
firstpos=numpy.array((x,y,z))
firsti=maxx//2
firstj=maxy//2
firstk=maxz//2
网格[firsti+nrofx*(firstj+nrofy*firstk)]=firstpos
活动。附加(firstpos)
当(len(活动)>0时:
randindex=数学地板(均匀(0,透镜(活动)))
pos=活动[randindex]
发现=错误
对于在范围(k)内的尝试:
偏移量x=均匀(mindis,2*mindis)
偏移量=均匀(mindis,2*mindis)
偏移量Z=均匀(mindis,2*mindis)
samplex=offsetx*random.choice([1,-1])
sampley=offsety*random.choice([1,-1])
samplez=offsetz*random.choice([1,-1])
sample=numpy.array((samplex、sampley、samplez))
样本=numpy.add(样本,位置)
xcoor=数学楼层(样本项(0)/单元大小)
ycoor=数学楼层(样本项目(1)/单元尺寸)
zcoor=数学楼层(样本项目(2)/单元尺寸)
尝试索引=xcoor+nrofx*(ycoor+nrofy*zcoor)
如果attemptindex>=0且attemptindex0且ycoor>0且zcoor>0:
测试=真
对于范围(-1,2)内的testx:
对于范围(-1,2)内的testy:
对于范围(-1,2)内的testz:
testindex=(xcoor+testx)+nrofx*((ycoor+testy)+nrofy*(zcoor+testz))
如果testindex>=0且testindexmindis:
测试=错误

如果测试==真且len(激活)您好,欢迎使用SO。链接缺失,您应该在问题正文中,而不是链接中包含对问题至关重要的信息。此外,无需道歉,我们都在这里学习。您使用的是什么IDE?几乎任何IDE都会让您逐步完成代码,并在每一步识别每个变量的值,这是非常重要的调试程序需要做什么。不要假设你在internet上找到的代码是正确的。嗨,我不是100%确定IDE的意思,但我认为它意味着我运行代码的地方?如果是,我将PyCharm与项目解释器Python 3.6.1一起使用。链接是[和[代码创建者在本视频中运行代码:[所以我认为这应该是我的问题。谢谢你的评论:)。