在python上生成泊松球体,但无法确定错误在哪里

2024-09-29 21:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉编程,所以我希望我的愚蠢问题不要打扰你。 我现在正试图用python计算泊松球分布(poisson磁盘的3D版本),然后将结果插入POV-RAY,这样我就可以生成一些随机分布的填充岩。 我遵循以下两个链接: [https://github.com/CodingTrain/Rainbow-Code/blob/master/CodingChallenges/CC_33_poisson_disc/sketch.js#L13] [https://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf] 太长,读不下去了 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))

我的代码似乎永远运行。 因此,我尝试在while循环的最后一个部分添加print(len(active))。 令人惊讶的是,我发现了这个bug,因为活动列表的长度一直在增加!(它应该和网格一样长)我认为问题是由活动.append(),但我不知道问题出在哪里,因为代码与希夫曼先生. 我不想搭便车,但我已经检查了一次又一次,同时纠正了一次又一次的代码:(。不过,我不知道窃听器在哪里。(为什么活动的[]继续追加!?) 谢谢你宝贵的时间。在


Tags: andsamplenumpyforlenifmathuniform

热门问题