I think you could save some computing time by computing first the weight, for example `w = (1. - di) * dj * ds` and then reuse `w` and `w**2` for updating `weight_norm` and `ray_sum`