Files
craft-libs/crates/draw/tests/it/perspective_crop.rs
T

25 lines
1.6 KiB
Rust

//! The Perspective Crop quad and tool through the public API: the pure quad rules, the quad's gestures and keys as a
//! canvas control, and the tool's box drag and four clicks on the background.
use craft_behavior::CanvasView;
use craft_behavior::kurbo::Point;
use craft_draw::ui::crop::{grid_lines, grid_steps};
use craft_ui::testing::trial;
use egui::{Pos2, Vec2};
#[trial]
fn grid_lines_meet_at_the_quads_vanishing_points() {
let view = CanvasView::new(egui::Rect::from_min_size(Pos2::ZERO, Vec2::new(400.0, 300.0)), Point::new(200.0, 150.0), 1.0);
let upright = [Point::new(100.0, 50.0), Point::new(300.0, 50.0), Point::new(300.0, 190.0), Point::new(100.0, 190.0)];
let lines = grid_lines(&view, &upright, 48.0);
assert_eq!(lines.len(), 2 * (grid_steps(200.0, 48.0) - 1), "one line per step boundary in each direction");
assert!(lines.iter().all(|[a, b]| (a.x - b.x).abs() < 1e-3 || (a.y - b.y).abs() < 1e-3), "an upright box has an axis-aligned grid");
// A trapezoid narrower at the top: its rows crowd towards the far edge, as the object's own lines do.
let trapezoid = [Point::new(150.0, 50.0), Point::new(250.0, 50.0), Point::new(300.0, 250.0), Point::new(100.0, 250.0)];
let mut rows: Vec<f32> = grid_lines(&view, &trapezoid, 48.0).iter().filter(|[a, b]| (a.y - b.y).abs() < 1e-3).map(|[a, _]| a.y).collect();
rows.sort_by(f32::total_cmp);
let gaps: Vec<f32> = rows.windows(2).map(|pair| pair[1] - pair[0]).collect();
assert!(gaps.len() >= 2 && gaps.windows(2).any(|pair| (pair[0] - pair[1]).abs() > 0.5), "perspective rows are not evenly spaced: {gaps:?}");
}