/*
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.
*/

//! Operation on angles.

use std;

/// Value of Pi.
pub const PI: f64 = std::f64::consts::PI;
/// Value of 2.0*Pi.
pub const DBLPI: f64 = std::f64::consts::TAU;
/// Value of Pi/2.0.
pub const HLFPI: f64 = std::f64::consts::FRAC_PI_2;
/// Arc seconds in a full circle.
pub const TURNAS: f64 = 1296000.0;
/// Seconds in a full circle.
pub const TURNS: f64 = 86400.0;
/// Degrees to radians.
pub const DEGRAD: f64 = std::f64::consts::PI / 180.0;
/// Radians to degrees.
pub const RADDEG: f64 = 180.0 / std::f64::consts::PI;
/// Arc seconds to radians.
pub const ASECRAD: f64 = DBLPI / TURNAS;
/// Radians to arc seconds.
pub const RADASEC: f64 = TURNAS / DBLPI;
/// Seconds to radians.
pub const SECRAD: f64 = DBLPI / TURNS;
/// Radians to seconds.
pub const RADSEC: f64 = TURNS / DBLPI;

/// Normalize angle into the range 0 <= a < 2pi.
/// # Arguments
/// * `ang` angle in radians
/// # Returns
/// angle in range 0-2pi
pub fn norm_dblpi(ang: f64) -> f64 {
    let out = ang % DBLPI as f64;
    if out < 0.0 {
        out + DBLPI
    } else {
        out
    }
}

/// Normalize angle into the range -pi <= a < +pi.
/// # Arguments
/// * `ang` angle in radians
/// # Returns
/// angle in range +/-pi
pub fn norm_pi(ang: f64) -> f64 {
    let out = ang % DBLPI as f64;
    if out.abs() >= PI {
        out - out.signum() * DBLPI
    } else {
        out
    }
}

/// Convert degrees, arcminutes, arcseconds to radians.
/// # Arguments
/// * `sign` '-' = negative, otherwise positive
/// * `deg` degrees
/// * `amin` arcminutes
/// * `asec` arcseconds
/// # Returns
/// angle in radians
pub fn dms_rad(sign: char, deg: f64, amin: f64, asec: f64) -> f64 {
    let out = (60.0 * ((60.0 * deg) + amin) + asec) * ASECRAD;
    if sign == '-' {
        -out
    } else {
        out
    }
}

/// Convert hours, minutes, seconds to radians.
/// # Arguments
/// * `hour` hours
/// * `min` minutes
/// * `sec` seconds
/// # Returns
/// angle in radians
pub fn hms_rad(hour: f64, min: f64, sec: f64) -> f64 {
    (60.0 * ((60.0 * hour) + min) + sec) * SECRAD
}

//TODO rad_dms, rad_hms
