use crate::RenderError;
#[derive(Debug, Clone, Copy)]
pub struct Aabb {
pub min: [f32; 3],
pub max: [f32; 3],
}
impl Aabb {
pub fn new(min: [f32; 3], max: [f32; 3]) -> Self {
Self { min, max }
}
pub fn from_point(p: [f32; 3]) -> Self {
Self { min: p, max: p }
}
pub fn from_center_radius(center: [f32; 3], radius: f32) -> Self {
Self {
min: [center[0] - radius, center[1] - radius, center[2] - radius],
max: [center[0] + radius, center[1] + radius, center[2] + radius],
}
}
pub fn union(&self, other: &Aabb) -> Aabb {
Aabb {
min: [
self.min[0].min(other.min[0]),
self.min[1].min(other.min[1]),
self.min[2].min(other.min[2]),
],
max: [
self.max[0].max(other.max[0]),
self.max[1].max(other.max[1]),
self.max[2].max(other.max[2]),
],
}
}
pub fn expand(&mut self, p: [f32; 3]) {
self.min[0] = self.min[0].min(p[0]);
self.min[1] = self.min[1].min(p[1]);
self.min[2] = self.min[2].min(p[2]);
self.max[0] = self.max[0].max(p[0]);
self.max[1] = self.max[1].max(p[1]);
self.max[2] = self.max[2].max(p[2]);
}
pub fn center(&self) -> [f32; 3] {
[
(self.min[0] + self.max[0]) * 0.5,
(self.min[1] + self.max[1]) * 0.5,
(self.min[2] + self.max[2]) * 0.5,
]
}
pub fn size(&self) -> [f32; 3] {
[
self.max[0] - self.min[0],
self.max[1] - self.min[1],
self.max[2] - self.min[2],
]
}
pub fn surface_area(&self) -> f32 {
let s = self.size();
2.0 * (s[0] * s[1] + s[1] * s[2] + s[2] * s[0])
}
pub fn longest_axis(&self) -> usize {
let s = self.size();
if s[0] >= s[1] && s[0] >= s[2] {
0
} else if s[1] >= s[2] {
1
} else {
2
}
}
pub fn contains_point(&self, p: [f32; 3]) -> bool {
p[0] >= self.min[0]
&& p[0] <= self.max[0]
&& p[1] >= self.min[1]
&& p[1] <= self.max[1]
&& p[2] >= self.min[2]
&& p[2] <= self.max[2]
}
pub fn intersects_sphere(&self, center: [f32; 3], radius: f32) -> bool {
let mut sq_dist = 0.0_f32;
for ((&v, &mn), &mx) in center.iter().zip(self.min.iter()).zip(self.max.iter()) {
if v < mn {
let d = mn - v;
sq_dist += d * d;
} else if v > mx {
let d = v - mx;
sq_dist += d * d;
}
}
sq_dist <= radius * radius
}
pub fn intersects_ray(&self, origin: [f32; 3], inv_dir: [f32; 3]) -> bool {
let mut t_min = f32::NEG_INFINITY;
let mut t_max = f32::INFINITY;
for i in 0..3 {
let t1 = (self.min[i] - origin[i]) * inv_dir[i];
let t2 = (self.max[i] - origin[i]) * inv_dir[i];
let t_near = t1.min(t2);
let t_far = t1.max(t2);
t_min = t_min.max(t_near);
t_max = t_max.min(t_far);
}
t_min <= t_max && t_max >= 0.0
}
}
const MAX_LEAF_SIZE: usize = 8;
const MAX_DEPTH: usize = 32;
const NUM_SAH_BUCKETS: usize = 16;
#[derive(Debug)]
pub enum BvhNode {
Leaf {
aabb: Aabb,
gaussian_indices: Vec<usize>,
},
Internal {
aabb: Aabb,
left: Box<BvhNode>,
right: Box<BvhNode>,
},
}
impl BvhNode {
fn aabb(&self) -> &Aabb {
match self {
BvhNode::Leaf { aabb, .. } => aabb,
BvhNode::Internal { aabb, .. } => aabb,
}
}
fn count_nodes(&self) -> usize {
match self {
BvhNode::Leaf { .. } => 1,
BvhNode::Internal { left, right, .. } => 1 + left.count_nodes() + right.count_nodes(),
}
}
fn max_depth(&self) -> usize {
match self {
BvhNode::Leaf { .. } => 1,
BvhNode::Internal { left, right, .. } => 1 + left.max_depth().max(right.max_depth()),
}
}
}
#[derive(Debug)]
pub struct GaussianBvh {
root: Option<BvhNode>,
primitive_aabbs: Vec<Aabb>,
num_gaussians: usize,
}
impl GaussianBvh {
pub fn build(positions: &[[f32; 3]], scales: &[[f32; 3]]) -> Result<Self, RenderError> {
let n = positions.len();
if scales.len() != n {
return Err(RenderError::MismatchedBufferSizes {
expected: n,
actual: scales.len(),
});
}
if n == 0 {
return Ok(Self {
root: None,
primitive_aabbs: Vec::new(),
num_gaussians: 0,
});
}
let aabbs: Vec<Aabb> = positions
.iter()
.zip(scales.iter())
.map(|(pos, scale)| {
let max_scale = scale[0].exp().max(scale[1].exp()).max(scale[2].exp());
Aabb::from_center_radius(*pos, max_scale)
})
.collect();
let mut indices: Vec<usize> = (0..n).collect();
let root = build_recursive(&aabbs, &mut indices, 0);
Ok(Self {
root: Some(root),
primitive_aabbs: aabbs,
num_gaussians: n,
})
}
pub fn frustum_cull(&self, planes: &[[f32; 4]; 6]) -> Vec<usize> {
let mut result = Vec::new();
if let Some(root) = &self.root {
frustum_cull_recursive(root, planes, &self.primitive_aabbs, &mut result);
}
result
}
pub fn sphere_query(&self, center: [f32; 3], radius: f32) -> Vec<usize> {
let mut result = Vec::new();
if let Some(root) = &self.root {
sphere_query_recursive(root, center, radius, &self.primitive_aabbs, &mut result);
}
result
}
pub fn ray_query(&self, origin: [f32; 3], direction: [f32; 3]) -> Vec<usize> {
let inv_dir = [1.0 / direction[0], 1.0 / direction[1], 1.0 / direction[2]];
let mut result = Vec::new();
if let Some(root) = &self.root {
ray_query_recursive(root, origin, inv_dir, &self.primitive_aabbs, &mut result);
}
result
}
pub fn num_nodes(&self) -> usize {
self.root.as_ref().map_or(0, |r| r.count_nodes())
}
pub fn depth(&self) -> usize {
self.root.as_ref().map_or(0, |r| r.max_depth())
}
pub fn num_gaussians(&self) -> usize {
self.num_gaussians
}
}
fn build_recursive(aabbs: &[Aabb], indices: &mut [usize], depth: usize) -> BvhNode {
let n = indices.len();
let node_aabb = compute_union_aabb(aabbs, indices);
if n <= MAX_LEAF_SIZE || depth >= MAX_DEPTH {
return BvhNode::Leaf {
aabb: node_aabb,
gaussian_indices: indices.to_vec(),
};
}
let split_pos = find_sah_split(aabbs, indices, &node_aabb);
let split_pos = if split_pos == 0 || split_pos == n {
midpoint_split(aabbs, indices, &node_aabb)
} else {
split_pos
};
if split_pos == 0 || split_pos == n {
return BvhNode::Leaf {
aabb: node_aabb,
gaussian_indices: indices.to_vec(),
};
}
let (left_indices, right_indices) = indices.split_at_mut(split_pos);
let left = Box::new(build_recursive(aabbs, left_indices, depth + 1));
let right = Box::new(build_recursive(aabbs, right_indices, depth + 1));
BvhNode::Internal {
aabb: node_aabb,
left,
right,
}
}
fn compute_union_aabb(aabbs: &[Aabb], indices: &[usize]) -> Aabb {
let first_idx = indices[0];
let mut result = aabbs[first_idx];
for &idx in &indices[1..] {
result = result.union(&aabbs[idx]);
}
result
}
fn find_sah_split(aabbs: &[Aabb], indices: &mut [usize], parent_aabb: &Aabb) -> usize {
let n = indices.len();
let axis = parent_aabb.longest_axis();
indices.sort_by(|&a, &b| {
let ca = aabbs[a].center()[axis];
let cb = aabbs[b].center()[axis];
ca.partial_cmp(&cb).unwrap_or(std::cmp::Ordering::Equal)
});
let mut left_aabbs: Vec<Aabb> = Vec::with_capacity(n);
{
let mut running = aabbs[indices[0]];
left_aabbs.push(running);
for &idx in &indices[1..] {
running = running.union(&aabbs[idx]);
left_aabbs.push(running);
}
}
let mut right_aabbs: Vec<Aabb> = vec![aabbs[indices[0]]; n]; {
let mut running = aabbs[indices[n - 1]];
right_aabbs[n - 1] = running;
for i in (0..n - 1).rev() {
running = running.union(&aabbs[indices[i]]);
right_aabbs[i] = running;
}
}
let bucket_size = n.max(NUM_SAH_BUCKETS) / NUM_SAH_BUCKETS;
let mut best_cost = f32::INFINITY;
let mut best_split = n / 2;
let parent_area = parent_aabb.surface_area();
let area_denom = if parent_area > 0.0 { parent_area } else { 1.0 };
for bucket in 1..NUM_SAH_BUCKETS {
let split = (bucket * bucket_size).min(n - 1);
if split == 0 || split >= n {
continue;
}
let left_area = left_aabbs[split - 1].surface_area();
let right_area = right_aabbs[split].surface_area();
let cost = (left_area * split as f32 + right_area * (n - split) as f32) / area_denom;
if cost < best_cost {
best_cost = cost;
best_split = split;
}
}
best_split
}
fn midpoint_split(aabbs: &[Aabb], indices: &mut [usize], parent_aabb: &Aabb) -> usize {
let n = indices.len();
let axis = parent_aabb.longest_axis();
indices.sort_by(|&a, &b| {
let ca = aabbs[a].center()[axis];
let cb = aabbs[b].center()[axis];
ca.partial_cmp(&cb).unwrap_or(std::cmp::Ordering::Equal)
});
n / 2
}
fn aabb_outside_frustum(aabb: &Aabb, planes: &[[f32; 4]; 6]) -> bool {
for plane in planes {
let [a, b, c, d] = *plane;
let px = if a >= 0.0 { aabb.max[0] } else { aabb.min[0] };
let py = if b >= 0.0 { aabb.max[1] } else { aabb.min[1] };
let pz = if c >= 0.0 { aabb.max[2] } else { aabb.min[2] };
if a * px + b * py + c * pz + d < 0.0 {
return true; }
}
false
}
fn frustum_cull_recursive(
node: &BvhNode,
planes: &[[f32; 4]; 6],
primitive_aabbs: &[Aabb],
result: &mut Vec<usize>,
) {
let aabb = node.aabb();
if aabb_outside_frustum(aabb, planes) {
return;
}
match node {
BvhNode::Leaf {
gaussian_indices, ..
} => {
for &idx in gaussian_indices {
if let Some(prim_aabb) = primitive_aabbs.get(idx) {
if !aabb_outside_frustum(prim_aabb, planes) {
result.push(idx);
}
}
}
}
BvhNode::Internal { left, right, .. } => {
frustum_cull_recursive(left, planes, primitive_aabbs, result);
frustum_cull_recursive(right, planes, primitive_aabbs, result);
}
}
}
fn sphere_query_recursive(
node: &BvhNode,
center: [f32; 3],
radius: f32,
primitive_aabbs: &[Aabb],
result: &mut Vec<usize>,
) {
if !node.aabb().intersects_sphere(center, radius) {
return;
}
match node {
BvhNode::Leaf {
gaussian_indices, ..
} => {
for &idx in gaussian_indices {
if let Some(prim_aabb) = primitive_aabbs.get(idx) {
if prim_aabb.intersects_sphere(center, radius) {
result.push(idx);
}
}
}
}
BvhNode::Internal { left, right, .. } => {
sphere_query_recursive(left, center, radius, primitive_aabbs, result);
sphere_query_recursive(right, center, radius, primitive_aabbs, result);
}
}
}
fn ray_query_recursive(
node: &BvhNode,
origin: [f32; 3],
inv_dir: [f32; 3],
primitive_aabbs: &[Aabb],
result: &mut Vec<usize>,
) {
if !node.aabb().intersects_ray(origin, inv_dir) {
return;
}
match node {
BvhNode::Leaf {
gaussian_indices, ..
} => {
for &idx in gaussian_indices {
if let Some(prim_aabb) = primitive_aabbs.get(idx) {
if prim_aabb.intersects_ray(origin, inv_dir) {
result.push(idx);
}
}
}
}
BvhNode::Internal { left, right, .. } => {
ray_query_recursive(left, origin, inv_dir, primitive_aabbs, result);
ray_query_recursive(right, origin, inv_dir, primitive_aabbs, result);
}
}
}
pub fn extract_frustum_planes(view_proj: &[[f32; 4]; 4]) -> [[f32; 4]; 6] {
let row = |r: usize| -> [f32; 4] {
[
view_proj[0][r],
view_proj[1][r],
view_proj[2][r],
view_proj[3][r],
]
};
let r0 = row(0);
let r1 = row(1);
let r2 = row(2);
let r3 = row(3);
let left = add_planes(r3, r0); let right = sub_planes(r3, r0); let bottom = add_planes(r3, r1); let top = sub_planes(r3, r1); let near = r2;
let far = sub_planes(r3, r2);
[
normalize_plane(left),
normalize_plane(right),
normalize_plane(bottom),
normalize_plane(top),
normalize_plane(near),
normalize_plane(far),
]
}
fn add_planes(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]]
}
fn sub_planes(a: [f32; 4], b: [f32; 4]) -> [f32; 4] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2], a[3] - b[3]]
}
fn normalize_plane(plane: [f32; 4]) -> [f32; 4] {
let [a, b, c, d] = plane;
let len = (a * a + b * b + c * c).sqrt();
if len > 0.0 {
[a / len, b / len, c / len, d / len]
} else {
plane
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_aabb_new() {
let aabb = Aabb::new([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]);
assert_eq!(aabb.min, [1.0, 2.0, 3.0]);
assert_eq!(aabb.max, [4.0, 5.0, 6.0]);
}
#[test]
fn test_aabb_from_center_radius() {
let aabb = Aabb::from_center_radius([0.0, 0.0, 0.0], 1.0);
assert_eq!(aabb.min, [-1.0, -1.0, -1.0]);
assert_eq!(aabb.max, [1.0, 1.0, 1.0]);
let aabb2 = Aabb::from_center_radius([1.0, 2.0, 3.0], 0.5);
assert!((aabb2.min[0] - 0.5).abs() < 1e-6);
assert!((aabb2.max[2] - 3.5).abs() < 1e-6);
}
#[test]
fn test_aabb_union() {
let a = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
let b = Aabb::new([-1.0, 0.5, 0.0], [2.0, 1.5, 3.0]);
let u = a.union(&b);
assert_eq!(u.min, [-1.0, 0.0, 0.0]);
assert_eq!(u.max, [2.0, 1.5, 3.0]);
}
#[test]
fn test_aabb_expand() {
let mut aabb = Aabb::from_point([0.0, 0.0, 0.0]);
aabb.expand([1.0, -1.0, 2.0]);
assert_eq!(aabb.min, [0.0, -1.0, 0.0]);
assert_eq!(aabb.max, [1.0, 0.0, 2.0]);
}
#[test]
fn test_aabb_surface_area() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
assert!((aabb.surface_area() - 6.0).abs() < 1e-6);
let aabb2 = Aabb::new([0.0, 0.0, 0.0], [2.0, 3.0, 4.0]);
assert!((aabb2.surface_area() - 52.0).abs() < 1e-6);
let aabb3 = Aabb::new([0.0, 0.0, 0.0], [2.0, 3.0, 0.0]);
assert!((aabb3.surface_area() - 12.0).abs() < 1e-6);
}
#[test]
fn test_aabb_longest_axis() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [3.0, 1.0, 2.0]);
assert_eq!(aabb.longest_axis(), 0);
let aabb2 = Aabb::new([0.0, 0.0, 0.0], [1.0, 5.0, 2.0]);
assert_eq!(aabb2.longest_axis(), 1);
let aabb3 = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 4.0]);
assert_eq!(aabb3.longest_axis(), 2);
}
#[test]
fn test_aabb_contains_point() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
assert!(aabb.contains_point([0.5, 0.5, 0.5]));
assert!(aabb.contains_point([0.0, 0.0, 0.0]));
assert!(aabb.contains_point([1.0, 1.0, 1.0]));
assert!(!aabb.contains_point([1.1, 0.5, 0.5]));
assert!(!aabb.contains_point([-0.1, 0.5, 0.5]));
}
#[test]
fn test_aabb_intersects_sphere_inside() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [2.0, 2.0, 2.0]);
assert!(aabb.intersects_sphere([1.0, 1.0, 1.0], 0.1));
assert!(aabb.intersects_sphere([3.0, 1.0, 1.0], 1.5));
}
#[test]
fn test_aabb_intersects_sphere_outside() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
assert!(!aabb.intersects_sphere([5.0, 0.0, 0.0], 1.0));
assert!(!aabb.intersects_sphere([3.0, 3.0, 3.0], 1.0));
assert!(aabb.intersects_sphere([2.0, 2.0, 2.0], 2.0));
}
#[test]
fn test_aabb_intersects_ray() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
let inv_dir = [1.0, f32::INFINITY, f32::INFINITY];
assert!(aabb.intersects_ray([-1.0, 0.5, 0.5], inv_dir));
assert!(aabb.intersects_ray([0.5, 0.5, 0.5], [1.0, 0.0, 0.0]));
}
#[test]
fn test_aabb_ray_miss() {
let aabb = Aabb::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0]);
let inv_dir = [1.0, f32::INFINITY, f32::INFINITY];
assert!(!aabb.intersects_ray([-1.0, 2.0, 0.5], inv_dir));
assert!(!aabb.intersects_ray([5.0, 0.5, 0.5], [1.0, 0.0, 0.0]));
}
#[test]
fn test_bvh_build_empty() {
let bvh =
GaussianBvh::build(&[], &[]).expect("empty (length-matched) build should succeed");
assert_eq!(bvh.num_gaussians(), 0);
assert_eq!(bvh.num_nodes(), 0);
assert_eq!(bvh.depth(), 0);
}
#[test]
fn test_bvh_build_mismatched_lengths_errors() {
let positions = [[0.0f32, 0.0, 0.0], [1.0, 1.0, 1.0]];
let scales = [[0.0f32, 0.0, 0.0]]; let result = GaussianBvh::build(&positions, &scales);
match result {
Err(RenderError::MismatchedBufferSizes { expected, actual }) => {
assert_eq!(expected, 2);
assert_eq!(actual, 1);
}
other => panic!("expected MismatchedBufferSizes, got {other:?}"),
}
}
#[test]
fn test_bvh_build_single() {
let positions = [[0.0f32, 0.0, 0.0]];
let scales = [[0.0f32, 0.0, 0.0]]; let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
assert_eq!(bvh.num_gaussians(), 1);
assert_eq!(bvh.num_nodes(), 1);
assert_eq!(bvh.depth(), 1);
let hits = bvh.sphere_query([0.0, 0.0, 0.0], 2.0);
assert_eq!(hits.len(), 1);
assert_eq!(hits[0], 0);
}
#[test]
fn test_bvh_build_many() {
let n = 100usize;
let positions: Vec<[f32; 3]> = (0..n)
.map(|i| [i as f32, (i % 10) as f32, (i / 10) as f32])
.collect();
let scales: Vec<[f32; 3]> = vec![[0.0f32, 0.0, 0.0]; n]; let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
assert_eq!(bvh.num_gaussians(), n);
assert!(bvh.num_nodes() > 1);
let hits = bvh.sphere_query([50.0, 5.0, 5.0], 200.0);
assert_eq!(hits.len(), n);
}
#[test]
fn test_bvh_depth_bounded() {
let n = 1024usize;
let positions: Vec<[f32; 3]> = (0..n).map(|i| [i as f32 * 0.001, 0.0, 0.0]).collect();
let scales: Vec<[f32; 3]> = vec![[-5.0f32, -5.0, -5.0]; n]; let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
assert!(bvh.depth() <= MAX_DEPTH + 1);
}
#[test]
fn test_extract_frustum_planes_near_plane_boundary() {
let fov_y = std::f32::consts::FRAC_PI_2;
let aspect = 1.0_f32;
let near = 1.0_f32;
let far = 100.0_f32;
let proj = glam::camera::rh::proj::directx::perspective(fov_y, aspect, near, far);
let flat = proj.to_cols_array();
let view_proj: [[f32; 4]; 4] = [
[flat[0], flat[1], flat[2], flat[3]],
[flat[4], flat[5], flat[6], flat[7]],
[flat[8], flat[9], flat[10], flat[11]],
[flat[12], flat[13], flat[14], flat[15]],
];
let planes = extract_frustum_planes(&view_proj);
let [a, b, c, d] = planes[4]; let signed_distance = |z: f32| a * 0.0 + b * 0.0 + c * z + d;
let on_boundary = signed_distance(-near);
assert!(
on_boundary.abs() < 1e-3,
"a point at exactly the near distance should lie on the near-plane \
boundary, got signed distance {on_boundary}"
);
let just_inside = signed_distance(-(near + 0.1));
assert!(
just_inside > 0.0,
"a point just past the near plane should be inside, got {just_inside}"
);
let just_behind = signed_distance(-(near - 0.1));
assert!(
just_behind < 0.0,
"a point just behind the near plane should be outside, got {just_behind}"
);
}
fn unit_cube_planes() -> [[f32; 4]; 6] {
[
[1.0, 0.0, 0.0, 1.0],
[-1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, -1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, -1.0, 1.0],
]
}
#[test]
fn test_frustum_cull_all_inside() {
let n = 10usize;
let positions: Vec<[f32; 3]> = (0..n)
.map(|i| {
let t = i as f32 / n as f32 - 0.45;
[t, t * 0.5, t * 0.3]
})
.collect();
let scales: Vec<[f32; 3]> = vec![[-4.0f32, -4.0, -4.0]; n]; let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
let planes = unit_cube_planes();
let hits = bvh.frustum_cull(&planes);
assert_eq!(hits.len(), n);
}
#[test]
fn test_frustum_cull_some_outside() {
let positions = [[0.0f32, 0.0, 0.0], [100.0, 100.0, 100.0]];
let scales = [[-4.0f32, -4.0, -4.0], [-4.0, -4.0, -4.0]];
let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
let planes = unit_cube_planes();
let hits = bvh.frustum_cull(&planes);
assert!(hits.contains(&0));
assert!(!hits.contains(&1));
}
#[test]
fn test_sphere_query() {
let positions = [[0.0f32, 0.0, 0.0], [10.0, 0.0, 0.0], [20.0, 0.0, 0.0]];
let scales = [[-4.0f32, -4.0, -4.0]; 3];
let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
let hits = bvh.sphere_query([0.0, 0.0, 0.0], 1.0);
assert!(hits.contains(&0));
assert!(!hits.contains(&2));
let hits_all = bvh.sphere_query([10.0, 0.0, 0.0], 15.0);
assert_eq!(hits_all.len(), 3);
}
#[test]
fn test_ray_query() {
let positions = [
[1.0f32, 0.0, 0.0],
[5.0, 0.0, 0.0],
[0.0, 10.0, 0.0], ];
let scales = [[-1.0f32, -1.0, -1.0]; 3]; let bvh = GaussianBvh::build(&positions, &scales).expect("BVH build failed");
let hits = bvh.ray_query([0.0, 0.0, 0.0], [1.0, 0.0, 0.0]);
assert!(hits.contains(&0));
assert!(hits.contains(&1));
assert!(!hits.contains(&2));
}
}