Skip to main content

brep_render/
formatting.rs

1//! Numeric formatting shared by annotations and app panels.
2
3/// Fixed precision without trailing fractional zeros. Preserves signed zero;
4/// callers that use the result as an identifier may choose to normalize it.
5pub fn compact_decimal(value: f64, precision: usize) -> String {
6    let text = format!("{value:.precision$}");
7    if precision == 0 {
8        return text;
9    }
10    text.trim_end_matches('0').trim_end_matches('.').to_string()
11}
12
13/// The grid every viewport-gizmo drag writes to, per unit shown (mm, degrees).
14const DRAG_GRID_PER_UNIT: f64 = 1000.0;
15
16/// Below this a drag has not moved a stored number: it is pose or solver noise.
17const DRAG_UNMOVED: f64 = 1e-6;
18
19/// `value` on the 0.001 drag grid. Divides by the exact integer rather than
20/// multiplying by `0.001`, which is not representable and would bring back the
21/// `12.300000000000001` the grid exists to remove. Never returns `-0`.
22pub fn snap_drag_value(value: f64) -> f64 {
23    if !value.is_finite() {
24        return value;
25    }
26    (value * DRAG_GRID_PER_UNIT).round() / DRAG_GRID_PER_UNIT + 0.0
27}
28
29/// One stored number after a drag: on the grid if the drag moved it, otherwise
30/// exactly what it was, so that dragging X leaves a typed Y of `1.23456` alone.
31pub fn snap_drag_moved(before: f64, after: f64) -> f64 {
32    if (after - before).abs() <= DRAG_UNMOVED {
33        before
34    } else {
35        snap_drag_value(after)
36    }
37}
38