Skip to main content

qualia_client_core/wellfair/api/
anatomy.rs

1//! 3D anatomy asset cache + physiological state
2
3use super::*;
4
5impl WebizenHostApi {
6    // --- 3D Anatomy asset cache (S5.8 — user-triggered real-mesh acquisition) -------------------
7    //
8    // The person triggers a download of the CCF/HRA reference-organ GLB set from the live SPARQL
9    // endpoint; the host fetches + compiles each to a sealed `.10d` and caches both under
10    // `{storage_root}/assets/ccf/{model}/`. Subsequent runs load the cached `.10d` directly — no
11    // re-download. The cache is the person's own, generated on demand.
12
13    /// Whether the body assets for a model are cached + complete (manifest exists + every referenced
14    /// `.10d` is on disk). `model` is `"male"` / `"female"` (case-insensitive).
15    pub fn body_assets_status(
16        &self,
17        model: &str,
18    ) -> Result<super::super::anatomy_assets::BodyAssetsStatus, String> {
19        let m = parse_anatomy_model(model)?;
20        Ok(super::super::anatomy_assets::status(&self.storage_root, m))
21    }
22
23    /// The cached organ keys for a model (empty if not cached).
24    pub fn cached_organ_keys(&self, model: &str) -> Result<Vec<String>, String> {
25        let m = parse_anatomy_model(model)?;
26        Ok(super::super::anatomy_assets::cached_organ_keys(
27            &self.storage_root,
28            m,
29        ))
30    }
31
32    /// Load a cached `.10d` for one organ. Returns the raw container bytes (for the browser portal's
33    /// `load_10d_colored`).
34    pub fn load_cached_organ_10d(&self, model: &str, organ_key: &str) -> Result<Vec<u8>, String> {
35        let m = parse_anatomy_model(model)?;
36        super::super::anatomy_assets::load_cached_10d(&self.storage_root, m, organ_key)
37    }
38
39    /// The per-organ dual-modality percepts for the cached organ set — so the browser portal knows what
40    /// colour to paint each organ (σ → RGBA via `paint_organs`). Returns `(painted, unmapped)`.
41    pub fn cached_body_organ_percepts(
42        &self,
43        model: &str,
44    ) -> Result<(Vec<super::super::anatomy_view::OrganPercept>, Vec<String>), String> {
45        let m = parse_anatomy_model(model)?;
46        let organ_keys = super::super::anatomy_assets::cached_organ_keys(&self.storage_root, m);
47        if organ_keys.is_empty() {
48            return Ok((Vec::new(), Vec::new()));
49        }
50        let report = self.compute_anatomy_view("person", 2)?;
51        let key_refs: Vec<&str> = organ_keys.iter().map(|s| s.as_str()).collect();
52        let (painted, unmapped) = report.paint_organs(&key_refs);
53        let fit = self.body_fit();
54        let painted = painted
55            .into_iter()
56            .filter(|p| !fit.hides(&p.organ_key))
57            .collect();
58        Ok((painted, unmapped))
59    }
60
61    /// The person's declared constitution, or an empty one if they have not authored it.
62    pub fn get_body_constitution(&self) -> wellfare_core::anatomy::BodyConstitution {
63        super::super::body_constitution::load(&self.storage_root).unwrap_or_default()
64    }
65
66    /// `true` when the body constitution is declared.
67    pub fn body_constitution_is_declared(&self) -> bool {
68        super::super::body_constitution::load(&self.storage_root).is_some()
69    }
70
71    /// Sets body-constitution.
72    pub fn set_body_constitution(
73        &self,
74        body: &wellfare_core::anatomy::BodyConstitution,
75    ) -> Result<(), String> {
76        super::super::body_constitution::save(&self.storage_root, body)
77    }
78
79    /// Resets the body constitution.
80    pub fn reset_body_constitution(&self) -> Result<(), String> {
81        super::super::body_constitution::clear(&self.storage_root)
82    }
83
84    /// View transform for the current constitution + physiological state.
85    pub fn body_fit(&self) -> wellfare_core::anatomy::BodyFit {
86        let constitution = self.get_body_constitution();
87        let phys = self.get_physiological_state();
88        super::super::body_constitution::fit_for(&constitution, &phys)
89    }
90
91    /// Clear the cache for a model (idempotent). The person can re-acquire later.
92    pub fn clear_body_cache(&self, model: &str) -> Result<(), String> {
93        let m = parse_anatomy_model(model)?;
94        super::super::anatomy_assets::clear_cache(&self.storage_root, m)
95    }
96
97    /// The accumulative, traceable **score-card** + investigable hypotheses over the person's own records —
98    /// the reading they can act on. Forum-internum / `Sanctuary`-class selfhood content; a set of
99    /// **hypotheses** and pathway-starts, never a diagnosis, never a rating. The card is computed at the
100    /// person's **declared physiological state** (their point on the reproductive continuum), or
101    /// `PhysiologicalState::Baseline()` if they have not declared one.
102    pub fn compute_scorecard(
103        &self,
104        convergence_threshold: usize,
105    ) -> Result<super::super::anatomy_view::WellbeingScorecardReport, String> {
106        let conditions = self.list_journal_by_kind("condition", 256)?;
107        let medications = self.list_journal_by_kind("medication", 256)?;
108        let diet = self.list_journal_by_kind("diet", 256)?;
109        // Read the person through **their own** weight model — their authorship of how they're read — falling
110        // back to the seed *suggestion* only if they have not authored one.
111        let weights = self.get_weight_model();
112        // Read the person at **their declared physiological state** — their own statement of where they are
113        // on the reproductive continuum — falling back to Baseline if they have not declared one.
114        let state = self.get_physiological_state();
115        Ok(
116            super::super::anatomy_view::build_scorecard_report_from_journal_with_weights(
117                &conditions,
118                &medications,
119                &diet,
120                convergence_threshold,
121                &weights,
122                state,
123            ),
124        )
125    }
126
127    /// The person's own score-card **weight model** — the interpretive lens the card uses — or the seed
128    /// *suggestion* if they have not authored one. Theirs to see, edit, or reset; the software offers a
129    /// starting point, it does not *define* how they are read.
130    pub fn get_weight_model(&self) -> wellfare_core::anatomy::WeightModel {
131        super::super::scorecard_prefs::load(&self.storage_root)
132            .unwrap_or_else(wellfare_core::anatomy::seed_weight_model)
133    }
134
135    /// The seed **suggestion** on its own — so a UI can show "this is the starting point; here's yours" and
136    /// let the person compare / adopt / edit.
137    pub fn seed_weight_model(&self) -> wellfare_core::anatomy::WeightModel {
138        wellfare_core::anatomy::seed_weight_model()
139    }
140
141    /// Whether the person has **authored their own** model (vs. still using the seed suggestion).
142    pub fn weight_model_is_authored(&self) -> bool {
143        super::super::scorecard_prefs::load(&self.storage_root).is_some()
144    }
145
146    /// **Set the person's own** weight model — their authorship of how the score-card reads them.
147    pub fn set_weight_model(
148        &self,
149        model: &wellfare_core::anatomy::WeightModel,
150    ) -> Result<(), String> {
151        super::super::scorecard_prefs::save(&self.storage_root, model)
152    }
153
154    /// **Reset** to the seed suggestion (clears the person's authored model — a choice, always reversible by
155    /// re-authoring).
156    pub fn reset_weight_model(&self) -> Result<(), String> {
157        super::super::scorecard_prefs::clear(&self.storage_root)
158    }
159
160    // --- Physiological state (P6 — the reproductive-continuum declaration) -----------------------
161    //
162    // The person's own statement of where they are on the reproductive continuum — their inward knowledge
163    // of their own body. Forum-internum / Sanctuary-class. The score-card is computed at this state so it
164    // reads them at their current life stage, not a neutral baseline.
165
166    /// The person's **declared** physiological state, or `PhysiologicalState::Baseline()` if they have not
167    /// declared one. Their own statement; the software never assumes.
168    pub fn get_physiological_state(&self) -> wellfare_core::anatomy::PhysiologicalState {
169        super::super::physiology_prefs::load(&self.storage_root)
170            .unwrap_or(wellfare_core::anatomy::PhysiologicalState::Baseline)
171    }
172
173    /// Whether the person has **declared** their physiological state (vs. still at the implicit baseline).
174    pub fn physiological_state_is_declared(&self) -> bool {
175        super::super::physiology_prefs::load(&self.storage_root).is_some()
176    }
177
178    /// **Set** the person's declared physiological state — their own statement of where they are on the
179    /// reproductive continuum. Forum-internum / Sanctuary-class.
180    pub fn set_physiological_state(
181        &self,
182        state: &wellfare_core::anatomy::PhysiologicalState,
183    ) -> Result<(), String> {
184        super::super::physiology_prefs::save(&self.storage_root, state)
185    }
186
187    /// **Clear** the declared state — revert to the implicit `PhysiologicalState::Baseline()`. Idempotent.
188    pub fn reset_physiological_state(&self) -> Result<(), String> {
189        super::super::physiology_prefs::clear(&self.storage_root)
190    }
191}