add some Point_ methods and operators
MODIFY
src/hub/manual/core/point.rs
src/hub/manual/core/point.rs
+use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
+use std::process::Output;
+
use crate::hub::manual::core::size::ValidSizeType;
valid_types!(ValidPointType, i32, i64, f32, f64);
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq)]
+/// [docs.opencv.org](https://docs.opencv.org/3.4.6/db/d4e/classcv_1_1Point__.html)
pub struct Point_<T: ValidPointType> {
pub x: T,
pub y: T,
@@ -15,15 +19,111 @@ impl<T: ValidPointType> Point_<T> {
}
}
+impl<T: ValidPointType + Mul<Output=T> + Add<Output=T>> Point_<T>
+ where
+ f64: From<T>
+{
+ pub fn norm(&self) -> f64 {
+ f64::from(self.x * self.x + self.y * self.y).sqrt()
+ }
+}
+
impl<T: ValidPointType + ValidSizeType> Point_<T> {
pub fn from_size(sz: super::Size_<T>) -> Self {
Self { x: sz.width, y: sz.height }
}
}
-
impl<T: ValidPointType + Default> Default for Point_<T> {
fn default() -> Self {
Self { x: Default::default(), y: Default::default() }
}
}
+
+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;
+ }
+}

ADD
tests/point.rs
+use opencv::core::Point;
+
+#[test]
+fn point_add() {
+ let src = Point::new(0, 0);
+
+ let res = Point::new(50, 50);
+ {
+ let src = src.clone();
+ let out = src + Point::new(50, 50);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out += Point::new(50, 50);
+ assert_eq!(out, res);
+ }
+}
+
+#[test]
+fn point_sub() {
+ let src = Point::new(50, 50);
+
+ let res = Point::new(25, 25);
+ {
+ let src = src.clone();
+ let out = src - Point::new(25, 25);
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out -= Point::new(25, 25);
+ assert_eq!(out, res);
+ }
+}
+
+#[test]
+fn point_mul() {
+ let src = Point::new(50, 50);
+
+ let res = Point::new(100, 100);
+ {
+ let src = src.clone();
+ let out = src * 2;
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out *= 2;
+ assert_eq!(out, res);
+ }
+}
+
+#[test]
+fn point_div() {
+ let src = Point::new(50, 50);
+
+ let res = Point::new(25, 25);
+ {
+ let src = src.clone();
+ let out = src / 2;
+ assert_eq!(out, res);
+ }
+
+ {
+ let mut out = src.clone();
+ out /= 2;
+ assert_eq!(out, res);
+ }
+}
+
+#[test]
+fn point_methods() {
+ let pt = Point::new(2, 2);
+ assert_eq!(2.8284271247461903, pt.norm());
+}

add some Point_ methods and operators