Skip to main content

sampling_planning/
goal.rs

1//! Goal regions.
2
3use crate::StateSpace;
4
5/// Every state within `tolerance` of `center` is a goal.
6///
7/// Goal bias samples `center`.
8#[derive(Debug, Clone, PartialEq)]
9pub struct GoalRegion<S> {
10    /// The goal state; also the state drawn for goal-biased samples.
11    center: S,
12    /// The largest accepted distance from `center`. Non-negative and finite.
13    tolerance: f64,
14}
15
16impl<S> GoalRegion<S> {
17    /// Creates a goal region around `center`.
18    ///
19    /// # Panics
20    ///
21    /// Panics if `tolerance` is negative or not finite.
22    #[must_use]
23    pub fn new(center: S, tolerance: f64) -> Self {
24        assert!(
25            tolerance.is_finite() && tolerance >= 0.0,
26            "goal tolerance must be non-negative and finite"
27        );
28
29        Self { center, tolerance }
30    }
31
32    /// The goal state.
33    #[must_use]
34    pub const fn center(&self) -> &S {
35        &self.center
36    }
37
38    /// The largest accepted distance from [`GoalRegion::center`].
39    #[must_use]
40    pub const fn tolerance(&self) -> f64 {
41        self.tolerance
42    }
43
44    /// Whether `state` lies in the region.
45    #[must_use]
46    pub fn contains<Sp: StateSpace<State = S>>(&self, space: &Sp, state: &S) -> bool {
47        space.distance(state, &self.center) <= self.tolerance
48    }
49}