Skip to content

Commit

Permalink
[Pathfinding] Make graphics an optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rmheuer committed Feb 5, 2024
1 parent 0c4fd27 commit 2c4d8bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
5 changes: 4 additions & 1 deletion Pathfinding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ arrayvec = "0.7.4"
bytes = "1.5.0"
itertools = "0.12.0"
lerp = "0.5.0"
macroquad = "0.4.4"
macroquad = { version = "0.4.4", optional = true }
messenger-client = { path = "../Messenger/messenger-client" }
ordered-float = "4.2.0"
serde = { version = "1.0.195", features = [ "derive" ] }
serde_json = "1.0"
tokio = { version = "1.34.0", features = [ "rt-multi-thread", "fs" ] }
uuid = { version = "1.6.1", features = [ "v4", "fast-rng", "serde" ] }

[features]
graphics = ["dep:macroquad"]
49 changes: 27 additions & 22 deletions Pathfinding/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
// If inside obstacle, find a way out first
// OPTIMIZATION: Use bidirectional Dijkstra for fast

use std::{
error::Error,
sync::{Arc, Mutex},
time::Instant,
};
use std::{error::Error, time::Instant};

use bytes::{Buf, BufMut, BytesMut};
use itertools::Itertools;
Expand All @@ -20,8 +16,12 @@ mod geom;
mod math;
mod obstacle;

#[cfg(feature = "graphics")]
mod gui;

#[cfg(feature = "graphics")]
use std::sync::{Arc, Mutex};

const MSG_SET_ENDPOINTS: &str = "Pathfinder:SetEndpoints";
const MSG_PATH: &str = "Pathfinder:Path";

Expand All @@ -40,27 +40,29 @@ async fn main() -> Result<(), Box<dyn Error>> {
.map(|(_, o)| o.clone().into_obstacle())
.collect_vec();

let field = Arc::new(geom::Field::generate(
&obstacles,
conf.robot_radius + conf.tolerance,
));
let field = geom::Field::generate(&obstacles, conf.robot_radius + conf.tolerance);
#[cfg(feature = "graphics")]
let field = Arc::new(field);

let mut start = Vec2f::new(1.0, 1.0);
let mut goal = Vec2f::new(1.2, 1.2);

let gui_state = Arc::new(Mutex::new(gui::GraphicsState {
start,
goal,
calc_time: 0.0,
path: None,
}));
gui::show_graphics_window(
Vec2f::new(environment.width, environment.height),
conf.robot_radius,
obstacles,
field.clone(),
gui_state.clone(),
);
#[cfg(feature = "graphics")]
{
let gui_state = Arc::new(Mutex::new(gui::GraphicsState {
start,
goal,
calc_time: 0.0,
path: None,
}));
gui::show_graphics_window(
Vec2f::new(environment.width, environment.height),
conf.robot_radius,
obstacles,
field.clone(),
gui_state.clone(),
);
}

loop {
let mut needs_recalc = false;
Expand Down Expand Up @@ -92,6 +94,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let calc_end = Instant::now();
let calc_time = calc_end.duration_since(calc_start).as_secs_f64();

#[cfg(feature = "graphics")]
if let Ok(mut state) = gui_state.lock() {
state.start = start;
state.goal = goal;
Expand All @@ -111,13 +114,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
buf.put_f64(point.y);
}

#[cfg(feature = "graphics")]
if let Ok(mut state) = gui_state.lock() {
state.path = Some((path, bezier_pts));
}

buf
}
None => {
#[cfg(feature = "graphics")]
if let Ok(mut state) = gui_state.lock() {
state.path = None;
}
Expand Down

0 comments on commit 2c4d8bd

Please sign in to comment.