use std::marker::PhantomData; use crate::generation::blender::BlenderImpl; use super::{ component_functions::{ ApplierImpl, ComponentFunctionImpl, ComponentReference, ComponentReferenceImplementation, ConverterEnvironment, ConverterImpl, DensityFunctionEnvironment, EnvironmentApplierImpl, ImmutableComponentFunctionImpl, MutableComponentFunctionImpl, MutableComponentReference, NoEnvironment, OwnedConverterEnvironment, SharedComponentReference, SharedConverterEnvironment, }, NoisePos, NoisePosImpl, }; pub struct BlendOffsetFunction {} impl BlendOffsetFunction { pub const INSTANCE: Self = Self {}; } impl ComponentFunctionImpl for BlendOffsetFunction {} impl ImmutableComponentFunctionImpl for BlendOffsetFunction { #[inline] fn sample(&self, _pos: &NoisePos) -> f64 { 0f64 } #[inline] fn fill(&self, arr: &mut [f64], _applier: &mut dyn ApplierImpl) { arr.fill(0f64); } fn shared_environment(&self) -> SharedConverterEnvironment { SharedConverterEnvironment::BlendOffset } } pub struct BlendAlphaFunction {} impl BlendAlphaFunction { pub const INSTANCE: Self = Self {}; } impl ComponentFunctionImpl for BlendAlphaFunction {} impl ImmutableComponentFunctionImpl for BlendAlphaFunction { #[inline] fn sample(&self, _pos: &NoisePos) -> f64 { 1f64 } #[inline] fn fill(&self, arr: &mut [f64], _applier: &mut dyn ApplierImpl) { arr.fill(1f64); } fn shared_environment(&self) -> SharedConverterEnvironment { SharedConverterEnvironment::BlendAlpha } } pub struct BlendDensityFunction> { pub(crate) function: R, _dummy: PhantomData, } impl> From> for MutableComponentReference { fn from(value: BlendDensityFunction) -> Self { Self(Box::new(value)) } } impl> BlendDensityFunction { pub fn new(function: R) -> Self { Self { function, _dummy: PhantomData:: {}, } } #[inline] fn apply_density(&self, pos: &NoisePos, density: f64) -> f64 { pos.get_blender().apply_blend_density(pos, density) } pub fn create_new_ref( function: ComponentReferenceImplementation, ) -> ComponentReferenceImplementation { match function { ComponentReferenceImplementation::Shared(shared) => { ComponentReferenceImplementation::Shared( BlendDensityFunction::::new(shared) .into(), ) } ComponentReferenceImplementation::Mutable(owned) => { ComponentReferenceImplementation::Mutable(BlendDensityFunction::new(owned).into()) } } } } impl> ComponentFunctionImpl for BlendDensityFunction { } impl ImmutableComponentFunctionImpl for BlendDensityFunction { #[inline] fn sample(&self, pos: &NoisePos) -> f64 { let density = self.function.sample(pos); self.apply_density(pos, density) } #[inline] fn fill(&self, arr: &mut [f64], applier: &mut dyn ApplierImpl) { self.function.fill(arr, applier); arr.iter_mut().enumerate().for_each(|(i, x)| { *x = self.apply_density(&applier.at(i), *x); }); } fn shared_environment(&self) -> SharedConverterEnvironment { SharedConverterEnvironment::BlendDensity(&self.function) } } impl> MutableComponentFunctionImpl for BlendDensityFunction { #[inline] fn sample_mut(&mut self, pos: &NoisePos, env: &E) -> f64 { let density = self.function.sample_mut(pos, env); self.apply_density(pos, density) } #[inline] fn fill_mut(&mut self, arr: &mut [f64], applier: &mut dyn EnvironmentApplierImpl) { self.function.fill_mut(arr, applier); arr.iter_mut().enumerate().for_each(|(i, x)| { *x = self.apply_density(&applier.at(i), *x); }); } fn environment(&self) -> ConverterEnvironment { ConverterEnvironment::BlendDensity(&self.function) } fn into_environment(self: Box) -> OwnedConverterEnvironment { OwnedConverterEnvironment::BlendDensity(self.function.wrapped_ref()) } fn convert( self: Box, converter: &mut dyn ConverterImpl, ) -> ComponentReferenceImplementation { Self::create_new_ref(self.function.convert(converter)) } fn clone_to_new_ref(&self) -> ComponentReferenceImplementation { Self::create_new_ref(self.function.clone_to_new_ref()) } }