# taichi1.4.1里有双线性插值和三线性插值函数吗？还是说要自己定义这种函数。

taichi1.4.1里有双线性插值和三线性插值函数吗？还是说要自己定义这种函数。

``````@ti.func
def bilinear_interpolate(val:ti.template(), x: ti.f32, y: ti.f32)->ti.f32:
'''
Bilinear interpolation of a 2D scalar field

Args:
val (ti.template()): 2D scalar field
x (ti.f32): x coordinate of the point
y (ti.f32): y coordinate of the point

Returns:
ti.f32: interpolated value
'''
shape = val.shape
i = int(x)
j = int(y)
if i < 0 or i >= shape[0]-1 or j < 0 or j >= shape[1]-1:
return 0.0
s = x - i
t = y - j
return (1-s)*(1-t)*val[i, j] + s*(1-t)*val[i+1, j] + (1-s)*t*val[i, j+1] + s*t*val[i+1, j+1]

@ti.func
def trilinear_interpolate(val:ti.template(), x: ti.f32, y: ti.f32, z: ti.f32)->ti.f32:
'''
Trilinear interpolation of a 3D scalar field

Args:
val (ti.template()): 3D scalar field
x (ti.f32): x coordinate of the point
y (ti.f32): y coordinate of the point
z (ti.f32): z coordinate of the point

Returns:
ti.f32: interpolated value
'''
shape = val.shape
i = int(x)
j = int(y)
k = int(z)
if i < 0 or i >= shape[0]-1 or j < 0 or j >= shape[1]-1 or k < 0 or k >= shape[2]-1:
return 0.0
s = x - i
t = y - j
u = z - k
return (1-s)*(1-t)*(1-u)*val[i, j, k] + s*(1-t)*(1-u)*val[i+1, j, k] + (1-s)*t*(1-u)*val[i, j+1, k] + s*t*(1-u)*val[i+1, j+1, k] + (1-s)*(1-t)*u*val[i, j, k+1] + s*(1-t)*u*val[i+1, j, k+1] + (1-s)*t*u*val[i, j+1, k+1] + s*t*u*val[i+1, j+1, k+1]
``````

3 Likes