qualia_core_db/inference/spatial_sieve.rs
1/// Minkowski Spatial Sieve for Android Asset Apportionment
2///
3/// This module provides the structural foundation for passing spatial logs
4/// and bounding boxes to a GPU Compute Shader (e.g., via wgpu/Vulkan).
5/// It calculates whether a specific financial transaction occurred within the
6/// Minkowski spatial overlap of a tracked Asset (e.g., a Work Vehicle or Business Location).
7
8#[derive(Clone, Copy)]
9pub struct GeoCoordinate {
10 pub lat: f64,
11 pub lng: f64,
12 pub timestamp_ms: u64,
13}
14
15#[derive(Clone, Copy)]
16pub struct BoundingBox {
17 pub min_lat: f64,
18 pub min_lng: f64,
19 pub max_lat: f64,
20 pub max_lng: f64,
21}
22
23/// A mock vectorised representation of the GPU-accelerated Minkowski overlap check.
24/// In a production environment with `wgpu`, this would dispatch a compute shader
25/// that performs matrix multiplications over thousands of GeoCoordinates instantly.
26pub fn compute_spatial_overlap_gpu_mock(
27 route_log: &[GeoCoordinate],
28 asset_bounds: BoundingBox,
29) -> f64 {
30 if route_log.is_empty() {
31 return 0.0;
32 }
33
34 let mut overlap_count = 0;
35
36 // Vectorized check representation
37 for point in route_log {
38 if point.lat >= asset_bounds.min_lat
39 && point.lat <= asset_bounds.max_lat
40 && point.lng >= asset_bounds.min_lng
41 && point.lng <= asset_bounds.max_lng
42 {
43 overlap_count += 1;
44 }
45 }
46
47 // Return the apportionment ratio (e.g. 50% of the trip was inside the asset bounds)
48 overlap_count as f64 / route_log.len() as f64
49}
50
51/// Encode a GPS fix into two `SPATIAL_CONTEXT` quins and return them.
52///
53/// Quin 1: `subject=geohash64 | predicate=P_HAS_GEOMETRY | object=geohash64`
54/// Quin 2: `subject=geohash64 | predicate=P_GENERATED_AT | object=ts`
55///
56/// The GeoHash-64 value is bit-interleaved lon/lat (32 bits each) computed
57/// by `kml_bridge::encode_geohash_64`. Callers write the returned quins to
58/// storage; this function never allocates a graph writer itself.
59pub fn log_spatial_coordinate(lat: f64, lng: f64, ts: u64) -> [crate::NQuin; 2] {
60 use crate::kml_bridge::{encode_geohash_64, P_GENERATED_AT, P_HAS_GEOMETRY, SPATIAL_CONTEXT};
61 let geohash = encode_geohash_64(lng, lat);
62 [
63 crate::NQuin {
64 subject: geohash,
65 predicate: P_HAS_GEOMETRY,
66 object: geohash,
67 context: SPATIAL_CONTEXT,
68 metadata: ts & 0xFFFF_FFFF,
69 parity: 0,
70 },
71 crate::NQuin {
72 subject: geohash,
73 predicate: P_GENERATED_AT,
74 object: ts,
75 context: SPATIAL_CONTEXT,
76 metadata: ts & 0xFFFF_FFFF,
77 parity: 0,
78 },
79 ]
80}
81
82/// Evaluates a recent spatial log against a claimed jurisdictional boundary
83/// (e.g., verifying the user is actually physically in Australia to prevent
84/// foreign scammer activity on a specific project or transaction).
85pub fn verify_proof_of_location(
86 recent_log: GeoCoordinate,
87 jurisdiction_bounds: BoundingBox,
88) -> bool {
89 recent_log.lat >= jurisdiction_bounds.min_lat
90 && recent_log.lat <= jurisdiction_bounds.max_lat
91 && recent_log.lng >= jurisdiction_bounds.min_lng
92 && recent_log.lng <= jurisdiction_bounds.max_lng
93}