Skip to main content

sampling_planning/space/
curve.rs

1//! Circular-arc and straight segments of car-like paths (Dubins, Reeds-Shepp).
2
3use super::Se2State;
4use super::so2::wrap_angle;
5
6/// The motion of one path segment.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum SegmentKind {
9    /// A counterclockwise arc of the turning radius.
10    Left,
11    /// A straight line.
12    Straight,
13    /// A clockwise arc of the turning radius.
14    Right,
15}
16
17/// One segment of a Dubins or Reeds-Shepp path.
18#[derive(Debug, Clone, Copy, PartialEq)]
19pub struct Segment {
20    /// The motion.
21    pub kind: SegmentKind,
22    /// The signed length in units of the turning radius: the turned angle in radians for arcs.
23    /// Negative lengths drive in reverse (Reeds-Shepp only).
24    pub length: f64,
25}
26
27/// Drives `pose`, given in units of the turning radius, along `kind` for the signed normalized
28/// length `v`.
29fn advance(pose: (f64, f64, f64), kind: SegmentKind, v: f64) -> (f64, f64, f64) {
30    let (x, y, phi) = pose;
31
32    match kind {
33        SegmentKind::Left => (
34            x + libm::sin(phi + v) - libm::sin(phi),
35            y - libm::cos(phi + v) + libm::cos(phi),
36            phi + v,
37        ),
38        SegmentKind::Right => (
39            x - libm::sin(phi - v) + libm::sin(phi),
40            y + libm::cos(phi - v) - libm::cos(phi),
41            phi - v,
42        ),
43        SegmentKind::Straight => (x + v * libm::cos(phi), y + v * libm::sin(phi), phi),
44    }
45}
46
47/// The pose reached from `from` after driving the first `arc` (normalized, non-negative) of the
48/// unsigned length of `segments`, with turning radius `radius`. The heading is wrapped into
49/// `[-pi, pi)`.
50pub(crate) fn walk(from: &Se2State, segments: &[Segment], arc: f64, radius: f64) -> Se2State {
51    let mut pose = (0.0, 0.0, from.theta);
52    let mut remaining = arc;
53
54    for segment in segments {
55        if remaining <= 0.0 {
56            break;
57        }
58
59        let v = if segment.length < 0.0 {
60            segment.length.max(-remaining)
61        } else {
62            segment.length.min(remaining)
63        };
64        remaining -= libm::fabs(v);
65        pose = advance(pose, segment.kind, v);
66    }
67
68    Se2State {
69        x: from.x + pose.0 * radius,
70        y: from.y + pose.1 * radius,
71        theta: wrap_angle(pose.2),
72    }
73}
74
75/// The unsigned total length of `segments`, in units of the turning radius.
76pub(crate) fn total_length(segments: &[Segment]) -> f64 {
77    segments
78        .iter()
79        .map(|segment| libm::fabs(segment.length))
80        .sum()
81}
82
83/// The goal `to` relative to `from`, rotated into
84/// `from`'s heading and scaled by `1 / radius`: `(x, y, phi)` as in Reeds and Shepp.
85pub(crate) fn local_goal(from: &Se2State, to: &Se2State, radius: f64) -> (f64, f64, f64) {
86    let (dx, dy) = (to.x - from.x, to.y - from.y);
87    let (s, c) = (libm::sin(from.theta), libm::cos(from.theta));
88
89    (
90        (c * dx + s * dy) / radius,
91        (-s * dx + c * dy) / radius,
92        to.theta - from.theta,
93    )
94}
95
96#[cfg(test)]
97pub(crate) mod tests {
98    use super::*;
99    use crate::{Sampler, UniformSe2};
100    use alloc::vec::Vec;
101    use rand_core::SeedableRng;
102
103    /// Asserts that `a` and `b` are the same pose up to `tolerance`, comparing headings modulo
104    /// `2 pi`.
105    pub(crate) fn assert_same_pose(a: &Se2State, b: &Se2State, tolerance: f64) {
106        let heading = libm::fabs(super::super::angle_difference(a.theta, b.theta));
107
108        assert!(
109            libm::fabs(a.x - b.x) < tolerance
110                && libm::fabs(a.y - b.y) < tolerance
111                && heading < tolerance,
112            "{a:?} vs {b:?}"
113        );
114    }
115
116    /// `count` random pose pairs in `[-4, 4]^2`, plus coincident and nearly coincident pairs.
117    pub(crate) fn pose_pairs(count: usize, seed: u64) -> Vec<(Se2State, Se2State)> {
118        let mut sampler = UniformSe2::new([-4.0, -4.0], [4.0, 4.0]);
119        let mut rng = rand_pcg::Pcg64Mcg::seed_from_u64(seed);
120        let mut pairs: Vec<_> = (0..count)
121            .map(|_| (sampler.sample(&mut rng), sampler.sample(&mut rng)))
122            .collect();
123        let a = Se2State::new(0.5, -0.5, 1.0);
124        pairs.push((a, a));
125        pairs.push((a, Se2State::new(0.5, -0.5, 1.0 + 1e-9)));
126        pairs.push((a, Se2State::new(0.5 + 1e-9, -0.5, 1.0)));
127
128        pairs
129    }
130
131    #[test]
132    fn walking_a_quarter_circle_turns_and_moves() {
133        let segment = [Segment {
134            kind: SegmentKind::Left,
135            length: core::f64::consts::FRAC_PI_2,
136        }];
137        let end = walk(&Se2State::new(1.0, 1.0, 0.0), &segment, 10.0, 2.0);
138
139        assert_same_pose(
140            &end,
141            &Se2State::new(3.0, 3.0, core::f64::consts::FRAC_PI_2),
142            1e-12,
143        );
144        assert_eq!(total_length(&segment), core::f64::consts::FRAC_PI_2);
145    }
146}