/*
Copyright (c) 2022 Peter Hristozov / Петър Христозов

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! Scalar, his velocity and acceleration.

/// Scalar.
pub mod scl {

    /// Evaluates a polynomial.
    /// # Arguments
    /// * `arg` The value of the independent variable
    /// * `coef` array of polynomial coefficients a0, a1 ...
    /// # Returns
    /// value of polynomial
    pub fn polynom(arg: f64, coef: &[f64]) -> f64 {
        let pow = coef.len() - 1;
        let mut ss = coef[pow];
        for i in Iterator::rev(0..pow) {
            ss = ss * arg + coef[i];
        }
        ss
    }
}

/// Represent scalar and his velocity (sv-scalar).
pub mod scl_sv {

    /// Scalar and his velocity.
    #[derive(Debug)]
    pub struct SclSv {
        /// value of scalar
        pub s: f64,
        /// velocity of scalar
        pub v: f64,
    }

    impl SclSv {}

    /// Scale sv-scalar.
    /// # Arguments
    /// # Returns
    /// s: ss*a.s, v:ss*a.v
    pub fn scale(ss: f64, a: &SclSv) -> SclSv {
        SclSv {
            s: (ss * a.s),
            v: (ss * a.v),
        }
    }

    /// Scale sv-scalar.
    /// # Arguments
    /// # Returns
    /// s: ss*a.s, v: sv*a.v
    pub fn scale2(ss: f64, sv: f64, a: &SclSv) -> SclSv {
        SclSv {
            s: (ss * a.s),
            v: (sv * a.v),
        }
    }

    /// Add two sv-scalars.
    /// # Arguments
    /// * `a` first sv-scalar
    /// * `b` second sv-scalar
    /// # Returns
    /// s: a.s+b.s, v: a.v+b.v
    pub fn add(a: &SclSv, b: &SclSv) -> SclSv {
        SclSv {
            s: (a.s + b.s),
            v: (a.v + b.v),
        }
    }

    /// Subtract two sv-scalars.
    /// # Arguments
    /// * `a` first sv-scalar
    /// * `b` second sv-scalar
    /// # Returns
    /// s: a.s-b.s, v: a.v-b.v
    pub fn sub(a: &SclSv, b: &SclSv) -> SclSv {
        SclSv {
            s: (a.s - b.s),
            v: (a.v - b.v),
        }
    }

    /// Multiply two sv-scalars.
    /// # Arguments
    /// * `a` first sv-scalar
    /// * `b` second sv-scalar
    /// # Returns
    /// s: a.s*b.s, v: a.v*b.s+a.s*b.v
    pub fn mul(a: &SclSv, b: &SclSv) -> SclSv {
        SclSv {
            s: (a.s * b.s),
            v: (b.s * a.v + a.s * b.v),
        }
    }

    /// Divide two sv-scalars.
    /// # Arguments
    /// * `a` first sv-scalar
    /// * `b` second sv-scalar
    /// # Returns
    /// s: a.s/b.s, v: (a.v*b.s-a.s*b.v)/(b.s*b.s)
    pub fn div(a: &SclSv, b: &SclSv) -> SclSv {
        SclSv {
            s: (a.s / b.s),
            v: ((a.v * b.s - a.s * b.v) / (b.s * b.s)),
        }
    }

    /// Evaluates a polynomial and derivative.
    /// # Arguments
    /// * `arg` The value of the independent variable
    /// * `coef` array of polynomial coefficients a0, a1 ...
    /// # Returns
    /// value and derivative of polynomial
    pub fn polynom(arg: f64, coef: &[f64]) -> SclSv {
        let pow = coef.len() - 1;
        let mut ss = coef[pow];
        let mut sv = pow as f64 * coef[pow];
        for i in Iterator::rev(0..pow) {
            ss = ss * arg + coef[i];
        }
        for i in Iterator::rev(1..pow) {
            sv = sv * arg + i as f64 * coef[i];
        }
        SclSv { s: (ss), v: (sv) }
    }

    /// Evaluates a polynomial and derivative.
    /// # Arguments
    /// * `arg` The value of the independent variable
    /// * `coef` array of polynomial coefficients a0, a1 ...
    /// * `norm` norm for s
    /// # Returns
    /// value and derivative of polynomial
    pub fn poly_norm(arg: f64, coef: &[f64], norm: f64) -> SclSv {
        let pow = coef.len() - 1;
        let mut ss = coef[pow];
        let mut sv = pow as f64 * coef[pow];
        for i in Iterator::rev(0..pow) {
            ss = ss * arg + coef[i];
        }
        for i in Iterator::rev(1..pow) {
            sv = sv * arg + i as f64 * coef[i];
        }
        SclSv {
            s: (ss) % norm,
            v: (sv),
        }
    }

    /// Interpolate sv-scalar in time `t`
    /// # Arguments
    /// * `t0` moment 0 (same dimension as time in velocity)
    /// * `sv0` sv-scalar in moment `t0`
    /// * `t` moment for interpolation
    /// # Returns
    /// sv-scalar in moment `t`
    pub fn update(t0: f64, sv0: &SclSv, t: f64) -> SclSv {
        let dt = t - t0;
        SclSv {
            s: (sv0.s + dt * sv0.v),
            v: (sv0.v),
        }
    }

    /// Interpolate sv-scalar in time `t`
    /// # Arguments
    /// * `t0` moment 0 (same dimension as time in velocity)
    /// * `sv0` sv-scalar in moment `t0`
    /// * `t1` moment 1 (same dimension as time in velocity)
    /// * `sv1` sv-scalar in moment `t1`
    /// * `t` moment for interpolation (t0 < t < t1)
    /// # Returns
    /// sv-scalar in moment `t`
    pub fn interp(t0: f64, sv0: &SclSv, t1: f64, sv1: &SclSv, t: f64) -> SclSv {
        let dt = t - t0;
        let acc = (sv1.v - sv0.v) / (t1 - t0);
        SclSv {
            s: (sv0.s + dt * (sv0.v + 0.5 * dt * acc)),
            v: (sv0.v + dt * acc),
        }
    }
}

pub mod scl_sva {
    #[derive(Debug)]
    pub struct SclSva {
        /// value of scalar
        pub s: f64,
        /// velocity of scalar
        pub v: f64,
        ///acceleration of scalar
        pub a: f64,
    }
    impl SclSva {}

    /// Scale sva-scalar.
    /// # Arguments
    /// # Returns
    /// s: ss*a.s, v:ss*a.v, a:ss*a.a
    pub fn scale(ss: f64, a: &SclSva) -> SclSva {
        SclSva {
            s: (ss * a.s),
            v: (ss * a.v),
            a: (ss * a.a),
        }
    }

    /// Scale sva-scalar.
    /// # Arguments
    /// # Returns
    /// s: ss*a.s, v: sv*a.v, , a: sa*a.a
    pub fn scale_other(ss: f64, sv: f64, sa: f64, a: &SclSva) -> SclSva {
        SclSva {
            s: (ss * a.s),
            v: (sv * a.v),
            a: (sa * a.a),
        }
    }

    /// Add two sva-scalars.
    /// # Arguments
    /// * `a` first sva-scalar
    /// * `b` second sva-scalar
    /// # Returns
    /// s: a.s+b.s, v: a.v+b.v, a: a.a+b.a
    pub fn add(a: &SclSva, b: &SclSva) -> SclSva {
        SclSva {
            s: (a.s + b.s),
            v: (a.v + b.v),
            a: (a.a + b.a),
        }
    }

    /// Subtract two sva-scalars.
    /// # Arguments
    /// * `a` first sva-scalar
    /// * `b` second sva-scalar
    /// # Returns
    /// s: a.s-b.s, v: a.v-b.v, a: a.a-b.a
    pub fn sub(a: &SclSva, b: &SclSva) -> SclSva {
        SclSva {
            s: (a.s - b.s),
            v: (a.v - b.v),
            a: (a.a - b.a),
        }
    }

    /// Multiply two sva-scalars.
    /// # Arguments
    /// * `a` first svascalar
    /// * `b` second sva-scalar
    /// # Returns
    /// s: a.s*b.s, v: a.v*b.s+a.s*b.v, a: 2.0 * b.v * a.v + b.s * a.a + a.s * b.a
    pub fn mul(a: &SclSva, b: &SclSva) -> SclSva {
        SclSva {
            s: (a.s * b.s),
            v: (b.s * a.v + a.s * b.v),
            a: (2.0 * b.v * a.v + b.s * a.a + a.s * b.a),
        }
    }

    /// Divide two sva-scalars.
    /// # Arguments
    /// * `a` first sva-scalar
    /// * `b` second sva-scalar
    /// # Returns
    /// s: a.s/b.s, v: (a.v-a.s*b.v*(1/b.s))*(1/b.s), a: (a.a + ((-a.s * b.a - 2.0 * a.v * b.v) + 2.0 * a.s * b.v * b.v * (1/b.s)) *(1/b.s)) * (1/b.s)
    pub fn div(a: &SclSva, b: &SclSva) -> SclSva {
        let del = 1.0 / b.s;
        SclSva {
            s: (a.s / b.s),
            v: ((a.v - a.s * b.v * del) * del),
            a: ((a.a + ((-a.s * b.a - 2.0 * a.v * b.v) + 2.0 * a.s * b.v * b.v * del) * del) * del),
        }
    }

    /// Evaluates a polynomial and derivatives.
    /// # Arguments
    /// * `arg` The value of the independent variable
    /// * `coef` array of polynomial coefficients a0, a1 ...
    /// # Returns
    /// value and derivatives of polynomial
    pub fn polynom(arg: f64, coef: &[f64]) -> SclSva {
        let pow = coef.len() - 1;
        let mut ss = coef[pow];
        let mut sv = pow as f64 * coef[pow];
        let mut sa = (pow * (pow - 1)) as f64 * coef[pow]; //?
        for i in Iterator::rev(0..pow) {
            ss = ss * arg + coef[i];
        }
        for i in Iterator::rev(1..pow) {
            sv = sv * arg + i as f64 * coef[i];
        }
        for i in Iterator::rev(2..pow) {
            sa = sa * arg + (i * (i - 1)) as f64 * coef[i];
        }
        SclSva {
            s: (ss),
            v: (sv),
            a: (sa),
        }
    }

    /// Interpolate sva-scalar in time `t`
    /// # Arguments
    /// * `t0` moment 0 (same dimension as time in velocity)
    /// * `sva0` sva-scalar in moment `t0`
    /// * `t` moment for interpolation
    /// # Returns
    /// sva-scalar in moment `t`
    pub fn update(t0: f64, sva0: &SclSva, t: f64) -> SclSva {
        let dt = t - t0;
        SclSva {
            s: (sva0.s + dt * (sva0.v + 0.5 * dt * sva0.a)),
            v: (sva0.v + dt * sva0.a),
            a: (sva0.a),
        }
    }

    /// Interpolate sva-scalar in time `t`
    /// # Arguments
    /// * `t0` moment 0 (same dimension as time in velocity)
    /// * `sva0` sva-scalar in moment `t0`
    /// * `t1` moment 1 (same dimension as time in velocity)
    /// * `sva1` sva-scalar in moment `t1`
    /// * `t` moment for interpolation (t0 < t < t1)
    /// # Returns
    /// sva-scalar in moment `t`
    pub fn interp(t0: f64, sva0: &SclSva, t1: f64, sva1: &SclSva, t: f64) -> SclSva {
        let dt = t - t0;
        let acc = (sva1.a - sva0.a) / (t1 - t0);
        SclSva {
            s: (sva0.s + dt * (sva0.v + dt * (0.5 * sva0.a + dt * acc / 6.0))),
            v: (sva0.v + dt * (sva0.a + 0.5 * dt * acc)),
            a: (sva0.a + dt * acc),
        }
    }
}
