Skip to content

Commit

Permalink
feat(assets): junction_sdf back in action
Browse files Browse the repository at this point in the history
  • Loading branch information
jens-hj committed Mar 21, 2024
1 parent 096a4c1 commit 25b5b10
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 139 deletions.
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
# pixi environments
.pixi

*.png
*.dot

screenshot_*.png
# factorgraphs.png
# screenshot_*.png
**/*.png
!**/assets/**/*

.bacon-locations
Binary file added gbpplanner-rs/assets/imgs/junction_sdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions gbpplanner-rs/src/config/environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy::ecs::system::Resource;
use serde::{Deserialize, Serialize};

use super::geometry::Shape;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Grid(Vec<String>);
Expand Down Expand Up @@ -35,9 +37,9 @@ pub struct Cell {
pub col: usize,
}

// #[derive(Debug, Serialize, Deserialize)]
// #[serde(rename_all = "kebab-case")]
// pub struct shapes(Vec<(Cell, Shape)>);
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct shapes(Vec<(Cell, Shape)>);

/// **Bevy** [`Resource`]
/// The en
Expand Down
137 changes: 4 additions & 133 deletions gbpplanner-rs/src/config/formation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use serde::{Deserialize, Serialize};
use typed_floats::StrictlyPositiveFinite;
use unit_interval::UnitInterval;

