1use crate::scene::Primitive;
11
12pub const DEBUG_BOX_STROKE: &str = "#e91e63";
14pub const DEBUG_BOX_STROKE_PX: f64 = 1.0;
16pub const DEBUG_BOX_DASH: &str = "4 2";
17pub const DEBUG_BOX_OPACITY: f64 = 0.45;
19
20pub fn debug_rect(x: f64, y: f64, w: f64, h: f64, class: &str) -> Primitive {
22 let x2 = x + w;
23 let y2 = y + h;
24 Primitive::Path {
25 d: format!("M {:.2} {:.2} H {:.2} V {:.2} H {:.2} Z", x, y, x2, y2, x),
26 stroke: Some(DEBUG_BOX_STROKE.into()),
27 fill: Some("none".into()),
28 stroke_width: DEBUG_BOX_STROKE_PX,
29 opacity: DEBUG_BOX_OPACITY,
30 stroke_dasharray: Some(DEBUG_BOX_DASH.into()),
31 stroke_linecap: Some("butt".into()),
32 class: Some(class.into()),
33 data_text: None,
34 }
35}
36
37pub fn label_debug_rect(
39 min_x: f64,
40 min_y: f64,
41 max_x: f64,
42 max_y: f64,
43 atom_index: i32,
44) -> Primitive {
45 debug_rect(
46 min_x,
47 min_y,
48 (max_x - min_x).max(0.0),
49 (max_y - min_y).max(0.0),
50 &format!("debug-box debug-box-label atom-{atom_index}"),
51 )
52}
53
54pub fn mol_debug_rect(width: f64, height: f64) -> Primitive {
56 debug_rect(0.0, 0.0, width, height, "debug-box debug-box-mol")
57}
58
59pub fn scene_debug_rect(width: f64, height: f64) -> Primitive {
61 debug_rect(0.0, 0.0, width, height, "debug-box debug-box-scene")
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn debug_rect_is_closed_path() {
70 let p = debug_rect(1.0, 2.0, 10.0, 20.0, "debug-box");
71 match p {
72 Primitive::Path {
73 d,
74 fill: Some(f),
75 stroke: Some(s),
76 opacity,
77 class: Some(c),
78 ..
79 } => {
80 assert!(d.ends_with('Z') || d.ends_with("Z"));
81 assert_eq!(f, "none");
82 assert_eq!(s, DEBUG_BOX_STROKE);
83 assert!(
84 (opacity - DEBUG_BOX_OPACITY).abs() < 1e-9 && opacity < 1.0,
85 "debug borders need alpha so overlaps are visible; got {opacity}"
86 );
87 assert!(c.contains("debug-box"));
88 }
89 _ => panic!("expected path"),
90 }
91 }
92}