use crate::{
graph::occupancy::Cell,
motion::{
se2::{GoalSE2, Orientation, StartSE2, WaypointSE2},
TimePoint, Trajectory,
},
};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
pub type LinearTrajectorySE2 = Trajectory<WaypointSE2>;
#[derive(Serialize, Deserialize, Clone, Debug, Copy)]
pub struct Agent {
pub start: [i64; 2],
pub yaw: f64,
pub goal: [i64; 2],
#[serde(default = " default_radius")]
pub radius: f64,
#[serde(default = "default_speed")]
pub speed: f64,
#[serde(default = "default_spin")]
pub spin: f64,
}
impl Agent {
pub fn start_cell(&self) -> Cell {
self.start.into()
}
pub fn goal_cell(&self) -> Cell {
self.goal.into()
}
pub fn make_start(&self) -> StartSE2<Cell> {
StartSE2 {
time: TimePoint::zero(),
key: Cell::from(self.start),
orientation: Orientation::from_angle(self.yaw),
}
}
pub fn make_goal(&self) -> GoalSE2<Cell> {
GoalSE2::new(Cell::from(self.goal))
}
}
#[derive(Serialize, Deserialize)]
pub struct Obstacle {
pub trajectory: Vec<(f64, i64, i64)>,
#[serde(default = "default_radius")]
pub radius: f64,
#[serde(default = "bool_false", skip_serializing_if = "is_false")]
pub indefinite_start: bool,
#[serde(default = "bool_false", skip_serializing_if = "is_false")]
pub indefinite_finish: bool,
}
impl Obstacle {
pub fn new(radius: f64, trajectory: &LinearTrajectorySE2, cell_size: f64) -> Obstacle {
Obstacle {
trajectory: trajectory
.iter()
.map(|wp| {
let cell = Cell::from_point(wp.position.translation.vector.into(), cell_size);
(wp.time.as_secs_f64(), cell.x, cell.y)
})
.collect(),
radius,
indefinite_start: trajectory.has_indefinite_initial_time(),
indefinite_finish: trajectory.has_indefinite_finish_time(),
}
}
}
#[derive(Serialize, Deserialize)]
pub struct Scenario {
pub agents: BTreeMap<String, Agent>,
pub obstacles: Vec<Obstacle>,
pub occupancy: HashMap<i64, Vec<i64>>,
#[serde(default = "default_cell_size")]
pub cell_size: f64,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub camera_bounds: Option<[[f32; 2]; 2]>,
}
pub fn default_radius() -> f64 {
0.45
}
pub fn default_speed() -> f64 {
0.75
}
pub fn default_spin() -> f64 {
60_f64.to_radians()
}
pub fn default_cell_size() -> f64 {
1.0
}
pub fn bool_false() -> bool {
false
}
pub fn is_false(b: &bool) -> bool {
!b
}