Skip to content

Commit

Permalink
Added 2D visualization (not tried)
Browse files Browse the repository at this point in the history
  • Loading branch information
skalimoi authored May 3, 2024
1 parent d16e9f1 commit 893d16b
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,16 @@ enum Message {
struct ViewState {
mode: WeatherVisualization,
hour: u32,
layer: u8
layer: u8,
proj: ViewMode
}

enum ViewMode {
2D,
3D
}


fn menu_do(w: &mut impl MenuExt, sender: &Sender<Message>) {
w.add_emit(
"&File/New Scenario...\t",
Expand Down Expand Up @@ -307,7 +314,8 @@ fn main() {
let mut view_state = ViewState {
mode: Init,
hour: 0,
layer: 0
layer: 0,
proj: ViewMode::3D
};

let climates: [Climate; 18] = [koppen_cfa(), koppen_cfb(), koppen_cfc(), koppen_dfb(), koppen_dfc(), koppen_dfa(), koppen_cwc(), koppen_cwb(), koppen_cwa(), koppen_et(), koppen_afam(), koppen_as(), koppen_aw(), koppen_dsc(), koppen_bsh(), koppen_bsk(), koppen_bwh(), koppen_bwk()];
Expand Down Expand Up @@ -676,7 +684,11 @@ fn main() {
6 => view_state.layer = 6,
_ => view_state.layer = 0
}
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}

}
Message::WeatherSeedInput => {
weather_pane::weather_seed_do(&mut ui.weather_seed_input, &mut file);
Expand Down Expand Up @@ -738,29 +750,44 @@ fn main() {
},
Message::ViewHumidity => {
weather_pane::set_view_state(&mut view_state, WeatherVisualization::Humidity);
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}
ui.legend_box.set_image(Some(SharedImage::load("icons/humidity_legend.png").unwrap()));
ui.legend_box.redraw_label();
},
Message::ViewPressure => {
weather_pane::set_view_state(&mut view_state, WeatherVisualization::Pressure);
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}
ui.legend_box.set_image(Some(SharedImage::load("icons/pressure_legend.png").unwrap()));
ui.legend_box.redraw_label();
},
Message::ViewTemperature => {
weather_pane::set_view_state(&mut view_state, WeatherVisualization::Temperature);
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}
ui.legend_box.set_image(Some(SharedImage::load("icons/temp_legend.png").unwrap()));
ui.legend_box.redraw_label();
},
Message::ViewWind => {
weather_pane::set_view_state(&mut view_state, WeatherVisualization::Wind);
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}
},
Message::DaySlider => {
weather_pane::set_hour(&mut ui.day_vis_slider, &mut view_state);
weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
match view_state.proj {
ViewMode::3D => weather_pane::update_grid_at_time(view_state.hour, &mut file.weather_data, &mut mesh_v, &view_state);
ViewMode::2D => weather_pane::vis_image(&mut ui.putframehere, view_state.hour, &mut file.weather_data, &view_state);
}
}
Message::SaveFile => {
save_file_do(&mut file, &mut is_file_workspace, &mut workspace_path, &mut file_name);
Expand Down
119 changes: 119 additions & 0 deletions src/weather_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,125 @@ latitude: 0,
grid_size: 16,
};

