lnmp_spatial/
validate.rs

1use crate::error::SpatialError;
2use crate::types::*;
3
4pub struct SpatialConstraints {
5    pub max_velocity: f32,
6    pub max_acceleration: f32,
7    pub max_rotation_speed: f32, // TAU radians/second
8    pub max_coordinate: f32,
9}
10
11impl Default for SpatialConstraints {
12    fn default() -> Self {
13        Self {
14            max_velocity: 100.0,
15            max_acceleration: 50.0,
16            max_rotation_speed: std::f32::consts::TAU,
17            max_coordinate: 1000.0,
18        }
19    }
20}
21
22pub fn validate_spatial_state(
23    state: &SpatialState,
24    constraints: &SpatialConstraints,
25) -> Result<(), SpatialError> {
26    if let Some(pos) = &state.position {
27        if pos.x.abs() > constraints.max_coordinate
28            || pos.y.abs() > constraints.max_coordinate
29            || pos.z.abs() > constraints.max_coordinate
30        {
31            return Err(SpatialError::ValidationError(
32                "Position exceeds max coordinate value".into(),
33            ));
34        }
35    }
36
37    if let Some(vel) = &state.velocity {
38        let speed = (vel.vx * vel.vx + vel.vy * vel.vy + vel.vz * vel.vz).sqrt();
39        if speed > constraints.max_velocity {
40            return Err(SpatialError::ValidationError(format!(
41                "Velocity {} exceeds max {}",
42                speed, constraints.max_velocity
43            )));
44        }
45    }
46
47    if let Some(acc) = &state.acceleration {
48        let mag = (acc.ax * acc.ax + acc.ay * acc.ay + acc.az * acc.az).sqrt();
49        if mag > constraints.max_acceleration {
50            return Err(SpatialError::ValidationError(format!(
51                "Acceleration {} exceeds max {}",
52                mag, constraints.max_acceleration
53            )));
54        }
55    }
56
57    Ok(())
58}