Skip to main content

qualia_vision/spatial/
compile_10d_handoff.rs

1//! Host handoff notes for sealing vision geometry into `.10d`.
2//!
3//! Pure MeshIR export lives in `specialized_libs::computer_vision::spatial`.
4//! Host (desktop / client-core) constructs `render::assets::Mesh` and seals via
5//! `compile_mesh_to_10d_vision`.
6
7use crate::types::Detection;
8use qualia_core_db::specialized_libs::computer_vision::spatial::{
9    detection_center_to_node_hint, detection_to_sigma, NodeHint, RenderMeshExport,
10};
11
12/// Geometry fields layout-compatible with `qualia_core_db::render::assets::Mesh`.
13///
14/// Host code maps this 1:1 into `Mesh { positions, triangles, min, max }`.
15#[derive(Debug, Clone, PartialEq)]
16pub struct GeometryFor10d {
17    pub positions: Vec<[f32; 3]>,
18    pub triangles: Vec<[u32; 3]>,
19    pub min: [f32; 3],
20    pub max: [f32; 3],
21}
22
23/// Pack a [`RenderMeshExport`] into fields ready for host `Mesh` construction.
24///
25/// Does not allocate beyond cloning the export vectors (cold path).
26#[inline]
27pub fn pack_geometry_export_for_10d(export: &RenderMeshExport) -> GeometryFor10d {
28    GeometryFor10d {
29        positions: export.positions.clone(),
30        triangles: export.triangles.clone(),
31        min: export.min,
32        max: export.max,
33    }
34}
35
36/// Map detections to Tensor10D-style node hints (D1/D2).
37///
38/// - `x,y` = box centre in normalised 0..1 space (`u16` / 65535)
39/// - `z` = 0 (image-plane stub)
40/// - `t` = `frame_index` as f32
41/// - `sigma` = class×score map ([`detection_to_sigma`]) for spectral paint
42///
43/// Writes at most `out.len()` hints; returns count written.
44pub fn detections_to_node_hints(dets: &[Detection], out: &mut [NodeHint]) -> usize {
45    let n = dets.len().min(out.len());
46    for i in 0..n {
47        let d = &dets[i];
48        let cx = (d.x_min_u16 as f32 + d.x_max_u16 as f32) * 0.5 / 65535.0;
49        let cy = (d.y_min_u16 as f32 + d.y_max_u16 as f32) * 0.5 / 65535.0;
50        out[i] = detection_center_to_node_hint(cx, cy, d.frame_index as f32, detection_to_sigma(d));
51    }
52    n
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use crate::types::Detection;
59    use qualia_core_db::specialized_libs::computer_vision::spatial::{
60        detection_to_sigma, mesh_ir_to_export, MeshIR,
61    };
62
63    #[test]
64    fn pack_preserves_mesh_fields() {
65        let mut m = MeshIR::empty();
66        m.positions = vec![[0.0, 0.0, 0.0], [2.0, 0.0, 0.0], [0.0, 3.0, 1.0]];
67        m.indices = vec![0, 1, 2];
68        m.recompute_bounds_and_hash();
69        let e = mesh_ir_to_export(&m).unwrap();
70        let g = pack_geometry_export_for_10d(&e);
71        assert_eq!(g.positions, e.positions);
72        assert_eq!(g.triangles, e.triangles);
73        assert_eq!(g.min, e.min);
74        assert_eq!(g.max, e.max);
75        assert_eq!(g.min, [0.0, 0.0, 0.0]);
76        assert_eq!(g.max, [2.0, 3.0, 1.0]);
77    }
78
79    #[test]
80    fn detections_map_score_to_sigma() {
81        let mut d = Detection::empty();
82        d.x_min_u16 = 0;
83        d.x_max_u16 = 65535;
84        d.y_min_u16 = 0;
85        d.y_max_u16 = 65535;
86        d.score_u16 = 32768; // ~0.5
87        d.class_hash = 4;
88        d.frame_index = 7;
89        let mut out = [NodeHint {
90            x: 0.0,
91            y: 0.0,
92            z: 1.0,
93            t: 0.0,
94            sigma: 0.0,
95        }; 2];
96        let n = detections_to_node_hints(&[d], &mut out);
97        assert_eq!(n, 1);
98        assert!((out[0].x - 0.5).abs() < 1e-4);
99        assert!((out[0].y - 0.5).abs() < 1e-4);
100        assert_eq!(out[0].z, 0.0);
101        assert_eq!(out[0].t, 7.0);
102        let expected = detection_to_sigma(&d);
103        assert!((out[0].sigma - expected).abs() < 1e-5);
104    }
105}