Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epaint: Add EllipseShape #4122

Merged
merged 10 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/epaint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub use {
mesh::{Mesh, Mesh16, Vertex},
shadow::Shadow,
shape::{
CircleShape, PaintCallback, PaintCallbackInfo, PathShape, RectShape, Rounding, Shape,
TextShape,
CircleShape, EllipseShape, PaintCallback, PaintCallbackInfo, PathShape, RectShape,
Rounding, Shape, TextShape,
},
stats::PaintStats,
stroke::Stroke,
Expand Down
74 changes: 74 additions & 0 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub enum Shape {
/// Circle with optional outline and fill.
Circle(CircleShape),

/// Ellipse with optional outline and fill.
Ellipse(EllipseShape),

/// A line between two points.
LineSegment { points: [Pos2; 2], stroke: Stroke },

Expand Down Expand Up @@ -236,6 +239,16 @@ impl Shape {
Self::Circle(CircleShape::stroke(center, radius, stroke))
}

#[inline]
pub fn ellipse_filled(center: Pos2, radius: Vec2, fill_color: impl Into<Color32>) -> Self {
Self::Ellipse(EllipseShape::filled(center, radius, fill_color))
}

#[inline]
pub fn ellipse_stroke(center: Pos2, radius: Vec2, stroke: impl Into<Stroke>) -> Self {
Self::Ellipse(EllipseShape::stroke(center, radius, stroke))
}

#[inline]
pub fn rect_filled(
rect: Rect,
Expand Down Expand Up @@ -324,6 +337,7 @@ impl Shape {
rect
}
Self::Circle(circle_shape) => circle_shape.visual_bounding_rect(),
Self::Ellipse(ellipse_shape) => ellipse_shape.visual_bounding_rect(),
Self::LineSegment { points, stroke } => {
if stroke.is_empty() {
Rect::NOTHING
Expand Down Expand Up @@ -372,6 +386,11 @@ impl Shape {
circle_shape.radius *= transform.scaling;
circle_shape.stroke.width *= transform.scaling;
}
Self::Ellipse(ellipse_shape) => {
ellipse_shape.center = transform * ellipse_shape.center;
ellipse_shape.radius *= transform.scaling;
ellipse_shape.stroke.width *= transform.scaling;
}
Self::LineSegment { points, stroke } => {
for p in points {
*p = transform * *p;
Expand Down Expand Up @@ -480,6 +499,61 @@ impl From<CircleShape> for Shape {

// ----------------------------------------------------------------------------

/// How to paint an ellipse.
#[derive(Copy, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct EllipseShape {
pub center: Pos2,

/// Radius is the vector (a, b) where the width of the Ellipse is 2a and the height is 2b
pub radius: Vec2,
pub fill: Color32,
pub stroke: Stroke,
}

impl EllipseShape {
#[inline]
pub fn filled(center: Pos2, radius: Vec2, fill_color: impl Into<Color32>) -> Self {
Self {
center,
radius,
fill: fill_color.into(),
stroke: Default::default(),
}
}

#[inline]
pub fn stroke(center: Pos2, radius: Vec2, stroke: impl Into<Stroke>) -> Self {
Self {
center,
radius,
fill: Default::default(),
stroke: stroke.into(),
}
}

/// The visual bounding rectangle (includes stroke width)
pub fn visual_bounding_rect(&self) -> Rect {
if self.fill == Color32::TRANSPARENT && self.stroke.is_empty() {
Rect::NOTHING
} else {
Rect::from_center_size(
self.center,
self.radius * 2.0 + Vec2::splat(self.stroke.width),
)
}
}
}

impl From<EllipseShape> for Shape {
#[inline(always)]
fn from(shape: EllipseShape) -> Self {
Self::Ellipse(shape)
}
}

// ----------------------------------------------------------------------------

/// A path which can be stroked and/or filled (if closed).
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
Expand Down
6 changes: 6 additions & 0 deletions crates/epaint/src/shape_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub fn adjust_colors(shape: &mut Shape, adjust_color: &impl Fn(&mut Color32)) {
fill,
stroke,
})
| Shape::Ellipse(EllipseShape {
center: _,
radius: _,
fill,
stroke,
})
| Shape::Path(PathShape {
points: _,
closed: _,
Expand Down
1 change: 1 addition & 0 deletions crates/epaint/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl PaintStats {
}
Shape::Noop
| Shape::Circle { .. }
| Shape::Ellipse { .. }
| Shape::LineSegment { .. }
| Shape::Rect { .. }
| Shape::CubicBezier(_)
Expand Down
72 changes: 71 additions & 1 deletion crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,9 @@ impl Tessellator {
Shape::Circle(circle) => {
self.tessellate_circle(circle, out);
}
Shape::Ellipse(ellipse) => {
self.tessellate_ellipse(ellipse, out);
}
Shape::Mesh(mesh) => {
crate::profile_scope!("mesh");

Expand Down Expand Up @@ -1315,6 +1318,73 @@ impl Tessellator {
.stroke_closed(self.feathering, stroke, out);
}

/// Tessellate a single [`EllipseShape`] into a [`Mesh`].
///
/// * `shape`: the ellipse to tessellate.
/// * `out`: triangles are appended to this.
pub fn tessellate_ellipse(&mut self, shape: EllipseShape, out: &mut Mesh) {
let EllipseShape {
center,
radius,
fill,
stroke,
} = shape;

if radius.x <= 0.0 || radius.y <= 0.0 {
return;
}

if self.options.coarse_tessellation_culling
&& !self
.clip_rect
.expand2(radius + Vec2::splat(stroke.width))
.contains(center)
{
return;
}

// Get the max pixel radius
let max_radius = (radius.max_elem() * self.pixels_per_point) as u32;

// Ensure there is at least 8 points in each quarter of the ellipse
let num_points = u32::max(8, max_radius / 16);

// Create an ease ratio based the ellipses a and b
let ratio = ((radius.y / radius.x) / 2.0).clamp(0.0, 1.0);

// Generate points between the 0 to pi/2
let quarter: Vec<Vec2> = (1..num_points)
.map(|i| {
let percent = i as f32 / num_points as f32;

// Ease the percent value, concentrating points around tight bends
let eased = 2.0 * (percent - percent.powf(2.0)) * ratio + percent.powf(2.0);

// Scale the ease to the quarter
let t = eased * std::f32::consts::FRAC_PI_2;
Vec2::new(radius.x * f32::cos(t), radius.y * f32::sin(t))
})
.collect();

// Build the ellipse from the 4 known vertices filling arcs between
// them by mirroring the points between 0 and pi/2
let mut points = Vec::new();
points.push(center + Vec2::new(radius.x, 0.0));
points.extend(quarter.iter().map(|p| center + *p));
points.push(center + Vec2::new(0.0, radius.y));
points.extend(quarter.iter().rev().map(|p| center + Vec2::new(-p.x, p.y)));
points.push(center + Vec2::new(-radius.x, 0.0));
points.extend(quarter.iter().map(|p| center - *p));
points.push(center + Vec2::new(0.0, -radius.y));
points.extend(quarter.iter().rev().map(|p| center + Vec2::new(p.x, -p.y)));

self.scratchpad_path.clear();
self.scratchpad_path.add_line_loop(&points);
self.scratchpad_path.fill(self.feathering, fill, out);
self.scratchpad_path
.stroke_closed(self.feathering, stroke, out);
}

/// Tessellate a single [`Mesh`] into a [`Mesh`].
///
/// * `mesh`: the mesh to tessellate.
Expand Down Expand Up @@ -1776,7 +1846,7 @@ impl Tessellator {

Shape::Path(path_shape) => 32 < path_shape.points.len(),

Shape::QuadraticBezier(_) | Shape::CubicBezier(_) => true,
Shape::QuadraticBezier(_) | Shape::CubicBezier(_) | Shape::Ellipse(_) => true,

Shape::Noop
| Shape::Text(_)
Expand Down
Loading