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
//! The boxes module exposes AABox (axis-aligned box) and RBox (rotated box).
//! These are used to model objects.

use core::ops;

use crate::math::Vec2;

/// An axis-aligned box.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AABox {
    /// The position of the box.
    pub pos: Vec2,
    /// The size, both components must be greater than zero.
    pub size: Vec2,
}

impl ops::Add<Vec2> for AABox {
    type Output = Self;

    fn add(self, other: Vec2) -> Self {
        Self {
            pos: self.pos + other,
            size: self.size,
        }
    }
}

impl ops::AddAssign<Vec2> for AABox {
    fn add_assign(&mut self, other: Vec2) {
        self.pos += other
    }
}

impl ops::Sub<Vec2> for AABox {
    type Output = Self;

    fn sub(self, other: Vec2) -> Self {
        Self {
            pos: self.pos - other,
            size: self.size,
        }
    }
}

impl ops::SubAssign<Vec2> for AABox {
    fn sub_assign(&mut self, other: Vec2) {
        self.pos -= other
    }
}

/// A rotated box.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RBox {
    /// The origin.
    pub pos: Vec2,
    /// Vector 1.
    pub v1: Vec2,
    /// Vector 2: This vector always has to be orthogonal to v1.
    pub v2: Vec2,
}

impl RBox {
    /// Creates a new rotated box from a position, an orientation and a width.
    // v2 has the same direction as v1 rotated to the left by 90°
    pub fn new(pos: Vec2, orientation: Vec2, height: f32) -> Self {
        let scale = height / orientation.norm();
        let orth = Vec2::new(-orientation.y(), orientation.x()) * scale;
        Self {
            pos,
            v1: orientation,
            v2: orth,
        }
    }
}

impl ops::Add<Vec2> for RBox {
    type Output = Self;

    fn add(self, other: Vec2) -> Self {
        Self {
            pos: self.pos + other,
            v1: self.v1,
            v2: self.v2,
        }
    }
}

impl ops::AddAssign<Vec2> for RBox {
    fn add_assign(&mut self, other: Vec2) {
        self.pos += other
    }
}

impl ops::Sub<Vec2> for RBox {
    type Output = Self;

    fn sub(self, other: Vec2) -> Self {
        Self {
            pos: self.pos - other,
            v1: self.v1,
            v2: self.v2,
        }
    }
}

impl ops::SubAssign<Vec2> for RBox {
    fn sub_assign(&mut self, other: Vec2) {
        self.pos -= other
    }
}

impl From<AABox> for RBox {
    fn from(aabox: AABox) -> Self {
        Self {
            pos: aabox.pos,
            v1: Vec2::new(aabox.size.x(), 0.0),
            v2: Vec2::new(0.0, aabox.size.y()),
        }
    }
}

impl From<&spine::skeleton::srt::SRT> for RBox {
    fn from(srt: &spine::skeleton::srt::SRT) -> RBox {
        let pos = srt.transform([-1.0, -1.0]).into();
        let v1 = Vec2::from(srt.transform([1.0, -1.0])) - pos;
        let v2 = Vec2::from(srt.transform([-1.0, 1.0])) - pos;
        RBox { pos, v1, v2 }
    }
}