Path: blob/main/crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs
6598 views
//! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives).12use crate::{3bounding::BoundingVolume,4ops,5primitives::{6Annulus, Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d,7Plane2d, Rectangle, RegularPolygon, Rhombus, Segment2d, Triangle2d,8},9Dir2, Isometry2d, Mat2, Rot2, Vec2,10};11use core::f32::consts::{FRAC_PI_2, PI, TAU};1213#[cfg(feature = "alloc")]14use crate::primitives::{ConvexPolygon, Polygon, Polyline2d};1516use smallvec::SmallVec;1718use super::{Aabb2d, Bounded2d, BoundingCircle};1920impl Bounded2d for Circle {21fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {22let isometry = isometry.into();23Aabb2d::new(isometry.translation, Vec2::splat(self.radius))24}2526fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {27let isometry = isometry.into();28BoundingCircle::new(isometry.translation, self.radius)29}30}3132// Compute the axis-aligned bounding points of a rotated arc, used for computing the AABB of arcs and derived shapes.33// The return type has room for 7 points so that the CircularSector code can add an additional point.34#[inline]35fn arc_bounding_points(arc: Arc2d, rotation: impl Into<Rot2>) -> SmallVec<[Vec2; 7]> {36// Otherwise, the extreme points will always be either the endpoints or the axis-aligned extrema of the arc's circle.37// We need to compute which axis-aligned extrema are actually contained within the rotated arc.38let mut bounds = SmallVec::<[Vec2; 7]>::new();39let rotation = rotation.into();40bounds.push(rotation * arc.left_endpoint());41bounds.push(rotation * arc.right_endpoint());4243// The half-angles are measured from a starting point of π/2, being the angle of Vec2::Y.44// Compute the normalized angles of the endpoints with the rotation taken into account, and then45// check if we are looking for an angle that is between or outside them.46let left_angle = ops::rem_euclid(FRAC_PI_2 + arc.half_angle + rotation.as_radians(), TAU);47let right_angle = ops::rem_euclid(FRAC_PI_2 - arc.half_angle + rotation.as_radians(), TAU);48let inverted = left_angle < right_angle;49for extremum in [Vec2::X, Vec2::Y, Vec2::NEG_X, Vec2::NEG_Y] {50let angle = ops::rem_euclid(extremum.to_angle(), TAU);51// If inverted = true, then right_angle > left_angle, so we are looking for an angle that is not between them.52// There's a chance that this condition fails due to rounding error, if the endpoint angle is juuuust shy of the axis.53// But in that case, the endpoint itself is within rounding error of the axis and will define the bounds just fine.54let angle_within_parameters = if inverted {55angle >= right_angle || angle <= left_angle56} else {57angle >= right_angle && angle <= left_angle58};59if angle_within_parameters {60bounds.push(extremum * arc.radius);61}62}63bounds64}6566impl Bounded2d for Arc2d {67fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {68// If our arc covers more than a circle, just return the bounding box of the circle.69if self.half_angle >= PI {70return Circle::new(self.radius).aabb_2d(isometry);71}7273let isometry = isometry.into();7475Aabb2d::from_point_cloud(76Isometry2d::from_translation(isometry.translation),77&arc_bounding_points(*self, isometry.rotation),78)79}8081fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {82let isometry = isometry.into();8384// There are two possibilities for the bounding circle.85if self.is_major() {86// If the arc is major, then the widest distance between two points is a diameter of the arc's circle;87// therefore, that circle is the bounding radius.88BoundingCircle::new(isometry.translation, self.radius)89} else {90// Otherwise, the widest distance between two points is the chord,91// so a circle of that diameter around the midpoint will contain the entire arc.92let center = isometry.rotation * self.chord_midpoint();93BoundingCircle::new(center + isometry.translation, self.half_chord_length())94}95}96}9798impl Bounded2d for CircularSector {99fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {100let isometry = isometry.into();101102// If our sector covers more than a circle, just return the bounding box of the circle.103if self.half_angle() >= PI {104return Circle::new(self.radius()).aabb_2d(isometry);105}106107// Otherwise, we use the same logic as for Arc2d, above, just with the circle's center as an additional possibility.108let mut bounds = arc_bounding_points(self.arc, isometry.rotation);109bounds.push(Vec2::ZERO);110111Aabb2d::from_point_cloud(Isometry2d::from_translation(isometry.translation), &bounds)112}113114fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {115if self.arc.is_major() {116let isometry = isometry.into();117118// If the arc is major, that is, greater than a semicircle,119// then bounding circle is just the circle defining the sector.120BoundingCircle::new(isometry.translation, self.arc.radius)121} else {122// However, when the arc is minor,123// we need our bounding circle to include both endpoints of the arc as well as the circle center.124// This means we need the circumcircle of those three points.125// The circumcircle will always have a greater curvature than the circle itself, so it will contain126// the entire circular sector.127Triangle2d::new(128Vec2::ZERO,129self.arc.left_endpoint(),130self.arc.right_endpoint(),131)132.bounding_circle(isometry)133}134}135}136137impl Bounded2d for CircularSegment {138fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {139self.arc.aabb_2d(isometry)140}141142fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {143self.arc.bounding_circle(isometry)144}145}146147impl Bounded2d for Ellipse {148fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {149let isometry = isometry.into();150151// V = (hh * cos(beta), hh * sin(beta))152// #####*#####153// ### | ###154// # hh | #155// # *---------* U = (hw * cos(alpha), hw * sin(alpha))156// # hw #157// ### ###158// ###########159160let (hw, hh) = (self.half_size.x, self.half_size.y);161162// Sine and cosine of rotation angle alpha.163let (alpha_sin, alpha_cos) = isometry.rotation.sin_cos();164165// Sine and cosine of alpha + pi/2. We can avoid the trigonometric functions:166// sin(beta) = sin(alpha + pi/2) = cos(alpha)167// cos(beta) = cos(alpha + pi/2) = -sin(alpha)168let (beta_sin, beta_cos) = (alpha_cos, -alpha_sin);169170// Compute points U and V, the extremes of the ellipse171let (ux, uy) = (hw * alpha_cos, hw * alpha_sin);172let (vx, vy) = (hh * beta_cos, hh * beta_sin);173174let half_size = Vec2::new(ops::hypot(ux, vx), ops::hypot(uy, vy));175176Aabb2d::new(isometry.translation, half_size)177}178179fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {180let isometry = isometry.into();181BoundingCircle::new(isometry.translation, self.semi_major())182}183}184185impl Bounded2d for Annulus {186fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {187let isometry = isometry.into();188Aabb2d::new(isometry.translation, Vec2::splat(self.outer_circle.radius))189}190191fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {192let isometry = isometry.into();193BoundingCircle::new(isometry.translation, self.outer_circle.radius)194}195}196197impl Bounded2d for Rhombus {198fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {199let isometry = isometry.into();200201let [rotated_x_half_diagonal, rotated_y_half_diagonal] = [202isometry.rotation * Vec2::new(self.half_diagonals.x, 0.0),203isometry.rotation * Vec2::new(0.0, self.half_diagonals.y),204];205let aabb_half_extent = rotated_x_half_diagonal206.abs()207.max(rotated_y_half_diagonal.abs());208209Aabb2d {210min: -aabb_half_extent + isometry.translation,211max: aabb_half_extent + isometry.translation,212}213}214215fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {216let isometry = isometry.into();217BoundingCircle::new(isometry.translation, self.circumradius())218}219}220221impl Bounded2d for Plane2d {222fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {223let isometry = isometry.into();224225let normal = isometry.rotation * *self.normal;226let facing_x = normal == Vec2::X || normal == Vec2::NEG_X;227let facing_y = normal == Vec2::Y || normal == Vec2::NEG_Y;228229// Dividing `f32::MAX` by 2.0 is helpful so that we can do operations230// like growing or shrinking the AABB without breaking things.231let half_width = if facing_x { 0.0 } else { f32::MAX / 2.0 };232let half_height = if facing_y { 0.0 } else { f32::MAX / 2.0 };233let half_size = Vec2::new(half_width, half_height);234235Aabb2d::new(isometry.translation, half_size)236}237238fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {239let isometry = isometry.into();240BoundingCircle::new(isometry.translation, f32::MAX / 2.0)241}242}243244impl Bounded2d for Line2d {245fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {246let isometry = isometry.into();247248let direction = isometry.rotation * *self.direction;249250// Dividing `f32::MAX` by 2.0 is helpful so that we can do operations251// like growing or shrinking the AABB without breaking things.252let max = f32::MAX / 2.0;253let half_width = if direction.x == 0.0 { 0.0 } else { max };254let half_height = if direction.y == 0.0 { 0.0 } else { max };255let half_size = Vec2::new(half_width, half_height);256257Aabb2d::new(isometry.translation, half_size)258}259260fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {261let isometry = isometry.into();262BoundingCircle::new(isometry.translation, f32::MAX / 2.0)263}264}265266impl Bounded2d for Segment2d {267fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {268Aabb2d::from_point_cloud(isometry, &[self.point1(), self.point2()])269}270271fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {272let isometry: Isometry2d = isometry.into();273let local_center = self.center();274let radius = local_center.distance(self.point1());275let local_circle = BoundingCircle::new(local_center, radius);276local_circle.transformed_by(isometry.translation, isometry.rotation)277}278}279280#[cfg(feature = "alloc")]281impl Bounded2d for Polyline2d {282fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {283Aabb2d::from_point_cloud(isometry, &self.vertices)284}285286fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {287BoundingCircle::from_point_cloud(isometry, &self.vertices)288}289}290291impl Bounded2d for Triangle2d {292fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {293let isometry = isometry.into();294let [a, b, c] = self.vertices.map(|vtx| isometry.rotation * vtx);295296let min = Vec2::new(a.x.min(b.x).min(c.x), a.y.min(b.y).min(c.y));297let max = Vec2::new(a.x.max(b.x).max(c.x), a.y.max(b.y).max(c.y));298299Aabb2d {300min: min + isometry.translation,301max: max + isometry.translation,302}303}304305fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {306let isometry = isometry.into();307let [a, b, c] = self.vertices;308309// The points of the segment opposite to the obtuse or right angle if one exists310let side_opposite_to_non_acute = if (b - a).dot(c - a) <= 0.0 {311Some((b, c))312} else if (c - b).dot(a - b) <= 0.0 {313Some((c, a))314} else if (a - c).dot(b - c) <= 0.0 {315Some((a, b))316} else {317// The triangle is acute.318None319};320321// Find the minimum bounding circle. If the triangle is obtuse, the circle passes through two vertices.322// Otherwise, it's the circumcircle and passes through all three.323if let Some((point1, point2)) = side_opposite_to_non_acute {324// The triangle is obtuse or right, so the minimum bounding circle's diameter is equal to the longest side.325// We can compute the minimum bounding circle from the line segment of the longest side.326let segment = Segment2d::new(point1, point2);327segment.bounding_circle(isometry)328} else {329// The triangle is acute, so the smallest bounding circle is the circumcircle.330let (Circle { radius }, circumcenter) = self.circumcircle();331BoundingCircle::new(isometry * circumcenter, radius)332}333}334}335336impl Bounded2d for Rectangle {337fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {338let isometry = isometry.into();339340// Compute the AABB of the rotated rectangle by transforming the half-extents341// by an absolute rotation matrix.342let (sin, cos) = isometry.rotation.sin_cos();343let abs_rot_mat =344Mat2::from_cols_array(&[ops::abs(cos), ops::abs(sin), ops::abs(sin), ops::abs(cos)]);345let half_size = abs_rot_mat * self.half_size;346347Aabb2d::new(isometry.translation, half_size)348}349350fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {351let isometry = isometry.into();352let radius = self.half_size.length();353BoundingCircle::new(isometry.translation, radius)354}355}356357#[cfg(feature = "alloc")]358impl Bounded2d for Polygon {359fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {360Aabb2d::from_point_cloud(isometry, &self.vertices)361}362363fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {364BoundingCircle::from_point_cloud(isometry, &self.vertices)365}366}367368#[cfg(feature = "alloc")]369impl Bounded2d for ConvexPolygon {370fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {371Aabb2d::from_point_cloud(isometry, self.vertices())372}373374fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {375BoundingCircle::from_point_cloud(isometry, self.vertices())376}377}378379impl Bounded2d for RegularPolygon {380fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {381let isometry = isometry.into();382383let mut min = Vec2::ZERO;384let mut max = Vec2::ZERO;385386for vertex in self.vertices(isometry.rotation.as_radians()) {387min = min.min(vertex);388max = max.max(vertex);389}390391Aabb2d {392min: min + isometry.translation,393max: max + isometry.translation,394}395}396397fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {398let isometry = isometry.into();399BoundingCircle::new(isometry.translation, self.circumcircle.radius)400}401}402403impl Bounded2d for Capsule2d {404fn aabb_2d(&self, isometry: impl Into<Isometry2d>) -> Aabb2d {405let isometry = isometry.into();406407// Get the line segment between the semicircles of the rotated capsule408let segment = Segment2d::from_direction_and_length(409isometry.rotation * Dir2::Y,410self.half_length * 2.,411);412let (a, b) = (segment.point1(), segment.point2());413414// Expand the line segment by the capsule radius to get the capsule half-extents415let min = a.min(b) - Vec2::splat(self.radius);416let max = a.max(b) + Vec2::splat(self.radius);417418Aabb2d {419min: min + isometry.translation,420max: max + isometry.translation,421}422}423424fn bounding_circle(&self, isometry: impl Into<Isometry2d>) -> BoundingCircle {425let isometry = isometry.into();426BoundingCircle::new(isometry.translation, self.radius + self.half_length)427}428}429430#[cfg(test)]431#[expect(clippy::print_stdout, reason = "Allowed in tests.")]432mod tests {433use core::f32::consts::{FRAC_PI_2, FRAC_PI_3, FRAC_PI_4, FRAC_PI_6, TAU};434use std::println;435436use approx::assert_abs_diff_eq;437use glam::Vec2;438439use crate::{440bounding::Bounded2d,441ops::{self, FloatPow},442primitives::{443Annulus, Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d,444Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Rhombus, Segment2d,445Triangle2d,446},447Dir2, Isometry2d, Rot2,448};449450#[test]451fn circle() {452let circle = Circle { radius: 1.0 };453let translation = Vec2::new(2.0, 1.0);454let isometry = Isometry2d::from_translation(translation);455456let aabb = circle.aabb_2d(isometry);457assert_eq!(aabb.min, Vec2::new(1.0, 0.0));458assert_eq!(aabb.max, Vec2::new(3.0, 2.0));459460let bounding_circle = circle.bounding_circle(isometry);461assert_eq!(bounding_circle.center, translation);462assert_eq!(bounding_circle.radius(), 1.0);463}464465#[test]466// Arcs and circular segments have the same bounding shapes so they share test cases.467fn arc_and_segment() {468struct TestCase {469name: &'static str,470arc: Arc2d,471translation: Vec2,472rotation: f32,473aabb_min: Vec2,474aabb_max: Vec2,475bounding_circle_center: Vec2,476bounding_circle_radius: f32,477}478479impl TestCase {480fn isometry(&self) -> Isometry2d {481Isometry2d::new(self.translation, self.rotation.into())482}483}484485// The apothem of an arc covering 1/6th of a circle.486let apothem = ops::sqrt(3.0) / 2.0;487let tests = [488// Test case: a basic minor arc489TestCase {490name: "1/6th circle untransformed",491arc: Arc2d::from_radians(1.0, FRAC_PI_3),492translation: Vec2::ZERO,493rotation: 0.0,494aabb_min: Vec2::new(-0.5, apothem),495aabb_max: Vec2::new(0.5, 1.0),496bounding_circle_center: Vec2::new(0.0, apothem),497bounding_circle_radius: 0.5,498},499// Test case: a smaller arc, verifying that radius scaling works500TestCase {501name: "1/6th circle with radius 0.5",502arc: Arc2d::from_radians(0.5, FRAC_PI_3),503translation: Vec2::ZERO,504rotation: 0.0,505aabb_min: Vec2::new(-0.25, apothem / 2.0),506aabb_max: Vec2::new(0.25, 0.5),507bounding_circle_center: Vec2::new(0.0, apothem / 2.0),508bounding_circle_radius: 0.25,509},510// Test case: a larger arc, verifying that radius scaling works511TestCase {512name: "1/6th circle with radius 2.0",513arc: Arc2d::from_radians(2.0, FRAC_PI_3),514translation: Vec2::ZERO,515rotation: 0.0,516aabb_min: Vec2::new(-1.0, 2.0 * apothem),517aabb_max: Vec2::new(1.0, 2.0),518bounding_circle_center: Vec2::new(0.0, 2.0 * apothem),519bounding_circle_radius: 1.0,520},521// Test case: translation of a minor arc522TestCase {523name: "1/6th circle translated",524arc: Arc2d::from_radians(1.0, FRAC_PI_3),525translation: Vec2::new(2.0, 3.0),526rotation: 0.0,527aabb_min: Vec2::new(1.5, 3.0 + apothem),528aabb_max: Vec2::new(2.5, 4.0),529bounding_circle_center: Vec2::new(2.0, 3.0 + apothem),530bounding_circle_radius: 0.5,531},532// Test case: rotation of a minor arc533TestCase {534name: "1/6th circle rotated",535arc: Arc2d::from_radians(1.0, FRAC_PI_3),536translation: Vec2::ZERO,537// Rotate left by 1/12 of a circle, so the right endpoint is on the y-axis.538rotation: FRAC_PI_6,539aabb_min: Vec2::new(-apothem, 0.5),540aabb_max: Vec2::new(0.0, 1.0),541// The exact coordinates here are not obvious, but can be computed by constructing542// an altitude from the midpoint of the chord to the y-axis and using the right triangle543// similarity theorem.544bounding_circle_center: Vec2::new(-apothem / 2.0, apothem.squared()),545bounding_circle_radius: 0.5,546},547// Test case: handling of axis-aligned extrema548TestCase {549name: "1/4er circle rotated to be axis-aligned",550arc: Arc2d::from_radians(1.0, FRAC_PI_2),551translation: Vec2::ZERO,552// Rotate right by 1/8 of a circle, so the right endpoint is on the x-axis and the left endpoint is on the y-axis.553rotation: -FRAC_PI_4,554aabb_min: Vec2::ZERO,555aabb_max: Vec2::splat(1.0),556bounding_circle_center: Vec2::splat(0.5),557bounding_circle_radius: ops::sqrt(2.0) / 2.0,558},559// Test case: a basic major arc560TestCase {561name: "5/6th circle untransformed",562arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),563translation: Vec2::ZERO,564rotation: 0.0,565aabb_min: Vec2::new(-1.0, -apothem),566aabb_max: Vec2::new(1.0, 1.0),567bounding_circle_center: Vec2::ZERO,568bounding_circle_radius: 1.0,569},570// Test case: a translated major arc571TestCase {572name: "5/6th circle translated",573arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),574translation: Vec2::new(2.0, 3.0),575rotation: 0.0,576aabb_min: Vec2::new(1.0, 3.0 - apothem),577aabb_max: Vec2::new(3.0, 4.0),578bounding_circle_center: Vec2::new(2.0, 3.0),579bounding_circle_radius: 1.0,580},581// Test case: a rotated major arc, with inverted left/right angles582TestCase {583name: "5/6th circle rotated",584arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),585translation: Vec2::ZERO,586// Rotate left by 1/12 of a circle, so the left endpoint is on the y-axis.587rotation: FRAC_PI_6,588aabb_min: Vec2::new(-1.0, -1.0),589aabb_max: Vec2::new(1.0, 1.0),590bounding_circle_center: Vec2::ZERO,591bounding_circle_radius: 1.0,592},593];594595for test in tests {596#[cfg(feature = "std")]597println!("subtest case: {}", test.name);598let segment: CircularSegment = test.arc.into();599600let arc_aabb = test.arc.aabb_2d(test.isometry());601assert_abs_diff_eq!(test.aabb_min, arc_aabb.min);602assert_abs_diff_eq!(test.aabb_max, arc_aabb.max);603let segment_aabb = segment.aabb_2d(test.isometry());604assert_abs_diff_eq!(test.aabb_min, segment_aabb.min);605assert_abs_diff_eq!(test.aabb_max, segment_aabb.max);606607let arc_bounding_circle = test.arc.bounding_circle(test.isometry());608assert_abs_diff_eq!(test.bounding_circle_center, arc_bounding_circle.center);609assert_abs_diff_eq!(test.bounding_circle_radius, arc_bounding_circle.radius());610let segment_bounding_circle = segment.bounding_circle(test.isometry());611assert_abs_diff_eq!(test.bounding_circle_center, segment_bounding_circle.center);612assert_abs_diff_eq!(613test.bounding_circle_radius,614segment_bounding_circle.radius()615);616}617}618619#[test]620fn circular_sector() {621struct TestCase {622name: &'static str,623arc: Arc2d,624translation: Vec2,625rotation: f32,626aabb_min: Vec2,627aabb_max: Vec2,628bounding_circle_center: Vec2,629bounding_circle_radius: f32,630}631632impl TestCase {633fn isometry(&self) -> Isometry2d {634Isometry2d::new(self.translation, self.rotation.into())635}636}637638// The apothem of an arc covering 1/6th of a circle.639let apothem = ops::sqrt(3.0) / 2.0;640let inv_sqrt_3 = ops::sqrt(3.0).recip();641let tests = [642// Test case: A sector whose arc is minor, but whose bounding circle is not the circumcircle of the endpoints and center643TestCase {644name: "1/3rd circle",645arc: Arc2d::from_radians(1.0, TAU / 3.0),646translation: Vec2::ZERO,647rotation: 0.0,648aabb_min: Vec2::new(-apothem, 0.0),649aabb_max: Vec2::new(apothem, 1.0),650bounding_circle_center: Vec2::new(0.0, 0.5),651bounding_circle_radius: apothem,652},653// The remaining test cases are selected as for arc_and_segment.654TestCase {655name: "1/6th circle untransformed",656arc: Arc2d::from_radians(1.0, FRAC_PI_3),657translation: Vec2::ZERO,658rotation: 0.0,659aabb_min: Vec2::new(-0.5, 0.0),660aabb_max: Vec2::new(0.5, 1.0),661// The bounding circle is a circumcircle of an equilateral triangle with side length 1.662// The distance from the corner to the center of such a triangle is 1/sqrt(3).663bounding_circle_center: Vec2::new(0.0, inv_sqrt_3),664bounding_circle_radius: inv_sqrt_3,665},666TestCase {667name: "1/6th circle with radius 0.5",668arc: Arc2d::from_radians(0.5, FRAC_PI_3),669translation: Vec2::ZERO,670rotation: 0.0,671aabb_min: Vec2::new(-0.25, 0.0),672aabb_max: Vec2::new(0.25, 0.5),673bounding_circle_center: Vec2::new(0.0, inv_sqrt_3 / 2.0),674bounding_circle_radius: inv_sqrt_3 / 2.0,675},676TestCase {677name: "1/6th circle with radius 2.0",678arc: Arc2d::from_radians(2.0, FRAC_PI_3),679translation: Vec2::ZERO,680rotation: 0.0,681aabb_min: Vec2::new(-1.0, 0.0),682aabb_max: Vec2::new(1.0, 2.0),683bounding_circle_center: Vec2::new(0.0, 2.0 * inv_sqrt_3),684bounding_circle_radius: 2.0 * inv_sqrt_3,685},686TestCase {687name: "1/6th circle translated",688arc: Arc2d::from_radians(1.0, FRAC_PI_3),689translation: Vec2::new(2.0, 3.0),690rotation: 0.0,691aabb_min: Vec2::new(1.5, 3.0),692aabb_max: Vec2::new(2.5, 4.0),693bounding_circle_center: Vec2::new(2.0, 3.0 + inv_sqrt_3),694bounding_circle_radius: inv_sqrt_3,695},696TestCase {697name: "1/6th circle rotated",698arc: Arc2d::from_radians(1.0, FRAC_PI_3),699translation: Vec2::ZERO,700// Rotate left by 1/12 of a circle, so the right endpoint is on the y-axis.701rotation: FRAC_PI_6,702aabb_min: Vec2::new(-apothem, 0.0),703aabb_max: Vec2::new(0.0, 1.0),704// The x-coordinate is now the inradius of the equilateral triangle, which is sqrt(3)/2.705bounding_circle_center: Vec2::new(-inv_sqrt_3 / 2.0, 0.5),706bounding_circle_radius: inv_sqrt_3,707},708TestCase {709name: "1/4er circle rotated to be axis-aligned",710arc: Arc2d::from_radians(1.0, FRAC_PI_2),711translation: Vec2::ZERO,712// Rotate right by 1/8 of a circle, so the right endpoint is on the x-axis and the left endpoint is on the y-axis.713rotation: -FRAC_PI_4,714aabb_min: Vec2::ZERO,715aabb_max: Vec2::splat(1.0),716bounding_circle_center: Vec2::splat(0.5),717bounding_circle_radius: ops::sqrt(2.0) / 2.0,718},719TestCase {720name: "5/6th circle untransformed",721arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),722translation: Vec2::ZERO,723rotation: 0.0,724aabb_min: Vec2::new(-1.0, -apothem),725aabb_max: Vec2::new(1.0, 1.0),726bounding_circle_center: Vec2::ZERO,727bounding_circle_radius: 1.0,728},729TestCase {730name: "5/6th circle translated",731arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),732translation: Vec2::new(2.0, 3.0),733rotation: 0.0,734aabb_min: Vec2::new(1.0, 3.0 - apothem),735aabb_max: Vec2::new(3.0, 4.0),736bounding_circle_center: Vec2::new(2.0, 3.0),737bounding_circle_radius: 1.0,738},739TestCase {740name: "5/6th circle rotated",741arc: Arc2d::from_radians(1.0, 5.0 * FRAC_PI_3),742translation: Vec2::ZERO,743// Rotate left by 1/12 of a circle, so the left endpoint is on the y-axis.744rotation: FRAC_PI_6,745aabb_min: Vec2::new(-1.0, -1.0),746aabb_max: Vec2::new(1.0, 1.0),747bounding_circle_center: Vec2::ZERO,748bounding_circle_radius: 1.0,749},750];751752for test in tests {753#[cfg(feature = "std")]754println!("subtest case: {}", test.name);755let sector: CircularSector = test.arc.into();756757let aabb = sector.aabb_2d(test.isometry());758assert_abs_diff_eq!(test.aabb_min, aabb.min);759assert_abs_diff_eq!(test.aabb_max, aabb.max);760761let bounding_circle = sector.bounding_circle(test.isometry());762assert_abs_diff_eq!(test.bounding_circle_center, bounding_circle.center);763assert_abs_diff_eq!(test.bounding_circle_radius, bounding_circle.radius());764}765}766767#[test]768fn ellipse() {769let ellipse = Ellipse::new(1.0, 0.5);770let translation = Vec2::new(2.0, 1.0);771let isometry = Isometry2d::from_translation(translation);772773let aabb = ellipse.aabb_2d(isometry);774assert_eq!(aabb.min, Vec2::new(1.0, 0.5));775assert_eq!(aabb.max, Vec2::new(3.0, 1.5));776777let bounding_circle = ellipse.bounding_circle(isometry);778assert_eq!(bounding_circle.center, translation);779assert_eq!(bounding_circle.radius(), 1.0);780}781782#[test]783fn annulus() {784let annulus = Annulus::new(1.0, 2.0);785let translation = Vec2::new(2.0, 1.0);786let rotation = Rot2::radians(1.0);787let isometry = Isometry2d::new(translation, rotation);788789let aabb = annulus.aabb_2d(isometry);790assert_eq!(aabb.min, Vec2::new(0.0, -1.0));791assert_eq!(aabb.max, Vec2::new(4.0, 3.0));792793let bounding_circle = annulus.bounding_circle(isometry);794assert_eq!(bounding_circle.center, translation);795assert_eq!(bounding_circle.radius(), 2.0);796}797798#[test]799fn rhombus() {800let rhombus = Rhombus::new(2.0, 1.0);801let translation = Vec2::new(2.0, 1.0);802let rotation = Rot2::radians(FRAC_PI_4);803let isometry = Isometry2d::new(translation, rotation);804805let aabb = rhombus.aabb_2d(isometry);806assert_eq!(aabb.min, Vec2::new(1.2928932, 0.29289323));807assert_eq!(aabb.max, Vec2::new(2.7071068, 1.7071068));808809let bounding_circle = rhombus.bounding_circle(isometry);810assert_eq!(bounding_circle.center, translation);811assert_eq!(bounding_circle.radius(), 1.0);812813let rhombus = Rhombus::new(0.0, 0.0);814let translation = Vec2::new(0.0, 0.0);815let isometry = Isometry2d::new(translation, rotation);816817let aabb = rhombus.aabb_2d(isometry);818assert_eq!(aabb.min, Vec2::new(0.0, 0.0));819assert_eq!(aabb.max, Vec2::new(0.0, 0.0));820821let bounding_circle = rhombus.bounding_circle(isometry);822assert_eq!(bounding_circle.center, translation);823assert_eq!(bounding_circle.radius(), 0.0);824}825826#[test]827fn plane() {828let translation = Vec2::new(2.0, 1.0);829let isometry = Isometry2d::from_translation(translation);830831let aabb1 = Plane2d::new(Vec2::X).aabb_2d(isometry);832assert_eq!(aabb1.min, Vec2::new(2.0, -f32::MAX / 2.0));833assert_eq!(aabb1.max, Vec2::new(2.0, f32::MAX / 2.0));834835let aabb2 = Plane2d::new(Vec2::Y).aabb_2d(isometry);836assert_eq!(aabb2.min, Vec2::new(-f32::MAX / 2.0, 1.0));837assert_eq!(aabb2.max, Vec2::new(f32::MAX / 2.0, 1.0));838839let aabb3 = Plane2d::new(Vec2::ONE).aabb_2d(isometry);840assert_eq!(aabb3.min, Vec2::new(-f32::MAX / 2.0, -f32::MAX / 2.0));841assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));842843let bounding_circle = Plane2d::new(Vec2::Y).bounding_circle(isometry);844assert_eq!(bounding_circle.center, translation);845assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);846}847848#[test]849fn line() {850let translation = Vec2::new(2.0, 1.0);851let isometry = Isometry2d::from_translation(translation);852853let aabb1 = Line2d { direction: Dir2::Y }.aabb_2d(isometry);854assert_eq!(aabb1.min, Vec2::new(2.0, -f32::MAX / 2.0));855assert_eq!(aabb1.max, Vec2::new(2.0, f32::MAX / 2.0));856857let aabb2 = Line2d { direction: Dir2::X }.aabb_2d(isometry);858assert_eq!(aabb2.min, Vec2::new(-f32::MAX / 2.0, 1.0));859assert_eq!(aabb2.max, Vec2::new(f32::MAX / 2.0, 1.0));860861let aabb3 = Line2d {862direction: Dir2::from_xy(1.0, 1.0).unwrap(),863}864.aabb_2d(isometry);865assert_eq!(aabb3.min, Vec2::new(-f32::MAX / 2.0, -f32::MAX / 2.0));866assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));867868let bounding_circle = Line2d { direction: Dir2::Y }.bounding_circle(isometry);869assert_eq!(bounding_circle.center, translation);870assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);871}872873#[test]874fn segment() {875let segment = Segment2d::new(Vec2::new(-1.0, -0.5), Vec2::new(1.0, 0.5));876let translation = Vec2::new(2.0, 1.0);877let isometry = Isometry2d::from_translation(translation);878879let aabb = segment.aabb_2d(isometry);880assert_eq!(aabb.min, Vec2::new(1.0, 0.5));881assert_eq!(aabb.max, Vec2::new(3.0, 1.5));882883let bounding_circle = segment.bounding_circle(isometry);884assert_eq!(bounding_circle.center, translation);885assert_eq!(bounding_circle.radius(), ops::hypot(1.0, 0.5));886}887888#[test]889fn polyline() {890let polyline = Polyline2d::new([891Vec2::ONE,892Vec2::new(-1.0, 1.0),893Vec2::NEG_ONE,894Vec2::new(1.0, -1.0),895]);896let translation = Vec2::new(2.0, 1.0);897let isometry = Isometry2d::from_translation(translation);898899let aabb = polyline.aabb_2d(isometry);900assert_eq!(aabb.min, Vec2::new(1.0, 0.0));901assert_eq!(aabb.max, Vec2::new(3.0, 2.0));902903let bounding_circle = polyline.bounding_circle(isometry);904assert_eq!(bounding_circle.center, translation);905assert_eq!(bounding_circle.radius(), core::f32::consts::SQRT_2);906}907908#[test]909fn acute_triangle() {910let acute_triangle =911Triangle2d::new(Vec2::new(0.0, 1.0), Vec2::NEG_ONE, Vec2::new(1.0, -1.0));912let translation = Vec2::new(2.0, 1.0);913let isometry = Isometry2d::from_translation(translation);914915let aabb = acute_triangle.aabb_2d(isometry);916assert_eq!(aabb.min, Vec2::new(1.0, 0.0));917assert_eq!(aabb.max, Vec2::new(3.0, 2.0));918919// For acute triangles, the center is the circumcenter920let (Circle { radius }, circumcenter) = acute_triangle.circumcircle();921let bounding_circle = acute_triangle.bounding_circle(isometry);922assert_eq!(bounding_circle.center, circumcenter + translation);923assert_eq!(bounding_circle.radius(), radius);924}925926#[test]927fn obtuse_triangle() {928let obtuse_triangle = Triangle2d::new(929Vec2::new(0.0, 1.0),930Vec2::new(-10.0, -1.0),931Vec2::new(10.0, -1.0),932);933let translation = Vec2::new(2.0, 1.0);934let isometry = Isometry2d::from_translation(translation);935936let aabb = obtuse_triangle.aabb_2d(isometry);937assert_eq!(aabb.min, Vec2::new(-8.0, 0.0));938assert_eq!(aabb.max, Vec2::new(12.0, 2.0));939940// For obtuse and right triangles, the center is the midpoint of the longest side (diameter of bounding circle)941let bounding_circle = obtuse_triangle.bounding_circle(isometry);942assert_eq!(bounding_circle.center, translation - Vec2::Y);943assert_eq!(bounding_circle.radius(), 10.0);944}945946#[test]947fn rectangle() {948let rectangle = Rectangle::new(2.0, 1.0);949let translation = Vec2::new(2.0, 1.0);950951let aabb = rectangle.aabb_2d(Isometry2d::new(translation, Rot2::radians(FRAC_PI_4)));952let expected_half_size = Vec2::splat(1.0606601);953assert_eq!(aabb.min, translation - expected_half_size);954assert_eq!(aabb.max, translation + expected_half_size);955956let bounding_circle = rectangle.bounding_circle(Isometry2d::from_translation(translation));957assert_eq!(bounding_circle.center, translation);958assert_eq!(bounding_circle.radius(), ops::hypot(1.0, 0.5));959}960961#[test]962fn polygon() {963let polygon = Polygon::new([964Vec2::ONE,965Vec2::new(-1.0, 1.0),966Vec2::NEG_ONE,967Vec2::new(1.0, -1.0),968]);969let translation = Vec2::new(2.0, 1.0);970let isometry = Isometry2d::from_translation(translation);971972let aabb = polygon.aabb_2d(isometry);973assert_eq!(aabb.min, Vec2::new(1.0, 0.0));974assert_eq!(aabb.max, Vec2::new(3.0, 2.0));975976let bounding_circle = polygon.bounding_circle(isometry);977assert_eq!(bounding_circle.center, translation);978assert_eq!(bounding_circle.radius(), core::f32::consts::SQRT_2);979}980981#[test]982fn regular_polygon() {983let regular_polygon = RegularPolygon::new(1.0, 5);984let translation = Vec2::new(2.0, 1.0);985let isometry = Isometry2d::from_translation(translation);986987let aabb = regular_polygon.aabb_2d(isometry);988assert!((aabb.min - (translation - Vec2::new(0.9510565, 0.8090169))).length() < 1e-6);989assert!((aabb.max - (translation + Vec2::new(0.9510565, 1.0))).length() < 1e-6);990991let bounding_circle = regular_polygon.bounding_circle(isometry);992assert_eq!(bounding_circle.center, translation);993assert_eq!(bounding_circle.radius(), 1.0);994}995996#[test]997fn capsule() {998let capsule = Capsule2d::new(0.5, 2.0);999let translation = Vec2::new(2.0, 1.0);1000let isometry = Isometry2d::from_translation(translation);10011002let aabb = capsule.aabb_2d(isometry);1003assert_eq!(aabb.min, translation - Vec2::new(0.5, 1.5));1004assert_eq!(aabb.max, translation + Vec2::new(0.5, 1.5));10051006let bounding_circle = capsule.bounding_circle(isometry);1007assert_eq!(bounding_circle.center, translation);1008assert_eq!(bounding_circle.radius(), 1.5);1009}1010}101110121013