BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / image_view_math.rs

image_view_math.rs

image_view_math.rs 7.3 KiB Download Raw
1use std::ops::Range;
2
3pub type Pair = (f32, f32);
4
5pub const MAX_ZOOM: f32 = 4.;
6pub const ZOOM_STEP: f32 = 1.25;
7const WHEEL_SENSITIVITY: f32 = 0.01;
8const SCALE_EPSILON: f32 = 0.001;
9
10pub fn rotated_size(size: Pair, quarter_turns: u8) -> Pair {
11 if quarter_turns % 2 == 0 {
12 size
13 } else {
14 (size.1, size.0)
15 }
16}
17
18pub fn next_rotation(quarter_turns: u8) -> u8 {
19 (quarter_turns + 1) % 4
20}
21
22pub fn fit_scale(image: Pair, stage: Pair) -> f32 {
23 if image.0 <= 0. || image.1 <= 0. || stage.0 <= 0. || stage.1 <= 0. {
24 return 1.;
25 }
26 (stage.0 / image.0).min(stage.1 / image.1).min(1.)
27}
28
29pub fn clamp_scale(scale: f32, fit: f32) -> f32 {
30 scale.clamp(fit, MAX_ZOOM.max(fit))
31}
32
33pub fn display_size(image: Pair, scale: f32) -> Pair {
34 (image.0 * scale, image.1 * scale)
35}
36
37pub fn clamp_pan(pan: Pair, display: Pair, stage: Pair) -> Pair {
38 let limit = |display: f32, stage: f32| ((display - stage) / 2.).max(0.);
39 let (limit_x, limit_y) = (limit(display.0, stage.0), limit(display.1, stage.1));
40 (
41 pan.0.clamp(-limit_x, limit_x),
42 pan.1.clamp(-limit_y, limit_y),
43 )
44}
45
46pub fn image_origin(display: Pair, stage: Pair, pan: Pair) -> Pair {
47 (
48 (stage.0 - display.0) / 2. + pan.0,
49 (stage.1 - display.1) / 2. + pan.1,
50 )
51}
52
53#[derive(Debug, Clone, Copy, PartialEq)]
54pub struct View {
55 pub scale: f32,
56 pub pan: Pair,
57}
58
59pub fn zoom_at(view: View, wanted: f32, anchor: Pair, image: Pair, stage: Pair) -> View {
60 let fit = fit_scale(image, stage);
61 let scale = clamp_scale(wanted, fit);
62 let ratio = scale / view.scale;
63 let pan = (
64 anchor.0 - (anchor.0 - view.pan.0) * ratio,
65 anchor.1 - (anchor.1 - view.pan.1) * ratio,
66 );
67 View {
68 scale,
69 pan: clamp_pan(pan, display_size(image, scale), stage),
70 }
71}
72
73pub fn wheel_scale(scale: f32, delta_y: f32) -> f32 {
74 scale * (delta_y * WHEEL_SENSITIVITY).exp()
75}
76
77pub fn step_scale(scale: f32, direction: i32) -> f32 {
78 scale * ZOOM_STEP.powi(direction)
79}
80
81pub fn toggle_target(scale: f32, fit: f32) -> f32 {
82 if scale > fit + SCALE_EPSILON {
83 fit
84 } else {
85 1_f32.max(fit)
86 }
87}
88
89pub fn percent_label(scale: f32) -> String {
90 format!("{}%", (scale * 100.).round() as u32)
91}
92
93pub fn step_index(current: usize, len: usize, delta: isize) -> usize {
94 if len == 0 {
95 return 0;
96 }
97 current
98 .saturating_add_signed(delta)
99 .min(len - 1)
100}
101
102pub fn counter_label(index: usize, len: usize) -> String {
103 format!("{} of {}", index + 1, len)
104}
105
106pub fn thumbnail_window(index: usize, len: usize, capacity: usize) -> Range<usize> {
107 let capacity = capacity.max(1).min(len);
108 let start = index
109 .saturating_sub(capacity / 2)
110 .min(len.saturating_sub(capacity));
111 start..start + capacity
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 const STAGE: Pair = (800., 600.);
119
120 #[test]
121 fn fit_contains_large_images_and_never_upscales() {
122 assert_eq!(fit_scale((1600., 1200.), STAGE), 0.5);
123 assert_eq!(fit_scale((1600., 300.), STAGE), 0.5);
124 assert_eq!(fit_scale((100., 100.), STAGE), 1.);
125 assert_eq!(fit_scale((0., 100.), STAGE), 1.);
126 }
127
128 #[test]
129 fn rotation_swaps_the_sides_on_odd_quarter_turns() {
130 assert_eq!(rotated_size((400., 200.), 0), (400., 200.));
131 assert_eq!(rotated_size((400., 200.), 1), (200., 400.));
132 assert_eq!(rotated_size((400., 200.), 2), (400., 200.));
133 assert_eq!(rotated_size((400., 200.), 3), (200., 400.));
134 assert_eq!(next_rotation(3), 0);
135 assert_eq!(next_rotation(0), 1);
136 }
137
138 #[test]
139 fn scale_is_limited_to_fit_and_four_times_the_source() {
140 assert_eq!(clamp_scale(0.1, 0.5), 0.5);
141 assert_eq!(clamp_scale(9., 0.5), MAX_ZOOM);
142 assert_eq!(clamp_scale(2., 0.5), 2.);
143 }
144
145 #[test]
146 fn pan_is_clamped_to_the_image_bounds() {
147 let display = (1600., 400.);
148 assert_eq!(clamp_pan((900., 50.), display, STAGE), (400., 0.));
149 assert_eq!(clamp_pan((-900., -50.), display, STAGE), (-400., 0.));
150 assert_eq!(clamp_pan((10., 10.), (400., 300.), STAGE), (0., 0.));
151 }
152
153 #[test]
154 fn image_origin_centers_and_follows_the_pan() {
155 assert_eq!(image_origin((400., 300.), STAGE, (0., 0.)), (200., 150.));
156 assert_eq!(image_origin((1600., 600.), STAGE, (-100., 0.)), (-500., 0.));
157 }
158
159 #[test]
160 fn zoom_keeps_the_point_under_the_cursor_fixed() {
161 let image = (2000., 1500.);
162 let start = View {
163 scale: 0.4,
164 pan: (0., 0.),
165 };
166 let anchor = (100., 50.);
167 let zoomed = zoom_at(start, 1., anchor, image, STAGE);
168 assert_eq!(zoomed.scale, 1.);
169 let before = ((anchor.0 - start.pan.0) / start.scale, (anchor.1 - start.pan.1) / start.scale);
170 let after = ((anchor.0 - zoomed.pan.0) / zoomed.scale, (anchor.1 - zoomed.pan.1) / zoomed.scale);
171 assert!((before.0 - after.0).abs() < 0.01);
172 assert!((before.1 - after.1).abs() < 0.01);
173 }
174
175 #[test]
176 fn zooming_back_to_fit_recenters() {
177 let image = (2000., 1500.);
178 let zoomed = View {
179 scale: 2.,
180 pan: (300., -200.),
181 };
182 let fit = zoom_at(zoomed, 0.1, (50., 50.), image, STAGE);
183 assert_eq!(fit.scale, 0.4);
184 assert_eq!(fit.pan, (0., 0.));
185 }
186
187 #[test]
188 fn zoom_never_pans_past_the_edges() {
189 let image = (2000., 1500.);
190 let start = View {
191 scale: 0.4,
192 pan: (0., 0.),
193 };
194 let zoomed = zoom_at(start, 4., (-400., -300.), image, STAGE);
195 let limit = (2000. * 4. - 800.) / 2.;
196 assert!(zoomed.pan.0.abs() <= limit);
197 }
198
199 #[test]
200 fn wheel_zooms_in_up_and_out_down_symmetrically() {
201 assert!(wheel_scale(1., 20.) > 1.);
202 assert!(wheel_scale(1., -20.) < 1.);
203 assert!((wheel_scale(wheel_scale(1., 20.), -20.) - 1.).abs() < 1e-5);
204 assert_eq!(wheel_scale(1., 0.), 1.);
205 }
206
207 #[test]
208 fn keyboard_steps_multiply_and_divide_by_the_step() {
209 assert_eq!(step_scale(1., 1), ZOOM_STEP);
210 assert!((step_scale(ZOOM_STEP, -1) - 1.).abs() < 1e-6);
211 }
212
213 #[test]
214 fn double_click_toggles_between_fit_and_full_size() {
215 assert_eq!(toggle_target(0.4, 0.4), 1.);
216 assert_eq!(toggle_target(1., 0.4), 0.4);
217 assert_eq!(toggle_target(2., 0.4), 0.4);
218 assert_eq!(toggle_target(1., 1.), 1.);
219 }
220
221 #[test]
222 fn percent_label_is_relative_to_the_source_size() {
223 assert_eq!(percent_label(1.), "100%");
224 assert_eq!(percent_label(0.375), "38%");
225 assert_eq!(percent_label(4.), "400%");
226 }
227
228 #[test]
229 fn navigation_stops_at_both_ends() {
230 assert_eq!(step_index(0, 5, -1), 0);
231 assert_eq!(step_index(4, 5, 1), 4);
232 assert_eq!(step_index(2, 5, 1), 3);
233 assert_eq!(step_index(2, 5, -1), 1);
234 assert_eq!(step_index(0, 0, 1), 0);
235 }
236
237 #[test]
238 fn counter_is_one_based() {
239 assert_eq!(counter_label(2, 12), "3 of 12");
240 }
241
242 #[test]
243 fn thumbnail_window_follows_the_current_image_inside_the_list() {
244 assert_eq!(thumbnail_window(0, 20, 5), 0..5);
245 assert_eq!(thumbnail_window(10, 20, 5), 8..13);
246 assert_eq!(thumbnail_window(19, 20, 5), 15..20);
247 assert_eq!(thumbnail_window(1, 3, 5), 0..3);
248 assert_eq!(thumbnail_window(0, 0, 5), 0..0);
249 assert_eq!(thumbnail_window(2, 4, 0), 2..3);
250 }
251}