Refactoring

This commit is contained in:
Rico Riedel 2022-08-06 13:43:32 +02:00
parent 97aa376e3c
commit 703fe1a62b
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
3 changed files with 43 additions and 41 deletions

View File

@ -119,8 +119,8 @@ struct PatternConfig {
shift: bool,
invert: bool,
swap: bool,
segments: u8,
slices: u8,
segments: f32,
slices: f32,
}
impl Args {
@ -130,8 +130,8 @@ impl Args {
true,
self.char_invert.unwrap_or(rng.gen()),
self.char_swap.unwrap_or(rng.gen()),
self.char_segments.unwrap_or(rng.gen_range(1..=4)),
self.char_slices.unwrap_or(rng.gen_range(1..=4)),
self.char_segments.unwrap_or(rng.gen_range(1..=4)) as f32,
self.char_slices.unwrap_or(rng.gen_range(1..=4)) as f32,
)
}
@ -141,8 +141,8 @@ impl Args {
self.color_shift.unwrap_or(rng.gen()),
self.color_invert.unwrap_or(rng.gen()),
self.color_swap.unwrap_or(rng.gen()),
1,
self.color_slices.unwrap_or(rng.gen_range(1..=4)),
1.0,
self.color_slices.unwrap_or(rng.gen_range(1..=4)) as f32,
)
}
@ -213,10 +213,10 @@ impl PatternConfig {
if self.swap {
pattern = Box::new(SwapFactory::new(pattern))
}
if self.segments != 1 {
if self.segments != 1.0 {
pattern = Box::new(SegmentsFactory::new(pattern, self.segments));
}
if self.slices != 1 {
if self.slices != 1.0 {
pattern = Box::new(SliceFactory::new(pattern, self.slices));
}
pattern
@ -257,6 +257,7 @@ fn main() -> Result<(), Error> {
#[cfg(test)]
mod test {
use super::*;
use approx::*;
use rand::rngs::mock::StepRng;
#[test]
@ -335,7 +336,7 @@ mod test {
char_segments: Some(12),
..Args::default()
};
assert_eq!(12, args.char_config(rng).segments);
assert_abs_diff_eq!(12.0, args.char_config(rng).segments);
}
#[test]
@ -345,7 +346,7 @@ mod test {
char_slices: Some(42),
..Args::default()
};
assert_eq!(42, args.char_config(rng).slices);
assert_abs_diff_eq!(42.0, args.char_config(rng).slices);
}
#[test]
@ -393,7 +394,7 @@ mod test {
let rng = &mut StepRng::new(1, 1);
let args = Args::default();
assert_eq!(1, args.color_config(rng).segments);
assert_abs_diff_eq!(1.0, args.color_config(rng).segments);
}
#[test]
@ -403,7 +404,7 @@ mod test {
color_slices: Some(23),
..Args::default()
};
assert_eq!(23, args.color_config(rng).slices);
assert_abs_diff_eq!(23.0, args.color_config(rng).slices);
}
#[test]
@ -414,8 +415,8 @@ mod test {
shift: true,
invert: true,
swap: true,
segments: 3,
slices: 2,
segments: 3.0,
slices: 2.0,
};
config
.create()

View File

@ -4,13 +4,13 @@ use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct SegmentsFactory {
child: Box<dyn PatternFactory>,
segments: u8,
segments: f32,
}
#[derive(derive_more::Constructor)]
pub struct Segments {
child: Box<dyn Pattern>,
segments: u8,
segments: f32,
}
impl PatternFactory for SegmentsFactory {
@ -24,7 +24,7 @@ impl Pattern for Segments {
let sample = self.child.sample(pos);
if 0.0 <= sample && sample < 1.0 {
(sample * self.segments as f32) % 1.0
sample * self.segments % 1.0
} else {
sample
}
@ -51,7 +51,7 @@ mod test {
.once()
.returning(|_| Box::new(MockPattern::new()));
SegmentsFactory::new(Box::new(child), 2).create(&config);
SegmentsFactory::new(Box::new(child), 2.0).create(&config);
}
#[test]
@ -63,7 +63,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 3).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 3.0).create(&Config::default());
assert_abs_diff_eq!(1.1, sampler.sample(Vector::default()));
}
@ -77,7 +77,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 3).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 3.0).create(&Config::default());
assert_abs_diff_eq!(-0.1, sampler.sample(Vector::default()));
}
@ -91,7 +91,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_abs_diff_eq!(0.96, sampler.sample(Vector::default()), epsilon = 0.01);
}
@ -105,7 +105,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
}
@ -119,7 +119,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_eq!(0.96, sampler.sample(Vector::default()));
}
@ -133,7 +133,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
}
@ -151,7 +151,7 @@ mod test {
Box::new(sampler)
});
let sampler = SegmentsFactory::new(Box::new(child), 3).create(&Config::default());
let sampler = SegmentsFactory::new(Box::new(child), 3.0).create(&Config::default());
sampler.sample(Vector::new(5.0, 1.0));
}

View File

@ -1,33 +1,34 @@
use crate::pattern::*;
use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct SliceFactory {
child: Box<dyn PatternFactory>,
slices: u8,
width: f32,
rest: f32,
}
#[derive(derive_more::Constructor)]
pub struct Slice {
child: Box<dyn Pattern>,
width: f32,
rest: f32,
}
impl PatternFactory for SliceFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Slice::new(self.child.create(config), self.slices))
}
}
impl Slice {
pub fn new(child: Box<dyn Pattern>, slices: u8) -> Self {
let width = 1.0 / slices as f32;
impl SliceFactory {
pub fn new(child: Box<dyn PatternFactory>, slices: f32) -> Self {
let width = 1.0 / slices;
let rest = 1.0 - width;
Self { child, width, rest }
}
}
impl PatternFactory for SliceFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Slice::new(self.child.create(config), self.width, self.rest))
}
}
impl Pattern for Slice {
fn sample(&self, pos: Vector) -> f32 {
(self.child.sample(pos) - self.rest) / self.width
@ -54,7 +55,7 @@ mod test {
.once()
.returning(|_| Box::new(MockPattern::new()));
SliceFactory::new(Box::new(child), 4).create(&config);
SliceFactory::new(Box::new(child), 4.0).create(&config);
}
#[test]
@ -66,7 +67,7 @@ mod test {
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SliceFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_abs_diff_eq!(1.0, sampler.sample(Vector::default()));
}
@ -80,7 +81,7 @@ mod test {
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SliceFactory::new(Box::new(child), 4.0).create(&Config::default());
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
}
@ -94,7 +95,7 @@ mod test {
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 4).create(&Config::default());
let sampler = SliceFactory::new(Box::new(child), 4.0).create(&Config::default());
assert!(sampler.sample(Vector::default()) < 0.0);
}
@ -112,7 +113,7 @@ mod test {
Box::new(sampler)
});
let sampler = SliceFactory::new(Box::new(child), 3).create(&Config::default());
let sampler = SliceFactory::new(Box::new(child), 3.0).create(&Config::default());
sampler.sample(Vector::new(3.0, 5.0));
}