sim-lib-view-spatial 0.1.1

Spatial glasses surface encoding over the shared SIM Web Scene and device adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Pose-free spatial layout parsing and Scene builders.

use sim_kernel::{Error, Expr, Result, Symbol};
use sim_lib_scene::{Anchor, AnchorSpace, Transform3};
use sim_table_core::{TableOp, TablePath};
use sim_value::{access, build};

/// Namespace for persisted glasses workspace layout records.
pub const WORKSPACE_LAYOUT_NAMESPACE: &str = "workspace";

/// Kind name for persisted glasses workspace layout records.
pub const WORKSPACE_LAYOUT_KIND: &str = "layout";

/// Namespace used for table keys that store glasses workspace layouts.
pub const WORKSPACE_LAYOUT_TABLE_NAMESPACE: &str = "workspace-layout";

const DEFAULT_LAYOUT_KEY: &str = "default";
const DEFAULT_WORLD_ANCHOR: &str = "workspace";

/// A spatial layout made of anchored panels.
#[derive(Clone, Debug, PartialEq)]
pub struct SpatialLayout {
    panels: Vec<PanelLayout>,
}

impl SpatialLayout {
    /// Builds the default single-panel layout.
    pub fn single_panel() -> Self {
        Self {
            panels: vec![PanelLayout::default()],
        }
    }

    /// Builds the default Viture workspace arc.
    pub fn default_arc() -> Self {
        WorkspaceLayout::default_arc().to_spatial_layout()
    }

    /// Parses a layout expression.
    ///
    /// A layout is a map with an optional `panels` list. Each panel may carry
    /// `id`, `anchor`, and `transform` fields. Missing fields use stable
    /// defaults so a value can be encoded spatially without layout metadata.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        reject_runtime_fields(expr)?;
        let Some(Expr::List(items) | Expr::Vector(items)) = access::field(expr, "panels") else {
            return Ok(Self::single_panel());
        };
        if items.is_empty() {
            return Ok(Self::single_panel());
        }
        let panels = items
            .iter()
            .enumerate()
            .map(|(index, item)| PanelLayout::from_expr(index, item))
            .collect::<Result<Vec<_>>>()?;
        Ok(Self { panels })
    }

    /// Returns the panel layouts in order.
    pub fn panels(&self) -> &[PanelLayout] {
        &self.panels
    }
}

/// Persisted co-use layout shared by the Viture workspace and Halo glance path.
#[derive(Clone, Debug, PartialEq)]
pub struct WorkspaceLayout {
    panels: Vec<PanelPlacement>,
    glance: GlancePreference,
}

impl WorkspaceLayout {
    /// Builds the default one-panel Viture arc with urgency-first Halo glance.
    pub fn default_arc() -> Self {
        Self {
            panels: vec![
                PanelPlacement::new(
                    Symbol::new("main"),
                    AnchorSpace::World,
                    Transform3::new([0.0, 1.2, -1.6], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0]),
                )
                .with_world_anchor(Symbol::new(DEFAULT_WORLD_ANCHOR)),
            ],
            glance: GlancePreference::default(),
        }
    }

    /// Builds a workspace layout from explicit placements and glance preference.
    pub fn new(panels: Vec<PanelPlacement>, glance: GlancePreference) -> Result<Self> {
        if panels.is_empty() {
            return Err(Error::HostError(
                "workspace/layout must contain at least one panel".to_owned(),
            ));
        }
        Ok(Self { panels, glance })
    }

    /// Returns Viture panel placements in render order.
    pub fn panels(&self) -> &[PanelPlacement] {
        &self.panels
    }

    /// Returns the Halo glance preference stored with the workspace layout.
    pub fn glance(&self) -> &GlancePreference {
        &self.glance
    }

    /// Encodes the layout as a portable SIM `Expr`.
    pub fn to_expr(&self) -> Expr {
        build::map(vec![
            (
                "kind",
                Expr::Symbol(Symbol::qualified(
                    WORKSPACE_LAYOUT_NAMESPACE,
                    WORKSPACE_LAYOUT_KIND,
                )),
            ),
            (
                "panels",
                build::list(self.panels.iter().map(PanelPlacement::to_expr).collect()),
            ),
            ("glance", self.glance.to_expr()),
        ])
    }

    /// Decodes a portable SIM `Expr` into a workspace layout.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        reject_runtime_fields(expr)?;
        expect_kind(expr, WORKSPACE_LAYOUT_KIND, "workspace/layout")?;
        let panels_expr = access::required(expr, "panels", "workspace/layout")?;
        let panels = match panels_expr {
            Expr::List(items) | Expr::Vector(items) => items
                .iter()
                .map(PanelPlacement::from_expr)
                .collect::<Result<Vec<_>>>()?,
            _ => {
                return Err(Error::HostError(
                    "workspace/layout panels field must be a list".to_owned(),
                ));
            }
        };
        let glance = access::field(expr, "glance")
            .map(GlancePreference::from_expr)
            .transpose()?
            .unwrap_or_default();
        Self::new(panels, glance)
    }

    fn to_spatial_layout(&self) -> SpatialLayout {
        SpatialLayout {
            panels: self
                .panels
                .iter()
                .map(PanelPlacement::to_panel_layout)
                .collect(),
        }
    }
}

