Skip to main content

qualia_core_db/entity_view/
projection.rs

1//! Flat + scene projection IR for mindware HID (cold-path friendly serde types).
2
3use super::entity_id::{EntityId, EntityKind};
4use super::observer::{AffordanceBits, RepresentationWing};
5use serde::{Deserialize, Serialize};
6
7/// Presentation morphology level P0-P6 (see presentation-morphology plan).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
9#[repr(u8)]
10pub enum PresentationLevel {
11    /// P0 — flat document/card presentation.
12    #[default]
13    Document = 0,
14    /// P1 — application habitat (windowed UI).
15    AppHabitat = 1,
16    /// P2 — spatial desk arrangement.
17    SpatialDesk = 2,
18    /// P3 — staged world presentation.
19    StageWorld = 3,
20    /// P4 — embodied world (avatar-mediated).
21    EmbodiedWorld = 4,
22    /// P5 — infosphere field.
23    Infosphere = 5,
24    /// P6 — multi-sensory presentation.
25    MultiSensory = 6,
26}
27
28impl PresentationLevel {
29    /// Decode a `u8` into a presentation level; out-of-range values default
30    /// to `Document` (P0).
31    pub fn from_u8(v: u8) -> Self {
32        match v {
33            1 => Self::AppHabitat,
34            2 => Self::SpatialDesk,
35            3 => Self::StageWorld,
36            4 => Self::EmbodiedWorld,
37            5 => Self::Infosphere,
38            6 => Self::MultiSensory,
39            _ => Self::Document,
40        }
41    }
42
43    /// The level's discriminant (P0–P6).
44    pub fn as_u8(self) -> u8 {
45        self as u8
46    }
47}
48
49/// Card descriptor for flat HID (Aether / browser chrome lists).
50#[derive(Debug, Clone, Serialize, Deserialize)]
51pub struct FlatCard {
52    pub entity_id: u64,
53    pub kind: EntityKind,
54    pub title: String,
55    pub excerpt: String,
56    pub wing: RepresentationWing,
57    pub affordance_bits: u8,
58    pub honesty: String,
59    pub uri: String,
60}
61
62/// Scene node projection (maps to webizen-render SceneNode fields).
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct SceneNodeProj {
65    pub entity_id: u64,
66    pub id: String,
67    pub x: f64,
68    pub y: f64,
69    pub z: f64,
70    pub color: String,
71    pub radius: f64,
72    pub alpha: f64,
73    pub affordance_bits: u8,
74}
75
76/// Combined projection result for one observer session.
77#[derive(Debug, Clone, Serialize, Deserialize, Default)]
78pub struct ProjectionResult {
79    pub observer: String,
80    pub presentation_level: u8,
81    pub flat: Vec<FlatCard>,
82    pub scene_nodes: Vec<SceneNodeProj>,
83    pub hidden_count: u32,
84}
85
86/// Layout entity with optional geo for pin placement.
87#[derive(Debug, Clone, Copy)]
88pub struct LayoutInput {
89    pub entity_id: EntityId,
90    pub lat: Option<f32>,
91    pub lon: Option<f32>,
92    pub affordances: AffordanceBits,
93    pub wing: RepresentationWing,
94}
95
96/// Wing → presentation colour (cinema-readable, not neon random).
97pub fn wing_color(wing: RepresentationWing) -> &'static str {
98    match wing {
99        RepresentationWing::Private => "#a78bfa", // violet
100        RepresentationWing::Offered => "#34d399", // emerald
101        RepresentationWing::Commons => "#38bdf8", // sky
102    }
103}
104
105/// Place nodes: geo pins when lat/lon present; otherwise golden-angle manifold field.
106/// Writes up to `out.len()` nodes; returns count.
107pub fn layout_scene_nodes(inputs: &[LayoutInput], out: &mut [SceneNodeProj]) -> usize {
108    let mut n = 0;
109    let mut field_i = 0usize;
110    let field_total = inputs
111        .iter()
112        .filter(|i| i.lat.is_none() || i.lon.is_none())
113        .count()
114        .max(1);
115    for inp in inputs {
116        if n >= out.len() {
117            break;
118        }
119        let (x, y, z) = if let (Some(lat), Some(lon)) = (inp.lat, inp.lon) {
120            // Equirectangular sketch in 0..1 for 2D map morph.
121            let xn = ((lon as f64) + 180.0) / 360.0;
122            let yn = 1.0 - ((lat as f64) + 90.0) / 180.0;
123            (xn.clamp(0.05, 0.95), yn.clamp(0.05, 0.95), 0.15)
124        } else {
125            // Golden-angle disk → slight depth for spatial field (prestige morph).
126            let i = field_i as f64;
127            let tot = field_total as f64;
128            field_i += 1;
129            let golden = std::f64::consts::PI * (3.0 - 5.0_f64.sqrt());
130            let r = ((i + 0.5) / tot).sqrt() * 0.38;
131            let theta = i * golden;
132            let xn = 0.5 + r * theta.cos();
133            let yn = 0.5 + r * theta.sin() * 0.72;
134            let zn = 0.2 + (i / tot) * 0.55;
135            (
136                xn.clamp(0.08, 0.92),
137                yn.clamp(0.08, 0.92),
138                zn.clamp(0.05, 0.95),
139            )
140        };
141        let bits = inp.affordances.pack();
142        let radius = 5.5
143            + if inp.affordances.can_edit { 2.0 } else { 0.0 }
144            + if inp.affordances.can_share { 1.0 } else { 0.0 };
145        out[n] = SceneNodeProj {
146            entity_id: inp.entity_id.raw(),
147            id: format!("{:016x}", inp.entity_id.raw()),
148            x,
149            y,
150            z,
151            color: wing_color(inp.wing).into(),
152            radius,
153            alpha: (0.5 + z * 0.5).clamp(0.45, 1.0),
154            affordance_bits: bits,
155        };
156        n += 1;
157    }
158    n
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164    use crate::entity_view::entity_id::EntityId;
165    use crate::entity_view::observer::AffordanceBits;
166
167    #[test]
168    fn layout_uses_geo_when_present() {
169        let inputs = [LayoutInput {
170            entity_id: EntityId::from_uri("urn:pin"),
171            lat: Some(0.0),
172            lon: Some(0.0),
173            affordances: AffordanceBits::FULL,
174            wing: RepresentationWing::Commons,
175        }];
176        let mut out: Vec<SceneNodeProj> = (0..2)
177            .map(|_| SceneNodeProj {
178                entity_id: 0,
179                id: String::new(),
180                x: 0.0,
181                y: 0.0,
182                z: 0.0,
183                color: String::new(),
184                radius: 0.0,
185                alpha: 0.0,
186                affordance_bits: 0,
187            })
188            .collect();
189        let n = layout_scene_nodes(&inputs, &mut out);
190        assert_eq!(n, 1);
191        assert!((out[0].x - 0.5).abs() < 0.01);
192    }
193}