~qeef/bcar

Add parking slot skeleton v3 REJECTED

Jiri Vlasak: 5
 Add parking slot skeleton
 Refactor constructors
 Add parking slot heading getter
 Add parking slot parallel? getter
 Add is parking slot on right? function

 6 files changed, 150 insertions(+), 14 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~qeef/bcar/patches/11778/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v3 1/5] Add parking slot skeleton Export this patch

---
In this patchset version, there are additional computations to the
parking slot type (PSlot).

 src/lib.rs   |  3 +++
 src/pslot.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 src/pslot.rs

diff --git a/src/lib.rs b/src/lib.rs
index dc3cb87..3b018b6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,6 +11,9 @@ pub use crate::properties::Size;
mod bcar;
pub use crate::bcar::BCar;

mod pslot;
pub use crate::pslot::PSlot;

#[cfg(test)]
mod tests {
    use super::*;
diff --git a/src/pslot.rs b/src/pslot.rs
new file mode 100644
index 0000000..55a167c
--- /dev/null
+++ b/src/pslot.rs
@@ -0,0 +1,58 @@
//! Parking slot type and related methods

use crate::geometry::Point;

/// Parking slot structure.
///
///
/// Examples
/// ========
///
/// ```
/// let p1 = bcar::Point::new(0.0, 0.0);
/// let p2 = bcar::Point::new(0.0, -1.0);
/// let p3 = bcar::Point::new(2.0, -1.0);
/// let p4 = bcar::Point::new(2.0, 0.0);
/// let ps = bcar::PSlot::new([p1, p2, p3, p4]);
/// assert_eq!(ps, bcar::PSlot::default());
/// ```
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct PSlot {
    pub border: [Point; 4],
}

impl PSlot {
    pub fn new(border: [Point; 4]) -> PSlot {
        PSlot {border}
    }

