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

Tessellate: Implement precomputed vertices for rounding #1507

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
144 changes: 131 additions & 13 deletions epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,117 @@ pub mod path {

use super::*;

mod precomputed_quadrants {
use emath::{vec2, Vec2};

pub const QUADRANTS_0_5: [[Vec2; 3]; 4] = [
emilk marked this conversation as resolved.
Show resolved Hide resolved
// quadrant 0: right bottom
[
vec2(1.000000, 0.000000),
vec2(0.707107, 0.707107),
vec2(-0.000000, 1.000000)
],
// quadrant 1: left bottom
[
vec2(-0.000000, 1.000000),
vec2(-0.707107, 0.707107),
vec2(-1.000000, -0.000000),
],
// quadrant 2: left top
[
vec2(-1.000000, -0.000000),
vec2(-0.707107, -0.707107),
vec2(0.000000, -1.000000),
],
// quadrant 3: right top
[
vec2(0.000000, -1.000000),
vec2(0.707107, -0.707107),
vec2(1.000000, 0.000000),
],
];

pub const QUADRANTS_5_10: [[Vec2; 5]; 4] = [
// quadrant 0: right bottom
[
vec2(1.000000, 0.000000),
vec2(0.923880, 0.382683),
vec2(0.707107, 0.707107),
vec2(0.382683, 0.923880),
vec2(-0.000000, 1.000000),
],
// quadrant 1: left bottom
[
vec2(-0.000000, 1.000000),
vec2(-0.382684, 0.923880),
vec2(-0.707107, 0.707107),
vec2(-0.923880, 0.382683),
vec2(-1.000000, -0.000000),
],
// quadrant 2: left top
[
vec2(-1.000000, -0.000000),
vec2(-0.923880, -0.382683),
vec2(-0.707107, -0.707107),
vec2(-0.382684, -0.923880),
vec2(0.000000, -1.000000),
],
// quadrant 3: right top
[
vec2(0.000000, -1.000000),
vec2(0.382684, -0.923879),
vec2(0.707107, -0.707107),
vec2(0.923880, -0.382683),
vec2(1.000000, 0.000000),
],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having the four quadrants in separate array, we could just have all the circle vertices in circular order, and just slice into it. That way we could reuse the same vertices for circles too! It can wait for a separate PR though if you want.

];

pub const QUADRANTS_10_20: [[Vec2; 7]; 4] = [
// quadrant 0: right bottom
[
vec2(1.000000, 0.000000),
vec2(0.965926, 0.258819),
vec2(0.866025, 0.500000),
vec2(0.707107, 0.707107),
vec2(0.500000, 0.866025),
vec2(0.258819, 0.965926),
vec2(-0.000000, 1.000000),
],
// quadrant 1: left bottom
[
vec2(-0.000000, 1.000000),
vec2(-0.258819, 0.965926),
vec2(-0.500000, 0.866025),
vec2(-0.707107, 0.707107),
vec2(-0.866025, 0.500000),
vec2(-0.965926, 0.258819),
vec2(-1.000000, -0.000000),
],
// quadrant 2: left top
[
vec2(-1.000000, -0.000000),
vec2(-0.965926, -0.258819),
vec2(-0.866026, -0.500000),
vec2(-0.707107, -0.707107),
vec2(-0.500000, -0.866025),
vec2(-0.258819, -0.965926),
vec2(0.000000, -1.000000),
],
// quadrant 3: right top
[
vec2(0.000000, -1.000000),
vec2(0.258819, -0.965926),
vec2(0.500000, -0.866025),
vec2(0.707107, -0.707107),
vec2(0.866025, -0.500000),
vec2(0.965926, -0.258819),
vec2(1.000000, 0.000000),
],
];
}

use precomputed_quadrants::*;

/// overwrites existing points
pub fn rounded_rectangle(path: &mut Vec<Pos2>, rect: Rect, rounding: Rounding) {
path.clear();
Expand Down Expand Up @@ -236,19 +347,26 @@ pub mod path {
// - quadrant 3: right top
// * angle 4 * TAU / 4 = right
pub fn add_circle_quadrant(path: &mut Vec<Pos2>, center: Pos2, radius: f32, quadrant: f32) {
// TODO: optimize with precalculated vertices for some radii ranges

let n = (radius * 0.75).round() as i32; // TODO: tweak a bit more
let n = n.clamp(2, 32);
const RIGHT_ANGLE: f32 = TAU / 4.0;
path.reserve(n as usize + 1);
for i in 0..=n {
let angle = remap(
i as f32,
0.0..=n as f32,
quadrant * RIGHT_ANGLE..=(quadrant + 1.0) * RIGHT_ANGLE,
);
path.push(center + radius * Vec2::angled(angle));
if radius == 0.0 {
emilk marked this conversation as resolved.
Show resolved Hide resolved
path.push(center);
} else if radius <= 5.0 {
let quadrant_vertices = QUADRANTS_0_5[quadrant as usize];
path.reserve(quadrant_vertices.len());
quadrant_vertices
.iter()
.for_each(|v| path.push(center + radius * *v));
emilk marked this conversation as resolved.
Show resolved Hide resolved
} else if radius > 5.0 && radius <= 10.0 {
emilk marked this conversation as resolved.
Show resolved Hide resolved
let quadrant_vertices = QUADRANTS_5_10[quadrant as usize];
path.reserve(quadrant_vertices.len());
quadrant_vertices
.iter()
.for_each(|v| path.push(center + radius * *v));
} else {
let quadrant_vertices = QUADRANTS_10_20[quadrant as usize];
path.reserve(quadrant_vertices.len());
quadrant_vertices
.iter()
.for_each(|v| path.push(center + radius * *v));
}
}

Expand Down