qualia_core_db/entity_view/
projection.rs1use super::entity_id::{EntityId, EntityKind};
4use super::observer::{AffordanceBits, RepresentationWing};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
9#[repr(u8)]
10pub enum PresentationLevel {
11 #[default]
13 Document = 0,
14 AppHabitat = 1,
16 SpatialDesk = 2,
18 StageWorld = 3,
20 EmbodiedWorld = 4,
22 Infosphere = 5,
24 MultiSensory = 6,
26}
27
28impl PresentationLevel {
29 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 pub fn as_u8(self) -> u8 {
45 self as u8
46 }
47}
48
49#[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#[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#[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#[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
96pub fn wing_color(wing: RepresentationWing) -> &'static str {
98 match wing {
99 RepresentationWing::Private => "#a78bfa", RepresentationWing::Offered => "#34d399", RepresentationWing::Commons => "#38bdf8", }
103}
104
105pub 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 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 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}