mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Clip transform previews in source space
This commit is contained in:
@@ -128,6 +128,16 @@ pub struct PreviewMesh {
|
||||
}
|
||||
|
||||
impl PreviewMesh {
|
||||
/// Clips to a quadrilateral in normalized source texture coordinates.
|
||||
#[must_use]
|
||||
pub fn clipped_to_source(self, quad: [Point; 4], extent: [f32; 2]) -> PreviewMesh {
|
||||
if !extent.iter().all(|v| v.is_finite() && *v > 0.0) {
|
||||
return PreviewMesh::default();
|
||||
}
|
||||
let quad = quad.map(|p| Point::new(p.x * f64::from(extent[0]), p.y * f64::from(extent[1])));
|
||||
clip_quad(self, quad, true)
|
||||
}
|
||||
|
||||
/// Clips the preview to a convex quadrilateral in document space.
|
||||
#[must_use]
|
||||
pub fn clipped_to_quad(self, quad: [Point; 4]) -> PreviewMesh {
|
||||
@@ -336,6 +346,10 @@ pub(super) fn clip_to_canvas(mesh: PreviewMesh, canvas: [u32; 2]) -> PreviewMesh
|
||||
}
|
||||
|
||||
fn clip_to_quad(mesh: PreviewMesh, quad: [Point; 4]) -> PreviewMesh {
|
||||
clip_quad(mesh, quad, false)
|
||||
}
|
||||
|
||||
fn clip_quad(mesh: PreviewMesh, quad: [Point; 4], source: bool) -> PreviewMesh {
|
||||
let cross = |a: Point, b: Point, p: Point| (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||
let area: f64 = (0..4).map(|i| quad[i].x * quad[(i + 1) % 4].y - quad[(i + 1) % 4].x * quad[i].y).sum();
|
||||
if !area.is_finite() || area.abs() < 1e-12 || (0..4).any(|i| cross(quad[i], quad[(i + 1) % 4], quad[(i + 2) % 4]) * area < 0.0) {
|
||||
@@ -343,7 +357,12 @@ fn clip_to_quad(mesh: PreviewMesh, quad: [Point; 4]) -> PreviewMesh {
|
||||
}
|
||||
let distance = |edge: usize, p: Point| cross(quad[edge], quad[(edge + 1) % 4], p) * area.signum();
|
||||
let inside = |p: &Point| (0..4).all(|edge| distance(edge, *p) >= 0.0);
|
||||
if mesh.points.iter().all(inside) {
|
||||
let entirely_inside = if source {
|
||||
mesh.uv.iter().all(|uv| inside(&Point::new(f64::from(uv[0]), f64::from(uv[1]))))
|
||||
} else {
|
||||
mesh.points.iter().all(inside)
|
||||
};
|
||||
if entirely_inside {
|
||||
return mesh;
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -361,7 +380,7 @@ fn clip_to_quad(mesh: PreviewMesh, quad: [Point; 4]) -> PreviewMesh {
|
||||
let Some(mut poly) = tri.iter().map(|&k| vertex(k)).collect::<Option<Vec<_>>>() else { continue };
|
||||
// Clip each triangle against the four edges, interpolating its texture coordinates.
|
||||
for edge in 0..4 {
|
||||
let dist = |v: &Vertex| distance(edge, v.point);
|
||||
let dist = |v: &Vertex| distance(edge, if source { Point::new(f64::from(v.uv[0]), f64::from(v.uv[1])) } else { v.point });
|
||||
let mut next = Vec::with_capacity(poly.len() + 2);
|
||||
for (k, a) in poly.iter().enumerate() {
|
||||
let Some(b) = poly.get((k + 1) % poly.len()) else { continue };
|
||||
|
||||
@@ -118,6 +118,11 @@ pub struct TransformPreview<'a> {
|
||||
/// A convex quadrilateral in document space limiting the visible preview.
|
||||
#[prop]
|
||||
clip_quad: Option<[Point; 4]>,
|
||||
/// A convex quadrilateral in normalized source coordinates, clipped before transformation.
|
||||
/// With a source clip the plain mesh is shown, since the styled composite includes effects
|
||||
/// outside the source and cannot be clipped in source space after compositing.
|
||||
#[prop]
|
||||
source_clip: Option<[Point; 4]>,
|
||||
/// The monitor conversion of the displayed upload; the mip levels stay raw.
|
||||
#[prop]
|
||||
display: Option<&'a PreviewDisplay>,
|
||||
@@ -145,6 +150,7 @@ impl<'a> TransformPreview<'a> {
|
||||
nearest: false,
|
||||
clip: None,
|
||||
clip_quad: None,
|
||||
source_clip: None,
|
||||
display: None,
|
||||
styled: None,
|
||||
dragging: false,
|
||||
@@ -195,8 +201,8 @@ impl Component for TransformPreview<'_> {
|
||||
let scale = map.source_scale(self.textures.size());
|
||||
let tint = Color32::from_white_alpha((self.opacity.clamp(0.0, 1.0) * 255.0).round() as u8);
|
||||
|
||||
let styled = match (&self.styled, &map, self.clip) {
|
||||
(Some((preview, source)), PreviewMap::Quad(quad), Some(canvas)) => {
|
||||
let styled = match (&self.styled, &map, self.clip, self.source_clip) {
|
||||
(Some((preview, source)), PreviewMap::Quad(quad), Some(canvas), None) => {
|
||||
let mut frame = StyledFrame::new(self.textures, *quad, canvas, zoom);
|
||||
frame.quad_scale = scale;
|
||||
frame.nearest = self.nearest;
|
||||
@@ -219,6 +225,10 @@ impl Component for TransformPreview<'_> {
|
||||
let choice = PreviewTextures::choose(zoom, scale, self.nearest);
|
||||
let Some((texture, uv)) = self.textures.pick(&ctx, choice, self.display) else { return };
|
||||
let mesh = map.build(view.zoom * f64::from(device), uv);
|
||||
let mesh = match self.source_clip {
|
||||
Some(clip) => mesh.clipped_to_source(clip, uv),
|
||||
None => mesh,
|
||||
};
|
||||
add_mesh(&painter, mesh, texture, tint, self.clip, self.clip_quad, &view);
|
||||
}
|
||||
if let Some(first) = first {
|
||||
|
||||
@@ -66,6 +66,20 @@ fn unit_quad() -> [Point; 4] {
|
||||
[pt(0.0, 0.0), pt(100.0, 0.0), pt(100.0, 100.0), pt(0.0, 100.0)]
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn source_clip_moves_with_the_preview_and_preserves_texture_coordinates() {
|
||||
let clip = [pt(0.0, 0.0), pt(1.0, 0.0), pt(1.0, 0.5), pt(0.0, 0.5)];
|
||||
let moved = [pt(20.0, 40.0), pt(120.0, 40.0), pt(120.0, 140.0), pt(20.0, 140.0)];
|
||||
let mesh = PreviewMap::quad(moved).build(1.0, [0.8, 0.6]).clipped_to_source(clip, [0.8, 0.6]);
|
||||
assert!(!mesh.indices.is_empty());
|
||||
for &index in &mesh.indices {
|
||||
let p = mesh.points[index as usize];
|
||||
let uv = mesh.uv[index as usize];
|
||||
assert!(p.y <= 90.001, "{p:?}");
|
||||
assert!((f64::from(uv[1]) / 0.6 - (p.y - 40.0) / 100.0).abs() < 1e-5);
|
||||
}
|
||||
}
|
||||
|
||||
/// A clipped preview retains texture coordinates where its frame cuts the image.
|
||||
#[trial]
|
||||
fn a_preview_clipped_to_a_frame_keeps_its_texture_coordinates() {
|
||||
|
||||
Reference in New Issue
Block a user