qualia_core_db/poet_host/invoke/manifold/
project.rs1use super::super::args;
4use crate::entity_view::projection::PresentationLevel;
5use vibe::{Diagnostic, Span, Value};
6
7pub fn project(args_v: &Value, _span: Span) -> Result<Value, Diagnostic> {
11 let x = args::rec_f64(args_v, "x").unwrap_or(0.0);
12 let y = args::rec_f64(args_v, "y").unwrap_or(0.0);
13 let z = args::rec_f64(args_v, "z").unwrap_or(0.0);
14 let t = args::rec_f64(args_v, "t").unwrap_or(0.0);
15 let fov_deg = args::rec_f64(args_v, "fov").unwrap_or(60.0);
16 let cam_dist = args::rec_f64(args_v, "distance").unwrap_or(5.0).max(0.1);
17 let level_u8 = args::rec_u64(args_v, "level").unwrap_or(2) as u8;
18 let level = PresentationLevel::from_u8(level_u8);
19 let id = args::rec_u64(args_v, "entity").unwrap_or(0);
20
21 let (proj_x, proj_y, proj_z, depth_scale, visible) = match level {
23 PresentationLevel::Document | PresentationLevel::AppHabitat => {
24 (x, y, 0.0, 1.0, true)
26 }
27 PresentationLevel::SpatialDesk => {
28 let theta = 30.0_f64.to_radians();
30 let cos_t = theta.cos();
31 let sin_t = theta.sin();
32 let y_tilted = y * cos_t - z * sin_t;
33 let z_depth = y * sin_t + z * cos_t;
34 let scale = cam_dist / (cam_dist + z_depth).max(0.01);
35 (
36 x * scale,
37 y_tilted * scale,
38 z_depth,
39 scale,
40 z_depth > -cam_dist,
41 )
42 }
43 PresentationLevel::StageWorld | PresentationLevel::EmbodiedWorld => {
44 let focal = 1.0 / (fov_deg.to_radians() * 0.5).tan();
46 let eye_z = z + cam_dist;
47 if eye_z <= 0.01 {
48 (x, y, eye_z, 0.0, false)
50 } else {
51 let scale = focal / eye_z;
52 (x * scale, y * scale, z, scale, true)
53 }
54 }
55 PresentationLevel::Infosphere | PresentationLevel::MultiSensory => {
56 let focal = 1.0 / (fov_deg.to_radians() * 0.5).tan();
58 let t_phase = (t * 0.1).sin() * 0.05;
59 let eye_z = z + cam_dist + t_phase;
60 let scale = if eye_z > 0.01 { focal / eye_z } else { 1.0 };
61 (x * scale, y * scale, z, scale, eye_z > 0.01)
62 }
63 };
64
65 Ok(args::record([
66 ("entity_id", Value::U64(id)),
67 ("x", Value::F64(proj_x)),
68 ("y", Value::F64(proj_y)),
69 ("z", Value::F64(proj_z)),
70 ("t", Value::F64(t)),
71 ("depth_scale", Value::F64(depth_scale)),
72 ("visible", Value::Bool(visible)),
73 ("level", Value::U64(level.as_u8() as u64)),
74 ("level_name", Value::String(format!("{level:?}"))),
75 (
76 "spatial",
77 Value::Bool(level.as_u8() >= PresentationLevel::SpatialDesk.as_u8()),
78 ),
79 ("honesty", Value::String("real".into())),
80 ]))
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86 use std::collections::BTreeMap;
87
88 #[test]
89 fn spatial_desk_is_spatial() {
90 let mut m = BTreeMap::new();
91 m.insert("level".into(), Value::U64(2));
92 match project(&Value::Record(m), Span { start: 0, end: 0 }).unwrap() {
93 Value::Record(r) => assert_eq!(r.get("spatial"), Some(&Value::Bool(true))),
94 other => panic!("{other:?}"),
95 }
96 }
97
98 #[test]
99 fn document_level_flattens_z_to_zero() {
100 let mut m = BTreeMap::new();
101 m.insert("level".into(), Value::U64(0));
102 m.insert("x".into(), Value::F64(10.0));
103 m.insert("y".into(), Value::F64(20.0));
104 m.insert("z".into(), Value::F64(30.0));
105 match project(&Value::Record(m), Span { start: 0, end: 0 }).unwrap() {
106 Value::Record(r) => {
107 assert_eq!(r.get("z"), Some(&Value::F64(0.0)));
108 assert_eq!(r.get("x"), Some(&Value::F64(10.0)));
109 assert_eq!(r.get("spatial"), Some(&Value::Bool(false)));
110 }
111 other => panic!("{other:?}"),
112 }
113 }
114
115 #[test]
116 fn manifold_project_honesty_is_real() {
117 let m = BTreeMap::new();
118 match project(&Value::Record(m), Span { start: 0, end: 0 }).unwrap() {
119 Value::Record(r) => assert_eq!(r.get("honesty"), Some(&Value::String("real".into()))),
120 other => panic!("{other:?}"),
121 }
122 }
123}