I am trying to understand the interpolation technique that someone else has implemented at work. Since I can't ask him, I posted the question here.
So I have the following scenario. I am trying to linearly interpolate between two lines in 3 dimensions. The points are part of the so called consumption map and we want to compute the electrical power, given by the coordinate $y$. The first line connects the points $p_1 = (x_1, y_1, \hat{z})$ and $p_2 = (x_2, y_2, \hat{z})$. And the second line connects the points $p_3 = (x_3, y_3, \bar{z})$ and $p_4 = (x_4, y_4, \bar{z})$.
In the code this is done by holding $x \in [x_{\text{min}}, x_{\text{max}}]$ and $z \in [\hat{z}, \bar{z}]$ constant and applying \begin{equation} y = \hat{y} + \frac{y_2-y_1}{x_2-x_1}(x_{\text{min}}+x) + z_{\text{slope}}(\hat{z} +z). \end{equation} where $x_{\text{min}}$ is the minimum between $x_1$ and $x_3$ and $x_{\text{max}}$ is the maximum between $x_2$ and $x_4$.
$\hat{y}$ is the $y$ coordinate corresponding to $x_{\text{min}}$ in the line that goes through $p_1$ and $p_2$, i.e. either $y_1$ or \begin{equation} \frac{y_2-y_1}{x_2-x_1}(x_{\text{min}}-x_1)+y_1. \end{equation}
Last, $z_{\text{slope}}$ chooses the steepest slope between \begin{align} \text{slopeA} &= \frac{y_3-y_A}{\bar{z}- \hat{z}} \quad \text{where} \quad y_A= \frac{y_2-y_1}{x_2-x_1}(x_3-x_1)+y_1 \quad\text{and}\\ \text{slopeB} &= \frac{y_4-y_B}{\bar{z}- \hat{z}} \quad \text{where} \quad y_B= \frac{y_2-y_1}{x_2-x_1}(x_4-x_1)+y_1. \end{align}
Now if I did this myself, I would just define a triangulation and use barycentric coordinates.
I am very confused because I tried both of these techniques and the interpolated values are very different.
Can anyone tell me if this is correct or if anyone recognizes what technique has been used here?
Are you looking for bilinear interpolation?
Let's say you have a function $f(u, v)$ that you wish to interpolate.
Point $\vec{p}_0$ corresponds to $f(0, 0)$, point $\vec{p}_1$ to $f(1,0)$, point $\vec{p}_2$ to $f(0,1)$, and point $\vec{p}_3$ to $f(1,1)$.
Then, $(u, v)$ (and the value $f(u,v)$) corresponds to point $\vec{p}$, $$\begin{aligned} \vec{p} &= (1-u)(1-v)\vec{p}_0 + u (1-v)\vec{p}_1 + (1-u) v \vec{p}_2 + u v \vec{p}_3 \\ ~ &= \bigl(\vec{p}_3 - \vec{p}_2 - \vec{p}_1 + \vec{p}_0 \bigr) u v + \bigl( \vec{p}_1 - \vec{p}_0 \bigr) u + \bigl( \vec{p}_2 - \vec{p}_0 \bigr) v + \vec{p}_0 \\ \end{aligned}$$
The inverse is straightforward, except that since we only have two unknowns, we need to drop one of the dimensions out: $$\left\lbrace ~ \begin{aligned} x &= (1-u) (1-v) x_0 + u (1-v) x_1 + (1-u) v x_2 + u v x_3 \\ y &= (1-u) (1-v) y_0 + u (1-v) y_1 + (1-u) v y_2 + u v y_3 \\ z &= (1-u) (1-v) z_0 + u (1-v) z_1 + (1-u) v z_2 + u v z_3 \\ \end{aligned} \right .$$ It is best to drop the dimension with the smallest difference between the minimum and maximum coordinates among the four points; i.e., the smallest dimension considering the axis-aligned bounding box for the quadrangle.
In other words, drop the least interesting dimension, and solve the above for $u$ and $v$. Because quadrangle is not the 2D simplex (triangle is), this generally won't be a linear mapping, and you'll typically get a pair of results. (Pick the one that yields both $0 \le u \le 1$ and $0 \le v \le 1$.)
For triangles, use barycentric coordinates $(u, v, w)$, where $w = 1 - u - v$ (so that $u + v + w = 1$); usually $w$ is dropped from explicit use.
Define two base vectors: $$\begin{aligned} \vec{e}_u &= \vec{p}_1 - \vec{p}_0 \\ \vec{e}_v &= \vec{p}_2 - \vec{p}_0 \\ \end{aligned}$$ Then, point $\vec{p}$ is $$\begin{aligned} \vec{p} &= \vec{p}_0 + u \vec{e}_u + v \vec{e}_v \\ ~ &= w \vec{p}_0 + u \vec{p}_1 + v \vec{p}_2 \\ ~ &= \vec{p}_0 + u (\vec{p}_1 - \vec{p}_0) + v (\vec{p}_2 - \vec{p}_0) \\ \end{aligned}$$ and the inverse is now also linear. For example, dropping $z$ as the least useful dimension, and using $\vec{e}_u = (x_u, y_u, z_u)$, $\vec{e}_v = (x_v, y_v, z_v)$, you get $$\left\lbrace \begin{aligned} d &= x_u y_v - x_v y_u \\ u &= \frac{(x - x_0) y_v - (y - y_0) x_v }{d} \\ ~ &= x \frac{y_v}{d} + y \frac{-x_v}{d} + \frac{y_0 x_v - x_0 y_v}{d} \\ v &= \frac{(y - y_0) x_u - (x - x_0) y_u }{d} \\ ~ &= x \frac{-y_u}{d} + y \frac{x_u}{d} + \frac{x_0 y_u - y_0 x_u}{d} \\ \end{aligned} \right .$$ which shows that in the general case, and especially a computer program, you can use $$\left\lbrace ~ \begin{aligned} u &= \vec{p} \cdot \vec{s}_u + C_u \\ v &= \vec{p} \cdot \vec{s}_v + C_v \\ \end{aligned} \right .$$ where the four constants (eight scalars) only depend on the three vectors $\vec{p}_0$, $\vec{p}_1$, and $\vec{p}_2$, and can be precalculated; noting that one (the dropped coordinate) component of $\vec{s}_u$ and $\vec{s}_v$ will be zero.