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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
use crate::inner_prelude::*; use crate::HasAabb; ///The results of the binning process. pub struct Binned<'a, T: 'a> { pub middle: &'a mut [T], pub left: &'a mut [T], pub right: &'a mut [T], } unsafe fn swap_unchecked<T>(arr: &mut [T], a: usize, b: usize) { let a = &mut *(arr.get_unchecked_mut(a) as *mut T); let b = &mut *(arr.get_unchecked_mut(b) as *mut T); core::mem::swap(a, b) } /// Sorts the bots into three bins. Those to the left of the divider, those that intersect with the divider, and those to the right. /// They will be laid out in memory s.t. middile<left<right pub fn bin_middle_left_right<'b, A: AxisTrait, X: HasAabb>( axis: A, med: &X::Num, bots: &'b mut [X], ) -> Binned<'b, X> { let bot_len = bots.len(); let mut left_end = 0; let mut middle_end = 0; // | middile | left| right |---------| // // ^ ^ ^ // middile_end left_end index_at for index_at in 0..bot_len { match bots[index_at] .get().rect .get_range(axis) .left_or_right_or_contain(med) { //If the divider is less than the bot core::cmp::Ordering::Equal => { //left bots.swap(index_at, left_end); bots.swap(left_end, middle_end); middle_end += 1; left_end += 1; } //If the divider is greater than the bot core::cmp::Ordering::Greater => { //middile bots.swap(index_at, left_end); left_end += 1; } core::cmp::Ordering::Less => { //right } } } let (rest, right) = bots.split_at_mut(left_end); let (middle, left) = rest.split_at_mut(middle_end); //println!("middile left right={:?}",(middle.len(),left.len(),right.len())); debug_assert!(left.len() + right.len() + middle.len() == bot_len); Binned { left, middle, right, } } /// Sorts the bots into three bins. Those to the left of the divider, those that intersect with the divider, and those to the right. /// They will be laid out in memory s.t. middile<left<right pub unsafe fn bin_middle_left_right_unchecked<'b, A: AxisTrait, X: HasAabb>( axis: A, med: &X::Num, bots: &'b mut [X], ) -> Binned<'b, X> { let bot_len = bots.len(); let mut left_end = 0; let mut middle_end = 0; // | middile | left| right |---------| // // ^ ^ ^ // middile_end left_end index_at for index_at in 0..bot_len { match bots .get_unchecked(index_at) .get().rect .get_range(axis) .left_or_right_or_contain(med) { //If the divider is less than the bot core::cmp::Ordering::Equal => { //left swap_unchecked(bots, index_at, left_end); swap_unchecked(bots, left_end, middle_end); middle_end += 1; left_end += 1; } //If the divider is greater than the bot core::cmp::Ordering::Greater => { //middile swap_unchecked(bots, index_at, left_end); left_end += 1; } core::cmp::Ordering::Less => { //right } } } let (rest, right) = bots.split_at_mut(left_end); let (middle, left) = rest.split_at_mut(middle_end); //println!("middile left right={:?}",(middle.len(),left.len(),right.len())); debug_assert!(left.len() + right.len() + middle.len() == bot_len); Binned { left, middle, right, } } #[inline(always)] pub fn compare_bots<T: HasAabb>(axis: impl AxisTrait, a: &T, b: &T) -> core::cmp::Ordering { let (p1, p2) = (a.get().rect.get_range(axis).left, b.get().rect.get_range(axis).left); if p1 > p2 { core::cmp::Ordering::Greater }else{ core::cmp::Ordering::Less } } ///Sorts the bots based on an axis. #[inline(always)] pub fn sweeper_update<I: HasAabb, A: AxisTrait>(axis: A, collision_botids: &mut [I]) { let sclosure = |a: &I, b: &I| -> core::cmp::Ordering { compare_bots(axis, a, b) }; collision_botids.sort_unstable_by(sclosure); } /* #[cfg(test)] mod test{ use test_support; use test_support::Bot; use test_support::create_unordered; use super::*; use axgeom; //use Blee; use support::BBox; use *; use ordered_float::NotNaN; #[test] fn test_get_section(){ for _ in 0..100{ let world=test_support::create_word(); let axis=axgeom::XAXIS; let rr=Range{start:100.0,end:110.0}; let mut vec1:Vec<BBox<NotNaN<f32>,Bot>>=(0..1000).map(|a| { let rect=test_support::get_random_rect(&world); let bot=Bot::new(a); BBox::new(bot,rect) } ).collect(); //let mut vec1:Vec<Bot>=(0..500).map(|a|Bot{id:a,rect:support::get_random_rect(&world)}).collect(); let src:Vec<usize>={ let mut src_temp=Vec::new(); for a in vec1.iter(){ if rr.intersects(a.rect.get_range(axis)){ src_temp.push(a.val.id); } } src_temp }; let mut sw=Sweeper::new(); let a=Blee::new(axis); Sweeper::update(&mut vec1,&a); /* println!("Bots:"); for b in vec1.iter(){ println!("{:?}",(b.id,b.rect.get_range(axis))); } */ let target=sw.get_section(&mut vec1,&rr,&a); match target{ Some(x)=>{ //Assert that all bots that intersect the rect are somewhere in the list outputted by get_setion(). for k in src.iter(){ let mut found=false; for j in x.iter(){ if *k==j.val.id{ found=true; break; } } assert!(found); } //Assert that the first bot in the outputted list intersects with get_section(). let first=x.first().unwrap(); let mut found=false; for j in src.iter(){ if first.val.id==*j{ found=true; break; } } assert!(found); //Assert that the last bot in the outputted list intersects with get_section(). let last=&x[x.len()-1]; let mut found=false; for j in src.iter(){ if last.val.id==*j{ found=true; break; } } assert!(found); }, None=>{ assert!(src.len()==0); } } } } #[test] fn test_bijective_parallel(){ for _ in 0..100{ let world=test_support::create_word(); //let mut vec1:Vec<BBox<Bot>>=(0..5).map(|a|Bot{id:a,rect:support::get_random_rect(&world)}).collect(); //let mut vec2:Vec<BBox<Bot>>=(0..5).map(|a|Bot{id:vec1.len()+a,rect:support::get_random_rect(&world)}).collect(); let mut vec1:Vec<BBox<NotNaN<f32>,Bot>>=(0..5).map(|a| { let rect=test_support::get_random_rect(&world); let bot=Bot::new(a); BBox::new(bot,rect) } ).collect(); let mut vec2:Vec<BBox<NotNaN<f32>,Bot>>=(0..5).map(|a| { let rect=test_support::get_random_rect(&world); let bot=Bot::new(vec1.len()+a); BBox::new(bot,rect) } ).collect(); let axis=axgeom::XAXIS; let mut src:Vec<(usize,usize)>={ let mut src_temp=Vec::new(); for i in vec1.iter(){ for j in vec2.iter(){ let (a,b):(&BBox<NotNaN<f32>,Bot>,&BBox<NotNaN<f32>,NotNaN<f32>,Bot>)=(i,j); if a.rect.get_range(axis).intersects(b.rect.get_range(axis)){ src_temp.push(create_unordered(&a.val,&b.val)); } } } src_temp }; let mut sw=Sweeper::new(); let a=Blee::new(axis); Sweeper::update(&mut vec1,&a); Sweeper::update(&mut vec2,&a); let mut val=Vec::new(); //let rr=world.get_range(axis); { let mut f=|cc:ColPair<BBox<NotNaN<f32>,Bot>>|{ val.push(create_unordered(cc.a.1,cc.b.1)); }; let mut bk=BleekSF::new(&mut f); sw.find_bijective_parallel((&mut vec1,&mut vec2),&a,&mut bk); } src.sort_by(&test_support::compair_bot_pair); val.sort_by(&test_support::compair_bot_pair); /* println!("naive result:\n{:?}",(src.len(),&src)); println!("sweep result:\n{:?}",(val.len(),&val)); println!("Bots:"); for b in vec1{ println!("{:?}",(b.id,b.rect.get_range(axis))); } println!(); for b in vec2{ println!("{:?}",(b.id,b.rect.get_range(axis))); } */ assert!(src==val); } } #[test] fn test_find(){ //let world=axgeom::Rect::new(-1000.0,1000.0,-1000.0,1000.0); let world=test_support::create_word(); let mut vec:Vec<BBox<NotNaN<f32>,Bot>>=(0..500).map(|a| { let rect=test_support::get_random_rect(&world); let bot=Bot::new(a); BBox::new(bot,rect) } ).collect(); //Lets always order the ids smaller to larger to make it easier to look up. // let mut map:HashMap<(usize,usize),()>=HashMap::new(); let mut src:Vec<(usize,usize)>=Vec::new(); let axis=axgeom::XAXIS; for (e,i) in vec.iter().enumerate(){ for j in vec[e+1..].iter(){ let (a,b):(&BBox<NotNaN<f32>,Bot>,&BBox<NotNaN<f32>,Bot>)=(i,j); if a.rect.get_range(axis).intersects(b.rect.get_range(axis)){ src.push(create_unordered(&a.val,&b.val)); } } } let mut sw=Sweeper::new(); let a=Blee::new(axis); Sweeper::update(&mut vec,&a); let mut val=Vec::new(); { let mut f=|cc:ColPair<BBox<NotNaN<f32>,Bot>>|{ val.push(create_unordered(cc.a.1,cc.b.1)); }; let mut bk=BleekSF::new(&mut f); sw.find(&mut vec,&a,&mut bk); } src.sort_by(&test_support::compair_bot_pair); val.sort_by(&test_support::compair_bot_pair); //println!("{:?}",(src.len(),val.len())); //println!("{:?}",val); assert!(src==val); } } */