1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Bicycle car object and related methods.

use crate::geometry::Point;
use crate::geometry::Pose;
use crate::properties::Motion;
use crate::properties::Size;

/// Bicycle car object.
///
///
/// Examples
/// ========
///
/// ```
/// let bc = bcar::BCar::default();
/// assert_eq!(bc, bcar::BCar::new(
///     bcar::Pose::default(),
///     bcar::Size::default(),
///     bcar::Motion::default(),
/// ));
///
/// let bc = bcar::BCar::new_xyh(1.0, 2.0, 3.0);
/// assert_eq!(bc.pose, bcar::Pose::new(1.0, 2.0, 3.0));
/// let default_size = bcar::Size::default();
/// assert_eq!(bc.size, default_size);
/// assert_eq!(bc.motion, bcar::Motion::default());
/// ```
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct BCar {
    pub pose: Pose,
    pub size: Size,
    pub motion: Motion,
}

impl BCar {
    pub fn new(pose: Pose, size: Size, motion: Motion) -> BCar {
        BCar {pose, size, motion}
    }

    /// Return new `BCar`.
    ///
    /// Arguments
    /// ---------
    ///
    /// - `x` -- Horizontal coordinate of rear axle center.
    /// - `y` -- Vertical coordinate of rear axle center.
    /// - `h` -- The heading of the car in the interval [-pi, +pi] radians.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let bc = bcar::BCar::new_xyh(1.0, 2.0, 3.0);
    /// let bc_pose = bc.pose;
    /// assert_eq!(1.0, bc_pose.x);
    /// assert_eq!(2.0, bc_pose.y);
    /// assert_eq!(3.0, bc_pose.h);
    /// ```
    pub fn new_xyh(x: f64, y: f64, h: f64) -> BCar {
        BCar {
            pose: Pose::new(x, y, h),
            size: Size::default(),
            motion: Motion::default(),
        }
    }

    /// Return default `BCar`.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let bc = bcar::BCar::default();
    /// let p = bcar::Pose::default();
    /// let s = bcar::Size::default();
    /// let m = bcar::Motion::default();
    /// assert_eq!(bc, bcar::BCar::new(p, s, m));
    /// ```
    pub fn default() -> BCar {
        BCar {
            pose: Pose::default(),
            size: Size::default(),
            motion: Motion::default(),
        }
    }

    /// Return `BCar` size.
    pub fn size(&self) -> Size {
        self.size
    }

    /// Set `BCar` dimensions.
    ///
    /// Arguments
    /// ---------
    ///
    /// - `s` -- New size of the car.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let size = bcar::Size::new(10.0, 2.0, 3.0, 4.0, 5.0);
    /// let mut bc = bcar::BCar::new_xyh(1.0, 2.0, 3.0);
    /// bc.set_size(size);
    /// assert_eq!(bc.size, size);
    /// ```
    pub fn set_size(&mut self, s: Size) {
        self.size = s;
    }

    /// Return `BCar` pose.
    pub fn pose(&self) -> Pose {
        self.pose
    }

    /// Set `BCar` pose.
    ///
    /// Arguments
    /// ---------
    ///
    /// - `p` -- New pose of the car.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let pose = bcar::Pose::new(1.0, 4.0, 9.0);
    /// let mut bc = bcar::BCar::new_xyh(1.0, 2.0, 3.0);
    /// bc.set_pose(pose);
    /// assert_eq!(bc.pose, pose);
    /// ```
    pub fn set_pose(&mut self, p: Pose) {
        self.pose = p
    }

    /// Return `BCar` motion.
    pub fn motion(&self) -> Motion {
        self.motion
    }

    /// Set `BCar` motion.
    ///
    /// Arguments
    /// ---------
    ///
    /// - `m` -- New motion of the car.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let m = bcar::Motion::new(10.0, 0.1);
    /// let mut bc = bcar::BCar::new_xyh(1.0, 2.0, 3.0);
    /// bc.set_motion(m);
    /// assert_eq!(bc.motion, m);
    /// ```
    pub fn set_motion(&mut self, m: Motion) {
        self.motion = m
    }

