1use crate::scene::{AlignDebug, AlignDebugRole, AtomIn, Primitive, TextAnchor};
11
12pub const DEBUG_ALIGN_OPACITY: f64 = 0.75;
13pub const DEBUG_ALIGN_UNMATCHED: &str = "#9e9e9e";
14pub const DEBUG_ALIGN_BADGE: &str = "#1565c0";
15
16pub const DEBUG_ALIGN_PALETTE: &[&str] = &[
18 "#e53935", "#8e24aa", "#3949ab", "#039be5", "#00897b", "#43a047", "#f9a825", "#fb8c00",
19 "#6d4c41", "#546e7a", "#c0ca33", "#d81b60",
20];
21
22pub fn tag_color(tag: i32) -> &'static str {
23 let i = tag.rem_euclid(DEBUG_ALIGN_PALETTE.len() as i32) as usize;
24 DEBUG_ALIGN_PALETTE[i]
25}
26
27pub fn align_debug_template(peer_ids: &[String], maps: &[Vec<(i32, i32)>]) -> AlignDebug {
29 let mut tags: Vec<(i32, i32)> = Vec::new();
30 let mut seen = std::collections::HashSet::new();
31 for map in maps {
32 for &(_q, r) in map {
33 if seen.insert(r) {
34 tags.push((r, r));
35 }
36 }
37 }
38 tags.sort_by_key(|(a, _)| *a);
39 AlignDebug {
40 role: AlignDebugRole::Template,
41 peer_id: if peer_ids.is_empty() {
42 None
43 } else {
44 Some(peer_ids.join(","))
45 },
46 tags,
47 }
48}
49
50pub fn align_debug_query(peer_id: &str, query_to_ref: &[(i32, i32)]) -> AlignDebug {
52 let mut tags: Vec<(i32, i32)> = query_to_ref.iter().map(|&(q, r)| (q, r)).collect();
53 tags.sort_by_key(|(a, _)| *a);
54 AlignDebug {
55 role: AlignDebugRole::Query,
56 peer_id: Some(peer_id.to_string()),
57 tags,
58 }
59}
60
61pub fn paint_align_debug(
63 atoms: &[AtomIn],
64 dbg: &AlignDebug,
65 dx: f64,
66 dy: f64,
67 font_px: f64,
68) -> Vec<Primitive> {
69 let mut out = Vec::new();
70 let by: std::collections::HashMap<i32, &AtomIn> = atoms.iter().map(|a| (a.index, a)).collect();
71 let matched: std::collections::HashSet<i32> = dbg.tags.iter().map(|(a, _)| *a).collect();
72 let r = (font_px * 0.45).max(3.0);
73
74 if dbg.role == AlignDebugRole::Query {
75 for a in atoms {
76 if matched.contains(&a.index) {
77 continue;
78 }
79 out.push(Primitive::Circle {
80 cx: a.x + dx,
81 cy: a.y + dy,
82 r: r * 0.7,
83 fill: Some("none".into()),
84 stroke: Some(DEBUG_ALIGN_UNMATCHED.into()),
85 stroke_width: 1.0,
86 opacity: 0.55,
87 class: Some(format!(
88 "debug-align debug-align-unmatched atom-{}",
89 a.index
90 )),
91 });
92 }
93 }
94
95 for &(atom_idx, tag) in &dbg.tags {
96 let Some(a) = by.get(&atom_idx) else {
97 continue;
98 };
99 let color = tag_color(tag);
100 let cx = a.x + dx;
101 let cy = a.y + dy;
102 out.push(Primitive::Circle {
103 cx,
104 cy,
105 r,
106 fill: Some(color.into()),
107 stroke: Some("#ffffff".into()),
108 stroke_width: 0.8,
109 opacity: DEBUG_ALIGN_OPACITY,
110 class: Some(format!(
111 "debug-align debug-align-atom atom-{atom_idx} tag-{tag}"
112 )),
113 });
114 out.push(Primitive::Text {
115 x: cx,
116 y: cy + font_px * 0.35,
117 text: tag.to_string(),
118 fill: "#ffffff".into(),
119 font_size: font_px * 0.85,
120 anchor: TextAnchor::Middle,
121 class: Some(format!(
122 "debug-align debug-align-tag atom-{atom_idx} tag-{tag}"
123 )),
124 rotate: None,
125 });
126 }
127
128 let badge = match dbg.role {
129 AlignDebugRole::Template => {
130 let peers = dbg.peer_id.as_deref().unwrap_or("?");
131 format!("align★T ← {peers} ({} atoms)", dbg.tags.len())
132 }
133 AlignDebugRole::Query => {
134 let peer = dbg.peer_id.as_deref().unwrap_or("?");
135 format!("align→Q → {peer} ({} atoms)", dbg.tags.len())
136 }
137 };
138 out.push(Primitive::Text {
139 x: 4.0,
140 y: font_px * 1.1,
141 text: badge,
142 fill: DEBUG_ALIGN_BADGE.into(),
143 font_size: font_px * 0.7,
144 anchor: TextAnchor::Start,
145 class: Some(format!(
146 "debug-align debug-align-badge role-{}",
147 match dbg.role {
148 AlignDebugRole::Template => "template",
149 AlignDebugRole::Query => "query",
150 }
151 )),
152 rotate: None,
153 });
154 out
155}
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160 use crate::scene::AlignDebugRole;
161
162 #[test]
163 fn query_tags_use_ref_indices() {
164 let d = align_debug_query("m0", &[(2, 5), (3, 1)]);
165 assert_eq!(d.role, AlignDebugRole::Query);
166 assert_eq!(d.tags, vec![(2, 5), (3, 1)]);
167 }
168
169 #[test]
170 fn template_tags_dedupe_ref_atoms() {
171 let d = align_debug_template(
172 &["a".into(), "b".into()],
173 &[vec![(0, 1), (1, 2)], vec![(0, 1), (2, 3)]],
174 );
175 assert_eq!(d.role, AlignDebugRole::Template);
176 assert_eq!(d.tags, vec![(1, 1), (2, 2), (3, 3)]);
177 assert_eq!(d.peer_id.as_deref(), Some("a,b"));
178 }
179}