Skip to content

Commit

Permalink
KdTree intersect tests for flat cells
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Mar 27, 2024
1 parent 4e031d0 commit 65e409a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 64 deletions.
6 changes: 6 additions & 0 deletions geometry/src/algorithms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct TriangleRayIntersection {
pub v: f32,
}

impl TriangleRayIntersection {
pub fn new(t: f32, u: f32, v: f32) -> Self {
TriangleRayIntersection { t, u, v }
}
}

pub fn intersect_triangle_ray(triangle: &Triangle, ray: &Ray) -> Option<TriangleRayIntersection> {
let b0 = triangle.base0();
let b1 = triangle.base1();
Expand Down
143 changes: 79 additions & 64 deletions kdtree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,25 +235,11 @@ mod tests {

assert_eq!(
tree.intersect(&ray1, 0., 1.),
Some((
0,
TriangleRayIntersection {
t: 0.25,
u: 0.,
v: 0.5
}
))
Some((0, TriangleRayIntersection::new(0.25, 0., 0.5)))
);
assert_eq!(
tree.intersect(&ray2, 0., 1.),
Some((
1,
TriangleRayIntersection {
t: 0.25,
u: 0.,
v: 0.5
}
))
Some((1, TriangleRayIntersection::new(0.25, 0., 0.5)))
);
}

Expand All @@ -270,40 +256,23 @@ mod tests {
v2: Vector3::new(2., 1., 0.),
};
let tree = KdTree {
root: Box::new(KdNode::Node {
plane: Aap {
axis: Axis::X,
distance: 1.,
},
left: Box::new(KdNode::Leaf(vec![0])),
right: Box::new(KdNode::Leaf(vec![1])),
}),
root: KdNode::new_node(
Aap::new_x(1.0),
KdNode::new_leaf(vec![0]),
KdNode::new_leaf(vec![1]),
),
triangles: vec![triangle0, triangle1],
};
let ray_triangle0_v0 = Ray::between(&Vector3::new(0., 0., -1.), &Vector3::new(0., 0., 1.));
let ray_triangle1_v1 = Ray::between(&Vector3::new(2., 0., -1.), &Vector3::new(2., 0., 1.));

assert_eq!(
tree.intersect(&ray_triangle0_v0, 0., 1.),
Some((
0,
TriangleRayIntersection {
t: 0.5,
u: 0.,
v: 0.
}
))
Some((0, TriangleRayIntersection::new(0.5, 0., 0.)))
);
assert_eq!(
tree.intersect(&ray_triangle1_v1, 0., 1.),
Some((
1,
TriangleRayIntersection {
t: 0.5,
u: 1.,
v: 0.
}
))
Some((1, TriangleRayIntersection::new(0.5, 1., 0.)))
);
}

Expand All @@ -320,40 +289,86 @@ mod tests {
v2: Vector3::new(2., 1., 1.),
};
let tree = KdTree {
root: Box::new(KdNode::Node {
plane: Aap {
axis: Axis::X,
distance: 1.,
},
left: Box::new(KdNode::Leaf(vec![0])),
right: Box::new(KdNode::Leaf(vec![1])),
}),
root: KdNode::new_node(
Aap::new_x(1.0),
KdNode::new_leaf(vec![0]),
KdNode::new_leaf(vec![1]),
),
triangles: vec![triangle0, triangle1],
};
let ray1 = Ray::between(&Vector3::new(-1., 0., 0.), &Vector3::new(3., 0., 0.));
let ray2 = ray1.reverse();

assert_eq!(
tree.intersect(&ray1, 0., 1.),
Some((
0,
TriangleRayIntersection {
t: 0.25,
u: 0.,
v: 0.5
}
))
Some((0, TriangleRayIntersection::new(0.25, 0., 0.5)))
);
assert_eq!(
tree.intersect(&ray2, 0., 1.),
Some((
1,
TriangleRayIntersection {
t: 0.25,
u: 0.,
v: 0.5
}
))
Some((1, TriangleRayIntersection::new(0.25, 0., 0.5)))
);
}

#[test]
fn intersect_split_at_axially_aligned_triangle() {
let triangle = Triangle {
v0: Vector3::new(0., 0., 0.),
v1: Vector3::new(1., 0., 0.),
v2: Vector3::new(0., 1., 0.),
};
let tree_left = KdTree {
root: KdNode::new_node(
Aap::new_z(0.0),
KdNode::new_leaf(vec![0]),
KdNode::new_leaf(vec![]),
),
triangles: vec![triangle.clone()],
};
let tree_right = KdTree {
root: KdNode::new_node(
Aap::new_z(0.0),
KdNode::new_leaf(vec![]),
KdNode::new_leaf(vec![0]),
),
triangles: vec![triangle],
};
let ray = Ray::between(&Vector3::new(0., 0., -1.), &Vector3::new(0., 0., 1.));

assert_eq!(
tree_left.intersect(&ray, 0., 1.),
Some((0, TriangleRayIntersection::new(0.5, 0., 0.)))
);
assert_eq!(
tree_right.intersect(&ray, 0., 1.),
Some((0, TriangleRayIntersection::new(0.5, 0., 0.)))
);
}

#[test]
fn intersect_flat_cell() {
let triangle = Triangle {
v0: Vector3::new(0., 0., 0.),
v1: Vector3::new(1., 0., 0.),
v2: Vector3::new(0., 1., 0.),
};
let tree = KdTree {
root: KdNode::new_node(
Aap::new_z(0.0),
KdNode::new_node(Aap::new_z(0.0), KdNode::new_leaf(vec![0]), KdNode::empty()),
KdNode::empty(),
),
triangles: vec![triangle.clone()],
};
let ray0 = Ray::between(&Vector3::new(0., 0., -1.), &Vector3::new(0., 0., 1.));
let ray1 = ray0.reverse();

assert_eq!(
tree.intersect(&ray0, 0., 1.),
Some((0, TriangleRayIntersection::new(0.5, 0., 0.)))
);
assert_eq!(
tree.intersect(&ray1, 0., 1.),
Some((0, TriangleRayIntersection::new(0.5, 0., 0.)))
);
}
}

0 comments on commit 65e409a

Please sign in to comment.