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
/*
 * File: amplitude.rs
 * Project: common
 * Created Date: 14/10/2023
 * Author: Shun Suzuki
 * -----
 * Last Modified: 16/10/2023
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
 *
 */

use crate::{
    defined::{float, PI},
    derive::prelude::AUTDInternalError,
};

use super::DutyRatio;

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Amplitude {
    value: float,
}

impl Amplitude {
    pub const MAX: Amplitude = Amplitude { value: 1.0 };
    pub const MIN: Amplitude = Amplitude { value: 0.0 };

    pub fn new(value: float) -> Result<Self, AUTDInternalError> {
        if !(0.0..=1.0).contains(&value) {
            Err(AUTDInternalError::AmplitudeOutOfRange(value))
        } else {
            Ok(Self { value })
        }
    }

    pub fn new_clamped(value: float) -> Self {
        Self {
            value: value.clamp(0.0, 1.0),
        }
    }

    pub const fn value(&self) -> float {
        self.value
    }
}

impl From<float> for Amplitude {
    fn from(value: float) -> Self {
        Amplitude::new_clamped(value)
    }
}

impl From<DutyRatio> for Amplitude {
    fn from(f: DutyRatio) -> Self {
        Self {
            value: (f.value() * PI).sin(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn amplitude() {
        let amp = Amplitude::new(0.0);
        assert!(amp.is_ok());
        assert_eq!(amp.unwrap().value(), 0.0);

        let amp = Amplitude::new(1.0);
        assert!(amp.is_ok());
        assert_eq!(amp.unwrap().value(), 1.0);

        let amp = Amplitude::new(1.1);
        assert!(amp.is_err());

        let amp = Amplitude::new(-0.1);
        assert!(amp.is_err());
    }

    #[test]
    fn amplitude_clamped() {
        let amp = Amplitude::new_clamped(0.0);
        assert_eq!(amp.value(), 0.0);

        let amp = Amplitude::new_clamped(1.0);
        assert_eq!(amp.value(), 1.0);

        let amp = Amplitude::new_clamped(1.1);
        assert_eq!(amp.value(), 1.0);

        let amp = Amplitude::new_clamped(-0.1);
        assert_eq!(amp.value(), 0.0);
    }

    #[test]
    fn amplitude_from_f64() {
        let amp = Amplitude::from(0.0);
        assert_eq!(amp.value(), 0.0);

        let amp = Amplitude::from(1.0);
        assert_eq!(amp.value(), 1.0);

        let amp = Amplitude::from(1.1);
        assert_eq!(amp.value(), 1.0);

        let amp = Amplitude::from(-0.1);
        assert_eq!(amp.value(), 0.0);
    }

    #[test]
    fn amplitude_from_duty_ratio() {
        let amp = Amplitude::from(DutyRatio::MIN);
        assert_eq!(amp, Amplitude::MIN);

        let amp = Amplitude::from(DutyRatio::MAX);
        assert_eq!(amp, Amplitude::MAX);

        let amp = Amplitude::from(DutyRatio::new_clamped(0.1));
        assert_approx_eq::assert_approx_eq!(amp.value(), 0.3090169943749474);
    }

    #[test]
    fn amplitude_clone() {
        let amp = Amplitude::new_clamped(0.5);
        let amp2 = Clone::clone(&amp);
        assert_eq!(amp, amp2);
    }

    #[test]
    fn amplitude_debug() {
        let amp = Amplitude::new_clamped(0.5);
        assert_eq!(format!("{:?}", amp), "Amplitude { value: 0.5 }");
    }
}