Skip to content

Commit

Permalink
Finishing soil panel, fixed vegetation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
skalimoi committed May 16, 2024
1 parent f3522e2 commit e607497
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ target
.replit
*.ron
/.idea
soil_test.png
soil_test_main.png
soil_veg_test.png
test.png
test_16.png
31 changes: 19 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use fltk::app::Sender;
use map_range::MapRange;
use fltk::image::{RgbImage, SharedImage};
use fltk::{*, prelude::*};
use image_crate::{DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, Luma, Pixel};
use image_crate::{DynamicImage, EncodableLayout, GenericImageView, ImageBuffer, Luma, Pixel, Rgb};
use noise::utils::NoiseMapBuilder;
use noise::{Fbm, MultiFractal, Perlin, Seedable};
use rand::{Rng, thread_rng};
Expand Down Expand Up @@ -228,7 +228,7 @@ fn new_do(program_data: &mut FileData) {
}

fn set_data(loaded_data: &mut FileData, data: &mut FileData) {
let _ = swap::<FileData>(data, &mut loaded_data.clone());
let _ = replace::<FileData>(data, loaded_data.clone());
}

fn export_do(program_data: &mut FileData) {
Expand Down Expand Up @@ -686,10 +686,7 @@ fn main() {
blocklist: soilchoices,
vegetationlist: HashMap::new()
};

let mut veg_collection = VegetationCollection {
generated: HashMap::new(),
};


let mut dir: PathBuf = PathBuf::new();

Expand All @@ -711,18 +708,24 @@ fn main() {
gl_widget.show();
}
Message::NextVeg => {
println!("{:?}", file.vegetation_maps.generated);
if index == index_list.len() {
index = 0;
}
dbg!(&index_list.len());
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();
let b: ImageBuffer<Luma<u8>, Vec<u8>> = ImageBuffer::from_raw(1024, 1024, map.clone()).unwrap();
let i = RgbImage::new(b.into_raw().as_slice(), 1024, 1024, ColorDepth::L8).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 => {
file.vegetation_maps.generated.clear();
generate_selected_do(&mut ui.vegetation_list, &mut soil_veg_params, &mut file);
dbg!(file.vegetation_maps.generated.keys());
for element in file.vegetation_maps.clone().generated.into_iter() {
let name = element.0.clone();
index_list.push(name);
Expand Down Expand Up @@ -827,6 +830,9 @@ fn main() {
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);
file.soil = soil;
//TODO VER POR QUÉ SALE MAL LA IMAGEN, SERÁ PORQUE ES RGB EN VEZ DE LUMA?
let x: ImageBuffer<Luma<u8>, Vec<u8>> = ImageBuffer::from_raw(1024, 1024, file.soil.clone()).unwrap();
x.save("soil_test_main.png");
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();
Expand Down Expand Up @@ -1062,6 +1068,7 @@ fn main() {
file.raw_map_512 = p.0.raw_map_512;
file.soil = p.0.soil;
file.vegetation_maps = p.0.vegetation_maps;
file.datamaps = p.0.datamaps;

if !file.color_map_512.is_empty() {
topography::update_noise_img(&mut ui.preview_box_topo, &file, 0, ColorDepth::Rgb8);
Expand Down Expand Up @@ -1090,11 +1097,11 @@ fn main() {
ui.detail_level.redraw();
ui.erod_scale.set_value(format!("{}", &file.topography.erod_scale.clone()).as_str().parse().unwrap());
ui.erod_scale.redraw();
ui.min_height_input.set_value(file.topography.min_height.clone() as f64);
ui.min_height_input.set_value(file.topography.min_height as f64);
ui.min_height_input.redraw();
ui.max_height_input.set_value(file.topography.max_height.clone() as f64);
ui.max_height_input.set_value(file.topography.max_height as f64);
ui.max_height_input.redraw();
ui.erosion_cycles_input.set_value(file.topography.erosion_cycles.clone() as f64);
ui.erosion_cycles_input.set_value(file.topography.erosion_cycles as f64);
ui.erosion_cycles_input.redraw();
ui.weather_seed_input.set_value(format!("{}", &file.weather.seed.unwrap().clone()).as_str());
ui.weather_seed_input.redraw();
Expand All @@ -1119,7 +1126,7 @@ fn main() {
terrain_material.clone(),
Arc::new(
move |x, y| {
map_b.clone().get_pixel(x as u32, y as u32).channels().first().unwrap().clone() as f32 * 0.01
*map_b.clone().get_pixel(x as u32, y as u32).channels().first().unwrap() as f32 * 0.01
}
),
512.0,
Expand Down
9 changes: 5 additions & 4 deletions src/soil/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::soil_def::{VegetationCollection, VegetationMaps};
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct GreyscaleImage<T> {
pub image: Vec<T>,
len: usize,
pub len: usize,
}

impl<T> GreyscaleImage<T> {
Expand Down Expand Up @@ -222,9 +222,10 @@ impl SimConfig {
// TODO: HACER STRUCT SOLO PARA ESTAS IMAGENES Y NO TENER QUE GUARDARLAS
// TODO: COMPROBAR EL TEMA IMAGEN SI LO COGE BIEN O NO
// TODO: TERMINAR DE CONFIGURAR SOIL_DEF.RS
pub fn calculate_probabilities(&self, mapdata: &VegetationMaps, vegetation_names: &[&str], _daylight_hours: i32, vegetation_collection: &mut VegetationCollection) {
pub fn calculate_probabilities(&self, mapdata: &mut VegetationMaps, vegetation_names: &[&str], _daylight_hours: i32, vegetation_collection: &mut VegetationCollection) {
let soil_ids_map = GreyscaleImage::new(self.maps.texture_map_path.clone());

let x: ImageBuffer<Luma<u8>, Vec<u8>> = ImageBuffer::from_raw(1024, 1024, self.maps.texture_map_path.clone()).unwrap();
x.save("soil_veg_test.png");
for vegetation in vegetation_names {
let probabilities_map = calculate_probabilities(
&self.vegetations[*vegetation],
Expand All @@ -241,7 +242,7 @@ impl SimConfig {
)
.unwrap();
imageproc::contrast::stretch_contrast_mut(&mut probabilities_image, 100, 255);
vegetation_collection.generated.insert(vegetation.clone().parse().unwrap(), probabilities_image.into_raw());
vegetation_collection.generated.insert(vegetation.parse().unwrap(), probabilities_image.into_raw());
}
}
}
5 changes: 3 additions & 2 deletions src/soil/soilmaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ pub fn init_soilmaker(f: &mut Frame, base_soil: SoilType, blocklist: &HashMap<So
let colormap = color_reduce::palette::BasePalette::new(
color_vec
);
quantize(&mut old_to_convert, &colormap, QuantizeMethod::CIE2000, None);
quantize(&mut old_to_convert, &colormap, QuantizeMethod::Luma, None);
let i = image_newest::imageops::resize(&old_to_convert, 1024, 1024, image_newest::imageops::FilterType::Nearest);
i.save("soil_test.png");
f.set_image_scaled(None::<SharedImage>);
let s = RgbImage::new(i.as_raw().as_slice(), 1024, 1024, ColorDepth::Rgb8).unwrap();
f.set_image(SharedImage::from_image(s).ok());
Expand Down Expand Up @@ -185,7 +186,7 @@ fn generate_low_soils(_i: &mut DynamicImage, h_map: &DynamicImage) -> DynamicIma
let expanded_1 = imageproc::morphology::dilate(&expanded, Norm::L1, 3);
let expanded = imageproc::morphology::close(&expanded_1, Norm::L1, 8);
let mut converted_to_old_buffer: buffer_old<Rgb_old<u8>, Vec<u8>> = buffer_old::from_raw(DIM as u32, DIM as u32, ImageLuma8(clay).into_rgb8().into_raw()).unwrap();
quantize(&mut converted_to_old_buffer, &index, QuantizeMethod::CIE2000, None);
quantize(&mut converted_to_old_buffer, &index, QuantizeMethod::Luma, None);
let mut base = DynamicImage::ImageRgb8(ImageBuffer::from_raw(DIM as u32, DIM as u32, converted_to_old_buffer.into_raw()).unwrap());
for x in 0..DIM {
for y in 0..DIM {
Expand Down
17 changes: 8 additions & 9 deletions src/soil_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub struct 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);
let h = image_newest::imageops::resize(&h, 1024, 1024, FilterType::CatmullRom);
let h = GreyscaleImage::new(h.into_raw().into_iter()
.map(|x| x as f64)
.collect());
let m = Map {
biom: "TemperateZone".parse().unwrap(),
biom: "ArcticZone".parse().unwrap(),
height_map_path: h,
texture_map_path: filedata.soil.clone(),
height_conversion: 1.0,
Expand All @@ -63,7 +63,6 @@ pub fn generate_selected_do(c: &mut CheckBrowser, vegdata: &mut VegetationData,
let mut data = String::new();
File::open("bioms.yml").unwrap().read_to_string(&mut data).unwrap();
let bioms: HashMap<String, Biom> = serde_yaml::from_str(&data).unwrap();

let mut data = String::new();
File::open("soil_types.yml").unwrap().read_to_string(&mut data).unwrap();
let soils: HashMap<String, Soil> = serde_yaml::from_str(&data).unwrap();
Expand All @@ -82,24 +81,24 @@ pub fn generate_selected_do(c: &mut CheckBrowser, vegdata: &mut VegetationData,
let sim_config = SimConfig::from_configs(m, bioms, soils, vegetations);
let mut to_be_generated: Vec<&str> = Vec::new();
for (vegetation, status) in &vegdata.vegetationlist {
if status.clone() == true {
if *status {
to_be_generated.push(vegetation.as_str());
}
}
dbg!(&vegdata.vegetationlist);
let reflection_coefficient = 0.1;
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() {

if 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) {
data.vegetationlist.clear();
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 + 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,10 +7,10 @@ class UserInterface {open
Function {make_window()} {open
} {
Fl_Window main_window {
label {OpenBattlesim Map Generator} open selected
label {OpenBattlesim Map Generator} open
xywh {306 248 870 579} type Single color 55 visible
} {
Fl_Menu_Bar menu_bar {open
Fl_Menu_Bar menu_bar {open selected
xywh {-1 0 870 30} color 55
} {}
Fl_Tabs {} {open
Expand Down Expand Up @@ -288,11 +288,11 @@ class UserInterface {open
}
}
Fl_Group {} {
label {Soil preview} open
xywh {12 337 235 230}
label {Vegetation preview} open
xywh {12 337 238 232}
} {
Fl_Box veg_preview {
xywh {12 337 235 13}
xywh {15 339 230 230}
}
}
}
Expand Down
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 e607497

Please sign in to comment.