Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
spectria-limina committed Mar 9, 2024
1 parent 31b2be3 commit 9353ad0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,13 @@ category = "2D Rendering"
wasm = true

[[example]]
name = "mesh2d_circular"
path = "examples/2d/mesh2d_circular.rs"
name = "mesh2d_arcs"
path = "examples/2d/mesh2d_arcs.rs"
doc-scrape-examples = true

[package.metadata.example.mesh2d_circular]
name = "Circular 2D Meshes"
description = "Demonstrates UV-mapping of circular primitives"
name = "Arc 2D Meshes"
description = "Demonstrates UV-mapping of the circular segment and sector primitives"
category = "2D Rendering"
wasm = true

Expand Down
24 changes: 11 additions & 13 deletions crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! Contains [`Bounded2d`] implementations for [geometric primitives](crate::primitives).

use std::f32::consts::PI;
use std::f32::consts::{FRAC_PI_2, PI, TAU};

use glam::{Mat2, Vec2};
use smallvec::SmallVec;

use crate::{
primitives::{
BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment,
Ellipse, Line2d, Plane2d, Polygon,
Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
Arc2d, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, CircularSector, CircularSegment,
Ellipse, Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d,
Triangle2d,
},
Dir2, Mat2, Vec2,
};
Expand Down Expand Up @@ -40,11 +39,11 @@ fn arc_bounding_points(arc: Arc2d, rotation: f32) -> SmallVec<[Vec2; 7]> {
// The half-angles are measured from a starting point of π/2, being the angle of Vec2::Y.
// Compute the normalized angles of the endpoints with the rotation taken into account, and then
// check if we are looking for an angle that is between or outside them.
let left_angle = (PI / 2.0 + arc.half_angle + rotation).rem_euclid(2.0 * PI);
let right_angle = (PI / 2.0 - arc.half_angle + rotation).rem_euclid(2.0 * PI);
let left_angle = (FRAC_PI_2 + arc.half_angle + rotation).rem_euclid(TAU);
let right_angle = (FRAC_PI_2 - arc.half_angle + rotation).rem_euclid(TAU);
let inverted = left_angle < right_angle;
for extremum in [Vec2::X, Vec2::Y, Vec2::NEG_X, Vec2::NEG_Y] {
let angle = extremum.to_angle().rem_euclid(2.0 * PI);
let angle = extremum.to_angle().rem_euclid(TAU);
// If inverted = true, then right_angle > left_angle, so we are looking for an angle that is not between them.
// There's a chance that this condition fails due to rounding error, if the endpoint angle is juuuust shy of the axis.
// But in that case, the endpoint itself is within rounding error of the axis and will define the bounds just fine.
Expand Down Expand Up @@ -366,16 +365,16 @@ impl Bounded2d for Capsule2d {

#[cfg(test)]
mod tests {
use std::f32::consts::PI;
use std::f32::consts::{PI, TAU};

use approx::assert_abs_diff_eq;
use glam::Vec2;

use crate::{
bounding::Bounded2d,
primitives::{
Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Direction2d, Ellipse,
Line2d, Plane2d, Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
Arc2d, Capsule2d, Circle, CircularSector, CircularSegment, Ellipse, Line2d, Plane2d,
Polygon, Polyline2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
},
Dir2,
};
Expand Down Expand Up @@ -561,7 +560,7 @@ mod tests {
// Test case: An sector whose arc is minor, but whose bounding circle is not the circumcircle of the endpoints and center
TestCase {
name: "1/3rd circle",
arc: Arc2d::from_radians(1.0, 2.0 * PI / 3.0),
arc: Arc2d::from_radians(1.0, TAU / 3.0),
translation: Vec2::ZERO,
rotation: 0.0,
aabb_min: Vec2::new(-apothem, 0.0),
Expand Down Expand Up @@ -713,7 +712,6 @@ mod tests {
assert_eq!(aabb3.max, Vec2::new(f32::MAX / 2.0, f32::MAX / 2.0));

let bounding_circle = Plane2d::new(Vec2::Y).bounding_circle(translation, 0.0);
dbg!(bounding_circle);
assert_eq!(bounding_circle.center, translation);
assert_eq!(bounding_circle.radius(), f32::MAX / 2.0);
}
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_render/src/mesh/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ impl From<CircleMeshBuilder> for Mesh {
}

/// Specifies how to generate UV-mappings for the [`CircularSector`] and [`CircularSegment`] shapes.
///
/// Currently the only variant is `Mask`, which is good for showing a portion of a texture that includes
/// the entire circle, particularly the same texture will be displayed with different fractions of a
/// complete circle.
///
/// It's expected that more will be added in the future, such as a variant that causes the texture to be
/// scaled to fit the bounding box of the shape, which would be good for packed textures only including the
/// portion of the circle that is needed to display.
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum CircularMeshUvMode {
/// Treats the shape as a mask over a circle of equal size and radius,
/// with the center of the circle at the center of the texture.
Expand Down
11 changes: 4 additions & 7 deletions examples/2d/mesh2d_circular.rs → examples/2d/mesh2d_arcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::f32::consts::PI;

use bevy::{
color::palettes::css::{BLUE, DARK_SLATE_GREY, RED},
math::bounding::{Bounded2d, BoundingVolume},
prelude::*,
render::mesh::{CircularMeshUvMode, CircularSectorMeshBuilder, CircularSegmentMeshBuilder},
Expand Down Expand Up @@ -37,7 +38,7 @@ fn setup(

commands.spawn(Camera2dBundle {
camera: Camera {
clear_color: ClearColorConfig::Custom(Color::DARK_GRAY),
clear_color: ClearColorConfig::Custom(DARK_SLATE_GREY.into()),
..default()
},
..default()
Expand Down Expand Up @@ -115,13 +116,9 @@ fn draw_bounds<Shape: Bounded2d + Send + Sync + 'static>(
let rotation = rotation.to_euler(EulerRot::XYZ).2;

let aabb = shape.0.aabb_2d(translation, rotation);
gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, Color::RED);
gizmos.rect_2d(aabb.center(), 0.0, aabb.half_size() * 2.0, RED);

let bounding_circle = shape.0.bounding_circle(translation, rotation);
gizmos.circle_2d(
bounding_circle.center,
bounding_circle.radius(),
Color::BLUE,
);
gizmos.circle_2d(bounding_circle.center, bounding_circle.radius(), BLUE);
}
}

0 comments on commit 9353ad0

Please sign in to comment.