libnoise/core/adapters/
translate.rs

1use crate::core::generator::{Generator, Generator1D, Generator2D, Generator3D, Generator4D};
2
3/// A generator which translates input points before passing them to the underlying generator.
4///
5/// For details, see the documentation of [`translate()`]. Typically, this struct is not meant
6/// to be used directly. Instead, [`translate()`] implemented by [`Generator`], should be used
7/// to create [`Translate`].
8///
9/// [`translate()`]: Generator::translate
10#[derive(Clone, Copy, Debug)]
11pub struct Translate<const D: usize, G> {
12    generator: G,
13    translation: [f64; D],
14}
15
16impl<G: Generator<1>> Generator1D for Translate<1, G> {}
17impl<G: Generator<2>> Generator2D for Translate<2, G> {}
18impl<G: Generator<3>> Generator3D for Translate<3, G> {}
19impl<G: Generator<4>> Generator4D for Translate<4, G> {}
20
21impl<const D: usize, G> Translate<D, G>
22where
23    G: Generator<D>,
24{
25    #[inline]
26    pub fn new(generator: G, translation: [f64; D]) -> Self {
27        Self {
28            generator,
29            translation,
30        }
31    }
32}
33
34impl<const D: usize, G> Generator<D> for Translate<D, G>
35where
36    G: Generator<D>,
37{
38    #[inline]
39    fn sample(&self, point: [f64; D]) -> f64 {
40        self.generator
41            .sample(std::array::from_fn(|i| point[i] + self.translation[i]))
42    }
43}