grid_tree/
impl_glam.rs

1use crate::{BranchShape, ChildIndex, Level, Tree, VectorKey};
2
3use glam::{IVec2, IVec3, UVec2, UVec3};
4use ndshape::{
5    ConstPow2Shape2i32, ConstPow2Shape2u32, ConstPow2Shape3i32, ConstPow2Shape3u32, ConstShape,
6};
7
8/// A [`BranchShape`] for signed quadtrees.
9pub type QuadtreeShapeI32 = ConstPow2Shape2i32<1, 1>;
10/// A [`BranchShape`] for unsigned quadtrees.
11pub type QuadtreeShapeU32 = ConstPow2Shape2u32<1, 1>;
12
13/// A [`BranchShape`] for signed octrees.
14pub type OctreeShapeI32 = ConstPow2Shape3i32<1, 1, 1>;
15/// A [`BranchShape`] for unsigned octrees.
16pub type OctreeShapeU32 = ConstPow2Shape3u32<1, 1, 1>;
17
18/// The default quadtree with `i32` coordinates.
19pub type QuadtreeI32<T> = Tree<IVec2, QuadtreeShapeI32, T, 4>;
20/// The default quadtree with `u32` coordinates.
21pub type QuadtreeU32<T> = Tree<UVec2, QuadtreeShapeU32, T, 4>;
22
23/// The default octree with `i32` coordinates.
24pub type OctreeI32<T> = Tree<IVec3, OctreeShapeI32, T, 8>;
25/// The default octree with `u32` coordinates.
26pub type OctreeU32<T> = Tree<UVec3, OctreeShapeU32, T, 8>;
27
28impl<T> QuadtreeI32<T> {
29    pub fn new(height: Level) -> Self {
30        unsafe { Self::new_generic(height) }
31    }
32}
33impl<T> QuadtreeU32<T> {
34    pub fn new(height: Level) -> Self {
35        unsafe { Self::new_generic(height) }
36    }
37}
38impl<T> OctreeI32<T> {
39    pub fn new(height: Level) -> Self {
40        unsafe { Self::new_generic(height) }
41    }
42}
43impl<T> OctreeU32<T> {
44    pub fn new(height: Level) -> Self {
45        unsafe { Self::new_generic(height) }
46    }
47}
48
49macro_rules! impl_signed_branch_shape {
50    ($name:ty, $vector:ty, $shifter:expr) => {
51        impl BranchShape<$vector> for $name {
52            const SHAPE_SHIFTER: $vector = $shifter;
53
54            #[inline]
55            fn linearize_child(v: $vector) -> ChildIndex {
56                <$name>::linearize(v.into()) as ChildIndex
57            }
58
59            #[inline]
60            fn delinearize_child(i: ChildIndex) -> $vector {
61                <$name>::delinearize(i as i32).into()
62            }
63        }
64    };
65}
66
67macro_rules! impl_unsigned_branch_shape {
68    ($name:ty, $vector:ty, $shifter:expr) => {
69        impl BranchShape<$vector> for $name {
70            const SHAPE_SHIFTER: $vector = $shifter;
71
72            #[inline]
73            fn linearize_child(v: $vector) -> ChildIndex {
74                <$name>::linearize(v.into()) as ChildIndex
75            }
76
77            #[inline]
78            fn delinearize_child(i: ChildIndex) -> $vector {
79                <$name>::delinearize(i as u32).into()
80            }
81        }
82    };
83}
84
85impl_unsigned_branch_shape!(QuadtreeShapeU32, UVec2, UVec2::from_array([1; 2]));
86impl_unsigned_branch_shape!(OctreeShapeU32, UVec3, UVec3::from_array([1; 3]));
87impl_signed_branch_shape!(QuadtreeShapeI32, IVec2, IVec2::from_array([1; 2]));
88impl_signed_branch_shape!(OctreeShapeI32, IVec3, IVec3::from_array([1; 3]));
89
90impl VectorKey for IVec2 {
91    #[inline]
92    fn mul_u32(self, rhs: u32) -> Self {
93        self * rhs as i32
94    }
95}
96impl VectorKey for IVec3 {
97    #[inline]
98    fn mul_u32(self, rhs: u32) -> Self {
99        self * rhs as i32
100    }
101}
102impl VectorKey for UVec2 {
103    #[inline]
104    fn mul_u32(self, rhs: u32) -> Self {
105        self * rhs
106    }
107}
108impl VectorKey for UVec3 {
109    #[inline]
110    fn mul_u32(self, rhs: u32) -> Self {
111        self * rhs
112    }
113}