    /// Return default `PSlot`.
    ///
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let ps = bcar::PSlot::default();
    /// let p1 = bcar::Point::default();
    /// let p2 = bcar::Point::new(0.0, -1.0);
    /// let p3 = bcar::Point::new(2.0, -1.0);
    /// let p4 = bcar::Point::new(2.0, 0.0);
    /// assert!(ps.border[0].x - p1.x < 0.00001);
    /// assert!(ps.border[0].y - p1.y < 0.00001);
    /// assert!(ps.border[1].x - p2.x < 0.00001);
    /// assert!(ps.border[1].y - p2.y < 0.00001);
    /// assert!(ps.border[2].x - p3.x < 0.00001);
    /// assert!(ps.border[2].y - p3.y < 0.00001);
    /// assert!(ps.border[3].x - p4.x < 0.00001);
    /// assert!(ps.border[3].y - p4.y < 0.00001);
    /// ```
    pub fn default() -> PSlot {
        PSlot {border: [
            Point::new(0.0, 0.0),
            Point::new(0.0, -1.0),
            Point::new(2.0, -1.0),
            Point::new(2.0, 0.0),
        ]}
    }
}
-- 
2.20.1

[PATCH v3 2/5] Refactor constructors Export this patch

---
 src/pslot.rs | 43 +++++++++++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/src/pslot.rs b/src/pslot.rs
index 55a167c..cf3e7c3 100644
--- a/src/pslot.rs
+++ b/src/pslot.rs
@@ -9,12 +9,16 @@ use crate::geometry::Point;
/// ========
///
/// ```
/// let p1 = bcar::Point::new(0.0, 0.0);
/// let p2 = bcar::Point::new(0.0, -1.0);
/// let p3 = bcar::Point::new(2.0, -1.0);
/// let p4 = bcar::Point::new(2.0, 0.0);
/// let ps = bcar::PSlot::new([p1, p2, p3, p4]);
/// assert_eq!(ps, bcar::PSlot::default());
/// let ps = bcar::PSlot::new_phwl(bcar::Point::new(0.0, 0.0), 0.0, 1.0, 2.0);
/// let def_ps = bcar::PSlot::default();
/// assert!(ps.border[0].x - def_ps.border[0].x < 0.00001);
/// assert!(ps.border[0].y - def_ps.border[0].y < 0.00001);
/// assert!(ps.border[1].x - def_ps.border[1].x < 0.00001);
/// assert!(ps.border[1].y - def_ps.border[1].y < 0.00001);
/// assert!(ps.border[2].x - def_ps.border[2].x < 0.00001);
/// assert!(ps.border[2].y - def_ps.border[2].y < 0.00001);
/// assert!(ps.border[3].x - def_ps.border[3].x < 0.00001);
/// assert!(ps.border[3].y - def_ps.border[3].y < 0.00001);
/// ```
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct PSlot {
@@ -22,8 +26,24 @@ pub struct PSlot {
}

impl PSlot {
    pub fn new(border: [Point; 4]) -> PSlot {
        PSlot {border}
    /// Return new parking slot
    ///
    /// Arguments
    /// ---------
    ///
    /// - `p` -- Parking slot first point.
    /// - `h` -- Parking slot heading.
    /// - `w` -- Parking slot width.
    /// - `l` -- Parking slot length
    pub fn new_phwl(p: Point, h: f64, w: f64, l: f64) -> PSlot {
        let pi = std::f64::consts::PI;
        let p2 = Point::new(
            p.x + w*(h - pi/2.0).cos(),
            p.y + w*(h - pi/2.0).sin()
        );
        let p3 = Point::new(p2.x + l*h.cos(), p2.y + l*h.sin());
        let p4 = Point::new(p.x + l*h.cos(), p.y + l*h.sin());
        PSlot {border: [p, p2, p3, p4]}
    }

    /// Return default `PSlot`.
@@ -48,11 +68,6 @@ impl PSlot {
    /// assert!(ps.border[3].y - p4.y < 0.00001);
    /// ```
    pub fn default() -> PSlot {
        PSlot {border: [
            Point::new(0.0, 0.0),
            Point::new(0.0, -1.0),
            Point::new(2.0, -1.0),
            Point::new(2.0, 0.0),
        ]}
        PSlot::new_phwl(Point::default(), 0.0, 1.0, 2.0)
    }
}
-- 
2.20.1

[PATCH v3 3/5] Add parking slot heading getter Export this patch

---
 src/pslot.rs | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/pslot.rs b/src/pslot.rs
index cf3e7c3..dd69053 100644
--- a/src/pslot.rs
+++ b/src/pslot.rs
@@ -70,4 +70,21 @@ impl PSlot {
    pub fn default() -> PSlot {
        PSlot::new_phwl(Point::default(), 0.0, 1.0, 2.0)
    }

    /// Return the heading of the parking slot.
    ///
    /// The heading is given by the direction from the first point to
    /// the last point.
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let ps = bcar::PSlot::default();
    /// assert!(ps.heading() - 0.0 < 0.00001);
    /// ```
    pub fn heading(&self) -> f64 {
        (self.border[3].y - self.border[0].y)
        .atan2(self.border[3].x - self.border[0].x)
    }
}
-- 
2.20.1

[PATCH v3 4/5] Add parking slot parallel? getter Export this patch

---
 src/pslot.rs | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/pslot.rs b/src/pslot.rs
index dd69053..e24fc37 100644
--- a/src/pslot.rs
+++ b/src/pslot.rs
@@ -87,4 +87,27 @@ impl PSlot {
        (self.border[3].y - self.border[0].y)
        .atan2(self.border[3].x - self.border[0].x)
    }

    /// Return if the parking slot is parallel.
    ///
    /// If not, the parking slot is perpendicular.
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let ps = bcar::PSlot::default();
    /// assert!(ps.parallel());
    /// ```
    pub fn parallel(&self) -> bool {
        let d1 = (
            (self.border[1].x - self.border[0].x).powi(2)
            + (self.border[1].y - self.border[0].y).powi(2)
        ).sqrt();
        let d2 = (
            (self.border[2].x - self.border[1].x).powi(2)
            + (self.border[2].y - self.border[1].y).powi(2)
        ).sqrt();
        d1 < d2
    }
}
-- 
2.20.1

[PATCH v3 5/5] Add is parking slot on right? function Export this patch

---
 src/pslot.rs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/pslot.rs b/src/pslot.rs
index e24fc37..c13c2cd 100644
--- a/src/pslot.rs
+++ b/src/pslot.rs
@@ -110,4 +110,24 @@ impl PSlot {
        ).sqrt();
        d1 < d2
    }

    /// Return if the parking slot is on the right side.
    ///
    /// Right side of line given by the first and the last point.
    ///
    /// Examples
    /// ========
    ///
    /// ```
    /// let ps = bcar::PSlot::default();
    /// assert!(ps.right());
    /// ```
    pub fn right(&self) -> bool {
        (
            (self.border[1].x - self.border[0].x)
            * (self.border[3].y - self.border[0].y)
            - (self.border[1].y - self.border[0].y)
            * (self.border[3].x - self.border[0].x)
        ) > 0.0
    }
}
-- 
2.20.1