Slicing benchmarks

This commit is contained in:
Connor Slade
2024-07-10 18:40:50 -04:00
parent 67acf90a82
commit 60871ce750
7 changed files with 204 additions and 18 deletions

140
Cargo.lock generated
View File

@@ -198,6 +198,12 @@ dependencies = [
"libc",
]
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "anstream"
version = "0.6.14"
@@ -883,6 +889,12 @@ dependencies = [
"wayland-client",
]
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cc"
version = "1.0.99"
@@ -945,6 +957,33 @@ dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "ciborium"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757"
[[package]]
name = "ciborium-ll"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "4.5.7"
@@ -1182,6 +1221,42 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "criterion"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f"
dependencies = [
"anes",
"cast",
"ciborium",
"clap",
"criterion-plot",
"is-terminal",
"itertools 0.10.5",
"num-traits",
"once_cell",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools 0.10.5",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.5"
@@ -2222,6 +2297,17 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi 0.3.9",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
@@ -2237,6 +2323,15 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.12.1"
@@ -3058,6 +3153,12 @@ version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "oorandom"
version = "11.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9"
[[package]]
name = "orbclient"
version = "0.3.47"
@@ -3185,6 +3286,34 @@ dependencies = [
"typenum",
]
[[package]]
name = "plotters"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a15b6eccb8484002195a3e44fe65a4ce8e93a625797a063735536fd59cb01cf3"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "414cec62c6634ae900ea1c56128dfe87cf63e7caece0852ec76aba307cebadb7"
[[package]]
name = "plotters-svg"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81b30686a7d9c3e010b84284bdd26a29f2138574f52f5eb6f794fc0ad924e705"
dependencies = [
"plotters-backend",
]
[[package]]
name = "png"
version = "0.17.13"
@@ -3897,6 +4026,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"common",
"criterion",
"goo_format",
"itertools 0.13.0",
"nalgebra 0.32.6",
@@ -4177,6 +4307,16 @@ dependencies = [
"strict-num",
]
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "tinyvec"
version = "1.6.0"

View File

@@ -37,5 +37,6 @@
- [x] Improve include_shader macro
- [ ] Don't store rended mesh after sending to GPU
- [x] Fix orbit speed slowing with increasing height
- [ ] Use bumpalo or something for allocation of bvhs
- [x] Use bumpalo or something for allocation of bvhs
- [ ] Optimize bvh more (benchmarking with criterion)
- [ ] Add documentation

View File

@@ -14,3 +14,10 @@ stl_io = "0.7.0"
common = { path = "../common" }
goo_format = { path = "../goo_format" }
[dev-dependencies]
criterion = "0.5.1"
[[bench]]
name = "benchmark"
harness = false

View File

@@ -0,0 +1,32 @@
use std::{fs::File, io::BufReader, path::Path};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use nalgebra::Vector3;
use slicer::{bvh::Bvh, mesh::load_mesh};
pub fn bench(c: &mut Criterion) {
let mut group = c.benchmark_group("Mesh Intersections");
let parent = Path::new("..");
for mesh_name in ["teapot.stl", "dragon.stl", "david.stl"] {
let mut file = BufReader::new(File::open(parent.join(mesh_name)).unwrap());
let mesh = load_mesh(&mut file, "stl").unwrap();
let bvh = Bvh::from_mesh(&mesh);
group.bench_with_input(BenchmarkId::new("Linier", mesh_name), &mesh, |b, i| {
b.iter(|| i.intersect_plane(0.0))
});
group.bench_with_input(
BenchmarkId::new("Bounding Volume Hierarchy", mesh_name),
&(bvh, mesh),
|b, (bvh, mesh)| {
b.iter(|| bvh.intersect_plane(mesh, Vector3::zeros(), *Vector3::z_axis()))
},
);
}
}
criterion_group!(benches, bench);
criterion_main!(benches);

View File

@@ -58,22 +58,27 @@ impl BoundingBox {
let (min, max) = (self.min, self.max);
let cube_vertices = [
// Bottom vertices
(Vector3::new(min.x, min.y, min.z) - pos).dot(&normal),
(Vector3::new(max.x, min.y, min.z) - pos).dot(&normal),
(Vector3::new(min.x, max.y, min.z) - pos).dot(&normal),
(Vector3::new(max.x, max.y, min.z) - pos).dot(&normal),
Vector3::new(min.x, min.y, min.z),
Vector3::new(max.x, min.y, min.z),
Vector3::new(min.x, max.y, min.z),
Vector3::new(max.x, max.y, min.z),
// Top vertices
(Vector3::new(min.x, min.y, max.z) - pos).dot(&normal),
(Vector3::new(max.x, min.y, max.z) - pos).dot(&normal),
(Vector3::new(min.x, max.y, max.z) - pos).dot(&normal),
(Vector3::new(max.x, max.y, max.z) - pos).dot(&normal),
Vector3::new(min.x, min.y, max.z),
Vector3::new(max.x, min.y, max.z),
Vector3::new(min.x, max.y, max.z),
Vector3::new(max.x, max.y, max.z),
];
let intersection_test =
|a: usize, b: usize| (cube_vertices[a] > 0.0) ^ (cube_vertices[b] > 0.0);
let intersection_test = |a: usize, b: usize| {
let a = (cube_vertices[a] - pos).dot(&normal);
let b = (cube_vertices[b] - pos).dot(&normal);
(a > 0.0) ^ (b > 0.0)
};
(0..4).any(|x| intersection_test(x, (x + 1) % 4))
|| (0..4).any(|x| intersection_test(x + 4, (x + 1) % 4 + 4))
|| (0..4).any(|x| intersection_test(x, x + 4))
// (0..4).any(|x| intersection_test(x, (x + 1) % 4))
// || (0..4).any(|x| intersection_test(x + 4, (x + 1) % 4 + 4))
// || (0..4).any(|x| intersection_test(x, x + 4))
(0..4).any(|x| intersection_test(x, x + 4))
}
}

View File

@@ -14,9 +14,10 @@ impl BvhNode {
out: &mut Vec<Vector3<f32>>,
) {
match self {
BvhNode::Leaf { face_idx, bounds } if bounds.intersect_plane(pos, normal) => {
intersect_triangle(mesh, *face_idx, pos, normal, out)
}
BvhNode::Leaf {
face_idx,
bounds: _,
} => intersect_triangle(mesh, *face_idx, pos, normal, out),
BvhNode::Node {
left,
right,

View File

@@ -1,6 +1,6 @@
use nalgebra::Vector3;
mod bvh;
pub mod bvh;
pub mod mesh;
pub mod slicer;