def bilerp(f: ti.template(), pos): p = float(pos) I = int(ti.floor(p)) x = p - I y = 1 - x ti.static_assert(len(f.meta.shape) == 2) return (f[I + V(1, 1)] * x[0] * x[1] + f[I + V(1, 0)] * x[0] * y[1] + f[I + V(0, 0)] * y[0] * y[1] + f[I + V(0, 1)] * y[0] * x[1])
def clamp_index( I : Index , size : Vector) -> Index: # dim = ti.static(len(I.shape)) # index = [] # for i in range(dim): # index.append(max(0,min(I[i] , size[i]))) # return index ti.static_assert(len(size.shape) == 2 and len(I.shape) == 2) i = max(0,min(int(I[0]) ,size[0] -1)) j = max(0,min(int(I[1]) ,size[1] -1)) return ti.Vector([i,j])
def v4trans(mat, vec, wei): ti.static_assert(vec.n == 3, vec.n) if ti.static(vec.m == 1): return (mat @ ts.vec4(vec, wei)).xyz tmp = ti.Matrix.zero(float, 4, vec.m) for i, j in ti.static(ti.ndrange(vec.n, vec.m)): tmp[i, j] = vec[i, j] for i in ti.static(range(vec.m)): tmp[3, i] = wei tmp = mat @ tmp ret = ti.Matrix.zero(float, vec.n, vec.m) for i, j in ti.static(ti.ndrange(vec.n, vec.m)): ret[i, j] = tmp[i, j] return ret
def render(self): self.src.render() for I in ti.grouped(self.img): if ti.static(self.attr == 'mid'): mid = self.src['mid'][I] color = ts.vec3(abs(mid == 1 or mid == 4 or mid == 6), abs(mid == 2 or mid == 4 or mid == 5), abs(mid == 3 or mid == 5 or mid == 6)) self.img[I] = color elif ti.static(self.attr == 'normal'): normal = self.src['normal'][I] self.img[I] = normal * 0.5 + 0.5 elif ti.static(self.attr == 'tangent'): tangent = self.src['tangent'][I] self.img[I] = tangent * 0.5 + 0.5 elif ti.static(self.attr == 'position'): position = self.src['position'][I] self.img[I] = position * 0.5 + 0.5 elif ti.static(self.attr == 'texcoord'): texcoord = self.src['texcoord'][I] self.img[I] = ts.vec3(texcoord, 0.0) else: ti.static_assert(0, self.attr)
def vector_to_fast_image(img: ti.template(), out: ti.ext_arr()): # FIXME: Why is ``for i, j in img:`` slower than: for i, j in ti.ndrange(*img.shape): r, g, b = 0, 0, 0 color = img[i, img.shape[1] - 1 - j] if ti.static(img.dtype in [ti.f32, ti.f64]): r, g, b = min(255, max(0, int(color * 255))) else: ti.static_assert(img.dtype == ti.u8) r, g, b = color idx = j * img.shape[0] + i # We use i32 for |out| since OpenGL and Metal doesn't support u8 types if ti.static(ti.get_os_name() != 'osx'): out[idx] = (r << 16) + (g << 8) + b else: # What's -16777216? # # On Mac, we need to set the alpha channel to 0xff. Since Mac's GUI # is big-endian, the color is stored in ABGR order, and we need to # add 0xff000000, which is -16777216 in I32's legit range. (Albeit # the clarity, adding 0xff000000 doesn't work.) alpha = -16777216 out[idx] = (b << 16) + (g << 8) + r + alpha
def foo(): ti.static_assert(a == 32)
def func(): ti.static_assert(a == 32) foo()
def foo(): value = False ti.static_assert(value, "Oh, no!")
def func(): ti.static_assert(x.dtype == ti.f32)
def func(): ti.static_assert(x.n == 4)
def func(): ti.static_assert(x == 4, "Oh, no!")
def func(): x = 0 ti.static_assert(x) # Expr is not None
def image_at(self, i, j): ti.static_assert(len(self.img.meta.shape) == 2) scale = ti.Vector(self.img.meta.shape) / ti.Vector(self.res) pos = ti.Vector([i, j]) * scale r, g, b = self._cook(bilerp(self.img, pos)) return int(r), int(g), int(b)
def func3(): ti.static_assert(1 + 1 == 3)