Sami
Sami

Reputation: 1

3D grid interpolation in Python

I have a regularly sampled grid, x,y,z and variable v. I would like to interpolate based on given x,y, &z values. I do not want to use numpy or scipy, because they are not installed on the platform I will be running on.

If I can locate the surrounding 8 grid nodes what math can I use to interpolate?

Thanks

Upvotes: 0

Views: 2274

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 601451

This actually seems to be a math question, but you also mentioned Python, so I try to give some code. You would usually use trilinear interpolation.

So we assume your grid has the corners (0.0, 0.0, 0.0) and (max_x, max_y, max_z) and is aligned with the coordinate system. We denote the number of cells along each axis by (n_x, n_y, n_z) respectively and the point you wish to evaluate at by (x, y, z) (all of type float). Then your logic might be something similar to

a_x = x * n_x / max_x
a_y = y * n_y / max_y
a_z = z * n_z / max_z
i_x = math.floor(a_x)
i_y = math.floor(a_y)
i_z = math.floor(a_z)
l_x = a_x - i_x
l_y = a_y - i_y
l_z = a_z - i_z

The indices of the 8 adjacent grid vertices now are (i_x, i_y, i_z), (i_x+1, i_y, i_z), (i_x, i_y+1, i_z), ..., (i_x+1, i_y+1, i_z+1). The local coordinates of your point within the grid cell are (l_x, l_y, l_z). Together with the linked Wikipedia article, this should get you going (note that the notation is different there).

Upvotes: 2

Related Questions