Files
egui/crates/epaint/src/corner_radius.rs
T
Oscar GustafssonandEmil Ernerfeldt 2078a31e09 Replace constant divisions with constant multiplications (#8412)
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

* [x] I have followed the instructions in the PR template

Compiler optimization will replace these later on, but when building
without optimization they will (most often) stay divisions and divisions
are clearly slower than multiplications.

Note that the `/ 2.5` replacement is not bit-exact (since 0.4 cannot be
represented exactly, but 2.5 can), but my guess is that the
approximation is good enough. The others should be bit-exact.

(Division by 2 for unsigned numbers will be replaced to >>1 earlier in
the compile chain, so no point in doing the same there.)

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2026-10-05 12:44:07 +00:00

251 lines
6.5 KiB
Rust

/// How rounded the corners of things should be.
///
/// This specific the _corner radius_ of the underlying geometric shape (e.g. rectangle).
/// If there is a stroke, then the stroke will have an inner and outer corner radius
/// which will depends on its width and [`crate::StrokeKind`].
///
/// The rounding uses `u8` to save space,
/// so the amount of rounding is limited to integers in the range `[0, 255]`.
///
/// For calculations, you may want to use [`crate::CornerRadiusF32`] instead, which uses `f32`.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct CornerRadius {
/// Radius of the rounding of the North-West (left top) corner.
pub nw: u8,
/// Radius of the rounding of the North-East (right top) corner.
pub ne: u8,
/// Radius of the rounding of the South-West (left bottom) corner.
pub sw: u8,
/// Radius of the rounding of the South-East (right bottom) corner.
pub se: u8,
}
impl Default for CornerRadius {
#[inline]
fn default() -> Self {
Self::ZERO
}
}
impl From<u8> for CornerRadius {
#[inline]
fn from(radius: u8) -> Self {
Self::same(radius)
}
}
impl From<f32> for CornerRadius {
#[inline]
fn from(radius: f32) -> Self {
Self::same(radius.round() as u8)
}
}
impl CornerRadius {
/// No rounding on any corner.
pub const ZERO: Self = Self {
nw: 0,
ne: 0,
sw: 0,
se: 0,
};
/// Same rounding on all four corners.
#[inline]
pub const fn same(radius: u8) -> Self {
Self {
nw: radius,
ne: radius,
sw: radius,
se: radius,
}
}
/// Do all corners have the same rounding?
#[inline]
pub fn is_same(self) -> bool {
self.nw == self.ne && self.nw == self.sw && self.nw == self.se
}
/// Make sure each corner has a rounding of at least this.
#[inline]
pub fn at_least(self, min: u8) -> Self {
Self {
nw: self.nw.max(min),
ne: self.ne.max(min),
sw: self.sw.max(min),
se: self.se.max(min),
}
}
/// Make sure each corner has a rounding of at most this.
#[inline]
pub fn at_most(self, max: u8) -> Self {
Self {
nw: self.nw.min(max),
ne: self.ne.min(max),
sw: self.sw.min(max),
se: self.se.min(max),
}
}
/// Average rounding of the corners.
pub fn average(&self) -> f32 {
(self.nw as f32 + self.ne as f32 + self.sw as f32 + self.se as f32) * 0.25
}
}
impl core::ops::Add for CornerRadius {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Self {
nw: self.nw.saturating_add(rhs.nw),
ne: self.ne.saturating_add(rhs.ne),
sw: self.sw.saturating_add(rhs.sw),
se: self.se.saturating_add(rhs.se),
}
}
}
impl core::ops::Add<u8> for CornerRadius {
type Output = Self;
#[inline]
fn add(self, rhs: u8) -> Self {
Self {
nw: self.nw.saturating_add(rhs),
ne: self.ne.saturating_add(rhs),
sw: self.sw.saturating_add(rhs),
se: self.se.saturating_add(rhs),
}
}
}
impl core::ops::AddAssign for CornerRadius {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = Self {
nw: self.nw.saturating_add(rhs.nw),
ne: self.ne.saturating_add(rhs.ne),
sw: self.sw.saturating_add(rhs.sw),
se: self.se.saturating_add(rhs.se),
};
}
}
impl core::ops::AddAssign<u8> for CornerRadius {
#[inline]
fn add_assign(&mut self, rhs: u8) {
*self = Self {
nw: self.nw.saturating_add(rhs),
ne: self.ne.saturating_add(rhs),
sw: self.sw.saturating_add(rhs),
se: self.se.saturating_add(rhs),
};
}
}
impl core::ops::Sub for CornerRadius {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self {
nw: self.nw.saturating_sub(rhs.nw),
ne: self.ne.saturating_sub(rhs.ne),
sw: self.sw.saturating_sub(rhs.sw),
se: self.se.saturating_sub(rhs.se),
}
}
}
impl core::ops::Sub<u8> for CornerRadius {
type Output = Self;
#[inline]
fn sub(self, rhs: u8) -> Self {
Self {
nw: self.nw.saturating_sub(rhs),
ne: self.ne.saturating_sub(rhs),
sw: self.sw.saturating_sub(rhs),
se: self.se.saturating_sub(rhs),
}
}
}
impl core::ops::SubAssign for CornerRadius {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = Self {
nw: self.nw.saturating_sub(rhs.nw),
ne: self.ne.saturating_sub(rhs.ne),
sw: self.sw.saturating_sub(rhs.sw),
se: self.se.saturating_sub(rhs.se),
};
}
}
impl core::ops::SubAssign<u8> for CornerRadius {
#[inline]
fn sub_assign(&mut self, rhs: u8) {
*self = Self {
nw: self.nw.saturating_sub(rhs),
ne: self.ne.saturating_sub(rhs),
sw: self.sw.saturating_sub(rhs),
se: self.se.saturating_sub(rhs),
};
}
}
impl core::ops::Div<f32> for CornerRadius {
type Output = Self;
#[inline]
fn div(self, rhs: f32) -> Self {
Self {
nw: (self.nw as f32 / rhs) as u8,
ne: (self.ne as f32 / rhs) as u8,
sw: (self.sw as f32 / rhs) as u8,
se: (self.se as f32 / rhs) as u8,
}
}
}
impl core::ops::DivAssign<f32> for CornerRadius {
#[inline]
fn div_assign(&mut self, rhs: f32) {
*self = Self {
nw: (self.nw as f32 / rhs) as u8,
ne: (self.ne as f32 / rhs) as u8,
sw: (self.sw as f32 / rhs) as u8,
se: (self.se as f32 / rhs) as u8,
};
}
}
impl core::ops::Mul<f32> for CornerRadius {
type Output = Self;
#[inline]
fn mul(self, rhs: f32) -> Self {
Self {
nw: (self.nw as f32 * rhs) as u8,
ne: (self.ne as f32 * rhs) as u8,
sw: (self.sw as f32 * rhs) as u8,
se: (self.se as f32 * rhs) as u8,
}
}
}
impl core::ops::MulAssign<f32> for CornerRadius {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
*self = Self {
nw: (self.nw as f32 * rhs) as u8,
ne: (self.ne as f32 * rhs) as u8,
sw: (self.sw as f32 * rhs) as u8,
se: (self.se as f32 * rhs) as u8,
};
}
}