Skip to content

Commit

Permalink
Merge pull request #29 from jmbhughes/add-ifs-randomizer
Browse files Browse the repository at this point in the history
adds randomize, updates version
  • Loading branch information
jmbhughes committed Jan 13, 2024
2 parents d8f9518 + 0a91d15 commit 3670e51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "barnsley"
version = "0.2.1"
version = "0.2.2"
edition = "2021"
authors = ["Marcus Hughes <hughes.jmb@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand Down
9 changes: 8 additions & 1 deletion src/ifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use rand::prelude::*;
use rand::distributions::WeightedIndex;
use rand_distr::Distribution;
use crate::transform;
use crate::util::*;
use crate::transform::*;
use crate::image::*;
Expand All @@ -28,6 +29,13 @@ impl IFS{
distribution: WeightedIndex::new([1.]).unwrap()}
}

/// Update the IFS to new random weights and random parameters for each transform
pub fn randomize(&mut self) {
self.transforms = self.transforms.iter().map(|t| transform_from_str(t.get_name())).collect();
self.distribution = WeightedIndex::new(self.transforms.iter().map(|t| t.get_weight())).unwrap();
self.total_weight = self.transforms.iter().map(|t| t.get_weight()).sum();
}

pub fn len(&self) -> usize {
self.num_transforms
}
Expand Down Expand Up @@ -84,7 +92,6 @@ impl IFS{
fn choose_transform(&self) -> &Transform {
let mut rng = thread_rng();
self.transforms.get(self.distribution.sample(&mut rng)).unwrap()
//self.transforms.get(self.distribution.sample(&mut rng)).unwrap()
}

/// Evaluate a transform
Expand Down

0 comments on commit 3670e51

Please sign in to comment.