tinybvh_rs/ray.rs
1use core::f32;
2
3use crate::ffi;
4
5/// Intersection data.
6///
7/// Contains intersection distance, as well as barycentric coordinates.
8#[repr(C)]
9#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
10pub struct Intersection {
11 /// Intersection distance. [`crate::INFINITE`] when empty.
12 pub t: f32,
13 /// Barycentric weight along the first edge.
14 pub u: f32,
15 /// Barycentric weight along the second edge.
16 pub v: f32,
17 /// Primitive index.
18 pub prim: u32,
19}
20
21impl Intersection {
22 /// Create a new intersection.
23 ///
24 /// The intersection distance defaults to[`crate::INFINITE`] with empty
25 /// barycentric coordinates, and primitive.
26 pub fn new() -> Self {
27 Self {
28 t: crate::INFINITE,
29 ..Default::default()
30 }
31 }
32}
33
34/// Ray data.
35///
36/// Origin, distance, and [`Intersection`].
37///
38/// # Notes
39///
40/// Padding is unused and required for optimal alignment and performance.
41#[repr(C, align(16))]
42#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
43pub struct Ray {
44 /// Ray origin
45 pub origin: [f32; 3],
46 pub padding_0: u32,
47 /// Ray direction
48 pub dir: [f32; 3],
49 pub padding_1: u32,
50 /// Ray inverse direction.
51 /// Automatically computed when using [`Ray::new`].
52 pub r_d: [f32; 3],
53 pub padding_2: u32,
54 /// Ray intersection data.
55 pub hit: Intersection,
56}
57
58impl Ray {
59 /// Create a new ray.
60 ///
61 /// Automatically computes [`Ray::r_d`].
62 pub fn new(origin: [f32; 3], dir: [f32; 3]) -> Self {
63 ffi::ray_new(&origin, &dir)
64 }
65}