Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a feature to serialize LinearTrajectory2d #16

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mapf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ slotmap = "1.0"

[dev-dependencies]
approx = "*"

[features]
serde = ["nalgebra/serde-serialize", "time-point/serde"]
72 changes: 71 additions & 1 deletion mapf/src/motion/se2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl Velocity {
}

pub mod timed_position;
pub use timed_position::*;

pub use timed_position::*;
pub mod space;
pub use space::*;

Expand All @@ -47,8 +47,78 @@ pub use oriented::*;

pub type LinearTrajectorySE2 = super::Trajectory<WaypointSE2>;

#[cfg(feature = "serde")]
use serde::de::{Deserializer, Error, SeqAccess, Visitor};
#[cfg(feature = "serde")]
use serde::ser::{Serialize, SerializeSeq, Serializer};

#[cfg(feature = "serde")]
impl serde::Serialize for LinearTrajectorySE2 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.len()))?;
for e in self.iter() {
seq.serialize_element(&e)?;
}
seq.end()
}
}

#[cfg(feature = "serde")]
struct SE2TrajectoryVisitor;

#[cfg(feature = "serde")]
impl<'de> Visitor<'de> for SE2TrajectoryVisitor {
type Value = LinearTrajectorySE2;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("SE2 key value sequence.")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
let start = if let Some(wp) = seq.next_element::<WaypointSE2>()? {
Some(wp)
} else {
None
};

let second = if let Some(wp) = seq.next_element::<WaypointSE2>()? {
Some(wp)
} else {
None
};

if start.is_some() && second.is_some() {
if let Ok(mut traj) = Trajectory::new(start.unwrap(), second.unwrap()) {
while let Some(wp) = seq.next_element::<WaypointSE2>()? {
traj.insert(wp);
}
return Ok(traj);
}
}
return Err(A::Error::custom("Trajectory needs at least 2 points"));
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for LinearTrajectorySE2 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_seq(SE2TrajectoryVisitor)
}
}

pub mod quickest_path;
pub use quickest_path::{QuickestPathHeuristic, QuickestPathPlanner};

pub mod differential_drive_line_follow;
pub use differential_drive_line_follow::*;

use super::Trajectory;
1 change: 1 addition & 0 deletions mapf/src/motion/se2/timed_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
use arrayvec::ArrayVec;

#[derive(Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WaypointSE2 {
pub time: TimePoint,
pub position: Position,
Expand Down