opencv/manual/core/
point.rs

1use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
2
3use num_traits::{NumCast, NumOps, ToPrimitive};
4
5use crate::core::{Rect_, Size_, VecN};
6use crate::opencv_type_simple_generic;
7
8/// [docs.opencv.org](https://docs.opencv.org/master/db/d4e/classcv_1_1Point__.html)
9#[repr(C)]
10#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd)]
11pub struct Point_<T> {
12	pub x: T,
13	pub y: T,
14}
15
16impl<T> Point_<T> {
17	#[inline]
18	pub const fn new(x: T, y: T) -> Self {
19		Self { x, y }
20	}
21
22	#[inline]
23	pub fn from_vec2(vec: VecN<T, 2>) -> Self {
24		let [x, y] = vec.0;
25		Self::new(x, y)
26	}
27
28	#[inline]
29	pub fn from_size(sz: Size_<T>) -> Self {
30		Self::new(sz.width, sz.height)
31	}
32
33	#[inline]
34	pub fn cross(self, pt: Point_<T>) -> f64
35	where
36		f64: From<T>,
37	{
38		let self_x: f64 = From::from(self.x);
39		let self_y: f64 = From::from(self.y);
40		let pt_x: f64 = From::from(pt.x);
41		let pt_y: f64 = From::from(pt.y);
42		self_x * pt_y - self_y * pt_x
43	}
44
45	#[inline]
46	pub fn dot(self, pt: Point_<T>) -> T
47	where
48		T: NumOps,
49	{
50		self.x * pt.x + self.y * pt.y
51	}
52
53	#[inline]
54	pub fn ddot(self, pt: Point_<T>) -> f64
55	where
56		f64: From<T>,
57	{
58		let self_x: f64 = From::from(self.x);
59		let self_y: f64 = From::from(self.y);
60		let pt_x: f64 = From::from(pt.x);
61		let pt_y: f64 = From::from(pt.y);
62		self_x * pt_x + self_y * pt_y
63	}
64
65	#[inline]
66	pub fn inside(self, rect: Rect_<T>) -> bool
67	where
68		T: PartialOrd + Add<Output = T> + Copy,
69	{
70		rect.contains(self)
71	}
72
73	#[inline]
74	pub fn norm(self) -> f64
75	where
76		f64: From<T>,
77	{
78		let self_x: f64 = From::from(self.x);
79		let self_y: f64 = From::from(self.y);
80		(self_x.powi(2) + self_y.powi(2)).sqrt()
81	}
82
83	/// Cast `Point` to the other coord type
84	#[inline]
85	pub fn to<D: NumCast>(self) -> Option<Point_<D>>
86	where
87		T: ToPrimitive,
88	{
89		Some(Point_::new(D::from(self.x)?, D::from(self.y)?))
90	}
91
92	#[inline]
93	pub fn to_vec2(self) -> VecN<T, 2> {
94		VecN::<_, 2>::from_array([self.x, self.y])
95	}
96}
97
98impl<T> From<(T, T)> for Point_<T> {
99	#[inline]
100	fn from(s: (T, T)) -> Self {
101		Self::new(s.0, s.1)
102	}
103}
104
105impl<T> From<VecN<T, 2>> for Point_<T> {
106	#[inline]
107	fn from(s: VecN<T, 2>) -> Self {
108		Self::from_vec2(s)
109	}
110}
111
112impl<T> From<Size_<T>> for Point_<T> {
113	#[inline]
114	fn from(s: Size_<T>) -> Self {
115		Self::from_size(s)
116	}
117}
118
119impl<T> Add for Point_<T>
120where
121	Self: AddAssign,
122{
123	type Output = Self;
124
125	fn add(mut self, rhs: Self) -> Self::Output {
126		self += rhs;
127		self
128	}
129}
130
131impl<T> Sub for Point_<T>
132where
133	Self: SubAssign,
134{
135	type Output = Self;
136
137	fn sub(mut self, rhs: Self) -> Self::Output {
138		self -= rhs;
139		self
140	}
141}
142
143impl<T> Mul<T> for Point_<T>
144where
145	Self: MulAssign<T>,
146{
147	type Output = Self;
148
149	fn mul(mut self, rhs: T) -> Self::Output {
150		self *= rhs;
151		self
152	}
153}
154
155impl<T> Div<T> for Point_<T>
156where
157	Self: DivAssign<T>,
158{
159	type Output = Self;
160
161	fn div(mut self, rhs: T) -> Self::Output {
162		self /= rhs;
163		self
164	}
165}
166
167impl<T: AddAssign> AddAssign for Point_<T> {
168	fn add_assign(&mut self, rhs: Self) {
169		self.x += rhs.x;
170		self.y += rhs.y;
171	}
172}
173
174impl<T: SubAssign> SubAssign for Point_<T> {
175	fn sub_assign(&mut self, rhs: Self) {
176		self.x -= rhs.x;
177		self.y -= rhs.y;
178	}
179}
180
181impl<T: MulAssign + Copy> MulAssign<T> for Point_<T> {
182	fn mul_assign(&mut self, rhs: T) {
183		self.x *= rhs;
184		self.y *= rhs;
185	}
186}
187
188impl<T: DivAssign + Copy> DivAssign<T> for Point_<T> {
189	fn div_assign(&mut self, rhs: T) {
190		self.x /= rhs;
191		self.y /= rhs;
192	}
193}
194
195opencv_type_simple_generic! { Point_<Copy> }