CSG-Forge/csg/benches/csg_bench.rs

103 lines
2.9 KiB
Rust
Raw Permalink Normal View History

2023-10-12 11:04:21 +00:00
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);