bvh: handling for median clumps

This commit is contained in:
Quaternions 2024-08-19 13:33:18 -07:00
parent 2b8bb0b705
commit 27dba8a90d

View File

@ -137,19 +137,27 @@ fn generate_bvh_node<T>(boxen:Vec<(T,Aabb)>,force:bool)->BvhNode<T>{
let median_x=sort_x[h].1; let median_x=sort_x[h].1;
let median_y=sort_y[h].1; let median_y=sort_y[h].1;
let median_z=sort_z[h].1; let median_z=sort_z[h].1;
//locate a run of values equal to the median
//partition point gives the first index for which the predicate evaluates to false //partition point gives the first index for which the predicate evaluates to false
let first_index_gt_median_x=sort_x.partition_point(|tup|!(median_x<tup.1)); let first_index_eq_median_x=sort_x.partition_point(|&(_,x)|x<median_x);
let first_index_gt_median_y=sort_y.partition_point(|tup|!(median_y<tup.1)); let first_index_eq_median_y=sort_y.partition_point(|&(_,y)|y<median_y);
let first_index_gt_median_z=sort_z.partition_point(|tup|!(median_z<tup.1)); let first_index_eq_median_z=sort_z.partition_point(|&(_,z)|z<median_z);
let first_index_gt_median_x=sort_x.partition_point(|&(_,x)|x<=median_x);
let first_index_gt_median_y=sort_y.partition_point(|&(_,y)|y<=median_y);
let first_index_gt_median_z=sort_z.partition_point(|&(_,z)|z<=median_z);
//pick which side median value copies go into such that both sides are as balanced as possible based on distance from n/2
let partition_point_x=if n.abs_diff(2*first_index_eq_median_x)<n.abs_diff(2*first_index_gt_median_x){first_index_eq_median_x}else{first_index_gt_median_x};
let partition_point_y=if n.abs_diff(2*first_index_eq_median_y)<n.abs_diff(2*first_index_gt_median_y){first_index_eq_median_y}else{first_index_gt_median_y};
let partition_point_z=if n.abs_diff(2*first_index_eq_median_z)<n.abs_diff(2*first_index_gt_median_z){first_index_eq_median_z}else{first_index_gt_median_z};
//this ids which octant the boxen is put in //this ids which octant the boxen is put in
let mut octant=vec![0;n]; let mut octant=vec![0;n];
for &(i,_) in &sort_x[first_index_gt_median_x..]{ for &(i,_) in &sort_x[partition_point_x..]{
octant[i]+=1<<0; octant[i]+=1<<0;
} }
for &(i,_) in &sort_y[first_index_gt_median_y..]{ for &(i,_) in &sort_y[partition_point_y..]{
octant[i]+=1<<1; octant[i]+=1<<1;
} }
for &(i,_) in &sort_z[first_index_gt_median_z..]{ for &(i,_) in &sort_z[partition_point_z..]{
octant[i]+=1<<2; octant[i]+=1<<2;
} }
//generate lists for unique octant values //generate lists for unique octant values