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

use num_traits::{NumCast, ToPrimitive, Zero};

use crate::core::Point_;
use crate::opencv_type_simple_generic;

/// [docs.opencv.org](https://docs.opencv.org/master/d6/d50/classcv_1_1Size__.html)
#[repr(C)]
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd)]
pub struct Size_<T> {
	pub width: T,
	pub height: T,
}

impl<T> Size_<T> {
	#[inline]
	pub const fn new(width: T, height: T) -> Self {
		Self { width, height }
	}

	#[inline]
	pub fn from_point(pt: Point_<T>) -> Self {
		Self {
			width: pt.x,
			height: pt.y,
		}
	}

	#[inline]
	pub fn area(self) -> T
	where
		T: Mul<Output = T>,
	{
		self.width * self.height
	}

	#[inline]
	pub fn empty(self) -> bool
	where
		T: PartialOrd + Zero,
	{
		self.width <= T::zero() || self.height <= T::zero()
	}

	/// Cast `Size` to the other coord type
	#[inline]
	pub fn to<D: NumCast>(self) -> Option<Size_<D>>
	where
		T: ToPrimitive,
	{
		Some(Size_ {
			width: D::from(self.width)?,
			height: D::from(self.height)?,
		})
	}
}

impl<T> From<(T, T)> for Size_<T> {
	#[inline]
	fn from(s: (T, T)) -> Self {
		Self::new(s.0, s.1)
	}
}

impl<T> From<Point_<T>> for Size_<T> {
	#[inline]
	fn from(s: Point_<T>) -> Self {
		Self::from_point(s)
	}
}

impl<T> Add for Size_<T>
where
	Self: AddAssign,
{
	type Output = Self;

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

impl<T> Sub for Size_<T>
where
	Self: SubAssign,
{
	type Output = Self;

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

impl<T> Mul<T> for Size_<T>
where
	Self: MulAssign<T>,
{
	type Output = Self;

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

impl<T> Div<T> for Size_<T>
where
	Self: DivAssign<T>,
{
	type Output = Self;

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

impl<T: AddAssign> AddAssign for Size_<T> {
	fn add_assign(&mut self, rhs: Self) {
		self.width += rhs.width;
		self.height += rhs.height;
	}
}

impl<T: SubAssign> SubAssign for Size_<T> {
	fn sub_assign(&mut self, rhs: Self) {
		self.width -= rhs.width;
		self.height -= rhs.height;
	}
}

impl<T: MulAssign + Copy> MulAssign<T> for Size_<T> {
	fn mul_assign(&mut self, rhs: T) {
		self.width *= rhs;
		self.height *= rhs;
	}
}

impl<T: DivAssign + Copy> DivAssign<T> for Size_<T> {
	fn div_assign(&mut self, rhs: T) {
		self.width /= rhs;
		self.height /= rhs;
	}
}

opencv_type_simple_generic! { Size_<Copy> }