game_kernel_utils/
lib.rs

1#[cfg(test)]
2mod tests {
3    #[test]
4    fn it_works() {
5        assert_eq!(2 + 2, 4);
6    }
7}
8
9pub mod hierarchy;
10
11use std::collections::HashMap;
12#[derive(Clone)]
13pub struct AutoIndexMap<T> {
14    data: Vec<T>,
15    current_index: u64,
16}
17
18pub type KeyType = u64;
19impl<T> AutoIndexMap<T> {
20    pub fn new() -> Self {
21        Self {
22            data: Vec::new(),
23            current_index: 0,
24        }
25    }
26
27    pub fn insert(&mut self, v: T) -> u64 {
28        self.data.push(v);
29        self.data.len() as u64 - 1
30    }
31
32    pub fn iter(&self) -> std::iter::Enumerate<std::slice::Iter<'_, T>> {
33        self.data
34            .iter()
35            .enumerate()
36    }
37
38    pub fn get(&self, k: KeyType) -> Option<&T> {
39        self.data.get(k as usize)
40    }
41
42    pub fn get_mut(&mut self, k: KeyType) -> Option<&mut T> {
43        self.data.get_mut(k as usize)
44    }
45
46    pub fn is_empty(&self) -> bool {
47        self.data.is_empty()
48    }
49
50    pub fn len(&self) -> usize {
51        self.data.len()
52    }
53}
54
55impl<T: Eq + PartialEq> AutoIndexMap<T> {
56    pub fn contains(&self, x: &T) -> bool {
57        self.data.contains(x)
58    }
59}
60
61use std::ops::Index;
62
63impl<T> Index<&KeyType> for AutoIndexMap<T> {
64    type Output = T;
65
66    fn index(&self, index: &KeyType) -> &T {
67        &self.data[*index as usize]
68    }
69}
70
71pub trait MapAbstract<K, V> {
72    fn get<Q: Sized>(&self, k: &Q) -> Option<&V>
73    where
74        Self: Sized;
75
76    fn get_mut<Q: Sized>(&mut self, k: &Q) -> Option<&mut V>
77    where
78        Self: Sized;
79}
80
81impl<K, V> MapAbstract<K, V> for HashMap<K, V> {
82    fn get<Q: Sized>(&self, k: &Q) -> Option<&V>
83    where
84        Self: Sized,
85    {
86        self.get(k)
87    }
88
89    fn get_mut<Q: Sized>(&mut self, k: &Q) -> Option<&mut V>
90    where
91        Self: Sized,
92    {
93        self.get_mut(k)
94    }
95}
96
97use std::marker::PhantomData;
98
99pub struct MappedMap<K, V, N, M, F, FM>
100where
101    M: MapAbstract<K, V>,
102    F: Fn(&V) -> &N,
103    FM: Fn(&mut V) -> &mut N,
104{
105    mapped: M,
106    f: F,
107    fm: FM,
108    p1: PhantomData<K>,
109    p2: PhantomData<V>,
110    p3: PhantomData<N>,
111}
112
113impl<K, V, N, M, F, FM> MappedMap<K, V, N, M, F, FM>
114where
115    M: MapAbstract<K, V>,
116    F: Fn(&V) -> &N,
117    FM: Fn(&mut V) -> &mut N,
118{
119    fn new(mapped: M, f: F, fm: FM) -> Self {
120        Self {
121            mapped,
122            f,
123            fm,
124            p1: PhantomData,
125            p2: PhantomData,
126            p3: PhantomData,
127        }
128    }
129
130    fn get<Q: Sized>(&self, k: &Q) -> Option<&N> {
131        Some((self.f)(self.mapped.get(k)?)) //doh
132    }
133
134    fn get_mut<Q: Sized>(&mut self, k: &Q) -> Option<&mut N> {
135        Some((self.fm)(self.mapped.get_mut(k)?)) //doh
136    }
137}
138
139impl<K, V, N, M, F, FM> MapAbstract<K, N> for MappedMap<K, V, N, M, F, FM>
140where
141    M: MapAbstract<K, V>,
142    F: Fn(&V) -> &N,
143    FM: Fn(&mut V) -> &mut N,
144{
145    fn get<Q: Sized>(&self, k: &Q) -> Option<&N>
146    where
147        Self: Sized,
148    {
149        self.get(k)
150    }
151
152    fn get_mut<Q: Sized>(&mut self, k: &Q) -> Option<&mut N>
153    where
154        Self: Sized,
155    {
156        self.get_mut(k)
157    }
158}
159
160pub trait MapMap<K, V> {
161    fn map_map<N, F, FM, M>(self, f: F, fm: FM) -> MappedMap<K, V, N, Self, F, FM>
162    where
163        F: Fn(&V) -> &N,
164        FM: Fn(&mut V) -> &mut N,
165        Self: MapAbstract<K, V>,
166        Self: Sized,
167    {
168        MappedMap::new(self, f, fm)
169    }
170}
171
172impl<K, V> MapMap<K, V> for HashMap<K, V> {}
173
174extern crate cgmath;
175use cgmath::{Matrix3, Matrix4, /*BaseFloat, InnerSpace, SquareMatrix*/};
176
177/*pub trait MatOps<T> {
178    fn transponse(&self) -> Matrix3<T>;
179    fn inverse(&self) -> Matrix3<T>;
180}
181
182fn mat3_transponse<T: Copy>(a: &Matrix3<T>) -> Matrix3<T> {
183    Matrix3::new(a[0][0], a[1][0], a[2][0],
184                 a[0][1], a[1][1], a[2][1],
185                 a[0][2], a[1][2], a[2][2],)
186}
187
188*/
189pub fn mat4_transponse<T: Copy>(a: &Matrix4<T>) -> Matrix4<T> {
190    Matrix4::new(a[0][0], a[1][0], a[2][0], a[3][0],
191                 a[0][1], a[1][1], a[2][1], a[3][1],
192                 a[0][2], a[1][2], a[2][2], a[3][2],
193                 a[0][3], a[1][3], a[2][3], a[3][3],)
194}
195/*
196impl<T: BaseFloat> MatOps<T> for Matrix3<T> {
197    fn transponse(&self) -> Matrix3<T> {
198        mat3_transponse(self)
199    }
200
201    fn inverse(&self) -> Matrix3<T> {
202        let mt = Matrix3::new(self[0][0], self[1][0], self[2][0],
203                     self[0][1], self[1][1], self[2][1],
204                     self[0][2], self[1][2], self[2][2],);
205        let det = mt.x.cross(mt.y).dot(mt.z);
206        let adj = Matrix3::from_cols(mt.y.cross(mt.z), mt.z.cross(mt.x), mt.x.cross(mt.y));
207        adj/det
208    }
209}*/
210
211pub fn submat3<T: Copy>(a: Matrix4<T>) -> Matrix3<T> {
212    Matrix3::new(a[0][0], a[0][1], a[0][2], 
213                 a[1][0], a[1][1], a[1][2],
214                 a[2][0], a[2][1], a[2][2],)
215}
216
217pub fn mat3_extend(a: Matrix3<f32>) -> [[f32; 4]; 3] {
218    [[a[0][0], a[0][1], a[0][2], 0.0],
219    [a[1][0], a[1][1], a[1][2], 0.0],
220    [a[2][0], a[2][1], a[2][2], 0.0],]
221}