use super::geometry::Shape;
use crate::{line, polygon};

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PlacementStrategy {
Expand All @@ -16,139 +19,6 @@ pub enum PlacementStrategy {
Map,
}

/// A relative point within the boudaries of the map.
/// ...
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct RelativePoint {
pub x: UnitInterval,
pub y: UnitInterval,
}

impl RelativePoint {
/// Create a new `RelativePoint` from a pair of values.
/// Returns an error if either `x` or `y` is not in the interval [0.0, 1.0].
pub fn new(x: f64, y: f64) -> Result<Self, unit_interval::UnitIntervalError> {
Ok(Self {
x: UnitInterval::new(x)?,
y: UnitInterval::new(y)?,
})
}

/// Returns the x and y values as a tuple
pub fn get(&self) -> (f64, f64) {
(self.x.get(), self.y.get())
}
}

impl TryFrom<(f64, f64)> for RelativePoint {
type Error = unit_interval::UnitIntervalError;

fn try_from(value: (f64, f64)) -> Result<Self, Self::Error> {
Ok(Self {
x: UnitInterval::new(value.0)?,
y: UnitInterval::new(value.1)?,
})
}
}

// #[derive(Debug, thiserror::Error)]
// pub enum PointError {
// #[error("x is out of bounds: {0}")]
// XOutOfBounds(#[from] unit_interval::UnitIntervalError),
// #[error("y is out of bounds: {0}")]
// YOutOfBounds(#[from] unit_interval::UnitIntervalError),
// #[error("both x and y are out of bounds: x: {0}, y: {1}")]
// BothOutOfBounds(f64, f64),
// }
//
// impl From<Point> for bevy::math::Vec2 {
// fn from(value: Point) -> Self {
// Self {
// x: value.x,
// y: value.y,
// }
// }
// }
//
// impl From<&Point> for bevy::math::Vec2 {
// fn from(value: &Point) -> Self {
// Self {
// x: value.x,
// y: value.y,
// }
// }
// }
//
// impl From<(f32, f32)> for Point {
// fn from(value: (f32, f32)) -> Self {
// Self {
// x: value.0,
// y: value.1,
// }
// }
// }

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Shape {
Circle {
radius: StrictlyPositiveFinite<f32>,
center: RelativePoint,
},
Polygon(OneOrMore<RelativePoint>),
Line((RelativePoint, RelativePoint)),
}

impl Shape {
/// Returns `true` if the shape is [`Polygon`].
///
/// [`Polygon`]: Shape::Polygon
#[must_use]
pub fn is_polygon(&self) -> bool {
matches!(self, Self::Polygon(..))
}

/// Returns `true` if the shape is [`Circle`].
///
/// [`Circle`]: Shape::Circle
#[must_use]
pub fn is_circle(&self) -> bool {
matches!(self, Self::Circle { .. })
}

pub fn as_polygon(&self) -> Option<&OneOrMore<RelativePoint>> {
if let Self::Polygon(v) = self {
Some(v)
} else {
None
}
}
}

/// Shorthand to construct `Shape::Polygon(vec![Point {x: $x, y: $y}, ... ])`
#[macro_export]
macro_rules! polygon {
[$(($x:expr, $y:expr)),+ $(,)?] => {{
let vertices = vec![
$(
RelativePoint::new($x, $y).expect("both x and y are within the interval [0.0, 1.0]")
),+
];
Shape::Polygon(OneOrMore::new(vertices).expect("at least one vertex"))
}}
}

/// Shorthand to construct `Shape::Line((Point {x: $x1, y: $y1}, Point {x: $x2,
/// y: $y2}))`
#[macro_export]
macro_rules! line {
[($x1:expr, $y1:expr), ($x2:expr, $y2:expr)] => {
// Shape::Line((Point { x: $x1, y: $y1 }, Point { x: $x2, y: $y2 }))
// Shape::Line((Point { x: ($x1 as f64).try_from().unwrap(), y: ($y1 as f64).try_from().unwrap() }, Point { x: ($x2 as f64).try_from().unwrap(), y: f64::try_from().unwrap() }))
Shape::Line((RelativePoint::new($x1, $y1).expect("both x1 and y1 are within the interval [0.0, 1.0]"), RelativePoint::new($x2, $y2).expect("both x2 and y2 are within the interval [0.0, 1.0]")))
};
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Waypoint {
Expand Down Expand Up @@ -409,6 +279,7 @@ mod tests {
use pretty_assertions::assert_eq;

use super::*;
use crate::polygon;

// fn float_eq(lhs: f32, rhs: f32) -> bool {
// f32::abs(lhs - rhs) <= f32::EPSILON
Expand Down
100 changes: 100 additions & 0 deletions gbpplanner-rs/src/config/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use min_len_vec::OneOrMore;
use serde::{Deserialize, Serialize};
use typed_floats::StrictlyPositiveFinite;
use unit_interval::UnitInterval;

/// A relative point within the boudaries of the map.
/// ...
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub struct RelativePoint {
pub x: UnitInterval,
pub y: UnitInterval,
}

impl RelativePoint {
/// Create a new `RelativePoint` from a pair of values.
/// Returns an error if either `x` or `y` is not in the interval [0.0, 1.0].
pub fn new(x: f64, y: f64) -> Result<Self, unit_interval::UnitIntervalError> {
Ok(Self {
x: UnitInterval::new(x)?,
y: UnitInterval::new(y)?,
})
}

/// Returns the x and y values as a tuple
pub fn get(&self) -> (f64, f64) {
(self.x.get(), self.y.get())
}
}

impl TryFrom<(f64, f64)> for RelativePoint {
type Error = unit_interval::UnitIntervalError;

fn try_from(value: (f64, f64)) -> Result<Self, Self::Error> {
Ok(Self {
x: UnitInterval::new(value.0)?,
y: UnitInterval::new(value.1)?,
})
}
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Shape {
Circle {
radius: StrictlyPositiveFinite<f32>,
center: RelativePoint,
},
Polygon(OneOrMore<RelativePoint>),
Line((RelativePoint, RelativePoint)),
}

impl Shape {
/// Returns `true` if the shape is [`Polygon`].
///
/// [`Polygon`]: Shape::Polygon
#[must_use]
pub fn is_polygon(&self) -> bool {
matches!(self, Self::Polygon(..))
}

/// Returns `true` if the shape is [`Circle`].
///
/// [`Circle`]: Shape::Circle
#[must_use]
pub fn is_circle(&self) -> bool {
matches!(self, Self::Circle { .. })
}

pub fn as_polygon(&self) -> Option<&OneOrMore<RelativePoint>> {
if let Self::Polygon(v) = self {
Some(v)
} else {
None
}
}
}

/// Shorthand to construct `Shape::Polygon(vec![Point {x: $x, y: $y}, ... ])`
#[macro_export]
macro_rules! polygon {
[$(($x:expr, $y:expr)),+ $(,)?] => {{
let vertices = vec![
$(
crate::config::geometry::RelativePoint::new($x, $y).expect("both x and y are within the interval [0.0, 1.0]")
),+
];
Shape::Polygon(OneOrMore::new(vertices).expect("at least one vertex"))
}}
}

/// Shorthand to construct `Shape::Line((Point {x: $x1, y: $y1}, Point {x: $x2,
/// y: $y2}))`
#[macro_export]
macro_rules! line {
[($x1:expr, $y1:expr), ($x2:expr, $y2:expr)] => {
// Shape::Line((Point { x: $x1, y: $y1 }, Point { x: $x2, y: $y2 }))
// Shape::Line((Point { x: ($x1 as f64).try_from().unwrap(), y: ($y1 as f64).try_from().unwrap() }, Point { x: ($x2 as f64).try_from().unwrap(), y: f64::try_from().unwrap() }))
Shape::Line((crate::config::geometry::RelativePoint::new($x1, $y1).expect("both x1 and y1 are within the interval [0.0, 1.0]"), crate::config::geometry::RelativePoint::new($x2, $y2).expect("both x2 and y2 are within the interval [0.0, 1.0]")))
};
}
1 change: 1 addition & 0 deletions gbpplanner-rs/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod environment;
pub mod formation;
pub mod geometry;

use std::num::NonZeroUsize;

Expand Down
2 changes: 1 addition & 1 deletion gbpplanner-rs/src/planner/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::robot::VariableTimestepsResource;
use crate::{
asset_loader::SceneAssets,
config::{
formation::{RelativePoint, Shape},
geometry::{RelativePoint, Shape},
Config, Formation, FormationGroup,
},
planner::robot::RobotBundle,
Expand Down

0 comments on commit 25b5b10

Please sign in to comment.