From 4b7685ae3c10747a98b87264bd2f839f4825e8a7 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 12 Oct 2023 12:04:21 +0100 Subject: [PATCH] Add some basic benchmarks --- csg/Cargo.toml | 5 ++ csg/benches/csg_bench.rs | 102 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 csg/benches/csg_bench.rs diff --git a/csg/Cargo.toml b/csg/Cargo.toml index 5d04a1d..65f042f 100644 --- a/csg/Cargo.toml +++ b/csg/Cargo.toml @@ -8,3 +8,8 @@ edition = "2021" [dependencies] glam = "0.24.1" itertools = "0.11.0" +criterion = "0.5.1" + +[[bench]] +name = "csg_bench" +harness = false diff --git a/csg/benches/csg_bench.rs b/csg/benches/csg_bench.rs new file mode 100644 index 0000000..e17fa18 --- /dev/null +++ b/csg/benches/csg_bench.rs @@ -0,0 +1,102 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use csg::{ + brush::{Brush, BrushPlane}, + plane::Plane, +}; +use glam::{Mat3, Vec3}; + +fn generate_box_brush(position: Vec3, half_extent: Vec3) -> Brush { + let normals = vec![ + glam::vec3(1.0, 0.0, 0.0), + glam::vec3(-1.0, 0.0, 0.0), + glam::vec3(0.0, 1.0, 0.0), + glam::vec3(0.0, -1.0, 0.0), + glam::vec3(0.0, 0.0, 1.0), + glam::vec3(0.0, 0.0, -1.0), + ]; + + let mut brush_planes = vec![]; + for normal in &normals { + let point = position + *normal * half_extent; + + brush_planes.push(BrushPlane { + plane: Plane::from_point_normal(point, *normal), + ..Default::default() + }); + } + + Brush::new(&brush_planes) +} + +/// Position is center of base plane +fn generate_pyramid_brush(position: Vec3, height: f32, side_count: u32) -> Brush { + let tip = position + Vec3::Y * height; + let base_plane = Plane::from_point_normal(position, Vec3::NEG_Y); + + let mut brush_planes = vec![BrushPlane { + plane: base_plane, + ..Default::default() + }]; + + for i in 0..side_count { + let angle = (i as f32 / side_count as f32) * 360.0_f32.to_radians(); + let rot_mat = Mat3::from_cols( + Vec3::new(angle.cos(), 0.0, -angle.sin()), + Vec3::Y, + Vec3::new(angle.sin(), 0.0, angle.cos()), + ); + let normal = rot_mat * Vec3::new(1.0, 1.0, 0.0); + + brush_planes.push(BrushPlane { + plane: Plane::from_point_normal(tip, normal), + ..Default::default() + }); + } + + Brush::new(&brush_planes) +} + +fn get_bench_brushes() -> (Brush, Brush) { + let mut b1 = generate_pyramid_brush(Vec3::ZERO, 1.0, 64); + b1.rebuild(); + let mut b2 = generate_box_brush(Vec3::new(2.0, 2.0, 0.0), Vec3::new(1.0, 1.0, 1.0)); + b2.rebuild(); + (b1, b2) +} + +pub fn bench_aabb_intersects_with(c: &mut Criterion) { + let (b1, b2) = get_bench_brushes(); + c.bench_function("AABB Intersection", |b| { + b.iter(|| b1.aabb_intersects_with(black_box(&b2))) + }); +} + +pub fn bench_planes_intersects_with(c: &mut Criterion) { + let (b1, b2) = get_bench_brushes(); + c.bench_function("Planes Intersection", |b| { + b.iter(|| b1.planes_intersect_with(black_box(&b2))) + }); +} + +pub fn bench_planes_intersects_with_mat(c: &mut Criterion) { + let (b1, b2) = get_bench_brushes(); + c.bench_function("Planes Intersection Matrix", |b| { + b.iter(|| b1.planes_intersect_with_mat(black_box(&b2))) + }); +} + +pub fn bench_planes_intersects_with_gauss(c: &mut Criterion) { + let (b1, b2) = get_bench_brushes(); + c.bench_function("Planes Intersection Gaussian", |b| { + b.iter(|| b1.planes_intersect_with_gauss(black_box(&b2))) + }); +} + +criterion_group!( + benches, + bench_aabb_intersects_with, + bench_planes_intersects_with, + bench_planes_intersects_with_mat, + bench_planes_intersects_with_gauss +); +criterion_main!(benches);