广度优先树生成问题

发布于 2024-08-19 10:01:14 字数 1302 浏览 2 评论 0原文

我的广度优先算法有问题,我的脚本在 Maya 中生成曲线,定位它们,旋转和缩放它们,以便它们给我树的形状, 我有这些变量

cs=当前状态,

p=父节点,

节点=未访问节点列表

lvl=当前深度

maxlvl=最大深度

问题是我无法确定当前深度,并在那里终止树, 它存在而没有访问所有节点

,这是我的脚本,

import random
import math
import maya.cmds as mc
#----define set-----
#--- curve, #children,angle,l--------
#----define rule----
#    l=80%   ch<=3   angle<=45
l=0.8
ch=6
ang=50
#---list to track----
nodes=[]
#--- begin-----
#---start curve-----
nodes.append(mc.curve(n="a",d=3,p=((0,0,0),(0,1,0),(0,3,0),(0,5,0),(0,6,0)),k=(0,0,0,1,2,2,2)))
cs="" #---current state----
p="a" #---paretn of the current state----
maxlvl=6 #--max depth for the tree
lvl=1 #-- cuurent level----
while (len(nodes)!=0 and lvl<5):
 #---generate children number----
 chN=random.randint(0,ch)
 for j in range(chN):
  #----generate node----
  mc.select(p,r=1)
  cs=mc.duplicate(rr=1)[0]
  #---append to node lst----
  nodes.append(cs)
  #---adjust pos---
  pos=mc.pointPosition(p+".cv[4]")
  mc.xform(cs,t=(pos[0],pos[1],pos[2]),ws=1)
  #---adjust rotation---
  mc.rotate(0,0,(random.random()*45+5)*math.pow(-1,j%2),cs,r=1)
  #---adjust scale---
  mc.scale(0,math.pow(0.8,lvl),0,cs)
  j+=1
 #--- go to next parent---
 nodes.pop(0)
 if nodes:
  p=nodes[0]
 lvl+=1

提前谢谢您

i have a problem with breadth first algorithm, my script generates curves in maya, position them, rotate and scale them so they give me the tree shape,
i have these variables

cs=current State,

p=parent,

nodes=non visited node List

lvl=current depth

maxlvl= max depth

the problem is that i cant determine current depth, and terminate the tree there,
it exists without all nodes being visited

here is my script

import random
import math
import maya.cmds as mc
#----define set-----
#--- curve, #children,angle,l--------
#----define rule----
#    l=80%   ch<=3   angle<=45
l=0.8
ch=6
ang=50
#---list to track----
nodes=[]
#--- begin-----
#---start curve-----
nodes.append(mc.curve(n="a",d=3,p=((0,0,0),(0,1,0),(0,3,0),(0,5,0),(0,6,0)),k=(0,0,0,1,2,2,2)))
cs="" #---current state----
p="a" #---paretn of the current state----
maxlvl=6 #--max depth for the tree
lvl=1 #-- cuurent level----
while (len(nodes)!=0 and lvl<5):
 #---generate children number----
 chN=random.randint(0,ch)
 for j in range(chN):
  #----generate node----
  mc.select(p,r=1)
  cs=mc.duplicate(rr=1)[0]
  #---append to node lst----
  nodes.append(cs)
  #---adjust pos---
  pos=mc.pointPosition(p+".cv[4]")
  mc.xform(cs,t=(pos[0],pos[1],pos[2]),ws=1)
  #---adjust rotation---
  mc.rotate(0,0,(random.random()*45+5)*math.pow(-1,j%2),cs,r=1)
  #---adjust scale---
  mc.scale(0,math.pow(0.8,lvl),0,cs)
  j+=1
 #--- go to next parent---
 nodes.pop(0)
 if nodes:
  p=nodes[0]
 lvl+=1

thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

将军与妓 2024-08-26 10:01:14

您需要将深度与每个节点相关联。要么将其作为节点类中的成员,要么让队列条目同时存储深度和节点,如下所示:

nodes.append((1, firstNode))
while nodes and lvl<5:
    lvl, p = nodes.pop(0)

    For each child:
        #...create child
        nodes.append((lvl+1, cs))

一些不相关的注释:

  • 使用collections.deque而不是list:对于 FIFO 队列来说更快。

  • for 循环中不需要 j+=1 语句。

You need to associate the depth with each node. Either put it as a member in the node class, or have your queue entries store both depth and node like this:

nodes.append((1, firstNode))
while nodes and lvl<5:
    lvl, p = nodes.pop(0)

    For each child:
        #...create child
        nodes.append((lvl+1, cs))

Some unrelated notes:

  • Use collections.deque instead of list: It's faster for FIFO queues.

  • The j+=1 statement is unnecessary in the for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文