1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
pub mod kernel;

pub use kernel::*;
use na::storage::Storage;
use na::{
    DMatrix, DVector, Matrix3, Matrix3x4, Matrix4, Point3, RealField, Vector, Vector3, Vector4, U1,
    U3, U4,
};
use num_traits::{Float, Zero};

/// Floating point real trait used throughout this library.
pub trait Real: Float + RealField + std::fmt::Debug {}
impl<T> Real for T where T: Float + RealField + std::fmt::Debug {}

/// Shorthand for an HRBF with a constant `x^3` kernel.
pub type Pow3HRBF<T> = HRBF<T, kernel::Pow3<T>>;
/// Shorthand for an HRBF with a constant `x^5` kernel.
pub type Pow5HRBF<T> = HRBF<T, kernel::Pow5<T>>;
/// Shorthand for an HRBF with a constant Gaussian `exp(-x*x)` kernel.
pub type GaussHRBF<T> = HRBF<T, kernel::Gauss<T>>;
/// Shorthand for an HRBF with a constant CSRBF(3,1) `(1-x)^4 (4x+1)` kernel of type.
pub type Csrbf31HRBF<T> = HRBF<T, kernel::Csrbf31<T>>;
/// Shorthand for an HRBF with a constant CSRBF(4,1) `(1-x)^6 (35x^2 + 18x + 3)` kernel of type.
pub type Csrbf42HRBF<T> = HRBF<T, kernel::Csrbf42<T>>;

/// HRBF specific kernel type. In general, we can assign a unique kernel to each HRBF site, or we
/// can use the same kernel for all points. This corresponds to Variable and Constant kernel types
/// respectively.
#[derive(Clone, Debug)]
pub enum KernelType<K> {
    Variable(Vec<K>), // each site has its own kernel
    Constant(K),      // same kernel for all sites
}

impl<K> ::std::ops::Index<usize> for KernelType<K> {
    type Output = K;

    fn index(&self, index: usize) -> &K {
        match *self {
            KernelType::Variable(ref ks) => &ks[index],
            KernelType::Constant(ref k) => k,
        }
    }
}

#[derive(Clone, Debug)]
pub struct HRBF<T, K>
where
    T: Real,
    K: Kernel<T>,
{
    sites: Vec<Point3<T>>,
    betas: Vec<Vector4<T>>,
    kernel: KernelType<K>,
}

/// HRBF public interface. It is not necessary to use this trait, but it allows using the HRBF as a
/// trait object. For example this is used to pass HRBF objects between functions in the C API.
pub trait HRBFTrait<T: Real> {
    fn fit(&mut self, points: &[Point3<T>], normals: &[Vector3<T>]) -> bool;
    fn fit_offset(&mut self, points: &[Point3<T>], offsets: &[T], normals: &[Vector3<T>]) -> bool;
    fn fit_system(
        &self,
        points: &[Point3<T>],
        potential: &[T],
        normals: &[Vector3<T>],
    ) -> (DMatrix<T>, DVector<T>);
    fn eval(&self, p: Point3<T>) -> T;
    fn grad(&self, p: Point3<T>) -> Vector3<T>;
    fn hess(&self, p: Point3<T>) -> Matrix3<T>;
}

impl<T, K> HRBFTrait<T> for HRBF<T, K>
where
    T: Real,
    K: Kernel<T> + Default,
{
    fn fit(&mut self, points: &[Point3<T>], normals: &[Vector3<T>]) -> bool {
        HRBF::fit(self, points, normals)
    }
    fn fit_offset(&mut self, points: &[Point3<T>], offsets: &[T], normals: &[Vector3<T>]) -> bool {
        HRBF::fit_offset(self, points, offsets, normals)
    }
    fn fit_system(
        &self,
        points: &[Point3<T>],
        potential: &[T],
        normals: &[Vector3<T>],
    ) -> (DMatrix<T>, DVector<T>) {
        HRBF::fit_system(self, points, potential, normals)
    }
    fn eval(&self, p: Point3<T>) -> T {
        HRBF::eval(self, p)
    }
    fn grad(&self, p: Point3<T>) -> Vector3<T> {
        HRBF::grad(self, p)
    }
    fn hess(&self, p: Point3<T>) -> Matrix3<T> {
        HRBF::hess(self, p)
    }
}

