Add shift transform

This commit is contained in:
Rico Riedel 2022-08-03 18:00:26 +02:00
parent 32cd72552e
commit 74e83cd543
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
3 changed files with 122 additions and 60 deletions

View File

@ -1,5 +1,6 @@
pub mod convert; pub mod convert;
pub mod pattern; pub mod pattern;
pub mod transform;
mod error; mod error;
mod printer; mod printer;
@ -17,6 +18,7 @@ pub use vec::*;
use crate::convert::*; use crate::convert::*;
use crate::pattern::*; use crate::pattern::*;
use crate::transform::*;
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use crossterm::style::Color; use crossterm::style::Color;
use crossterm::style::Color::*; use crossterm::style::Color::*;
@ -43,6 +45,8 @@ struct Args {
colors: Vec<PalletEnum>, colors: Vec<PalletEnum>,
#[clap(long, value_enum)] #[clap(long, value_enum)]
color_pattern: Vec<PatternEnum>, color_pattern: Vec<PatternEnum>,
#[clap(long)]
color_shift: Option<bool>,
} }
#[derive(ValueEnum, Clone)] #[derive(ValueEnum, Clone)]
@ -81,53 +85,23 @@ enum PatternEnum {
Wheel, Wheel,
} }
fn main() -> Result<(), Error> { #[derive(derive_more::Constructor)]
let args = Args::parse(); struct PatternConfig<'a> {
let rand = &mut thread_rng(); patterns: &'a Vec<PatternEnum>,
shift: Option<bool>,
let char: Box<dyn PatternFactory> = create_pattern(choose(args.char_pattern, rand));
let color: Box<dyn PatternFactory> = create_pattern(choose(args.color_pattern, rand));
let pallet = create_pallet(choose(args.colors, rand));
let sampler = SamplerFactoryImpl::new(char, color);
let char_converter = CharConverterImpl::new(args.chars);
let color_converter = ColorConverterImpl::new(pallet);
let converter = ConverterImpl::new(char_converter, color_converter);
let term = TerminalImpl::new(stdout());
let printer = PrinterImpl::new(term)?;
let renderer = RendererImpl::new(sampler, converter, printer);
let clock = ClockImpl::default();
let duration = Duration::from_millis(args.duration);
let delay = Duration::from_nanos(1_000_000_000 / args.fps);
let timer = Timer::new(clock, duration, delay);
timer.run(renderer)
} }
fn choose<TValue: ValueEnum + Clone, TRand: Rng>(options: Vec<TValue>, rand: &mut TRand) -> TValue { impl Args {
if options.is_empty() { fn char_config(&self) -> PatternConfig {
TValue::value_variants() PatternConfig::new(&self.char_pattern, Some(true))
.iter()
.choose(rand)
.unwrap()
.clone()
} else {
options.iter().choose(rand).unwrap().clone()
}
} }
fn create_pattern(pattern: PatternEnum) -> Box<dyn PatternFactory> { fn color_config(&self) -> PatternConfig {
match pattern { PatternConfig::new(&self.color_pattern, self.color_shift)
PatternEnum::Circle => Box::new(CircleFactory::default()),
PatternEnum::Line => Box::new(LineFactory::default()),
PatternEnum::Rhombus => Box::new(RhombusFactory::default()),
PatternEnum::Wheel => Box::new(WheelFactory::default()),
}
} }
fn create_pallet(colors: PalletEnum) -> Vec<Color> { fn pallet(&self, rand: &mut impl Rng) -> Vec<Color> {
match colors { match choose(&self.colors, rand) {
PalletEnum::Red => vec![DarkRed, Red, White], PalletEnum::Red => vec![DarkRed, Red, White],
PalletEnum::Yellow => vec![DarkYellow, Yellow, White], PalletEnum::Yellow => vec![DarkYellow, Yellow, White],
PalletEnum::Green => vec![DarkGreen, Green, White], PalletEnum::Green => vec![DarkGreen, Green, White],
@ -161,3 +135,63 @@ fn create_pallet(colors: PalletEnum) -> Vec<Color> {
PalletEnum::Gray => vec![Black, DarkGrey, Grey, White], PalletEnum::Gray => vec![Black, DarkGrey, Grey, White],
} }
} }
}
impl<'a> PatternConfig<'a> {
fn create_base(&self, rand: &mut impl Rng) -> Box<dyn PatternFactory> {
match choose(self.patterns, rand) {
PatternEnum::Circle => Box::new(CircleFactory::default()),
PatternEnum::Line => Box::new(LineFactory::default()),
PatternEnum::Rhombus => Box::new(RhombusFactory::default()),
PatternEnum::Wheel => Box::new(WheelFactory::default()),
}
}
fn create(&self, rand: &mut impl Rng) -> Box<dyn PatternFactory> {
let mut pattern = self.create_base(rand);
if self.shift.unwrap_or(rand.gen()) {
pattern = Box::new(ShiftFactory::new(pattern))
}
pattern
}
}
fn choose<TValue: ValueEnum + Clone, TRand: Rng>(
options: &Vec<TValue>,
rand: &mut TRand,
) -> TValue {
if options.is_empty() {
TValue::value_variants()
.iter()
.choose(rand)
.unwrap()
.clone()
} else {
options.iter().choose(rand).unwrap().clone()
}
}
fn main() -> Result<(), Error> {
let args = Args::parse();
let rand = &mut thread_rng();
let char = args.char_config().create(rand);
let color = args.color_config().create(rand);
let pallet = args.pallet(rand);
let sampler = SamplerFactoryImpl::new(char, color);
let char_converter = CharConverterImpl::new(args.chars);
let color_converter = ColorConverterImpl::new(pallet);
let converter = ConverterImpl::new(char_converter, color_converter);
let term = TerminalImpl::new(stdout());
let printer = PrinterImpl::new(term)?;
let renderer = RendererImpl::new(sampler, converter, printer);
let clock = ClockImpl::default();
let duration = Duration::from_millis(args.duration);
let delay = Duration::from_nanos(1_000_000_000 / args.fps);
let timer = Timer::new(clock, duration, delay);
timer.run(renderer)
}

3
src/transform/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod shift;
pub use shift::*;

25
src/transform/shift.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::pattern::*;
use crate::Vector;
#[derive(derive_more::Constructor)]
pub struct ShiftFactory {
child: Box<dyn PatternFactory>,
}
#[derive(derive_more::Constructor)]
pub struct Shift {
child: Box<dyn Pattern>,
shift: f32,
}
impl PatternFactory for ShiftFactory {
fn create(&self, config: &Config) -> Box<dyn Pattern> {
Box::new(Shift::new(self.child.create(config), config.step))
}
}
impl Pattern for Shift {
fn sample(&self, pos: Vector) -> f32 {
self.child.sample(pos) + 1.0 - 2.0 * self.shift
}
}