capri-ui: add geom module
This commit is contained in:
1 file changed
+92
@@ -0,0 +1,92 @@
|
||||
//! Polygon helpers for the vector effects: egui only fills convex paths, so the
|
||||
//! lightning ribbons, shards and clip plates are triangulated here.
|
||||
use eframe::egui::{epaint::Vertex, Color32, Mesh, Painter, Pos2, Rect, Shape, TextureId};
|
||||
|
||||
fn cross(o: Pos2, a: Pos2, b: Pos2) -> f32 {
|
||||
(a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
|
||||
}
|
||||
|
||||
fn signed_area(p: &[Pos2]) -> f32 {
|
||||
(0..p.len()).map(|i| {
|
||||
let (a, b) = (p[i], p[(i + 1) % p.len()]);
|
||||
a.x * b.y - b.x * a.y
|
||||
}).sum::<f32>() / 2.0
|
||||
}
|
||||
|
||||
/// Ear clipping. Self-intersecting input (sharp ribbon notches) ends with a fan of the rest.
|
||||
pub fn triangulate(p: &[Pos2]) -> Vec<[usize; 3]> {
|
||||
let mut idx: Vec<usize> = (0..p.len()).collect();
|
||||
let mut out = Vec::new();
|
||||
if p.len() < 3 {
|
||||
return out;
|
||||
}
|
||||
let sign = if signed_area(p) >= 0.0 { 1.0 } else { -1.0 };
|
||||
let mut guard = 0;
|
||||
while idx.len() > 3 && guard < p.len() * p.len() {
|
||||
guard += 1;
|
||||
let n = idx.len();
|
||||
let ear = (0..n).find(|&i| {
|
||||
let (a, b, c) = (p[idx[(i + n - 1) % n]], p[idx[i]], p[idx[(i + 1) % n]]);
|
||||
if cross(a, b, c) * sign <= 0.0 {
|
||||
return false;
|
||||
}
|
||||
!idx.iter().any(|&j| {
|
||||
let q = p[j];
|
||||
q != a && q != b && q != c && cross(a, b, q) * sign >= 0.0 && cross(b, c, q) * sign >= 0.0 && cross(c, a, q) * sign >= 0.0
|
||||
})
|
||||
});
|
||||
let Some(i) = ear else { break };
|
||||
out.push([idx[(i + n - 1) % n], idx[i], idx[(i + 1) % n]]);
|
||||
idx.remove(i);
|
||||
}
|
||||
for i in 1..idx.len().saturating_sub(1) {
|
||||
out.push([idx[0], idx[i], idx[i + 1]]);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn mesh(points: &[Pos2], color: Color32, uv: impl Fn(Pos2) -> Pos2, texture: TextureId) -> Mesh {
|
||||
let mut m = Mesh::with_texture(texture);
|
||||
m.vertices = points.iter().map(|&pos| Vertex { pos, uv: uv(pos), color }).collect();
|
||||
m.indices = triangulate(points).into_iter().flatten().map(|i| i as u32).collect();
|
||||
m
|
||||
}
|
||||
|
||||
/// Fills any simple polygon.
|
||||
pub fn fill(painter: &Painter, points: &[Pos2], color: Color32) {
|
||||
if points.len() >= 3 && points.iter().all(|p| p.x.is_finite() && p.y.is_finite()) {
|
||||
painter.add(Shape::mesh(mesh(points, color, |_| Pos2::ZERO, TextureId::default())));
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws the part of `image` (stretched over `rect`) inside the polygon `clip`.
|
||||
pub fn image_clipped(painter: &Painter, texture: TextureId, rect: Rect, clip: &[Pos2], tint: Color32) {
|
||||
if clip.len() >= 3 {
|
||||
let uv = |p: Pos2| Pos2::new((p.x - rect.min.x) / rect.width(), (p.y - rect.min.y) / rect.height());
|
||||
painter.add(Shape::mesh(mesh(clip, tint, uv, texture)));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rect_points(r: Rect) -> [Pos2; 4] {
|
||||
[r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn area(p: &[Pos2], tris: &[[usize; 3]]) -> f32 {
|
||||
tris.iter().map(|t| cross(p[t[0]], p[t[1]], p[t[2]]).abs() / 2.0).sum()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn triangulates_concave_polygons() {
|
||||
// An L shape in both orientations: area 3.
|
||||
let l = [Pos2::new(0., 0.), Pos2::new(2., 0.), Pos2::new(2., 1.), Pos2::new(1., 1.), Pos2::new(1., 2.), Pos2::new(0., 2.)];
|
||||
let tris = triangulate(&l);
|
||||
assert!((area(&l, &tris) - 3.0).abs() < 1e-4, "{tris:?}");
|
||||
let mut r = l.to_vec();
|
||||
r.reverse();
|
||||
assert!((area(&r, &triangulate(&r)) - 3.0).abs() < 1e-4);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user