pub fn vis_image(box: &mut Frame, hour: u32, grid_vector: &mut Vec<GenData>, state: &ViewState) {
let mut i: ImageBuffer<Rgba<u8>, Vec<u8>> = ImageBuffer::new(16, 16);
for component in grid_vector.as_slice() {
let p = &mut cube_vector[component.index.0 as usize + 16 *(component.index.1 as usize + 6 * component.index.2 as usize)];
let range = match hour {
0 => 0..24,
_ => (((24 * hour) - 24) as usize)..((24 * hour) as usize)
};
match state.mode {
Init => {},
WeatherVisualization::Wind => {},
WeatherVisualization::Temperature => {
if !component.pressure.is_empty() {
let median = (component.temperature[range.clone()].iter().sum::<OrderedFloat<f64>>().0 as isize) / range.clone().len() as isize;
let color = match median {
-60..=-10 => Color::from_rgba8(30, 92, 179, 255),
-11..=-1 => Color::from_rgba8(4, 161, 230, 255),
0..=5 => Color::from_rgba8(102, 204, 206, 255),
6..=10 => Color::from_rgba8(192, 229, 136, 255),
11..=15 => Color::from_rgba8(204, 230, 75, 255),
16..=20 => Color::from_rgba8(243, 240, 29, 255),
21..=25 => Color::from_rgba8(248, 157, 14, 255),
26..=30 => Color::from_rgba8(219, 30, 38, 255),
31..=90 => Color::from_rgba8(164, 38, 44, 255),
_ => Color::from_rgba8(255, 255, 255, 255)
};
let color_rgba = color.to_linear_rgba_u8();


match state.layer {
0 => {opacity = 10},
1 => {if component.index.1 != 0 {opacity = 0}},
2 => {if component.index.1 != 1 {opacity = 0}},
3 => {if component.index.1 != 2 {opacity = 0}},
4 => {if component.index.1 != 3 {opacity = 0}},
5 => {if component.index.1 != 4 {opacity = 0}},
6 => {if component.index.1 != 5 {opacity = 0}},
_ => { opacity = 10 }
}
if color.r == 1.0 && color.g == 1.0 && color.b == 1.0 {
opacity = 10;
}
i.put_pixel(component.index.x, component.index.z, Rgba[color.r, color.g, color.b, opacity]);
box.set_image_scaled(None::<SharedImage>);
box.set_image_scaled(Some(SharedImage::from_image(i).unwrap()));
box.redraw();
}
},
WeatherVisualization::Pressure => {
if !component.pressure.is_empty() {
let median = (component.pressure[range.clone()].iter().sum::<OrderedFloat<f64>>().0 as usize) / range.clone().len();
let color = match median {
50..=950 => Color::from_rgba8(40, 40, 255, 255) ,
951..=990 => Color::from_rgba8(102, 102, 255, 255),
991..=1000 => Color::from_rgba8(161, 161, 255, 255),
1001..=1015 => Color::from_rgba8(203, 203, 255, 255),
1016..=1030 => Color::from_rgba8(255, 138, 138, 255),
1031..=1060 => Color::from_rgba8(255, 103, 103, 255),
1061..=2000 => Color::from_rgba8(255, 41, 41, 255),
_ => Color::from_rgba8(255, 255, 255, 0)
};
let color_rgba = color.to_linear_rgba_u8();
let mut opacity = 10;
match state.layer {
0 => {opacity = 10},
1 => {if component.index.1 != 0 {opacity = 0}},
2 => {if component.index.1 != 1 {opacity = 0}},
3 => {if component.index.1 != 2 {opacity = 0}},
4 => {if component.index.1 != 3 {opacity = 0}},
5 => {if component.index.1 != 4 {opacity = 0}},
6 => {if component.index.1 != 5 {opacity = 0}},
_ => { opacity = 10 }
}
i.put_pixel(component.index.x, component.index.z, Rgba[color.r, color.g, color.b, opacity]);
box.set_image_scaled(None::<SharedImage>);
box.set_image_scaled(Some(SharedImage::from_image(i).unwrap()));
box.redraw();
}
},
WeatherVisualization::Humidity => {
let median = (component.humidity[range.clone()].iter().sum::<OrderedFloat<f64>>().0 as usize) / range.clone().len();
let color = match median {
0..=10 => Color::from_rgba8(255, 255, 217, 255),
11..=20 => Color::from_rgba8(237, 248, 177, 255),
21..=30 => Color::from_rgba8(199, 233, 180, 255),
31..=40 => Color::from_rgba8(127, 205, 187, 255),
41..=50 => Color::from_rgba8(65, 182, 196, 255),
51..=60 => Color::from_rgba8(29, 145, 192, 255),
61..=70 => Color::from_rgba8(34, 94, 168, 255),
71..=80 => Color::from_rgba8(37, 52, 148, 255),
81..=90 => Color::from_rgba8(8, 29, 88, 255),
91..=100 => Color::from_rgba8(3, 20, 70, 255),
_ => Color::from_rgba8(255, 255, 255, 0)
};
let color_rgba = color.to_linear_rgba_u8();
let mut opacity = 10;

match state.layer {
0 => {opacity = 10},
1 => {if component.index.1 != 0 {opacity = 0}},
2 => {if component.index.1 != 1 {opacity = 0}},
3 => {if component.index.1 != 2 {opacity = 0}},
4 => {if component.index.1 != 3 {opacity = 0}},
5 => {if component.index.1 != 4 {opacity = 0}},
6 => {if component.index.1 != 5 {opacity = 0}},
_ => { opacity = 10 }
}
if color.r == 1.0 && color.g == 1.0 && color.b == 1.0 {
opacity = 0;
}
i.put_pixel(component.index.x, component.index.z, Rgba[color.r, color.g, color.b, opacity]);
box.set_image_scaled(None::<SharedImage>);
box.set_image_scaled(Some(SharedImage::from_image(i).unwrap()));
box.redraw(); }
}
}
box.show();
}

pub fn set_hour(w: &mut impl ValuatorExt, state: &mut ViewState) {
state.hour = w.value() as u32;
}
Expand Down

0 comments on commit 893d16b

Please sign in to comment.