/// A persisted Viture panel placement.
#[derive(Clone, Debug, PartialEq)]
pub struct PanelPlacement {
    /// Stable panel id.
    pub panel_id: Symbol,
    /// Pose-free coordinate space for the panel.
    pub space: AnchorSpace,
    /// Static transform applied before device pose.
    pub transform: Transform3,
    /// Optional stable world anchor id.
    pub world_anchor: Option<Symbol>,
}

impl PanelPlacement {
    /// Builds a placement without a world anchor.
    pub fn new(panel_id: Symbol, space: AnchorSpace, transform: Transform3) -> Self {
        Self {
            panel_id,
            space,
            transform,
            world_anchor: None,
        }
    }

    /// Attaches a stable world anchor id.
    pub fn with_world_anchor(mut self, world_anchor: Symbol) -> Self {
        self.world_anchor = Some(world_anchor);
        self
    }

    /// Encodes the placement as portable workspace layout data.
    pub fn to_expr(&self) -> Expr {
        let mut fields = vec![
            (
                "kind",
                Expr::Symbol(Symbol::qualified(
                    WORKSPACE_LAYOUT_NAMESPACE,
                    "panel-placement",
                )),
            ),
            ("panel-id", Expr::Symbol(self.panel_id.clone())),
            ("space", self.space.to_expr()),
            ("transform", self.transform.to_expr()),
        ];
        if let Some(anchor) = &self.world_anchor {
            fields.push(("world-anchor", Expr::Symbol(anchor.clone())));
        }
        build::map(fields)
    }

    /// Decodes one persisted panel placement.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        reject_runtime_fields(expr)?;
        expect_kind(expr, "panel-placement", "workspace/panel-placement")?;
        let panel_id = access::required_sym(expr, "panel-id", "workspace/panel-placement")?;
        let space = AnchorSpace::from_expr(access::required(
            expr,
            "space",
            "workspace/panel-placement",
        )?)?;
        let transform = Transform3::from_expr(access::required(
            expr,
            "transform",
            "workspace/panel-placement",
        )?)?;
        Ok(Self {
            panel_id,
            space,
            transform,
            world_anchor: access::field_sym(expr, "world-anchor"),
        })
    }

    fn to_panel_layout(&self) -> PanelLayout {
        let target = self
            .world_anchor
            .as_ref()
            .map(Symbol::as_qualified_str)
            .unwrap_or_else(|| DEFAULT_WORLD_ANCHOR.to_owned());
        PanelLayout {
            id: self.panel_id.as_qualified_str(),
            anchor: Anchor::new(self.space, target),
            transform: self.transform.clone(),
        }
    }
}

/// Halo glance selection policy stored with a workspace layout.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum GlancePreference {
    /// Keep the shared glance reducer's urgency-first selection.
    #[default]
    UrgencyFirst,
    /// Prefer a particular item class when the host reducer supports it.
    ItemClass(Symbol),
}

impl GlancePreference {
    /// Builds a preference for an item class.
    pub fn item_class(class: Symbol) -> Self {
        Self::ItemClass(class)
    }

    /// Returns the preferred item class, if one is set.
    pub fn preferred_item_class(&self) -> Option<&Symbol> {
        match self {
            Self::UrgencyFirst => None,
            Self::ItemClass(class) => Some(class),
        }
    }

    /// Encodes the preference as portable workspace layout data.
    pub fn to_expr(&self) -> Expr {
        let mut fields = vec![
            (
                "kind",
                Expr::Symbol(Symbol::qualified(
                    WORKSPACE_LAYOUT_NAMESPACE,
                    "glance-preference",
                )),
            ),
            (
                "mode",
                build::sym(match self {
                    Self::UrgencyFirst => "urgency-first",
                    Self::ItemClass(_) => "item-class",
                }),
            ),
        ];
        if let Self::ItemClass(class) = self {
            fields.push(("item-class", Expr::Symbol(class.clone())));
        }
        build::map(fields)
    }

    /// Decodes a Halo glance preference.
    pub fn from_expr(expr: &Expr) -> Result<Self> {
        reject_runtime_fields(expr)?;
        expect_kind(expr, "glance-preference", "workspace/glance-preference")?;
        let mode = access::required_sym(expr, "mode", "workspace/glance-preference")?;
        if mode.namespace.is_some() {
            return Err(Error::HostError(
                "workspace/glance-preference mode must be unqualified".to_owned(),
            ));
        }
        match mode.name.as_ref() {
            "urgency-first" => Ok(Self::UrgencyFirst),
            "item-class" => Ok(Self::ItemClass(access::required_sym(
                expr,
                "item-class",
                "workspace/glance-preference",
            )?)),
            other => Err(Error::HostError(format!(
                "unknown workspace/glance-preference mode {other}"
            ))),
        }
    }
}

