Skip to content

Commit

Permalink
Fixed vegetation bugs and completed serializing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
skalimoi committed May 6, 2024
1 parent a90c641 commit f3522e2
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 36 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ image_crate = { package = "image", version = "0.24.7" }
image_newest = {package = "image", version= "0.23.14", default-features = false}
imageproc = "0.23.0"
map-range = "0.1.2"
nalgebra = "0.32.3"
nalgebra = { version = "0.32.3", features = ["serde-serialize"] }
noise = {version = "0.8.2", features = ["images"]}
rand = "0.8.5"
rand_chacha = "0.3.1"
Expand Down
80 changes: 61 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ struct FileData {
eroded_full_color: Vec<u8>,
eroded_full: Vec<u16>,
weather_data: Vec<GenData>,
discharge: Vec<u8>
discharge: Vec<u8>,
soil: Vec<u8>,
vegetation_maps: VegetationCollection,
datamaps: VegetationMaps
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -209,6 +212,16 @@ fn new_do(program_data: &mut FileData) {
eroded_full: vec![],
weather_data: vec![],
discharge: vec![],
soil: vec![],
vegetation_maps: VegetationCollection {
generated: HashMap::new()
},
datamaps: VegetationMaps {
insolation: GreyscaleImage::new(vec![]),
edaphology: GreyscaleImage::new(vec![]),
hydrology: GreyscaleImage::new(vec![]),
orography: GreyscaleImage::new(vec![]),
}
};

let _ = replace::<FileData>(program_data, clean);
Expand Down Expand Up @@ -236,6 +249,13 @@ fn export_do(program_data: &mut FileData) {
i.save(dir_string.clone() + "/terrain/map.png").unwrap();
let d: ImageBuffer<Luma<u8>, Vec<u8>> = image_crate::ImageBuffer::from_raw(8192, 8192, program_data.discharge.clone()).unwrap();
d.save(dir_string.clone() + "/terrain/hydro.png").unwrap();
for element in program_data.vegetation_maps.generated.iter() {
let mask: ImageBuffer<Luma<u8>, Vec<u8>> = image_crate::ImageBuffer::from_raw(1024, 1024, element.1.clone()).unwrap();
mask.save(dir_string.clone() + format!("/textures/{}.png", element.0.clone()).as_str()).unwrap()
}
let soil: ImageBuffer<Luma<u8>, Vec<u8>> = image_crate::ImageBuffer::from_raw(1024, 1024, program_data.soil.clone()).unwrap();
soil.save(dir_string.clone() + "/terrain/soil_ids.png").unwrap();

}

fn save_file_do(program_data: &mut FileData, is_workplace: &mut bool, path: &mut String, filename: &mut String) {
Expand Down Expand Up @@ -288,6 +308,16 @@ fn open_file_do(program_data: &mut FileData) -> (FileData, PathBuf) {
eroded_full: vec![],
weather_data: vec![],
discharge: vec![],
soil: vec![],
vegetation_maps: VegetationCollection {
generated: HashMap::new()
},
datamaps: VegetationMaps {
insolation: GreyscaleImage::new(vec![]),
edaphology: GreyscaleImage::new(vec![]),
hydrology: GreyscaleImage::new(vec![]),
orography: GreyscaleImage::new(vec![]),
}
};
let mut nfc = dialog::NativeFileChooser::new(dialog::NativeFileChooserType::BrowseFile);
nfc.set_filter("RON files\t*.ron");
Expand Down Expand Up @@ -372,7 +402,17 @@ fn main() {
eroded_full_color: vec![],
eroded_full: vec![],
weather_data: vec![],
discharge: vec![]
discharge: vec![],
soil: vec![],
vegetation_maps: VegetationCollection {
generated: HashMap::new()
},
datamaps: VegetationMaps {
insolation: GreyscaleImage::new(vec![]),
edaphology: GreyscaleImage::new(vec![]),
hydrology: GreyscaleImage::new(vec![]),
orography: GreyscaleImage::new(vec![]),
}
};


Expand Down Expand Up @@ -639,17 +679,10 @@ fn main() {
soilchoices.insert(SoilType::Loam, false);
soilchoices.insert(SoilType::Clay, false);
soilchoices.insert(SoilType::Sand, false);

let mut vegmaps = VegetationMaps {
insolation: GreyscaleImage::new(vec![]),
edaphology: GreyscaleImage::new(vec![]),
hydrology: GreyscaleImage::new(vec![]),
orography: GreyscaleImage::new(vec![]),
soil_int: vec![]
};


let mut soil_veg_params = VegetationData {
base: SoilType::Dirt,
base: SoilType::Stone,
blocklist: soilchoices,
vegetationlist: HashMap::new()
};
Expand Down Expand Up @@ -678,18 +711,19 @@ fn main() {
gl_widget.show();
}
Message::NextVeg => {
index+=1;
let element = index_list[index-1].clone();
let map = veg_collection.clone().generated.get(&element).unwrap().clone();
println!("{:?}", file.vegetation_maps.generated);
let element = index_list[index].clone();
let map = file.vegetation_maps.clone().generated.get(&element).unwrap().clone();
let i = RgbImage::new(map.as_slice(), 1024, 1024, ColorDepth::Rgb8).unwrap();
ui.veg_preview.set_image_scaled(None::<SharedImage>);
ui.veg_preview.set_image_scaled(SharedImage::from_image(i).ok());
ui.veg_preview.redraw();
ui.veg_name.set_label(format!("Now displaying: {}", element).as_str());
index+=1;
}
Message::GenVegSel => {
generate_selected_do(&mut ui.vegetation_list, &mut vegmaps, &mut soil_veg_params, &mut file, &mut veg_collection);
for element in veg_collection.clone().generated.into_iter() {
generate_selected_do(&mut ui.vegetation_list, &mut soil_veg_params, &mut file);
for element in file.vegetation_maps.clone().generated.into_iter() {
let name = element.0.clone();
index_list.push(name);
}
Expand Down Expand Up @@ -792,9 +826,10 @@ fn main() {
let i: ImageBuffer<Luma<u16>, Vec<u16>> = ImageBuffer::from_raw(8192, 8192, file.eroded_full.clone()).unwrap();
let hydro: ImageBuffer<Luma<u8>, Vec<u8>> = ImageBuffer::from_raw(8192, 8192, file.discharge.clone()).unwrap();
let soil = init_soilmaker(&mut ui.soil_preview, soil_base, &soil_veg_params.blocklist, &i, &hydro);
vegmaps.soil_int = soil;
file.soil = soil;
ui.soil_preview.set_image_scaled(None::<SharedImage>);
ui.soil_preview.set_image_scaled(SharedImage::from_image(RgbImage::new(vegmaps.soil_int.as_slice(), 1024, 1024, ColorDepth::Rgb8).unwrap()).ok());
ui.soil_preview.set_image_scaled(SharedImage::from_image(RgbImage::new(file.soil.as_slice(), 1024, 1024, ColorDepth::Rgb8).unwrap()).ok());
ui.soil_preview.redraw();
}
Message::ImportHeightmap => {
heightmap_importer_win.show();
Expand Down Expand Up @@ -1023,8 +1058,10 @@ fn main() {
file.raw_full = p.0.raw_full;
file.color_eroded_512 = p.0.color_eroded_512;
file.eroded_raw_512 = p.0.eroded_raw_512;
file.color_map_512 = p.0.color_map_512.clone();
file.color_map_512.clone_from(&p.0.color_map_512);
file.raw_map_512 = p.0.raw_map_512;
file.soil = p.0.soil;
file.vegetation_maps = p.0.vegetation_maps;

if !file.color_map_512.is_empty() {
topography::update_noise_img(&mut ui.preview_box_topo, &file, 0, ColorDepth::Rgb8);
Expand All @@ -1038,6 +1075,11 @@ fn main() {
if !file.discharge.is_empty() {
hydro::update_hydro_prev(&mut ui.hydro_mask_preview, false, &mut file);
}
if !file.soil.is_empty() {
ui.soil_preview.set_image_scaled(None::<SharedImage>);
ui.soil_preview.set_image_scaled(SharedImage::from_image(RgbImage::new(file.soil.as_slice(), 1024, 1024, ColorDepth::Rgb8).unwrap()).ok());
ui.soil_preview.redraw();
}


ui.seed_input.set_value(format!("{}", &file.topography.seed.unwrap()).as_str());
Expand Down
2 changes: 1 addition & 1 deletion src/soil/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::soil::orography::calculate_normal_map;
use crate::soil::probabilities::calculate_probabilities;
use crate::soil_def::{VegetationCollection, VegetationMaps};

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GreyscaleImage<T> {
pub image: Vec<T>,
len: usize,
Expand Down
23 changes: 14 additions & 9 deletions src/soil_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use fltk::prelude::{BrowserExt, MenuExt};
use image_newest::{ImageBuffer, Luma};
use image_newest::imageops::FilterType;
use nalgebra::Vector3;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use crate::FileData;
use crate::soil::config::{Biom, GreyscaleImage, Map, SimConfig, Soil, SunConfig, Vegetation};

Expand All @@ -30,21 +30,22 @@ pub struct VegetationData {
pub vegetationlist: HashMap<String, bool>
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct VegetationMaps {
pub insolation: GreyscaleImage<f64>,
pub edaphology: GreyscaleImage<f64>,
pub hydrology: GreyscaleImage<f64>,
pub orography: GreyscaleImage<Vector3<f64>>,
pub soil_int: Vec<u8>
}

#[derive(Clone)]


#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct VegetationCollection {
pub generated: HashMap<String, Vec<u8>>
}

pub fn generate_selected_do(c: &mut CheckBrowser, vegetation_maps: &mut VegetationMaps, vegdata: &mut VegetationData, filedata: &mut FileData, vegetation_collection: &mut VegetationCollection) {
pub fn generate_selected_do(c: &mut CheckBrowser, vegdata: &mut VegetationData, filedata: &mut FileData) {
collect_values(c, vegdata);
let h: ImageBuffer<Luma<u16>, Vec<u16>> = ImageBuffer::from_raw(8192, 8192, filedata.eroded_full.clone()).unwrap();
let h = image_newest::imageops::resize(&h, 1024, 1024, FilterType::Nearest);
Expand All @@ -54,7 +55,7 @@ pub fn generate_selected_do(c: &mut CheckBrowser, vegetation_maps: &mut Vegetati
let m = Map {
biom: "TemperateZone".parse().unwrap(),
height_map_path: h,
texture_map_path: vegetation_maps.clone().soil_int,
texture_map_path: filedata.soil.clone(),
height_conversion: 1.0,
max_soil_depth: 300.0,
pixel_size: 100.0
Expand Down Expand Up @@ -86,16 +87,20 @@ pub fn generate_selected_do(c: &mut CheckBrowser, vegetation_maps: &mut Vegetati
}
}
let reflection_coefficient = 0.1;
sim_config.calculate_maps(&sun_config, reflection_coefficient, vegetation_maps);
sim_config.calculate_probabilities(vegetation_maps, to_be_generated.as_slice(), sun_config.daylight_hours, vegetation_collection);
if filedata.datamaps.orography.image.is_empty() || filedata.datamaps.hydrology.image.is_empty() || filedata.datamaps.edaphology.image.is_empty() || filedata.datamaps.insolation.image.is_empty() {
sim_config.calculate_maps(&sun_config, reflection_coefficient, &mut filedata.datamaps);
}
sim_config.calculate_probabilities(&mut filedata.datamaps, to_be_generated.as_slice(), sun_config.daylight_hours, &mut filedata.vegetation_maps);
}

// TODO falla aquí - option none dice
pub fn collect_values(w: &mut CheckBrowser, data: &mut VegetationData) {
let nitems = w.nitems();
for i in 0..nitems {
println!("{:?}", w.nitems());
println!("{:?}", w.text((i + 1) as i32));
data.vegetationlist.clear();
data.vegetationlist.insert(w.text(i as i32).unwrap(), w.checked(i as i32));
data.vegetationlist.insert(w.text((i + 1) as i32).unwrap(), w.checked((i+1) as i32));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/ui.fl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class UserInterface {open
Function {make_window()} {open
} {
Fl_Window main_window {
label {OpenBattlesim Map Generator} open
xywh {51 237 870 579} type Single color 55 visible
label {OpenBattlesim Map Generator} open selected
xywh {306 248 870 579} type Single color 55 visible
} {
Fl_Menu_Bar menu_bar {open
xywh {-1 0 870 30} color 55
Expand Down Expand Up @@ -152,12 +152,12 @@ class UserInterface {open
}
Fl_Group weather_pane {
label Weather open
xywh {0 60 870 520}
xywh {0 60 870 520} hide
} {
Fl_Group {} {open
xywh {0 60 870 520} box BORDER_BOX color 7
} {
Fl_Group weather_preview {open selected
Fl_Group weather_preview {open
xywh {28 99 447 446} box BORDER_FRAME color 37 labelcolor 8 align 0
} {}
Fl_Group {} {
Expand Down Expand Up @@ -248,7 +248,7 @@ class UserInterface {open
}
Fl_Group soil_pane {
label Soil open
xywh {0 60 870 520} color 7 hide
xywh {0 60 870 520} color 7
} {
Fl_Group {} {open
xywh {0 60 870 520}
Expand Down
2 changes: 1 addition & 1 deletion target/.rustc_info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"rustc_fingerprint":17218152085934761169,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.79.0-nightly (0bf471f33 2024-04-13)\nbinary: rustc\ncommit-hash: 0bf471f339837af930ec90ef5e1e9cb232e99f29\ncommit-date: 2024-04-13\nhost: x86_64-pc-windows-msvc\nrelease: 1.79.0-nightly\nLLVM version: 18.1.3\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\xcc1006\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\npacked\n___\ndebug_assertions\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""}},"successes":{}}
{"rustc_fingerprint":6129803241811068645,"outputs":{"15341137518005059754":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\xcc1006\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\npacked\n___\nclippy\ndebug_assertions\nfeature=\"cargo-clippy\"\noverflow_checks\npanic=\"unwind\"\nproc_macro\nrelocation_model=\"pic\"\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"lahfsahf\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_has_atomic\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"128\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store\ntarget_has_atomic_load_store=\"128\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nub_checks\nwindows\n","stderr":""},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.79.0-nightly (0bf471f33 2024-04-13)\nbinary: rustc\ncommit-hash: 0bf471f339837af930ec90ef5e1e9cb232e99f29\ncommit-date: 2024-04-13\nhost: x86_64-pc-windows-msvc\nrelease: 1.79.0-nightly\nLLVM version: 18.1.3\n","stderr":""}},"successes":{}}
Binary file modified test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f3522e2

Please sign in to comment.