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
/*
 * File: emit_intensity.rs
 * Project: common
 * Created Date: 11/11/2023
 * Author: Shun Suzuki
 * -----
 * Last Modified: 25/11/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(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct EmitIntensity {
    value: u8,
}

impl EmitIntensity {
    pub const MAX: EmitIntensity = EmitIntensity { value: 255 };
    pub const MIN: EmitIntensity = EmitIntensity { value: 0 };
    pub const DEFAULT_CORRECTED_ALPHA: float = 0.803;

    pub fn new(value: u8) -> Self {
        Self { value }
    }

    pub fn new_with_correction(value: u8) -> Self {
        Self::new_with_correction_alpha(value, Self::DEFAULT_CORRECTED_ALPHA)
    }

    pub fn new_with_correction_alpha(value: u8, alpha: float) -> Self {
        Self {
            value: ((value as float / 255.).powf(1. / alpha).asin() / PI * 510.0).round() as u8,
        }
    }

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

impl From<u8> for EmitIntensity {
    fn from(v: u8) -> Self {
        Self::new(v)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        collections::hash_map::DefaultHasher,
        hash::{Hash, Hasher},
    };

    #[test]
    fn test_new() {
        for i in 0x00..=0xFF {
            let intensity = EmitIntensity::new(i);
            assert_eq!(intensity.value(), i);
        }
    }

    #[test]
    fn test_new_with_correction() {
        for i in 0..=0xFF {
            let intensity = EmitIntensity::new_with_correction(i);
            assert_eq!(
                intensity.value(),
                ((i as float / 255.)
                    .powf(1. / EmitIntensity::DEFAULT_CORRECTED_ALPHA)
                    .asin()
                    / PI
                    * 510.0)
                    .round() as u8
            );
        }
    }

    #[test]
    fn test_clone() {
        let intensity = EmitIntensity::new(0);
        assert_eq!(intensity.value(), intensity.clone().value());
    }

    #[test]
    fn test_debug() {
        let intensity = EmitIntensity::new(0);
        assert_eq!(format!("{:?}", intensity), "EmitIntensity { value: 0 }");
    }

    #[test]
    fn test_ord() {
        let intensity1 = EmitIntensity::new(0);
        let intensity2 = EmitIntensity::new(1);
        assert!(intensity1 < intensity2);
        assert_eq!(intensity1.min(intensity2), intensity1);
    }

    #[test]
    fn hash() {
        let intensity = EmitIntensity::new(0);
        let mut s = DefaultHasher::new();
        assert_eq!(intensity.hash(&mut s), 0.hash(&mut s));
        s.finish();
    }
}