#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是两个维度,我不知道有没有更好的做法。