    /// Return if it's possible to drive to the `pose` trivially.
    ///
    /// Trivially in this context means to reach the `pose` with combination of
    /// line-arc-line segments, where arc is less than pi / 2.
    ///
    /// Arguments
    /// ---------
    ///
    /// - `pose` -- Pose to drive to.
    pub fn drivable(&self, pose: Pose) -> bool {
        let pi = std::f64::consts::PI;
        let pi2 = pi / 2.0;

        let mut a_1 = (pose.y - self.pose.y).atan2(pose.x - self.pose.x);
        a_1 -= self.pose.h;
        while a_1 < -pi { a_1 += 2.0 * pi; }
        while a_1 > pi { a_1 -= 2.0 * pi; }

        let mut h_d = pose.h - self.pose.h;
        while h_d < -pi { h_d += 2.0 * pi; }
        while h_d > pi { h_d -= 2.0 * pi; }

        let mut a_2 = 0.0;
        let mut zb = self.pose;

        if h_d == 0.0 && (a_1 == 0.0 || a_2 == pi || a_2 == -pi) {
            return true;
        } else if 0.0 < a_1 && a_1 <= pi2 { // Q2
            zb.rotate(self.ccl(), h_d);
            // assert zb.h == self.pose.h
            if pose.y == zb.y && pose.x == zb.x { // pose on zone border
                return true;
            }
            a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
            while a_2 < -pi { a_2 += 2.0 * pi; }
            while a_2 > pi { a_2 -= 2.0 * pi; }
            if zb.h >= a_2 && a_2 >= self.pose.h {
                return true;
            }
        } else if pi2 < a_1 && a_1 <= pi { // Q3
            zb.rotate(self.ccl(), h_d);
            // assert zb.h == self.pose.h
            if pose.y == zb.y && pose.x == zb.x { // pose on zone border
                return true;
            }
            a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
            a_2 -= pi;
            while a_2 < -pi { a_2 += 2.0 * pi; }
            while a_2 > pi { a_2 -= 2.0 * pi; }
            if self.pose.h >= a_2 && a_2 >= zb.h {
                return true;
            }
        } else if 0.0 > a_1 && a_1 >= -pi2 { // Q1
            zb.rotate(self.ccr(), h_d);
            // assert zb.h == self.pose.h
            if pose.y == zb.y && pose.x == zb.x { // pose on zone border
                return true;
            }
            a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
            while a_2 < -pi { a_2 += 2.0 * pi; }
            while a_2 > pi { a_2 -= 2.0 * pi; }
            if self.pose.h >= a_2 && a_2 >= zb.h {
                return true;
            }
        } else if -pi2 > a_1 && a_1 >= -pi { // Q4
            zb.rotate(self.ccr(), h_d);
            // assert zb.h == self.pose.h
            if pose.y == zb.y && pose.x == zb.x { // pose on zone border
                return true;
            }
            a_2 = (pose.y - zb.y).atan2(pose.x - zb.x);
            a_2 -= pi;
            while a_2 < -pi { a_2 += 2.0 * pi; }
            while a_2 > pi { a_2 -= 2.0 * pi; }
            if zb.h >= a_2 && a_2 >= self.pose.h {
                return true;
            }
        }

        false
    }

    /// Compute minimum turning radius (MTR).
    pub fn mtr(&self) -> f64 {
        (
            (self.size.curb_to_curb / 2.0).powi(2)
            - self.size.wheelbase.powi(2)
        ).sqrt()
        - self.size.width / 2.0
    }

    /// Compute MTR circle center on the left side of the car.
    pub fn ccl(&self) -> Point {
        let x = self.pose.x;
        let y = self.pose.y;
        let h = self.pose.h;
        Point::new(
            x + self.mtr() * (h + std::f64::consts::PI / 2.0).cos(),
            y + self.mtr() * (h + std::f64::consts::PI / 2.0).sin(),
        )
    }

    /// Compute MTR circle center on the right side of the car.
    pub fn ccr(&self) -> Point {
        let x = self.pose.x;
        let y = self.pose.y;
        let h = self.pose.h;
        Point::new(
            x + self.mtr() * (h - std::f64::consts::PI / 2.0).cos(),
            y + self.mtr() * (h - std::f64::consts::PI / 2.0).sin(),
        )
    }

    /// Return car frame's center front.
    pub fn cf(&self) -> Point {
        let mut x = self.pose.x;
        x += self.size.distance_to_front * self.pose.h.cos();
        let mut y = self.pose.y;
        y += self.size.distance_to_front * self.pose.h.sin();
        Point {x, y}
    }

    /// Return car frame's left front corner.
    pub fn lf(&self) -> Point {
        let pi = std::f64::consts::PI;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
        x += self.size.distance_to_front * self.pose.h.cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
        y += self.size.distance_to_front * self.pose.h.sin();
        Point {x, y}
    }

    /// Return car frame's coordinate for rear axle on left.
    pub fn la(&self) -> Point {
        let pi = std::f64::consts::PI;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
        Point {x, y}
    }

    /// Return car frame's left rear corner.
    pub fn lr(&self) -> Point {
        let pi = std::f64::consts::PI;
        let distance_to_rear = self.size.length - self.size.distance_to_front;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h + pi/2.0).cos();
        x += -distance_to_rear * self.pose.h.cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h + pi/2.0).sin();
        y += -distance_to_rear * self.pose.h.sin();
        Point {x, y}
    }

    /// Return car frame's right rear corner.
    pub fn rr(&self) -> Point {
        let pi = std::f64::consts::PI;
        let distance_to_rear = self.size.length - self.size.distance_to_front;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
        x += -distance_to_rear * self.pose.h.cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
        y += -distance_to_rear * self.pose.h.sin();
        Point {x, y}
    }

    /// Return car frame's coordinate for rear axle on right.
    pub fn ra(&self) -> Point {
        let pi = std::f64::consts::PI;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
        Point {x, y}
    }

    /// Return car frame's right front corner.
    pub fn rf(&self) -> Point {
        let pi = std::f64::consts::PI;
        let mut x = self.pose.x;
        x += self.size.width/2.0 * (self.pose.h - pi/2.0).cos();
        x += self.size.distance_to_front * self.pose.h.cos();
        let mut y = self.pose.y;
        y += self.size.width/2.0 * (self.pose.h - pi/2.0).sin();
        y += self.size.distance_to_front * self.pose.h.sin();
        Point {x, y}
    }
}

impl Iterator for BCar {
    type Item = BCar;

    /// Return `BCar` new state.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let mut bc = bcar::BCar::default();
    /// bc.set_motion(bcar::Motion::new(1.0, 0.0));
    /// bc.next();
    /// assert_eq!(bc, bcar::BCar::new(
    ///     bcar::Pose::new(1.0, 0.0, 0.0),
    ///     bcar::Size::default(),
    ///     bcar::Motion::new(1.0, 0.0),
    /// ));
    /// ```
    fn next(&mut self) -> Option<Self::Item> {
        self.pose.x += self.motion.speed * self.pose.h.cos();
        self.pose.y += self.motion.speed * self.pose.h.sin();
        self.pose.h += self.motion.speed
            / self.size.wheelbase
            * self.motion.steer.tan();
        Some(*self)
    }
}