求问,在太极作用域中,如何遍历一个以自定义的太极类对象为元素的List(或者有其它方式保存这种对象为数组结构的方式)?
Hi, @shihuan, 欢迎来到太极论坛。
关于List的问题,可以推荐你先看这里:here。
其次,用一个List来保存太极类对象,我们推荐使用struct field。 还有就是多个对象统一用一个类,每个对象的数据统一放在一个field里面,有点类似stuct of array (SOA)的方式。
谢谢回复,我大致明白你的意思,用struct代替自定义类,然后struct的shape就是‘list’的长度,然后去遍历;但是这对struct中key一致的情况比较合适吧,对于面向对象中父类派生的多个子类组成的list合适吗?每个子类的成员变量都不一样吗,,比如做Raytracing(The Rest of Your Life)中场景中模型类的基类hittable派生的各子类,球体Sphere,立方体Box等,用struct也不合适吧?
另外关于taichi中python的List,之前我也在论坛中别的地方看到说不能用,我自己测试了下也可以这样用的,只是遍历的时候要报错
import taichi as ti
ti.init(arch=ti.gpu)
@ti.data_oriented
class foo:
nor=None
def __init__(self, *n ):
# print(str(len(n)))
self.nor=n
@ti.func
def run(self, n ):
print("this is foo obj")
print(n)
return 123
@ti.data_oriented
class foo2:
alist=None
def __init__(self, *n ):
# print(str(len(n)))
self.alist=[]
def add(self, n ):
# print(33333)
# print(self.nor)
# bar()
self.alist.append(n)
return 123
@ti.func
def run(self):
#这种方式可以访问对象list的元素
self.alist[0].run(3434)
self.alist[1].run(2222)
#这种方式遍历就不行
# for i in self.alist:
# self.alist[i].run(3434)
return 123
@ti.func
def bar(aaa) :
aaa.run()
@ti.kernel
def doStr():
bar( xx)
cacc = ti.Vector([1, 23, 4])
f1 = foo(cacc,2)
f2 = foo(cacc)
xx=foo2()
xx.add(f1)
xx.add(f2)
doStr()