1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
use super::*; ///CollidingPairs created via [`TreeRefInd::collect_colliding_pairs`] pub struct CollidingPairs<T, D> { ///See collect_intersections_list() ///The same elements can be part of ///multiple intersecting pairs. ///So pointer aliasing rules are not ///being met if we were to just use this ///vec according to its type signature. cols: Vec<(Ptr<T>, Ptr<T>, D)>, orig: Ptr<[T]>, } impl<T, D> CollidingPairs<T, D> { ///Return a read only list of colliding pairs. ///We can't return a list of mutable pairs since some might ///alias, but we can return a list if they are not mutable. pub fn get(&self, arr: &[T]) -> &[(&T, &T, D)] { assert_eq!(self.orig.0 as *const _, arr as *const _); unsafe { &*(self.cols.as_slice() as *const _ as *const _) } } ///Visit every colliding pair. ///panics if the slice passed is not the slice used to create this ///`CollidingPairs` object. pub fn for_every_pair_mut( &mut self, arr: &mut [T], mut func: impl FnMut(&mut T, &mut T, &mut D), ) { assert_eq!(self.orig.0, arr as *mut _); for (a, b, d) in self.cols.iter_mut() { func(unsafe { &mut *(*a).0 }, unsafe { &mut *(*b).0 }, d) } } } ///CollidingPairsPar created via [`TreeRefInd::collect_colliding_pairs_par`] ///All colliding pairs partitioned into ///mutually exclusive sets so that they can be traversed in parallel pub struct CollidingPairsPar<T, D> { cols: Vec<Vec<(Ptr<T>, Ptr<T>, D)>>, original: Ptr<[T]>, } impl<T, D> CollidingPairsPar<T, D> { pub fn get(&self, arr: &[T]) -> &[Vec<(&T, &T, D)>] { assert_eq!(arr as *const _, self.original.0 as *const _); unsafe { &*(self.cols.as_slice() as *const _ as *const _) } } } impl<T: Send + Sync, D: Send + Sync> CollidingPairsPar<T, D> { pub fn for_every_pair_mut_par( &mut self, arr: &mut [T], func: impl Fn(&mut T, &mut T, &mut D) + Send + Sync + Copy, ) { assert_eq!(arr as *mut _, self.original.0); fn parallelize<T: Visitor + Send + Sync>(a: T, func: impl Fn(T::Item) + Sync + Send + Copy) where T::Item: Send + Sync, { let (n, l) = a.next(); func(n); if let Some([left, right]) = l { rayon::join(|| parallelize(left, func), || parallelize(right, func)); } } let mtree = compt::dfs_order::CompleteTree::from_preorder_mut(&mut self.cols).unwrap(); parallelize(mtree.vistr_mut(), |a| { for (a, b, d) in a.iter_mut() { let a = unsafe { &mut *a.0 }; let b = unsafe { &mut *b.0 }; func(a, b, d) } }); } } impl<'a, A: Axis, N: Num, T: Send + Sync> TreeRefInd<'a, A, N, T> { /// The parallel version of [`TreeRefInd::collect_colliding_pairs`] that instead /// returns a [`CollidingPairsPar`]. /// /// # Examples /// ///``` /// let mut aabbs = [ /// broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0), /// broccoli::bbox(broccoli::rect(15, 20, 15, 20), 1), /// broccoli::bbox(broccoli::rect(5, 15, 5, 15), 2), /// ]; /// /// let mut tree = broccoli::collections::TreeRefInd::new_par(&mut aabbs,|a|{ /// a.rect /// }); /// /// //Find all colliding aabbs only once. /// let mut pairs=tree.collect_colliding_pairs_par(|a, b| { /// a.inner += 1; /// b.inner += 1; /// Some(()) /// }); /// /// //Iterate over the pairs multiple times /// for _ in 0..3{ /// //mutate every colliding pair. /// pairs.for_every_pair_mut_par(&mut aabbs,|a,b,()|{ /// a.inner+=1; /// b.inner+=1; /// }) /// } pub fn collect_colliding_pairs_par<D: Send + Sync>( &mut self, func: impl Fn(&mut T, &mut T) -> Option<D> + Send + Sync + Copy, ) -> CollidingPairsPar<T, D> { let cols = self.collect_colliding_pairs_par_inner(|a, b| match func(a, b) { Some(d) => Some((Ptr(a as *mut _), Ptr(b as *mut _), d)), None => None, }); CollidingPairsPar { cols, original: self.tree.orig, } } fn collect_colliding_pairs_par_inner<D: Send + Sync>( &mut self, func: impl Fn(&mut T, &mut T) -> Option<D> + Send + Sync + Copy, ) -> Vec<Vec<D>> { struct Foo<T: Visitor> { current: T::Item, next: Option<[T; 2]>, } impl<T: Visitor> Foo<T> { fn new(a: T) -> Foo<T> { let (n, f) = a.next(); Foo { current: n, next: f, } } } let height = 1 + par::compute_level_switch_sequential( par::SWITCH_SEQUENTIAL_DEFAULT, self.get_height(), ) .get_depth_to_switch_at(); let mut cols: Vec<Vec<D>> = (0..compt::compute_num_nodes(height)) .map(|_| Vec::new()) .collect(); let mtree = compt::dfs_order::CompleteTree::from_preorder_mut(&mut cols).unwrap(); self.find_colliding_pairs_par_ext( move |a| { let next = a.next.take(); if let Some([left, right]) = next { let l = Foo::new(left); let r = Foo::new(right); *a = l; r } else { unreachable!() } }, move |_a, _b| {}, move |c, a, b| { if let Some(d) = func(a, b) { c.current.push(d); } }, Foo::new(mtree.vistr_mut()), ); cols //CollidingPairsPar{cols,_p:PhantomData} } } ///Contains a filtered list of all elements in the tree from calling [`TreeRefInd::collect_all`]. pub struct FilteredElements<T, D> { elems: Vec<(Ptr<T>, D)>, orig: Ptr<[T]>, } impl<T, D> FilteredElements<T, D> { pub fn get(&self, arr: &[T]) -> &[(&T, D)] { assert_eq!(self.orig.0 as *const _, arr as *const _); unsafe { &*(self.elems.as_slice() as *const _ as *const _) } } pub fn get_mut(&mut self, arr: &mut [T]) -> &mut [(&mut T, D)] { assert_eq!(self.orig.0, arr as *mut _); unsafe { &mut *(self.elems.as_mut_slice() as *mut _ as *mut _) } } } impl<'a, A: Axis, N: Num, T> TreeRefInd<'a, A, N, T> { /// Collect all elements based off of a predicate and return a [`FilteredElements`]. /// /// # Examples /// ///``` /// let mut aabbs = [ /// broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0), /// broccoli::bbox(broccoli::rect(15, 20, 15, 20), 1), /// broccoli::bbox(broccoli::rect(5, 15, 5, 15), 2), /// ]; /// /// let mut tree = broccoli::collections::TreeRefInd::new(&mut aabbs,|a|{ /// a.rect /// }); /// /// //Find a group of elements only once. /// let mut pairs=tree.collect_all(|_,b| { /// if b.inner % 2 ==0{ /// Some(()) /// }else{ /// None /// } /// }); /// /// //Iterate over that group multiple times /// for _ in 0..3{ /// //mutate every colliding pair. /// for (a,()) in pairs.get_mut(&mut aabbs){ /// a.inner+=1; /// } /// } pub fn collect_all<D: Send + Sync>( &mut self, mut func: impl FnMut(&Rect<N>, &mut T) -> Option<D>, ) -> FilteredElements<T, D> { let mut elems = Vec::new(); for node in self.get_nodes_mut().iter_mut() { for b in node.get_mut().bots.iter_mut() { let (x, y) = b.unpack(); if let Some(d) = func(x, y) { elems.push((Ptr(*y as *mut _), d)); } } } FilteredElements { orig: self.tree.orig, elems, } } } impl<'a, A: Axis, N: Num, T> TreeRefInd<'a, A, N, T> { /// Find all colliding pairs based on a predicate and return a [`CollidingPairs`]. /// /// # Examples /// ///``` /// let mut aabbs = [ /// broccoli::bbox(broccoli::rect(0isize, 10, 0, 10), 0), /// broccoli::bbox(broccoli::rect(15, 20, 15, 20), 1), /// broccoli::bbox(broccoli::rect(5, 15, 5, 15), 2), /// ]; /// /// let mut tree = broccoli::collections::TreeRefInd::new(&mut aabbs,|a|{ /// a.rect /// }); /// /// //Find all colliding aabbs only once. /// let mut pairs=tree.collect_colliding_pairs(|a, b| { /// a.inner += 1; /// b.inner += 1; /// Some(()) /// }); /// /// //Iterate over the pairs multiple times /// for _ in 0..3{ /// //mutate every colliding pair. /// pairs.for_every_pair_mut(&mut aabbs,|a,b,()|{ /// a.inner+=1; /// b.inner+=1; /// }) /// } pub fn collect_colliding_pairs<D: Send + Sync>( &mut self, mut func: impl FnMut(&mut T, &mut T) -> Option<D> + Send + Sync, ) -> CollidingPairs<T, D> { let mut cols: Vec<_> = Vec::new(); self.find_colliding_pairs_mut(|a, b| { if let Some(d) = func(a, b) { //We use unsafe to collect mutable references of //all colliding pairs. //This is safe to do because the user is forced //to iterate through all the colliding pairs //one at a time. let a = Ptr(*a as *mut T); let b = Ptr(*b as *mut T); cols.push((a, b, d)); } }); CollidingPairs { cols, orig: self.tree.orig, } } }