path_planning/algorithms/
mod.rs

1/* Copyright (C) 2020 Dylan Staatz - All Rights Reserved. */
2
3pub mod rrt_2d_f32;
4pub mod rrt_2d_f64;
5pub mod rrt_3d_f32;
6pub mod rrt_3d_f64;
7
8pub mod rrtstar_2d_f32;
9pub mod rrtstar_2d_f64;
10pub mod rrtstar_3d_f32;
11pub mod rrtstar_3d_f64;
12
13pub mod rrtx_2d_f32;
14pub mod rrtx_2d_f64;
15pub mod rrtx_3d_f32;
16pub mod rrtx_3d_f64;
17
18pub mod rrtx_2d_f32_dual;
19pub mod rrtx_2d_f64_dual;
20pub mod rrtx_3d_f32_dual;
21pub mod rrtx_3d_f64_dual;
22
23pub mod rrtx_2d_f32_dual_polar;
24pub mod rrtx_2d_f64_dual_polar;
25
26pub mod rrtx_3d_f32_dual_spherical;
27pub mod rrtx_3d_f64_dual_spherical;
28
29pub use rrt_2d_f32::Rrt2df32;
30pub use rrt_2d_f64::Rrt2df64;
31pub use rrt_3d_f32::Rrt3df32;
32pub use rrt_3d_f64::Rrt3df64;
33
34pub use rrtstar_2d_f32::RrtStar2df32;
35pub use rrtstar_2d_f64::RrtStar2df64;
36pub use rrtstar_3d_f32::RrtStar3df32;
37pub use rrtstar_3d_f64::RrtStar3df64;
38
39pub use rrtx_2d_f32::Rrtx2df32;
40pub use rrtx_2d_f64::Rrtx2df64;
41pub use rrtx_3d_f32::Rrtx3df32;
42pub use rrtx_3d_f64::Rrtx3df64;
43
44pub use rrtx_2d_f32_dual::Rrtx2df32Dual;
45pub use rrtx_2d_f64_dual::Rrtx2df64Dual;
46pub use rrtx_3d_f32_dual::Rrtx3df32Dual;
47pub use rrtx_3d_f64_dual::Rrtx3df64Dual;
48
49pub use rrtx_2d_f32_dual_polar::Rrtx2df32DualPolar;
50pub use rrtx_2d_f64_dual_polar::Rrtx2df64DualPolar;
51
52pub use rrtx_3d_f32_dual_spherical::Rrtx3df32DualSpherical;
53pub use rrtx_3d_f64_dual_spherical::Rrtx3df64DualSpherical;
54
55////////////////////////////////////////////////////////////////////////////////
56
57use std::convert::{From, Into};
58use std::fmt::{Display, Error as FmtError, Formatter};
59use std::result::Result as StdResult;
60use std::str::FromStr;
61
62use serde::{Deserialize, Serialize};
63
64use crate::error::Result;
65use crate::run::{self, Settings};
66
67pub fn run_and_save(settings: Settings) -> Result<()> {
68  let algo = Algo::from_str(&settings.algorithm).unwrap();
69
70  match (algo, settings.double) {
71    (Algo::Rrt2d, false) => {
72      run::run_and_save::<Rrt2df32>(settings)?;
73    }
74    (Algo::Rrt2d, true) => {
75      run::run_and_save::<Rrt2df64>(settings)?;
76    }
77    (Algo::Rrt3d, false) => {
78      run::run_and_save::<Rrt3df32>(settings)?;
79    }
80    (Algo::Rrt3d, true) => {
81      run::run_and_save::<Rrt3df64>(settings)?;
82    }
83    (Algo::RrtStar2d, false) => {
84      run::run_and_save::<RrtStar2df32>(settings)?;
85    }
86    (Algo::RrtStar2d, true) => {
87      run::run_and_save::<RrtStar2df64>(settings)?;
88    }
89    (Algo::RrtStar3d, false) => {
90      run::run_and_save::<RrtStar3df32>(settings)?;
91    }
92    (Algo::RrtStar3d, true) => {
93      run::run_and_save::<RrtStar3df64>(settings)?;
94    }
95    (Algo::Rrtx2d, false) => {
96      run::run_and_save::<Rrtx2df32>(settings)?;
97    }
98    (Algo::Rrtx2d, true) => {
99      run::run_and_save::<Rrtx2df64>(settings)?;
100    }
101    (Algo::Rrtx3d, false) => {
102      run::run_and_save::<Rrtx3df32>(settings)?;
103    }
104    (Algo::Rrtx3d, true) => {
105      run::run_and_save::<Rrtx3df64>(settings)?;
106    }
107    (Algo::Rrtx2dDual, false) => {
108      run::run_and_save::<Rrtx2df32DualPolar>(settings)?;
109    }
110    (Algo::Rrtx2dDual, true) => {
111      run::run_and_save::<Rrtx2df64DualPolar>(settings)?;
112    }
113    (Algo::Rrtx3dDual, false) => {
114      run::run_and_save::<Rrtx3df32DualSpherical>(settings)?;
115    }
116    (Algo::Rrtx3dDual, true) => {
117      run::run_and_save::<Rrtx3df64DualSpherical>(settings)?;
118    }
119  };
120  Ok(())
121}
122
123/// Enumeration of all different algorithms implemented
124#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
125pub enum Algo {
126  Rrt2d,
127  Rrt3d,
128  RrtStar2d,
129  RrtStar3d,
130  Rrtx2d,
131  Rrtx3d,
132  Rrtx2dDual,
133  Rrtx3dDual,
134}
135
136impl Display for Algo {
137  fn fmt(&self, f: &mut Formatter<'_>) -> StdResult<(), FmtError> {
138    f.write_str(self.into())
139  }
140}
141
142impl From<&Algo> for &'_ str {
143  fn from(algo: &Algo) -> &'static str {
144    (*algo).into()
145  }
146}
147
148impl From<Algo> for &'_ str {
149  fn from(algo: Algo) -> &'static str {
150    match algo {
151      Algo::Rrt2d => "rrt-2d",
152      Algo::Rrt3d => "rrt-3d",
153      Algo::RrtStar2d => "rrtstar-2d",
154      Algo::RrtStar3d => "rrtstar-3d",
155      Algo::Rrtx2d => "rrtx-2d",
156      Algo::Rrtx3d => "rrtx-3d",
157      Algo::Rrtx2dDual => "rrtx-2d-dual",
158      Algo::Rrtx3dDual => "rrtx-3d-dual",
159    }
160  }
161}
162
163impl FromStr for Algo {
164  type Err = ();
165  fn from_str(s: &str) -> StdResult<Self, Self::Err> {
166    match &s.to_lowercase()[..] {
167      "rrt-2d" => Ok(Algo::Rrt2d),
168      "rrt-3d" => Ok(Algo::Rrt3d),
169      "rrtstar-2d" => Ok(Algo::RrtStar2d),
170      "rrtstar-3d" => Ok(Algo::RrtStar3d),
171      "rrtx-2d" => Ok(Algo::Rrtx2d),
172      "rrtx-3d" => Ok(Algo::Rrtx3d),
173      "rrtx-2d-dual" => Ok(Algo::Rrtx2dDual),
174      "rrtx-3d-dual" => Ok(Algo::Rrtx3dDual),
175      _ => Err(()),
176    }
177  }
178}
179
180struct Iter(Option<Algo>);
181
182impl Algo {
183  fn next(&self) -> Option<Self> {
184    match self {
185      Algo::Rrt2d => Some(Algo::Rrt3d),
186      Algo::Rrt3d => Some(Algo::RrtStar2d),
187      Algo::RrtStar2d => Some(Algo::RrtStar3d),
188      Algo::RrtStar3d => Some(Algo::Rrtx2d),
189      Algo::Rrtx2d => Some(Algo::Rrtx3d),
190      Algo::Rrtx3d => Some(Algo::Rrtx2dDual),
191      Algo::Rrtx2dDual => Some(Algo::Rrtx3dDual),
192      Algo::Rrtx3dDual => None,
193    }
194  }
195}
196
197impl Iterator for Iter {
198  type Item = Algo;
199  fn next(&mut self) -> Option<Self::Item> {
200    let this = self.0?;
201    self.0 = this.next();
202    Some(this)
203  }
204}
205
206impl Algo {
207  pub fn long_help() -> String {
208    let mut s = String::from("Which algorithm to run. Supported algorithms:\n");
209
210    for ref algo in Algo::all() {
211      s.push_str(algo.into());
212      s.push_str(" ");
213    }
214    s
215  }
216
217  fn all() -> Iter {
218    Iter(Some(Algo::Rrt2d))
219  }
220}