拒绝采样以及维度无关

#only supports for two dimension
@ti.kernel
def calc_points_udf(sample_coords: ti.types.ndarray(),labels:ti.types.ndarray(),epsilon:ti.float32):
    for i in range(sample_coords.shape[0]):
        while True:
            sample_coord=[ti.random(ti.f32) for j in range(2)] #hard-coded 2 for now because there is no better solution
            udf=unsigned_distance_two_bubbles(ti.Vector(sample_coord))
            if(udf<epsilon):
                labels[i,0]=udf
                sample_coords[i,0]=sample_coord[0] #hard-coded indices 0 and 1 now because there us no better solution
                sample_coords[i,1]=sample_coord[1]
                break

这个函数描述了采样一个随机2D坐标的udf, 如果udf小于epsilon 则接受并把输入的sample_coords的对应元素覆盖,反之则生成一个新的随机坐标。
有两个问题:
一是这里这个拒绝采样有没有什么更好的办法

二是如果想让这个代码也支持3d的话应该怎么操作?sample_coords.shape[1] 描述坐标的维度。
根据太极kernel的语法语法,sample_coord=[ti.random(ti.f32) for j in range(2)] ,我并不能直接把range(2)改成 ‘range(sample_coords.shape[1])’ 而我希望能这样做。
现在 sample_coords[i,0]=sample_coord[0] sample_coords[i,1]=sample_coord[1] 这里也是写死的,其中的0和1是两个维度,我不知道有没有更好的做法。

hello,可以详细描述一下你的问题么?大家不太容易从你现在的问题看出你想做什么。

我改了一下,你看看现在是否更清楚一些呢。谢谢。

sample_coord=[ti.random(ti.f32) for j in range(3)]

应该可以给出 3 维的坐标?

ti.types.ndarray() 支持向量类型作为 dtype,高维的点可能需要用类似
ti.types.ndarray(dtype=ti.types.vector(n, float)) 这样的语法
这样在 kernel 里面你可以用 sample_coords.dtype.n 拿到这个维数

1 个赞