mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-26 01:46:36 +00:00
Refactoring
This commit is contained in:
parent
97aa376e3c
commit
703fe1a62b
29
src/main.rs
29
src/main.rs
@ -119,8 +119,8 @@ struct PatternConfig {
|
|||||||
shift: bool,
|
shift: bool,
|
||||||
invert: bool,
|
invert: bool,
|
||||||
swap: bool,
|
swap: bool,
|
||||||
segments: u8,
|
segments: f32,
|
||||||
slices: u8,
|
slices: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Args {
|
impl Args {
|
||||||
@ -130,8 +130,8 @@ impl Args {
|
|||||||
true,
|
true,
|
||||||
self.char_invert.unwrap_or(rng.gen()),
|
self.char_invert.unwrap_or(rng.gen()),
|
||||||
self.char_swap.unwrap_or(rng.gen()),
|
self.char_swap.unwrap_or(rng.gen()),
|
||||||
self.char_segments.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)),
|
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_shift.unwrap_or(rng.gen()),
|
||||||
self.color_invert.unwrap_or(rng.gen()),
|
self.color_invert.unwrap_or(rng.gen()),
|
||||||
self.color_swap.unwrap_or(rng.gen()),
|
self.color_swap.unwrap_or(rng.gen()),
|
||||||
1,
|
1.0,
|
||||||
self.color_slices.unwrap_or(rng.gen_range(1..=4)),
|
self.color_slices.unwrap_or(rng.gen_range(1..=4)) as f32,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,10 +213,10 @@ impl PatternConfig {
|
|||||||
if self.swap {
|
if self.swap {
|
||||||
pattern = Box::new(SwapFactory::new(pattern))
|
pattern = Box::new(SwapFactory::new(pattern))
|
||||||
}
|
}
|
||||||
if self.segments != 1 {
|
if self.segments != 1.0 {
|
||||||
pattern = Box::new(SegmentsFactory::new(pattern, self.segments));
|
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 = Box::new(SliceFactory::new(pattern, self.slices));
|
||||||
}
|
}
|
||||||
pattern
|
pattern
|
||||||
@ -257,6 +257,7 @@ fn main() -> Result<(), Error> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use approx::*;
|
||||||
use rand::rngs::mock::StepRng;
|
use rand::rngs::mock::StepRng;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -335,7 +336,7 @@ mod test {
|
|||||||
char_segments: Some(12),
|
char_segments: Some(12),
|
||||||
..Args::default()
|
..Args::default()
|
||||||
};
|
};
|
||||||
assert_eq!(12, args.char_config(rng).segments);
|
assert_abs_diff_eq!(12.0, args.char_config(rng).segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -345,7 +346,7 @@ mod test {
|
|||||||
char_slices: Some(42),
|
char_slices: Some(42),
|
||||||
..Args::default()
|
..Args::default()
|
||||||
};
|
};
|
||||||
assert_eq!(42, args.char_config(rng).slices);
|
assert_abs_diff_eq!(42.0, args.char_config(rng).slices);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -393,7 +394,7 @@ mod test {
|
|||||||
let rng = &mut StepRng::new(1, 1);
|
let rng = &mut StepRng::new(1, 1);
|
||||||
let args = Args::default();
|
let args = Args::default();
|
||||||
|
|
||||||
assert_eq!(1, args.color_config(rng).segments);
|
assert_abs_diff_eq!(1.0, args.color_config(rng).segments);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -403,7 +404,7 @@ mod test {
|
|||||||
color_slices: Some(23),
|
color_slices: Some(23),
|
||||||
..Args::default()
|
..Args::default()
|
||||||
};
|
};
|
||||||
assert_eq!(23, args.color_config(rng).slices);
|
assert_abs_diff_eq!(23.0, args.color_config(rng).slices);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -414,8 +415,8 @@ mod test {
|
|||||||
shift: true,
|
shift: true,
|
||||||
invert: true,
|
invert: true,
|
||||||
swap: true,
|
swap: true,
|
||||||
segments: 3,
|
segments: 3.0,
|
||||||
slices: 2,
|
slices: 2.0,
|
||||||
};
|
};
|
||||||
config
|
config
|
||||||
.create()
|
.create()
|
||||||
|
@ -4,13 +4,13 @@ use crate::Vector;
|
|||||||
#[derive(derive_more::Constructor)]
|
#[derive(derive_more::Constructor)]
|
||||||
pub struct SegmentsFactory {
|
pub struct SegmentsFactory {
|
||||||
child: Box<dyn PatternFactory>,
|
child: Box<dyn PatternFactory>,
|
||||||
segments: u8,
|
segments: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(derive_more::Constructor)]
|
#[derive(derive_more::Constructor)]
|
||||||
pub struct Segments {
|
pub struct Segments {
|
||||||
child: Box<dyn Pattern>,
|
child: Box<dyn Pattern>,
|
||||||
segments: u8,
|
segments: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PatternFactory for SegmentsFactory {
|
impl PatternFactory for SegmentsFactory {
|
||||||
@ -24,7 +24,7 @@ impl Pattern for Segments {
|
|||||||
let sample = self.child.sample(pos);
|
let sample = self.child.sample(pos);
|
||||||
|
|
||||||
if 0.0 <= sample && sample < 1.0 {
|
if 0.0 <= sample && sample < 1.0 {
|
||||||
(sample * self.segments as f32) % 1.0
|
sample * self.segments % 1.0
|
||||||
} else {
|
} else {
|
||||||
sample
|
sample
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ mod test {
|
|||||||
.once()
|
.once()
|
||||||
.returning(|_| Box::new(MockPattern::new()));
|
.returning(|_| Box::new(MockPattern::new()));
|
||||||
|
|
||||||
SegmentsFactory::new(Box::new(child), 2).create(&config);
|
SegmentsFactory::new(Box::new(child), 2.0).create(&config);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -63,7 +63,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(1.1, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(-0.1, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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);
|
assert_abs_diff_eq!(0.96, sampler.sample(Vector::default()), epsilon = 0.01);
|
||||||
}
|
}
|
||||||
@ -105,7 +105,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_eq!(0.96, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -133,7 +133,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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));
|
sampler.sample(Vector::new(5.0, 1.0));
|
||||||
}
|
}
|
||||||
|
@ -1,33 +1,34 @@
|
|||||||
use crate::pattern::*;
|
use crate::pattern::*;
|
||||||
use crate::Vector;
|
use crate::Vector;
|
||||||
|
|
||||||
#[derive(derive_more::Constructor)]
|
|
||||||
pub struct SliceFactory {
|
pub struct SliceFactory {
|
||||||
child: Box<dyn PatternFactory>,
|
child: Box<dyn PatternFactory>,
|
||||||
slices: u8,
|
width: f32,
|
||||||
|
rest: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(derive_more::Constructor)]
|
||||||
pub struct Slice {
|
pub struct Slice {
|
||||||
child: Box<dyn Pattern>,
|
child: Box<dyn Pattern>,
|
||||||
width: f32,
|
width: f32,
|
||||||
rest: f32,
|
rest: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PatternFactory for SliceFactory {
|
impl SliceFactory {
|
||||||
fn create(&self, config: &Config) -> Box<dyn Pattern> {
|
pub fn new(child: Box<dyn PatternFactory>, slices: f32) -> Self {
|
||||||
Box::new(Slice::new(self.child.create(config), self.slices))
|
let width = 1.0 / slices;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Slice {
|
|
||||||
pub fn new(child: Box<dyn Pattern>, slices: u8) -> Self {
|
|
||||||
let width = 1.0 / slices as f32;
|
|
||||||
let rest = 1.0 - width;
|
let rest = 1.0 - width;
|
||||||
|
|
||||||
Self { child, width, rest }
|
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 {
|
impl Pattern for Slice {
|
||||||
fn sample(&self, pos: Vector) -> f32 {
|
fn sample(&self, pos: Vector) -> f32 {
|
||||||
(self.child.sample(pos) - self.rest) / self.width
|
(self.child.sample(pos) - self.rest) / self.width
|
||||||
@ -54,7 +55,7 @@ mod test {
|
|||||||
.once()
|
.once()
|
||||||
.returning(|_| Box::new(MockPattern::new()));
|
.returning(|_| Box::new(MockPattern::new()));
|
||||||
|
|
||||||
SliceFactory::new(Box::new(child), 4).create(&config);
|
SliceFactory::new(Box::new(child), 4.0).create(&config);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -66,7 +67,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(1.0, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -80,7 +81,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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()));
|
assert_abs_diff_eq!(0.0, sampler.sample(Vector::default()));
|
||||||
}
|
}
|
||||||
@ -94,7 +95,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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);
|
assert!(sampler.sample(Vector::default()) < 0.0);
|
||||||
}
|
}
|
||||||
@ -112,7 +113,7 @@ mod test {
|
|||||||
Box::new(sampler)
|
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));
|
sampler.sample(Vector::new(3.0, 5.0));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user