|
| 1 | +use bevy_camera::primitives::{Aabb, Frustum, Sphere}; |
| 2 | +use bevy_math::{ |
| 3 | + primitives::{HalfSpace, ViewFrustum}, |
| 4 | + Affine3A, Quat, Vec3, Vec3A, Vec4, |
| 5 | +}; |
| 6 | +use core::hint::black_box; |
| 7 | +use criterion::{criterion_group, Criterion}; |
| 8 | + |
| 9 | +pub fn intersects_obb(c: &mut Criterion) { |
| 10 | + let mut group = c.benchmark_group("intersects_obb"); |
| 11 | + |
| 12 | + let aabb = Aabb { |
| 13 | + center: Vec3A::ZERO, |
| 14 | + half_extents: Vec3A::new(0.5, 0.5, 0.5), |
| 15 | + }; |
| 16 | + |
| 17 | + let world_from_local = Affine3A::from_rotation_translation( |
| 18 | + Quat::from_rotation_y(std::f32::consts::FRAC_PI_4), |
| 19 | + Vec3::new(1.0, 0.5, -0.5), |
| 20 | + ); |
| 21 | + |
| 22 | + let identity_transform = Affine3A::IDENTITY; |
| 23 | + |
| 24 | + let sphere = Sphere { |
| 25 | + center: Vec3A::new(1.0, 0.5, 0.0), |
| 26 | + radius: 1.5, |
| 27 | + }; |
| 28 | + |
| 29 | + let frustum = Frustum(ViewFrustum { |
| 30 | + half_spaces: [ |
| 31 | + HalfSpace::new(Vec4::new(-0.9701, -0.2425, -0.0000, 0.7276)), |
| 32 | + HalfSpace::new(Vec4::new(-0.0000, 1.0000, -0.0000, 1.0000)), |
| 33 | + HalfSpace::new(Vec4::new(-0.0000, -0.2425, -0.9701, 0.7276)), |
| 34 | + HalfSpace::new(Vec4::new(-0.0000, -1.0000, -0.0000, 1.0000)), |
| 35 | + HalfSpace::new(Vec4::new(-0.0000, -0.2425, 0.9701, 0.7276)), |
| 36 | + HalfSpace::new(Vec4::new(0.9701, -0.2425, -0.0000, 0.7276)), |
| 37 | + ], |
| 38 | + }); |
| 39 | + |
| 40 | + assert_eq!(sphere.intersects_obb(&aabb, &world_from_local), true); |
| 41 | + group.bench_function("sphere_intersects_obb", |b| { |
| 42 | + b.iter(|| black_box(sphere.intersects_obb(black_box(&aabb), black_box(&world_from_local)))); |
| 43 | + }); |
| 44 | + |
| 45 | + assert_eq!( |
| 46 | + frustum.intersects_obb(&aabb, &world_from_local, true, true), |
| 47 | + true |
| 48 | + ); |
| 49 | + group.bench_function("frustum_intersects_obb", |b| { |
| 50 | + b.iter(|| { |
| 51 | + black_box(frustum.intersects_obb( |
| 52 | + black_box(&aabb), |
| 53 | + black_box(&world_from_local), |
| 54 | + black_box(true), // intersect_near |
| 55 | + black_box(true), // intersect_far |
| 56 | + )) |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + assert_eq!( |
| 61 | + frustum.intersects_obb(&aabb, &identity_transform, true, true), |
| 62 | + true |
| 63 | + ); |
| 64 | + group.bench_function("frustum_intersects_obb_fallback_identity", |b| { |
| 65 | + b.iter(|| { |
| 66 | + black_box(frustum.intersects_obb( |
| 67 | + black_box(&aabb), |
| 68 | + black_box(&identity_transform), |
| 69 | + black_box(true), |
| 70 | + black_box(true), |
| 71 | + )) |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + assert_eq!(frustum.intersects_obb_identity(&aabb), true); |
| 76 | + group.bench_function("frustum_intersects_obb_identity", |b| { |
| 77 | + b.iter(|| black_box(frustum.intersects_obb_identity(black_box(&aabb)))); |
| 78 | + }); |
| 79 | + |
| 80 | + group.finish(); |
| 81 | +} |
| 82 | + |
| 83 | +criterion_group!(benches, intersects_obb); |
0 commit comments