Skip to main content

qualia_core_db/domains/geospatial/
scale_continuum.rs

1//! Scale-Continuum Engine (Phase 1.5)
2//!
3//! Manages scale-driven LOD transitions across the macro-verse (Earth) and
4//! micro-verse (anatomy/cellular), leveraging the hierarchical ReferenceFrame.
5
6use crate::domains::geospatial::reference_frame::ReferenceFrame;
7
8/// Determines if a scale transition is required based on the current camera scale
9/// factor and the active reference frame.
10///
11/// Threshold heuristic: If the observer zooms in by more than 1000x relative
12/// to the frame's base scale, we should transition to a child frame (micro-verse).
13pub fn requires_micro_transition(camera_scale: f64, active_frame: &ReferenceFrame) -> bool {
14    let scale_ratio = camera_scale / active_frame.scale;
15    scale_ratio > 1000.0
16}
17
18/// Evaluates cross-scale RCC-8 (Region Connection Calculus) containment.
19///
20/// True if a target coordinate in a given reference frame is structurally
21/// "Inside" or "CoveredBy" the region defined by the parent frame.
22///
23/// Zero-heap constraint: Uses stack allocations and bounded iterations.
24pub fn is_contained_in_parent(
25    local_pt: [f64; 3],
26    frame: &ReferenceFrame,
27    parent_bounds_min: [f64; 3],
28    parent_bounds_max: [f64; 3],
29) -> bool {
30    let global_pt = frame.transform_to_parent(local_pt);
31
32    global_pt[0] >= parent_bounds_min[0]
33        && global_pt[0] <= parent_bounds_max[0]
34        && global_pt[1] >= parent_bounds_min[1]
35        && global_pt[1] <= parent_bounds_max[1]
36        && global_pt[2] >= parent_bounds_min[2]
37        && global_pt[2] <= parent_bounds_max[2]
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::domains::geospatial::reference_frame::ReferenceFrame;
44
45    #[test]
46    fn test_requires_micro_transition() {
47        let frame = ReferenceFrame::new(1, None); // default scale is 1.0
48
49        assert!(!requires_micro_transition(100.0, &frame));
50        assert!(requires_micro_transition(1001.0, &frame));
51    }
52
53    #[test]
54    fn test_is_contained_in_parent() {
55        let mut frame = ReferenceFrame::new(2, Some(1));
56        frame.translation = [10.0, 10.0, 10.0];
57        frame.scale = 0.1;
58
59        let local_pt = [5.0, 0.0, 0.0];
60        // global pt = [10.5, 10.0, 10.0]
61
62        let min_bounds = [0.0, 0.0, 0.0];
63        let max_bounds = [20.0, 20.0, 20.0];
64
65        assert!(is_contained_in_parent(
66            local_pt, &frame, min_bounds, max_bounds
67        ));
68
69        // Outside parent bounds
70        let out_bounds = [0.0, 0.0, 1.0];
71        assert!(!is_contained_in_parent(
72            local_pt, &frame, min_bounds, out_bounds
73        ));
74    }
75}