Skip to content

Commit c31f3aa

Browse files
authored
Add Aabb2d::new and Aabb3d::new constructors (#11433)
# Objective Currently, the only way to create an AABB is to specify its `min` and `max` coordinates. However, it's often more useful to use the center and half-size instead. ## Solution Add `new` constructors for `Aabb2d` and `Aabb3d`. This: ```rust let aabb = Aabb3d { min: center - half_size, max: center + half_size, } ``` becomes this: ```rust let aabb = Aabb3d::new(center, half_size); ``` I also made the usage of "half-extents" vs. "half-size" a bit more consistent.
1 parent 440bba8 commit c31f3aa

File tree

4 files changed

+44
-60
lines changed

4 files changed

+44
-60
lines changed

crates/bevy_math/src/bounding/bounded2d/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ pub struct Aabb2d {
3838
}
3939

4040
impl Aabb2d {
41+
/// Constructs an AABB from its center and half-size.
42+
#[inline(always)]
43+
pub fn new(center: Vec2, half_size: Vec2) -> Self {
44+
debug_assert!(half_size.x >= 0.0 && half_size.y >= 0.0);
45+
Self {
46+
min: center - half_size,
47+
max: center + half_size,
48+
}
49+
}
50+
4151
/// Computes the smallest [`Aabb2d`] containing the given set of points,
4252
/// transformed by `translation` and `rotation`.
4353
///
@@ -248,7 +258,7 @@ pub struct BoundingCircle {
248258
}
249259

250260
impl BoundingCircle {
251-
/// Construct a bounding circle from its center and radius
261+
/// Constructs a bounding circle from its center and radius.
252262
#[inline(always)]
253263
pub fn new(center: Vec2, radius: f32) -> Self {
254264
debug_assert!(radius >= 0.);

crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ use super::{Aabb2d, Bounded2d, BoundingCircle};
1111

1212
impl Bounded2d for Circle {
1313
fn aabb_2d(&self, translation: Vec2, _rotation: f32) -> Aabb2d {
14-
Aabb2d {
15-
min: translation - Vec2::splat(self.radius),
16-
max: translation + Vec2::splat(self.radius),
17-
}
14+
Aabb2d::new(translation, Vec2::splat(self.radius))
1815
}
1916

2017
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
@@ -47,12 +44,9 @@ impl Bounded2d for Ellipse {
4744
let (ux, uy) = (hw * alpha_cos, hw * alpha_sin);
4845
let (vx, vy) = (hh * beta_cos, hh * beta_sin);
4946

50-
let half_extents = Vec2::new(ux.hypot(vx), uy.hypot(vy));
47+
let half_size = Vec2::new(ux.hypot(vx), uy.hypot(vy));
5148

52-
Aabb2d {
53-
min: translation - half_extents,
54-
max: translation + half_extents,
55-
}
49+
Aabb2d::new(translation, half_size)
5650
}
5751

5852
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
@@ -72,10 +66,7 @@ impl Bounded2d for Plane2d {
7266
let half_height = if facing_y { 0.0 } else { f32::MAX / 2.0 };
7367
let half_size = Vec2::new(half_width, half_height);
7468

75-
Aabb2d {
76-
min: translation - half_size,
77-
max: translation + half_size,
78-
}
69+
Aabb2d::new(translation, half_size)
7970
}
8071

8172
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
@@ -94,10 +85,7 @@ impl Bounded2d for Line2d {
9485
let half_height = if direction.y == 0.0 { 0.0 } else { max };
9586
let half_size = Vec2::new(half_width, half_height);
9687

97-
Aabb2d {
98-
min: translation - half_size,
99-
max: translation + half_size,
100-
}
88+
Aabb2d::new(translation, half_size)
10189
}
10290

10391
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
@@ -109,12 +97,9 @@ impl Bounded2d for Segment2d {
10997
fn aabb_2d(&self, translation: Vec2, rotation: f32) -> Aabb2d {
11098
// Rotate the segment by `rotation`
11199
let direction = Mat2::from_angle(rotation) * *self.direction;
112-
let half_extent = (self.half_length * direction).abs();
100+
let half_size = (self.half_length * direction).abs();
113101

114-
Aabb2d {
115-
min: translation - half_extent,
116-
max: translation + half_extent,
117-
}
102+
Aabb2d::new(translation, half_size)
118103
}
119104

120105
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
@@ -195,10 +180,7 @@ impl Bounded2d for Rectangle {
195180
let abs_rot_mat = Mat2::from_cols_array(&[cos.abs(), sin.abs(), sin.abs(), cos.abs()]);
196181
let half_size = abs_rot_mat * self.half_size;
197182

198-
Aabb2d {
199-
min: translation - half_size,
200-
max: translation + half_size,
201-
}
183+
Aabb2d::new(translation, half_size)
202184
}
203185

204186
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {

crates/bevy_math/src/bounding/bounded3d/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ pub struct Aabb3d {
3333
}
3434

3535
impl Aabb3d {
36+
/// Constructs an AABB from its center and half-size.
37+
#[inline(always)]
38+
pub fn new(center: Vec3, half_size: Vec3) -> Self {
39+
debug_assert!(half_size.x >= 0.0 && half_size.y >= 0.0 && half_size.z >= 0.0);
40+
Self {
41+
min: center - half_size,
42+
max: center + half_size,
43+
}
44+
}
45+
3646
/// Computes the smallest [`Aabb3d`] containing the given set of points,
3747
/// transformed by `translation` and `rotation`.
3848
///
@@ -243,7 +253,7 @@ pub struct BoundingSphere {
243253
}
244254

245255
impl BoundingSphere {
246-
/// Construct a bounding sphere from its center and radius.
256+
/// Constructs a bounding sphere from its center and radius.
247257
pub fn new(center: Vec3, radius: f32) -> Self {
248258
debug_assert!(radius >= 0.);
249259
Self {

crates/bevy_math/src/bounding/bounded3d/primitive_impls.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use super::{Aabb3d, Bounded3d, BoundingSphere};
1414

1515
impl Bounded3d for Sphere {
1616
fn aabb_3d(&self, translation: Vec3, _rotation: Quat) -> Aabb3d {
17-
Aabb3d {
18-
min: translation - Vec3::splat(self.radius),
19-
max: translation + Vec3::splat(self.radius),
20-
}
17+
Aabb3d::new(translation, Vec3::splat(self.radius))
2118
}
2219

2320
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
@@ -39,10 +36,7 @@ impl Bounded3d for Plane3d {
3936
let half_depth = if facing_z { 0.0 } else { f32::MAX / 2.0 };
4037
let half_size = Vec3::new(half_width, half_height, half_depth);
4138

42-
Aabb3d {
43-
min: translation - half_size,
44-
max: translation + half_size,
45-
}
39+
Aabb3d::new(translation, half_size)
4640
}
4741

4842
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
@@ -62,10 +56,7 @@ impl Bounded3d for Line3d {
6256
let half_depth = if direction.z == 0.0 { 0.0 } else { max };
6357
let half_size = Vec3::new(half_width, half_height, half_depth);
6458

65-
Aabb3d {
66-
min: translation - half_size,
67-
max: translation + half_size,
68-
}
59+
Aabb3d::new(translation, half_size)
6960
}
7061

7162
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
@@ -77,12 +68,9 @@ impl Bounded3d for Segment3d {
7768
fn aabb_3d(&self, translation: Vec3, rotation: Quat) -> Aabb3d {
7869
// Rotate the segment by `rotation`
7970
let direction = rotation * *self.direction;
80-
let half_extent = (self.half_length * direction).abs();
71+
let half_size = (self.half_length * direction).abs();
8172

82-
Aabb3d {
83-
min: translation - half_extent,
84-
max: translation + half_extent,
85-
}
73+
Aabb3d::new(translation, half_size)
8674
}
8775

8876
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
@@ -112,7 +100,7 @@ impl Bounded3d for BoxedPolyline3d {
112100

113101
impl Bounded3d for Cuboid {
114102
fn aabb_3d(&self, translation: Vec3, rotation: Quat) -> Aabb3d {
115-
// Compute the AABB of the rotated cuboid by transforming the half-extents
103+
// Compute the AABB of the rotated cuboid by transforming the half-size
116104
// by an absolute rotation matrix.
117105
let rot_mat = Mat3::from_quat(rotation);
118106
let abs_rot_mat = Mat3::from_cols(
@@ -122,10 +110,7 @@ impl Bounded3d for Cuboid {
122110
);
123111
let half_size = abs_rot_mat * self.half_size;
124112

125-
Aabb3d {
126-
min: translation - half_size,
127-
max: translation + half_size,
128-
}
113+
Aabb3d::new(translation, half_size)
129114
}
130115

131116
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {
@@ -147,11 +132,11 @@ impl Bounded3d for Cylinder {
147132
let bottom = -top;
148133

149134
let e = Vec3::ONE - segment_dir * segment_dir;
150-
let half_extents = self.radius * Vec3::new(e.x.sqrt(), e.y.sqrt(), e.z.sqrt());
135+
let half_size = self.radius * Vec3::new(e.x.sqrt(), e.y.sqrt(), e.z.sqrt());
151136

152137
Aabb3d {
153-
min: translation + (top - half_extents).min(bottom - half_extents),
154-
max: translation + (top + half_extents).max(bottom + half_extents),
138+
min: translation + (top - half_size).min(bottom - half_size),
139+
max: translation + (top + half_size).max(bottom + half_size),
155140
}
156141
}
157142

@@ -305,15 +290,12 @@ impl Bounded3d for Torus {
305290
// Reference: http://iquilezles.org/articles/diskbbox/
306291
let normal = rotation * Vec3::Y;
307292
let e = 1.0 - normal * normal;
308-
let disc_half_extents = self.major_radius * Vec3::new(e.x.sqrt(), e.y.sqrt(), e.z.sqrt());
293+
let disc_half_size = self.major_radius * Vec3::new(e.x.sqrt(), e.y.sqrt(), e.z.sqrt());
309294

310-
// Expand the disc by the minor radius to get the torus half extents
311-
let half_extents = disc_half_extents + Vec3::splat(self.minor_radius);
295+
// Expand the disc by the minor radius to get the torus half-size
296+
let half_size = disc_half_size + Vec3::splat(self.minor_radius);
312297

313-
Aabb3d {
314-
min: translation - half_extents,
315-
max: translation + half_extents,
316-
}
298+
Aabb3d::new(translation, half_size)
317299
}
318300

319301
fn bounding_sphere(&self, translation: Vec3, _rotation: Quat) -> BoundingSphere {

0 commit comments

Comments
 (0)