Improve Rectangle and Cuboid consistency (#11434)

# Objective

The `Rectangle` and `Cuboid` primitives currently use different
representations:

```rust
pub struct Rectangle {
    /// The half width of the rectangle
    pub half_width: f32,
    /// The half height of the rectangle
    pub half_height: f32,
}

pub struct Cuboid {
    /// Half of the width, height and depth of the cuboid
    pub half_extents: Vec3,
}
```

The property names and helpers are also inconsistent. `Cuboid` has
`half_extents`, but it also has a method called `from_size`. Most
existing code also uses "size" instead of "extents".

## Solution

Represent both `Rectangle` and `Cuboid` with `half_size` properties.
This commit is contained in:
Joona Aalto 2024-01-20 20:03:47 +02:00 committed by GitHub
parent 320ac65a9e
commit 6337fb33ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 20 deletions

View File

@ -189,22 +189,20 @@ impl Bounded2d for Triangle2d {
impl Bounded2d for Rectangle { impl Bounded2d for Rectangle {
fn aabb_2d(&self, translation: Vec2, rotation: f32) -> Aabb2d { fn aabb_2d(&self, translation: Vec2, rotation: f32) -> Aabb2d {
let half_size = Vec2::new(self.half_width, self.half_height);
// Compute the AABB of the rotated rectangle by transforming the half-extents // Compute the AABB of the rotated rectangle by transforming the half-extents
// by an absolute rotation matrix. // by an absolute rotation matrix.
let (sin, cos) = rotation.sin_cos(); let (sin, cos) = rotation.sin_cos();
let abs_rot_mat = Mat2::from_cols_array(&[cos.abs(), sin.abs(), sin.abs(), cos.abs()]); let abs_rot_mat = Mat2::from_cols_array(&[cos.abs(), sin.abs(), sin.abs(), cos.abs()]);
let half_extents = abs_rot_mat * half_size; let half_size = abs_rot_mat * self.half_size;
Aabb2d { Aabb2d {
min: translation - half_extents, min: translation - half_size,
max: translation + half_extents, max: translation + half_size,
} }
} }
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle { fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
let radius = self.half_width.hypot(self.half_height); let radius = self.half_size.length();
BoundingCircle::new(translation, radius) BoundingCircle::new(translation, radius)
} }
} }

View File

@ -120,12 +120,11 @@ impl Bounded3d for Cuboid {
rot_mat.y_axis.abs(), rot_mat.y_axis.abs(),
rot_mat.z_axis.abs(), rot_mat.z_axis.abs(),
); );
let half_size = abs_rot_mat * self.half_size;
let half_extents = abs_rot_mat * self.half_extents;
Aabb3d { Aabb3d {
min: translation - half_extents, min: translation - half_size,
max: translation + half_extents, max: translation + half_size,
} }
} }
@ -133,7 +132,7 @@ impl Bounded3d for Cuboid {
BoundingSphere { BoundingSphere {
center: translation, center: translation,
sphere: Sphere { sphere: Sphere {
radius: self.half_extents.length(), radius: self.half_size.length(),
}, },
} }
} }

View File

@ -311,12 +311,9 @@ impl Triangle2d {
#[doc(alias = "Quad")] #[doc(alias = "Quad")]
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Rectangle { pub struct Rectangle {
/// The half width of the rectangle /// Half of the width and height of the rectangle
pub half_width: f32, pub half_size: Vec2,
/// The half height of the rectangle
pub half_height: f32,
} }
impl Primitive2d for Rectangle {}
impl Rectangle { impl Rectangle {
/// Create a rectangle from a full width and height /// Create a rectangle from a full width and height
@ -327,8 +324,7 @@ impl Rectangle {
/// Create a rectangle from a given full size /// Create a rectangle from a given full size
pub fn from_size(size: Vec2) -> Self { pub fn from_size(size: Vec2) -> Self {
Self { Self {
half_width: size.x / 2., half_size: size / 2.,
half_height: size.y / 2.,
} }
} }
} }

View File

@ -222,7 +222,7 @@ impl BoxedPolyline3d {
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct Cuboid { pub struct Cuboid {
/// Half of the width, height and depth of the cuboid /// Half of the width, height and depth of the cuboid
pub half_extents: Vec3, pub half_size: Vec3,
} }
impl Primitive3d for Cuboid {} impl Primitive3d for Cuboid {}
@ -235,7 +235,7 @@ impl Cuboid {
/// Create a cuboid from a given full size /// Create a cuboid from a given full size
pub fn from_size(size: Vec3) -> Self { pub fn from_size(size: Vec3) -> Self {
Self { Self {
half_extents: size / 2., half_size: size / 2.,
} }
} }
} }