/// A single anchored panel in a spatial layout.
#[derive(Clone, Debug, PartialEq)]
pub struct PanelLayout {
    /// Stable panel id.
    pub id: String,
    /// Pose-free anchor for the panel.
    pub anchor: Anchor,
    /// Static transform applied at render time.
    pub transform: Transform3,
}

impl Default for PanelLayout {
    fn default() -> Self {
        Self {
            id: "panel-0".to_owned(),
            anchor: Anchor::new(AnchorSpace::World, "workspace"),
            transform: Transform3::identity(),
        }
    }
}

impl PanelLayout {
    fn from_expr(index: usize, expr: &Expr) -> Result<Self> {
        reject_runtime_fields(expr)?;
        let default = Self {
            id: format!("panel-{index}"),
            ..Self::default()
        };
        Ok(Self {
            id: access::field_str(expr, "id")
                .map(str::to_owned)
                .unwrap_or(default.id),
            anchor: access::field(expr, "anchor")
                .map(Anchor::from_expr)
                .transpose()?
                .unwrap_or(default.anchor),
            transform: access::field(expr, "transform")
                .map(Transform3::from_expr)
                .transpose()?
                .unwrap_or(default.transform),
        })
    }
}

/// Returns optional spatial layout metadata embedded in a value.
pub fn layout_expr(value: &Expr) -> Option<&Expr> {
    access::field(value, "workspace-layout")
        .or_else(|| access::field(value, "spatial-workspace-layout"))
        .or_else(|| access::field(value, "spatial-layout"))
        .or_else(|| access::field(value, "layout"))
}

/// Wraps a flat Scene in a pose-free `scene/spatial` panel layout.
pub fn arrange_spatial_panels(scene: Expr, layout: Option<&Expr>) -> Result<Expr> {
    let layout = layout
        .map(spatial_layout_from_expr)
        .transpose()?
        .unwrap_or_else(SpatialLayout::default_arc);
    let panels = layout
        .panels()
        .iter()
        .map(|panel| {
            sim_lib_scene::panel(
                panel.id.clone(),
                scene.clone(),
                panel.anchor.clone(),
                panel.transform.clone(),
            )
        })
        .collect();
    Ok(sim_lib_scene::spatial(panels))
}

/// Builds the table key used for persisted workspace layouts.
pub fn layout_table_key(path: &TablePath) -> Symbol {
    let key = if path.segments().is_empty() {
        DEFAULT_LAYOUT_KEY.to_owned()
    } else {
        path.segments().join(".")
    };
    Symbol::qualified(WORKSPACE_LAYOUT_TABLE_NAMESPACE, key)
}

/// Builds a `table/set` operation storing `layout` at `path`.
pub fn layout_save_op(path: &TablePath, layout: &WorkspaceLayout) -> TableOp {
    TableOp::Set(layout_table_key(path), layout.to_expr())
}

/// Builds a `table/get` operation loading the layout stored at `path`.
pub fn layout_load_op(path: &TablePath) -> TableOp {
    TableOp::Get(layout_table_key(path))
}

fn spatial_layout_from_expr(expr: &Expr) -> Result<SpatialLayout> {
    if access::field(expr, "kind").is_some() {
        WorkspaceLayout::from_expr(expr).map(|layout| layout.to_spatial_layout())
    } else {
        SpatialLayout::from_expr(expr)
    }
}

fn expect_kind(expr: &Expr, name: &str, context: &str) -> Result<()> {
    let kind = access::required_sym(expr, "kind", context)?;
    if kind.namespace.as_deref() == Some(WORKSPACE_LAYOUT_NAMESPACE) && kind.name.as_ref() == name {
        Ok(())
    } else {
        Err(Error::HostError(format!(
            "{context} kind must be {WORKSPACE_LAYOUT_NAMESPACE}/{name}"
        )))
    }
}

fn reject_runtime_fields(expr: &Expr) -> Result<()> {
    match expr {
        Expr::Map(entries) => {
            for (key, value) in entries {
                if is_runtime_key(key) {
                    return Err(Error::HostError(
                        "spatial layout must not carry runtime tracking fields".to_owned(),
                    ));
                }
                reject_runtime_fields(value)?;
            }
        }
        Expr::List(items) | Expr::Vector(items) | Expr::Set(items) => {
            for item in items {
                reject_runtime_fields(item)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn is_runtime_key(key: &Expr) -> bool {
    matches!(
        key,
        Expr::Symbol(symbol)
            if symbol.namespace.is_none() && matches!(symbol.name.as_ref(), "pose" | "tick")
    )
}