clay_core/shape/
mapper.rs

1use std::collections::HashSet;
2use crate::{
3    prelude::*,
4    map::Map,
5    shape::*,
6};
7
8/// A new shape obtained by applying some mapping to another shape.
9pub struct ShapeMapper<S: Shape, M: Map> {
10    pub shape: S,
11    pub map: M,
12}
13
14impl<S: Shape, M: Map> ShapeMapper<S, M> {
15    pub fn new(shape: S, map: M) -> Self {
16        Self { shape, map }
17    }
18}
19
20impl<S: Shape, M: Map> Shape for ShapeMapper<S, M> {}
21
22impl<S: Shape, M: Map> Instance<ShapeClass> for ShapeMapper<S, M> {
23    fn source(cache: &mut HashSet<u64>) -> String {
24        if !cache.insert(Self::type_hash()) {
25            return String::new()
26        }
27        [
28            S::source(cache),
29            M::source(cache),
30            "#include <clay_core/shape/mapper.h>".to_string(),
31            format!(
32                "MAP_SHAPE_FN_DEF({}, {}, {}, {}, {})",
33                Self::inst_name(),
34                S::inst_name(),
35                M::inst_name(),
36                S::size_int(), S::size_float(),
37            ),
38        ].join("\n")
39    }
40    fn inst_name() -> String {
41        format!(
42            "__mapper_{:x}",
43            Self::type_hash(),
44        )
45    }
46}
47
48
49impl<S: Shape, M: Map> Pack for ShapeMapper<S, M> {
50    fn size_int() -> usize {
51        S::size_int() + M::size_int()
52    }
53    fn size_float() -> usize {
54        S::size_float() + M::size_float()
55    }
56    fn pack_to(&self, buffer_int: &mut [i32], buffer_float: &mut [f32]) {
57        Packer::new(buffer_int, buffer_float)
58        .pack(&self.shape)
59        .pack(&self.map);
60    }
61}