Skip to content

DavidPeicho/tinybvh-rs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinybvh-rs

Opinionated rust wrapper for tinybvh.

Features

  • [BVH], [MBVH] [BVH8_CPU], [CWBVH]
  • Intersection

For more information about each layout: tinybvh.

Unimplemented features:

  • Optimize via Verbose
  • Loading/saving from/to disk

Examples

BVH Wald

use tinybvh_rs::{Intersector, Ray};

let primitives = vec![
    [-2.0, 1.0, -1.0, 0.0],    //
    [-1.0, 1.0, -1.0, 0.0],    // Left triangle
    [-2.0, 0.0, -1.0, 0.0],    //

    [2.0, 1.0, -1.0, 0.0],     //
    [2.0, 0.0, -1.0, 0.0],     // Right triangle
    [1.0, 0.0, -1.0, 0.0],     //
];

let bvh = wald::BVH::new(primitives.as_slice().into()).unwrap();

// No intersection, ray pass between the primitives
let mut ray = Ray::new([0.0, 0.0, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance: {}", ray.hit.t); // 1e30

// Intersects left primitive
let mut ray = Ray::new([-1.5, 0.5, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance & primtive: {} / {}", ray.hit.t, ray.hit.prim); // 1.0 / 0

// Intersects right primitive
let mut ray = Ray::new([1.5, 0.45, 0.0], [0.0, 0.0, -1.0]);
bvh.intersect(&mut ray);
println!("Hit distance & primtive: {} / {}", ray.hit.t, ray.hit.prim); // 1.0 / 1

CWBVH

let primitives = vec![
    [-2.0, 1.0, -1.0, 0.0],    //
    [-1.0, 1.0, -1.0, 0.0],    // Left triangle
    [-2.0, 0.0, -1.0, 0.0],    //

    [2.0, 1.0, -1.0, 0.0],     //
    [2.0, 0.0, -1.0, 0.0],     // Right triangle
    [1.0, 0.0, -1.0, 0.0],     //
];
let mut bvh = wald::BVH::new(primitives.as_slice().into()).unwrap();
bvh.split_leaves(3); // Required or the CWBVH will return an error

let mbvh = mbvh::BVH::new(&bvh);
let cwbvh = cwbvh::BVH::new(&mbvh).unwrap();

println!("{}", cwbvh.nodes());
println!("{}", cwbvh.primitives());

Strided

If the vertices position are strided (located in a Vertex struct for instance), you can enable the strided feature and use:

use tinybvh_rs::{Intersector, Ray};

#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct Vertex {
    position: [f32; 4],
    normal: [f32; 3],
}

let vertices = [
    Vertex {
        position: [-1.0, 1.0, -1.0, 0.0],
        normal: [0.0, 0.0, 1.0]
    },
    Vertex {
        position: [-0.5, 1.0, -1.0, 0.0],
        normal: [0.0, 0.0, 1.0]
    },
    Vertex {
        position: [-1.0, 0.0, -1.0, 0.0],
        normal: [0.0, 0.0, 1.0]
    },
];
let positions = pas::slice_attr!(vertices, [0].position);
let bvh = wald::BVH::new(positions);

About

Rust wrapper for the tinybvh libary

Resources

License

Stars

Watchers

Forks

Packages

No packages published