impl<T, K> Default for HRBF<T, K>
where
    T: Real,
    K: Kernel<T> + Default,
{
    fn default() -> Self {
        HRBF {
            sites: Vec::new(),
            betas: Vec::new(),
            kernel: KernelType::Constant(K::default()),
        }
    }
}

impl<T, K> HRBF<T, K>
where
    T: Real,
    K: Kernel<T> + LocalKernel<T>,
{
    pub fn radius(mut self, radius: T) -> Self {
        self.kernel = KernelType::Constant(K::new(radius));
        self
    }

    pub fn radii(mut self, radii: Vec<T>) -> Self {
        self.kernel = KernelType::Variable(radii.into_iter().map(|r| K::new(r)).collect());
        self
    }
}

impl<T, K> HRBF<T, K>
where
    T: Real,
    K: Kernel<T> + Default,
{
    /// Main constructor. Assigns the degrees of freedom used by this HRBF in a form of 3D points
    /// at which the kernel will be evaluated.
    pub fn new(sites: Vec<Point3<T>>) -> Self {
        HRBF {
            sites,
            ..HRBF::default()
        }
    }

    /// Builder that assigns a particular kernel to this HRBF.
    pub fn with_kernel(self, kernel: KernelType<K>) -> Self {
        HRBF { kernel, ..self }
    }

    /// Returns a reference to the vector of site locations used by this HRBF.
    pub fn sites(&self) -> &Vec<Point3<T>> {
        &self.sites
    }

    /// Advanced. Returns a reference to the vector of 4D weight vectors, which determine the
    /// global HRBF potential. These are the unknowns computed during fitting.
    /// Each 4D vector has the structure `[aⱼ; bⱼ]` per site `j` where `a` is a scalar weighing
    /// the contribution from the kernel at site `j` and b is a 3D vector weighin the contribution
    /// from the kernel gradient at site `j` to the total HRBF potential.
    pub fn betas(&self) -> &Vec<Vector4<T>> {
        &self.betas
    }

    /// Fit the current HRBF to the given data. Return true if successful.
    /// NOTE: Currently, points must be the same size as as sites.
    #[allow(non_snake_case)]
    pub fn fit(&mut self, points: &[Point3<T>], normals: &[Vector3<T>]) -> bool {
        assert!(normals.len() == points.len());
        let num_sites = self.sites.len();

        let mut potential = Vec::new();
        potential.resize(num_sites, T::zero());
        let (A, b) = self.fit_system(points, &potential, normals);

        self.betas.clear();
        if let Some(x) = A.lu().solve(&b) {
            assert!(x.len() == 4 * num_sites);

            self.betas.resize(num_sites, Vector4::zero());
            for j in 0..num_sites {
                self.betas[j].copy_from(&x.fixed_rows::<U4>(4 * j));
            }

            true
        } else {
            false
        }
    }

    /// Fit the current HRBF to the given data. The resulting HRBF field is equal to `offsets`
    /// and has a gradient equal to `normals`.
    /// Return true if successful.
    /// NOTE: Currently, points must be the same size as as sites.
    #[allow(non_snake_case)]
    pub fn fit_offset(
        &mut self,
        points: &[Point3<T>],
        offsets: &[T],
        normals: &[Vector3<T>],
    ) -> bool {
        assert!(normals.len() == points.len());
        let num_sites = self.sites.len();

        let (A, b) = self.fit_system(points, offsets, normals);

        self.betas.clear();
        if let Some(x) = A.lu().solve(&b) {
            assert!(x.len() == 4 * num_sites);

            self.betas.resize(num_sites, Vector4::zero());
            for j in 0..num_sites {
                self.betas[j].copy_from(&x.fixed_rows::<U4>(4 * j));
            }

            true
        } else {
            false
        }
    }

    /// Advanced. Returns the fitting matrix `A` and corresponding right-hand-side `b`.
    /// `b` is a stacked vector of 4D vectors representing the desired HRBF potential
    /// and normal at data point `i`, so `A.inverse()*b` gives the `betas` (or weights)
    /// defining the HRBF potential.
    #[allow(non_snake_case)]
    pub fn fit_system(
        &self,
        points: &[Point3<T>],
        potential: &[T],
        normals: &[Vector3<T>],
    ) -> (DMatrix<T>, DVector<T>) {
        assert!(normals.len() == points.len());
        assert!(potential.len() == points.len());
        let rows = 4 * points.len();
        let num_sites = self.sites.len();
        let cols = 4 * num_sites;
        let mut A = DMatrix::<T>::zeros(rows, cols);
        let mut b = DVector::<T>::zeros(rows);
        for (i, p) in points.iter().enumerate() {
            b[4 * i] = potential[i];
            b.fixed_rows_mut::<U3>(4 * i + 1).copy_from(&normals[i]);
            for j in 0..num_sites {
                A.fixed_slice_mut::<U4, U4>(4 * i, 4 * j)
                    .copy_from(&self.fit_block(*p, j));
            }
        }
        (A, b)
    }

    /// The following are derivatives of the function
    ///
    /// `phi(x) := kernel(|x|)`
    ///
    /// Given a vector `x` and its norm `l`, return the gradient of the kernel evaluated
    /// at `l` wrt `x`. `j` denotes the site at which the kernel is evaluated.
    fn grad_phi(&self, x: Vector3<T>, l: T, j: usize) -> Vector3<T> {
        x * self.kernel[j].df_l(l)
    }

    // TODO: The computations below do more than needed. For instance most computed
    // matrices are symmetric, if we can reformulate this formulas below into operations
    // on symmetric matrices, we can optimize a lot of flops out.

    /// Given a vector `x` and its norm `l`, return the hessian of the kernel evaluated
    /// at `l` wrt `x`. `j` denotes the site at which the kernel is evaluated.
    fn hess_phi(&self, x: Vector3<T>, l: T, j: usize) -> Matrix3<T> {
        let df_l = self.kernel[j].df_l(l);
        let mut hess = Matrix3::identity();
        if l <= T::zero() {
            debug_assert!({
                let g = self.kernel[j].ddf(l) - df_l;
                g * g < T::from(1e-12).unwrap()
            });
            return hess * df_l;
        }

        let ddf = self.kernel[j].ddf(l);
        let x_hat = x / l;
        // df_l*I + x_hat*x_hat.transpose()*(ddf - df_l)
        hess.ger(ddf - df_l, &x_hat, &x_hat, df_l);
        hess
    }

    /// Given a vector `x` and its norm `l`, return the third derivative of the kernel evaluated
    /// at `l` wrt `x` when multiplied by vector b.
    /// `j` denotes the site at which the kernel is evaluated.
    fn third_deriv_prod_phi<S>(
        &self,
        x: Vector3<T>,
        l: T,
        b: &Vector<T, U3, S>,
        j: usize,
    ) -> Matrix3<T>
    where
        S: Storage<T, U3, U1>,
    {
        if l <= T::zero() {
            debug_assert!({
                let g = self.kernel[j].g(l); // ddf(l)/l - df(l)/l^2
                let dddf = self.kernel[j].dddf(l);
                dddf == T::zero() && g == T::zero()
            });
            return Matrix3::zero();
        }

        let g = self.kernel[j].g(l); // ddf(l)/l - df(l)/l^2
        let dddf = self.kernel[j].dddf(l);
        let x_hat = x / l;
        let x_dot_b = b.dot(&x_hat);
        let mut mtx = Matrix3::identity();
        let _3 = T::from(3).unwrap();
        let _1 = T::one();

        // TODO: optimize this expression. we can probably achieve the same thing with less flops
        // (bxT + xTb*I + xbT)*g + xxT*((dddf - T::from(3).unwrap()*g)*xTb)
        mtx.ger(_1, b, &x_hat, x_dot_b);
        mtx.ger(_1, &x_hat, b, _1);
        mtx.ger((dddf - _3 * g) * x_dot_b, &x_hat, &x_hat, g);
        mtx
    }

    /// Given a vector `x` and its norm `l`, return the fourth derivative of the kernel
    /// evaluated at `l` wrt `x` when multiplied by vectors b and c.
    /// `j` denotes the site at which the kernel is evaluated.
    #[inline]
    fn fourth_deriv_prod_phi<S>(
        &self,
        x: Vector3<T>,
        l: T,
        b: &Vector<T, U3, S>,
        c: &Vector<T, U3, S>,
        j: usize,
    ) -> Matrix3<T>
    where
        S: Storage<T, U3, U1>,
    {
        let g_l = self.kernel[j].g_l(l);
        let bc_tr = Matrix3::new(
            b[0] * c[0],
            b[1] * c[0],
            b[2] * c[0],
            b[0] * c[1],
            b[1] * c[1],
            b[2] * c[1],
            b[0] * c[2],
            b[1] * c[2],
            b[2] * c[2],
        );
        let c_dot_b = bc_tr.trace();
        let bc_tr_plus_cb_tr = bc_tr + bc_tr.transpose();
        let mut res = Matrix3::identity();
        if l <= T::zero() {
            debug_assert!({
                let h3 = self.kernel[j].h(l, T::from(3).unwrap());
                let h52 = self.kernel[j].h(l, T::from(5.0 / 2.0).unwrap());
                let ddddf = self.kernel[j].ddddf(l);
                let a = ddddf - T::from(6.0).unwrap() * h52;
                h3 == T::zero() && a * a < T::from(1e-12).unwrap()
            });
            return res * (g_l * c_dot_b) + (bc_tr_plus_cb_tr) * g_l;
        }

        let h3 = self.kernel[j].h(l, T::from(3).unwrap());
        let h52 = self.kernel[j].h(l, T::from(5.0 / 2.0).unwrap());
        let ddddf = self.kernel[j].ddddf(l);
        let a = ddddf - T::from(6.0).unwrap() * h52;
        let x_hat = x / l;
        let x_dot_b = x_hat.dot(b);
        let x_dot_c = x_hat.dot(c);
        let cb_sum = c * x_dot_b + b * x_dot_c;
        let _1 = T::one();

        // TODO: optimize this expression. we can probably achieve the same thing with less flops
        //xxT*(a*xTb*xTc + h3*cTb)
        //    + I*(h3*xTc*xTb + g_l*cTb)
        //    + ((cxT + xcT)*xTb + (bxT + xbT)*xTc)*h3
        //    + (bcT + cbT)*g_l
        res.ger(
            a * x_dot_b * x_dot_c + h3 * c_dot_b,
            &x_hat,
            &x_hat,
            h3 * x_dot_c * x_dot_b + g_l * c_dot_b,
        );
        res.ger(h3, &cb_sum, &x_hat, _1);
        res.ger(h3, &x_hat, &cb_sum, _1);
        res + (bc_tr_plus_cb_tr) * g_l
    }

    /// Evaluate the HRBF at point `p`.
    pub fn eval(&self, p: Point3<T>) -> T {
        self.betas
            .iter()
            .enumerate()
            .fold(T::zero(), |sum, (j, b)| sum + self.eval_block(p, j).dot(b))
    }

    /// Helper function for `eval`.
    fn eval_block(&self, p: Point3<T>, j: usize) -> Vector4<T> {
        let x = p - self.sites[j];
        let l = x.norm();
        let w = self.kernel[j].f(l);
        let g = self.grad_phi(x, l, j);
        Vector4::new(w, g[0], g[1], g[2])
    }

    /// Gradient of the HRBF function at point `p`.
    pub fn grad(&self, p: Point3<T>) -> Vector3<T> {
        self.betas
            .iter()
            .enumerate()
            .fold(Vector3::zero(), |sum, (j, b)| {
                sum + self.grad_block(p, j) * b
            })
    }

    /// Helper function for `grad`. Returns a 3x4 matrix that gives the gradient of the HRBF when
    /// multiplied by the corresponding coefficients.
    fn grad_block(&self, p: Point3<T>, j: usize) -> Matrix3x4<T> {
        let x = p - self.sites[j];
        let l = x.norm();
        let h = self.hess_phi(x, l, j);
        let mut grad = Matrix3x4::zero();
        grad.column_mut(0).copy_from(&self.grad_phi(x, l, j));
        grad.fixed_columns_mut::<U3>(1).copy_from(&h);
        grad
    }

    /// Compute the hessian of the HRBF function.
    pub fn hess(&self, p: Point3<T>) -> Matrix3<T> {
        self.betas
            .iter()
            .enumerate()
            .fold(Matrix3::zero(), |sum, (j, b)| {
                sum + self.hess_block_prod(p, b, j)
            })
    }

    /// Helper function for computing the hessian
    #[inline]
    fn hess_block_prod(&self, p: Point3<T>, b: &Vector4<T>, j: usize) -> Matrix3<T> {
        let x = p - self.sites[j];
        let l = x.norm();
        let b3 = b.fixed_rows::<U3>(1);
        let h = self.hess_phi(x, l, j);
        h * b[0] + self.third_deriv_prod_phi(x, l, &b3, j)
    }

    /// Advanced. Recall that the HRBF fit is done as
    ///
    /// ```verbatim
    /// ∑ⱼ ⎡  𝜙(𝑥ᵢ - 𝑥ⱼ)  ∇𝜙(𝑥ᵢ - 𝑥ⱼ)'⎤ ⎡ 𝛼ⱼ⎤ = ⎡ 0 ⎤
    ///    ⎣ ∇𝜙(𝑥ᵢ - 𝑥ⱼ) ∇∇𝜙(𝑥ᵢ - 𝑥ⱼ) ⎦ ⎣ 𝛽ⱼ⎦   ⎣ 𝑛ᵢ⎦
    /// ```
    ///
    /// for every HRBF site i, where the sum runs over HRBF sites j
    /// where 𝜙(𝑥) = 𝜑(||𝑥||) for one of the basis kernels we define in kernel.rs
    /// If we rewrite the equation above as
    ///
    /// ` ∑ⱼ Aⱼ(𝑥ᵢ)bⱼ = rᵢ `
    ///
    /// this function returns the matrix Aⱼ(p).
    ///
    /// This is the symmetric 4x4 matrix block that is used to fit the HRBF coefficients.
    /// This is equivalent to stacking the vector from `eval_block` on top of the
    /// 3x4 matrix returned by `grad_block`. This function is more efficient than
    /// evaluating `eval_block` and `grad_block`.
    /// This is [g ∇g]' = [𝜙 (∇𝜙)'; ∇𝜙 ∇(∇𝜙)'] in matlab notation.
    pub fn fit_block(&self, p: Point3<T>, j: usize) -> Matrix4<T> {
        let x = p - self.sites[j];
        let l = x.norm();
        let w = self.kernel[j].f(l);
        let g = self.grad_phi(x, l, j);
        let h = self.hess_phi(x, l, j);
        Matrix4::new(
            w,
            g[0],
            g[1],
            g[2],
            g[0],
            h[(0, 0)],
            h[(0, 1)],
            h[(0, 2)],
            g[1],
            h[(1, 0)],
            h[(1, 1)],
            h[(1, 2)],
            g[2],
            h[(2, 0)],
            h[(2, 1)],
            h[(2, 2)],
        )
    }

    /// Advanced. Using the same notation as above,
    /// this function returns the matrix ∇(Aⱼ(p)b)'
    pub fn grad_fit_block_prod(&self, p: Point3<T>, b: Vector4<T>, j: usize) -> Matrix3x4<T> {
        let x = p - self.sites[j];
        let l = x.norm();

        let b3 = b.fixed_rows::<U3>(1);

        let g = self.grad_phi(x, l, j);
        let h = self.hess_phi(x, l, j);

        let third = h * b[0] + self.third_deriv_prod_phi(x, l, &b3, j);

        let mut grad = Matrix3x4::zero();
        grad.column_mut(0).copy_from(&(g * b[0] + h * b3));
        grad.fixed_columns_mut::<U3>(1).copy_from(&third);
        grad
    }

    /// Using the same notation as above,
    /// given a 4d vector lagrange multiplier `c`, this function returns the matrix
    ///
    /// ` ∇(∇(Aⱼ(p)βⱼ)'c)' `
    ///
    /// where βⱼ are taken from `self.betas`
    fn hess_fit_prod_block(&self, p: Point3<T>, c: Vector4<T>, j: usize) -> Matrix3<T> {
        let x = p - self.sites[j];
        let l = x.norm();

        let c3 = c.fixed_rows::<U3>(1);
        let a = self.betas[j][0];
        let b = self.betas[j].fixed_rows::<U3>(1);

        // Compute in blocks
        self.hess_phi(x, l, j) * c[0] * a
            + self.third_deriv_prod_phi(x, l, &b, j) * c[0]
            + self.third_deriv_prod_phi(x, l, &c3, j) * a
            + self.fourth_deriv_prod_phi(x, l, &b, &c3, j)
    }

    /// Sum of hess_fit_prod_block evaluated at all sites.
    pub fn hess_fit_prod(&self, p: Point3<T>, c: Vector4<T>) -> Matrix3<T> {
        (0..self.sites.len()).fold(Matrix3::zero(), |sum, j| {
            sum + self.hess_fit_prod_block(p, c, j)
        })
    }
}