坐标偏移

  • Taichi 张量支持 坐标偏移(coordinate offsets) 的定义方式。偏移量会移动张量的边界,使得张量的原点不再是零向量。一个典型的例子是在物理模拟中支持负坐标的体素。
  • 例如,一个大小为 32x64 、起始元素坐标偏移为 (-16, 8) 的矩阵可以按照以下形式来定义:
a = ti.Matrix(2, 2, dt=ti.f32, shape=(32, 64), offset=(-16, 8))

通过这样,张量的下标就是从 (-16, 8)(16, 72) 了(半开半闭区间).

a[-16, 32]  # 左下角
a[16, 32]   # 右下角
a[-16, 64]  # 左上角
a[16, 64]   # 右上角

注解

The dimensionality of tensor shapes should be consistent with that of the offset. Otherwise, a AssertionError will be raised.

a = ti.Matrix(2, 3, dt=ti.f32, shape=(32,), offset=(-16, ))          # 有效!
b = ti.Vector(3, dt=ti.f32, shape=(16, 32, 64), offset=(7, 3, -4))   # 有效!
c = ti.Matrix(2, 1, dt=ti.f32, shape=None, offset=(32,))             # 断言错误
d = ti.Matrix(3, 2, dt=ti.f32, shape=(32, 32), offset=(-16, ))       # 断言错误
e = ti.var(dt=ti.i32, shape=16, offset=-16)                          # 有效!
f = ti.var(dt=ti.i32, shape=None, offset=-16)                        # 断言错误
g = ti.var(dt=ti.i32, shape=(16, 32), offset=-16)                    # 断言错误