# B-Spline Volumes

B-Spline Volumes are a simple extension of B-Splines to 3 Dimensions. This is
a straightforward adaption of the 2-Dimensional version.

## Nomenclature

$x,y,z$ denote space-coordinates,  
$u,v,w$ denote spline-coordinates (Between 0-1),  
$P_{ijk}$ denote the control-Points on the control-Polygon,  
$N_{i,d,\tau}(u)$ denote the value of the underlying Basis-Functions at value
$u$ using the $i$-th Basis-Function of degree $d$ in range $\tau$.

For our case we only care about degree-3 splines, so we omit the d furtheron.
$\tau$ is defined statically (in each direction) with each $P$ as Position on the whole surface/volume and within [0,1].
For a regular Control-Grid this defaults to $\tau_i = i/n$

Given $n,m,o$ control points in $x,y,z$-direction each Point on the curve is
defined by

$$C(u,v,w) = \sum_{i=0}^{n-d-2} \sum_{j=0}^{m-d-2} \sum_{k=0}^{o-d-2} P_{ijk} N_{i}(u) N_j(v) N_k(w)$$

## Calculate $u, v, w$

Given a target-point $\textbf{p}^*$ and an initial guess $\textbf{p}=C(u,v,w)$ we
define the error-function as:

$$Err(u,v,w,\textbf{p}^{*}) = \textbf{p}^{*} - C(u,v,w)$$


$$Err_x(u,v,w,\textbf{p}^{*}) = p^{*}_x - \sum_{i=0}^{n-d-2} \sum_{j=0}^{m-d-2} \sum_{k=0}^{o-d-2} {P_{ijk}}_x N_{i}(u) N_j(v) N_k(w) $$

To solve this we derive:

$$
\begin{array}{rl}
        \displaystyle \frac{\partial Err_x}{\partial u} & p^{*}_x - \displaystyle \sum_{i=0}^{n-d-2} \sum_{j=0}^{m-d-2} \sum_{k=0}^{o-d-2} {P_{ijk}}_x N_{i}(u) N_j(v) N_k(w) \\
                                  = & \displaystyle - \sum_{i=0}^{n-d-2} \sum_{j=0}^{m-d-2} \sum_{k=0}^{o-d-2} {P_{ijk}}_x N'_{i}(u) N_j(v) N_k(w)
\end{array}
$$

The other partial derivatives follow the same pattern yiedling the Jacobian:

$$
        J(Err(u,v,w)) = 
        \left(
        \begin{array}{ccc}
        \frac{\partial Err_x}{\partial u} & \frac{\partial Err_x}{\partial v} & \frac{\partial Err_x}{\partial w} \\
        \frac{\partial Err_y}{\partial u} & \frac{\partial Err_y}{\partial v} & \frac{\partial Err_y}{\partial w} \\
        \frac{\partial Err_z}{\partial u} & \frac{\partial Err_z}{\partial v} & \frac{\partial Err_z}{\partial w}
        \end{array}
        \right)
$$

Iterate with

$$J(Err(u,v,w)) \cdot \Delta \left( \begin{array}{c} u \\ v \\ w \end{array} \right) = -Err(u,v,w)$$

using Cramers rule for solving the SLE.

## Basis-Splines and Derivatives

The previously mentioned $N_{i,d,\tau}$ are defined recursively:

$$N_{i,0,\tau}(u) = \begin{cases} 1, & u \in [\tau_i, \tau_{i+1}[ \\ 0, & \mbox{otherwise} \end{cases} $$

and

$$N_{i,d,\tau}(u) = \frac{u-\tau_i}{\tau_{i+d}} N_{i,d-1,\tau}(u) + \frac{\tau_{i+d+1} - u}{\tau_{i+d+1}-\tau_{i+1}} N_{i+1,d-1,\tau}(u) $$

This fact can be exploited to get the derivative for an arbitrary $N$:

$$\frac{d}{du} N_{i,d,r}(u) = \frac{d}{\tau_{i+d} - \tau_i} N_{i,d-1,\tau}(u) - \frac{d}{\tau_{i+d+1} - \tau_{i+1}} N_{i+1,d-1,\tau}(u)$$

*Warning:* in the case of $d=1$ the recursion-formula yields a $0$ denominator, but $N$ is also $0$. The right solution for this case is a derivative of $0$
