bvh/lib.rs
1//! A crate which exports rays, axis-aligned bounding boxes, and binary bounding
2//! volume hierarchies.
3//!
4//! ## About
5//!
6//! This crate can be used for applications which contain intersection computations of rays
7//! with primitives. For this purpose a binary tree [`Bvh`](bvh::Bvh) (Bounding Volume Hierarchy) is of great
8//! use if the scene which the ray traverses contains a huge number of primitives. With a [`Bvh`](bvh::Bvh) the
9//! intersection test complexity is reduced from O(n) to O(log2(n)) at the cost of building
10//! the [`Bvh`](bvh::Bvh) once in advance. This technique is especially useful in ray/path tracers. For
11//! use in a shader this module also exports a flattening procedure, which allows for
12//! iterative traversal of the [`Bvh`](bvh::Bvh).
13//!
14//! ## Note
15//!
16//! If you are concerned about performance and do not mind using nightly, it is recommended to
17//! use the `simd` feature as it introduces explicitly written simd to optimize certain areas
18//! of the BVH.
19//!
20//! ## Example
21//!
22//! ```
23//! use bvh::aabb::{Aabb, Bounded};
24//! use bvh::bounding_hierarchy::{BHShape, BoundingHierarchy};
25//! use bvh::bvh::Bvh;
26//! use nalgebra::{Point3, Vector3};
27//! use bvh::ray::Ray;
28//!
29//! let origin = Point3::new(0.0,0.0,0.0);
30//! let direction = Vector3::new(1.0,0.0,0.0);
31//! let ray = Ray::new(origin, direction);
32//!
33//! struct Sphere {
34//! position: Point3<f32>,
35//! radius: f32,
36//! node_index: usize,
37//! }
38//!
39//! impl Bounded<f32,3> for Sphere {
40//! fn aabb(&self) -> Aabb<f32,3> {
41//! let half_size = Vector3::new(self.radius, self.radius, self.radius);
42//! let min = self.position - half_size;
43//! let max = self.position + half_size;
44//! Aabb::with_bounds(min, max)
45//! }
46//! }
47//!
48//! impl BHShape<f32,3> for Sphere {
49//! fn set_bh_node_index(&mut self, index: usize) {
50//! self.node_index = index;
51//! }
52//!
53//! fn bh_node_index(&self) -> usize {
54//! self.node_index
55//! }
56//! }
57//!
58//! let mut spheres = Vec::new();
59//! for i in 0..1000u32 {
60//! let position = Point3::new(i as f32, i as f32, i as f32);
61//! let radius = (i % 10) as f32 + 1.0;
62//! spheres.push(Sphere {
63//! position: position,
64//! radius: radius,
65//! node_index: 0,
66//! });
67//! }
68//!
69//! let bvh = Bvh::build_par(&mut spheres);
70//! let hit_sphere_aabbs = bvh.traverse(&ray, &spheres);
71//! ```
72//!
73//! ## Features
74//!
75//! - `serde` (default **disabled**) - adds `Serialize` and `Deserialize` implementations for some types
76//! - `simd` (default **disabled**) - adds explicitly written SIMD instructions for certain architectures (requires nightly)
77//!
78
79#![no_std]
80#![deny(missing_docs)]
81#![cfg_attr(feature = "bench", feature(test))]
82#![cfg_attr(feature = "simd", feature(min_specialization))]
83#![cfg(feature = "std")]
84extern crate std;
85
86#[cfg(all(feature = "bench", test))]
87extern crate test;
88
89extern crate alloc;
90
91pub mod aabb;
92pub mod ball;
93pub mod bounding_hierarchy;
94pub mod bvh;
95pub mod flat_bvh;
96pub mod point_query;
97pub mod ray;
98mod utils;
99
100#[cfg(test)]
101mod testbase;
102
103#[cfg(doctest)]
104doc_comment::doctest!("../README.md", readme);