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
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

use num::{NumCast, ToPrimitive};

use crate::core::{Rect_, Size_, ValidRectType, ValidSizeType};

valid_types!(ValidPointType, i32, i64, f32, f64);

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq, PartialOrd)]
/// [docs.opencv.org](https://docs.opencv.org/master/db/d4e/classcv_1_1Point__.html)
pub struct Point_<T: ValidPointType> {
    pub x: T,
    pub y: T,
}

impl<T: ValidPointType> Point_<T> {
    #[inline]
    pub fn new(x: T, y: T) -> Self {
        Self { x, y }
    }

    #[inline]
    pub fn from_size(sz: Size_<T>) -> Self where T: ValidSizeType {
        Self { x: sz.width, y: sz.height }
    }

    #[inline]
    pub fn cross(self, pt: Point_<T>) -> f64 where f64: From<T> {
        let self_x: f64 = From::from(self.x);
        let self_y: f64 = From::from(self.y);
        let pt_x: f64 = From::from(pt.x);
        let pt_y: f64 = From::from(pt.y);
        self_x * pt_y - self_y * pt_x
    }

    #[inline]
    pub fn dot(self, pt: Point_<T>) -> T {
        self.x * pt.x + self.y * pt.y
    }

    #[inline]
    pub fn ddot(self, pt: Point_<T>) -> f64 where f64: From<T> {
        let self_x: f64 = From::from(self.x);
        let self_y: f64 = From::from(self.y);
        let pt_x: f64 = From::from(pt.x);
        let pt_y: f64 = From::from(pt.y);
        self_x * pt_x + self_y * pt_y
    }

    #[inline]
    pub fn inside(self, rect: Rect_<T>) -> bool where T: ValidRectType {
        rect.contains(self)
    }

    #[inline]
    pub fn norm(self) -> f64 where f64: From<T> {
        let self_x: f64 = From::from(self.x);
        let self_y: f64 = From::from(self.y);
        (self_x.powi(2) + self_y.powi(2)).sqrt()
    }

    #[inline]
    pub fn to<D: ValidPointType + NumCast>(self) -> Option<Point_<D>> where T: ToPrimitive {
        Some(Point_ { x: D::from(self.x)?, y: D::from(self.y)? })
    }
}

impl<T> Add for Point_<T>
    where
        T: ValidPointType + AddAssign,
{
    type Output = Point_<T>;

    fn add(mut self, rhs: Point_<T>) -> Self::Output {
        self += rhs;
        self
    }
}

impl<T> Sub for Point_<T>
    where
        T: ValidPointType + SubAssign,
{
    type Output = Point_<T>;

    fn sub(mut self, rhs: Point_<T>) -> Self::Output {
        self -= rhs;
        self
    }
}

impl<T> Mul<T> for Point_<T>
    where
        T: ValidPointType + MulAssign
{
    type Output = Point_<T>;

    fn mul(mut self, rhs: T) -> Self::Output {
        self *= rhs;
        self
    }
}

impl<T> Div<T> for Point_<T>
    where
        T: ValidPointType + DivAssign
{
    type Output = Point_<T>;

    fn div(mut self, rhs: T) -> Self::Output {
        self /= rhs;
        self
    }
}

impl<T> AddAssign for Point_<T>
    where
        T: ValidPointType + AddAssign,
{
    fn add_assign(&mut self, rhs: Point_<T>) {
        self.x += rhs.x;
        self.y += rhs.y;
    }
}

impl<T> SubAssign for Point_<T>
    where
        T: ValidPointType + SubAssign,
{
    fn sub_assign(&mut self, rhs: Point_<T>) {
        self.x -= rhs.x;
        self.y -= rhs.y;
    }
}

impl<T> MulAssign<T> for Point_<T>
    where
        T: ValidPointType + MulAssign
{
    fn mul_assign(&mut self, rhs: T) {
        self.x *= rhs;
        self.y *= rhs;
    }
}

impl<T> DivAssign<T> for Point_<T>
    where
        T: ValidPointType + DivAssign
{
    fn div_assign(&mut self, rhs: T) {
        self.x /= rhs;
        self.y /= rhs;
    }
}