use rand_core::Rng;
use super::so2::{angle_difference, interpolate_angle, uniform_angle};
use crate::{Sampler, StateSpace, UniformBox};
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Se2State {
pub x: f64,
pub y: f64,
pub theta: f64,
}
impl Se2State {
#[must_use]
pub const fn new(x: f64, y: f64, theta: f64) -> Self {
Self { x, y, theta }
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Se2 {
rotation_weight: f64,
}
impl Se2 {
#[must_use]
pub fn new(rotation_weight: f64) -> Self {
assert!(
rotation_weight.is_finite() && rotation_weight > 0.0,
"rotation weight must be positive and finite"
);
Self { rotation_weight }
}
#[must_use]
pub const fn rotation_weight(&self) -> f64 {
self.rotation_weight
}
}
impl StateSpace for Se2 {
type State = Se2State;
fn dimension(&self) -> usize {
3
}
fn distance(&self, from: &Se2State, to: &Se2State) -> f64 {
let position = libm::hypot(to.x - from.x, to.y - from.y);
let rotation = libm::fabs(angle_difference(from.theta, to.theta));
position + self.rotation_weight * rotation
}
fn interpolate(&self, from: &Se2State, to: &Se2State, t: f64) -> Se2State {
if t <= 0.0 {
return *from;
}
if t >= 1.0 {
return *to;
}
Se2State {
x: from.x + (to.x - from.x) * t,
y: from.y + (to.y - from.y) * t,
theta: interpolate_angle(from.theta, to.theta, t),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct UniformSe2 {
position: UniformBox<2>,
}
impl UniformSe2 {
#[must_use]
pub fn new(lower: [f64; 2], upper: [f64; 2]) -> Self {
Self {
position: UniformBox::new(lower, upper),
}
}
#[must_use]
pub fn contains(&self, state: &Se2State) -> bool {
self.position.contains(&[state.x, state.y])
}
}
impl Sampler<Se2State> for UniformSe2 {
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Se2State {
let [x, y] = self.position.sample(rng);
Se2State {
x,
y,
theta: uniform_angle(rng),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::f64::consts::PI;
use rand_core::SeedableRng;
#[test]
fn interpolation_is_linear_in_position_and_short_in_heading() {
let space = Se2::new(1.0);
let a = Se2State::new(0.0, 0.0, 3.0);
let b = Se2State::new(2.0, -2.0, -3.0);
let mid = space.interpolate(&a, &b, 0.5);
assert_eq!((mid.x, mid.y), (1.0, -1.0));
assert!((libm::fabs(mid.theta) - PI).abs() < 1e-12);
assert!((space.distance(&a, &mid) - 0.5 * space.distance(&a, &b)).abs() < 1e-12);
assert_eq!(space.interpolate(&a, &b, 0.0), a);
assert_eq!(space.interpolate(&a, &b, 1.0), b);
}
#[test]
fn distance_is_a_metric_on_samples() {
let space = Se2::new(0.7);
let mut sampler = UniformSe2::new([-5.0, -5.0], [5.0, 5.0]);
let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(2);
for _ in 0..1000 {
let [a, b, c] = [(); 3].map(|()| sampler.sample(&mut rng));
assert!(sampler.contains(&a) && (-PI..PI).contains(&a.theta));
assert!((space.distance(&a, &b) - space.distance(&b, &a)).abs() < 1e-12);
assert!(
space.distance(&a, &c) <= space.distance(&a, &b) + space.distance(&b, &c) + 1e-9
);
}
let a = Se2State::new(1.0, 1.0, 1.0);
assert!(space.distance(&a, &a).abs() < f64::EPSILON);
}
#[test]
#[should_panic(expected = "rotation weight")]
fn rejects_non_positive_weights() {
let _ = Se2::new(0.0);
}
}