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
/*
 * File: viewer_settings.rs
 * Project: src
 * Created Date: 26/11/2021
 * Author: Shun Suzuki
 * -----
 * Last Modified: 24/07/2023
 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
 * -----
 * Copyright (c) 2021 Hapis Lab. All rights reserved.
 *
 */

use crate::{Quaternion, Vector3, SCALE, ZPARITY};
use cgmath::{Deg, Euler};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum ColorMapType {
    Viridis,
    Magma,
    Inferno,
    Plasma,
}

/// Viewer settings
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ViewerSettings {
    pub window_width: u32,
    pub window_height: u32,
    pub vsync: bool,
    pub gpu_idx: i32,
    pub slice_pos_x: f32,
    pub slice_pos_y: f32,
    pub slice_pos_z: f32,
    pub slice_width: f32,
    pub slice_height: f32,
    pub slice_pixel_size: f32,
    pub camera_pos_x: f32,
    pub camera_pos_y: f32,
    pub camera_pos_z: f32,
    pub camera_near_clip: f32,
    pub camera_far_clip: f32,
    pub sound_speed: f32,
    pub slice_rot_x: f32,
    pub slice_rot_y: f32,
    pub slice_rot_z: f32,
    pub slice_color_scale: f32,
    pub slice_alpha: f32,
    pub color_map_type: ColorMapType,
    pub show_radiation_pressure: bool,
    pub camera_rot_x: f32,
    pub camera_rot_y: f32,
    pub camera_rot_z: f32,
    pub camera_fov: f32,
    pub font_size: f32,
    pub background: [f32; 4],
    pub show_mod_plot: bool,
    pub show_mod_plot_raw: bool,
    pub mod_enable: bool,
    pub mod_auto_play: bool,
    pub stm_auto_play: bool,
    pub image_save_path: String,
    pub port: u16,
    pub camera_move_speed: f32,
}

impl ViewerSettings {
    pub fn new() -> ViewerSettings {
        Self::default()
    }

    pub(crate) fn slice_pos(&self) -> Vector3 {
        Vector3::new(self.slice_pos_x, self.slice_pos_y, self.slice_pos_z)
    }

    pub(crate) fn slice_rotation(&self) -> Quaternion {
        Quaternion::from(Euler {
            x: Deg(self.slice_rot_x),
            y: Deg(self.slice_rot_y),
            z: Deg(self.slice_rot_z),
        })
    }

    pub(crate) fn set_camera_pos(&mut self, v: Vector3) {
        self.camera_pos_x = v.x;
        self.camera_pos_y = v.y;
        self.camera_pos_z = v.z;
    }

    pub(crate) fn set_camera_rot(&mut self, rot: Quaternion) {
        let euler = Euler::from(rot);
        self.camera_rot_x = Deg::from(euler.x).0;
        self.camera_rot_y = Deg::from(euler.y).0;
        self.camera_rot_z = Deg::from(euler.z).0;
    }
}

impl Default for ViewerSettings {
    fn default() -> Self {
        ViewerSettings {
            window_width: 800,
            window_height: 600,
            vsync: true,
            gpu_idx: 0,
            slice_pos_x: 86.6252 * SCALE,
            slice_pos_y: 66.7133 * SCALE,
            slice_pos_z: 150.0 * SCALE * ZPARITY,
            slice_width: 300.0 * SCALE,
            slice_height: 300.0 * SCALE,
            slice_pixel_size: 1.0 * SCALE,
            camera_pos_x: 86.6252 * SCALE,
            camera_pos_y: -533.2867 * SCALE,
            camera_pos_z: 150.0 * SCALE * ZPARITY,
            camera_near_clip: 0.1 * SCALE,
            camera_far_clip: 1000. * SCALE,
            sound_speed: 340.0e3 * SCALE,
            slice_rot_x: 90.0 * ZPARITY,
            slice_rot_y: 0.,
            slice_rot_z: 0.,
            slice_color_scale: 2.,
            slice_alpha: 1.,
            color_map_type: ColorMapType::Inferno,
            show_radiation_pressure: false,
            camera_rot_x: 90.0 * ZPARITY,
            camera_rot_y: 0.,
            camera_rot_z: 0.,
            camera_fov: 45.,
            font_size: 16.,
            background: [0.3, 0.3, 0.3, 1.],
            show_mod_plot: false,
            show_mod_plot_raw: false,
            mod_enable: false,
            mod_auto_play: false,
            stm_auto_play: false,
            image_save_path: "image.png".to_string(),
            port: 8080,
            camera_move_speed: 10. * SCALE,
        }
    }
}