feat(domain): per-view minimum point radius in preview state
PreviewState.min_point_radius maps a view id to the smallest point hitbox radius that passes TooSmall on the smallest preview, so the sheet editor can mark it on a size slider. Twin constants named in layer.rs; frame.rs shares one first-view drawing lookup.
This commit is contained in:
1 parent
56c75b12ef
commit
5ca2b7e3ad
8 files changed
+202
-9
No files matched your search
@@ -7,12 +7,16 @@
|
||||
|
||||
mod common;
|
||||
|
||||
use faunosteo_core::anatomy::frame::{
|
||||
Frame, MIN_VISIBLE_PX, Placement, annotation_placement, min_point_radius, view_frame,
|
||||
};
|
||||
use faunosteo_core::anatomy::interior::Budget;
|
||||
use faunosteo_core::domain::js::parse_json;
|
||||
use faunosteo_core::domain::sheet::preview::{
|
||||
PreviewDrawing, PreviewDrawingStatus, PreviewFailure, PreviewInput, PreviewSheet, PreviewState,
|
||||
PreviewStatus,
|
||||
};
|
||||
use faunosteo_core::domain::sheet::{BundledSheetData, LoadedSheet, SheetIndexData};
|
||||
use faunosteo_core::domain::sheet::{Annotation, BundledSheetData, LoadedSheet, SheetIndexData};
|
||||
use faunosteo_core::facade::{call_with_preview, call_with_sheet};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -466,3 +470,93 @@ fn a_drawing_outside_the_stored_form_shows_rejected() {
|
||||
PreviewDrawingStatus::Ok
|
||||
);
|
||||
}
|
||||
|
||||
/// The minimum radius the formula gives on a frame, rounded up to 0.01, written apart from the
|
||||
/// function under test.
|
||||
fn formula_radius(frame: &Frame, main: bool) -> Option<f64> {
|
||||
let (w, h) = if main { (286.0, 310.0) } else { (254.0, 191.0) };
|
||||
let scale = (w / frame.width).min(h / frame.height);
|
||||
let half_disc = MIN_VISIBLE_PX / 2.0 / scale;
|
||||
(half_disc > 2.0).then(|| ((12.0 + half_disc) * 100.0).ceil() / 100.0)
|
||||
}
|
||||
|
||||
/// The frames of the equine draft's views, read from the fixture's document and drawing.
|
||||
fn equine_frames(sheet: &PreviewSheet) -> Vec<(String, bool, Frame)> {
|
||||
let drawing = sheet.drawing(HORSE_ID).unwrap().as_ref().unwrap();
|
||||
sheet
|
||||
.document()
|
||||
.views
|
||||
.iter()
|
||||
.map(|v| {
|
||||
(
|
||||
v.id().to_owned(),
|
||||
v.is_main(),
|
||||
view_frame(v, drawing).unwrap(),
|
||||
)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn each_view_states_the_smallest_radius_a_point_may_have() {
|
||||
let sheet = published();
|
||||
let radii = state(&sheet).min_point_radius;
|
||||
let frames = equine_frames(&sheet);
|
||||
assert_eq!(radii.len(), frames.len());
|
||||
for (id, main, frame) in &frames {
|
||||
assert_eq!(radii.get(id).copied(), formula_radius(frame, *main), "{id}");
|
||||
}
|
||||
assert!((radii["main"] - 26.50).abs() < 0.01, "{radii:?}");
|
||||
assert!((radii["head"] - 15.98).abs() < 0.01, "{radii:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_view_that_shows_every_radius_or_has_no_frame_states_none() {
|
||||
let main = |w: f64, h: f64| Frame {
|
||||
min_x: 0.0,
|
||||
min_y: 0.0,
|
||||
width: w,
|
||||
height: h,
|
||||
};
|
||||
// Width 100: the main preview scale is 2.86, the twin's floor of 2 units shows 11 px.
|
||||
assert_eq!(min_point_radius(&main(100.0, 100.0), true), None);
|
||||
// Scale of exactly 1: the floor shows exactly 4 px, no radius fails.
|
||||
assert_eq!(min_point_radius(&main(286.0, 310.0), true), None);
|
||||
assert!(min_point_radius(&main(286.1, 310.0), true).is_some());
|
||||
let mut doc = parse_json(&document_text()).unwrap();
|
||||
doc["views"][1]["view_box"] = json!({"min_x": 0, "min_y": 0, "width": 0.5, "height": 0.5});
|
||||
let draft = input(PreviewStatus::Draft, doc.to_string(), None);
|
||||
let sheet = PreviewSheet::load(draft, vec![horse_drawing()]).unwrap();
|
||||
assert_eq!(
|
||||
state(&sheet).min_point_radius.keys().collect::<Vec<_>>(),
|
||||
["main"]
|
||||
);
|
||||
}
|
||||
|
||||
/// A point of the draft with the radius `r`, centred in the middle of its view's frame.
|
||||
fn point_with(frame: &Frame, view: &str, r: f64) -> Annotation {
|
||||
let mut point = parse_json(&document_text()).unwrap()["annotations"][0].clone();
|
||||
assert_eq!(point["geometry"]["type"], json!("circle"));
|
||||
point["view_id"] = json!(view);
|
||||
point["geometry"] = json!({
|
||||
"type": "circle",
|
||||
"cx": frame.min_x + frame.width / 2.0,
|
||||
"cy": frame.min_y + frame.height / 2.0,
|
||||
"r": r
|
||||
});
|
||||
serde_json::from_value(point).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_stated_radius_passes_and_a_hundredth_less_is_too_small() {
|
||||
let sheet = published();
|
||||
let radii = state(&sheet).min_point_radius;
|
||||
for (id, main, frame) in equine_frames(&sheet) {
|
||||
let placement = |r: f64| {
|
||||
let point = point_with(&frame, &id, r);
|
||||
annotation_placement(&point, &frame, main, &mut Budget::default())
|
||||
};
|
||||
assert_eq!(placement(radii[&id]), Placement::InFrame, "{id}");
|
||||
assert_eq!(placement(radii[&id] - 0.01), Placement::TooSmall, "{id}");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user